import type { QuickJSAsyncContext } from 'quickjs-emscripten'; import { QuickJSContext } from 'quickjs-emscripten'; import type { QuickJSDeferredPromise } from 'quickjs-emscripten'; import type { QuickJSHandle } from 'quickjs-emscripten'; import type { SuccessOrFail } from 'quickjs-emscripten'; import type { VmCallResult } from 'quickjs-emscripten'; /** * The Arena class manages all generated handles at once by quickjs-emscripten and automatically converts objects between the host and the QuickJS VM. */ export declare class Arena { context: QuickJSContextEx; _map: VMMap; _registeredMap: VMMap; _registeredMapDispose: Set; _sync: Set; _temporalSync: Set; _symbol: symbol; _symbolHandle: QuickJSHandle; _options?: Options; /** Constructs a new Arena instance. It requires a quickjs-emscripten context initialized with `quickjs.newContext()`. */ constructor(ctx: QuickJSContext, options?: Options); /** * Dispose of the arena and managed handles. This method won't dispose the VM itself, so the VM has to be disposed of manually. */ dispose(): void; /** * Evaluate JS code in the VM and get the result as an object on the host side. It also converts and re-throws error objects when an error is thrown during evaluation. */ evalCode(code: string): T; /** * Almost same as `vm.executePendingJobs()`, but it converts and re-throws error objects when an error is thrown during evaluation. */ executePendingJobs(maxJobsToExecute?: number): number; /** * Expose objects as global objects in the VM. * * By default, exposed objects are not synchronized between the host and the VM. * If you want to sync an objects, first wrap the object with sync method, and then expose the wrapped object. */ expose(obj: { [k: string]: any; }): void; /** * Enables sync for the object between the host and the VM and returns objects wrapped with proxies. * * The return value is necessary in order to reflect changes to the object from the host to the VM. Please note that setting a value in the field or deleting a field in the original object will not synchronize it. */ sync(target: T): T; /** * Register a pair of objects that will be considered the same between the host and the QuickJS VM. * * Instead of a string, you can also pass a QuickJSHandle directly. In that case, however, when you have to dispose them manually when destroying the VM. */ register(target: any, handleOrCode: QuickJSHandle | string): void; /** * Execute `register` methods for each pair. */ registerAll(map: Iterable<[any, QuickJSHandle | string]>): void; /** * Unregister a pair of objects that were registered with `registeredObjects` option and `register` method. */ unregister(target: any, dispose?: boolean): void; /** * Execute `unregister` methods for each target. */ unregisterAll(targets: Iterable, dispose?: boolean): void; startSync(target: any): void; endSync(target: any): void; _unwrapResult(result: SuccessOrFail): T; _unwrapResultAndUnmarshal(result: VmCallResult | undefined): any; _isMarshalable: (t: unknown) => boolean | "json"; _marshalFind: (t: unknown) => QuickJSHandle | undefined; _marshalPre: (t: unknown, h: QuickJSHandle | QuickJSDeferredPromise, mode: true | "json" | undefined) => Wrapped | undefined; _marshalPreApply: (target: Function, that: unknown, args: unknown[]) => void; _marshal: (target: any) => [QuickJSHandle, boolean]; _preUnmarshal: (t: any, h: QuickJSHandle) => Wrapped; _unmarshalFind: (h: QuickJSHandle) => unknown; _unmarshal: (handle: QuickJSHandle) => any; _register(t: any, h: QuickJSHandle, map?: VMMap, sync?: boolean): [Wrapped, Wrapped] | undefined; _syncMode: (obj: any) => "both" | undefined; _wrap(target: T): Wrapped | undefined; _unwrap(target: T): T; _unwrapIfNotSynced: (target: T) => T; _wrapHandle(handle: QuickJSHandle): [Wrapped | undefined, boolean]; _unwrapHandle(target: QuickJSHandle): [QuickJSHandle, boolean]; } export declare class AsyncArena extends Arena { asyncContext: QuickJSAsyncContext; constructor(ctx: QuickJSAsyncContext, options?: Options); /** * Evaluate JS code in the VM and get the result as an object on the host side. It also converts and re-throws error objects when an error is thrown during evaluation. */ evalCodeAsync(code: string): Promise; } export declare function call(ctx: QuickJSContext, code: string, thisArg?: QuickJSHandle, ...args: QuickJSHandle[]): QuickJSHandle; /** * Measure the complexity of an object as you traverse the field and prototype chain. If max is specified, when the complexity reaches max, the traversal is terminated and it returns the max. In this function, one object and function are counted as a complexity of 1, and primitives are not counted as a complexity. */ export declare function complexity(value: any, max?: number): number; export declare function consumeAll(handles: T, cb: (handles: T) => K): K; /** * Default value of registeredObjects option of the Arena class constructor. */ export declare const defaultRegisteredObjects: [any, string][]; export declare function eq(ctx: QuickJSContext, a: QuickJSHandle, b: QuickJSHandle): boolean; export declare function isES2015Class(cls: any): cls is new (...args: any[]) => any; export declare function isHandleObject(ctx: QuickJSContext, h: QuickJSHandle): boolean; export declare function isObject(value: any): value is object | Function; export declare function json(ctx: QuickJSContext, target: any): QuickJSHandle; export declare function marshal(target: unknown, options: Options_2): QuickJSHandle; export declare type Options = { /** A callback that returns a boolean value that determines whether an object is marshalled or not. If false, no marshaling will be done and undefined will be passed to the QuickJS VM, otherwise marshaling will be done. By default, all objects will be marshalled. */ isMarshalable?: boolean | "json" | ((target: any) => boolean | "json"); /** Pre-registered pairs of objects that will be considered the same between the host and the QuickJS VM. This will be used automatically during the conversion. By default, it will be registered automatically with `defaultRegisteredObjects`. * * Instead of a string, you can also pass a QuickJSHandle directly. In that case, however, you have to dispose of them manually when destroying the VM. */ registeredObjects?: Iterable<[any, QuickJSHandle | string]>; /** Register functions to convert an object to a QuickJS handle. */ customMarshaller?: Iterable<(target: unknown, ctx: QuickJSContext) => QuickJSHandle | undefined>; /** Register functions to convert a QuickJS handle to an object. */ customUnmarshaller?: Iterable<(target: QuickJSHandle, ctx: QuickJSContext) => any>; /** A callback that returns a boolean value that determines whether an object is wrappable by proxies. If returns false, note that the object cannot be synchronized between the host and the QuickJS even if arena.sync is used. */ isWrappable?: (target: any) => boolean; /** A callback that returns a boolean value that determines whether an QuickJS handle is wrappable by proxies. If returns false, note that the handle cannot be synchronized between the host and the QuickJS even if arena.sync is used. */ isHandleWrappable?: (handle: QuickJSHandle, ctx: QuickJSContext) => boolean; /** Compatibility with quickjs-emscripten prior to v0.15. Inject code for compatibility into context at Arena class initialization time. */ compat?: boolean; /** Experimental: use QuickJSContextEx, which wraps existing QuickJSContext. */ experimentalContextEx?: boolean; }; declare type Options_2 = { ctx: QuickJSContext; unmarshal: (handle: QuickJSHandle) => unknown; isMarshalable?: (target: unknown) => boolean | "json"; find: (target: unknown) => QuickJSHandle | undefined; pre: (target: unknown, handle: QuickJSHandle | QuickJSDeferredPromise, mode: true | "json" | undefined) => QuickJSHandle | undefined; preApply?: (target: Function, thisArg: unknown, args: unknown[]) => any; custom?: Iterable<(obj: unknown, ctx: QuickJSContext) => QuickJSHandle | undefined>; }; declare type Options_3 = { ctx: QuickJSContext; /** marshal returns handle and boolean indicates that the handle should be disposed after use */ marshal: (target: unknown) => [QuickJSHandle, boolean]; find: (handle: QuickJSHandle) => unknown | undefined; pre: (target: T, handle: QuickJSHandle) => T | undefined; custom?: Iterable<(obj: QuickJSHandle, ctx: QuickJSContext) => any>; }; declare type QuickJSContextEx = QuickJSContext & { disposeEx?: () => void; }; export declare function unmarshal(handle: QuickJSHandle, options: Options_3): any; export declare class VMMap { ctx: QuickJSContext; _map1: Map; _map2: Map; _map3: Map; _map4: Map; _counterMap: Map; _disposables: Set; _mapGet: QuickJSHandle; _mapSet: QuickJSHandle; _mapDelete: QuickJSHandle; _mapClear: QuickJSHandle; _counter: number; constructor(ctx: QuickJSContext); set(key: any, handle: QuickJSHandle, key2?: any, handle2?: QuickJSHandle): boolean; merge(iteratable: Iterable<[any, QuickJSHandle | undefined] | [any, QuickJSHandle | undefined, any, QuickJSHandle | undefined]> | undefined): void; get(key: any): QuickJSHandle | undefined; getByHandle(handle: QuickJSHandle): any; has(key: any): boolean; hasHandle(handle: QuickJSHandle): boolean; keys(): IterableIterator; delete(key: any, dispose?: boolean): void; deleteByHandle(handle: QuickJSHandle, dispose?: boolean): void; clear(): void; dispose(): void; get size(): number; [Symbol.iterator](): Iterator<[any, QuickJSHandle, any, QuickJSHandle | undefined]>; _get2(num: number): any; _call(fn: QuickJSHandle, thisArg: QuickJSHandle | undefined, ...args: QuickJSHandle[]): QuickJSHandle; } export declare function walkObject(value: any, callback?: (target: any, set: Set) => boolean | void): Set; declare type Wrapped = T & { __qes_wrapped: never; }; export { }