kernel-language.js 12 KB

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