1
0

kernel-fundamentals.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* ====================================================================
  2. |
  3. | Amber Smalltalk
  4. | http://amber-lang.net
  5. |
  6. ======================================================================
  7. ======================================================================
  8. |
  9. | Copyright (c) 2010-2014
  10. | Nicolas Petton <petton.nicolas@gmail.com>
  11. |
  12. | Copyright (c) 2012-2017
  13. | The Amber team https://lolg.it/org/amber/members
  14. | Amber contributors (see /CONTRIBUTORS)
  15. |
  16. | Amber is released under the MIT license
  17. |
  18. | Permission is hereby granted, free of charge, to any person obtaining
  19. | a copy of this software and associated documentation files (the
  20. | 'Software'), to deal in the Software without restriction, including
  21. | without limitation the rights to use, copy, modify, merge, publish,
  22. | distribute, sublicense, and/or sell copies of the Software, and to
  23. | permit persons to whom the Software is furnished to do so, subject to
  24. | the following conditions:
  25. |
  26. | The above copyright notice and this permission notice shall be
  27. | included in all copies or substantial portions of the Software.
  28. |
  29. | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  30. | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  33. | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  34. | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  35. | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. |
  37. ==================================================================== */
  38. //jshint eqnull:true
  39. define(function () {
  40. "use strict";
  41. function inherits (child, parent) {
  42. child.prototype = Object.create(parent.prototype, {
  43. constructor: {
  44. value: child,
  45. enumerable: false, configurable: true, writable: true
  46. }
  47. });
  48. return child;
  49. }
  50. function SmalltalkGlobalsBrik (brikz, st) {
  51. // jshint evil:true
  52. var jsGlobals = new Function("return this")();
  53. var globals = Object.create(jsGlobals);
  54. globals.SmalltalkSettings = {};
  55. this.globals = globals;
  56. }
  57. function EventBrik (brikz, st) {
  58. var emit = {};
  59. this.emit = emit;
  60. this.declareEvent = function (event) {
  61. if (!emit[event]) emit[event] = function () {
  62. };
  63. }
  64. }
  65. function RootBrik (brikz, st) {
  66. /* Smalltalk foundational objects */
  67. var coreFns = this.coreFns = {};
  68. /* SmalltalkRoot is the hidden root of the normal Amber hierarchy.
  69. All objects including `ProtoObject` inherit from SmalltalkRoot.
  70. Detached roots (eg. wrapped JS classes like Number or Date)
  71. do not directly inherit from SmalltalkRoot, but employ a workaround.*/
  72. function SmalltalkRoot () {
  73. }
  74. function SmalltalkProtoObject () {
  75. }
  76. function SmalltalkObject () {
  77. }
  78. coreFns.ProtoObject = inherits(SmalltalkProtoObject, SmalltalkRoot);
  79. coreFns.Object = inherits(SmalltalkObject, SmalltalkProtoObject);
  80. this.Root = SmalltalkRoot;
  81. this.Object = SmalltalkObject;
  82. }
  83. SelectorsBrik.deps = ["selectorConversion"];
  84. function SelectorsBrik (brikz, st) {
  85. var selectorSet = Object.create(null);
  86. var selectors = this.selectors = [];
  87. var selectorPairs = this.selectorPairs = [];
  88. this.registerSelector = function (stSelector) {
  89. if (selectorSet[stSelector]) return null;
  90. var jsSelector = st.st2js(stSelector);
  91. selectorSet[stSelector] = true;
  92. selectors.push(stSelector);
  93. var pair = {st: stSelector, js: jsSelector};
  94. selectorPairs.push(pair);
  95. return pair;
  96. };
  97. st.allSelectors = function () {
  98. return selectors;
  99. };
  100. }
  101. function PackagesBrik (brikz, st) {
  102. st.packages = st.packageDescriptors = {};
  103. /* Add a package load descriptor to the system */
  104. st.addPackage = function (name, properties) {
  105. if (!name) return null;
  106. return st.packageDescriptors[name] = {properties: properties};
  107. };
  108. }
  109. BehaviorsBrik.deps = ["root", "smalltalkGlobals", "arraySet"];
  110. function BehaviorsBrik (brikz, st) {
  111. var globals = brikz.smalltalkGlobals.globals;
  112. var addElement = brikz.arraySet.addElement;
  113. var removeElement = brikz.arraySet.removeElement;
  114. /* Smalltalk classes and traits */
  115. var traitsOrClasses = [];
  116. this.buildTraitOrClass = function (category, builder) {
  117. var traitOrClass = globals.hasOwnProperty(builder.name) && globals[builder.name];
  118. if (traitOrClass) {
  119. builder.updateExisting(traitOrClass);
  120. } else {
  121. traitOrClass = builder.make();
  122. traitOrClass.category = category;
  123. addTraitOrClass(traitOrClass);
  124. }
  125. return traitOrClass;
  126. };
  127. function addTraitOrClass (traitOrClass) {
  128. globals[traitOrClass.name] = traitOrClass;
  129. addElement(traitsOrClasses, traitOrClass);
  130. traitOrClass.added();
  131. }
  132. function removeTraitOrClass (traitOrClass) {
  133. traitOrClass.removed();
  134. removeElement(traitsOrClasses, traitOrClass);
  135. delete globals[traitOrClass.name];
  136. }
  137. this.removeTraitOrClass = removeTraitOrClass;
  138. /* Create an alias for an existing class */
  139. st.alias = function (traitOrClass, alias) {
  140. globals[alias] = traitOrClass;
  141. };
  142. st.traitsOrClasses = this.traitsOrClasses = traitsOrClasses;
  143. }
  144. MethodsBrik.deps = ["event", "selectors", "root", "selectorConversion"];
  145. function MethodsBrik (brikz, st) {
  146. var registerSelector = brikz.selectors.registerSelector;
  147. var SmalltalkObject = brikz.root.Object;
  148. var coreFns = brikz.root.coreFns;
  149. var emit = brikz.event.emit;
  150. var declareEvent = brikz.event.declareEvent;
  151. function SmalltalkMethod () {
  152. }
  153. coreFns.CompiledMethod = inherits(SmalltalkMethod, SmalltalkObject);
  154. /* Smalltalk method object. To add a method to a class,
  155. use api.addMethod() */
  156. st.method = function (spec) {
  157. var that = new SmalltalkMethod();
  158. var selector = spec.selector;
  159. that.selector = selector;
  160. that.jsSelector = st.st2js(selector);
  161. that.args = spec.args || {};
  162. that.protocol = spec.protocol;
  163. that.source = spec.source;
  164. that.messageSends = spec.messageSends || [];
  165. // TODO remove .referencedClasses, have .referencedGlobals
  166. that.referencedClasses = spec.referencedClasses || [];
  167. that.fn = spec.fn;
  168. return that;
  169. };
  170. /* Add/remove a method to/from a class */
  171. st.addMethod = function (method, traitOrBehavior) {
  172. if (method.owner != null) {
  173. throw new Error("addMethod: Method " + method.selector + " already bound to " + method.owner);
  174. }
  175. method.owner = traitOrBehavior;
  176. // TODO deprecation helper; remove
  177. Object.defineProperty(method, "methodClass", {
  178. get: function () {
  179. console.warn("Use of .methodClass deprecated, use .owner");
  180. return method.owner;
  181. },
  182. set: function (v) {
  183. console.warn("Use of .methodClass= deprecated, use .owner=");
  184. method.owner = v;
  185. }
  186. });
  187. registerNewSelectors(method);
  188. traitOrBehavior.localMethods[method.selector] = method;
  189. updateMethod(method.selector, traitOrBehavior);
  190. };
  191. declareEvent("selectorsAdded");
  192. function registerNewSelectors (method) {
  193. var newSelectors = [];
  194. function selectorInUse (stSelector) {
  195. var pair = registerSelector(stSelector);
  196. if (pair) {
  197. newSelectors.push(pair);
  198. }
  199. }
  200. selectorInUse(method.selector);
  201. method.messageSends.forEach(selectorInUse);
  202. emit.selectorsAdded(newSelectors);
  203. }
  204. st.removeMethod = function (method, traitOrBehavior) {
  205. if (traitOrBehavior.localMethods[method.selector] !== method) return;
  206. delete traitOrBehavior.localMethods[method.selector];
  207. updateMethod(method.selector, traitOrBehavior);
  208. };
  209. this.setupMethods = function (traitOrBehavior) {
  210. traitOrBehavior.localMethods = Object.create(null);
  211. traitOrBehavior.methods = Object.create(null);
  212. };
  213. declareEvent("methodReplaced");
  214. function updateMethod (selector, traitOrBehavior) {
  215. var oldMethod = traitOrBehavior.methods[selector],
  216. newMethod = traitOrBehavior.localMethods[selector];
  217. if (oldMethod == null && newMethod == null) {
  218. console.warn("Removal of nonexistent method " + traitOrBehavior + " >> " + selector);
  219. return;
  220. }
  221. if (newMethod === oldMethod) return;
  222. if (newMethod != null) {
  223. traitOrBehavior.methods[selector] = newMethod;
  224. traitOrBehavior.methodAdded(newMethod);
  225. } else {
  226. delete traitOrBehavior.methods[selector];
  227. traitOrBehavior.methodRemoved(oldMethod);
  228. }
  229. emit.methodReplaced(newMethod, oldMethod, traitOrBehavior);
  230. }
  231. this.updateMethod = updateMethod;
  232. }
  233. function ArraySetBrik (brikz, st) {
  234. st.addElement = this.addElement = function (array, el) {
  235. if (typeof el === 'undefined') {
  236. return;
  237. }
  238. if (array.indexOf(el) === -1) {
  239. array.push(el);
  240. }
  241. };
  242. st.removeElement = this.removeElement = function (array, el) {
  243. var i = array.indexOf(el);
  244. if (i !== -1) {
  245. array.splice(i, 1);
  246. }
  247. };
  248. }
  249. function SelectorConversionBrik (brikz, st) {
  250. /* Convert a Smalltalk selector into a JS selector */
  251. st.st2js = function (string) {
  252. return '_' + string
  253. .replace(/:/g, '_')
  254. .replace(/[\&]/g, '_and')
  255. .replace(/[\|]/g, '_or')
  256. .replace(/[+]/g, '_plus')
  257. .replace(/-/g, '_minus')
  258. .replace(/[*]/g, '_star')
  259. .replace(/[\/]/g, '_slash')
  260. .replace(/[\\]/g, '_backslash')
  261. .replace(/[\~]/g, '_tild')
  262. .replace(/%/g, '_percent')
  263. .replace(/>/g, '_gt')
  264. .replace(/</g, '_lt')
  265. .replace(/=/g, '_eq')
  266. .replace(/,/g, '_comma')
  267. .replace(/[@]/g, '_at');
  268. };
  269. /* Convert a string to a valid smalltalk selector.
  270. if you modify the following functions, also change st2js
  271. accordingly */
  272. st.js2st = function (selector) {
  273. if (selector.match(/^__/)) {
  274. return binaryJsToSt(selector);
  275. } else {
  276. return keywordJsToSt(selector);
  277. }
  278. };
  279. function keywordJsToSt (selector) {
  280. return selector.replace(/^_/, '').replace(/_/g, ':');
  281. }
  282. function binaryJsToSt (selector) {
  283. return selector
  284. .replace(/^_/, '')
  285. .replace(/_and/g, '&')
  286. .replace(/_or/g, '|')
  287. .replace(/_plus/g, '+')
  288. .replace(/_minus/g, '-')
  289. .replace(/_star/g, '*')
  290. .replace(/_slash/g, '/')
  291. .replace(/_backslash/g, '\\')
  292. .replace(/_tild/g, '~')
  293. .replace(/_percent/g, '%')
  294. .replace(/_gt/g, '>')
  295. .replace(/_lt/g, '<')
  296. .replace(/_eq/g, '=')
  297. .replace(/_comma/g, ',')
  298. .replace(/_at/g, '@');
  299. }
  300. st.st2prop = function (stSelector) {
  301. var colonPosition = stSelector.indexOf(':');
  302. return colonPosition === -1 ? stSelector : stSelector.slice(0, colonPosition);
  303. };
  304. }
  305. NilBrik.deps = ["root"];
  306. function NilBrik (brikz, st) {
  307. var SmalltalkObject = brikz.root.Object;
  308. var coreFns = brikz.root.coreFns;
  309. function SmalltalkNil () {
  310. }
  311. coreFns.UndefinedObject = inherits(SmalltalkNil, SmalltalkObject);
  312. this.nilAsReceiver = new SmalltalkNil();
  313. this.nilAsValue = this.nilAsReceiver; // TODO null
  314. // Adds an `a$nil` (and legacy `isNil`) property to the `nil` object. When sending
  315. // nil objects from one environment to another, doing
  316. // `anObject == nil` (in JavaScript) does not always answer
  317. // true as the referenced nil object might come from the other
  318. // environment.
  319. Object.defineProperty(this.nilAsReceiver, 'a$nil', {
  320. value: true,
  321. enumerable: false, configurable: false, writable: false
  322. });
  323. Object.defineProperty(this.nilAsReceiver, 'isNil', {
  324. value: true,
  325. enumerable: false, configurable: false, writable: false
  326. });
  327. }
  328. /* Making smalltalk that has basic building blocks */
  329. function configureWithFundamentals (brikz) {
  330. brikz.smalltalkGlobals = SmalltalkGlobalsBrik;
  331. brikz.root = RootBrik;
  332. brikz.nil = NilBrik;
  333. brikz.event = EventBrik;
  334. brikz.arraySet = ArraySetBrik;
  335. brikz.selectorConversion = SelectorConversionBrik;
  336. brikz.selectors = SelectorsBrik;
  337. brikz.packages = PackagesBrik;
  338. brikz.behaviors = BehaviorsBrik;
  339. brikz.methods = MethodsBrik;
  340. brikz.rebuild();
  341. }
  342. return configureWithFundamentals;
  343. });