1
0

boot.js 16 KB

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