1
0

boot.js 24 KB

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