1
0

kernel-fundamentals.js 9.6 KB

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