boot.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* ====================================================================
  2. |
  3. | Jtalk Smalltalk
  4. | http://jtalk-project.org
  5. |
  6. ======================================================================
  7. ======================================================================
  8. |
  9. | Copyright (c) 2010-2011
  10. | Nicolas Petton <petton.nicolas@gmail.com>
  11. |
  12. | Jtalk 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 SmalltalkMetaclass(){
  39. this.meta = true;
  40. };
  41. function SmalltalkMethod(){};
  42. function SmalltalkNil(){};
  43. function Smalltalk(){
  44. var st = this;
  45. st.debugMode = true;
  46. /* Smalltalk class creation. A class is an instance of an automatically
  47. created metaclass object. Newly created classes (not their metaclass)
  48. should be added to the smalltalk object, see smalltalk.addClass().
  49. Superclass linking is *not* handled here, see smalltalk.init() */
  50. function klass(spec) {
  51. var spec = spec || {};
  52. var that;
  53. if(spec.meta) {
  54. that = new SmalltalkMetaclass();
  55. } else {
  56. that = new (klass({meta: true})).fn;
  57. that.klass.instanceClass = that;
  58. that.className = spec.className;
  59. that.klass.className = that.className + ' class';
  60. }
  61. that.fn = spec.fn || function(){};
  62. that.superclass = spec.superclass;
  63. that.iVarNames = spec.iVarNames || [];
  64. if(that.superclass) {
  65. that.klass.superclass = that.superclass.klass;
  66. }
  67. that.category = spec.category || "";
  68. that.fn.prototype.methods = {};
  69. that.fn.prototype.inheritedMethods = {};
  70. that.fn.prototype.klass = that;
  71. return that;
  72. };
  73. /* Smalltalk method object. To add a method to a class,
  74. use smalltalk.addMethod() */
  75. st.method = function(spec) {
  76. var that = new SmalltalkMethod();
  77. that.selector = spec.selector;
  78. that.jsSelector = spec.jsSelector;
  79. that.category = spec.category;
  80. that.source = spec.source;
  81. that.messageSends = spec.messageSends || [];
  82. that.referencedClasses = spec.referencedClasses || [];
  83. that.fn = spec.fn;
  84. return that
  85. };
  86. /* Initialize a class in its class hierarchy. Handle both class and
  87. metaclasses. */
  88. st.init = function(klass) {
  89. var subclasses = st.subclasses(klass);
  90. var methods;
  91. // Initializing inst vars
  92. for(var i=0;i<klass.iVarNames.length;i++) {
  93. klass.fn.prototype["@"+klass.iVarNames[i]] = nil;
  94. }
  95. if(klass.superclass && klass.superclass !== nil) {
  96. methods = st.methods(klass.superclass);
  97. //Methods linking
  98. for(var i in methods) {
  99. if(!klass.fn.prototype.methods[i]) {
  100. klass.fn.prototype.inheritedMethods[i] = methods[i];
  101. klass.fn.prototype[methods[i].jsSelector] = methods[i].fn;
  102. }
  103. }
  104. //Instance variables linking
  105. for(var i=0;i<klass.superclass.iVarNames.length;i++) {
  106. if(!klass["@"+klass.superclass.iVarNames[i]]) {
  107. klass.fn.prototype["@"+klass.superclass.iVarNames[i]] = nil;
  108. }
  109. }
  110. }
  111. for(var i=0;i<subclasses.length;i++) {
  112. st.init(subclasses[i]);
  113. }
  114. if(klass.klass && !klass.meta) {
  115. st.init(klass.klass);
  116. }
  117. };
  118. /* Answer all registered Smalltalk classes */
  119. st.classes = function() {
  120. var classes = [];
  121. for(var i in st) {
  122. if(i.search(/^[A-Z]/g) != -1) {
  123. classes.push(st[i]);
  124. }
  125. }
  126. return classes
  127. };
  128. /* Answer all methods (included inherited ones) of klass. */
  129. st.methods = function(klass) {
  130. var methods = {};
  131. for(var i in klass.fn.prototype.methods) {
  132. methods[i] = klass.fn.prototype.methods[i]
  133. }
  134. for(var i in klass.fn.prototype.inheritedMethods) {
  135. methods[i] = klass.fn.prototype.inheritedMethods[i]
  136. }
  137. return methods;
  138. }
  139. /* Answer the direct subclasses of klass. */
  140. st.subclasses = function(klass) {
  141. var subclasses = [];
  142. var classes = st.classes();
  143. for(var i in classes) {
  144. if(classes[i].fn) {
  145. //Metaclasses
  146. if(classes[i].klass && classes[i].klass.superclass === klass) {
  147. subclasses.push(classes[i].klass);
  148. }
  149. //Classes
  150. if(classes[i].superclass === klass) {
  151. subclasses.push(classes[i]);
  152. }
  153. }
  154. }
  155. return subclasses;
  156. };
  157. /* Create a new class wrapping a JavaScript constructor, and add it to the
  158. global smalltalk object. */
  159. st.mapClassName = function(className, category, fn, superclass) {
  160. st[className] = klass({
  161. className: className,
  162. category: category,
  163. superclass: superclass,
  164. fn: fn
  165. });
  166. };
  167. /* Add a class to the smalltalk object, creating a new one if needed. */
  168. st.addClass = function(className, superclass, iVarNames, category) {
  169. if(st[className]) {
  170. st[className].superclass = superclass;
  171. st[className].iVarNames = iVarNames;
  172. st[className].category = category || st[className].category;
  173. } else {
  174. st[className] = klass({
  175. className: className,
  176. iVarNames: iVarNames,
  177. superclass: superclass
  178. });
  179. st[className].category = category || '';
  180. }
  181. };
  182. /* Add a method to a class */
  183. st.addMethod = function(jsSelector, method, klass) {
  184. klass.fn.prototype[jsSelector] = method.fn;
  185. klass.fn.prototype.methods[method.selector] = method;
  186. method.methodClass = klass;
  187. method.jsSelector = jsSelector;
  188. };
  189. /* Handles Smalltalk message send. Automatically converts undefined to the nil object.
  190. If the receiver does not understand the selector, call its #doesNotUnderstand: method */
  191. st.send = function(receiver, selector, args, klass) {
  192. if(typeof receiver === "undefined") {
  193. receiver = nil;
  194. }
  195. if(!klass && receiver.klass && receiver[selector]) {
  196. return receiver[selector].apply(receiver, args);
  197. } else if(klass && klass.fn.prototype[selector]) {
  198. return klass.fn.prototype[selector].apply(receiver, args)
  199. }
  200. return messageNotUnderstood(receiver, selector, args);
  201. };
  202. /* Handles #dnu: *and* JavaScript method calls.
  203. if the receiver has no klass, we consider it a JS object (outside of the
  204. Jtalk system). Else assume that the receiver understands #doesNotUnderstand: */
  205. function messageNotUnderstood(receiver, selector, args) {
  206. /* Handles JS method calls. */
  207. if(receiver.klass === undefined) {
  208. return callJavaScriptMethod(receiver, selector, args);
  209. }
  210. /* Handles not understood messages. Also see the Jtalk counter-part
  211. Object>>doesNotUnderstand: */
  212. return receiver._doesNotUnderstand_(
  213. st.Message._new()
  214. ._selector_(convertSelector(selector))
  215. ._arguments_(args)
  216. );
  217. };
  218. function callJavaScriptMethod(receiver, selector, args) {
  219. /* Call a method of a JS object, or answer a property.
  220. Converts keyword-based selectors by using the first
  221. keyword only, but keeping all message arguments.
  222. Example:
  223. "self do: aBlock with: anObject" -> "self.do(aBlock, anObject)" */
  224. var jsSelector = selector
  225. .replace(/^_/, '')
  226. .replace(/_.*/g, '');
  227. var jsProperty = receiver[jsSelector];
  228. if(typeof jsProperty === "function") {
  229. return jsProperty.apply(receiver, args);
  230. } else if(jsProperty !== undefined) {
  231. return jsProperty
  232. }
  233. smalltalk.Error._signal_(receiver + ' is not a Jtalk object and "' + jsSelector + '" is undefined')
  234. }
  235. /* Convert a string to a valid smalltalk selector.
  236. if you modify the following functions, also change String>>asSelector
  237. accordingly */
  238. function convertSelector(selector) {
  239. if(selector.match(/__/)) {
  240. return convertBinarySelector(selector);
  241. } else {
  242. return convertKeywordSelector(selector);
  243. }
  244. };
  245. function convertKeywordSelector(selector) {
  246. return selector.replace(/^_/, '').replace(/_/g, ':');
  247. };
  248. function convertBinarySelector(selector) {
  249. return selector
  250. .replace(/^_/, '')
  251. .replace(/_plus/, '+')
  252. .replace(/_minus/, '-')
  253. .replace(/_star/, '*')
  254. .replace(/_slash/, '/')
  255. .replace(/_gt/, '>')
  256. .replace(/_lt/, '<')
  257. .replace(/_eq/, '=')
  258. .replace(/_comma/, ',')
  259. .replace(/_at/, '@')
  260. };
  261. /* Converts a JavaScript object to valid Smalltalk Object */
  262. st.readJSObject = function(js) {
  263. var object = js;
  264. var readObject = (js.constructor === Object);
  265. var readArray = (js.constructor === Array);
  266. if(readObject) {
  267. object = smalltalk.Dictionary._new();
  268. }
  269. for(var i in js) {
  270. if(readObject) {
  271. object._at_put_(i, st.readJSObject(js[i]));
  272. }
  273. if(readArray) {
  274. object[i] = st.readJSObject(js[i]);
  275. }
  276. }
  277. return object;
  278. };
  279. }
  280. function SmalltalkMethodContext() {
  281. this.stack = [];
  282. this.push = function(context) {
  283. stack.push(context);
  284. };
  285. this.pop = function() {
  286. stack.pop();
  287. };
  288. }
  289. /* Global Smalltalk objects. nil and thisContext shouldn't be globals. */
  290. var nil = new SmalltalkNil();
  291. var smalltalk = new Smalltalk();
  292. var thisContext = nil;
  293. /****************************************************************************************/
  294. /* Base classes mapping. If you edit this part, do not forget to set the superclass of the
  295. object metaclass to Class after the definition of Object */
  296. smalltalk.mapClassName("Object", "Kernel", SmalltalkObject);
  297. smalltalk.mapClassName("Smalltalk", "Kernel", Smalltalk, smalltalk.Object);
  298. smalltalk.mapClassName("Behavior", "Kernel", SmalltalkBehavior, smalltalk.Object);
  299. smalltalk.mapClassName("Class", "Kernel", SmalltalkClass, smalltalk.Behavior);
  300. smalltalk.mapClassName("Metaclass", "Kernel", SmalltalkMetaclass, smalltalk.Behavior);
  301. smalltalk.mapClassName("CompiledMethod", "Kernel", SmalltalkMethod, smalltalk.Object);
  302. smalltalk.Object.klass.superclass = smalltalk.Class
  303. smalltalk.mapClassName("Number", "Kernel", Number, smalltalk.Object);
  304. smalltalk.mapClassName("BlockClosure", "Kernel", Function, smalltalk.Object);
  305. smalltalk.mapClassName("Boolean", "Kernel", Boolean, smalltalk.Object);
  306. smalltalk.mapClassName("Date", "Kernel", Date, smalltalk.Object);
  307. smalltalk.mapClassName("UndefinedObject", "Kernel", SmalltalkNil, smalltalk.Object);
  308. smalltalk.mapClassName("Collection", "Kernel", null, smalltalk.Object);
  309. smalltalk.mapClassName("SequenceableCollection", "Kernel", null, smalltalk.Collection);
  310. smalltalk.mapClassName("String", "Kernel", String, smalltalk.SequenceableCollection);
  311. smalltalk.mapClassName("Array", "Kernel", Array, smalltalk.SequenceableCollection);
  312. smalltalk.mapClassName("RegularExpression", "Kernel", RegExp, smalltalk.String);
  313. smalltalk.mapClassName("Error", "Kernel", Error, smalltalk.Object);
  314. if(this.CanvasRenderingContext2D) {
  315. smalltalk.mapClassName("CanvasRenderingContext", "Canvas", CanvasRenderingContext2D, smalltalk.Object);
  316. }