kernel-language.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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-2017
  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' /* TODO remove */], 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 SmalltalkObject = brikz.root.Object;
  60. var setupMethods = brikz.composition.setupMethods;
  61. var traitMethodChanged = brikz.composition.traitMethodChanged;
  62. var buildBehaviorBody = brikz.behaviors.buildBehaviorBody;
  63. var addElement = brikz.arraySet.addElement;
  64. var removeElement = brikz.arraySet.removeElement;
  65. function SmalltalkTrait () {
  66. }
  67. coreFns.Trait = inherits(SmalltalkTrait, SmalltalkObject);
  68. SmalltalkTrait.prototype.trait = true;
  69. defineMethod(SmalltalkTrait, "toString", function () {
  70. return 'Smalltalk Trait ' + this.className;
  71. });
  72. defineMethod(SmalltalkTrait, "added", function () {
  73. if (st._traitAdded) st._traitAdded(this);
  74. });
  75. defineMethod(SmalltalkTrait, "removed", function () {
  76. if (st._traitRemoved) st._traitRemoved(this);
  77. });
  78. defineMethod(SmalltalkTrait, "methodAdded", function (method) {
  79. var self = this;
  80. this.traitUsers.forEach(function (each) {
  81. traitMethodChanged(method.selector, method, self, each);
  82. });
  83. if (st._traitMethodAdded) st._traitMethodAdded(method, this);
  84. });
  85. defineMethod(SmalltalkTrait, "methodRemoved", function (method) {
  86. var self = this;
  87. this.traitUsers.forEach(function (each) {
  88. traitMethodChanged(method.selector, null, self, each);
  89. });
  90. if (st._traitMethodRemoved) st._traitMethodRemoved(method, this);
  91. });
  92. defineMethod(SmalltalkTrait, "addUser", function (behaviorBody) {
  93. addElement(this.traitUsers, behaviorBody);
  94. });
  95. defineMethod(SmalltalkTrait, "removeUser", function (behaviorBody) {
  96. removeElement(this.traitUsers, behaviorBody);
  97. });
  98. function traitBuilder (className) {
  99. return {
  100. className: className,
  101. make: function (pkg) {
  102. var that = new SmalltalkTrait();
  103. that.className = className;
  104. that.pkg = pkg;
  105. that.traitUsers = [];
  106. setupMethods(that);
  107. return that;
  108. },
  109. updateExisting: function (trait, pkg) {
  110. if (pkg) trait.pkg = pkg;
  111. }
  112. };
  113. }
  114. st.addTrait = function (className, pkgName) {
  115. return buildBehaviorBody(pkgName, traitBuilder(className));
  116. };
  117. }
  118. ClassesBrik.deps = ["root", "behaviors", "composition", "arraySet", "smalltalkGlobals"];
  119. function ClassesBrik (brikz, st) {
  120. var SmalltalkRoot = brikz.root.Root;
  121. var coreFns = brikz.root.coreFns;
  122. var globals = brikz.smalltalkGlobals.globals;
  123. var SmalltalkObject = brikz.root.Object;
  124. var buildBehaviorBody = brikz.behaviors.buildBehaviorBody;
  125. var setupMethods = brikz.composition.setupMethods;
  126. var removeBehaviorBody = brikz.behaviors.removeBehaviorBody;
  127. var addElement = brikz.arraySet.addElement;
  128. var removeElement = brikz.arraySet.removeElement;
  129. function SmalltalkBehavior () {
  130. }
  131. function SmalltalkClass () {
  132. }
  133. function SmalltalkMetaclass () {
  134. }
  135. coreFns.Behavior = inherits(SmalltalkBehavior, SmalltalkObject);
  136. coreFns.Class = inherits(SmalltalkClass, SmalltalkBehavior);
  137. coreFns.Metaclass = inherits(SmalltalkMetaclass, SmalltalkBehavior);
  138. // Fake root class of the system.
  139. // Effective superclass of all classes created with `nil subclass: ...`.
  140. var nilAsClass = this.nilAsClass = {
  141. fn: SmalltalkRoot,
  142. a$cls: {fn: SmalltalkClass},
  143. klass: {fn: SmalltalkClass}
  144. };
  145. SmalltalkMetaclass.prototype.meta = true;
  146. defineMethod(SmalltalkClass, "toString", function () {
  147. return 'Smalltalk ' + this.className;
  148. });
  149. defineMethod(SmalltalkMetaclass, "toString", function () {
  150. return 'Smalltalk Metaclass ' + this.instanceClass.className;
  151. });
  152. defineMethod(SmalltalkClass, "added", function () {
  153. addSubclass(this);
  154. if (st._classAdded) st._classAdded(this);
  155. });
  156. defineMethod(SmalltalkClass, "removed", function () {
  157. if (st._classRemoved) st._classRemoved(this);
  158. removeSubclass(this);
  159. });
  160. defineMethod(SmalltalkBehavior, "methodAdded", function (method) {
  161. if (st._methodAdded) st._methodAdded(method, this);
  162. });
  163. defineMethod(SmalltalkBehavior, "methodRemoved", function (method) {
  164. if (st._methodRemoved) st._methodRemoved(method, this);
  165. });
  166. this.bootstrapHierarchy = function () {
  167. var nilSubclasses = [globals.ProtoObject];
  168. nilAsClass.a$cls = nilAsClass.klass = globals.Class;
  169. nilSubclasses.forEach(function (each) {
  170. each.a$cls.superclass = globals.Class;
  171. addSubclass(each.a$cls);
  172. });
  173. };
  174. /* Smalltalk class creation. A class is an instance of an automatically
  175. created metaclass object. Newly created classes (not their metaclass)
  176. should be added to the system, see smalltalk.addClass().
  177. Superclass linking is *not* handled here, see api.initialize() */
  178. function classBuilder (className, superclass, iVarNames, fn) {
  179. var logicalSuperclass = superclass;
  180. if (superclass == null || superclass.a$nil) {
  181. superclass = nilAsClass;
  182. logicalSuperclass = null;
  183. }
  184. function klass (pkg) {
  185. var that = metaclass().instanceClass;
  186. that.superclass = logicalSuperclass;
  187. that.fn = fn || inherits(function () {
  188. }, superclass.fn);
  189. that.iVarNames = iVarNames || [];
  190. that.className = className;
  191. that.pkg = pkg;
  192. that.subclasses = [];
  193. setupMethods(that);
  194. return that;
  195. }
  196. function metaclass () {
  197. var that = new SmalltalkMetaclass();
  198. that.superclass = superclass.a$cls;
  199. that.fn = inherits(function () {
  200. }, that.superclass.fn);
  201. that.iVarNames = [];
  202. that.instanceClass = new that.fn();
  203. wireKlass(that);
  204. setupMethods(that);
  205. return that;
  206. }
  207. return {
  208. className: className,
  209. make: klass,
  210. updateExisting: function (klass, pkg) {
  211. if (klass.superclass == logicalSuperclass && (!fn || fn === klass.fn)) {
  212. if (iVarNames) klass.iVarNames = iVarNames;
  213. if (pkg) klass.pkg = pkg;
  214. } else throw new Error("Incompatible change of class: " + klass.className);
  215. }
  216. };
  217. }
  218. function wireKlass (klass) {
  219. Object.defineProperty(klass.fn.prototype, "a$cls", {
  220. value: klass,
  221. enumerable: false, configurable: true, writable: true
  222. });
  223. Object.defineProperty(klass.fn.prototype, "klass", {
  224. value: klass,
  225. enumerable: false, configurable: true, writable: true
  226. });
  227. }
  228. this.wireKlass = wireKlass;
  229. /* Add a class to the system, creating a new one if needed.
  230. A Package is lazily created if one with given name does not exist. */
  231. st.addClass = function (className, superclass, iVarNames, pkgName) {
  232. // While subclassing nil is allowed, it might be an error, so
  233. // warn about it.
  234. if (typeof superclass === 'undefined' || superclass && superclass.a$nil) {
  235. console.warn('Compiling ' + className + ' as a subclass of `nil`. A dependency might be missing.');
  236. }
  237. return buildBehaviorBody(pkgName, classBuilder(className, superclass, iVarNames, coreFns[className]));
  238. };
  239. st.removeClass = removeBehaviorBody;
  240. function addSubclass (klass) {
  241. if (klass.superclass) {
  242. addElement(klass.superclass.subclasses, klass);
  243. }
  244. }
  245. function removeSubclass (klass) {
  246. if (klass.superclass) {
  247. removeElement(klass.superclass.subclasses, klass);
  248. }
  249. }
  250. function metaSubclasses (metaclass) {
  251. return metaclass.instanceClass.subclasses
  252. .filter(function (each) {
  253. return !each.meta;
  254. })
  255. .map(function (each) {
  256. return each.a$cls;
  257. });
  258. }
  259. st.metaSubclasses = metaSubclasses;
  260. st.traverseClassTree = function (klass, fn) {
  261. var queue = [klass], sentinel = {};
  262. for (var i = 0; i < queue.length; ++i) {
  263. var item = queue[i];
  264. if (fn(item, sentinel) === sentinel) continue;
  265. var subclasses = item.meta ? metaSubclasses(item) : item.subclasses;
  266. queue.push.apply(queue, subclasses);
  267. }
  268. };
  269. }
  270. NilBrik.deps = ["root"];
  271. function NilBrik (brikz, st) {
  272. var SmalltalkObject = brikz.root.Object;
  273. var coreFns = brikz.root.coreFns;
  274. function SmalltalkNil () {
  275. }
  276. coreFns.UndefinedObject = inherits(SmalltalkNil, SmalltalkObject);
  277. this.nilAsReceiver = new SmalltalkNil();
  278. this.nilAsValue = this.nilAsReceiver; // TODO null
  279. // Adds an `a$nil` (and legacy `isNil`) property to the `nil` object. When sending
  280. // nil objects from one environment to another, doing
  281. // `anObject == nil` (in JavaScript) does not always answer
  282. // true as the referenced nil object might come from the other
  283. // environment.
  284. Object.defineProperty(this.nilAsReceiver, 'a$nil', {
  285. value: true,
  286. enumerable: false, configurable: false, writable: false
  287. });
  288. Object.defineProperty(this.nilAsReceiver, 'isNil', {
  289. value: true,
  290. enumerable: false, configurable: false, writable: false
  291. });
  292. }
  293. /* Defines asReceiver to be present at load time */
  294. /* (logically it belongs more to PrimitiveBrik) */
  295. AsReceiverBrik.deps = ["nil"];
  296. function AsReceiverBrik (brikz, st) {
  297. var nilAsReceiver = brikz.nil.nilAsReceiver;
  298. /**
  299. * This function is used all over the compiled amber code.
  300. * It takes any value (JavaScript or Smalltalk)
  301. * and returns a proper Amber Smalltalk receiver.
  302. *
  303. * null or undefined -> nilAsReceiver,
  304. * object having Smalltalk signature -> unchanged,
  305. * otherwise wrapped foreign (JS) object
  306. */
  307. this.asReceiver = function (o) {
  308. if (o == null) return nilAsReceiver;
  309. else if (o.a$cls != null) return o;
  310. else return st.wrapJavaScript(o);
  311. };
  312. }
  313. /* Making smalltalk that can load */
  314. function configureWithHierarchy (brikz) {
  315. brikz.traits = TraitsBrik;
  316. brikz.classes = ClassesBrik;
  317. brikz.nil = NilBrik;
  318. brikz.asReceiver = AsReceiverBrik;
  319. brikz.rebuild();
  320. }
  321. return configureWithHierarchy;
  322. });