boot.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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([
  40. 'require', './kernel-checks', './brikz', './kernel-fundamentals', './kernel-language'
  41. ], function (require, _, Brikz, configureWithFundamentals, configureWithHierarchy) {
  42. "use strict";
  43. var runtimeLoadedPromise = new Promise(function (resolve, reject) {
  44. require(['./kernel-runtime'], resolve, reject);
  45. });
  46. function SmalltalkInitBrik (brikz, st) {
  47. var initialized = false;
  48. /* Smalltalk initialization. Called on page load */
  49. st.initialize = function () {
  50. return runtimeLoadedPromise.then(function (configureWithRuntime) {
  51. if (initialized) return;
  52. brikz.classes.bootstrapHierarchy();
  53. configureWithRuntime(brikz);
  54. // TODO deprecation helper; remove
  55. Object.defineProperty(st, "packages", {
  56. get: function () {
  57. console.warn("Use of .packages deprecated, use .packageDescriptors");
  58. return st.packageDescriptors;
  59. },
  60. set: function (v) {
  61. console.trace();
  62. throw new Error("No one should be setting st.packages directly on initialized Amber.");
  63. }
  64. });
  65. return Promise.resolve(brikz.startImage.run())
  66. .then(function () {
  67. initialized = true;
  68. });
  69. });
  70. };
  71. }
  72. /* Adds AMD and requirejs related methods to the api */
  73. function AMDBrik (brikz, st) {
  74. st.amdRequire = require;
  75. st.defaultTransportType = st.defaultTransportType || "amd";
  76. st.defaultAmdNamespace = st.defaultAmdNamespace || "amber_core";
  77. }
  78. /* Defines asReceiver to be present at load time */
  79. /* (logically it belongs more to PrimitiveBrik) */
  80. AsReceiverBrik.deps = ["nil"];
  81. function AsReceiverBrik (brikz, st) {
  82. var nilAsReceiver = brikz.nil.nilAsReceiver;
  83. /**
  84. * This function is used all over the compiled amber code.
  85. * It takes any value (JavaScript or Smalltalk)
  86. * and returns a proper Amber Smalltalk receiver.
  87. *
  88. * null or undefined -> nilAsReceiver,
  89. * object having Smalltalk signature -> unchanged,
  90. * otherwise wrapped foreign (JS) object
  91. */
  92. this.asReceiver = function (o) {
  93. if (o == null) return nilAsReceiver;
  94. else if (o.a$cls != null) return o;
  95. else return st.wrapJavaScript(o);
  96. };
  97. }
  98. var api = {};
  99. var brikz = new Brikz(api);
  100. configureWithFundamentals(brikz);
  101. configureWithHierarchy(brikz);
  102. brikz.asReceiver = AsReceiverBrik;
  103. brikz.stInit = SmalltalkInitBrik;
  104. brikz.amd = AMDBrik;
  105. brikz.rebuild();
  106. // TODO deprecated, remove
  107. Object.defineProperty(brikz.smalltalkGlobals.globals, "CharacterArray", {
  108. enumerable: true,
  109. configurable: true,
  110. get: function () {
  111. return this.String;
  112. }
  113. });
  114. return {
  115. api: api,
  116. nilAsReceiver: brikz.nil.nilAsReceiver,
  117. nilAsValue: brikz.nil.nilAsValue,
  118. nilAsClass: brikz.classes.nilAsClass,
  119. globals: brikz.smalltalkGlobals.globals,
  120. asReceiver: brikz.asReceiver.asReceiver
  121. };
  122. });