1
0

boot.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 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. /* Smalltalk constructors definition */
  45. function SmalltalkObject(){};
  46. function SmalltalkBehavior(){};
  47. function SmalltalkClass(){};
  48. function SmalltalkPackage(){};
  49. function SmalltalkMetaclass(){
  50. this.meta = true;
  51. };
  52. function SmalltalkMethod(){};
  53. function SmalltalkNil(){};
  54. function Smalltalk(){
  55. var st = this;
  56. /* This is the current call context object. While it is publicly available,
  57. Use smalltalk.getThisContext() instead which will answer a safe copy of
  58. the current context */
  59. st.thisContext = undefined;
  60. /* List of all reserved words in JavaScript. They may not be used as variables
  61. in Smalltalk. */
  62. st.reservedWords = ['break', 'case', 'catch', 'class', 'continue', 'debugger',
  63. 'default', 'delete', 'do', 'else', 'finally', 'for', 'function',
  64. 'if', 'in', 'instanceof', 'new', 'private', 'protected',
  65. 'public', 'return', 'static', 'switch', 'this', 'throw',
  66. 'try', 'typeof', 'var', 'void', 'while', 'with', 'yield'];
  67. /* We hold all Packages in a separate Object */
  68. st.packages = {};
  69. /* Smalltalk package creation. To add a Package, use smalltalk.addPackage() */
  70. function pkg(spec) {
  71. var that = new SmalltalkPackage();
  72. that.pkgName = spec.pkgName;
  73. that.requires = spec.requires || [];
  74. return that;
  75. };
  76. /* Smalltalk class creation. A class is an instance of an automatically
  77. created metaclass object. Newly created classes (not their metaclass)
  78. should be added to the smalltalk object, see smalltalk.addClass().
  79. Superclass linking is *not* handled here, see smalltalk.init() */
  80. function klass(spec) {
  81. var spec = spec || {};
  82. var that;
  83. if(spec.meta) {
  84. that = new SmalltalkMetaclass();
  85. } else {
  86. that = new (klass({meta: true})).fn;
  87. that.klass.instanceClass = that;
  88. that.className = spec.className;
  89. that.klass.className = that.className + ' class';
  90. }
  91. that.fn = spec.fn || function(){};
  92. that.superclass = spec.superclass;
  93. that.iVarNames = spec.iVarNames || [];
  94. if(that.superclass) {
  95. that.klass.superclass = that.superclass.klass;
  96. }
  97. that.pkg = spec.pkg;
  98. // For a while we keep the category attribute...
  99. if(!(spec.pkg === undefined)) {
  100. that.category = spec.pkg.pkgName;
  101. }
  102. that.fn.prototype.methods = {};
  103. that.fn.prototype.inheritedMethods = {};
  104. that.fn.prototype.klass = that;
  105. return that;
  106. };
  107. /* Smalltalk method object. To add a method to a class,
  108. use smalltalk.addMethod() */
  109. st.method = function(spec) {
  110. var that = new SmalltalkMethod();
  111. that.selector = spec.selector;
  112. that.jsSelector = spec.jsSelector;
  113. that.args = spec.args || {};
  114. that.category = spec.category;
  115. that.source = spec.source;
  116. that.messageSends = spec.messageSends || [];
  117. that.referencedClasses = spec.referencedClasses || [];
  118. that.fn = spec.fn;
  119. return that
  120. };
  121. /* Initialize a class in its class hierarchy. Handle both class and
  122. metaclasses. */
  123. st.init = function(klass) {
  124. var subclasses = st.subclasses(klass);
  125. var methods;
  126. if(klass.superclass && klass.superclass !== nil) {
  127. methods = st.methods(klass.superclass);
  128. //Methods linking
  129. for(var i in methods) {
  130. if(!klass.fn.prototype.methods[i]) {
  131. klass.fn.prototype.inheritedMethods[i] = methods[i];
  132. klass.fn.prototype[methods[i].jsSelector] = methods[i].fn;
  133. }
  134. }
  135. }
  136. for(var i=0;i<subclasses.length;i++) {
  137. st.init(subclasses[i]);
  138. }
  139. if(klass.klass && !klass.meta) {
  140. st.init(klass.klass);
  141. }
  142. };
  143. /* Answer all registered Packages as Array */
  144. st.packages.all = function() {
  145. var packages = [];
  146. for(var i in st.packages) {
  147. if (!st.packages.hasOwnProperty(i) || typeof(st.packages[i]) === "function") continue;
  148. packages.push(st.packages[i]);
  149. }
  150. return packages
  151. };
  152. /* Answer all registered Smalltalk classes */
  153. st.classes = function() {
  154. var classes = [];
  155. for(var i in st) {
  156. if(i.search(/^[A-Z]/g) != -1) {
  157. classes.push(st[i]);
  158. }
  159. }
  160. return classes
  161. };
  162. /* Answer all methods (included inherited ones) of klass. */
  163. st.methods = function(klass) {
  164. var methods = {};
  165. for(var i in klass.fn.prototype.methods) {
  166. methods[i] = klass.fn.prototype.methods[i]
  167. }
  168. for(var i in klass.fn.prototype.inheritedMethods) {
  169. methods[i] = klass.fn.prototype.inheritedMethods[i]
  170. }
  171. return methods;
  172. }
  173. /* Answer the direct subclasses of klass. */
  174. st.subclasses = function(klass) {
  175. var subclasses = [];
  176. var classes = st.classes();
  177. for(var i in classes) {
  178. if(classes[i].fn) {
  179. //Metaclasses
  180. if(classes[i].klass && classes[i].klass.superclass === klass) {
  181. subclasses.push(classes[i].klass);
  182. }
  183. //Classes
  184. if(classes[i].superclass === klass) {
  185. subclasses.push(classes[i]);
  186. }
  187. }
  188. }
  189. return subclasses;
  190. };
  191. /* Create a new class wrapping a JavaScript constructor, and add it to the
  192. global smalltalk object. Package is lazily created if it does not exist with given name. */
  193. st.mapClassName = function(className, pkgName, fn, superclass) {
  194. var pkg = st.addPackage(pkgName);
  195. st[className] = klass({
  196. className: className,
  197. superclass: superclass,
  198. pkg: pkg,
  199. fn: fn
  200. });
  201. };
  202. /* Add a package to the smalltalk.packages object, creating a new one if needed.
  203. If pkgName is nil or empty we return nil, which is an allowed package for a class. */
  204. st.addPackage = function(pkgName) {
  205. if(!pkgName) {return nil;}
  206. if(!(st.packages[pkgName])) {
  207. st.packages[pkgName] = pkg({
  208. pkgName: pkgName
  209. });
  210. }
  211. return st.packages[pkgName];
  212. };
  213. /* Add a class to the smalltalk object, creating a new one if needed.
  214. Package is lazily created if it does not exist with given name.*/
  215. st.addClass = function(className, superclass, iVarNames, pkgName) {
  216. var pkg = st.addPackage(pkgName);
  217. if(st[className]) {
  218. st[className].superclass = superclass;
  219. st[className].iVarNames = iVarNames;
  220. st[className].pkg = pkg || st[className].pkg;
  221. } else {
  222. st[className] = klass({
  223. className: className,
  224. superclass: superclass,
  225. pkg: pkg,
  226. iVarNames: iVarNames
  227. });
  228. }
  229. };
  230. /* Add a method to a class */
  231. st.addMethod = function(jsSelector, method, klass) {
  232. klass.fn.prototype[jsSelector] = method.fn;
  233. klass.fn.prototype.methods[method.selector] = method;
  234. method.methodClass = klass;
  235. method.jsSelector = jsSelector;
  236. };
  237. /* Handles Smalltalk message send. Automatically converts undefined to the nil object.
  238. If the receiver does not understand the selector, call its #doesNotUnderstand: method */
  239. sendWithoutContext = function(receiver, selector, args, klass) {
  240. if(receiver === undefined || receiver === null) {
  241. receiver = nil;
  242. }
  243. if(!klass && receiver.klass && receiver[selector]) {
  244. return receiver[selector].apply(receiver, args);
  245. } else if(klass && klass.fn.prototype[selector]) {
  246. return klass.fn.prototype[selector].apply(receiver, args)
  247. }
  248. return messageNotUnderstood(receiver, selector, args);
  249. };
  250. /* Handles unhandled errors during message sends */
  251. sendWithContext = function(receiver, selector, args, klass) {
  252. if(st.thisContext) {
  253. return withContextSend(receiver, selector, args, klass);
  254. } else {
  255. try {return withContextSend(receiver, selector, args, klass)}
  256. catch(error) {
  257. // Reset the context stack in any case
  258. st.thisContext = undefined;
  259. if(error.smalltalkError) {
  260. handleError(error);
  261. } else {
  262. throw(error);
  263. }
  264. }
  265. }
  266. };
  267. /* Same as sendWithoutContext but creates a methodContext. */
  268. withContextSend = function(receiver, selector, args, klass) {
  269. var call, context;
  270. if(receiver === undefined || receiver === null) {
  271. receiver = nil;
  272. }
  273. if(!klass && receiver.klass && receiver[selector]) {
  274. context = pushContext(receiver, selector, args);
  275. call = receiver[selector].apply(receiver, args);
  276. popContext(context);
  277. return call;
  278. } else if(klass && klass.fn.prototype[selector]) {
  279. context = pushContext(receiver, selector, args);
  280. call = klass.fn.prototype[selector].apply(receiver, args);
  281. popContext(context);
  282. return call;
  283. }
  284. return messageNotUnderstood(receiver, selector, args);
  285. };
  286. /* Handles Smalltalk errors. Triggers the registered ErrorHandler
  287. (See the Smalltalk class ErrorHandler and its subclasses */
  288. function handleError(error) {
  289. st.thisContext = undefined;
  290. smalltalk.ErrorHandler._current()._handleError_(error);
  291. }
  292. /* Handles #dnu: *and* JavaScript method calls.
  293. if the receiver has no klass, we consider it a JS object (outside of the
  294. Amber system). Else assume that the receiver understands #doesNotUnderstand: */
  295. function messageNotUnderstood(receiver, selector, args) {
  296. /* Handles JS method calls. */
  297. if(receiver.klass === undefined || receiver.allowJavaScriptCalls) {
  298. return callJavaScriptMethod(receiver, selector, args);
  299. }
  300. /* Handles not understood messages. Also see the Amber counter-part
  301. Object>>doesNotUnderstand: */
  302. return receiver._doesNotUnderstand_(
  303. st.Message._new()
  304. ._selector_(st.convertSelector(selector))
  305. ._arguments_(args)
  306. );
  307. };
  308. function callJavaScriptMethod(receiver, selector, args) {
  309. /* Call a method of a JS object, or answer a property if it exists.
  310. Else try wrapping a JSObjectProxy around the receiver.
  311. Converts keyword-based selectors by using the first
  312. keyword only, but keeping all message arguments.
  313. Example:
  314. "self do: aBlock with: anObject" -> "self.do(aBlock, anObject)" */
  315. var jsSelector = selector._asJavaScriptSelector();
  316. var jsProperty = receiver[jsSelector];
  317. if(typeof jsProperty === "function") {
  318. return jsProperty.apply(receiver, args);
  319. } else if(jsProperty !== undefined) {
  320. if(args[0]) {
  321. receiver[jsSelector] = args[0];
  322. return nil;
  323. } else {
  324. return jsProperty
  325. }
  326. }
  327. return st.send(st.JSObjectProxy._on_(receiver), selector, args);
  328. };
  329. /* Reuse old contexts stored in oldContexts */
  330. st.oldContexts = [];
  331. /* Handle thisContext pseudo variable */
  332. st.getThisContext = function() {
  333. if(st.thisContext) {
  334. return st.thisContext.copy();
  335. } else {
  336. return undefined;
  337. }
  338. }
  339. pushContext = function(receiver, selector, temps) {
  340. if(st.thisContext) {
  341. return st.thisContext = st.thisContext.newContext(receiver, selector, temps);
  342. } else {
  343. return st.thisContext = new SmalltalkMethodContext(receiver, selector, temps);
  344. }
  345. };
  346. popContext = function(context) {
  347. if(context) {
  348. context.removeYourself();
  349. }
  350. };
  351. /* Convert a string to a valid smalltalk selector.
  352. if you modify the following functions, also change String>>asSelector
  353. accordingly */
  354. st.convertSelector = function(selector) {
  355. if(selector.match(/__/)) {
  356. return convertBinarySelector(selector);
  357. } else {
  358. return convertKeywordSelector(selector);
  359. }
  360. };
  361. function convertKeywordSelector(selector) {
  362. return selector.replace(/^_/, '').replace(/_/g, ':');
  363. };
  364. function convertBinarySelector(selector) {
  365. return selector
  366. .replace(/^_/, '')
  367. .replace(/_plus/, '+')
  368. .replace(/_minus/, '-')
  369. .replace(/_star/, '*')
  370. .replace(/_slash/, '/')
  371. .replace(/_gt/, '>')
  372. .replace(/_lt/, '<')
  373. .replace(/_eq/, '=')
  374. .replace(/_comma/, ',')
  375. .replace(/_at/, '@')
  376. };
  377. /* Converts a JavaScript object to valid Smalltalk Object */
  378. st.readJSObject = function(js) {
  379. var object = js;
  380. var readObject = (js.constructor === Object);
  381. var readArray = (js.constructor === Array);
  382. if(readObject) {
  383. object = smalltalk.Dictionary._new();
  384. }
  385. for(var i in js) {
  386. if(readObject) {
  387. object._at_put_(i, st.readJSObject(js[i]));
  388. }
  389. if(readArray) {
  390. object[i] = st.readJSObject(js[i]);
  391. }
  392. }
  393. return object;
  394. };
  395. /* Toggle deployment mode (no context will be handled during message send */
  396. st.setDeploymentMode = function() {
  397. st.send = sendWithoutContext;
  398. };
  399. st.setDevelopmentMode = function() {
  400. st.send = sendWithContext;
  401. }
  402. /* Set development mode by default */
  403. st.setDevelopmentMode();
  404. }
  405. function SmalltalkMethodContext(receiver, selector, temps, home) {
  406. var that = this;
  407. that.receiver = receiver;
  408. that.selector = selector;
  409. that.temps = temps || {};
  410. that.homeContext = home;
  411. that.copy = function() {
  412. var home = that.homeContext;
  413. if(home) {home = home.copy()}
  414. return new SmalltalkMethodContext(
  415. that.receiver,
  416. that.selector,
  417. that.temps,
  418. home
  419. );
  420. }
  421. that.newContext = function(receiver, selector, temps) {
  422. var c = smalltalk.oldContexts.pop();
  423. if(c) {
  424. c.homeContext = that;
  425. c.receiver = receiver;
  426. c.selector = selector;
  427. c.temps = temps || {};
  428. } else {
  429. c = new SmalltalkMethodContext(receiver, selector, temps, that);
  430. }
  431. return c;
  432. }
  433. that.removeYourself = function() {
  434. smalltalk.thisContext = that.homeContext;
  435. that.homeContext = undefined;
  436. smalltalk.oldContexts.push(that);
  437. }
  438. }
  439. /* Global Smalltalk objects. */
  440. var nil = new SmalltalkNil();
  441. var smalltalk = new Smalltalk();
  442. if(this.jQuery) {
  443. this.jQuery.allowJavaScriptCalls = true;
  444. }
  445. /****************************************************************************************/
  446. /* Base classes mapping. If you edit this part, do not forget to set the superclass of the
  447. object metaclass to Class after the definition of Object */
  448. smalltalk.mapClassName("Object", "Kernel", SmalltalkObject);
  449. smalltalk.mapClassName("Smalltalk", "Kernel", Smalltalk, smalltalk.Object);
  450. smalltalk.mapClassName("Package", "Kernel", SmalltalkPackage, smalltalk.Object);
  451. smalltalk.mapClassName("Behavior", "Kernel", SmalltalkBehavior, smalltalk.Object);
  452. smalltalk.mapClassName("Class", "Kernel", SmalltalkClass, smalltalk.Behavior);
  453. smalltalk.mapClassName("Metaclass", "Kernel", SmalltalkMetaclass, smalltalk.Behavior);
  454. smalltalk.mapClassName("CompiledMethod", "Kernel", SmalltalkMethod, smalltalk.Object);
  455. smalltalk.Object.klass.superclass = smalltalk.Class;
  456. smalltalk.mapClassName("Number", "Kernel", Number, smalltalk.Object);
  457. smalltalk.mapClassName("BlockClosure", "Kernel", Function, smalltalk.Object);
  458. smalltalk.mapClassName("Boolean", "Kernel", Boolean, smalltalk.Object);
  459. smalltalk.mapClassName("Date", "Kernel", Date, smalltalk.Object);
  460. smalltalk.mapClassName("UndefinedObject", "Kernel", SmalltalkNil, smalltalk.Object);
  461. smalltalk.mapClassName("Collection", "Kernel", null, smalltalk.Object);
  462. smalltalk.mapClassName("SequenceableCollection", "Kernel", null, smalltalk.Collection);
  463. smalltalk.mapClassName("String", "Kernel", String, smalltalk.SequenceableCollection);
  464. smalltalk.mapClassName("Array", "Kernel", Array, smalltalk.SequenceableCollection);
  465. smalltalk.mapClassName("RegularExpression", "Kernel", RegExp, smalltalk.String);
  466. smalltalk.mapClassName("Error", "Kernel", Error, smalltalk.Object);
  467. smalltalk.mapClassName("MethodContext", "Kernel", SmalltalkMethodContext, smalltalk.Object);