kernel-language.js 13 KB

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