quickjs-emscripten-sync 1.5.2 → 1.7.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 CHANGED
@@ -1,8 +1,9 @@
1
+ import { Intrinsics } from 'quickjs-emscripten';
1
2
  import { QuickJSContext } from 'quickjs-emscripten';
2
- import type { QuickJSDeferredPromise } from 'quickjs-emscripten';
3
- import type { QuickJSHandle } from 'quickjs-emscripten';
4
- import type { SuccessOrFail } from 'quickjs-emscripten';
5
- import type { VmCallResult } from 'quickjs-emscripten';
3
+ import { QuickJSDeferredPromise } from 'quickjs-emscripten';
4
+ import { QuickJSHandle } from 'quickjs-emscripten';
5
+ import { SuccessOrFail } from 'quickjs-emscripten';
6
+ import { VmCallResult } from 'quickjs-emscripten';
6
7
 
7
8
  /**
8
9
  * The Arena class manages all generated handles at once by quickjs-emscripten and automatically converts objects between the host and the QuickJS VM.
@@ -27,10 +28,141 @@ export declare class Arena {
27
28
  * 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
29
  */
29
30
  evalCode<T = any>(code: string): T;
31
+ /**
32
+ * Evaluate ES module code in the VM and get the module's exports.
33
+ *
34
+ * Requires quickjs-emscripten >= 0.29.0 for export access.
35
+ *
36
+ * @param code - The ES module code to evaluate
37
+ * @param filename - Optional filename for debugging purposes (default: "module.js")
38
+ * @returns The module's exports object, or a Promise resolving to exports if using top-level await
39
+ *
40
+ * @example
41
+ * ```js
42
+ * // Simple module with exports
43
+ * const exports = arena.evalModule(`
44
+ * export const value = 42;
45
+ * export function greet(name) {
46
+ * return "Hello, " + name;
47
+ * }
48
+ * `);
49
+ * console.log(exports.value); // 42
50
+ * console.log(exports.greet("World")); // "Hello, World"
51
+ *
52
+ * // Module with default export
53
+ * const mod = arena.evalModule('export default function(x) { return x * 2; }');
54
+ * console.log(mod.default(21)); // 42
55
+ *
56
+ * // Module with top-level await
57
+ * const promise = arena.evalModule('export const data = await Promise.resolve(123);');
58
+ * arena.executePendingJobs();
59
+ * const exports = await promise;
60
+ * console.log(exports.data); // 123
61
+ * ```
62
+ */
63
+ evalModule<T = any>(code: string, filename?: string): T | Promise<T>;
30
64
  /**
31
65
  * Almost same as `vm.executePendingJobs()`, but it converts and re-throws error objects when an error is thrown during evaluation.
32
66
  */
33
67
  executePendingJobs(maxJobsToExecute?: number): number;
68
+ /**
69
+ * Set the max memory this runtime can allocate.
70
+ * To remove the limit, set to `-1`.
71
+ *
72
+ * This is useful for preventing runaway memory usage in untrusted code.
73
+ *
74
+ * @param limitBytes - Maximum memory in bytes, or -1 to remove limit
75
+ *
76
+ * @example
77
+ * ```js
78
+ * // Limit sandbox to 10MB
79
+ * arena.setMemoryLimit(10 * 1024 * 1024);
80
+ *
81
+ * try {
82
+ * arena.evalCode(`const huge = new Array(1000000000);`);
83
+ * } catch (e) {
84
+ * console.log("Memory limit exceeded");
85
+ * }
86
+ * ```
87
+ */
88
+ setMemoryLimit(limitBytes: number): void;
89
+ /**
90
+ * Set the max stack size for this runtime, in bytes.
91
+ * To remove the limit, set to `0`.
92
+ *
93
+ * This is useful for preventing stack overflow from deeply nested calls or recursion.
94
+ *
95
+ * @param stackSize - Maximum stack size in bytes, or 0 to remove limit
96
+ *
97
+ * @example
98
+ * ```js
99
+ * // Limit stack to 512KB
100
+ * arena.setMaxStackSize(512 * 1024);
101
+ *
102
+ * try {
103
+ * arena.evalCode(`function recurse() { recurse(); } recurse();`);
104
+ * } catch (e) {
105
+ * console.log("Stack overflow prevented");
106
+ * }
107
+ * ```
108
+ */
109
+ setMaxStackSize(stackSize: number): void;
110
+ /**
111
+ * Get detailed memory usage statistics for this runtime.
112
+ *
113
+ * @returns An object containing detailed memory allocation information
114
+ *
115
+ * @example
116
+ * ```js
117
+ * const stats = arena.getMemoryUsage();
118
+ * console.log(`Memory used: ${stats.memory_used_size} bytes`);
119
+ * console.log(`Object count: ${stats.obj_count}`);
120
+ * console.log(`Memory limit: ${stats.malloc_limit}`);
121
+ * ```
122
+ */
123
+ getMemoryUsage(): {
124
+ malloc_limit: number;
125
+ memory_used_size: number;
126
+ malloc_count: number;
127
+ memory_used_count: number;
128
+ atom_count: number;
129
+ atom_size: number;
130
+ str_count: number;
131
+ str_size: number;
132
+ obj_count: number;
133
+ obj_size: number;
134
+ prop_count: number;
135
+ prop_size: number;
136
+ shape_count: number;
137
+ shape_size: number;
138
+ js_func_count: number;
139
+ js_func_size: number;
140
+ js_func_code_size: number;
141
+ js_func_pc2line_count: number;
142
+ js_func_pc2line_size: number;
143
+ c_func_count: number;
144
+ array_count: number;
145
+ fast_array_count: number;
146
+ fast_array_elements: number;
147
+ binary_object_count: number;
148
+ binary_object_size: number;
149
+ };
150
+ /**
151
+ * Get a human-readable description of memory usage in this runtime.
152
+ *
153
+ * @returns A formatted string showing memory statistics
154
+ *
155
+ * @example
156
+ * ```js
157
+ * console.log(arena.dumpMemoryUsage());
158
+ * // Output:
159
+ * // QuickJS memory usage:
160
+ * // malloc_limit: 4294967295
161
+ * // memory_used_size: 67078
162
+ * // ...
163
+ * ```
164
+ */
165
+ dumpMemoryUsage(): string;
34
166
  /**
35
167
  * Expose objects as global objects in the VM.
36
168
  *
@@ -101,6 +233,8 @@ export declare const defaultRegisteredObjects: [any, string][];
101
233
 
102
234
  export declare function eq(ctx: QuickJSContext, a: QuickJSHandle, b: QuickJSHandle): boolean;
103
235
 
236
+ export { Intrinsics }
237
+
104
238
  export declare function isES2015Class(cls: any): cls is new (...args: any[]) => any;
105
239
 
106
240
  export declare function isHandleObject(ctx: QuickJSContext, h: QuickJSHandle): boolean;
@@ -178,7 +312,7 @@ export declare class VMMap {
178
312
  getByHandle(handle: QuickJSHandle): any;
179
313
  has(key: any): boolean;
180
314
  hasHandle(handle: QuickJSHandle): boolean;
181
- keys(): IterableIterator<any>;
315
+ keys(): MapIterator<any>;
182
316
  delete(key: any, dispose?: boolean): void;
183
317
  deleteByHandle(handle: QuickJSHandle, dispose?: boolean): void;
184
318
  clear(): void;