1
0

kernel-language.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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", "behaviorProviders", "composition", "arraySet", "root"];
  57. function TraitsBrik (brikz, st) {
  58. var coreFns = brikz.root.coreFns;
  59. var SmalltalkObject = brikz.root.Object;
  60. var setupMethods = brikz.behaviorProviders.setupMethods;
  61. var traitMethodChanged = brikz.composition.traitMethodChanged;
  62. var buildTraitOrClass = brikz.behaviors.buildTraitOrClass;
  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 (traitOrBehavior) {
  93. addElement(this.traitUsers, traitOrBehavior);
  94. });
  95. defineMethod(SmalltalkTrait, "removeUser", function (traitOrBehavior) {
  96. removeElement(this.traitUsers, traitOrBehavior);
  97. });
  98. function traitBuilder (className) {
  99. return {
  100. className: className,
  101. make: function () {
  102. var that = new SmalltalkTrait();
  103. that.className = className;
  104. that.traitUsers = [];
  105. setupMethods(that);
  106. return that;
  107. },
  108. updateExisting: function (trait) {
  109. }
  110. };
  111. }
  112. st.addTrait = function (className, pkgName) {
  113. return buildTraitOrClass(pkgName, traitBuilder(className));
  114. };
  115. }
  116. MethodCompositionBrik.deps = ["behaviorProviders"];
  117. function MethodCompositionBrik (brikz, st) {
  118. var updateMethod = brikz.behaviorProviders.updateMethod;
  119. function aliased (selector, method) {
  120. if (method.selector === selector) return method;
  121. var result = st.method({
  122. selector: selector,
  123. args: method.args,
  124. protocol: method.protocol,
  125. source: '"Aliased as ' + selector + '"\n' + method.source,
  126. messageSends: method.messageSends,
  127. referencesClasses: method.referencedClasses,
  128. fn: method.fn
  129. });
  130. result.methodClass = method.methodClass;
  131. return result;
  132. }
  133. function deleteKeysFrom (keys, obj) {
  134. keys.forEach(function (each) {
  135. delete obj[each];
  136. });
  137. }
  138. function fillTraitTransformation (traitTransformation, obj) {
  139. // assert(Object.getOwnProperties(obj).length === 0)
  140. var traitMethods = traitTransformation.trait.methods;
  141. Object.keys(traitMethods).forEach(function (selector) {
  142. obj[selector] = traitMethods[selector];
  143. });
  144. var traitAliases = traitTransformation.aliases;
  145. if (traitAliases) {
  146. Object.keys(traitAliases).forEach(function (aliasSelector) {
  147. var aliasedMethod = traitMethods[traitAliases[aliasSelector]];
  148. if (aliasedMethod) obj[aliasSelector] = aliased(aliasSelector, aliasedMethod);
  149. // else delete obj[aliasSelector]; // semantically correct; optimized away
  150. });
  151. }
  152. var traitExclusions = traitTransformation.exclusions;
  153. if (traitExclusions) {
  154. deleteKeysFrom(traitExclusions, obj);
  155. }
  156. return obj;
  157. }
  158. function buildCompositionChain (traitComposition) {
  159. return traitComposition.reduce(function (soFar, each) {
  160. return fillTraitTransformation(each, Object.create(soFar));
  161. }, null);
  162. }
  163. st.setTraitComposition = function (traitComposition, traitOrBehavior) {
  164. var oldLocalMethods = traitOrBehavior.localMethods,
  165. newLocalMethods = Object.create(buildCompositionChain(traitComposition));
  166. Object.keys(oldLocalMethods).forEach(function (selector) {
  167. newLocalMethods[selector] = oldLocalMethods[selector];
  168. });
  169. var selector;
  170. traitOrBehavior.localMethods = newLocalMethods;
  171. for (selector in newLocalMethods) {
  172. updateMethod(selector, traitOrBehavior);
  173. }
  174. for (selector in oldLocalMethods) {
  175. updateMethod(selector, traitOrBehavior);
  176. }
  177. (traitOrBehavior.traitComposition || []).forEach(function (each) {
  178. each.trait.removeUser(traitOrBehavior);
  179. });
  180. traitOrBehavior.traitComposition = traitComposition && traitComposition.length ? traitComposition : null;
  181. (traitOrBehavior.traitComposition || []).forEach(function (each) {
  182. each.trait.addUser(traitOrBehavior);
  183. });
  184. };
  185. function aliasesOfSelector (selector, traitAliases) {
  186. if (!traitAliases) return [selector];
  187. var result = Object.keys(traitAliases).filter(function (aliasSelector) {
  188. return traitAliases[aliasSelector] === selector
  189. });
  190. if (!traitAliases[selector]) result.push(selector);
  191. return result;
  192. }
  193. function applyTraitMethodAddition (selector, method, traitTransformation, obj) {
  194. var changes = aliasesOfSelector(selector, traitTransformation.aliases);
  195. changes.forEach(function (aliasSelector) {
  196. obj[aliasSelector] = aliased(aliasSelector, method);
  197. });
  198. var traitExclusions = traitTransformation.exclusions;
  199. if (traitExclusions) {
  200. deleteKeysFrom(traitExclusions, obj);
  201. }
  202. return changes;
  203. }
  204. function applyTraitMethodDeletion (selector, traitTransformation, obj) {
  205. var changes = aliasesOfSelector(selector, traitTransformation.aliases);
  206. deleteKeysFrom(changes, obj);
  207. return changes;
  208. }
  209. function traitMethodChanged (selector, method, trait, traitOrBehavior) {
  210. var traitComposition = traitOrBehavior.traitComposition,
  211. chain = traitOrBehavior.localMethods,
  212. changes = [];
  213. for (var i = traitComposition.length - 1; i >= 0; --i) {
  214. chain = Object.getPrototypeOf(chain);
  215. var traitTransformation = traitComposition[i];
  216. if (traitTransformation.trait !== trait) continue;
  217. changes.push.apply(changes, method ?
  218. applyTraitMethodAddition(selector, method, traitTransformation, chain) :
  219. applyTraitMethodDeletion(selector, traitTransformation, chain));
  220. }
  221. // assert(chain === null);
  222. changes.forEach(function (each) {
  223. updateMethod(each, traitOrBehavior);
  224. });
  225. }
  226. this.traitMethodChanged = traitMethodChanged;
  227. }
  228. ClassesBrik.deps = ["root", "behaviors", "behaviorProviders", "arraySet", "smalltalkGlobals"];
  229. function ClassesBrik (brikz, st) {
  230. var SmalltalkRoot = brikz.root.Root;
  231. var coreFns = brikz.root.coreFns;
  232. var globals = brikz.smalltalkGlobals.globals;
  233. var SmalltalkObject = brikz.root.Object;
  234. var buildTraitOrClass = brikz.behaviors.buildTraitOrClass;
  235. var setupMethods = brikz.behaviorProviders.setupMethods;
  236. var removeTraitOrClass = brikz.behaviors.removeTraitOrClass;
  237. var addElement = brikz.arraySet.addElement;
  238. var removeElement = brikz.arraySet.removeElement;
  239. function SmalltalkBehavior () {
  240. }
  241. function SmalltalkClass () {
  242. }
  243. function SmalltalkMetaclass () {
  244. }
  245. coreFns.Behavior = inherits(SmalltalkBehavior, SmalltalkObject);
  246. coreFns.Class = inherits(SmalltalkClass, SmalltalkBehavior);
  247. coreFns.Metaclass = inherits(SmalltalkMetaclass, SmalltalkBehavior);
  248. // Fake root class of the system.
  249. // Effective superclass of all classes created with `nil subclass: ...`.
  250. var nilAsClass = this.nilAsClass = {
  251. fn: SmalltalkRoot,
  252. a$cls: {fn: SmalltalkClass},
  253. klass: {fn: SmalltalkClass}
  254. };
  255. SmalltalkMetaclass.prototype.meta = true;
  256. defineMethod(SmalltalkClass, "toString", function () {
  257. return 'Smalltalk ' + this.className;
  258. });
  259. defineMethod(SmalltalkMetaclass, "toString", function () {
  260. return 'Smalltalk Metaclass ' + this.instanceClass.className;
  261. });
  262. defineMethod(SmalltalkClass, "added", function () {
  263. addSubclass(this);
  264. if (st._classAdded) st._classAdded(this);
  265. });
  266. defineMethod(SmalltalkClass, "removed", function () {
  267. if (st._classRemoved) st._classRemoved(this);
  268. removeSubclass(this);
  269. });
  270. defineMethod(SmalltalkBehavior, "methodAdded", function (method) {
  271. if (st._behaviorMethodAdded) st._behaviorMethodAdded(method, this);
  272. });
  273. defineMethod(SmalltalkBehavior, "methodRemoved", function (method) {
  274. if (st._behaviorMethodRemoved) st._behaviorMethodRemoved(method, this);
  275. });
  276. this.bootstrapHierarchy = function () {
  277. var nilSubclasses = [globals.ProtoObject];
  278. nilAsClass.a$cls = nilAsClass.klass = globals.Class;
  279. nilSubclasses.forEach(function (each) {
  280. each.a$cls.superclass = globals.Class;
  281. addSubclass(each.a$cls);
  282. });
  283. };
  284. /* Smalltalk class creation. A class is an instance of an automatically
  285. created metaclass object. Newly created classes (not their metaclass)
  286. should be added to the system, see smalltalk.addClass().
  287. Superclass linking is *not* handled here, see api.initialize() */
  288. function classBuilder (className, superclass, iVarNames, fn) {
  289. var logicalSuperclass = superclass;
  290. if (superclass == null || superclass.a$nil) {
  291. superclass = nilAsClass;
  292. logicalSuperclass = null;
  293. }
  294. function klass () {
  295. var that = metaclass().instanceClass;
  296. that.superclass = logicalSuperclass;
  297. that.fn = fn || inherits(function () {
  298. }, superclass.fn);
  299. that.iVarNames = iVarNames || [];
  300. that.className = className;
  301. that.subclasses = [];
  302. setupMethods(that);
  303. return that;
  304. }
  305. function metaclass () {
  306. var that = new SmalltalkMetaclass();
  307. that.superclass = superclass.a$cls;
  308. that.fn = inherits(function () {
  309. }, that.superclass.fn);
  310. that.iVarNames = [];
  311. that.instanceClass = new that.fn();
  312. wireKlass(that);
  313. setupMethods(that);
  314. return that;
  315. }
  316. return {
  317. className: className,
  318. make: klass,
  319. updateExisting: function (klass) {
  320. if (klass.superclass == logicalSuperclass && (!fn || fn === klass.fn)) {
  321. if (iVarNames) klass.iVarNames = iVarNames;
  322. } else throw new Error("Incompatible change of class: " + klass.className);
  323. }
  324. };
  325. }
  326. function wireKlass (klass) {
  327. Object.defineProperty(klass.fn.prototype, "a$cls", {
  328. value: klass,
  329. enumerable: false, configurable: true, writable: true
  330. });
  331. Object.defineProperty(klass.fn.prototype, "klass", {
  332. value: klass,
  333. enumerable: false, configurable: true, writable: true
  334. });
  335. }
  336. this.wireKlass = wireKlass;
  337. /* Add a class to the system, creating a new one if needed.
  338. A Package is lazily created if one with given name does not exist. */
  339. st.addClass = function (className, superclass, iVarNames, pkgName) {
  340. // While subclassing nil is allowed, it might be an error, so
  341. // warn about it.
  342. if (typeof superclass === 'undefined' || superclass && superclass.a$nil) {
  343. console.warn('Compiling ' + className + ' as a subclass of `nil`. A dependency might be missing.');
  344. }
  345. return buildTraitOrClass(pkgName, classBuilder(className, superclass, iVarNames, coreFns[className]));
  346. };
  347. st.removeClass = removeTraitOrClass;
  348. function addSubclass (klass) {
  349. if (klass.superclass) {
  350. addElement(klass.superclass.subclasses, klass);
  351. }
  352. }
  353. function removeSubclass (klass) {
  354. if (klass.superclass) {
  355. removeElement(klass.superclass.subclasses, klass);
  356. }
  357. }
  358. function metaSubclasses (metaclass) {
  359. return metaclass.instanceClass.subclasses
  360. .filter(function (each) {
  361. return !each.meta;
  362. })
  363. .map(function (each) {
  364. return each.a$cls;
  365. });
  366. }
  367. st.metaSubclasses = metaSubclasses;
  368. st.traverseClassTree = function (klass, fn) {
  369. var queue = [klass], sentinel = {};
  370. for (var i = 0; i < queue.length; ++i) {
  371. var item = queue[i];
  372. if (fn(item, sentinel) === sentinel) continue;
  373. var subclasses = item.meta ? metaSubclasses(item) : item.subclasses;
  374. queue.push.apply(queue, subclasses);
  375. }
  376. };
  377. }
  378. NilBrik.deps = ["root"];
  379. function NilBrik (brikz, st) {
  380. var SmalltalkObject = brikz.root.Object;
  381. var coreFns = brikz.root.coreFns;
  382. function SmalltalkNil () {
  383. }
  384. coreFns.UndefinedObject = inherits(SmalltalkNil, SmalltalkObject);
  385. this.nilAsReceiver = new SmalltalkNil();
  386. this.nilAsValue = this.nilAsReceiver; // TODO null
  387. // Adds an `a$nil` (and legacy `isNil`) property to the `nil` object. When sending
  388. // nil objects from one environment to another, doing
  389. // `anObject == nil` (in JavaScript) does not always answer
  390. // true as the referenced nil object might come from the other
  391. // environment.
  392. Object.defineProperty(this.nilAsReceiver, 'a$nil', {
  393. value: true,
  394. enumerable: false, configurable: false, writable: false
  395. });
  396. Object.defineProperty(this.nilAsReceiver, 'isNil', {
  397. value: true,
  398. enumerable: false, configurable: false, writable: false
  399. });
  400. }
  401. /* Defines asReceiver to be present at load time */
  402. /* (logically it belongs more to PrimitiveBrik) */
  403. AsReceiverBrik.deps = ["nil"];
  404. function AsReceiverBrik (brikz, st) {
  405. var nilAsReceiver = brikz.nil.nilAsReceiver;
  406. /**
  407. * This function is used all over the compiled amber code.
  408. * It takes any value (JavaScript or Smalltalk)
  409. * and returns a proper Amber Smalltalk receiver.
  410. *
  411. * null or undefined -> nilAsReceiver,
  412. * object having Smalltalk signature -> unchanged,
  413. * otherwise wrapped foreign (JS) object
  414. */
  415. this.asReceiver = function (o) {
  416. if (o == null) return nilAsReceiver;
  417. else if (o.a$cls != null) return o;
  418. else return st.wrapJavaScript(o);
  419. };
  420. }
  421. /* Making smalltalk that can load */
  422. function configureWithHierarchy (brikz) {
  423. brikz.traits = TraitsBrik;
  424. brikz.composition = MethodCompositionBrik;
  425. brikz.classes = ClassesBrik;
  426. brikz.nil = NilBrik;
  427. brikz.asReceiver = AsReceiverBrik;
  428. brikz.rebuild();
  429. }
  430. return configureWithHierarchy;
  431. });