boot.js 25 KB

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