quickjs-emscripten-sync 1.5.1 → 1.6.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/dist/index.d.ts +128 -7
- package/dist/quickjs-emscripten-sync.mjs +694 -479
- package/dist/quickjs-emscripten-sync.umd.js +17 -10
- package/package.json +10 -10
- package/src/contextex.test.ts +43 -0
- package/src/contextex.ts +65 -0
- package/src/edge.test.ts +70 -0
- package/src/index.test.ts +324 -28
- package/src/index.ts +142 -2
- package/src/marshal/index.test.ts +2 -2
- package/src/vmmap.ts +1 -1
- package/src/wrapper.ts +44 -32
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
1
|
+
import { QuickJSContext } from 'quickjs-emscripten';
|
|
2
|
+
import { QuickJSDeferredPromise } from 'quickjs-emscripten';
|
|
3
|
+
import { QuickJSHandle } from 'quickjs-emscripten';
|
|
4
|
+
import { SuccessOrFail } from 'quickjs-emscripten';
|
|
5
|
+
import { VmCallResult } from 'quickjs-emscripten';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* The Arena class manages all generated handles at once by quickjs-emscripten and automatically converts objects between the host and the QuickJS VM.
|
|
9
9
|
*/
|
|
10
10
|
export declare class Arena {
|
|
11
|
-
context:
|
|
11
|
+
context: QuickJSContextEx;
|
|
12
12
|
_map: VMMap;
|
|
13
13
|
_registeredMap: VMMap;
|
|
14
14
|
_registeredMapDispose: Set<any>;
|
|
@@ -27,10 +27,125 @@ export declare class Arena {
|
|
|
27
27
|
* 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.
|
|
28
28
|
*/
|
|
29
29
|
evalCode<T = any>(code: string): T;
|
|
30
|
+
/**
|
|
31
|
+
* Evaluate ES module code in the VM. The module can import/export, but the return value
|
|
32
|
+
* depends on whether the module exports are accessible (implementation varies by quickjs-emscripten version).
|
|
33
|
+
* Use this for side effects or when you don't need access to exports.
|
|
34
|
+
*
|
|
35
|
+
* @param code - The ES module code to evaluate
|
|
36
|
+
* @param filename - Optional filename for debugging purposes (default: "module.js")
|
|
37
|
+
* @returns Undefined in most cases (module side effects are applied)
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```js
|
|
41
|
+
* // Execute module code with side effects
|
|
42
|
+
* arena.expose({ data: { count: 0 } });
|
|
43
|
+
* arena.evalModule('import { data } from "globals"; data.count = 42;'); // undefined, but data.count is now 42
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
evalModule(code: string, filename?: string): void;
|
|
30
47
|
/**
|
|
31
48
|
* Almost same as `vm.executePendingJobs()`, but it converts and re-throws error objects when an error is thrown during evaluation.
|
|
32
49
|
*/
|
|
33
50
|
executePendingJobs(maxJobsToExecute?: number): number;
|
|
51
|
+
/**
|
|
52
|
+
* Set the max memory this runtime can allocate.
|
|
53
|
+
* To remove the limit, set to `-1`.
|
|
54
|
+
*
|
|
55
|
+
* This is useful for preventing runaway memory usage in untrusted code.
|
|
56
|
+
*
|
|
57
|
+
* @param limitBytes - Maximum memory in bytes, or -1 to remove limit
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```js
|
|
61
|
+
* // Limit sandbox to 10MB
|
|
62
|
+
* arena.setMemoryLimit(10 * 1024 * 1024);
|
|
63
|
+
*
|
|
64
|
+
* try {
|
|
65
|
+
* arena.evalCode(`const huge = new Array(1000000000);`);
|
|
66
|
+
* } catch (e) {
|
|
67
|
+
* console.log("Memory limit exceeded");
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
setMemoryLimit(limitBytes: number): void;
|
|
72
|
+
/**
|
|
73
|
+
* Set the max stack size for this runtime, in bytes.
|
|
74
|
+
* To remove the limit, set to `0`.
|
|
75
|
+
*
|
|
76
|
+
* This is useful for preventing stack overflow from deeply nested calls or recursion.
|
|
77
|
+
*
|
|
78
|
+
* @param stackSize - Maximum stack size in bytes, or 0 to remove limit
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```js
|
|
82
|
+
* // Limit stack to 512KB
|
|
83
|
+
* arena.setMaxStackSize(512 * 1024);
|
|
84
|
+
*
|
|
85
|
+
* try {
|
|
86
|
+
* arena.evalCode(`function recurse() { recurse(); } recurse();`);
|
|
87
|
+
* } catch (e) {
|
|
88
|
+
* console.log("Stack overflow prevented");
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
setMaxStackSize(stackSize: number): void;
|
|
93
|
+
/**
|
|
94
|
+
* Get detailed memory usage statistics for this runtime.
|
|
95
|
+
*
|
|
96
|
+
* @returns An object containing detailed memory allocation information
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```js
|
|
100
|
+
* const stats = arena.getMemoryUsage();
|
|
101
|
+
* console.log(`Memory used: ${stats.memory_used_size} bytes`);
|
|
102
|
+
* console.log(`Object count: ${stats.obj_count}`);
|
|
103
|
+
* console.log(`Memory limit: ${stats.malloc_limit}`);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
getMemoryUsage(): {
|
|
107
|
+
malloc_limit: number;
|
|
108
|
+
memory_used_size: number;
|
|
109
|
+
malloc_count: number;
|
|
110
|
+
memory_used_count: number;
|
|
111
|
+
atom_count: number;
|
|
112
|
+
atom_size: number;
|
|
113
|
+
str_count: number;
|
|
114
|
+
str_size: number;
|
|
115
|
+
obj_count: number;
|
|
116
|
+
obj_size: number;
|
|
117
|
+
prop_count: number;
|
|
118
|
+
prop_size: number;
|
|
119
|
+
shape_count: number;
|
|
120
|
+
shape_size: number;
|
|
121
|
+
js_func_count: number;
|
|
122
|
+
js_func_size: number;
|
|
123
|
+
js_func_code_size: number;
|
|
124
|
+
js_func_pc2line_count: number;
|
|
125
|
+
js_func_pc2line_size: number;
|
|
126
|
+
c_func_count: number;
|
|
127
|
+
array_count: number;
|
|
128
|
+
fast_array_count: number;
|
|
129
|
+
fast_array_elements: number;
|
|
130
|
+
binary_object_count: number;
|
|
131
|
+
binary_object_size: number;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Get a human-readable description of memory usage in this runtime.
|
|
135
|
+
*
|
|
136
|
+
* @returns A formatted string showing memory statistics
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```js
|
|
140
|
+
* console.log(arena.dumpMemoryUsage());
|
|
141
|
+
* // Output:
|
|
142
|
+
* // QuickJS memory usage:
|
|
143
|
+
* // malloc_limit: 4294967295
|
|
144
|
+
* // memory_used_size: 67078
|
|
145
|
+
* // ...
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
dumpMemoryUsage(): string;
|
|
34
149
|
/**
|
|
35
150
|
* Expose objects as global objects in the VM.
|
|
36
151
|
*
|
|
@@ -129,6 +244,8 @@ export declare type Options = {
|
|
|
129
244
|
isHandleWrappable?: (handle: QuickJSHandle, ctx: QuickJSContext) => boolean;
|
|
130
245
|
/** Compatibility with quickjs-emscripten prior to v0.15. Inject code for compatibility into context at Arena class initialization time. */
|
|
131
246
|
compat?: boolean;
|
|
247
|
+
/** Experimental: use QuickJSContextEx, which wraps existing QuickJSContext. */
|
|
248
|
+
experimentalContextEx?: boolean;
|
|
132
249
|
};
|
|
133
250
|
|
|
134
251
|
declare type Options_2 = {
|
|
@@ -150,6 +267,10 @@ declare type Options_3 = {
|
|
|
150
267
|
custom?: Iterable<(obj: QuickJSHandle, ctx: QuickJSContext) => any>;
|
|
151
268
|
};
|
|
152
269
|
|
|
270
|
+
declare type QuickJSContextEx = QuickJSContext & {
|
|
271
|
+
disposeEx?: () => void;
|
|
272
|
+
};
|
|
273
|
+
|
|
153
274
|
export declare function unmarshal(handle: QuickJSHandle, options: Options_3): any;
|
|
154
275
|
|
|
155
276
|
export declare class VMMap {
|
|
@@ -172,7 +293,7 @@ export declare class VMMap {
|
|
|
172
293
|
getByHandle(handle: QuickJSHandle): any;
|
|
173
294
|
has(key: any): boolean;
|
|
174
295
|
hasHandle(handle: QuickJSHandle): boolean;
|
|
175
|
-
keys():
|
|
296
|
+
keys(): MapIterator<any>;
|
|
176
297
|
delete(key: any, dispose?: boolean): void;
|
|
177
298
|
deleteByHandle(handle: QuickJSHandle, dispose?: boolean): void;
|
|
178
299
|
clear(): void;
|