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