kernel-fundamentals.js 14 KB

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