boot.js 29 KB

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