kernel-runtime.js 14 KB

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