boot.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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.Object = SmalltalkObject;
  75. this.nilAsReceiver = new SmalltalkNil();
  76. // Adds an `isNil` property to the `nil` object. When sending
  77. // nil objects from one environment to another, doing
  78. // `anObject == nil` (in JavaScript) does not always answer
  79. // true as the referenced nil object might come from the other
  80. // environment.
  81. Object.defineProperty(this.nilAsReceiver, 'isNil', {
  82. value: true,
  83. enumerable: false, configurable: false, writable: false
  84. });
  85. // Fake root class of the system.
  86. // Effective superclass of all classes created with `nil subclass: ...`.
  87. this.nilAsClass = {fn: SmalltalkRoot};
  88. this.__init__ = function () {
  89. var globals = brikz.smalltalkGlobals.globals;
  90. var addCoupledClass = brikz.classes.addCoupledClass;
  91. st.addPackage("Kernel-Objects");
  92. addCoupledClass("ProtoObject", undefined, "Kernel-Objects", SmalltalkProtoObject);
  93. addCoupledClass("Object", globals.ProtoObject, "Kernel-Objects", SmalltalkObject);
  94. addCoupledClass("UndefinedObject", globals.Object, "Kernel-Objects", SmalltalkNil);
  95. };
  96. this.__init__.once = true;
  97. }
  98. OrganizeBrik.deps = ["augments", "root"];
  99. function OrganizeBrik (brikz, st) {
  100. var SmalltalkObject = brikz.root.Object;
  101. function SmalltalkOrganizer () {
  102. }
  103. function SmalltalkPackageOrganizer () {
  104. this.elements = [];
  105. }
  106. function SmalltalkClassOrganizer () {
  107. this.elements = [];
  108. }
  109. inherits(SmalltalkOrganizer, SmalltalkObject);
  110. inherits(SmalltalkPackageOrganizer, SmalltalkOrganizer);
  111. inherits(SmalltalkClassOrganizer, SmalltalkOrganizer);
  112. this.__init__ = function () {
  113. var globals = brikz.smalltalkGlobals.globals;
  114. var addCoupledClass = brikz.classes.addCoupledClass;
  115. st.addPackage("Kernel-Infrastructure");
  116. addCoupledClass("Organizer", globals.Object, "Kernel-Infrastructure", SmalltalkOrganizer);
  117. addCoupledClass("PackageOrganizer", globals.Organizer, "Kernel-Infrastructure", SmalltalkPackageOrganizer);
  118. addCoupledClass("ClassOrganizer", globals.Organizer, "Kernel-Infrastructure", SmalltalkClassOrganizer);
  119. };
  120. this.__init__.once = true;
  121. this.setupClassOrganization = function (behaviorBody) {
  122. behaviorBody.organization = new SmalltalkClassOrganizer();
  123. behaviorBody.organization.theClass = behaviorBody;
  124. };
  125. this.setupPackageOrganization = function (pkg) {
  126. pkg.organization = new SmalltalkPackageOrganizer();
  127. };
  128. this.addOrganizationElement = function (owner, element) {
  129. owner.organization.elements.addElement(element);
  130. };
  131. this.removeOrganizationElement = function (owner, element) {
  132. owner.organization.elements.removeElement(element);
  133. };
  134. }
  135. SelectorsBrik.deps = ["selectorConversion"];
  136. function SelectorsBrik (brikz, st) {
  137. var selectorSet = Object.create(null);
  138. var selectors = this.selectors = [];
  139. var selectorPairs = this.selectorPairs = [];
  140. this.registerSelector = function (stSelector) {
  141. if (selectorSet[stSelector]) return null;
  142. var jsSelector = st.st2js(stSelector);
  143. selectorSet[stSelector] = true;
  144. selectors.push(stSelector);
  145. var pair = {st: stSelector, js: jsSelector};
  146. selectorPairs.push(pair);
  147. return pair;
  148. };
  149. st.allSelectors = function () {
  150. return selectors;
  151. };
  152. }
  153. PackagesBrik.deps = ["organize", "root"];
  154. function PackagesBrik (brikz, st) {
  155. var setupPackageOrganization = brikz.organize.setupPackageOrganization;
  156. var SmalltalkObject = brikz.root.Object;
  157. function SmalltalkPackage () {
  158. }
  159. inherits(SmalltalkPackage, SmalltalkObject);
  160. this.__init__ = function () {
  161. var globals = brikz.smalltalkGlobals.globals;
  162. var addCoupledClass = brikz.classes.addCoupledClass;
  163. st.addPackage("Kernel-Infrastructure");
  164. addCoupledClass("Package", globals.Object, "Kernel-Infrastructure", SmalltalkPackage);
  165. };
  166. this.__init__.once = true;
  167. st.packages = {};
  168. /* Smalltalk package creation. To add a Package, use smalltalk.addPackage() */
  169. function pkg (spec) {
  170. var that = new SmalltalkPackage();
  171. that.pkgName = spec.pkgName;
  172. setupPackageOrganization(that);
  173. that.properties = spec.properties || {};
  174. return that;
  175. }
  176. /* Add a package to the system, creating a new one if needed.
  177. If pkgName is null or empty we return nil.
  178. If package already exists we still update the properties of it. */
  179. st.addPackage = function (pkgName, properties) {
  180. if (!pkgName) {
  181. return null;
  182. }
  183. if (!(st.packages[pkgName])) {
  184. st.packages[pkgName] = pkg({
  185. pkgName: pkgName,
  186. properties: properties
  187. });
  188. } else {
  189. if (properties) {
  190. st.packages[pkgName].properties = properties;
  191. }
  192. }
  193. return st.packages[pkgName];
  194. };
  195. }
  196. BehaviorsBrik.deps = ["organize", "root", "smalltalkGlobals"];
  197. function BehaviorsBrik (brikz, st) {
  198. var setupClassOrganization = brikz.organize.setupClassOrganization;
  199. var addOrganizationElement = brikz.organize.addOrganizationElement;
  200. var removeOrganizationElement = brikz.organize.removeOrganizationElement;
  201. var globals = brikz.smalltalkGlobals.globals;
  202. var SmalltalkObject = brikz.root.Object;
  203. function SmalltalkBehaviorBody () {
  204. }
  205. inherits(SmalltalkBehaviorBody, SmalltalkObject);
  206. this.BehaviorBody = SmalltalkBehaviorBody;
  207. this.__init__ = function () {
  208. var globals = brikz.smalltalkGlobals.globals;
  209. var addCoupledClass = brikz.classes.addCoupledClass;
  210. st.addPackage("Kernel-Classes");
  211. addCoupledClass("BehaviorBody", globals.Object, "Kernel-Classes", SmalltalkBehaviorBody);
  212. };
  213. this.__init__.once = true;
  214. /* Smalltalk classes */
  215. var classes = [];
  216. this.setupBehavior = function (behaviorBody, pkg) {
  217. if (pkg) {
  218. behaviorBody.pkg = pkg;
  219. }
  220. setupClassOrganization(behaviorBody);
  221. Object.defineProperty(behaviorBody, "methods", {
  222. value: Object.create(null),
  223. enumerable: false, configurable: true, writable: true
  224. });
  225. };
  226. this.buildBehaviorBody = function (pkgName, builder) {
  227. var pkg = st.packages[pkgName];
  228. if (!pkg) throw new Error("Missing package " + pkgName);
  229. var behaviorBody = makeBehaviorBody(builder, pkg);
  230. addBehaviorBody(behaviorBody);
  231. return behaviorBody;
  232. };
  233. function makeBehaviorBody (builder, pkg) {
  234. var behaviorBody = globals.hasOwnProperty(builder.className) && globals[builder.className];
  235. if (!behaviorBody) return builder.make(pkg);
  236. if (builder.updateExisting(behaviorBody, pkg)) return behaviorBody;
  237. var rebuilder = builder.rebuilderForExisting(behaviorBody);
  238. removeBehaviorBody(behaviorBody);
  239. return makeBehaviorBody(rebuilder, pkg);
  240. }
  241. function addBehaviorBody (behaviorBody) {
  242. globals[behaviorBody.className] = behaviorBody;
  243. classes.addElement(behaviorBody);
  244. addOrganizationElement(behaviorBody.pkg, behaviorBody);
  245. behaviorBody.added();
  246. }
  247. function removeBehaviorBody (behaviorBody) {
  248. behaviorBody.removed();
  249. removeOrganizationElement(behaviorBody.pkg, behaviorBody);
  250. classes.removeElement(behaviorBody);
  251. delete globals[behaviorBody.className];
  252. }
  253. this.removeBehaviorBody = removeBehaviorBody;
  254. /* Create an alias for an existing class */
  255. st.alias = function (behaviorBody, alias) {
  256. globals[alias] = behaviorBody;
  257. };
  258. /* Answer all registered Smalltalk classes */
  259. //TODO: remove the function and make smalltalk.classes an array
  260. st.classes = this.classes = function () {
  261. return classes;
  262. };
  263. }
  264. MethodsBrik.deps = ["organize", "selectors", "root", "selectorConversion"];
  265. function MethodsBrik (brikz, st) {
  266. var addOrganizationElement = brikz.organize.addOrganizationElement;
  267. var registerSelector = brikz.selectors.registerSelector;
  268. var SmalltalkObject = brikz.root.Object;
  269. function SmalltalkMethod () {
  270. }
  271. inherits(SmalltalkMethod, SmalltalkObject);
  272. this.__init__ = function () {
  273. var globals = brikz.smalltalkGlobals.globals;
  274. var addCoupledClass = brikz.classes.addCoupledClass;
  275. st.addPackage("Kernel-Methods");
  276. addCoupledClass("CompiledMethod", globals.Object, "Kernel-Methods", SmalltalkMethod);
  277. };
  278. this.__init__.once = true;
  279. /* Smalltalk method object. To add a method to a class,
  280. use api.addMethod() */
  281. st.method = function (spec) {
  282. var that = new SmalltalkMethod();
  283. var selector = spec.selector;
  284. that.selector = selector;
  285. that.jsSelector = st.st2js(selector);
  286. that.args = spec.args || {};
  287. that.protocol = spec.protocol;
  288. that.source = spec.source;
  289. that.messageSends = spec.messageSends || [];
  290. that.referencedClasses = spec.referencedClasses || [];
  291. that.fn = spec.fn;
  292. return that;
  293. };
  294. /* Add/remove a method to/from a class */
  295. st.addMethod = function (method, behaviorBody) {
  296. behaviorBody.methods[method.selector] = method;
  297. method.methodClass = behaviorBody;
  298. // During the bootstrap, #addCompiledMethod is not used.
  299. // Therefore we populate the organizer here too
  300. addOrganizationElement(behaviorBody, method.protocol);
  301. var newSelectors = [];
  302. function selectorInUse (stSelector) {
  303. var pair = registerSelector(stSelector);
  304. if (pair) {
  305. newSelectors.push(pair);
  306. }
  307. }
  308. selectorInUse(method.selector);
  309. method.messageSends.forEach(selectorInUse);
  310. behaviorBody.methodAdded(method);
  311. if (st._selectorsAdded) st._selectorsAdded(newSelectors);
  312. };
  313. st.removeMethod = function (method, behaviorBody) {
  314. if (behaviorBody.methods[method.selector] !== method) return;
  315. delete behaviorBody.methods[method.selector];
  316. behaviorBody.methodRemoved(method);
  317. // Do *not* delete protocols from here.
  318. // This is handled by #removeCompiledMethod
  319. };
  320. }
  321. TraitsBrik.deps = ["behaviors"];
  322. function TraitsBrik (brikz, st) {
  323. var SmalltalkBehaviorBody = brikz.behaviors.BehaviorBody;
  324. var setupBehavior = brikz.behaviors.setupBehavior;
  325. var buildBehaviorBody = brikz.behaviors.buildBehaviorBody;
  326. function SmalltalkTrait () {
  327. }
  328. inherits(SmalltalkTrait, SmalltalkBehaviorBody);
  329. SmalltalkTrait.prototype.trait = true;
  330. SmalltalkTrait.prototype.added = function () {
  331. if (st._traitAdded) st._traitAdded(this);
  332. };
  333. SmalltalkTrait.prototype.removed = function () {
  334. if (st._traitRemoved) st._traitRemoved(this);
  335. };
  336. SmalltalkTrait.prototype.methodAdded = function (method) {
  337. if (st._traitMethodAdded) st._traitMethodAdded(method, this);
  338. };
  339. SmalltalkTrait.prototype.methodRemoved = function (method) {
  340. if (st._traitMethodRemoved) st._traitMethodRemoved(method, this);
  341. };
  342. this.__init__ = function () {
  343. var globals = brikz.smalltalkGlobals.globals;
  344. var addCoupledClass = brikz.classes.addCoupledClass;
  345. st.addPackage("Kernel-Classes");
  346. addCoupledClass("Trait", globals.BehaviorBody, "Kernel-Classes", SmalltalkTrait);
  347. };
  348. this.__init__.once = true;
  349. function traitBuilder (className) {
  350. return {
  351. className: className,
  352. make: function (pkg) {
  353. var that = new SmalltalkTrait();
  354. that.className = className;
  355. setupBehavior(that, pkg);
  356. return that;
  357. },
  358. updateExisting: function (trait) {
  359. if (pkg) trait.pkg = pkg;
  360. return true;
  361. },
  362. rebuilderForExisting: function (trait) {
  363. return traitBuilder(className);
  364. }
  365. };
  366. }
  367. st.addTrait = function (className, pkgName) {
  368. return buildBehaviorBody(pkgName, traitBuilder(className));
  369. };
  370. }
  371. ClassesBrik.deps = ["root", "behaviors"];
  372. function ClassesBrik (brikz, st) {
  373. var nilAsClass = brikz.root.nilAsClass;
  374. var SmalltalkBehaviorBody = brikz.behaviors.BehaviorBody;
  375. var buildBehaviorBody = brikz.behaviors.buildBehaviorBody;
  376. var setupBehavior = brikz.behaviors.setupBehavior;
  377. var removeBehaviorBody = brikz.behaviors.removeBehaviorBody;
  378. nilAsClass.klass = {fn: SmalltalkClass};
  379. function SmalltalkBehavior () {
  380. }
  381. function SmalltalkClass () {
  382. }
  383. function SmalltalkMetaclass () {
  384. }
  385. inherits(SmalltalkBehavior, SmalltalkBehaviorBody);
  386. inherits(SmalltalkClass, SmalltalkBehavior);
  387. inherits(SmalltalkMetaclass, SmalltalkBehavior);
  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. SmalltalkInitBrik.deps = ["globals", "classes"];
  556. function SmalltalkInitBrik (brikz, st) {
  557. var globals = brikz.smalltalkGlobals.globals;
  558. var initialized = false;
  559. var runtimeLoadedPromise = new Promise(function (resolve, reject) {
  560. require(['./kernel-runtime'], resolve, reject);
  561. });
  562. /* Smalltalk initialization. Called on page load */
  563. st.initialize = function () {
  564. return runtimeLoadedPromise.then(function (configureWithRuntime) {
  565. if (initialized) {
  566. return;
  567. }
  568. configureWithRuntime(brikz);
  569. st.alias(globals.Array, "OrderedCollection");
  570. st.alias(globals.Date, "Time");
  571. st.classes().forEach(function (klass) {
  572. klass._initialize();
  573. });
  574. initialized = true;
  575. });
  576. };
  577. }
  578. function SelectorConversionBrik (brikz, st) {
  579. /* Convert a Smalltalk selector into a JS selector */
  580. st.st2js = function (string) {
  581. return '_' + string
  582. .replace(/:/g, '_')
  583. .replace(/[\&]/g, '_and')
  584. .replace(/[\|]/g, '_or')
  585. .replace(/[+]/g, '_plus')
  586. .replace(/-/g, '_minus')
  587. .replace(/[*]/g, '_star')
  588. .replace(/[\/]/g, '_slash')
  589. .replace(/[\\]/g, '_backslash')
  590. .replace(/[\~]/g, '_tild')
  591. .replace(/>/g, '_gt')
  592. .replace(/</g, '_lt')
  593. .replace(/=/g, '_eq')
  594. .replace(/,/g, '_comma')
  595. .replace(/[@]/g, '_at');
  596. };
  597. /* Convert a string to a valid smalltalk selector.
  598. if you modify the following functions, also change st2js
  599. accordingly */
  600. st.js2st = function (selector) {
  601. if (selector.match(/^__/)) {
  602. return binaryJsToSt(selector);
  603. } else {
  604. return keywordJsToSt(selector);
  605. }
  606. };
  607. function keywordJsToSt (selector) {
  608. return selector.replace(/^_/, '').replace(/_/g, ':');
  609. }
  610. function binaryJsToSt (selector) {
  611. return selector
  612. .replace(/^_/, '')
  613. .replace(/_and/g, '&')
  614. .replace(/_or/g, '|')
  615. .replace(/_plus/g, '+')
  616. .replace(/_minus/g, '-')
  617. .replace(/_star/g, '*')
  618. .replace(/_slash/g, '/')
  619. .replace(/_backslash/g, '\\')
  620. .replace(/_tild/g, '~')
  621. .replace(/_gt/g, '>')
  622. .replace(/_lt/g, '<')
  623. .replace(/_eq/g, '=')
  624. .replace(/_comma/g, ',')
  625. .replace(/_at/g, '@');
  626. }
  627. st.st2prop = function (stSelector) {
  628. var colonPosition = stSelector.indexOf(':');
  629. return colonPosition === -1 ? stSelector : stSelector.slice(0, colonPosition);
  630. };
  631. }
  632. /* Adds AMD and requirejs related methods to the api */
  633. function AMDBrik (brikz, st) {
  634. st.amdRequire = require;
  635. st.defaultTransportType = st.defaultTransportType || "amd";
  636. st.defaultAmdNamespace = st.defaultAmdNamespace || "amber_core";
  637. }
  638. /* Defines asReceiver to be present at load time */
  639. /* (logically it belongs more to PrimitiveBrik) */
  640. AsReceiverBrik.deps = ["smalltalkGlobals", "root"];
  641. function AsReceiverBrik (brikz, st) {
  642. var globals = brikz.smalltalkGlobals.globals;
  643. var nilAsReceiver = brikz.root.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 globals.JSObjectProxy._on_(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.asReceiver = AsReceiverBrik;
  675. brikz.amd = AMDBrik;
  676. brikz.rebuild();
  677. return {
  678. api: api,
  679. nil/* TODO deprecate */: brikz.root.nilAsReceiver,
  680. nilAsReceiver: brikz.root.nilAsReceiver,
  681. dnu/* TODO deprecate */: brikz.root.nilAsClass,
  682. nilAsClass: brikz.root.nilAsClass,
  683. globals: brikz.smalltalkGlobals.globals,
  684. asReceiver: brikz.asReceiver.asReceiver
  685. };
  686. });