kernel-fundamentals.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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-2016
  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(['./compatibility'], 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. OrganizeBrik.deps = ["arraySet", "root"];
  76. function OrganizeBrik (brikz, st) {
  77. var SmalltalkObject = brikz.root.Object;
  78. var coreFns = brikz.root.coreFns;
  79. var addElement = brikz.arraySet.addElement;
  80. var removeElement = brikz.arraySet.removeElement;
  81. function SmalltalkOrganizer () {
  82. }
  83. function SmalltalkPackageOrganizer () {
  84. this.elements = [];
  85. }
  86. function SmalltalkClassOrganizer () {
  87. this.elements = [];
  88. }
  89. coreFns.Organizer = inherits(SmalltalkOrganizer, SmalltalkObject);
  90. coreFns.PackageOrganizer = inherits(SmalltalkPackageOrganizer, SmalltalkOrganizer);
  91. coreFns.ClassOrganizer = inherits(SmalltalkClassOrganizer, SmalltalkOrganizer);
  92. this.setupClassOrganization = function (behaviorBody) {
  93. behaviorBody.organization = new SmalltalkClassOrganizer();
  94. behaviorBody.organization.theClass = behaviorBody;
  95. };
  96. this.setupPackageOrganization = function (pkg) {
  97. pkg.organization = new SmalltalkPackageOrganizer();
  98. };
  99. this.addOrganizationElement = function (owner, element) {
  100. addElement(owner.organization.elements, element);
  101. };
  102. this.removeOrganizationElement = function (owner, element) {
  103. removeElement(owner.organization.elements, element);
  104. };
  105. }
  106. SelectorsBrik.deps = ["selectorConversion"];
  107. function SelectorsBrik (brikz, st) {
  108. var selectorSet = Object.create(null);
  109. var selectors = this.selectors = [];
  110. var selectorPairs = this.selectorPairs = [];
  111. this.registerSelector = function (stSelector) {
  112. if (selectorSet[stSelector]) return null;
  113. var jsSelector = st.st2js(stSelector);
  114. selectorSet[stSelector] = true;
  115. selectors.push(stSelector);
  116. var pair = {st: stSelector, js: jsSelector};
  117. selectorPairs.push(pair);
  118. return pair;
  119. };
  120. st.allSelectors = function () {
  121. return selectors;
  122. };
  123. }
  124. PackagesBrik.deps = ["organize", "root"];
  125. function PackagesBrik (brikz, st) {
  126. var setupPackageOrganization = brikz.organize.setupPackageOrganization;
  127. var SmalltalkObject = brikz.root.Object;
  128. var coreFns = brikz.root.coreFns;
  129. function SmalltalkPackage () {
  130. }
  131. coreFns.Package = inherits(SmalltalkPackage, SmalltalkObject);
  132. st.packages = {};
  133. /* Smalltalk package creation. To add a Package, use smalltalk.addPackage() */
  134. function pkg (spec) {
  135. var that = new SmalltalkPackage();
  136. that.pkgName = spec.pkgName;
  137. setupPackageOrganization(that);
  138. that.properties = spec.properties || {};
  139. return that;
  140. }
  141. /* Add a package to the system, creating a new one if needed.
  142. If pkgName is null or empty we return nil.
  143. If package already exists we still update the properties of it. */
  144. st.addPackage = function (pkgName, properties) {
  145. if (!pkgName) return null;
  146. if (!st.packages[pkgName]) {
  147. st.packages[pkgName] = pkg({
  148. pkgName: pkgName,
  149. properties: properties
  150. });
  151. } else {
  152. if (properties) {
  153. st.packages[pkgName].properties = properties;
  154. }
  155. }
  156. return st.packages[pkgName];
  157. };
  158. }
  159. BehaviorsBrik.deps = ["organize", "root", "smalltalkGlobals", "arraySet"];
  160. function BehaviorsBrik (brikz, st) {
  161. var addOrganizationElement = brikz.organize.addOrganizationElement;
  162. var removeOrganizationElement = brikz.organize.removeOrganizationElement;
  163. var globals = brikz.smalltalkGlobals.globals;
  164. var SmalltalkObject = brikz.root.Object;
  165. var coreFns = brikz.root.coreFns;
  166. var addElement = brikz.arraySet.addElement;
  167. var removeElement = brikz.arraySet.removeElement;
  168. function SmalltalkBehaviorBody () {
  169. }
  170. coreFns.BehaviorBody = inherits(SmalltalkBehaviorBody, SmalltalkObject);
  171. this.BehaviorBody = SmalltalkBehaviorBody;
  172. /* Smalltalk classes */
  173. var classes = [];
  174. this.buildBehaviorBody = function (pkgName, builder) {
  175. var pkg = st.packages[pkgName];
  176. if (!pkg) throw new Error("Missing package " + pkgName);
  177. var behaviorBody = makeBehaviorBody(builder, pkg);
  178. addBehaviorBody(behaviorBody);
  179. return behaviorBody;
  180. };
  181. function makeBehaviorBody (builder, pkg) {
  182. var behaviorBody = globals.hasOwnProperty(builder.className) && globals[builder.className];
  183. if (!behaviorBody) return builder.make(pkg);
  184. if (builder.updateExisting(behaviorBody, pkg)) return behaviorBody;
  185. var rebuilder = builder.rebuilderForExisting(behaviorBody);
  186. removeBehaviorBody(behaviorBody);
  187. return makeBehaviorBody(rebuilder, pkg);
  188. }
  189. function addBehaviorBody (behaviorBody) {
  190. globals[behaviorBody.className] = behaviorBody;
  191. addElement(classes, behaviorBody);
  192. addOrganizationElement(behaviorBody.pkg, behaviorBody);
  193. behaviorBody.added();
  194. }
  195. function removeBehaviorBody (behaviorBody) {
  196. behaviorBody.removed();
  197. removeOrganizationElement(behaviorBody.pkg, behaviorBody);
  198. removeElement(classes, behaviorBody);
  199. delete globals[behaviorBody.className];
  200. }
  201. this.removeBehaviorBody = removeBehaviorBody;
  202. /* Create an alias for an existing class */
  203. st.alias = function (behaviorBody, alias) {
  204. globals[alias] = behaviorBody;
  205. };
  206. /* Answer all registered Smalltalk classes */
  207. //TODO: remove the function and make smalltalk.classes an array
  208. st.classes = this.classes = function () {
  209. return classes;
  210. };
  211. }
  212. MethodsBrik.deps = ["composition", "selectors", "root", "selectorConversion"];
  213. function MethodsBrik (brikz, st) {
  214. var registerSelector = brikz.selectors.registerSelector;
  215. var updateMethod = brikz.composition.updateMethod;
  216. var SmalltalkObject = brikz.root.Object;
  217. var coreFns = brikz.root.coreFns;
  218. function SmalltalkMethod () {
  219. }
  220. coreFns.CompiledMethod = inherits(SmalltalkMethod, SmalltalkObject);
  221. /* Smalltalk method object. To add a method to a class,
  222. use api.addMethod() */
  223. st.method = function (spec) {
  224. var that = new SmalltalkMethod();
  225. var selector = spec.selector;
  226. that.selector = selector;
  227. that.jsSelector = st.st2js(selector);
  228. that.args = spec.args || {};
  229. that.protocol = spec.protocol;
  230. that.source = spec.source;
  231. that.messageSends = spec.messageSends || [];
  232. that.referencedClasses = spec.referencedClasses || [];
  233. that.fn = spec.fn;
  234. return that;
  235. };
  236. /* Add/remove a method to/from a class */
  237. st.addMethod = function (method, behaviorBody) {
  238. if (method.methodClass != null) {
  239. throw new Error("addMethod: Method " + method.selector + " already bound to " + method.methodClass);
  240. }
  241. method.methodClass = behaviorBody;
  242. registerNewSelectors(method);
  243. behaviorBody.localMethods[method.selector] = method;
  244. updateMethod(method.selector, behaviorBody);
  245. };
  246. function registerNewSelectors (method) {
  247. var newSelectors = [];
  248. function selectorInUse (stSelector) {
  249. var pair = registerSelector(stSelector);
  250. if (pair) {
  251. newSelectors.push(pair);
  252. }
  253. }
  254. selectorInUse(method.selector);
  255. method.messageSends.forEach(selectorInUse);
  256. if (st._selectorsAdded) st._selectorsAdded(newSelectors);
  257. }
  258. st.removeMethod = function (method, behaviorBody) {
  259. if (behaviorBody.localMethods[method.selector] !== method) return;
  260. delete behaviorBody.localMethods[method.selector];
  261. updateMethod(method.selector, behaviorBody);
  262. };
  263. }
  264. MethodCompositionBrik.deps = ["organize"];
  265. function MethodCompositionBrik (brikz, st) {
  266. var setupClassOrganization = brikz.organize.setupClassOrganization;
  267. var addOrganizationElement = brikz.organize.addOrganizationElement;
  268. this.setupMethods = function (behaviorBody) {
  269. setupClassOrganization(behaviorBody);
  270. behaviorBody.localMethods = Object.create(null);
  271. behaviorBody.methods = Object.create(null);
  272. };
  273. function addMethod (method, behaviorBody) {
  274. behaviorBody.methods[method.selector] = method;
  275. // During the bootstrap, #addCompiledMethod is not used.
  276. // Therefore we populate the organizer here too
  277. addOrganizationElement(behaviorBody, method.protocol);
  278. behaviorBody.methodAdded(method);
  279. }
  280. function removeMethod (method, behaviorBody) {
  281. delete behaviorBody.methods[method.selector];
  282. behaviorBody.methodRemoved(method);
  283. // Do *not* delete protocols from here.
  284. // This is handled by #removeCompiledMethod
  285. }
  286. this.updateMethod = function (selector, behaviorBody) {
  287. var oldMethod = behaviorBody.methods[selector],
  288. newMethod = behaviorBody.localMethods[selector];
  289. if (oldMethod == null && newMethod == null) {
  290. console.warn("Removal of nonexistent method " + behaviorBody + " >> " + selector);
  291. return;
  292. }
  293. if (newMethod === oldMethod) return;
  294. if (newMethod != null) addMethod(newMethod, behaviorBody);
  295. else removeMethod(oldMethod, behaviorBody);
  296. };
  297. }
  298. function ArraySetBrik (brikz, st) {
  299. st.addElement = this.addElement = function (array, el) {
  300. if (typeof el === 'undefined') {
  301. return;
  302. }
  303. if (array.indexOf(el) === -1) {
  304. array.push(el);
  305. }
  306. };
  307. st.removeElement = this.removeElement = function (array, el) {
  308. var i = array.indexOf(el);
  309. if (i !== -1) {
  310. array.splice(i, 1);
  311. }
  312. };
  313. }
  314. function SelectorConversionBrik (brikz, st) {
  315. /* Convert a Smalltalk selector into a JS selector */
  316. st.st2js = function (string) {
  317. return '_' + string
  318. .replace(/:/g, '_')
  319. .replace(/[\&]/g, '_and')
  320. .replace(/[\|]/g, '_or')
  321. .replace(/[+]/g, '_plus')
  322. .replace(/-/g, '_minus')
  323. .replace(/[*]/g, '_star')
  324. .replace(/[\/]/g, '_slash')
  325. .replace(/[\\]/g, '_backslash')
  326. .replace(/[\~]/g, '_tild')
  327. .replace(/%/g, '_percent')
  328. .replace(/>/g, '_gt')
  329. .replace(/</g, '_lt')
  330. .replace(/=/g, '_eq')
  331. .replace(/,/g, '_comma')
  332. .replace(/[@]/g, '_at');
  333. };
  334. /* Convert a string to a valid smalltalk selector.
  335. if you modify the following functions, also change st2js
  336. accordingly */
  337. st.js2st = function (selector) {
  338. if (selector.match(/^__/)) {
  339. return binaryJsToSt(selector);
  340. } else {
  341. return keywordJsToSt(selector);
  342. }
  343. };
  344. function keywordJsToSt (selector) {
  345. return selector.replace(/^_/, '').replace(/_/g, ':');
  346. }
  347. function binaryJsToSt (selector) {
  348. return selector
  349. .replace(/^_/, '')
  350. .replace(/_and/g, '&')
  351. .replace(/_or/g, '|')
  352. .replace(/_plus/g, '+')
  353. .replace(/_minus/g, '-')
  354. .replace(/_star/g, '*')
  355. .replace(/_slash/g, '/')
  356. .replace(/_backslash/g, '\\')
  357. .replace(/_tild/g, '~')
  358. .replace(/_percent/g, '%')
  359. .replace(/_gt/g, '>')
  360. .replace(/_lt/g, '<')
  361. .replace(/_eq/g, '=')
  362. .replace(/_comma/g, ',')
  363. .replace(/_at/g, '@');
  364. }
  365. st.st2prop = function (stSelector) {
  366. var colonPosition = stSelector.indexOf(':');
  367. return colonPosition === -1 ? stSelector : stSelector.slice(0, colonPosition);
  368. };
  369. }
  370. /* Making smalltalk that has basic building blocks */
  371. function configureWithFundamentals (brikz) {
  372. brikz.smalltalkGlobals = SmalltalkGlobalsBrik;
  373. brikz.root = RootBrik;
  374. brikz.arraySet = ArraySetBrik;
  375. brikz.organize = OrganizeBrik;
  376. brikz.selectorConversion = SelectorConversionBrik;
  377. brikz.selectors = SelectorsBrik;
  378. brikz.packages = PackagesBrik;
  379. brikz.composition = MethodCompositionBrik;
  380. brikz.behaviors = BehaviorsBrik;
  381. brikz.methods = MethodsBrik;
  382. brikz.rebuild();
  383. }
  384. return configureWithFundamentals;
  385. });