1
0

kernel-runtime.js 14 KB

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