1
0

kernel-language.js 13 KB

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