quickjs-emscripten-sync 1.5.2 → 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 CHANGED
@@ -1,8 +1,8 @@
1
1
  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';
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.
@@ -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
  *
@@ -178,7 +293,7 @@ export declare class VMMap {
178
293
  getByHandle(handle: QuickJSHandle): any;
179
294
  has(key: any): boolean;
180
295
  hasHandle(handle: QuickJSHandle): boolean;
181
- keys(): IterableIterator<any>;
296
+ keys(): MapIterator<any>;
182
297
  delete(key: any, dispose?: boolean): void;
183
298
  deleteByHandle(handle: QuickJSHandle, dispose?: boolean): void;
184
299
  clear(): void;