1
0

kernel-runtime.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. DNUBrik.deps = ["selectors", "selectorConversion", "smalltalkGlobals", "classes"];
  17. function DNUBrik (brikz, st) {
  18. var selectors = brikz.selectors.selectors;
  19. var globals = brikz.smalltalkGlobals.globals;
  20. var nilAsClass = brikz.classes.nilAsClass;
  21. var st2js = brikz.selectorConversion.st2js;
  22. var jsSelectors = this.jsSelectors = [];
  23. /* Method not implemented handlers */
  24. function installNewSelectors (newSelectors, targetClasses) {
  25. newSelectors.forEach(function (selector) {
  26. var jsSelector = st2js(selector);
  27. jsSelectors.push(jsSelector);
  28. var fn = createHandler(selector);
  29. installJSMethod(nilAsClass.fn.prototype, jsSelector, fn);
  30. targetClasses.forEach(function (target) {
  31. installJSMethod(target.fn.prototype, jsSelector, fn);
  32. });
  33. });
  34. }
  35. this.installNewSelectors = installNewSelectors;
  36. /* Dnu handler method */
  37. function createHandler (stSelector) {
  38. return function () {
  39. return globals.Message._selector_arguments_notUnderstoodBy_(
  40. stSelector, [].slice.call(arguments), this
  41. );
  42. };
  43. }
  44. installNewSelectors(selectors, []);
  45. }
  46. RuntimeClassesBrik.deps = ["event", "dnu", "behaviors", "classes", "runtimeMethods"];
  47. function RuntimeClassesBrik (brikz, st) {
  48. var jsSelectors = brikz.dnu.jsSelectors;
  49. var installNewSelectors = brikz.dnu.installNewSelectors;
  50. var installMethod = brikz.runtimeMethods.installMethod;
  51. var traitsOrClasses = brikz.behaviors.traitsOrClasses;
  52. var wireKlass = brikz.classes.wireKlass;
  53. var emit = brikz.event.emit;
  54. var detachedRootClasses = [];
  55. function markClassDetachedRoot (klass) {
  56. klass.detachedRoot = true;
  57. detachedRootClasses = traitsOrClasses.filter(function (klass) {
  58. return klass.detachedRoot;
  59. });
  60. }
  61. this.detachedRootClasses = function () {
  62. return detachedRootClasses;
  63. };
  64. emit.selectorsAdded = function (newSelectors) {
  65. installNewSelectors(newSelectors, detachedRootClasses);
  66. };
  67. /* Initialize a class in its class hierarchy. Handle both classes and
  68. metaclasses. */
  69. function initClassAndMetaclass (klass) {
  70. initClass(klass);
  71. if (klass.a$cls && !klass.meta) {
  72. initClass(klass.a$cls);
  73. }
  74. }
  75. traitsOrClasses.forEach(function (traitOrClass) {
  76. if (!traitOrClass.trait) initClassAndMetaclass(traitOrClass);
  77. });
  78. emit.classAdded = function (klass) {
  79. initClassAndMetaclass(klass);
  80. klass._enterOrganization();
  81. };
  82. emit.traitAdded = function (trait) {
  83. trait._enterOrganization();
  84. };
  85. emit.classRemoved = function (klass) {
  86. klass._leaveOrganization();
  87. };
  88. emit.traitRemoved = function (trait) {
  89. trait._leaveOrganization();
  90. };
  91. function initClass (klass) {
  92. wireKlass(klass);
  93. if (klass.detachedRoot) {
  94. copySuperclass(klass);
  95. }
  96. installMethods(klass);
  97. }
  98. function copySuperclass (klass) {
  99. var myproto = klass.fn.prototype,
  100. superproto = klass.superclass.fn.prototype;
  101. jsSelectors.forEach(function (jsSelector) {
  102. installJSMethod(myproto, jsSelector, superproto[jsSelector]);
  103. });
  104. }
  105. function installMethods (klass) {
  106. var methods = klass.methods;
  107. Object.keys(methods).forEach(function (selector) {
  108. installMethod(methods[selector], klass);
  109. });
  110. }
  111. /* Manually set the constructor of an existing Smalltalk klass, making it a detached root class. */
  112. st.setClassConstructor = this.setClassConstructor = function (klass, constructor) {
  113. markClassDetachedRoot(klass);
  114. klass.fn = constructor;
  115. initClass(klass);
  116. };
  117. }
  118. FrameBindingBrik.deps = ["smalltalkGlobals", "runtimeClasses"];
  119. function FrameBindingBrik (brikz, st) {
  120. var globals = brikz.smalltalkGlobals.globals;
  121. var setClassConstructor = brikz.runtimeClasses.setClassConstructor;
  122. setClassConstructor(globals.Number, Number);
  123. setClassConstructor(globals.BlockClosure, Function);
  124. setClassConstructor(globals.Boolean, Boolean);
  125. setClassConstructor(globals.Date, Date);
  126. setClassConstructor(globals.String, String);
  127. setClassConstructor(globals.Array, Array);
  128. setClassConstructor(globals.RegularExpression, RegExp);
  129. setClassConstructor(globals.Error, Error);
  130. setClassConstructor(globals.Promise, Promise);
  131. this.__init__ = function () {
  132. st.alias(globals.Array, "OrderedCollection");
  133. st.alias(globals.Date, "Time");
  134. }
  135. }
  136. RuntimeMethodsBrik.deps = ["event"];
  137. function RuntimeMethodsBrik (brikz, st) {
  138. var emit = brikz.event.emit;
  139. function installMethod (method, klass) {
  140. installJSMethod(klass.fn.prototype, method.jsSelector, method.fn);
  141. }
  142. this.installMethod = installMethod;
  143. emit.behaviorMethodAdded = function (method, klass) {
  144. installMethod(method, klass);
  145. propagateMethodChange(klass, method, klass);
  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. });