variants.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.RELEASE_ASYNC = exports.DEBUG_ASYNC = exports.RELEASE_SYNC = exports.DEBUG_SYNC = exports.memoizePromiseFactory = exports.newQuickJSAsyncWASMModule = exports.newQuickJSWASMModule = void 0;
  27. const esmHelpers_1 = require("./esmHelpers");
  28. /**
  29. * Create a new, completely isolated WebAssembly module containing the QuickJS library.
  30. * See the documentation on [[QuickJSWASMModule]].
  31. *
  32. * Note that there is a hard limit on the number of WebAssembly modules in older
  33. * versions of v8:
  34. * https://bugs.chromium.org/p/v8/issues/detail?id=12076
  35. */
  36. async function newQuickJSWASMModule(
  37. /**
  38. * Optionally, pass a {@link SyncBuildVariant} to construct a different WebAssembly module.
  39. */
  40. variant = exports.RELEASE_SYNC) {
  41. const [wasmModuleLoader, QuickJSFFI, { QuickJSWASMModule }] = await Promise.all([
  42. variant.importModuleLoader(),
  43. variant.importFFI(),
  44. Promise.resolve().then(() => __importStar(require("./module.js"))).then(esmHelpers_1.unwrapTypescript),
  45. ]);
  46. const wasmModule = await wasmModuleLoader();
  47. wasmModule.type = "sync";
  48. const ffi = new QuickJSFFI(wasmModule);
  49. return new QuickJSWASMModule(wasmModule, ffi);
  50. }
  51. exports.newQuickJSWASMModule = newQuickJSWASMModule;
  52. /**
  53. * Create a new, completely isolated WebAssembly module containing a version of the QuickJS library
  54. * compiled with Emscripten's [ASYNCIFY](https://emscripten.org/docs/porting/asyncify.html) transform.
  55. *
  56. * This version of the library offers features that enable synchronous code
  57. * inside the VM to interact with asynchronous code in the host environment.
  58. * See the documentation on [[QuickJSAsyncWASMModule]], [[QuickJSAsyncRuntime]],
  59. * and [[QuickJSAsyncContext]].
  60. *
  61. * Note that there is a hard limit on the number of WebAssembly modules in older
  62. * versions of v8:
  63. * https://bugs.chromium.org/p/v8/issues/detail?id=12076
  64. */
  65. async function newQuickJSAsyncWASMModule(
  66. /**
  67. * Optionally, pass a {@link AsyncBuildVariant} to construct a different WebAssembly module.
  68. */
  69. variant = exports.RELEASE_ASYNC) {
  70. const [wasmModuleLoader, QuickJSAsyncFFI, { QuickJSAsyncWASMModule }] = await Promise.all([
  71. variant.importModuleLoader(),
  72. variant.importFFI(),
  73. Promise.resolve().then(() => __importStar(require("./module-asyncify.js"))).then(esmHelpers_1.unwrapTypescript),
  74. ]);
  75. const wasmModule = await wasmModuleLoader();
  76. wasmModule.type = "async";
  77. const ffi = new QuickJSAsyncFFI(wasmModule);
  78. return new QuickJSAsyncWASMModule(wasmModule, ffi);
  79. }
  80. exports.newQuickJSAsyncWASMModule = newQuickJSAsyncWASMModule;
  81. /**
  82. * Helper intended to memoize the creation of a WebAssembly module.
  83. * ```typescript
  84. * const getDebugModule = memoizePromiseFactory(() => newQuickJSWASMModule(DEBUG_SYNC))
  85. * ```
  86. */
  87. function memoizePromiseFactory(fn) {
  88. let promise;
  89. return () => {
  90. return (promise ?? (promise = fn()));
  91. };
  92. }
  93. exports.memoizePromiseFactory = memoizePromiseFactory;
  94. /**
  95. * This build variant is compiled with `-fsanitize=leak`. It instruments all
  96. * memory allocations and when combined with sourcemaps, can present stack trace
  97. * locations where memory leaks occur.
  98. *
  99. * See [[TestQuickJSWASMModule]] which provides access to the leak sanitizer via
  100. * {@link TestQuickJSWASMModule.assertNoMemoryAllocated}.
  101. *
  102. * The downside is that it's 100-1000x slower than the other variants.
  103. * Suggested use case: automated testing, regression testing, and interactive
  104. * debugging.
  105. */
  106. exports.DEBUG_SYNC = {
  107. type: "sync",
  108. async importFFI() {
  109. throw new Error("not implemented");
  110. // const mod = await import("./generated/ffi.WASM_DEBUG_SYNC.js")
  111. // return unwrapTypescript(mod).QuickJSFFI
  112. },
  113. async importModuleLoader() {
  114. throw new Error("not implemented");
  115. // const mod = await import("./generated/emscripten-module.WASM_DEBUG_SYNC.js")
  116. // return unwrapJavascript(mod).default
  117. },
  118. };
  119. /**
  120. * This is the default (synchronous) build variant.
  121. * {@link getQuickJS} returns a memoized instance of this build variant.
  122. */
  123. exports.RELEASE_SYNC = {
  124. type: "sync",
  125. async importFFI() {
  126. const mod = await Promise.resolve().then(() => __importStar(require("./generated/ffi.WASM_RELEASE_SYNC.js")));
  127. return (0, esmHelpers_1.unwrapTypescript)(mod).QuickJSFFI;
  128. },
  129. async importModuleLoader() {
  130. const mod = await Promise.resolve().then(() => __importStar(require("./generated/emscripten-module.WASM_RELEASE_SYNC.js")));
  131. return (0, esmHelpers_1.unwrapJavascript)(mod);
  132. },
  133. };
  134. /**
  135. * The async debug build variant may or may not have the sanitizer enabled.
  136. * It does print a lot of debug logs.
  137. *
  138. * Suggested use case: interactive debugging only.
  139. */
  140. exports.DEBUG_ASYNC = {
  141. type: "async",
  142. async importFFI() {
  143. throw new Error("not implemented");
  144. // const mod = await import("./generated/ffi.WASM_DEBUG_ASYNCIFY.js")
  145. // return unwrapTypescript(mod).QuickJSAsyncFFI
  146. },
  147. async importModuleLoader() {
  148. throw new Error("not implemented");
  149. // const mod = await import("./generated/emscripten-module.WASM_DEBUG_ASYNCIFY.js")
  150. // return unwrapJavascript(mod).default
  151. },
  152. };
  153. /**
  154. * This is the default asyncified build variant.
  155. */
  156. exports.RELEASE_ASYNC = {
  157. type: "async",
  158. async importFFI() {
  159. throw new Error("not implemented");
  160. // const mod = await import("./generated/ffi.WASM_RELEASE_ASYNCIFY.js")
  161. // return unwrapTypescript(mod).QuickJSAsyncFFI
  162. },
  163. async importModuleLoader() {
  164. throw new Error("not implemented");
  165. // const mod = await import("./generated/emscripten-module.WASM_RELEASE_ASYNCIFY.js")
  166. // return unwrapJavascript(mod).default
  167. },
  168. };
  169. //# sourceMappingURL=variants.js.map