1
0

kernel-language.js 13 KB

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