kernel-language.js 13 KB

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