boot.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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 (klass) {
  122. klass.organization = new SmalltalkClassOrganizer();
  123. klass.organization.theClass = klass;
  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. ClassesBrik.deps = ["organize", "root", "smalltalkGlobals"];
  197. function ClassesBrik (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 nilAsClass = brikz.root.nilAsClass;
  203. var SmalltalkObject = brikz.root.Object;
  204. nilAsClass.klass = {fn: SmalltalkClass};
  205. function SmalltalkBehavior () {
  206. }
  207. function SmalltalkTrait () {
  208. }
  209. function SmalltalkClass () {
  210. }
  211. function SmalltalkMetaclass () {
  212. }
  213. inherits(SmalltalkBehavior, SmalltalkObject);
  214. inherits(SmalltalkTrait, SmalltalkBehavior);
  215. inherits(SmalltalkClass, SmalltalkBehavior);
  216. inherits(SmalltalkMetaclass, SmalltalkBehavior);
  217. SmalltalkBehavior.prototype.toString = function () {
  218. return 'Smalltalk ' + this.className;
  219. };
  220. SmalltalkTrait.prototype.trait = true;
  221. SmalltalkMetaclass.prototype.meta = true;
  222. SmalltalkTrait.prototype.added = function () {
  223. if (st._traitAdded) st._traitAdded(this);
  224. };
  225. SmalltalkTrait.prototype.methodAdded = function (method) {
  226. if (st._traitMethodAdded) st._traitMethodAdded(method, this);
  227. };
  228. SmalltalkTrait.prototype.methodRemoved = function (method) {
  229. if (st._traitMethodRemoved) st._traitMethodRemoved(method, this);
  230. };
  231. SmalltalkClass.prototype.added = function () {
  232. if (st._classAdded) st._classAdded(this);
  233. };
  234. SmalltalkBehavior.prototype.methodAdded = function (method) {
  235. if (st._methodAdded) st._methodAdded(method, this);
  236. };
  237. SmalltalkBehavior.prototype.methodRemoved = function (method) {
  238. if (st._methodRemoved) st._methodRemoved(method, this);
  239. };
  240. this.__init__ = function () {
  241. var globals = brikz.smalltalkGlobals.globals;
  242. var addCoupledClass = brikz.classes.addCoupledClass;
  243. st.addPackage("Kernel-Classes");
  244. addCoupledClass("Behavior", globals.Object, "Kernel-Classes", SmalltalkBehavior);
  245. addCoupledClass("Metaclass", globals.Behavior, "Kernel-Classes", SmalltalkMetaclass);
  246. addCoupledClass("Class", globals.Behavior, "Kernel-Classes", SmalltalkClass);
  247. addCoupledClass("Trait", globals.Behavior, "Kernel-Classes", SmalltalkTrait);
  248. // Manually bootstrap the metaclass hierarchy
  249. globals.ProtoObject.klass.superclass = nilAsClass.klass = globals.Class;
  250. addSubclass(globals.ProtoObject.klass);
  251. };
  252. this.__init__.once = true;
  253. /* Smalltalk classes */
  254. var classes = [];
  255. /* Smalltalk class creation. A class is an instance of an automatically
  256. created metaclass object. Newly created classes (not their metaclass)
  257. should be added to the system, see smalltalk.addClass().
  258. Superclass linking is *not* handled here, see api.initialize() */
  259. function trait (spec) {
  260. var that = new SmalltalkTrait();
  261. that.className = spec.className;
  262. setupClass(that, spec);
  263. return that;
  264. }
  265. function klass (spec) {
  266. var setSuperClass = spec.superclass;
  267. if (!spec.superclass) {
  268. spec.superclass = nilAsClass;
  269. }
  270. var meta = metaclass(spec);
  271. var that = meta.instanceClass;
  272. that.superclass = setSuperClass;
  273. that.fn = spec.fn || inherits(function () {
  274. }, spec.superclass.fn);
  275. that.subclasses = [];
  276. setupClass(that, spec);
  277. that.className = spec.className;
  278. meta.className = spec.className + ' class';
  279. meta.superclass = spec.superclass.klass;
  280. return that;
  281. }
  282. function metaclass (spec) {
  283. var that = new SmalltalkMetaclass();
  284. that.fn = inherits(function () {
  285. }, spec.superclass.klass.fn);
  286. wireKlass(that);
  287. that.instanceClass = new that.fn();
  288. setupClass(that, {});
  289. return that;
  290. }
  291. function setupClass (klass, spec) {
  292. klass.iVarNames = spec.iVarNames || [];
  293. if (spec.pkg) {
  294. klass.pkg = spec.pkg;
  295. }
  296. setupClassOrganization(klass);
  297. Object.defineProperty(klass, "methods", {
  298. value: Object.create(null),
  299. enumerable: false, configurable: true, writable: true
  300. });
  301. }
  302. function wireKlass (klass) {
  303. Object.defineProperty(klass.fn.prototype, "klass", {
  304. value: klass,
  305. enumerable: false, configurable: true, writable: true
  306. });
  307. }
  308. this.wireKlass = wireKlass;
  309. /* Add a class to the system, creating a new one if needed.
  310. A Package is lazily created if one with given name does not exist. */
  311. st.addClass = function (className, superclass, iVarNames, pkgName) {
  312. // While subclassing nil is allowed, it might be an error, so
  313. // warn about it.
  314. if (typeof superclass == 'undefined' || superclass && superclass.isNil) {
  315. console.warn('Compiling ' + className + ' as a subclass of `nil`. A dependency might be missing.');
  316. }
  317. return rawAddClass(pkgName, "class", {className: className, superclass: superclass, iVarNames: iVarNames});
  318. };
  319. var classFactories = this.classFactories = {
  320. "class": klass
  321. };
  322. st.addTrait = function (className, pkgName) {
  323. return rawAddClass(pkgName, "trait", {className: className});
  324. };
  325. classFactories.trait = trait;
  326. function rawAddClass (pkgName, type, spec) {
  327. spec.pkg = st.packages[pkgName];
  328. if (!spec.pkg) {
  329. throw new Error("Missing package " + pkgName);
  330. }
  331. if (spec.superclass == null || spec.superclass.isNil) {
  332. spec.superclass = null;
  333. }
  334. var theClass = globals.hasOwnProperty(spec.className) && globals[spec.className];
  335. if (theClass && theClass.superclass == spec.superclass && !spec.fn) {
  336. if (spec.iVarNames) theClass.iVarNames = spec.iVarNames;
  337. if (spec.pkg) theClass.pkg = spec.pkg;
  338. } else {
  339. if (theClass) {
  340. spec.iVarNames = spec.iVarNames || theClass.iVarNames;
  341. st.removeClass(theClass);
  342. }
  343. theClass = globals[spec.className] = classFactories[type](spec);
  344. addSubclass(theClass);
  345. }
  346. classes.addElement(theClass);
  347. addOrganizationElement(spec.pkg, theClass);
  348. theClass.added();
  349. return theClass;
  350. }
  351. st.removeClass = function (klass) {
  352. removeOrganizationElement(klass.pkg, klass);
  353. classes.removeElement(klass);
  354. removeSubclass(klass);
  355. delete globals[klass.className];
  356. };
  357. function addSubclass (klass) {
  358. if (klass.superclass) {
  359. klass.superclass.subclasses.addElement(klass);
  360. }
  361. }
  362. function removeSubclass (klass) {
  363. if (klass.superclass) {
  364. klass.superclass.subclasses.removeElement(klass);
  365. }
  366. }
  367. /* Create a new class coupling with a JavaScript constructor,
  368. and add it to the system.*/
  369. this.addCoupledClass = function (className, superclass, pkgName, fn) {
  370. return rawAddClass(pkgName, "class", {className: className, superclass: superclass, fn: fn});
  371. };
  372. /* Create an alias for an existing class */
  373. st.alias = function (klass, alias) {
  374. globals[alias] = klass;
  375. };
  376. /* Answer all registered Smalltalk classes */
  377. //TODO: remove the function and make smalltalk.classes an array
  378. st.classes = this.classes = function () {
  379. return classes;
  380. };
  381. function metaSubclasses (metaclass) {
  382. return metaclass.instanceClass.subclasses
  383. .filter(function (each) {
  384. return !each.meta;
  385. })
  386. .map(function (each) {
  387. return each.klass;
  388. });
  389. }
  390. st.metaSubclasses = metaSubclasses;
  391. st.traverseClassTree = function (klass, fn) {
  392. var queue = [klass], sentinel = {};
  393. for (var i = 0; i < queue.length; ++i) {
  394. var item = queue[i];
  395. if (fn(item, sentinel) === sentinel) continue;
  396. var subclasses = item.meta ? metaSubclasses(item) : item.subclasses;
  397. queue.push.apply(queue, subclasses);
  398. }
  399. };
  400. }
  401. MethodsBrik.deps = ["organize", "selectors", "root", "selectorConversion"];
  402. function MethodsBrik (brikz, st) {
  403. var addOrganizationElement = brikz.organize.addOrganizationElement;
  404. var registerSelector = brikz.selectors.registerSelector;
  405. var SmalltalkObject = brikz.root.Object;
  406. function SmalltalkMethod () {
  407. }
  408. inherits(SmalltalkMethod, SmalltalkObject);
  409. this.__init__ = function () {
  410. var globals = brikz.smalltalkGlobals.globals;
  411. var addCoupledClass = brikz.classes.addCoupledClass;
  412. st.addPackage("Kernel-Methods");
  413. addCoupledClass("CompiledMethod", globals.Object, "Kernel-Methods", SmalltalkMethod);
  414. };
  415. this.__init__.once = true;
  416. /* Smalltalk method object. To add a method to a class,
  417. use api.addMethod() */
  418. st.method = function (spec) {
  419. var that = new SmalltalkMethod();
  420. var selector = spec.selector;
  421. that.selector = selector;
  422. that.jsSelector = st.st2js(selector);
  423. that.args = spec.args || {};
  424. that.protocol = spec.protocol;
  425. that.source = spec.source;
  426. that.messageSends = spec.messageSends || [];
  427. that.referencedClasses = spec.referencedClasses || [];
  428. that.fn = spec.fn;
  429. return that;
  430. };
  431. /* Add/remove a method to/from a class */
  432. st.addMethod = function (method, klass) {
  433. klass.methods[method.selector] = method;
  434. method.methodClass = klass;
  435. // During the bootstrap, #addCompiledMethod is not used.
  436. // Therefore we populate the organizer here too
  437. addOrganizationElement(klass, method.protocol);
  438. var newSelectors = [];
  439. function selectorInUse (stSelector) {
  440. var pair = registerSelector(stSelector);
  441. if (pair) {
  442. newSelectors.push(pair);
  443. }
  444. }
  445. selectorInUse(method.selector);
  446. method.messageSends.forEach(selectorInUse);
  447. klass.methodAdded(method);
  448. if (st._selectorsAdded) st._selectorsAdded(newSelectors);
  449. };
  450. st.removeMethod = function (method, klass) {
  451. if (klass.methods[method.selector] !== method) return;
  452. delete klass.methods[method.selector];
  453. klass.methodRemoved(method);
  454. // Do *not* delete protocols from here.
  455. // This is handled by #removeCompiledMethod
  456. };
  457. }
  458. function AugmentsBrik (brikz, st) {
  459. /* Array extensions */
  460. Array.prototype.addElement = function (el) {
  461. if (typeof el === 'undefined') {
  462. return;
  463. }
  464. if (this.indexOf(el) == -1) {
  465. this.push(el);
  466. }
  467. };
  468. Array.prototype.removeElement = function (el) {
  469. var i = this.indexOf(el);
  470. if (i !== -1) {
  471. this.splice(i, 1);
  472. }
  473. };
  474. }
  475. SmalltalkInitBrik.deps = ["globals", "classes"];
  476. function SmalltalkInitBrik (brikz, st) {
  477. var globals = brikz.smalltalkGlobals.globals;
  478. var initialized = false;
  479. var runtimeLoadedPromise = new Promise(function (resolve, reject) {
  480. require(['./kernel-runtime'], resolve, reject);
  481. });
  482. /* Smalltalk initialization. Called on page load */
  483. st.initialize = function () {
  484. return runtimeLoadedPromise.then(function (configureWithRuntime) {
  485. if (initialized) {
  486. return;
  487. }
  488. configureWithRuntime(brikz);
  489. st.alias(globals.Array, "OrderedCollection");
  490. st.alias(globals.Date, "Time");
  491. st.classes().forEach(function (klass) {
  492. klass._initialize();
  493. });
  494. initialized = true;
  495. });
  496. };
  497. }
  498. function SelectorConversionBrik (brikz, st) {
  499. /* Convert a Smalltalk selector into a JS selector */
  500. st.st2js = function (string) {
  501. return '_' + string
  502. .replace(/:/g, '_')
  503. .replace(/[\&]/g, '_and')
  504. .replace(/[\|]/g, '_or')
  505. .replace(/[+]/g, '_plus')
  506. .replace(/-/g, '_minus')
  507. .replace(/[*]/g, '_star')
  508. .replace(/[\/]/g, '_slash')
  509. .replace(/[\\]/g, '_backslash')
  510. .replace(/[\~]/g, '_tild')
  511. .replace(/>/g, '_gt')
  512. .replace(/</g, '_lt')
  513. .replace(/=/g, '_eq')
  514. .replace(/,/g, '_comma')
  515. .replace(/[@]/g, '_at');
  516. };
  517. /* Convert a string to a valid smalltalk selector.
  518. if you modify the following functions, also change st2js
  519. accordingly */
  520. st.js2st = function (selector) {
  521. if (selector.match(/^__/)) {
  522. return binaryJsToSt(selector);
  523. } else {
  524. return keywordJsToSt(selector);
  525. }
  526. };
  527. function keywordJsToSt (selector) {
  528. return selector.replace(/^_/, '').replace(/_/g, ':');
  529. }
  530. function binaryJsToSt (selector) {
  531. return selector
  532. .replace(/^_/, '')
  533. .replace(/_and/g, '&')
  534. .replace(/_or/g, '|')
  535. .replace(/_plus/g, '+')
  536. .replace(/_minus/g, '-')
  537. .replace(/_star/g, '*')
  538. .replace(/_slash/g, '/')
  539. .replace(/_backslash/g, '\\')
  540. .replace(/_tild/g, '~')
  541. .replace(/_gt/g, '>')
  542. .replace(/_lt/g, '<')
  543. .replace(/_eq/g, '=')
  544. .replace(/_comma/g, ',')
  545. .replace(/_at/g, '@');
  546. }
  547. st.st2prop = function (stSelector) {
  548. var colonPosition = stSelector.indexOf(':');
  549. return colonPosition === -1 ? stSelector : stSelector.slice(0, colonPosition);
  550. };
  551. }
  552. /* Adds AMD and requirejs related methods to the api */
  553. function AMDBrik (brikz, st) {
  554. st.amdRequire = require;
  555. st.defaultTransportType = st.defaultTransportType || "amd";
  556. st.defaultAmdNamespace = st.defaultAmdNamespace || "amber_core";
  557. }
  558. /* Defines asReceiver to be present at load time */
  559. /* (logically it belongs more to PrimitiveBrik) */
  560. AsReceiverBrik.deps = ["smalltalkGlobals", "root"];
  561. function AsReceiverBrik (brikz, st) {
  562. var globals = brikz.smalltalkGlobals.globals;
  563. var nilAsReceiver = brikz.root.nilAsReceiver;
  564. /**
  565. * This function is used all over the compiled amber code.
  566. * It takes any value (JavaScript or Smalltalk)
  567. * and returns a proper Amber Smalltalk receiver.
  568. *
  569. * null or undefined -> nilAsReceiver,
  570. * object having Smalltalk signature -> unchanged,
  571. * otherwise wrapped foreign (JS) object
  572. */
  573. this.asReceiver = function (o) {
  574. if (o == null) return nilAsReceiver;
  575. else if (o.klass != null) return o;
  576. else return globals.JSObjectProxy._on_(o);
  577. };
  578. }
  579. var api = {};
  580. var brikz = new Brikz(api);
  581. /* Making smalltalk that can load */
  582. brikz.smalltalkGlobals = SmalltalkGlobalsBrik;
  583. brikz.root = RootBrik;
  584. brikz.selectors = SelectorsBrik;
  585. brikz.organize = OrganizeBrik;
  586. brikz.selectorConversion = SelectorConversionBrik;
  587. brikz.packages = PackagesBrik;
  588. brikz.classes = ClassesBrik;
  589. brikz.methods = MethodsBrik;
  590. brikz.stInit = SmalltalkInitBrik;
  591. brikz.augments = AugmentsBrik;
  592. brikz.asReceiver = AsReceiverBrik;
  593. brikz.amd = AMDBrik;
  594. brikz.rebuild();
  595. return {
  596. api: api,
  597. nil/* TODO deprecate */: brikz.root.nilAsReceiver,
  598. nilAsReceiver: brikz.root.nilAsReceiver,
  599. dnu/* TODO deprecate */: brikz.root.nilAsClass,
  600. nilAsClass: brikz.root.nilAsClass,
  601. globals: brikz.smalltalkGlobals.globals,
  602. asReceiver: brikz.asReceiver.asReceiver
  603. };
  604. });