kernel-fundamentals.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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) {
  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. 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. 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. function ArraySetBrik (brikz, st) {
  176. st.addElement = this.addElement = function (array, el) {
  177. if (typeof el === 'undefined') {
  178. return;
  179. }
  180. if (array.indexOf(el) === -1) {
  181. array.push(el);
  182. }
  183. };
  184. st.removeElement = this.removeElement = function (array, el) {
  185. var i = array.indexOf(el);
  186. if (i !== -1) {
  187. array.splice(i, 1);
  188. }
  189. };
  190. }
  191. NilBrik.deps = ["root"];
  192. function NilBrik (brikz, st) {
  193. var SmalltalkObject = brikz.root.Object;
  194. var coreFns = brikz.root.coreFns;
  195. function SmalltalkNil () {
  196. }
  197. coreFns.UndefinedObject = inherits(SmalltalkNil, SmalltalkObject);
  198. this.nilAsReceiver = new SmalltalkNil();
  199. this.nilAsValue = this.nilAsReceiver; // TODO null
  200. // Adds an `a$nil` property to the `nil` object. When sending
  201. // nil objects from one environment to another, doing
  202. // `anObject == nil` (in JavaScript) does not always answer
  203. // true as the referenced nil object might come from the other
  204. // environment.
  205. Object.defineProperty(this.nilAsReceiver, 'a$nil', {
  206. value: true,
  207. enumerable: false, configurable: false, writable: false
  208. });
  209. }
  210. /* Making smalltalk that has basic building blocks */
  211. function configureWithFundamentals (brikz) {
  212. brikz.smalltalkGlobals = SmalltalkGlobalsBrik;
  213. brikz.root = RootBrik;
  214. brikz.nil = NilBrik;
  215. brikz.event = EventBrik;
  216. brikz.arraySet = ArraySetBrik;
  217. brikz.selectors = SelectorsBrik;
  218. brikz.packages = PackagesBrik;
  219. brikz.behaviors = BehaviorsBrik;
  220. brikz.methods = MethodsBrik;
  221. brikz();
  222. }
  223. return configureWithFundamentals;
  224. });