kernel-runtime.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 selectorPairs = brikz.selectors.selectorPairs;
  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. 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 = ["event", "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 emit = brikz.event.emit;
  59. var detachedRootClasses = [];
  60. function markClassDetachedRoot (klass) {
  61. klass.detachedRoot = true;
  62. detachedRootClasses = classes.filter(function (klass) {
  63. return klass.detachedRoot;
  64. });
  65. }
  66. this.detachedRootClasses = function () {
  67. return 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. classes.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. selectors.selectorPairs.forEach(function (selectorPair) {
  104. var jsSelector = selectorPair.js;
  105. installJSMethod(myproto, jsSelector, superproto[jsSelector]);
  106. });
  107. }
  108. function installMethods (klass) {
  109. var methods = klass.methods;
  110. Object.keys(methods).forEach(function (selector) {
  111. installMethod(methods[selector], klass);
  112. });
  113. }
  114. /* Manually set the constructor of an existing Smalltalk klass, making it a detached root class. */
  115. st.setClassConstructor = this.setClassConstructor = function (klass, constructor) {
  116. markClassDetachedRoot(klass);
  117. klass.fn = constructor;
  118. initClass(klass);
  119. };
  120. }
  121. FrameBindingBrik.deps = ["smalltalkGlobals", "runtimeClasses"];
  122. function FrameBindingBrik (brikz, st) {
  123. var globals = brikz.smalltalkGlobals.globals;
  124. var setClassConstructor = brikz.runtimeClasses.setClassConstructor;
  125. setClassConstructor(globals.Number, Number);
  126. setClassConstructor(globals.BlockClosure, Function);
  127. setClassConstructor(globals.Boolean, Boolean);
  128. setClassConstructor(globals.Date, Date);
  129. setClassConstructor(globals.String, String);
  130. setClassConstructor(globals.Array, Array);
  131. setClassConstructor(globals.RegularExpression, RegExp);
  132. setClassConstructor(globals.Error, Error);
  133. setClassConstructor(globals.Promise, Promise);
  134. this.__init__ = function () {
  135. st.alias(globals.Array, "OrderedCollection");
  136. st.alias(globals.Date, "Time");
  137. }
  138. }
  139. RuntimeMethodsBrik.deps = ["event", "manipulation", "dnu", "runtimeClasses"];
  140. function RuntimeMethodsBrik (brikz, st) {
  141. var installMethod = brikz.manipulation.installMethod;
  142. var installJSMethod = brikz.manipulation.installJSMethod;
  143. var makeDnuHandler = brikz.dnu.makeDnuHandler;
  144. var detachedRootClasses = brikz.runtimeClasses.detachedRootClasses;
  145. var emit = brikz.event.emit;
  146. emit.behaviorMethodAdded = function (method, klass) {
  147. installMethod(method, klass);
  148. propagateMethodChange(klass, method, klass);
  149. };
  150. emit.selectorsAdded = function (newSelectors) {
  151. var targetClasses = detachedRootClasses();
  152. newSelectors.forEach(function (pair) {
  153. makeDnuHandler(pair, targetClasses);
  154. });
  155. };
  156. emit.behaviorMethodRemoved = function (method, klass) {
  157. delete klass.fn.prototype[method.jsSelector];
  158. propagateMethodChange(klass, method, null);
  159. };
  160. emit.methodReplaced = function (newMethod, oldMethod, traitOrBehavior) {
  161. traitOrBehavior._methodOrganizationEnter_andLeave_(newMethod, oldMethod);
  162. };
  163. function propagateMethodChange (klass, method, exclude) {
  164. var selector = method.selector;
  165. var jsSelector = method.jsSelector;
  166. st.traverseClassTree(klass, function (subclass, sentinel) {
  167. if (subclass === exclude) return;
  168. if (subclass.methods[selector]) return sentinel;
  169. if (subclass.detachedRoot) {
  170. installJSMethod(subclass.fn.prototype, jsSelector, subclass.superclass.fn.prototype[jsSelector]);
  171. }
  172. });
  173. }
  174. }
  175. PrimitivesBrik.deps = ["smalltalkGlobals"];
  176. function PrimitivesBrik (brikz, st) {
  177. var globals = brikz.smalltalkGlobals.globals;
  178. var oid = 0;
  179. /* Unique ID number generator */
  180. st.nextId = function () {
  181. console.warn("$core.nextId() deprecated. Use your own unique counter.");
  182. oid += 1;
  183. return oid;
  184. };
  185. /* Converts a JavaScript object to valid Smalltalk Object */
  186. st.readJSObject = function (js) {
  187. if (js == null) return null;
  188. else if (Array.isArray(js)) return js.map(st.readJSObject);
  189. else if (js.constructor !== Object) return js;
  190. var pairs = [];
  191. for (var i in js) {
  192. pairs.push(i, st.readJSObject(js[i]));
  193. }
  194. return globals.Dictionary._newFromPairs_(pairs);
  195. };
  196. /* Boolean assertion */
  197. st.assert = function (shouldBeBoolean) {
  198. if (typeof shouldBeBoolean === "boolean") return shouldBeBoolean;
  199. else if (shouldBeBoolean != null && typeof shouldBeBoolean === "object") {
  200. shouldBeBoolean = shouldBeBoolean.valueOf();
  201. if (typeof shouldBeBoolean === "boolean") return shouldBeBoolean;
  202. }
  203. globals.NonBooleanReceiver._signalOn_(shouldBeBoolean);
  204. };
  205. // TODO remove
  206. st.globalJsVariables = [];
  207. }
  208. RuntimeBrik.deps = ["selectorConversion", "smalltalkGlobals", "runtimeClasses"];
  209. function RuntimeBrik (brikz, st) {
  210. var globals = brikz.smalltalkGlobals.globals;
  211. var setClassConstructor = brikz.runtimeClasses.setClassConstructor;
  212. function SmalltalkMethodContext (home, setup) {
  213. // TODO lazy fill of .sendIdx
  214. this.sendIdx = {};
  215. // TODO very likely .senderContext, not .homeContext here
  216. this.homeContext = home;
  217. this.setup = setup;
  218. }
  219. // Fallbacks
  220. SmalltalkMethodContext.prototype.supercall = false;
  221. SmalltalkMethodContext.prototype.locals = Object.freeze({});
  222. SmalltalkMethodContext.prototype.receiver = null;
  223. SmalltalkMethodContext.prototype.selector = null;
  224. SmalltalkMethodContext.prototype.lookupClass = null;
  225. SmalltalkMethodContext.prototype.outerContext = null;
  226. SmalltalkMethodContext.prototype.index = 0;
  227. defineMethod(SmalltalkMethodContext, "fill", function (receiver, selector, locals, lookupClass) {
  228. this.receiver = receiver;
  229. this.selector = selector;
  230. if (locals != null) this.locals = locals;
  231. this.lookupClass = lookupClass;
  232. if (this.homeContext) {
  233. this.homeContext.evaluatedSelector = selector;
  234. }
  235. });
  236. defineMethod(SmalltalkMethodContext, "fillBlock", function (locals, ctx, index) {
  237. if (locals != null) this.locals = locals;
  238. this.outerContext = ctx;
  239. if (index) this.index = index;
  240. });
  241. defineMethod(SmalltalkMethodContext, "method", function () {
  242. var method;
  243. var lookup = this.lookupClass || this.receiver.a$cls;
  244. while (!method && lookup) {
  245. method = lookup.methods[st.js2st(this.selector)];
  246. lookup = lookup.superclass;
  247. }
  248. return method;
  249. });
  250. setClassConstructor(globals.MethodContext, SmalltalkMethodContext);
  251. /* This is the current call context object.
  252. In Smalltalk code, it is accessible just by using 'thisContext' variable.
  253. In JS code, use api.getThisContext() (see below).
  254. */
  255. var thisContext = null;
  256. /*
  257. Runs worker function so that error handler is not set up
  258. if there isn't one. This is accomplished by unconditional
  259. wrapping inside a context of a simulated `nil seamlessDoIt` call,
  260. which then stops error handler setup (see st.withContext above).
  261. The effect is, $core.seamless(fn)'s exceptions are not
  262. handed into ST error handler and caller should process them.
  263. */
  264. st.seamless = function (worker) {
  265. var oldContext = thisContext;
  266. thisContext = new SmalltalkMethodContext(thisContext, function (ctx) {
  267. ctx.fill(null, "seamlessDoIt", {}, globals.UndefinedObject);
  268. });
  269. var result = worker(thisContext);
  270. thisContext = oldContext;
  271. return result;
  272. };
  273. function resultWithErrorHandling (worker) {
  274. try {
  275. return worker(thisContext);
  276. } catch (error) {
  277. globals.ErrorHandler._handleError_(error);
  278. thisContext = null;
  279. // Rethrow the error in any case.
  280. throw error;
  281. }
  282. }
  283. /*
  284. Standard way to run within context.
  285. Sets up error handler if entering first ST context in a stack.
  286. */
  287. st.withContext = function (worker, setup) {
  288. var oldContext = thisContext;
  289. thisContext = new SmalltalkMethodContext(thisContext, setup);
  290. var result = oldContext == null ? resultWithErrorHandling(worker) : worker(thisContext);
  291. thisContext = oldContext;
  292. return result;
  293. };
  294. /* Handle thisContext pseudo variable */
  295. st.getThisContext = function () {
  296. if (!thisContext) return null;
  297. for (var frame = thisContext; frame; frame = frame.homeContext) {
  298. frame.setup(frame);
  299. }
  300. return thisContext;
  301. };
  302. }
  303. MessageSendBrik.deps = ["smalltalkGlobals", "selectorConversion", "root"];
  304. function MessageSendBrik (brikz, st) {
  305. var globals = brikz.smalltalkGlobals.globals;
  306. var nilAsReceiver = brikz.root.nilAsReceiver;
  307. /* Send message programmatically. Used to implement #perform: & Co. */
  308. st.send2 = function (self, selector, args, klass) {
  309. if (self == null) {
  310. self = nilAsReceiver;
  311. }
  312. var method = klass ? klass.fn.prototype[st.st2js(selector)] : self.a$cls && self[st.st2js(selector)];
  313. return method != null ?
  314. method.apply(self, args || []) :
  315. globals.Message._selector_arguments_notUnderstoodBy_(
  316. selector, [].slice.call(args), self.a$cls ? self : wrapJavaScript(self)
  317. );
  318. };
  319. function wrapJavaScript (o) {
  320. return globals.JSObjectProxy._on_(o);
  321. }
  322. st.wrapJavaScript = wrapJavaScript;
  323. /* If the object property is a function, then call it, except if it starts with
  324. an uppercase character (we probably want to answer the function itself in this
  325. case and send it #new from Amber).
  326. */
  327. st.accessJavaScript = function accessJavaScript (self, propertyName, args) {
  328. var propertyValue = self[propertyName];
  329. if (typeof propertyValue === "function" && !/^[A-Z]/.test(propertyName)) {
  330. return propertyValue.apply(self, args || []);
  331. } else if (args.length === 0) {
  332. return propertyValue;
  333. } else {
  334. self[propertyName] = args[0];
  335. return self;
  336. }
  337. };
  338. }
  339. StartImageBrik.deps = ["smalltalkGlobals"];
  340. function StartImageBrik (brikz, st) {
  341. var globals = brikz.smalltalkGlobals.globals;
  342. this.run = function () {
  343. globals.AmberBootstrapInitialization._run();
  344. };
  345. }
  346. /* Making smalltalk that can run */
  347. function configureWithRuntime (brikz) {
  348. brikz.dnu = DNUBrik;
  349. brikz.manipulation = ManipulationBrik;
  350. brikz.runtimeClasses = RuntimeClassesBrik;
  351. brikz.frameBinding = FrameBindingBrik;
  352. brikz.runtimeMethods = RuntimeMethodsBrik;
  353. brikz.messageSend = MessageSendBrik;
  354. brikz.runtime = RuntimeBrik;
  355. brikz.primitives = PrimitivesBrik;
  356. brikz.startImage = StartImageBrik;
  357. brikz.rebuild();
  358. }
  359. return configureWithRuntime;
  360. });