quickjs-emscripten-sync 1.8.4 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +231 -150
- package/dist/index.d.ts +45 -7
- package/dist/quickjs-emscripten-sync.js +573 -342
- package/dist/quickjs-emscripten-sync.umd.cjs +17 -8
- package/package.json +4 -2
- package/src/index.test.ts +250 -5
- package/src/index.ts +85 -7
- package/src/marshal/custom.ts +16 -2
- package/src/marshal/hostref.ts +18 -0
- package/src/marshal/index.ts +12 -0
- package/src/marshal/mapset.ts +28 -0
- package/src/marshal/primitive.ts +2 -11
- package/src/marshal/properties.ts +8 -6
- package/src/unmarshal/custom.ts +35 -4
- package/src/unmarshal/function.ts +2 -2
- package/src/unmarshal/hostref.ts +21 -0
- package/src/unmarshal/index.ts +21 -1
- package/src/unmarshal/mapset.ts +43 -0
- package/src/unmarshal/object.ts +16 -20
- package/src/unmarshal/primitive.ts +6 -17
- package/src/unmarshal/properties.ts +7 -6
- package/src/vmmap.ts +59 -69
- package/src/vmutil.ts +94 -11
- package/src/wrapper.ts +97 -30
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Intrinsics } from 'quickjs-emscripten';
|
|
2
|
+
import { QuickJSAsyncContext } from 'quickjs-emscripten';
|
|
2
3
|
import { QuickJSContext } from 'quickjs-emscripten';
|
|
3
4
|
import { QuickJSDeferredPromise } from 'quickjs-emscripten';
|
|
4
5
|
import { QuickJSHandle } from 'quickjs-emscripten';
|
|
@@ -24,6 +25,8 @@ export declare class Arena {
|
|
|
24
25
|
* Dispose of the arena and managed handles. This method won't dispose the VM itself, so the VM has to be disposed of manually.
|
|
25
26
|
*/
|
|
26
27
|
dispose(): void;
|
|
28
|
+
/** Allows `using arena = new Arena(...)` to dispose the arena automatically. */
|
|
29
|
+
[Symbol.dispose](): void;
|
|
27
30
|
/**
|
|
28
31
|
* 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.
|
|
29
32
|
*/
|
|
@@ -199,6 +202,8 @@ export declare class Arena {
|
|
|
199
202
|
_unwrapResult<T>(result: SuccessOrFail<T, QuickJSHandle>): T;
|
|
200
203
|
_unwrapResultAndUnmarshal(result: VmCallResult<QuickJSHandle> | undefined): any;
|
|
201
204
|
_isMarshalable: (t: unknown) => boolean | "json";
|
|
205
|
+
_marshalByReference: (t: unknown) => boolean;
|
|
206
|
+
_registerHostRef: (t: unknown, handle: QuickJSHandle) => QuickJSHandle;
|
|
202
207
|
_marshalFind: (t: unknown) => QuickJSHandle | undefined;
|
|
203
208
|
_marshalPre: (t: unknown, h: QuickJSHandle | QuickJSDeferredPromise, mode: true | "json" | undefined) => Wrapped<QuickJSHandle> | undefined;
|
|
204
209
|
_marshalPreApply: (target: (...args: any[]) => any, that: unknown, args: unknown[]) => void;
|
|
@@ -215,6 +220,23 @@ export declare class Arena {
|
|
|
215
220
|
_unwrapHandle(target: QuickJSHandle): [QuickJSHandle, boolean];
|
|
216
221
|
}
|
|
217
222
|
|
|
223
|
+
/**
|
|
224
|
+
* An Arena backed by a {@link QuickJSAsyncContext}. In addition to everything
|
|
225
|
+
* `Arena` offers, it can evaluate code asynchronously with `evalCodeAsync`,
|
|
226
|
+
* which lets the VM await host promises (e.g. async module loaders or async
|
|
227
|
+
* functions exposed from the host) without manually pumping pending jobs.
|
|
228
|
+
*/
|
|
229
|
+
export declare class AsyncArena extends Arena {
|
|
230
|
+
asyncContext: QuickJSAsyncContext;
|
|
231
|
+
constructor(ctx: QuickJSAsyncContext, options?: Options);
|
|
232
|
+
/**
|
|
233
|
+
* Evaluate JS code asynchronously in the VM and get the result on the host
|
|
234
|
+
* side. Like `evalCode`, it converts and re-throws errors thrown during
|
|
235
|
+
* evaluation. Use this when the code awaits host-provided promises.
|
|
236
|
+
*/
|
|
237
|
+
evalCodeAsync<T = any>(code: string, filename?: string): Promise<T>;
|
|
238
|
+
}
|
|
239
|
+
|
|
218
240
|
export declare function call(ctx: QuickJSContext, code: string, thisArg?: QuickJSHandle, ...args: QuickJSHandle[]): QuickJSHandle;
|
|
219
241
|
|
|
220
242
|
/**
|
|
@@ -261,12 +283,18 @@ export declare type Options = {
|
|
|
261
283
|
compat?: boolean;
|
|
262
284
|
/** Experimental: use QuickJSContextEx, which wraps existing QuickJSContext. */
|
|
263
285
|
experimentalContextEx?: boolean;
|
|
286
|
+
/** Globally enable sync mode (default `true`). When `false`, objects are not wrapped with proxies and marshalled handles are disposed after use, so `arena.sync` has no effect but objects are not retained for their whole lifetime. Useful to avoid memory growth when frequently exchanging short-lived objects. */
|
|
287
|
+
syncEnabled?: boolean;
|
|
288
|
+
/** A callback that returns whether an object should be passed to the VM by reference (as an opaque HostRef) instead of being marshalled by value/proxy. The guest cannot read such objects, but can hold them and pass them back to the host, where they resolve to the original object. */
|
|
289
|
+
marshalByReference?: (target: any) => boolean;
|
|
264
290
|
};
|
|
265
291
|
|
|
266
292
|
declare type Options_2 = {
|
|
267
293
|
ctx: QuickJSContext;
|
|
268
294
|
unmarshal: (handle: QuickJSHandle) => unknown;
|
|
269
295
|
isMarshalable?: (target: unknown) => boolean | "json";
|
|
296
|
+
marshalByReference?: (target: unknown) => boolean;
|
|
297
|
+
registerHostRef?: (target: unknown, handle: QuickJSHandle) => QuickJSHandle;
|
|
270
298
|
find: (target: unknown) => QuickJSHandle | undefined;
|
|
271
299
|
pre: (target: unknown, handle: QuickJSHandle | QuickJSDeferredPromise, mode: true | "json" | undefined) => QuickJSHandle | undefined;
|
|
272
300
|
preApply?: (target: (...args: any[]) => any, thisArg: unknown, args: unknown[]) => any;
|
|
@@ -280,6 +308,8 @@ declare type Options_3 = {
|
|
|
280
308
|
find: (handle: QuickJSHandle) => unknown | undefined;
|
|
281
309
|
pre: <T = unknown>(target: T, handle: QuickJSHandle) => T | undefined;
|
|
282
310
|
custom?: Iterable<(obj: QuickJSHandle, ctx: QuickJSContext) => any>;
|
|
311
|
+
/** When true, resolve opaque HostRef handles back to their host value. */
|
|
312
|
+
hostRef?: boolean;
|
|
283
313
|
};
|
|
284
314
|
|
|
285
315
|
declare type QuickJSContextEx = QuickJSContext & {
|
|
@@ -288,19 +318,28 @@ declare type QuickJSContextEx = QuickJSContext & {
|
|
|
288
318
|
|
|
289
319
|
export declare function unmarshal(handle: QuickJSHandle, options: Options_3): any;
|
|
290
320
|
|
|
321
|
+
/**
|
|
322
|
+
* Bidirectional map between host values and QuickJS handles.
|
|
323
|
+
*
|
|
324
|
+
* Each registered pair gets a numeric id. A value may be registered under two
|
|
325
|
+
* keys (e.g. a proxy-wrapped object and the underlying object), each with its
|
|
326
|
+
* own handle, so every lookup direction is backed by an explicit map and id
|
|
327
|
+
* reverse-lookups, keeping `delete` O(1).
|
|
328
|
+
*/
|
|
291
329
|
export declare class VMMap {
|
|
292
330
|
ctx: QuickJSContext;
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
331
|
+
_keyToId: Map<any, number>;
|
|
332
|
+
_key2ToId: Map<any, number>;
|
|
333
|
+
_idToHandle: Map<number, QuickJSHandle>;
|
|
334
|
+
_idToHandle2: Map<number, QuickJSHandle>;
|
|
335
|
+
_idToKey: Map<number, any>;
|
|
336
|
+
_idToKey2: Map<number, any>;
|
|
298
337
|
_disposables: Set<QuickJSHandle>;
|
|
299
338
|
_mapGet: QuickJSHandle;
|
|
300
339
|
_mapSet: QuickJSHandle;
|
|
301
340
|
_mapDelete: QuickJSHandle;
|
|
302
341
|
_mapClear: QuickJSHandle;
|
|
303
|
-
|
|
342
|
+
_nextId: number;
|
|
304
343
|
constructor(ctx: QuickJSContext);
|
|
305
344
|
set(key: any, handle: QuickJSHandle, key2?: any, handle2?: QuickJSHandle): boolean;
|
|
306
345
|
merge(iteratable: Iterable<[any, QuickJSHandle | undefined] | [any, QuickJSHandle | undefined, any, QuickJSHandle | undefined]> | undefined): void;
|
|
@@ -315,7 +354,6 @@ export declare class VMMap {
|
|
|
315
354
|
dispose(): void;
|
|
316
355
|
get size(): number;
|
|
317
356
|
[Symbol.iterator](): Iterator<[any, QuickJSHandle, any, QuickJSHandle | undefined]>;
|
|
318
|
-
_get2(num: number): any;
|
|
319
357
|
_call(fn: QuickJSHandle, thisArg: QuickJSHandle | undefined, ...args: QuickJSHandle[]): QuickJSHandle;
|
|
320
358
|
}
|
|
321
359
|
|