1
0

kernel-language.js 17 KB

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