kernel-fundamentals.js 9.3 KB

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