context-asyncify.d.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { QuickJSContext } from "./context";
  2. import { QuickJSAsyncEmscriptenModule } from "./emscripten-types";
  3. import { QuickJSAsyncFFI } from "./variants";
  4. import { JSRuntimePointer } from "./types-ffi";
  5. import { Lifetime } from "./lifetime";
  6. import { QuickJSModuleCallbacks } from "./module";
  7. import { QuickJSAsyncRuntime } from "./runtime-asyncify";
  8. import { ContextEvalOptions, QuickJSHandle } from "./types";
  9. import { VmCallResult } from "./vm-interface";
  10. export type AsyncFunctionImplementation = (this: QuickJSHandle, ...args: QuickJSHandle[]) => Promise<QuickJSHandle | VmCallResult<QuickJSHandle> | void>;
  11. /**
  12. * Asyncified version of [[QuickJSContext]].
  13. *
  14. * *Asyncify* allows normally synchronous code to wait for asynchronous Promises
  15. * or callbacks. The asyncified version of QuickJSContext can wait for async
  16. * host functions as though they were synchronous.
  17. */
  18. export declare class QuickJSAsyncContext extends QuickJSContext {
  19. runtime: QuickJSAsyncRuntime;
  20. /** @private */
  21. protected module: QuickJSAsyncEmscriptenModule;
  22. /** @private */
  23. protected ffi: QuickJSAsyncFFI;
  24. /** @private */
  25. protected rt: Lifetime<JSRuntimePointer>;
  26. /** @private */
  27. protected callbacks: QuickJSModuleCallbacks;
  28. /**
  29. * Asyncified version of [[evalCode]].
  30. */
  31. evalCodeAsync(code: string, filename?: string,
  32. /** See [[EvalFlags]] for number semantics */
  33. options?: number | ContextEvalOptions): Promise<VmCallResult<QuickJSHandle>>;
  34. /**
  35. * Similar to [[newFunction]].
  36. * Convert an async host Javascript function into a synchronous QuickJS function value.
  37. *
  38. * Whenever QuickJS calls this function, the VM's stack will be unwound while
  39. * waiting the async function to complete, and then restored when the returned
  40. * promise resolves.
  41. *
  42. * Asyncified functions must never call other asyncified functions or
  43. * `import`, even indirectly, because the stack cannot be unwound twice.
  44. *
  45. * See [Emscripten's docs on Asyncify](https://emscripten.org/docs/porting/asyncify.html).
  46. */
  47. newAsyncifiedFunction(name: string, fn: AsyncFunctionImplementation): QuickJSHandle;
  48. }