kernel-language.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* ====================================================================
  2. |
  3. | Amber Smalltalk
  4. | http://amber-lang.net
  5. |
  6. ======================================================================
  7. ======================================================================
  8. |
  9. | Copyright (c) 2010-2014
  10. | Nicolas Petton <petton.nicolas@gmail.com>
  11. |
  12. | Copyright (c) 2012-2016
  13. | The Amber team https://lolg.it/org/amber/members
  14. | Amber contributors (see /CONTRIBUTORS)
  15. |
  16. | Amber is released under the MIT license
  17. |
  18. | Permission is hereby granted, free of charge, to any person obtaining
  19. | a copy of this software and associated documentation files (the
  20. | 'Software'), to deal in the Software without restriction, including
  21. | without limitation the rights to use, copy, modify, merge, publish,
  22. | distribute, sublicense, and/or sell copies of the Software, and to
  23. | permit persons to whom the Software is furnished to do so, subject to
  24. | the following conditions:
  25. |
  26. | The above copyright notice and this permission notice shall be
  27. | included in all copies or substantial portions of the Software.
  28. |
  29. | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  30. | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  33. | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  34. | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  35. | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. |
  37. ==================================================================== */
  38. //jshint eqnull:true
  39. define(['./compatibility'], function () {
  40. "use strict";
  41. function inherits (child, parent) {
  42. child.prototype = Object.create(parent.prototype, {
  43. constructor: {
  44. value: child,
  45. enumerable: false, configurable: true, writable: true
  46. }
  47. });
  48. return child;
  49. }
  50. function defineMethod (klass, name, method) {
  51. Object.defineProperty(klass.prototype, name, {
  52. value: method,
  53. enumerable: false, configurable: true, writable: true
  54. });
  55. }
  56. TraitsBrik.deps = ["behaviors", "composition", "arraySet", "root"];
  57. function TraitsBrik (brikz, st) {
  58. var coreFns = brikz.root.coreFns;
  59. var SmalltalkBehaviorBody = brikz.behaviors.BehaviorBody;
  60. var setupMethods = brikz.composition.setupMethods;
  61. var buildBehaviorBody = brikz.behaviors.buildBehaviorBody;
  62. var addElement = brikz.arraySet.addElement;
  63. var removeElement = brikz.arraySet.removeElement;
  64. function SmalltalkTrait () {
  65. }
  66. coreFns.Trait = inherits(SmalltalkTrait, SmalltalkBehaviorBody);
  67. SmalltalkTrait.prototype.trait = true;
  68. defineMethod(SmalltalkTrait, "toString", function () {
  69. return 'Smalltalk Trait ' + this.className;
  70. });
  71. defineMethod(SmalltalkTrait, "added", function () {
  72. if (st._traitAdded) st._traitAdded(this);
  73. });
  74. defineMethod(SmalltalkTrait, "removed", function () {
  75. if (st._traitRemoved) st._traitRemoved(this);
  76. });
  77. defineMethod(SmalltalkTrait, "methodAdded", function (method) {
  78. if (st._traitMethodAdded) st._traitMethodAdded(method, this);
  79. });
  80. defineMethod(SmalltalkTrait, "methodRemoved", function (method) {
  81. if (st._traitMethodRemoved) st._traitMethodRemoved(method, this);
  82. });
  83. defineMethod(SmalltalkTrait, "addUser", function (behaviorBody) {
  84. addElement(this.traitUsers, behaviorBody);
  85. });
  86. defineMethod(SmalltalkTrait, "removeUser", function (behaviorBody) {
  87. removeElement(this.traitUsers, behaviorBody);
  88. });
  89. function traitBuilder (className) {
  90. return {
  91. className: className,
  92. make: function (pkg) {
  93. var that = new SmalltalkTrait();
  94. that.className = className;
  95. that.pkg = pkg;
  96. that.traitUsers = [];
  97. setupMethods(that);
  98. return that;
  99. },
  100. updateExisting: function (trait, pkg) {
  101. if (pkg) trait.pkg = pkg;
  102. }
  103. };
  104. }
  105. st.addTrait = function (className, pkgName) {
  106. return buildBehaviorBody(pkgName, traitBuilder(className));
  107. };
  108. }
  109. ClassesBrik.deps = ["root", "behaviors", "composition", "arraySet", "smalltalkGlobals"];
  110. function ClassesBrik (brikz, st) {
  111. var SmalltalkRoot = brikz.root.Root;
  112. var coreFns = brikz.root.coreFns;
  113. var globals = brikz.smalltalkGlobals.globals;
  114. var SmalltalkBehaviorBody = brikz.behaviors.BehaviorBody;
  115. var buildBehaviorBody = brikz.behaviors.buildBehaviorBody;
  116. var setupMethods = brikz.composition.setupMethods;
  117. var removeBehaviorBody = brikz.behaviors.removeBehaviorBody;
  118. var addElement = brikz.arraySet.addElement;
  119. var removeElement = brikz.arraySet.removeElement;
  120. function SmalltalkBehavior () {
  121. }
  122. function SmalltalkClass () {
  123. }
  124. function SmalltalkMetaclass () {
  125. }
  126. coreFns.Behavior = inherits(SmalltalkBehavior, SmalltalkBehaviorBody);
  127. coreFns.Class = inherits(SmalltalkClass, SmalltalkBehavior);
  128. coreFns.Metaclass = inherits(SmalltalkMetaclass, SmalltalkBehavior);
  129. // Fake root class of the system.
  130. // Effective superclass of all classes created with `nil subclass: ...`.
  131. var nilAsClass = this.nilAsClass = {fn: SmalltalkRoot, klass: {fn: SmalltalkClass}};
  132. SmalltalkMetaclass.prototype.meta = true;
  133. defineMethod(SmalltalkClass, "toString", function () {
  134. return 'Smalltalk ' + this.className;
  135. });
  136. defineMethod(SmalltalkMetaclass, "toString", function () {
  137. return 'Smalltalk Metaclass ' + this.instanceClass.className;
  138. });
  139. defineMethod(SmalltalkClass, "added", function () {
  140. addSubclass(this);
  141. if (st._classAdded) st._classAdded(this);
  142. });
  143. defineMethod(SmalltalkClass, "removed", function () {
  144. if (st._classRemoved) st._classRemoved(this);
  145. removeSubclass(this);
  146. });
  147. defineMethod(SmalltalkBehavior, "methodAdded", function (method) {
  148. if (st._methodAdded) st._methodAdded(method, this);
  149. });
  150. defineMethod(SmalltalkBehavior, "methodRemoved", function (method) {
  151. if (st._methodRemoved) st._methodRemoved(method, this);
  152. });
  153. this.bootstrapHierarchy = function () {
  154. var nilSubclasses = [globals.ProtoObject];
  155. nilAsClass.klass = globals.Class;
  156. nilSubclasses.forEach(function (each) {
  157. each.klass.superclass = globals.Class;
  158. addSubclass(each.klass);
  159. });
  160. };
  161. /* Smalltalk class creation. A class is an instance of an automatically
  162. created metaclass object. Newly created classes (not their metaclass)
  163. should be added to the system, see smalltalk.addClass().
  164. Superclass linking is *not* handled here, see api.initialize() */
  165. function classBuilder (className, superclass, iVarNames, fn) {
  166. var logicalSuperclass = superclass;
  167. if (superclass == null || superclass.isNil) {
  168. superclass = nilAsClass;
  169. logicalSuperclass = null;
  170. }
  171. function klass (pkg) {
  172. var that = metaclass().instanceClass;
  173. that.superclass = logicalSuperclass;
  174. that.fn = fn || inherits(function () {
  175. }, superclass.fn);
  176. that.iVarNames = iVarNames || [];
  177. that.className = className;
  178. that.pkg = pkg;
  179. that.subclasses = [];
  180. setupMethods(that);
  181. return that;
  182. }
  183. function metaclass () {
  184. var that = new SmalltalkMetaclass();
  185. that.superclass = superclass.klass;
  186. that.fn = inherits(function () {
  187. }, that.superclass.fn);
  188. that.iVarNames = [];
  189. that.instanceClass = new that.fn();
  190. wireKlass(that);
  191. setupMethods(that);
  192. return that;
  193. }
  194. return {
  195. className: className,
  196. make: klass,
  197. updateExisting: function (klass, pkg) {
  198. if (klass.superclass == logicalSuperclass && (!fn || fn === klass.fn)) {
  199. if (iVarNames) klass.iVarNames = iVarNames;
  200. if (pkg) klass.pkg = pkg;
  201. } else throw new Error("Incompatible change of class: " + klass.className);
  202. }
  203. };
  204. }
  205. function wireKlass (klass) {
  206. Object.defineProperty(klass.fn.prototype, "klass", {
  207. value: klass,
  208. enumerable: false, configurable: true, writable: true
  209. });
  210. }
  211. this.wireKlass = wireKlass;
  212. /* Add a class to the system, creating a new one if needed.
  213. A Package is lazily created if one with given name does not exist. */
  214. st.addClass = function (className, superclass, iVarNames, pkgName) {
  215. // While subclassing nil is allowed, it might be an error, so
  216. // warn about it.
  217. if (typeof superclass == 'undefined' || superclass && superclass.isNil) {
  218. console.warn('Compiling ' + className + ' as a subclass of `nil`. A dependency might be missing.');
  219. }
  220. return buildBehaviorBody(pkgName, classBuilder(className, superclass, iVarNames, coreFns[className]));
  221. };
  222. st.removeClass = removeBehaviorBody;
  223. function addSubclass (klass) {
  224. if (klass.superclass) {
  225. addElement(klass.superclass.subclasses, klass);
  226. }
  227. }
  228. function removeSubclass (klass) {
  229. if (klass.superclass) {
  230. removeElement(klass.superclass.subclasses, klass);
  231. }
  232. }
  233. function metaSubclasses (metaclass) {
  234. return metaclass.instanceClass.subclasses
  235. .filter(function (each) {
  236. return !each.meta;
  237. })
  238. .map(function (each) {
  239. return each.klass;
  240. });
  241. }
  242. st.metaSubclasses = metaSubclasses;
  243. st.traverseClassTree = function (klass, fn) {
  244. var queue = [klass], sentinel = {};
  245. for (var i = 0; i < queue.length; ++i) {
  246. var item = queue[i];
  247. if (fn(item, sentinel) === sentinel) continue;
  248. var subclasses = item.meta ? metaSubclasses(item) : item.subclasses;
  249. queue.push.apply(queue, subclasses);
  250. }
  251. };
  252. }
  253. NilBrik.deps = ["root"];
  254. function NilBrik (brikz, st) {
  255. var SmalltalkObject = brikz.root.Object;
  256. var coreFns = brikz.root.coreFns;
  257. function SmalltalkNil () {
  258. }
  259. coreFns.UndefinedObject = inherits(SmalltalkNil, SmalltalkObject);
  260. this.nilAsReceiver = new SmalltalkNil();
  261. // Adds an `isNil` property to the `nil` object. When sending
  262. // nil objects from one environment to another, doing
  263. // `anObject == nil` (in JavaScript) does not always answer
  264. // true as the referenced nil object might come from the other
  265. // environment.
  266. Object.defineProperty(this.nilAsReceiver, 'isNil', {
  267. value: true,
  268. enumerable: false, configurable: false, writable: false
  269. });
  270. }
  271. /* Defines asReceiver to be present at load time */
  272. /* (logically it belongs more to PrimitiveBrik) */
  273. AsReceiverBrik.deps = ["nil"];
  274. function AsReceiverBrik (brikz, st) {
  275. var nilAsReceiver = brikz.nil.nilAsReceiver;
  276. /**
  277. * This function is used all over the compiled amber code.
  278. * It takes any value (JavaScript or Smalltalk)
  279. * and returns a proper Amber Smalltalk receiver.
  280. *
  281. * null or undefined -> nilAsReceiver,
  282. * object having Smalltalk signature -> unchanged,
  283. * otherwise wrapped foreign (JS) object
  284. */
  285. this.asReceiver = function (o) {
  286. if (o == null) return nilAsReceiver;
  287. else if (o.klass != null) return o;
  288. else return st.wrapJavaScript(o);
  289. };
  290. }
  291. /* Making smalltalk that can load */
  292. function configureWithHierarchy (brikz) {
  293. brikz.traits = TraitsBrik;
  294. brikz.classes = ClassesBrik;
  295. brikz.nil = NilBrik;
  296. brikz.asReceiver = AsReceiverBrik;
  297. brikz.rebuild();
  298. }
  299. return configureWithHierarchy;
  300. });