boot.js 25 KB

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