index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 __exportStar = (this && this.__exportStar) || function(m, exports) {
  19. for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
  20. };
  21. var __importStar = (this && this.__importStar) || function (mod) {
  22. if (mod && mod.__esModule) return mod;
  23. var result = {};
  24. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  25. __setModuleDefault(result, mod);
  26. return result;
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.shouldInterruptAfterDeadline = exports.newAsyncContext = exports.newAsyncRuntime = exports.getQuickJSSync = exports.getQuickJS = exports.errors = exports.RELEASE_SYNC = exports.RELEASE_ASYNC = exports.DEBUG_SYNC = exports.DEBUG_ASYNC = exports.newQuickJSAsyncWASMModule = exports.newQuickJSWASMModule = void 0;
  30. // Build variants
  31. const variants_1 = require("./variants");
  32. Object.defineProperty(exports, "newQuickJSWASMModule", { enumerable: true, get: function () { return variants_1.newQuickJSWASMModule; } });
  33. Object.defineProperty(exports, "newQuickJSAsyncWASMModule", { enumerable: true, get: function () { return variants_1.newQuickJSAsyncWASMModule; } });
  34. Object.defineProperty(exports, "DEBUG_ASYNC", { enumerable: true, get: function () { return variants_1.DEBUG_ASYNC; } });
  35. Object.defineProperty(exports, "DEBUG_SYNC", { enumerable: true, get: function () { return variants_1.DEBUG_SYNC; } });
  36. Object.defineProperty(exports, "RELEASE_ASYNC", { enumerable: true, get: function () { return variants_1.RELEASE_ASYNC; } });
  37. Object.defineProperty(exports, "RELEASE_SYNC", { enumerable: true, get: function () { return variants_1.RELEASE_SYNC; } });
  38. // Export helpers
  39. __exportStar(require("./vm-interface"), exports);
  40. __exportStar(require("./lifetime"), exports);
  41. /** Collects the informative errors this library may throw. */
  42. exports.errors = __importStar(require("./errors"));
  43. __exportStar(require("./deferred-promise"), exports);
  44. __exportStar(require("./module-test"), exports);
  45. let singleton = undefined;
  46. let singletonPromise = undefined;
  47. /**
  48. * Get a shared singleton {@link QuickJSWASMModule}. Use this to evaluate code
  49. * or create Javascript environments.
  50. *
  51. * This is the top-level entrypoint for the quickjs-emscripten library.
  52. *
  53. * If you need strictest possible isolation guarantees, you may create a
  54. * separate {@link QuickJSWASMModule} via {@link newQuickJSWASMModule}.
  55. *
  56. * To work with the asyncified version of this library, see these functions:
  57. *
  58. * - {@link newAsyncRuntime}.
  59. * - {@link newAsyncContext}.
  60. * - {@link newQuickJSAsyncWASMModule}.
  61. */
  62. async function getQuickJS() {
  63. singletonPromise ?? (singletonPromise = (0, variants_1.newQuickJSWASMModule)().then((instance) => {
  64. singleton = instance;
  65. return instance;
  66. }));
  67. return await singletonPromise;
  68. }
  69. exports.getQuickJS = getQuickJS;
  70. /**
  71. * Provides synchronous access to the shared {@link QuickJSWASMModule} instance returned by {@link getQuickJS}, as long as
  72. * least once.
  73. * @throws If called before `getQuickJS` resolves.
  74. */
  75. function getQuickJSSync() {
  76. if (!singleton) {
  77. throw new Error("QuickJS not initialized. Await getQuickJS() at least once.");
  78. }
  79. return singleton;
  80. }
  81. exports.getQuickJSSync = getQuickJSSync;
  82. /**
  83. * Create a new [[QuickJSAsyncRuntime]] in a separate WebAssembly module.
  84. *
  85. * Each runtime is isolated in a separate WebAssembly module, so that errors in
  86. * one runtime cannot contaminate another runtime, and each runtime can execute
  87. * an asynchronous action without conflicts.
  88. *
  89. * Note that there is a hard limit on the number of WebAssembly modules in older
  90. * versions of v8:
  91. * https://bugs.chromium.org/p/v8/issues/detail?id=12076
  92. */
  93. async function newAsyncRuntime(options) {
  94. const module = await (0, variants_1.newQuickJSAsyncWASMModule)();
  95. return module.newRuntime(options);
  96. }
  97. exports.newAsyncRuntime = newAsyncRuntime;
  98. /**
  99. * Create a new [[QuickJSAsyncContext]] (with an associated runtime) in an
  100. * separate WebAssembly module.
  101. *
  102. * Each context is isolated in a separate WebAssembly module, so that errors in
  103. * one runtime cannot contaminate another runtime, and each runtime can execute
  104. * an asynchronous action without conflicts.
  105. *
  106. * Note that there is a hard limit on the number of WebAssembly modules in older
  107. * versions of v8:
  108. * https://bugs.chromium.org/p/v8/issues/detail?id=12076
  109. */
  110. async function newAsyncContext(options) {
  111. const module = await (0, variants_1.newQuickJSAsyncWASMModule)();
  112. return module.newContext(options);
  113. }
  114. exports.newAsyncContext = newAsyncContext;
  115. /**
  116. * Returns an interrupt handler that interrupts Javascript execution after a deadline time.
  117. *
  118. * @param deadline - Interrupt execution if it's still running after this time.
  119. * Number values are compared against `Date.now()`
  120. */
  121. function shouldInterruptAfterDeadline(deadline) {
  122. const deadlineAsNumber = typeof deadline === "number" ? deadline : deadline.getTime();
  123. return function () {
  124. return Date.now() > deadlineAsNumber;
  125. };
  126. }
  127. exports.shouldInterruptAfterDeadline = shouldInterruptAfterDeadline;
  128. //# sourceMappingURL=index.js.map