2
0

kernel-runtime.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 = ["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 (traitOrClass) {
  77. if (!traitOrClass.trait) initClassAndMetaclass(traitOrClass);
  78. });
  79. st._classAdded = function (klass) {
  80. initClassAndMetaclass(klass);
  81. klass._enterOrganization();
  82. };
  83. st._traitAdded = function (trait) {
  84. trait._enterOrganization();
  85. };
  86. st._classRemoved = function (klass) {
  87. klass._leaveOrganization();
  88. };
  89. st._traitRemoved = function (trait) {
  90. trait._leaveOrganization();
  91. };
  92. function initClass (klass) {
  93. wireKlass(klass);
  94. if (klass.detachedRoot) {
  95. copySuperclass(klass);
  96. }
  97. installMethods(klass);
  98. }
  99. function copySuperclass (klass) {
  100. var myproto = klass.fn.prototype,
  101. superproto = klass.superclass.fn.prototype;
  102. selectors.selectorPairs.forEach(function (selectorPair) {
  103. var jsSelector = selectorPair.js;
  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 = ["manipulation", "dnu", "runtimeClasses"];
  139. function RuntimeMethodsBrik (brikz, st) {
  140. var installMethod = brikz.manipulation.installMethod;
  141. var installJSMethod = brikz.manipulation.installJSMethod;
  142. var makeDnuHandler = brikz.dnu.makeDnuHandler;
  143. var detachedRootClasses = brikz.runtimeClasses.detachedRootClasses;
  144. st._behaviorMethodAdded = function (method, klass) {
  145. installMethod(method, klass);
  146. propagateMethodChange(klass, method, klass);
  147. };
  148. st._selectorsAdded = function (newSelectors) {
  149. var targetClasses = detachedRootClasses();
  150. newSelectors.forEach(function (pair) {
  151. makeDnuHandler(pair, targetClasses);
  152. });
  153. };
  154. st._behaviorMethodRemoved = function (method, klass) {
  155. delete klass.fn.prototype[method.jsSelector];
  156. propagateMethodChange(klass, method, null);
  157. };
  158. st._methodReplaced = function (newMethod, oldMethod, traitOrBehavior) {
  159. traitOrBehavior._methodOrganizationEnter_andLeave_(newMethod, oldMethod);
  160. };
  161. function propagateMethodChange (klass, method, exclude) {
  162. var selector = method.selector;
  163. var jsSelector = method.jsSelector;
  164. st.traverseClassTree(klass, function (subclass, sentinel) {
  165. if (subclass !== exclude) {
  166. if (initMethodInClass(subclass, selector, jsSelector)) return sentinel;
  167. }
  168. });
  169. }
  170. function initMethodInClass (klass, selector, jsSelector) {
  171. if (klass.methods[selector]) return true;
  172. if (klass.detachedRoot) {
  173. installJSMethod(klass.fn.prototype, jsSelector, klass.superclass.fn.prototype[jsSelector]);
  174. }
  175. }
  176. }
  177. PrimitivesBrik.deps = ["smalltalkGlobals"];
  178. function PrimitivesBrik (brikz, st) {
  179. var globals = brikz.smalltalkGlobals.globals;
  180. var oid = 0;
  181. /* Unique ID number generator */
  182. st.nextId = function () {
  183. console.warn("$core.nextId() deprecated. Use your own unique counter.");
  184. oid += 1;
  185. return oid;
  186. };
  187. /* Converts a JavaScript object to valid Smalltalk Object */
  188. st.readJSObject = function (js) {
  189. if (js == null) return null;
  190. else if (Array.isArray(js)) return js.map(st.readJSObject);
  191. else if (js.constructor !== Object) return js;
  192. var pairs = [];
  193. for (var i in js) {
  194. pairs.push(i, st.readJSObject(js[i]));
  195. }
  196. return globals.Dictionary._newFromPairs_(pairs);
  197. };
  198. /* Boolean assertion */
  199. st.assert = function (shouldBeBoolean) {
  200. if (typeof shouldBeBoolean === "boolean") return shouldBeBoolean;
  201. else if (shouldBeBoolean != null && typeof shouldBeBoolean === "object") {
  202. shouldBeBoolean = shouldBeBoolean.valueOf();
  203. if (typeof shouldBeBoolean === "boolean") return shouldBeBoolean;
  204. }
  205. globals.NonBooleanReceiver._signalOn_(shouldBeBoolean);
  206. };
  207. // TODO remove
  208. st.globalJsVariables = [];
  209. }
  210. RuntimeBrik.deps = ["selectorConversion", "smalltalkGlobals", "runtimeClasses"];
  211. function RuntimeBrik (brikz, st) {
  212. var globals = brikz.smalltalkGlobals.globals;
  213. var setClassConstructor = brikz.runtimeClasses.setClassConstructor;
  214. function SmalltalkMethodContext (home, setup) {
  215. // TODO lazy fill of .sendIdx
  216. this.sendIdx = {};
  217. // TODO very likely .senderContext, not .homeContext here
  218. this.homeContext = home;
  219. this.setup = setup;
  220. }
  221. // Fallbacks
  222. SmalltalkMethodContext.prototype.supercall = false;
  223. SmalltalkMethodContext.prototype.locals = Object.freeze({});
  224. SmalltalkMethodContext.prototype.receiver = null;
  225. SmalltalkMethodContext.prototype.selector = null;
  226. SmalltalkMethodContext.prototype.lookupClass = null;
  227. SmalltalkMethodContext.prototype.outerContext = null;
  228. SmalltalkMethodContext.prototype.index = 0;
  229. defineMethod(SmalltalkMethodContext, "fill", function (receiver, selector, locals, lookupClass) {
  230. this.receiver = receiver;
  231. this.selector = selector;
  232. if (locals != null) this.locals = locals;
  233. this.lookupClass = lookupClass;
  234. if (this.homeContext) {
  235. this.homeContext.evaluatedSelector = selector;
  236. }
  237. });
  238. defineMethod(SmalltalkMethodContext, "fillBlock", function (locals, ctx, index) {
  239. if (locals != null) this.locals = locals;
  240. this.outerContext = ctx;
  241. if (index) this.index = index;
  242. });
  243. defineMethod(SmalltalkMethodContext, "init", function () {
  244. var frame = this;
  245. while (frame) {
  246. if (frame.init !== this.init) return frame.init();
  247. frame.setup(frame);
  248. frame = frame.homeContext;
  249. }
  250. });
  251. defineMethod(SmalltalkMethodContext, "method", function () {
  252. var method;
  253. var lookup = this.lookupClass || this.receiver.a$cls;
  254. while (!method && lookup) {
  255. method = lookup.methods[st.js2st(this.selector)];
  256. lookup = lookup.superclass;
  257. }
  258. return method;
  259. });
  260. setClassConstructor(globals.MethodContext, SmalltalkMethodContext);
  261. /* This is the current call context object.
  262. In Smalltalk code, it is accessible just by using 'thisContext' variable.
  263. In JS code, use api.getThisContext() (see below).
  264. */
  265. var thisContext = null;
  266. st.withContext = inContext;
  267. /*
  268. Runs worker function so that error handler is not set up
  269. if there isn't one. This is accomplished by unconditional
  270. wrapping inside a context of a simulated `nil seamlessDoIt` call,
  271. which then stops error handler setup (see st.withContext above).
  272. The effect is, $core.seamless(fn)'s exceptions are not
  273. handed into ST error handler and caller should process them.
  274. */
  275. st.seamless = function (worker) {
  276. var oldContext = thisContext;
  277. thisContext = new SmalltalkMethodContext(thisContext, function (ctx) {
  278. ctx.fill(null, "seamlessDoIt", {}, globals.UndefinedObject);
  279. });
  280. var result = worker(thisContext);
  281. thisContext = oldContext;
  282. return result;
  283. };
  284. function resultWithErrorHandling (worker) {
  285. try {
  286. return worker(thisContext);
  287. } catch (error) {
  288. globals.ErrorHandler._handleError_(error);
  289. thisContext = null;
  290. // Rethrow the error in any case.
  291. throw error;
  292. }
  293. }
  294. function inContext (worker, setup) {
  295. var oldContext = thisContext;
  296. thisContext = new SmalltalkMethodContext(thisContext, setup);
  297. var result = oldContext == null ? resultWithErrorHandling(worker) : worker(thisContext);
  298. thisContext = oldContext;
  299. return result;
  300. }
  301. /* Handle thisContext pseudo variable */
  302. st.getThisContext = function () {
  303. if (thisContext) {
  304. thisContext.init();
  305. return thisContext;
  306. } else {
  307. return null;
  308. }
  309. };
  310. }
  311. MessageSendBrik.deps = ["smalltalkGlobals", "selectorConversion", "root"];
  312. function MessageSendBrik (brikz, st) {
  313. var globals = brikz.smalltalkGlobals.globals;
  314. var nilAsReceiver = brikz.root.nilAsReceiver;
  315. /* Send message programmatically. Used to implement #perform: & Co. */
  316. st.send2 = function (self, selector, args, klass) {
  317. if (self == null) {
  318. self = nilAsReceiver;
  319. }
  320. var method = klass ? klass.fn.prototype[st.st2js(selector)] : self.a$cls && self[st.st2js(selector)];
  321. return method != null ?
  322. method.apply(self, args || []) :
  323. globals.Message._selector_arguments_notUnderstoodBy_(
  324. selector, [].slice.call(args), self.a$cls ? self : wrapJavaScript(self)
  325. );
  326. };
  327. function wrapJavaScript (o) {
  328. return globals.JSObjectProxy._on_(o);
  329. }
  330. st.wrapJavaScript = wrapJavaScript;
  331. /* If the object property is a function, then call it, except if it starts with
  332. an uppercase character (we probably want to answer the function itself in this
  333. case and send it #new from Amber).
  334. */
  335. st.accessJavaScript = function accessJavaScript (self, propertyName, args) {
  336. var propertyValue = self[propertyName];
  337. if (typeof propertyValue === "function" && !/^[A-Z]/.test(propertyName)) {
  338. return propertyValue.apply(self, args || []);
  339. } else if (args.length === 0) {
  340. return propertyValue;
  341. } else {
  342. self[propertyName] = args[0];
  343. return self;
  344. }
  345. };
  346. }
  347. StartImageBrik.deps = ["smalltalkGlobals"];
  348. function StartImageBrik (brikz, st) {
  349. var globals = brikz.smalltalkGlobals.globals;
  350. this.run = function () {
  351. globals.AmberBootstrapInitialization._run();
  352. };
  353. }
  354. /* Making smalltalk that can run */
  355. function configureWithRuntime (brikz) {
  356. brikz.dnu = DNUBrik;
  357. brikz.manipulation = ManipulationBrik;
  358. brikz.runtimeClasses = RuntimeClassesBrik;
  359. brikz.frameBinding = FrameBindingBrik;
  360. brikz.runtimeMethods = RuntimeMethodsBrik;
  361. brikz.messageSend = MessageSendBrik;
  362. brikz.runtime = RuntimeBrik;
  363. brikz.primitives = PrimitivesBrik;
  364. brikz.startImage = StartImageBrik;
  365. brikz.rebuild();
  366. }
  367. return configureWithRuntime;
  368. });