kernel-runtime.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. //jshint eqnull:true
  2. define(function () {
  3. "use strict";
  4. function defineMethod (klass, name, method) {
  5. Object.defineProperty(klass.prototype, name, {
  6. value: method,
  7. enumerable: false, configurable: true, writable: true
  8. });
  9. }
  10. DNUBrik.deps = ["selectors", "smalltalkGlobals", "manipulation", "classes"];
  11. function DNUBrik (brikz, st) {
  12. var selectorsBrik = brikz.selectors;
  13. var globals = brikz.smalltalkGlobals.globals;
  14. var installJSMethod = brikz.manipulation.installJSMethod;
  15. var nilAsClass = brikz.classes.nilAsClass;
  16. /* Method not implemented handlers */
  17. function makeDnuHandler (pair, targetClasses) {
  18. var jsSelector = pair.js;
  19. var fn = createHandler(pair.st);
  20. installJSMethod(nilAsClass.fn.prototype, jsSelector, fn);
  21. targetClasses.forEach(function (target) {
  22. installJSMethod(target.fn.prototype, jsSelector, fn);
  23. });
  24. }
  25. this.makeDnuHandler = makeDnuHandler;
  26. /* Dnu handler method */
  27. function createHandler (stSelector) {
  28. return function () {
  29. return globals.Message._selector_arguments_notUnderstoodBy_(
  30. stSelector, [].slice.call(arguments), this
  31. );
  32. };
  33. }
  34. selectorsBrik.selectorPairs.forEach(function (pair) {
  35. makeDnuHandler(pair, []);
  36. });
  37. }
  38. function ManipulationBrik (brikz, st) {
  39. function installJSMethod (obj, jsSelector, fn) {
  40. Object.defineProperty(obj, jsSelector, {
  41. value: fn,
  42. enumerable: false, configurable: true, writable: true
  43. });
  44. }
  45. function installMethod (method, klass) {
  46. installJSMethod(klass.fn.prototype, method.jsSelector, method.fn);
  47. }
  48. this.installMethod = installMethod;
  49. this.installJSMethod = installJSMethod;
  50. }
  51. RuntimeClassesBrik.deps = ["selectors", "dnu", "behaviors", "classes", "manipulation"];
  52. function RuntimeClassesBrik (brikz, st) {
  53. var selectors = brikz.selectors;
  54. var classes = brikz.behaviors.classes;
  55. var wireKlass = brikz.classes.wireKlass;
  56. var installMethod = brikz.manipulation.installMethod;
  57. var installJSMethod = brikz.manipulation.installJSMethod;
  58. var detachedRootClasses = [];
  59. function markClassDetachedRoot (klass) {
  60. klass.detachedRoot = true;
  61. detachedRootClasses = classes().filter(function (klass) {
  62. return klass.detachedRoot;
  63. });
  64. }
  65. this.detachedRootClasses = function () {
  66. return detachedRootClasses;
  67. };
  68. /* Initialize a class in its class hierarchy. Handle both classes and
  69. metaclasses. */
  70. function initClassAndMetaclass (klass) {
  71. initClass(klass);
  72. if (klass.a$cls && !klass.meta) {
  73. initClass(klass.a$cls);
  74. }
  75. }
  76. classes().forEach(function (klass) {
  77. if (!klass.trait) initClassAndMetaclass(klass);
  78. });
  79. st._classAdded = initClassAndMetaclass;
  80. function initClass (klass) {
  81. wireKlass(klass);
  82. if (klass.detachedRoot) {
  83. copySuperclass(klass);
  84. }
  85. installMethods(klass);
  86. }
  87. function copySuperclass (klass) {
  88. var myproto = klass.fn.prototype,
  89. superproto = klass.superclass.fn.prototype;
  90. selectors.selectorPairs.forEach(function (selectorPair) {
  91. var jsSelector = selectorPair.js;
  92. installJSMethod(myproto, jsSelector, superproto[jsSelector]);
  93. });
  94. }
  95. function installMethods (klass) {
  96. var methods = klass.methods;
  97. Object.keys(methods).forEach(function (selector) {
  98. installMethod(methods[selector], klass);
  99. });
  100. }
  101. /* Manually set the constructor of an existing Smalltalk klass, making it a detached root class. */
  102. st.setClassConstructor = this.setClassConstructor = function (klass, constructor) {
  103. markClassDetachedRoot(klass);
  104. klass.fn = constructor;
  105. initClass(klass);
  106. };
  107. }
  108. FrameBindingBrik.deps = ["smalltalkGlobals", "runtimeClasses"];
  109. function FrameBindingBrik (brikz, st) {
  110. var globals = brikz.smalltalkGlobals.globals;
  111. var setClassConstructor = brikz.runtimeClasses.setClassConstructor;
  112. setClassConstructor(globals.Number, Number);
  113. setClassConstructor(globals.BlockClosure, Function);
  114. setClassConstructor(globals.Boolean, Boolean);
  115. setClassConstructor(globals.Date, Date);
  116. setClassConstructor(globals.String, String);
  117. setClassConstructor(globals.Array, Array);
  118. setClassConstructor(globals.RegularExpression, RegExp);
  119. setClassConstructor(globals.Error, Error);
  120. setClassConstructor(globals.Promise, Promise);
  121. this.__init__ = function () {
  122. st.alias(globals.Array, "OrderedCollection");
  123. st.alias(globals.Date, "Time");
  124. }
  125. }
  126. RuntimeMethodsBrik.deps = ["manipulation", "dnu", "runtimeClasses"];
  127. function RuntimeMethodsBrik (brikz, st) {
  128. var installMethod = brikz.manipulation.installMethod;
  129. var installJSMethod = brikz.manipulation.installJSMethod;
  130. var makeDnuHandler = brikz.dnu.makeDnuHandler;
  131. var detachedRootClasses = brikz.runtimeClasses.detachedRootClasses;
  132. st._methodAdded = function (method, klass) {
  133. installMethod(method, klass);
  134. propagateMethodChange(klass, method, klass);
  135. };
  136. st._selectorsAdded = function (newSelectors) {
  137. var targetClasses = detachedRootClasses();
  138. newSelectors.forEach(function (pair) {
  139. makeDnuHandler(pair, targetClasses);
  140. });
  141. };
  142. st._methodRemoved = function (method, klass) {
  143. delete klass.fn.prototype[method.jsSelector];
  144. propagateMethodChange(klass, method, null);
  145. };
  146. function propagateMethodChange (klass, method, exclude) {
  147. var selector = method.selector;
  148. var jsSelector = method.jsSelector;
  149. st.traverseClassTree(klass, function (subclass, sentinel) {
  150. if (subclass !== exclude) {
  151. if (initMethodInClass(subclass, selector, jsSelector)) return sentinel;
  152. }
  153. });
  154. }
  155. function initMethodInClass (klass, selector, jsSelector) {
  156. if (klass.methods[selector]) return true;
  157. if (klass.detachedRoot) {
  158. installJSMethod(klass.fn.prototype, jsSelector, klass.superclass.fn.prototype[jsSelector]);
  159. }
  160. }
  161. }
  162. PrimitivesBrik.deps = ["smalltalkGlobals"];
  163. function PrimitivesBrik (brikz, st) {
  164. var globals = brikz.smalltalkGlobals.globals;
  165. var oid = 0;
  166. /* Unique ID number generator */
  167. st.nextId = function () {
  168. console.warn("$core.nextId() deprecated. Use your own unique counter.");
  169. oid += 1;
  170. return oid;
  171. };
  172. /* Converts a JavaScript object to valid Smalltalk Object */
  173. st.readJSObject = function (js) {
  174. if (js == null) return null;
  175. else if (Array.isArray(js)) return js.map(st.readJSObject);
  176. else if (js.constructor !== Object) return js;
  177. var pairs = [];
  178. for (var i in js) {
  179. pairs.push(i, st.readJSObject(js[i]));
  180. }
  181. return globals.Dictionary._newFromPairs_(pairs);
  182. };
  183. /* Boolean assertion */
  184. st.assert = function (shouldBeBoolean) {
  185. if (typeof shouldBeBoolean === "boolean") return shouldBeBoolean;
  186. else if (shouldBeBoolean != null && typeof shouldBeBoolean === "object") {
  187. shouldBeBoolean = shouldBeBoolean.valueOf();
  188. if (typeof shouldBeBoolean === "boolean") return shouldBeBoolean;
  189. }
  190. globals.NonBooleanReceiver._new()._object_(shouldBeBoolean)._signal();
  191. };
  192. // TODO remove
  193. st.globalJsVariables = [];
  194. }
  195. RuntimeBrik.deps = ["selectorConversion", "smalltalkGlobals", "runtimeClasses"];
  196. function RuntimeBrik (brikz, st) {
  197. var globals = brikz.smalltalkGlobals.globals;
  198. var setClassConstructor = brikz.runtimeClasses.setClassConstructor;
  199. function SmalltalkMethodContext (home, setup) {
  200. this.sendIdx = {};
  201. this.homeContext = home;
  202. this.setup = setup;
  203. }
  204. // Fallbacks
  205. SmalltalkMethodContext.prototype.supercall = false;
  206. SmalltalkMethodContext.prototype.locals = Object.freeze({});
  207. SmalltalkMethodContext.prototype.receiver = null;
  208. SmalltalkMethodContext.prototype.selector = null;
  209. SmalltalkMethodContext.prototype.lookupClass = null;
  210. SmalltalkMethodContext.prototype.outerContext = null;
  211. SmalltalkMethodContext.prototype.index = 0;
  212. defineMethod(SmalltalkMethodContext, "fill", function (receiver, selector, locals, lookupClass) {
  213. this.receiver = receiver;
  214. this.selector = selector;
  215. if (locals != null) this.locals = locals;
  216. this.lookupClass = lookupClass;
  217. if (this.homeContext) {
  218. this.homeContext.evaluatedSelector = selector;
  219. }
  220. });
  221. defineMethod(SmalltalkMethodContext, "fillBlock", function (locals, ctx, index) {
  222. if (locals != null) this.locals = locals;
  223. this.outerContext = ctx;
  224. if (index) this.index = index;
  225. });
  226. defineMethod(SmalltalkMethodContext, "init", function () {
  227. var frame = this;
  228. while (frame) {
  229. if (frame.init !== this.init) return frame.init();
  230. frame.setup(frame);
  231. if (frame.outerContext) {
  232. frame.outerContext.init();
  233. }
  234. frame = frame.homeContext;
  235. }
  236. });
  237. defineMethod(SmalltalkMethodContext, "method", function () {
  238. var method;
  239. var lookup = this.lookupClass || this.receiver.a$cls;
  240. while (!method && lookup) {
  241. method = lookup.methods[st.js2st(this.selector)];
  242. lookup = lookup.superclass;
  243. }
  244. return method;
  245. });
  246. setClassConstructor(globals.MethodContext, SmalltalkMethodContext);
  247. /* This is the current call context object.
  248. In Smalltalk code, it is accessible just by using 'thisContext' variable.
  249. In JS code, use api.getThisContext() (see below).
  250. */
  251. var thisContext = null;
  252. st.withContext = function (worker, setup) {
  253. return thisContext != null ?
  254. inContext(worker, setup) :
  255. inContextWithErrorHandling(worker, setup);
  256. };
  257. /*
  258. Runs worker function so that error handler is not set up
  259. if there isn't one. This is accomplished by unconditional
  260. wrapping inside a context of a simulated `nil seamlessDoIt` call,
  261. which then stops error handler setup (see st.withContext above).
  262. The effect is, $core.seamless(fn)'s exceptions are not
  263. handed into ST error handler and caller should process them.
  264. */
  265. st.seamless = function (worker) {
  266. return inContext(worker, function (ctx) {
  267. ctx.fill(null, "seamlessDoIt", {}, globals.UndefinedObject);
  268. });
  269. };
  270. function inContextWithErrorHandling (worker, setup) {
  271. try {
  272. return inContext(worker, setup);
  273. } catch (error) {
  274. globals.ErrorHandler._handleError_(error);
  275. thisContext = null;
  276. // Rethrow the error in any case.
  277. throw error;
  278. }
  279. }
  280. function inContext (worker, setup) {
  281. var oldContext = thisContext;
  282. thisContext = new SmalltalkMethodContext(thisContext, setup);
  283. var result = worker(thisContext);
  284. thisContext = oldContext;
  285. return result;
  286. }
  287. /* Handle thisContext pseudo variable */
  288. st.getThisContext = function () {
  289. if (thisContext) {
  290. thisContext.init();
  291. return thisContext;
  292. } else {
  293. return null;
  294. }
  295. };
  296. }
  297. MessageSendBrik.deps = ["smalltalkGlobals", "selectorConversion", "root"];
  298. function MessageSendBrik (brikz, st) {
  299. var globals = brikz.smalltalkGlobals.globals;
  300. var nilAsReceiver = brikz.root.nilAsReceiver;
  301. /* Send message programmatically. Used to implement #perform: & Co. */
  302. st.send2 = function (self, selector, args, klass) {
  303. if (self == null) {
  304. self = nilAsReceiver;
  305. }
  306. var method = klass ? klass.fn.prototype[st.st2js(selector)] : self.a$cls && self[st.st2js(selector)];
  307. return method != null ?
  308. method.apply(self, args || []) :
  309. globals.Message._selector_arguments_notUnderstoodBy_(
  310. selector, [].slice.call(args), self.a$cls ? self : wrapJavaScript(self)
  311. );
  312. };
  313. function wrapJavaScript (o) {
  314. return globals.JSObjectProxy._on_(o);
  315. }
  316. st.wrapJavaScript = wrapJavaScript;
  317. /* If the object property is a function, then call it, except if it starts with
  318. an uppercase character (we probably want to answer the function itself in this
  319. case and send it #new from Amber).
  320. */
  321. st.accessJavaScript = function accessJavaScript (self, propertyName, args) {
  322. var propertyValue = self[propertyName];
  323. if (typeof propertyValue === "function" && !/^[A-Z]/.test(propertyName)) {
  324. return propertyValue.apply(self, args || []);
  325. } else if (args.length > 0) {
  326. self[propertyName] = args[0];
  327. return self;
  328. } else {
  329. return propertyValue;
  330. }
  331. };
  332. }
  333. /* Making smalltalk that can run */
  334. function configureWithRuntime (brikz) {
  335. brikz.dnu = DNUBrik;
  336. brikz.manipulation = ManipulationBrik;
  337. brikz.runtimeClasses = RuntimeClassesBrik;
  338. brikz.frameBinding = FrameBindingBrik;
  339. brikz.runtimeMethods = RuntimeMethodsBrik;
  340. brikz.messageSend = MessageSendBrik;
  341. brikz.runtime = RuntimeBrik;
  342. brikz.primitives = PrimitivesBrik;
  343. brikz.rebuild();
  344. }
  345. return configureWithRuntime;
  346. });