kernel-fundamentals.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //jshint eqnull:true
  2. define(['./junk-drawer'], function ($goodies) {
  3. "use strict";
  4. var inherits = $goodies.inherits;
  5. var declareJsMethod = $goodies.declareJsMethod;
  6. var addElement = $goodies.addElement;
  7. var removeElement = $goodies.removeElement;
  8. function SelectorsBrik (brikz, st) {
  9. var selectorSet = Object.create(null);
  10. var selectors = this.selectors = [];
  11. this.registerSelector = function (stSelector) {
  12. if (selectorSet[stSelector]) return false;
  13. selectors.push(stSelector);
  14. return selectorSet[stSelector] = true;
  15. };
  16. st.allSelectors = function () {
  17. return selectors;
  18. };
  19. }
  20. function PackagesBrik (brikz, st) {
  21. st.packageDescriptors = {};
  22. /* Add a package load descriptor to the system */
  23. st.addPackage = function (name, properties) {
  24. if (!name) return null;
  25. return st.packageDescriptors[name] = {properties: properties};
  26. };
  27. }
  28. function FundamentalsFactory (globals, emit) {
  29. var specialConstructors = Object.create(null);
  30. function declareEvent (name) {
  31. declareJsMethod(emit, name);
  32. }
  33. function RootBrik (brikz, st) {
  34. /* Smalltalk foundational objects */
  35. /* SmalltalkRoot is the hidden root of the normal Amber hierarchy.
  36. All objects including `ProtoObject` inherit from SmalltalkRoot.
  37. Detached roots (eg. wrapped JS classes like Number or Date)
  38. do not directly inherit from SmalltalkRoot, but employ a workaround.*/
  39. function SmalltalkRoot () {
  40. }
  41. function SmalltalkProtoObject () {
  42. }
  43. function SmalltalkObject () {
  44. }
  45. specialConstructors.ProtoObject = inherits(SmalltalkProtoObject, SmalltalkRoot);
  46. specialConstructors.Object = inherits(SmalltalkObject, SmalltalkProtoObject);
  47. this.Root = SmalltalkRoot;
  48. this.Object = SmalltalkObject;
  49. }
  50. BehavioralsBrik.deps = ["root"];
  51. function BehavioralsBrik (brikz, st) {
  52. /* Smalltalk classes and traits */
  53. var traitsOrClasses = [];
  54. this.buildTraitOrClass = function (builder) {
  55. var traitOrClass = globals.hasOwnProperty(builder.name) && globals[builder.name];
  56. if (traitOrClass) {
  57. builder.updateExisting(traitOrClass);
  58. return traitOrClass;
  59. } else {
  60. return addTraitOrClass(builder.make());
  61. }
  62. };
  63. declareEvent("behavioralAdded");
  64. function addTraitOrClass (traitOrClass) {
  65. globals[traitOrClass.name] = traitOrClass;
  66. addElement(traitsOrClasses, traitOrClass);
  67. traitOrClass.added();
  68. emit.behavioralAdded(traitOrClass);
  69. return traitOrClass;
  70. }
  71. declareEvent("behavioralRemoved");
  72. function removeTraitOrClass (traitOrClass) {
  73. emit.behavioralRemoved(traitOrClass);
  74. traitOrClass.removed();
  75. removeElement(traitsOrClasses, traitOrClass);
  76. delete globals[traitOrClass.name];
  77. return traitOrClass;
  78. }
  79. this.removeTraitOrClass = removeTraitOrClass;
  80. st.traitsOrClasses = this.traitsOrClasses = traitsOrClasses;
  81. }
  82. MethodsBrik.deps = ["selectors", "root"];
  83. function MethodsBrik (brikz, st) {
  84. var registerSelector = brikz.selectors.registerSelector;
  85. var SmalltalkObject = brikz.root.Object;
  86. function SmalltalkMethod () {
  87. }
  88. specialConstructors.CompiledMethod = inherits(SmalltalkMethod, SmalltalkObject);
  89. /* Smalltalk method object. To add a method to a class,
  90. use api.addMethod() */
  91. st.method = function (spec, factory) {
  92. var that = new SmalltalkMethod();
  93. that.selector = spec.selector;
  94. that.args = spec.args || [];
  95. that.protocol = spec.protocol;
  96. that.source = spec.source;
  97. that.pragmas = spec.pragmas;
  98. that.messageSends = spec.messageSends || [];
  99. // TODO remove .referencedClasses, have .referencedGlobals
  100. that.referencedClasses = spec.referencedClasses || [];
  101. that.fn = spec.fn;
  102. if (factory) that.instantiateFn = factory;
  103. return that;
  104. };
  105. /* Add/remove a method to/from a class */
  106. st.addMethod = function (method, traitOrBehavior) {
  107. if (method.owner != null) {
  108. throw new Error("addMethod: Method " + method.selector + " already bound to " + method.owner);
  109. }
  110. method.owner = traitOrBehavior;
  111. registerNewSelectors(method);
  112. traitOrBehavior.localMethods[method.selector] = method;
  113. updateMethod(method.selector, traitOrBehavior);
  114. };
  115. declareEvent("selectorsAdded");
  116. function registerNewSelectors (method) {
  117. var newSelectors = [];
  118. function selectorInUse (stSelector) {
  119. if (registerSelector(stSelector)) {
  120. newSelectors.push(stSelector);
  121. }
  122. }
  123. selectorInUse(method.selector);
  124. method.messageSends.forEach(selectorInUse);
  125. emit.selectorsAdded(newSelectors);
  126. }
  127. st.removeMethod = function (method, traitOrBehavior) {
  128. if (traitOrBehavior.localMethods[method.selector] !== method) return;
  129. delete traitOrBehavior.localMethods[method.selector];
  130. updateMethod(method.selector, traitOrBehavior);
  131. };
  132. this.setupMethods = function (traitOrBehavior) {
  133. traitOrBehavior.localMethods = Object.create(null);
  134. traitOrBehavior.methods = Object.create(null);
  135. };
  136. function setLocalMethods (traitOrBehavior, newLocalMethods) {
  137. var oldLocalMethods = traitOrBehavior.localMethods;
  138. traitOrBehavior.localMethods = newLocalMethods;
  139. var selector;
  140. for (selector in newLocalMethods) {
  141. updateMethod(selector, traitOrBehavior);
  142. }
  143. for (selector in oldLocalMethods) {
  144. updateMethod(selector, traitOrBehavior);
  145. }
  146. }
  147. this.setLocalMethods = setLocalMethods;
  148. declareEvent("methodReplaced");
  149. function updateMethod (selector, traitOrBehavior) {
  150. var oldMethod = traitOrBehavior.methods[selector],
  151. newMethod = traitOrBehavior.localMethods[selector];
  152. if (oldMethod == null && newMethod == null) {
  153. console.warn("Removal of nonexistent method " + traitOrBehavior + " >> " + selector);
  154. return;
  155. }
  156. if (newMethod === oldMethod) return;
  157. if (newMethod != null) {
  158. if (newMethod.methodClass && newMethod.methodClass !== traitOrBehavior) {
  159. console.warn("Resetting methodClass of " + newMethod.methodClass.name + " >> " + selector + " to " + traitOrBehavior.name);
  160. }
  161. newMethod.methodClass = traitOrBehavior;
  162. if (newMethod.instantiateFn) {
  163. newMethod.fn = newMethod.instantiateFn(traitOrBehavior);
  164. }
  165. traitOrBehavior.methods[selector] = newMethod;
  166. traitOrBehavior.methodAdded(newMethod);
  167. } else {
  168. delete traitOrBehavior.methods[selector];
  169. traitOrBehavior.methodRemoved(oldMethod);
  170. }
  171. emit.methodReplaced(newMethod, oldMethod, traitOrBehavior);
  172. }
  173. this.updateMethod = updateMethod;
  174. }
  175. NilBrik.deps = ["root"];
  176. function NilBrik (brikz, st) {
  177. var SmalltalkObject = brikz.root.Object;
  178. function SmalltalkNil () {
  179. }
  180. specialConstructors.UndefinedObject = inherits(SmalltalkNil, SmalltalkObject);
  181. this.nilAsReceiver = new SmalltalkNil();
  182. this.nilAsValue = this.nilAsReceiver; // TODO null
  183. // Adds an `a$nil` property to the `nil` object. When sending
  184. // nil objects from one environment to another, doing
  185. // `anObject == nil` (in JavaScript) does not always answer
  186. // true as the referenced nil object might come from the other
  187. // environment.
  188. Object.defineProperty(this.nilAsReceiver, 'a$nil', {
  189. value: true,
  190. enumerable: false, configurable: false, writable: false
  191. });
  192. }
  193. /* Making smalltalk that has basic building blocks */
  194. function configure (brikz) {
  195. brikz.root = RootBrik;
  196. brikz.nil = NilBrik;
  197. brikz.selectors = SelectorsBrik;
  198. brikz.packages = PackagesBrik;
  199. brikz.behaviorals = BehavioralsBrik;
  200. brikz.methods = MethodsBrik;
  201. brikz();
  202. }
  203. return {configure: configure, specialConstructors: specialConstructors};
  204. }
  205. return FundamentalsFactory;
  206. });