kernel-fundamentals.js 14 KB

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