1
0

kernel-fundamentals.js 14 KB

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