1
0

kernel-language.js 18 KB

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