kernel-fundamentals.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. var globals = Object.create(global);
  52. globals.SmalltalkSettings = {};
  53. this.globals = globals;
  54. }
  55. function EventBrik (brikz, st) {
  56. var emit = {};
  57. this.emit = emit;
  58. this.declareEvent = function (event) {
  59. if (!emit[event]) emit[event] = function () {
  60. };
  61. }
  62. }
  63. function RootBrik (brikz, st) {
  64. /* Smalltalk foundational objects */
  65. var coreFns = this.coreFns = {};
  66. /* SmalltalkRoot is the hidden root of the normal Amber hierarchy.
  67. All objects including `ProtoObject` inherit from SmalltalkRoot.
  68. Detached roots (eg. wrapped JS classes like Number or Date)
  69. do not directly inherit from SmalltalkRoot, but employ a workaround.*/
  70. function SmalltalkRoot () {
  71. }
  72. function SmalltalkProtoObject () {
  73. }
  74. function SmalltalkObject () {
  75. }
  76. coreFns.ProtoObject = inherits(SmalltalkProtoObject, SmalltalkRoot);
  77. coreFns.Object = inherits(SmalltalkObject, SmalltalkProtoObject);
  78. this.Root = SmalltalkRoot;
  79. this.Object = SmalltalkObject;
  80. }
  81. SelectorsBrik.deps = ["selectorConversion"];
  82. function SelectorsBrik (brikz, st) {
  83. var selectorSet = Object.create(null);
  84. var selectors = this.selectors = [];
  85. var selectorPairs = this.selectorPairs = [];
  86. this.registerSelector = function (stSelector) {
  87. if (selectorSet[stSelector]) return null;
  88. var jsSelector = st.st2js(stSelector);
  89. selectorSet[stSelector] = true;
  90. selectors.push(stSelector);
  91. var pair = {st: stSelector, js: jsSelector};
  92. selectorPairs.push(pair);
  93. return pair;
  94. };
  95. st.allSelectors = function () {
  96. return selectors;
  97. };
  98. }
  99. function PackagesBrik (brikz, st) {
  100. st.packages = st.packageDescriptors = {};
  101. /* Add a package load descriptor to the system */
  102. st.addPackage = function (name, properties) {
  103. if (!name) return null;
  104. return st.packageDescriptors[name] = {properties: properties};
  105. };
  106. }
  107. BehaviorsBrik.deps = ["root", "smalltalkGlobals", "arraySet"];
  108. function BehaviorsBrik (brikz, st) {
  109. var globals = brikz.smalltalkGlobals.globals;
  110. var addElement = brikz.arraySet.addElement;
  111. var removeElement = brikz.arraySet.removeElement;
  112. /* Smalltalk classes and traits */
  113. var traitsOrClasses = [];
  114. this.buildTraitOrClass = function (category, builder) {
  115. var traitOrClass = globals.hasOwnProperty(builder.name) && globals[builder.name];
  116. if (traitOrClass) {
  117. builder.updateExisting(traitOrClass);
  118. } else {
  119. traitOrClass = builder.make();
  120. traitOrClass.category = category;
  121. addTraitOrClass(traitOrClass);
  122. }
  123. return traitOrClass;
  124. };
  125. function addTraitOrClass (traitOrClass) {
  126. globals[traitOrClass.name] = traitOrClass;
  127. addElement(traitsOrClasses, traitOrClass);
  128. traitOrClass.added();
  129. }
  130. function removeTraitOrClass (traitOrClass) {
  131. traitOrClass.removed();
  132. removeElement(traitsOrClasses, traitOrClass);
  133. delete globals[traitOrClass.name];
  134. }
  135. this.removeTraitOrClass = removeTraitOrClass;
  136. /* Create an alias for an existing class */
  137. st.alias = function (traitOrClass, alias) {
  138. globals[alias] = traitOrClass;
  139. };
  140. st.traitsOrClasses = this.traitsOrClasses = traitsOrClasses;
  141. }
  142. MethodsBrik.deps = ["event", "selectors", "root", "selectorConversion"];
  143. function MethodsBrik (brikz, st) {
  144. var registerSelector = brikz.selectors.registerSelector;
  145. var SmalltalkObject = brikz.root.Object;
  146. var coreFns = brikz.root.coreFns;
  147. var emit = brikz.event.emit;
  148. var declareEvent = brikz.event.declareEvent;
  149. function SmalltalkMethod () {
  150. }
  151. coreFns.CompiledMethod = inherits(SmalltalkMethod, SmalltalkObject);
  152. /* Smalltalk method object. To add a method to a class,
  153. use api.addMethod() */
  154. st.method = function (spec) {
  155. var that = new SmalltalkMethod();
  156. var selector = spec.selector;
  157. that.selector = selector;
  158. that.jsSelector = st.st2js(selector);
  159. that.args = spec.args || {};
  160. that.protocol = spec.protocol;
  161. that.source = spec.source;
  162. that.messageSends = spec.messageSends || [];
  163. // TODO remove .referencedClasses, have .referencedGlobals
  164. that.referencedClasses = spec.referencedClasses || [];
  165. that.fn = spec.fn;
  166. return that;
  167. };
  168. /* Add/remove a method to/from a class */
  169. st.addMethod = function (method, traitOrBehavior) {
  170. if (method.owner != null) {
  171. throw new Error("addMethod: Method " + method.selector + " already bound to " + method.owner);
  172. }
  173. method.owner = traitOrBehavior;
  174. // TODO deprecation helper; remove
  175. Object.defineProperty(method, "methodClass", {
  176. get: function () {
  177. console.warn("Use of .methodClass deprecated, use .owner");
  178. return method.owner;
  179. },
  180. set: function (v) {
  181. console.warn("Use of .methodClass= deprecated, use .owner=");
  182. method.owner = v;
  183. }
  184. });
  185. registerNewSelectors(method);
  186. traitOrBehavior.localMethods[method.selector] = method;
  187. updateMethod(method.selector, traitOrBehavior);
  188. };
  189. declareEvent("selectorsAdded");
  190. function registerNewSelectors (method) {
  191. var newSelectors = [];
  192. function selectorInUse (stSelector) {
  193. var pair = registerSelector(stSelector);
  194. if (pair) {
  195. newSelectors.push(pair);
  196. }
  197. }
  198. selectorInUse(method.selector);
  199. method.messageSends.forEach(selectorInUse);
  200. emit.selectorsAdded(newSelectors);
  201. }
  202. st.removeMethod = function (method, traitOrBehavior) {
  203. if (traitOrBehavior.localMethods[method.selector] !== method) return;
  204. delete traitOrBehavior.localMethods[method.selector];
  205. updateMethod(method.selector, traitOrBehavior);
  206. };
  207. this.setupMethods = function (traitOrBehavior) {
  208. traitOrBehavior.localMethods = Object.create(null);
  209. traitOrBehavior.methods = Object.create(null);
  210. };
  211. declareEvent("methodReplaced");
  212. function updateMethod (selector, traitOrBehavior) {
  213. var oldMethod = traitOrBehavior.methods[selector],
  214. newMethod = traitOrBehavior.localMethods[selector];
  215. if (oldMethod == null && newMethod == null) {
  216. console.warn("Removal of nonexistent method " + traitOrBehavior + " >> " + selector);
  217. return;
  218. }
  219. if (newMethod === oldMethod) return;
  220. if (newMethod != null) {
  221. traitOrBehavior.methods[selector] = newMethod;
  222. traitOrBehavior.methodAdded(newMethod);
  223. } else {
  224. delete traitOrBehavior.methods[selector];
  225. traitOrBehavior.methodRemoved(oldMethod);
  226. }
  227. emit.methodReplaced(newMethod, oldMethod, traitOrBehavior);
  228. }
  229. this.updateMethod = updateMethod;
  230. }
  231. function ArraySetBrik (brikz, st) {
  232. st.addElement = this.addElement = function (array, el) {
  233. if (typeof el === 'undefined') {
  234. return;
  235. }
  236. if (array.indexOf(el) === -1) {
  237. array.push(el);
  238. }
  239. };
  240. st.removeElement = this.removeElement = function (array, el) {
  241. var i = array.indexOf(el);
  242. if (i !== -1) {
  243. array.splice(i, 1);
  244. }
  245. };
  246. }
  247. function SelectorConversionBrik (brikz, st) {
  248. /* Convert a Smalltalk selector into a JS selector */
  249. st.st2js = function (string) {
  250. return '_' + string
  251. .replace(/:/g, '_')
  252. .replace(/[\&]/g, '_and')
  253. .replace(/[\|]/g, '_or')
  254. .replace(/[+]/g, '_plus')
  255. .replace(/-/g, '_minus')
  256. .replace(/[*]/g, '_star')
  257. .replace(/[\/]/g, '_slash')
  258. .replace(/[\\]/g, '_backslash')
  259. .replace(/[\~]/g, '_tild')
  260. .replace(/%/g, '_percent')
  261. .replace(/>/g, '_gt')
  262. .replace(/</g, '_lt')
  263. .replace(/=/g, '_eq')
  264. .replace(/,/g, '_comma')
  265. .replace(/[@]/g, '_at');
  266. };
  267. /* Convert a string to a valid smalltalk selector.
  268. if you modify the following functions, also change st2js
  269. accordingly */
  270. st.js2st = function (selector) {
  271. if (selector.match(/^__/)) {
  272. return binaryJsToSt(selector);
  273. } else {
  274. return keywordJsToSt(selector);
  275. }
  276. };
  277. function keywordJsToSt (selector) {
  278. return selector.replace(/^_/, '').replace(/_/g, ':');
  279. }
  280. function binaryJsToSt (selector) {
  281. return selector
  282. .replace(/^_/, '')
  283. .replace(/_and/g, '&')
  284. .replace(/_or/g, '|')
  285. .replace(/_plus/g, '+')
  286. .replace(/_minus/g, '-')
  287. .replace(/_star/g, '*')
  288. .replace(/_slash/g, '/')
  289. .replace(/_backslash/g, '\\')
  290. .replace(/_tild/g, '~')
  291. .replace(/_percent/g, '%')
  292. .replace(/_gt/g, '>')
  293. .replace(/_lt/g, '<')
  294. .replace(/_eq/g, '=')
  295. .replace(/_comma/g, ',')
  296. .replace(/_at/g, '@');
  297. }
  298. st.st2prop = function (stSelector) {
  299. var colonPosition = stSelector.indexOf(':');
  300. return colonPosition === -1 ? stSelector : stSelector.slice(0, colonPosition);
  301. };
  302. }
  303. NilBrik.deps = ["root"];
  304. function NilBrik (brikz, st) {
  305. var SmalltalkObject = brikz.root.Object;
  306. var coreFns = brikz.root.coreFns;
  307. function SmalltalkNil () {
  308. }
  309. coreFns.UndefinedObject = inherits(SmalltalkNil, SmalltalkObject);
  310. this.nilAsReceiver = new SmalltalkNil();
  311. this.nilAsValue = this.nilAsReceiver; // TODO null
  312. // Adds an `a$nil` (and legacy `isNil`) property to the `nil` object. When sending
  313. // nil objects from one environment to another, doing
  314. // `anObject == nil` (in JavaScript) does not always answer
  315. // true as the referenced nil object might come from the other
  316. // environment.
  317. Object.defineProperty(this.nilAsReceiver, 'a$nil', {
  318. value: true,
  319. enumerable: false, configurable: false, writable: false
  320. });
  321. Object.defineProperty(this.nilAsReceiver, 'isNil', {
  322. value: true,
  323. enumerable: false, configurable: false, writable: false
  324. });
  325. }
  326. /* Making smalltalk that has basic building blocks */
  327. function configureWithFundamentals (brikz) {
  328. brikz.smalltalkGlobals = SmalltalkGlobalsBrik;
  329. brikz.root = RootBrik;
  330. brikz.nil = NilBrik;
  331. brikz.event = EventBrik;
  332. brikz.arraySet = ArraySetBrik;
  333. brikz.selectorConversion = SelectorConversionBrik;
  334. brikz.selectors = SelectorsBrik;
  335. brikz.packages = PackagesBrik;
  336. brikz.behaviors = BehaviorsBrik;
  337. brikz.methods = MethodsBrik;
  338. brikz.rebuild();
  339. }
  340. return configureWithFundamentals;
  341. });