kernel-fundamentals.js 9.4 KB

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