1
0

boot.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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(['require', './brikz', './compatibility'], function (require, Brikz) {
  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 SmalltalkGlobalsBrik (brikz, st) {
  51. // jshint evil:true
  52. var jsGlobals = new Function("return this")();
  53. var globals = Object.create(jsGlobals);
  54. globals.SmalltalkSettings = {};
  55. this.globals = globals;
  56. }
  57. function RootBrik (brikz, st) {
  58. /* Smalltalk foundational objects */
  59. /* SmalltalkRoot is the hidden root of the normal Amber hierarchy.
  60. All objects including `ProtoObject` inherit from SmalltalkRoot.
  61. Detached roots (eg. wrapped JS classes like Number or Date)
  62. do not directly inherit from SmalltalkRoot, but employ a workaround.*/
  63. function SmalltalkRoot () {
  64. }
  65. function SmalltalkProtoObject () {
  66. }
  67. function SmalltalkObject () {
  68. }
  69. inherits(SmalltalkProtoObject, SmalltalkRoot);
  70. inherits(SmalltalkObject, SmalltalkProtoObject);
  71. this.Root = SmalltalkRoot;
  72. this.Object = SmalltalkObject;
  73. this.__init__ = function () {
  74. var globals = brikz.smalltalkGlobals.globals;
  75. var addCoupledClass = brikz.classes.addCoupledClass;
  76. st.addPackage("Kernel-Objects");
  77. addCoupledClass("ProtoObject", undefined, "Kernel-Objects", SmalltalkProtoObject);
  78. addCoupledClass("Object", globals.ProtoObject, "Kernel-Objects", SmalltalkObject);
  79. };
  80. this.__init__.once = true;
  81. }
  82. OrganizeBrik.deps = ["augments", "root"];
  83. function OrganizeBrik (brikz, st) {
  84. var SmalltalkObject = brikz.root.Object;
  85. function SmalltalkOrganizer () {
  86. }
  87. function SmalltalkPackageOrganizer () {
  88. this.elements = [];
  89. }
  90. function SmalltalkClassOrganizer () {
  91. this.elements = [];
  92. }
  93. inherits(SmalltalkOrganizer, SmalltalkObject);
  94. inherits(SmalltalkPackageOrganizer, SmalltalkOrganizer);
  95. inherits(SmalltalkClassOrganizer, SmalltalkOrganizer);
  96. this.__init__ = function () {
  97. var globals = brikz.smalltalkGlobals.globals;
  98. var addCoupledClass = brikz.classes.addCoupledClass;
  99. st.addPackage("Kernel-Infrastructure");
  100. addCoupledClass("Organizer", globals.Object, "Kernel-Infrastructure", SmalltalkOrganizer);
  101. addCoupledClass("PackageOrganizer", globals.Organizer, "Kernel-Infrastructure", SmalltalkPackageOrganizer);
  102. addCoupledClass("ClassOrganizer", globals.Organizer, "Kernel-Infrastructure", SmalltalkClassOrganizer);
  103. };
  104. this.__init__.once = true;
  105. this.setupClassOrganization = function (behaviorBody) {
  106. behaviorBody.organization = new SmalltalkClassOrganizer();
  107. behaviorBody.organization.theClass = behaviorBody;
  108. };
  109. this.setupPackageOrganization = function (pkg) {
  110. pkg.organization = new SmalltalkPackageOrganizer();
  111. };
  112. this.addOrganizationElement = function (owner, element) {
  113. owner.organization.elements.addElement(element);
  114. };
  115. this.removeOrganizationElement = function (owner, element) {
  116. owner.organization.elements.removeElement(element);
  117. };
  118. }
  119. SelectorsBrik.deps = ["selectorConversion"];
  120. function SelectorsBrik (brikz, st) {
  121. var selectorSet = Object.create(null);
  122. var selectors = this.selectors = [];
  123. var selectorPairs = this.selectorPairs = [];
  124. this.registerSelector = function (stSelector) {
  125. if (selectorSet[stSelector]) return null;
  126. var jsSelector = st.st2js(stSelector);
  127. selectorSet[stSelector] = true;
  128. selectors.push(stSelector);
  129. var pair = {st: stSelector, js: jsSelector};
  130. selectorPairs.push(pair);
  131. return pair;
  132. };
  133. st.allSelectors = function () {
  134. return selectors;
  135. };
  136. }
  137. PackagesBrik.deps = ["organize", "root"];
  138. function PackagesBrik (brikz, st) {
  139. var setupPackageOrganization = brikz.organize.setupPackageOrganization;
  140. var SmalltalkObject = brikz.root.Object;
  141. function SmalltalkPackage () {
  142. }
  143. inherits(SmalltalkPackage, SmalltalkObject);
  144. this.__init__ = function () {
  145. var globals = brikz.smalltalkGlobals.globals;
  146. var addCoupledClass = brikz.classes.addCoupledClass;
  147. st.addPackage("Kernel-Infrastructure");
  148. addCoupledClass("Package", globals.Object, "Kernel-Infrastructure", SmalltalkPackage);
  149. };
  150. this.__init__.once = true;
  151. st.packages = {};
  152. /* Smalltalk package creation. To add a Package, use smalltalk.addPackage() */
  153. function pkg (spec) {
  154. var that = new SmalltalkPackage();
  155. that.pkgName = spec.pkgName;
  156. setupPackageOrganization(that);
  157. that.properties = spec.properties || {};
  158. return that;
  159. }
  160. /* Add a package to the system, creating a new one if needed.
  161. If pkgName is null or empty we return nil.
  162. If package already exists we still update the properties of it. */
  163. st.addPackage = function (pkgName, properties) {
  164. if (!pkgName) {
  165. return null;
  166. }
  167. if (!(st.packages[pkgName])) {
  168. st.packages[pkgName] = pkg({
  169. pkgName: pkgName,
  170. properties: properties
  171. });
  172. } else {
  173. if (properties) {
  174. st.packages[pkgName].properties = properties;
  175. }
  176. }
  177. return st.packages[pkgName];
  178. };
  179. }
  180. BehaviorsBrik.deps = ["organize", "root", "smalltalkGlobals"];
  181. function BehaviorsBrik (brikz, st) {
  182. var setupClassOrganization = brikz.organize.setupClassOrganization;
  183. var addOrganizationElement = brikz.organize.addOrganizationElement;
  184. var removeOrganizationElement = brikz.organize.removeOrganizationElement;
  185. var globals = brikz.smalltalkGlobals.globals;
  186. var SmalltalkObject = brikz.root.Object;
  187. function SmalltalkBehaviorBody () {
  188. }
  189. inherits(SmalltalkBehaviorBody, SmalltalkObject);
  190. this.BehaviorBody = SmalltalkBehaviorBody;
  191. this.__init__ = function () {
  192. var globals = brikz.smalltalkGlobals.globals;
  193. var addCoupledClass = brikz.classes.addCoupledClass;
  194. st.addPackage("Kernel-Classes");
  195. addCoupledClass("BehaviorBody", globals.Object, "Kernel-Classes", SmalltalkBehaviorBody);
  196. };
  197. this.__init__.once = true;
  198. /* Smalltalk classes */
  199. var classes = [];
  200. this.setupBehavior = function (behaviorBody, pkg) {
  201. if (pkg) {
  202. behaviorBody.pkg = pkg;
  203. }
  204. setupClassOrganization(behaviorBody);
  205. Object.defineProperty(behaviorBody, "methods", {
  206. value: Object.create(null),
  207. enumerable: false, configurable: true, writable: true
  208. });
  209. };
  210. this.buildBehaviorBody = function (pkgName, builder) {
  211. var pkg = st.packages[pkgName];
  212. if (!pkg) throw new Error("Missing package " + pkgName);
  213. var behaviorBody = makeBehaviorBody(builder, pkg);
  214. addBehaviorBody(behaviorBody);
  215. return behaviorBody;
  216. };
  217. function makeBehaviorBody (builder, pkg) {
  218. var behaviorBody = globals.hasOwnProperty(builder.className) && globals[builder.className];
  219. if (!behaviorBody) return builder.make(pkg);
  220. if (builder.updateExisting(behaviorBody, pkg)) return behaviorBody;
  221. var rebuilder = builder.rebuilderForExisting(behaviorBody);
  222. removeBehaviorBody(behaviorBody);
  223. return makeBehaviorBody(rebuilder, pkg);
  224. }
  225. function addBehaviorBody (behaviorBody) {
  226. globals[behaviorBody.className] = behaviorBody;
  227. classes.addElement(behaviorBody);
  228. addOrganizationElement(behaviorBody.pkg, behaviorBody);
  229. behaviorBody.added();
  230. }
  231. function removeBehaviorBody (behaviorBody) {
  232. behaviorBody.removed();
  233. removeOrganizationElement(behaviorBody.pkg, behaviorBody);
  234. classes.removeElement(behaviorBody);
  235. delete globals[behaviorBody.className];
  236. }
  237. this.removeBehaviorBody = removeBehaviorBody;
  238. /* Create an alias for an existing class */
  239. st.alias = function (behaviorBody, alias) {
  240. globals[alias] = behaviorBody;
  241. };
  242. /* Answer all registered Smalltalk classes */
  243. //TODO: remove the function and make smalltalk.classes an array
  244. st.classes = this.classes = function () {
  245. return classes;
  246. };
  247. }
  248. MethodsBrik.deps = ["organize", "selectors", "root", "selectorConversion"];
  249. function MethodsBrik (brikz, st) {
  250. var addOrganizationElement = brikz.organize.addOrganizationElement;
  251. var registerSelector = brikz.selectors.registerSelector;
  252. var SmalltalkObject = brikz.root.Object;
  253. function SmalltalkMethod () {
  254. }
  255. inherits(SmalltalkMethod, SmalltalkObject);
  256. this.__init__ = function () {
  257. var globals = brikz.smalltalkGlobals.globals;
  258. var addCoupledClass = brikz.classes.addCoupledClass;
  259. st.addPackage("Kernel-Methods");
  260. addCoupledClass("CompiledMethod", globals.Object, "Kernel-Methods", SmalltalkMethod);
  261. };
  262. this.__init__.once = true;
  263. /* Smalltalk method object. To add a method to a class,
  264. use api.addMethod() */
  265. st.method = function (spec) {
  266. var that = new SmalltalkMethod();
  267. var selector = spec.selector;
  268. that.selector = selector;
  269. that.jsSelector = st.st2js(selector);
  270. that.args = spec.args || {};
  271. that.protocol = spec.protocol;
  272. that.source = spec.source;
  273. that.messageSends = spec.messageSends || [];
  274. that.referencedClasses = spec.referencedClasses || [];
  275. that.fn = spec.fn;
  276. return that;
  277. };
  278. /* Add/remove a method to/from a class */
  279. st.addMethod = function (method, behaviorBody) {
  280. behaviorBody.methods[method.selector] = method;
  281. method.methodClass = behaviorBody;
  282. // During the bootstrap, #addCompiledMethod is not used.
  283. // Therefore we populate the organizer here too
  284. addOrganizationElement(behaviorBody, method.protocol);
  285. var newSelectors = [];
  286. function selectorInUse (stSelector) {
  287. var pair = registerSelector(stSelector);
  288. if (pair) {
  289. newSelectors.push(pair);
  290. }
  291. }
  292. selectorInUse(method.selector);
  293. method.messageSends.forEach(selectorInUse);
  294. behaviorBody.methodAdded(method);
  295. if (st._selectorsAdded) st._selectorsAdded(newSelectors);
  296. };
  297. st.removeMethod = function (method, behaviorBody) {
  298. if (behaviorBody.methods[method.selector] !== method) return;
  299. delete behaviorBody.methods[method.selector];
  300. behaviorBody.methodRemoved(method);
  301. // Do *not* delete protocols from here.
  302. // This is handled by #removeCompiledMethod
  303. };
  304. }
  305. TraitsBrik.deps = ["behaviors"];
  306. function TraitsBrik (brikz, st) {
  307. var SmalltalkBehaviorBody = brikz.behaviors.BehaviorBody;
  308. var setupBehavior = brikz.behaviors.setupBehavior;
  309. var buildBehaviorBody = brikz.behaviors.buildBehaviorBody;
  310. function SmalltalkTrait () {
  311. }
  312. inherits(SmalltalkTrait, SmalltalkBehaviorBody);
  313. SmalltalkTrait.prototype.trait = true;
  314. SmalltalkTrait.prototype.added = function () {
  315. if (st._traitAdded) st._traitAdded(this);
  316. };
  317. SmalltalkTrait.prototype.removed = function () {
  318. if (st._traitRemoved) st._traitRemoved(this);
  319. };
  320. SmalltalkTrait.prototype.methodAdded = function (method) {
  321. if (st._traitMethodAdded) st._traitMethodAdded(method, this);
  322. };
  323. SmalltalkTrait.prototype.methodRemoved = function (method) {
  324. if (st._traitMethodRemoved) st._traitMethodRemoved(method, this);
  325. };
  326. this.__init__ = function () {
  327. var globals = brikz.smalltalkGlobals.globals;
  328. var addCoupledClass = brikz.classes.addCoupledClass;
  329. st.addPackage("Kernel-Classes");
  330. addCoupledClass("Trait", globals.BehaviorBody, "Kernel-Classes", SmalltalkTrait);
  331. };
  332. this.__init__.once = true;
  333. function traitBuilder (className) {
  334. return {
  335. className: className,
  336. make: function (pkg) {
  337. var that = new SmalltalkTrait();
  338. that.className = className;
  339. setupBehavior(that, pkg);
  340. return that;
  341. },
  342. updateExisting: function (trait, pkg) {
  343. if (pkg) trait.pkg = pkg;
  344. return true;
  345. },
  346. rebuilderForExisting: function (trait) {
  347. return traitBuilder(className);
  348. }
  349. };
  350. }
  351. st.addTrait = function (className, pkgName) {
  352. return buildBehaviorBody(pkgName, traitBuilder(className));
  353. };
  354. }
  355. ClassesBrik.deps = ["root", "behaviors"];
  356. function ClassesBrik (brikz, st) {
  357. var SmalltalkRoot = brikz.root.Root;
  358. var SmalltalkBehaviorBody = brikz.behaviors.BehaviorBody;
  359. var buildBehaviorBody = brikz.behaviors.buildBehaviorBody;
  360. var setupBehavior = brikz.behaviors.setupBehavior;
  361. var removeBehaviorBody = brikz.behaviors.removeBehaviorBody;
  362. function SmalltalkBehavior () {
  363. }
  364. function SmalltalkClass () {
  365. }
  366. function SmalltalkMetaclass () {
  367. }
  368. inherits(SmalltalkBehavior, SmalltalkBehaviorBody);
  369. inherits(SmalltalkClass, SmalltalkBehavior);
  370. inherits(SmalltalkMetaclass, SmalltalkBehavior);
  371. // Fake root class of the system.
  372. // Effective superclass of all classes created with `nil subclass: ...`.
  373. var nilAsClass = this.nilAsClass = {fn: SmalltalkRoot, klass: {fn: SmalltalkClass}};
  374. SmalltalkBehavior.prototype.toString = function () {
  375. return 'Smalltalk ' + this.className;
  376. };
  377. SmalltalkMetaclass.prototype.meta = true;
  378. SmalltalkClass.prototype.added = function () {
  379. addSubclass(this);
  380. if (st._classAdded) st._classAdded(this);
  381. };
  382. SmalltalkClass.prototype.removed = function () {
  383. if (st._classRemoved) st._classRemoved(this);
  384. removeSubclass(this);
  385. };
  386. SmalltalkBehavior.prototype.methodAdded = function (method) {
  387. if (st._methodAdded) st._methodAdded(method, this);
  388. };
  389. SmalltalkBehavior.prototype.methodRemoved = function (method) {
  390. if (st._methodRemoved) st._methodRemoved(method, this);
  391. };
  392. this.__init__ = function () {
  393. var globals = brikz.smalltalkGlobals.globals;
  394. var addCoupledClass = brikz.classes.addCoupledClass;
  395. var nilSubclasses = [globals.ProtoObject];
  396. st.addPackage("Kernel-Classes");
  397. addCoupledClass("Behavior", globals.BehaviorBody, "Kernel-Classes", SmalltalkBehavior);
  398. addCoupledClass("Metaclass", globals.Behavior, "Kernel-Classes", SmalltalkMetaclass);
  399. addCoupledClass("Class", globals.Behavior, "Kernel-Classes", SmalltalkClass);
  400. // Manually bootstrap the metaclass hierarchy
  401. nilAsClass.klass = globals.Class;
  402. nilSubclasses.forEach(function (each) {
  403. each.klass.superclass = globals.Class;
  404. addSubclass(each.klass);
  405. });
  406. };
  407. this.__init__.once = true;
  408. /* Smalltalk class creation. A class is an instance of an automatically
  409. created metaclass object. Newly created classes (not their metaclass)
  410. should be added to the system, see smalltalk.addClass().
  411. Superclass linking is *not* handled here, see api.initialize() */
  412. function klass (spec) {
  413. var setSuperClass = spec.superclass;
  414. if (!spec.superclass) {
  415. spec.superclass = nilAsClass;
  416. }
  417. var meta = metaclass(spec);
  418. var that = meta.instanceClass;
  419. that.superclass = setSuperClass;
  420. that.fn = spec.fn || inherits(function () {
  421. }, spec.superclass.fn);
  422. that.iVarNames = spec.iVarNames || [];
  423. that.subclasses = [];
  424. setupBehavior(that, spec.pkg);
  425. that.className = spec.className;
  426. meta.className = spec.className + ' class';
  427. meta.superclass = spec.superclass.klass;
  428. return that;
  429. }
  430. function metaclass (spec) {
  431. var that = new SmalltalkMetaclass();
  432. that.fn = inherits(function () {
  433. }, spec.superclass.klass.fn);
  434. wireKlass(that);
  435. that.instanceClass = new that.fn();
  436. that.iVarNames = [];
  437. setupBehavior(that);
  438. return that;
  439. }
  440. function wireKlass (klass) {
  441. Object.defineProperty(klass.fn.prototype, "klass", {
  442. value: klass,
  443. enumerable: false, configurable: true, writable: true
  444. });
  445. }
  446. this.wireKlass = wireKlass;
  447. /* Add a class to the system, creating a new one if needed.
  448. A Package is lazily created if one with given name does not exist. */
  449. st.addClass = function (className, superclass, iVarNames, pkgName) {
  450. // While subclassing nil is allowed, it might be an error, so
  451. // warn about it.
  452. if (typeof superclass == 'undefined' || superclass && superclass.isNil) {
  453. console.warn('Compiling ' + className + ' as a subclass of `nil`. A dependency might be missing.');
  454. }
  455. return buildBehaviorBody(pkgName, classBuilder(className, superclass, iVarNames));
  456. };
  457. function classBuilder (className, superclass, iVarNames, fn) {
  458. if (superclass == null || superclass.isNil) {
  459. superclass = null;
  460. }
  461. return {
  462. className: className,
  463. superclass: superclass,
  464. iVarNames: iVarNames,
  465. fn: fn,
  466. make: function (pkg) {
  467. return klass({
  468. className: className,
  469. pkg: pkg,
  470. superclass: superclass,
  471. iVarNames: iVarNames,
  472. fn: fn
  473. });
  474. },
  475. updateExisting: function (klass, pkg) {
  476. if (klass.superclass == superclass && !fn) {
  477. if (iVarNames) klass.iVarNames = iVarNames;
  478. if (pkg) klass.pkg = pkg;
  479. return true;
  480. }
  481. return false;
  482. },
  483. rebuilderForExisting: function (klass) {
  484. return classBuilder(className, superclass, iVarNames || klass.iVarNames, fn);
  485. }
  486. };
  487. }
  488. st.removeClass = removeBehaviorBody;
  489. function addSubclass (klass) {
  490. if (klass.superclass) {
  491. klass.superclass.subclasses.addElement(klass);
  492. }
  493. }
  494. function removeSubclass (klass) {
  495. if (klass.superclass) {
  496. klass.superclass.subclasses.removeElement(klass);
  497. }
  498. }
  499. /* Create a new class coupling with a JavaScript constructor,
  500. and add it to the system.*/
  501. this.addCoupledClass = function (className, superclass, pkgName, fn) {
  502. return buildBehaviorBody(pkgName, classBuilder(className, superclass, null, fn));
  503. };
  504. function metaSubclasses (metaclass) {
  505. return metaclass.instanceClass.subclasses
  506. .filter(function (each) {
  507. return !each.meta;
  508. })
  509. .map(function (each) {
  510. return each.klass;
  511. });
  512. }
  513. st.metaSubclasses = metaSubclasses;
  514. st.traverseClassTree = function (klass, fn) {
  515. var queue = [klass], sentinel = {};
  516. for (var i = 0; i < queue.length; ++i) {
  517. var item = queue[i];
  518. if (fn(item, sentinel) === sentinel) continue;
  519. var subclasses = item.meta ? metaSubclasses(item) : item.subclasses;
  520. queue.push.apply(queue, subclasses);
  521. }
  522. };
  523. }
  524. function AugmentsBrik (brikz, st) {
  525. /* Array extensions */
  526. Array.prototype.addElement = function (el) {
  527. if (typeof el === 'undefined') {
  528. return;
  529. }
  530. if (this.indexOf(el) == -1) {
  531. this.push(el);
  532. }
  533. };
  534. Array.prototype.removeElement = function (el) {
  535. var i = this.indexOf(el);
  536. if (i !== -1) {
  537. this.splice(i, 1);
  538. }
  539. };
  540. }
  541. function SmalltalkInitBrik (brikz, st) {
  542. var initialized = false;
  543. var runtimeLoadedPromise = new Promise(function (resolve, reject) {
  544. require(['./kernel-runtime'], resolve, reject);
  545. });
  546. /* Smalltalk initialization. Called on page load */
  547. st.initialize = function () {
  548. return runtimeLoadedPromise.then(function (configureWithRuntime) {
  549. if (initialized) return;
  550. configureWithRuntime(brikz);
  551. initialized = true;
  552. });
  553. };
  554. }
  555. function SelectorConversionBrik (brikz, st) {
  556. /* Convert a Smalltalk selector into a JS selector */
  557. st.st2js = function (string) {
  558. return '_' + string
  559. .replace(/:/g, '_')
  560. .replace(/[\&]/g, '_and')
  561. .replace(/[\|]/g, '_or')
  562. .replace(/[+]/g, '_plus')
  563. .replace(/-/g, '_minus')
  564. .replace(/[*]/g, '_star')
  565. .replace(/[\/]/g, '_slash')
  566. .replace(/[\\]/g, '_backslash')
  567. .replace(/[\~]/g, '_tild')
  568. .replace(/>/g, '_gt')
  569. .replace(/</g, '_lt')
  570. .replace(/=/g, '_eq')
  571. .replace(/,/g, '_comma')
  572. .replace(/[@]/g, '_at');
  573. };
  574. /* Convert a string to a valid smalltalk selector.
  575. if you modify the following functions, also change st2js
  576. accordingly */
  577. st.js2st = function (selector) {
  578. if (selector.match(/^__/)) {
  579. return binaryJsToSt(selector);
  580. } else {
  581. return keywordJsToSt(selector);
  582. }
  583. };
  584. function keywordJsToSt (selector) {
  585. return selector.replace(/^_/, '').replace(/_/g, ':');
  586. }
  587. function binaryJsToSt (selector) {
  588. return selector
  589. .replace(/^_/, '')
  590. .replace(/_and/g, '&')
  591. .replace(/_or/g, '|')
  592. .replace(/_plus/g, '+')
  593. .replace(/_minus/g, '-')
  594. .replace(/_star/g, '*')
  595. .replace(/_slash/g, '/')
  596. .replace(/_backslash/g, '\\')
  597. .replace(/_tild/g, '~')
  598. .replace(/_gt/g, '>')
  599. .replace(/_lt/g, '<')
  600. .replace(/_eq/g, '=')
  601. .replace(/_comma/g, ',')
  602. .replace(/_at/g, '@');
  603. }
  604. st.st2prop = function (stSelector) {
  605. var colonPosition = stSelector.indexOf(':');
  606. return colonPosition === -1 ? stSelector : stSelector.slice(0, colonPosition);
  607. };
  608. }
  609. /* Adds AMD and requirejs related methods to the api */
  610. function AMDBrik (brikz, st) {
  611. st.amdRequire = require;
  612. st.defaultTransportType = st.defaultTransportType || "amd";
  613. st.defaultAmdNamespace = st.defaultAmdNamespace || "amber_core";
  614. }
  615. NilBrik.deps = ["root"];
  616. function NilBrik (brikz, st) {
  617. var SmalltalkObject = brikz.root.Object;
  618. function SmalltalkNil () {
  619. }
  620. inherits(SmalltalkNil, SmalltalkObject);
  621. this.nilAsReceiver = new SmalltalkNil();
  622. // Adds an `isNil` property to the `nil` object. When sending
  623. // nil objects from one environment to another, doing
  624. // `anObject == nil` (in JavaScript) does not always answer
  625. // true as the referenced nil object might come from the other
  626. // environment.
  627. Object.defineProperty(this.nilAsReceiver, 'isNil', {
  628. value: true,
  629. enumerable: false, configurable: false, writable: false
  630. });
  631. this.__init__ = function () {
  632. var globals = brikz.smalltalkGlobals.globals;
  633. var addCoupledClass = brikz.classes.addCoupledClass;
  634. st.addPackage("Kernel-Objects");
  635. addCoupledClass("UndefinedObject", globals.Object, "Kernel-Objects", SmalltalkNil);
  636. };
  637. this.__init__.once = true;
  638. }
  639. /* Defines asReceiver to be present at load time */
  640. /* (logically it belongs more to PrimitiveBrik) */
  641. AsReceiverBrik.deps = ["nil"];
  642. function AsReceiverBrik (brikz, st) {
  643. var nilAsReceiver = brikz.nil.nilAsReceiver;
  644. /**
  645. * This function is used all over the compiled amber code.
  646. * It takes any value (JavaScript or Smalltalk)
  647. * and returns a proper Amber Smalltalk receiver.
  648. *
  649. * null or undefined -> nilAsReceiver,
  650. * object having Smalltalk signature -> unchanged,
  651. * otherwise wrapped foreign (JS) object
  652. */
  653. this.asReceiver = function (o) {
  654. if (o == null) return nilAsReceiver;
  655. else if (o.klass != null) return o;
  656. else return st.wrapJavaScript(o);
  657. };
  658. }
  659. var api = {};
  660. var brikz = new Brikz(api);
  661. /* Making smalltalk that can load */
  662. brikz.smalltalkGlobals = SmalltalkGlobalsBrik;
  663. brikz.root = RootBrik;
  664. brikz.selectors = SelectorsBrik;
  665. brikz.organize = OrganizeBrik;
  666. brikz.selectorConversion = SelectorConversionBrik;
  667. brikz.packages = PackagesBrik;
  668. brikz.behaviors = BehaviorsBrik;
  669. brikz.classes = ClassesBrik;
  670. brikz.methods = MethodsBrik;
  671. brikz.traits = TraitsBrik;
  672. brikz.stInit = SmalltalkInitBrik;
  673. brikz.augments = AugmentsBrik;
  674. brikz.nil = NilBrik;
  675. brikz.asReceiver = AsReceiverBrik;
  676. brikz.amd = AMDBrik;
  677. brikz.rebuild();
  678. return {
  679. api: api,
  680. nil/* TODO deprecate */: brikz.nil.nilAsReceiver,
  681. nilAsReceiver: brikz.nil.nilAsReceiver,
  682. dnu/* TODO deprecate */: brikz.classes.nilAsClass,
  683. nilAsClass: brikz.classes.nilAsClass,
  684. globals: brikz.smalltalkGlobals.globals,
  685. asReceiver: brikz.asReceiver.asReceiver
  686. };
  687. });