2
0

boot.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /* ====================================================================
  2. |
  3. | Amber Smalltalk
  4. | http://amber-lang.net
  5. |
  6. ======================================================================
  7. ======================================================================
  8. |
  9. | Copyright (c) 2010-2011
  10. | Nicolas Petton <petton.nicolas@gmail.com>
  11. |
  12. | Amber is released under the MIT license
  13. |
  14. | Permission is hereby granted, free of charge, to any person obtaining
  15. | a copy of this software and associated documentation files (the
  16. | 'Software'), to deal in the Software without restriction, including
  17. | without limitation the rights to use, copy, modify, merge, publish,
  18. | distribute, sublicense, and/or sell copies of the Software, and to
  19. | permit persons to whom the Software is furnished to do so, subject to
  20. | the following conditions:
  21. |
  22. | The above copyright notice and this permission notice shall be
  23. | included in all copies or substantial portions of the Software.
  24. |
  25. | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  26. | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  28. | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  29. | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  30. | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  31. | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. |
  33. ==================================================================== */
  34. /* Make sure that console is defined */
  35. if(typeof console === "undefined") {
  36. this.console = {
  37. log: function() {},
  38. warn: function() {},
  39. info: function() {},
  40. debug: function() {},
  41. error: function() {}
  42. };
  43. }
  44. /* Array extensions */
  45. Array.prototype.addElement = function(el) {
  46. if(typeof el === 'undefined') { return; }
  47. if(this.indexOf(el) == -1) {
  48. this.push(el);
  49. }
  50. };
  51. Array.prototype.removeElement = function(el) {
  52. var i = this.indexOf(el);
  53. if (i !== -1) { this.splice(i, 1); }
  54. };
  55. /* Smalltalk constructors definition */
  56. function SmalltalkObject() {}
  57. function SmalltalkBehavior() {}
  58. function SmalltalkClass() {}
  59. function SmalltalkMetaclass() {
  60. this.meta = true;
  61. }
  62. function SmalltalkPackage() {}
  63. function SmalltalkMethod() {}
  64. function SmalltalkNil() {}
  65. function SmalltalkSymbol(string) {
  66. this.value = string;
  67. }
  68. function SmalltalkOrganizer() {
  69. }
  70. function SmalltalkPackageOrganizer() {
  71. this.elements = [];
  72. }
  73. function SmalltalkClassOrganizer() {
  74. this.elements = [];
  75. }
  76. function inherits(child, parent) {
  77. child.prototype = Object.create(parent.prototype, {
  78. constructor: { value: child,
  79. enumerable: false, configurable: true, writable: true }
  80. });
  81. }
  82. inherits(SmalltalkBehavior, SmalltalkObject);
  83. inherits(SmalltalkClass, SmalltalkBehavior);
  84. inherits(SmalltalkMetaclass, SmalltalkBehavior);
  85. inherits(SmalltalkNil, SmalltalkObject);
  86. inherits(SmalltalkMethod, SmalltalkObject);
  87. inherits(SmalltalkPackage, SmalltalkObject);
  88. inherits(SmalltalkOrganizer, SmalltalkObject);
  89. inherits(SmalltalkPackageOrganizer, SmalltalkOrganizer);
  90. inherits(SmalltalkClassOrganizer, SmalltalkOrganizer);
  91. function Smalltalk() {
  92. var st = this;
  93. /* This is the current call context object. While it is publicly available,
  94. Use smalltalk.getThisContext() instead which will answer a safe copy of
  95. the current context */
  96. st.thisContext = undefined;
  97. /* List of all reserved words in JavaScript. They may not be used as variables
  98. in Smalltalk. */
  99. // list of reserved JavaScript keywords as of
  100. // http://es5.github.com/#x7.6.1.1
  101. // and
  102. // http://people.mozilla.org/~jorendorff/es6-draft.html#sec-7.6.1
  103. st.reservedWords = ['break', 'case', 'catch', 'continue', 'debugger',
  104. 'default', 'delete', 'do', 'else', 'finally', 'for', 'function',
  105. 'if', 'in', 'instanceof', 'new', 'return', 'switch', 'this', 'throw',
  106. 'try', 'typeof', 'var', 'void', 'while', 'with',
  107. // ES5: future use: http://es5.github.com/#x7.6.1.2
  108. 'class', 'const', 'enum', 'export', 'extends', 'import', 'super',
  109. // ES5: future use in strict mode
  110. 'implements', 'interface', 'let', 'package', 'private', 'protected',
  111. 'public', 'static', 'yield'];
  112. var initialized = false;
  113. /* Smalltalk classes */
  114. var classes = [];
  115. var wrappedClasses = [];
  116. /* Method not implemented handlers */
  117. var dnu = {
  118. methods: [],
  119. selectors: [],
  120. get: function (string) {
  121. var index = this.selectors.indexOf(string);
  122. if(index !== -1) {
  123. return this.methods[index];
  124. }
  125. this.selectors.push(string);
  126. var selector = st.selector(string);
  127. var method = {jsSelector: selector, fn: this.createHandler(selector)};
  128. this.methods.push(method);
  129. return method;
  130. },
  131. /* Dnu handler method */
  132. createHandler: function (selector) {
  133. return function () {
  134. var args = Array.prototype.slice.call(arguments);
  135. return messageNotUnderstood(this, selector, args);
  136. };
  137. }
  138. };
  139. /* The symbol table ensures symbol unicity */
  140. var symbolTable = {};
  141. st.symbolFor = function(string) {
  142. if(symbolTable[string] === undefined) {
  143. symbolTable[string] = new SmalltalkSymbol(string);
  144. }
  145. return symbolTable[string];
  146. };
  147. /* Unique ID number generator */
  148. var oid = 0;
  149. st.nextId = function() {
  150. oid += 1;
  151. return oid;
  152. };
  153. /* We hold all Packages in a separate Object */
  154. st.packages = {};
  155. /* Smalltalk package creation. To add a Package, use smalltalk.addPackage() */
  156. function pkg(spec) {
  157. var that = new SmalltalkPackage();
  158. that.pkgName = spec.pkgName;
  159. that.organization = new SmalltalkPackageOrganizer();
  160. that.properties = spec.properties || {};
  161. return that;
  162. }
  163. /* Smalltalk class creation. A class is an instance of an automatically
  164. created metaclass object. Newly created classes (not their metaclass)
  165. should be added to the smalltalk object, see smalltalk.addClass().
  166. Superclass linking is *not* handled here, see smalltalk.init() */
  167. function klass(spec) {
  168. spec = spec || {};
  169. var meta = metaclass(spec);
  170. var that = meta.instanceClass;
  171. that.fn = spec.fn || function() {};
  172. setupClass(that, spec);
  173. that.className = spec.className;
  174. that.wrapped = spec.wrapped || false;
  175. meta.className = spec.className + ' class';
  176. if(spec.superclass) {
  177. that.superclass = spec.superclass;
  178. meta.superclass = spec.superclass.klass;
  179. }
  180. return that;
  181. }
  182. function metaclass(spec) {
  183. spec = spec || {};
  184. var that = new SmalltalkMetaclass();
  185. inherits(
  186. that.fn = function() {},
  187. spec.superclass ? spec.superclass.klass.fn : SmalltalkClass
  188. );
  189. that.instanceClass = new that.fn();
  190. setupClass(that);
  191. return that;
  192. }
  193. function setupClass(klass, spec) {
  194. spec = spec || {};
  195. klass.iVarNames = spec.iVarNames || [];
  196. klass.pkg = spec.pkg;
  197. Object.defineProperty(klass, "toString", {
  198. value: function() { return 'Smalltalk ' + this.className; },
  199. enumerable:false, configurable: true, writable: false
  200. });
  201. klass.organization = new SmalltalkClassOrganizer();
  202. klass.organization.theClass = klass;
  203. Object.defineProperty(klass, "methods", {
  204. value: {},
  205. enumerable: false, configurable: true, writable: true
  206. });
  207. wireKlass(klass);
  208. }
  209. /* Smalltalk method object. To add a method to a class,
  210. use smalltalk.addMethod() */
  211. st.method = function(spec) {
  212. var that = new SmalltalkMethod();
  213. that.selector = spec.selector;
  214. that.jsSelector = spec.jsSelector;
  215. that.args = spec.args || {};
  216. that.category = spec.category;
  217. that.source = spec.source;
  218. that.messageSends = spec.messageSends || [];
  219. that.referencedClasses = spec.referencedClasses || [];
  220. that.fn = spec.fn;
  221. return that;
  222. };
  223. /* Initialize a class in its class hierarchy. Handle both classes and
  224. metaclasses. */
  225. st.init = function(klass) {
  226. st.initClass(klass);
  227. if(klass.klass && !klass.meta) {
  228. st.initClass(klass.klass);
  229. }
  230. };
  231. st.initClass = function(klass) {
  232. if(klass.wrapped) {
  233. copySuperclass(klass);
  234. }
  235. else {
  236. installSuperclass(klass);
  237. }
  238. if(klass === st.Object || klass.wrapped) {
  239. installDnuHandlers(klass);
  240. }
  241. };
  242. function wireKlass(klass) {
  243. Object.defineProperty(klass.fn.prototype, "klass", {
  244. value: klass,
  245. enumerable: false, configurable: true, writable: true
  246. });
  247. }
  248. function installSuperclass(klass) {
  249. // only if the klass has not been initialized yet.
  250. if(klass.fn.prototype._yourself) { return; }
  251. if(klass.superclass && klass.superclass !== nil) {
  252. inherits(klass.fn, klass.superclass.fn);
  253. wireKlass(klass);
  254. reinstallMethods(klass);
  255. }
  256. }
  257. function copySuperclass(klass, superclass) {
  258. for (superclass = superclass || klass.superclass;
  259. superclass && superclass !== nil;
  260. superclass = superclass.superclass) {
  261. for (var keys = Object.keys(superclass.methods), i = 0; i < keys.length; i++) {
  262. installMethodIfAbsent(superclass.methods[keys[i]], klass);
  263. }
  264. }
  265. }
  266. function installMethod(method, klass) {
  267. Object.defineProperty(klass.fn.prototype, method.jsSelector, {
  268. value: method.fn,
  269. enumerable: false, configurable: true, writable: true
  270. });
  271. }
  272. function installMethodIfAbsent(method, klass) {
  273. if(!klass.fn.prototype[method.jsSelector]) {
  274. installMethod(method, klass);
  275. }
  276. }
  277. function reinstallMethods(klass) {
  278. for(var keys = Object.keys(klass.methods), i=0; i<keys.length; i++) {
  279. installMethod(klass.methods[keys[i]], klass);
  280. }
  281. }
  282. function installDnuHandlers(klass) {
  283. var m = dnu.methods;
  284. for(var i=0; i<m.length; i++) {
  285. installMethodIfAbsent(m[i], klass);
  286. }
  287. }
  288. function installNewDnuHandler(newHandler) {
  289. installMethodIfAbsent(newHandler, st.Object);
  290. for(var i = 0; i < wrappedClasses.length; i++) {
  291. installMethodIfAbsent(newHandler, wrappedClasses[i]);
  292. }
  293. }
  294. /* Answer all registered Packages as Array */
  295. // TODO: Remove this hack
  296. st.packages.all = function() {
  297. var packages = [];
  298. for(var i in st.packages) {
  299. if(!st.packages.hasOwnProperty(i) || typeof(st.packages[i]) === "function") continue;
  300. packages.push(st.packages[i]);
  301. }
  302. return packages
  303. };
  304. /* Answer all registered Smalltalk classes */
  305. //TODO: remove the function and make smalltalk.classes an array
  306. st.classes = function() {
  307. return classes;
  308. };
  309. st.wrappedClasses = function() {
  310. return wrappedClasses;
  311. };
  312. /* Answer the direct subclasses of klass. */
  313. st.subclasses = function(klass) {
  314. var subclasses = [];
  315. var classes = st.classes();
  316. for(var i=0; i < classes.length; i++) {
  317. var c = classes[i];
  318. if(c.fn) {
  319. //Classes
  320. if(c.superclass === klass) {
  321. subclasses.push(c);
  322. }
  323. c = c.klass;
  324. //Metaclasses
  325. if(c && c.superclass === klass) {
  326. subclasses.push(c);
  327. }
  328. }
  329. }
  330. return subclasses;
  331. };
  332. /* Create a new class wrapping a JavaScript constructor, and add it to the
  333. global smalltalk object. Package is lazily created if it does not exist with given name. */
  334. st.wrapClassName = function(className, pkgName, fn, superclass, wrapped) {
  335. if(wrapped !== false) {
  336. wrapped = true;
  337. }
  338. var pkg = st.addPackage(pkgName);
  339. st[className] = klass({
  340. className: className,
  341. superclass: superclass,
  342. pkg: pkg,
  343. fn: fn,
  344. wrapped: wrapped
  345. });
  346. classes.addElement(st[className]);
  347. if(wrapped) {wrappedClasses.addElement(st[className])}
  348. pkg.organization.elements.addElement(st[className]);
  349. };
  350. /* Create an alias for an existing class */
  351. st.alias = function(klass, alias) {
  352. st[alias] = klass;
  353. };
  354. /* Add a package to the smalltalk.packages object, creating a new one if needed.
  355. If pkgName is null or empty we return nil, which is an allowed package for a class.
  356. If package already exists we still update the properties of it. */
  357. st.addPackage = function(pkgName, properties) {
  358. if(!pkgName) {return nil;}
  359. if(!(st.packages[pkgName])) {
  360. st.packages[pkgName] = pkg({
  361. pkgName: pkgName,
  362. properties: properties
  363. });
  364. } else {
  365. if(properties) {
  366. st.packages[pkgName].properties = properties;
  367. }
  368. }
  369. return st.packages[pkgName];
  370. };
  371. /* Add a class to the smalltalk object, creating a new one if needed.
  372. A Package is lazily created if it does not exist with given name. */
  373. st.addClass = function(className, superclass, iVarNames, pkgName) {
  374. var pkg = st.addPackage(pkgName);
  375. if (superclass == nil) { superclass = null; }
  376. if(st[className] && st[className].superclass == superclass) {
  377. st[className].superclass = superclass;
  378. st[className].iVarNames = iVarNames;
  379. st[className].pkg = pkg || st[className].pkg;
  380. } else {
  381. if(st[className]) {
  382. st.removeClass(st[className]);
  383. }
  384. st[className] = klass({
  385. className: className,
  386. superclass: superclass,
  387. pkg: pkg,
  388. iVarNames: iVarNames
  389. });
  390. }
  391. classes.addElement(st[className]);
  392. pkg.organization.elements.addElement(st[className]);
  393. };
  394. st.removeClass = function(klass) {
  395. klass.pkg.organization.elements.removeElement(klass);
  396. classes.removeElement(klass);
  397. delete st[klass.className];
  398. };
  399. /*
  400. * Add/remove a method to/from a class
  401. */
  402. st.addMethod = function(jsSelector, method, klass) {
  403. method.jsSelector = jsSelector;
  404. installMethod(method, klass);
  405. klass.methods[method.selector] = method;
  406. method.methodClass = klass;
  407. // During the bootstrap, #addCompiledMethod is not used.
  408. // Therefore we populate the organizer here too
  409. klass.organization.elements.addElement(method.category);
  410. for(var i=0; i<method.messageSends.length; i++) {
  411. var dnuHandler = dnu.get(method.messageSends[i]);
  412. if(initialized) {
  413. installNewDnuHandler(dnuHandler);
  414. }
  415. }
  416. };
  417. st.removeMethod = function(method) {
  418. var protocol = method.category;
  419. var klass = method.methodClass;
  420. delete klass.fn.prototype[st.selector(method.selector)];
  421. delete klass.methods[method.selector];
  422. var selectors = Object.keys(klass.methods);
  423. // Do *not* delete protocols from here.
  424. // This is handled by #removeCompiledMethod
  425. };
  426. /* Handles unhandled errors during message sends */
  427. // simply send the message and handle #dnu:
  428. st.send = function(receiver, selector, args, klass) {
  429. var method;
  430. if(receiver == null) {
  431. receiver = nil;
  432. }
  433. method = klass ? klass.fn.prototype[selector] : receiver.klass && receiver[selector];
  434. if(method) {
  435. return method.apply(receiver, args);
  436. } else {
  437. return messageNotUnderstood(receiver, selector, args);
  438. }
  439. }
  440. st.withContext = function(worker, setup) {
  441. if(st.thisContext) {
  442. st.thisContext.pc++;
  443. return inContext(worker, setup);
  444. } else {
  445. try {return inContext(worker, setup)}
  446. catch(error) {
  447. if(error.smalltalkError) {
  448. handleError(error);
  449. } else {
  450. var errorWrapper = st.JavaScriptException._on_(error);
  451. try {errorWrapper._signal()} catch(ex) {}
  452. errorWrapper._context_(st.getThisContext());
  453. handleError(errorWrapper);
  454. }
  455. // Reset the context stack in any case
  456. st.thisContext = undefined;
  457. // Throw the exception anyway, as we want to stop
  458. // the execution to avoid infinite loops
  459. throw error;
  460. }
  461. }
  462. };
  463. function inContext(worker, setup) {
  464. var context = pushContext(setup);
  465. var result = worker(context);
  466. popContext(context);
  467. return result;
  468. }
  469. /* Handles Smalltalk errors. Triggers the registered ErrorHandler
  470. (See the Smalltalk class ErrorHandler and its subclasses */
  471. function handleError(error) {
  472. st.ErrorHandler._current()._handleError_(error);
  473. }
  474. /* Handles #dnu: *and* JavaScript method calls.
  475. if the receiver has no klass, we consider it a JS object (outside of the
  476. Amber system). Else assume that the receiver understands #doesNotUnderstand: */
  477. function messageNotUnderstood(receiver, selector, args) {
  478. /* Handles JS method calls. */
  479. if(receiver.klass === undefined || receiver.allowJavaScriptCalls) {
  480. return callJavaScriptMethod(receiver, selector, args);
  481. }
  482. /* Handles not understood messages. Also see the Amber counter-part
  483. Object>>doesNotUnderstand: */
  484. return receiver._doesNotUnderstand_(
  485. st.Message._new()
  486. ._selector_(st.convertSelector(selector))
  487. ._arguments_(args)
  488. );
  489. }
  490. /* Call a method of a JS object, or answer a property if it exists.
  491. Else try wrapping a JSObjectProxy around the receiver.
  492. If the object property is a function, then call it, except if it starts with
  493. an uppercase character (we probably want to answer the function itself in this
  494. case and send it #new from Amber).
  495. Converts keyword-based selectors by using the first
  496. keyword only, but keeping all message arguments.
  497. Example:
  498. "self do: aBlock with: anObject" -> "self.do(aBlock, anObject)" */
  499. function callJavaScriptMethod(receiver, selector, args) {
  500. var jsSelector = selector._asJavaScriptSelector();
  501. var jsProperty = receiver[jsSelector];
  502. if(typeof jsProperty === "function" && !/^[A-Z]/.test(jsSelector)) {
  503. return jsProperty.apply(receiver, args);
  504. } else if(jsProperty !== undefined) {
  505. if(args[0]) {
  506. receiver[jsSelector] = args[0];
  507. return nil;
  508. } else {
  509. return jsProperty;
  510. }
  511. }
  512. return st.send(st.JSObjectProxy._on_(receiver), selector, args);
  513. }
  514. /* Handle thisContext pseudo variable */
  515. st.getThisContext = function() {
  516. if(st.thisContext) {
  517. st.thisContext.init();
  518. return st.thisContext;
  519. } else {
  520. return nil;
  521. }
  522. };
  523. function pushContext(setup) {
  524. return st.thisContext = new SmalltalkMethodContext(smalltalk.thisContext, setup);
  525. }
  526. function popContext(context) {
  527. st.thisContext = context.homeContext;
  528. }
  529. /* Convert a Smalltalk selector into a JS selector */
  530. st.selector = function(string) {
  531. var selector = '_' + string;
  532. selector = selector.replace(/:/g, '_');
  533. selector = selector.replace(/[\&]/g, '_and');
  534. selector = selector.replace(/[\|]/g, '_or');
  535. selector = selector.replace(/[+]/g, '_plus');
  536. selector = selector.replace(/-/g, '_minus');
  537. selector = selector.replace(/[*]/g ,'_star');
  538. selector = selector.replace(/[\/]/g ,'_slash');
  539. selector = selector.replace(/[\\]/g ,'_backslash');
  540. selector = selector.replace(/[\~]/g ,'_tild');
  541. selector = selector.replace(/>/g ,'_gt');
  542. selector = selector.replace(/</g ,'_lt');
  543. selector = selector.replace(/=/g ,'_eq');
  544. selector = selector.replace(/,/g ,'_comma');
  545. selector = selector.replace(/[@]/g ,'_at');
  546. return selector
  547. };
  548. /* Convert a string to a valid smalltalk selector.
  549. if you modify the following functions, also change String>>asSelector
  550. accordingly */
  551. st.convertSelector = function(selector) {
  552. if(selector.match(/__/)) {
  553. return convertBinarySelector(selector);
  554. } else {
  555. return convertKeywordSelector(selector);
  556. }
  557. };
  558. function convertKeywordSelector(selector) {
  559. return selector.replace(/^_/, '').replace(/_/g, ':');
  560. }
  561. function convertBinarySelector(selector) {
  562. return selector
  563. .replace(/^_/, '')
  564. .replace(/_and/g, '&')
  565. .replace(/_or/g, '|')
  566. .replace(/_plus/g, '+')
  567. .replace(/_minus/g, '-')
  568. .replace(/_star/g, '*')
  569. .replace(/_slash/g, '/')
  570. .replace(/_backslash/g, '\\')
  571. .replace(/_tild/g, '~')
  572. .replace(/_gt/g, '>')
  573. .replace(/_lt/g, '<')
  574. .replace(/_eq/g, '=')
  575. .replace(/_comma/g, ',')
  576. .replace(/_at/g, '@')
  577. }
  578. /* Converts a JavaScript object to valid Smalltalk Object */
  579. st.readJSObject = function(js) {
  580. var object = js;
  581. var readObject = (js.constructor === Object);
  582. var readArray = (js.constructor === Array);
  583. if(readObject) {
  584. object = st.Dictionary._new();
  585. }
  586. for(var i in js) {
  587. if(readObject) {
  588. object._at_put_(i, st.readJSObject(js[i]));
  589. }
  590. if(readArray) {
  591. object[i] = st.readJSObject(js[i]);
  592. }
  593. }
  594. return object;
  595. };
  596. /* Boolean assertion */
  597. st.assert = function(shouldBeBoolean) {
  598. if ((undefined !== shouldBeBoolean) && (shouldBeBoolean.klass === smalltalk.Boolean)) {
  599. return shouldBeBoolean == true;
  600. } else {
  601. smalltalk.NonBooleanReceiver._new()._object_(shouldBeBoolean)._signal();
  602. }
  603. };
  604. /* Smalltalk initialization. Called on page load */
  605. st.initialize = function() {
  606. if(initialized) { return; }
  607. classes.forEach(function(klass) {
  608. st.init(klass);
  609. });
  610. classes.forEach(function(klass) {
  611. klass._initialize();
  612. });
  613. initialized = true;
  614. };
  615. }
  616. inherits(Smalltalk, SmalltalkObject);
  617. function SmalltalkMethodContext(home, setup) {
  618. this.homeContext = home;
  619. this.setup = setup || function() {};
  620. this.pc = 0;
  621. }
  622. // Fallbacks
  623. SmalltalkMethodContext.prototype.locals = {};
  624. SmalltalkMethodContext.prototype.receiver = null;
  625. SmalltalkMethodContext.prototype.selector = null;
  626. SmalltalkMethodContext.prototype.lookupClass = null;
  627. inherits(SmalltalkMethodContext, SmalltalkObject);
  628. SmalltalkMethodContext.prototype.fill = function(receiver, selector, locals, lookupClass) {
  629. this.receiver = receiver;
  630. this.selector = selector;
  631. this.locals = locals || {};
  632. this.lookupClass = lookupClass;
  633. };
  634. SmalltalkMethodContext.prototype.fillBlock = function(locals, ctx) {
  635. this.locals = locals || {};
  636. this.methodContext = ctx;
  637. };
  638. SmalltalkMethodContext.prototype.init = function() {
  639. var home = this.homeContext;
  640. if(home) {home = home.init()}
  641. this.setup(this);
  642. };
  643. SmalltalkMethodContext.prototype.method = function() {
  644. var method;
  645. var lookup = this.lookupClass || this.receiver.klass;
  646. while(!method && lookup) {
  647. method = lookup.methods[smalltalk.convertSelector(this.selector)];
  648. lookup = lookup.superclass
  649. }
  650. return method;
  651. };
  652. // TODO: this is just wrong :)
  653. SmalltalkMethodContext.prototype.resume = function() {
  654. //Brutally set the receiver as thisContext, then re-enter the function
  655. smalltalk.thisContext = this;
  656. return this.method.apply(receiver, temps);
  657. };
  658. /* Global Smalltalk objects. */
  659. var nil = new SmalltalkNil();
  660. var smalltalk = new Smalltalk();
  661. if(this.jQuery) {
  662. this.jQuery.allowJavaScriptCalls = true;
  663. }
  664. /*
  665. * Answer the smalltalk representation of o.
  666. * Used in message sends
  667. */
  668. var _st = function(o) {
  669. if(o == null) {return nil}
  670. if(o.klass) {return o}
  671. return smalltalk.JSObjectProxy._on_(o);
  672. };
  673. /***************************************** BOOTSTRAP ******************************************/
  674. smalltalk.wrapClassName("Object", "Kernel-Objects", SmalltalkObject, undefined, false);
  675. smalltalk.wrapClassName("Behavior", "Kernel-Classes", SmalltalkBehavior, smalltalk.Object, false);
  676. smalltalk.wrapClassName("Metaclass", "Kernel-Classes", SmalltalkMetaclass, smalltalk.Behavior, false);
  677. smalltalk.wrapClassName("Class", "Kernel-Classes", SmalltalkClass, smalltalk.Behavior, false);
  678. smalltalk.Object.klass.superclass = smalltalk.Class;
  679. smalltalk.wrapClassName("Smalltalk", "Kernel-Objects", Smalltalk, smalltalk.Object, false);
  680. smalltalk.wrapClassName("Package", "Kernel-Objects", SmalltalkPackage, smalltalk.Object, false);
  681. smalltalk.wrapClassName("CompiledMethod", "Kernel-Methods", SmalltalkMethod, smalltalk.Object, false);
  682. smalltalk.wrapClassName("Organizer", "Kernel-Objects", SmalltalkOrganizer, smalltalk.Object, false);
  683. smalltalk.wrapClassName("PackageOrganizer", "Kernel-Objects", SmalltalkPackageOrganizer, smalltalk.Organizer, false);
  684. smalltalk.wrapClassName("ClassOrganizer", "Kernel-Objects", SmalltalkClassOrganizer, smalltalk.Organizer, false);
  685. smalltalk.wrapClassName("Number", "Kernel", Number, smalltalk.Object);
  686. smalltalk.wrapClassName("BlockClosure", "Kernel", Function, smalltalk.Object);
  687. smalltalk.wrapClassName("Boolean", "Kernel", Boolean, smalltalk.Object);
  688. smalltalk.wrapClassName("Date", "Kernel", Date, smalltalk.Object);
  689. smalltalk.wrapClassName("UndefinedObject", "Kernel", SmalltalkNil, smalltalk.Object, false);
  690. smalltalk.addClass("Collection", smalltalk.Object, null, "Kernel");
  691. smalltalk.addClass("IndexableCollection", smalltalk.Collection, null, "Kernel");
  692. smalltalk.addClass("SequenceableCollection", smalltalk.IndexableCollection, null, "Kernel");
  693. smalltalk.addClass("CharacterArray", smalltalk.SequenceableCollection, null, "Kernel");
  694. smalltalk.wrapClassName("String", "Kernel", String, smalltalk.CharacterArray);
  695. smalltalk.wrapClassName("Symbol", "Kernel", SmalltalkSymbol, smalltalk.CharacterArray, false);
  696. smalltalk.wrapClassName("Array", "Kernel", Array, smalltalk.SequenceableCollection);
  697. smalltalk.wrapClassName("RegularExpression", "Kernel", RegExp, smalltalk.Object);
  698. smalltalk.wrapClassName("Error", "Kernel", Error, smalltalk.Object);
  699. smalltalk.wrapClassName("MethodContext", "Kernel", SmalltalkMethodContext, smalltalk.Object, false);
  700. /* Alias definitions */
  701. smalltalk.alias(smalltalk.Array, "OrderedCollection");
  702. smalltalk.alias(smalltalk.Date, "Time");