module.d.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { QuickJSContext } from "./context";
  2. import { Asyncify, AsyncifySleepResult, EitherModule, EmscriptenModuleCallbacks } from "./emscripten-types";
  3. import { JSContextPointer, JSRuntimePointer } from "./types-ffi";
  4. import { InterruptHandler, QuickJSRuntime } from "./runtime";
  5. import { ContextOptions, EitherFFI, JSModuleLoader, RuntimeOptions, RuntimeOptionsBase } from "./types";
  6. type EmscriptenCallback<BaseArgs extends any[], Result> = (...args: [Asyncify | undefined, ...BaseArgs]) => Result | AsyncifySleepResult<Result>;
  7. type MaybeAsyncEmscriptenCallback<T extends EmscriptenCallback<any, any>> = T extends EmscriptenCallback<infer Args, infer Result> ? (...args: Args) => Result | Promise<Result> : never;
  8. type MaybeAsyncEmscriptenCallbacks = {
  9. [K in keyof EmscriptenModuleCallbacks]: MaybeAsyncEmscriptenCallback<EmscriptenModuleCallbacks[K]>;
  10. };
  11. /**
  12. * @private
  13. */
  14. export interface ContextCallbacks {
  15. callFunction: MaybeAsyncEmscriptenCallbacks["callFunction"];
  16. }
  17. /**
  18. * @private
  19. */
  20. export interface RuntimeCallbacks {
  21. shouldInterrupt: MaybeAsyncEmscriptenCallbacks["shouldInterrupt"];
  22. loadModuleSource: MaybeAsyncEmscriptenCallbacks["loadModuleSource"];
  23. normalizeModule: MaybeAsyncEmscriptenCallbacks["normalizeModule"];
  24. }
  25. /**
  26. * Options for [[QuickJSWASMModule.evalCode]].
  27. */
  28. export interface ModuleEvalOptions {
  29. /**
  30. * Interrupt evaluation if `shouldInterrupt` returns `true`.
  31. * See [[shouldInterruptAfterDeadline]].
  32. */
  33. shouldInterrupt?: InterruptHandler;
  34. /**
  35. * Memory limit, in bytes, of WebAssembly heap memory used by the QuickJS VM.
  36. */
  37. memoryLimitBytes?: number;
  38. /**
  39. * Stack size limit for this vm, in bytes
  40. * To remove the limit, set to `0`.
  41. */
  42. maxStackSizeBytes?: number;
  43. /**
  44. * Module loader for any `import` statements or expressions.
  45. */
  46. moduleLoader?: JSModuleLoader;
  47. }
  48. /**
  49. * We use static functions per module to dispatch runtime or context calls from
  50. * C to the host. This class manages the indirection from a specific runtime or
  51. * context pointer to the appropriate callback handler.
  52. *
  53. * @private
  54. */
  55. export declare class QuickJSModuleCallbacks {
  56. private module;
  57. private contextCallbacks;
  58. private runtimeCallbacks;
  59. constructor(module: EitherModule);
  60. setRuntimeCallbacks(rt: JSRuntimePointer, callbacks: RuntimeCallbacks): void;
  61. deleteRuntime(rt: JSRuntimePointer): void;
  62. setContextCallbacks(ctx: JSContextPointer, callbacks: ContextCallbacks): void;
  63. deleteContext(ctx: JSContextPointer): void;
  64. private suspendedCount;
  65. private suspended;
  66. private handleAsyncify;
  67. private cToHostCallbacks;
  68. }
  69. /**
  70. * Process RuntimeOptions and apply them to a QuickJSRuntime.
  71. * @private
  72. */
  73. export declare function applyBaseRuntimeOptions(runtime: QuickJSRuntime, options: RuntimeOptionsBase): void;
  74. /**
  75. * Process ModuleEvalOptions and apply them to a QuickJSRuntime.
  76. * @private
  77. */
  78. export declare function applyModuleEvalRuntimeOptions<T extends QuickJSRuntime>(runtime: T, options: ModuleEvalOptions): void;
  79. /**
  80. * This class presents a Javascript interface to QuickJS, a Javascript interpreter
  81. * that supports EcmaScript 2020 (ES2020).
  82. *
  83. * It wraps a single WebAssembly module containing the QuickJS library and
  84. * associated helper C code. WebAssembly modules are completely isolated from
  85. * each other by the host's WebAssembly runtime. Separate WebAssembly modules
  86. * have the most isolation guarantees possible with this library.
  87. *
  88. * The simplest way to start running code is {@link evalCode}. This shortcut
  89. * method will evaluate Javascript safely and return the result as a native
  90. * Javascript value.
  91. *
  92. * For more control over the execution environment, or to interact with values
  93. * inside QuickJS, create a context with {@link newContext} or a runtime with
  94. * {@link newRuntime}.
  95. */
  96. export declare class QuickJSWASMModule {
  97. /** @private */
  98. protected ffi: EitherFFI;
  99. /** @private */
  100. protected callbacks: QuickJSModuleCallbacks;
  101. /** @private */
  102. protected module: EitherModule;
  103. /** @private */
  104. constructor(module: EitherModule, ffi: EitherFFI);
  105. /**
  106. * Create a runtime.
  107. * Use the runtime to set limits on CPU and memory usage and configure module
  108. * loading for one or more [[QuickJSContext]]s inside the runtime.
  109. */
  110. newRuntime(options?: RuntimeOptions): QuickJSRuntime;
  111. /**
  112. * A simplified API to create a new [[QuickJSRuntime]] and a
  113. * [[QuickJSContext]] inside that runtime at the same time. The runtime will
  114. * be disposed when the context is disposed.
  115. */
  116. newContext(options?: ContextOptions): QuickJSContext;
  117. /**
  118. * One-off evaluate code without needing to create a [[QuickJSRuntime]] or
  119. * [[QuickJSContext]] explicitly.
  120. *
  121. * To protect against infinite loops, use the `shouldInterrupt` option. The
  122. * [[shouldInterruptAfterDeadline]] function will create a time-based deadline.
  123. *
  124. * If you need more control over how the code executes, create a
  125. * [[QuickJSRuntime]] (with [[newRuntime]]) or a [[QuickJSContext]] (with
  126. * [[newContext]] or [[QuickJSRuntime.newContext]]), and use its
  127. * [[QuickJSContext.evalCode]] method.
  128. *
  129. * Asynchronous callbacks may not run during the first call to `evalCode`. If
  130. * you need to work with async code inside QuickJS, create a runtime and use
  131. * [[QuickJSRuntime.executePendingJobs]].
  132. *
  133. * @returns The result is coerced to a native Javascript value using JSON
  134. * serialization, so properties and values unsupported by JSON will be dropped.
  135. *
  136. * @throws If `code` throws during evaluation, the exception will be
  137. * converted into a native Javascript value and thrown.
  138. *
  139. * @throws if `options.shouldInterrupt` interrupted execution, will throw a Error
  140. * with name `"InternalError"` and message `"interrupted"`.
  141. */
  142. evalCode(code: string, options?: ModuleEvalOptions): unknown;
  143. /**
  144. * Get a low-level interface to the QuickJS functions in this WebAssembly
  145. * module.
  146. * @experimental
  147. * @unstable No warranty is provided with this API. It could change at any time.
  148. * @private
  149. */
  150. getFFI(): EitherFFI;
  151. }
  152. export {};