kernel-language.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. }, spec.superclass.fn);
  173. that.iVarNames = spec.iVarNames || [];
  174. that.className = spec.className;
  175. that.pkg = spec.pkg;
  176. that.subclasses = [];
  177. setupBehavior(that);
  178. return that;
  179. }
  180. function metaclass (spec) {
  181. var that = new SmalltalkMetaclass();
  182. that.superclass = spec.superclass.klass;
  183. that.fn = inherits(function () {
  184. }, that.superclass.fn);
  185. that.iVarNames = [];
  186. that.instanceClass = new that.fn();
  187. wireKlass(that);
  188. setupBehavior(that);
  189. return that;
  190. }
  191. function wireKlass (klass) {
  192. Object.defineProperty(klass.fn.prototype, "klass", {
  193. value: klass,
  194. enumerable: false, configurable: true, writable: true
  195. });
  196. }
  197. this.wireKlass = wireKlass;
  198. /* Add a class to the system, creating a new one if needed.
  199. A Package is lazily created if one with given name does not exist. */
  200. st.addClass = function (className, superclass, iVarNames, pkgName) {
  201. // While subclassing nil is allowed, it might be an error, so
  202. // warn about it.
  203. if (typeof superclass == 'undefined' || superclass && superclass.isNil) {
  204. console.warn('Compiling ' + className + ' as a subclass of `nil`. A dependency might be missing.');
  205. }
  206. return buildBehaviorBody(pkgName, classBuilder(className, superclass, iVarNames, coreFns[className]));
  207. };
  208. function classBuilder (className, superclass, iVarNames, fn) {
  209. if (superclass == null || superclass.isNil) {
  210. superclass = null;
  211. }
  212. return {
  213. className: className,
  214. make: function (pkg) {
  215. return klass({
  216. className: className,
  217. pkg: pkg,
  218. superclass: superclass,
  219. iVarNames: iVarNames,
  220. fn: fn
  221. });
  222. },
  223. updateExisting: function (klass, pkg) {
  224. if (klass.superclass == superclass && !fn) {
  225. if (iVarNames) klass.iVarNames = iVarNames;
  226. if (pkg) klass.pkg = pkg;
  227. return true;
  228. }
  229. return false;
  230. },
  231. rebuilderForExisting: function (klass) {
  232. return classBuilder(className, superclass, iVarNames || klass.iVarNames, fn);
  233. }
  234. };
  235. }
  236. st.removeClass = removeBehaviorBody;
  237. function addSubclass (klass) {
  238. if (klass.superclass) {
  239. addElement(klass.superclass.subclasses, klass);
  240. }
  241. }
  242. function removeSubclass (klass) {
  243. if (klass.superclass) {
  244. removeElement(klass.superclass.subclasses, klass);
  245. }
  246. }
  247. function metaSubclasses (metaclass) {
  248. return metaclass.instanceClass.subclasses
  249. .filter(function (each) {
  250. return !each.meta;
  251. })
  252. .map(function (each) {
  253. return each.klass;
  254. });
  255. }
  256. st.metaSubclasses = metaSubclasses;
  257. st.traverseClassTree = function (klass, fn) {
  258. var queue = [klass], sentinel = {};
  259. for (var i = 0; i < queue.length; ++i) {
  260. var item = queue[i];
  261. if (fn(item, sentinel) === sentinel) continue;
  262. var subclasses = item.meta ? metaSubclasses(item) : item.subclasses;
  263. queue.push.apply(queue, subclasses);
  264. }
  265. };
  266. }
  267. NilBrik.deps = ["root"];
  268. function NilBrik (brikz, st) {
  269. var SmalltalkObject = brikz.root.Object;
  270. var coreFns = brikz.root.coreFns;
  271. function SmalltalkNil () {
  272. }
  273. coreFns.UndefinedObject = inherits(SmalltalkNil, SmalltalkObject);
  274. this.nilAsReceiver = new SmalltalkNil();
  275. // Adds an `isNil` property to the `nil` object. When sending
  276. // nil objects from one environment to another, doing
  277. // `anObject == nil` (in JavaScript) does not always answer
  278. // true as the referenced nil object might come from the other
  279. // environment.
  280. Object.defineProperty(this.nilAsReceiver, 'isNil', {
  281. value: true,
  282. enumerable: false, configurable: false, writable: false
  283. });
  284. }
  285. /* Defines asReceiver to be present at load time */
  286. /* (logically it belongs more to PrimitiveBrik) */
  287. AsReceiverBrik.deps = ["nil"];
  288. function AsReceiverBrik (brikz, st) {
  289. var nilAsReceiver = brikz.nil.nilAsReceiver;
  290. /**
  291. * This function is used all over the compiled amber code.
  292. * It takes any value (JavaScript or Smalltalk)
  293. * and returns a proper Amber Smalltalk receiver.
  294. *
  295. * null or undefined -> nilAsReceiver,
  296. * object having Smalltalk signature -> unchanged,
  297. * otherwise wrapped foreign (JS) object
  298. */
  299. this.asReceiver = function (o) {
  300. if (o == null) return nilAsReceiver;
  301. else if (o.klass != null) return o;
  302. else return st.wrapJavaScript(o);
  303. };
  304. }
  305. /* Making smalltalk that can load */
  306. function configureWithHierarchy (brikz) {
  307. brikz.traits = TraitsBrik;
  308. brikz.classes = ClassesBrik;
  309. brikz.nil = NilBrik;
  310. brikz.asReceiver = AsReceiverBrik;
  311. brikz.rebuild();
  312. }
  313. return configureWithHierarchy;
  314. });