@ricsam/quickjs-runtime 0.2.12 → 0.2.14

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.
@@ -101,16 +101,75 @@ function setupRuntime(context, options = {}) {
101
101
  encoding,
102
102
  timers,
103
103
  stateMap,
104
+ get context() {
105
+ return context;
106
+ },
107
+ drainPendingJobs(maxIterations = 1000) {
108
+ for (let i = 0;i < maxIterations; i++) {
109
+ if (!context.runtime.hasPendingJob()) {
110
+ return true;
111
+ }
112
+ const result = context.runtime.executePendingJobs();
113
+ if (result.error) {
114
+ result.error.dispose();
115
+ }
116
+ }
117
+ return !context.runtime.hasPendingJob();
118
+ },
104
119
  dispose() {
120
+ for (let i = 0;i < 1000; i++) {
121
+ if (!context.runtime.hasPendingJob())
122
+ break;
123
+ const result = context.runtime.executePendingJobs();
124
+ if (result.error)
125
+ result.error.dispose();
126
+ }
105
127
  timers?.dispose();
106
128
  encoding?.dispose();
107
129
  consoleHandle?.dispose();
108
130
  fs?.dispose();
109
131
  fetch?.dispose();
132
+ for (let i = 0;i < 1000; i++) {
133
+ if (!context.runtime.hasPendingJob())
134
+ break;
135
+ const result = context.runtime.executePendingJobs();
136
+ if (result.error)
137
+ result.error.dispose();
138
+ }
139
+ import_quickjs_core.cleanupUnmarshaledHandles(context);
140
+ import_quickjs_core.clearAllInstanceState();
141
+ try {
142
+ const clearGlobals = context.evalCode(`
143
+ (function() {
144
+ const keysToDelete = Object.keys(globalThis).filter(k =>
145
+ k !== 'globalThis' && k !== 'undefined' && k !== 'NaN' && k !== 'Infinity'
146
+ );
147
+ for (const key of keysToDelete) {
148
+ try { globalThis[key] = undefined; } catch (e) {}
149
+ }
150
+ for (const key of keysToDelete) {
151
+ try { delete globalThis[key]; } catch (e) {}
152
+ }
153
+ return keysToDelete.length;
154
+ })()
155
+ `);
156
+ if (clearGlobals.error) {
157
+ clearGlobals.error.dispose();
158
+ } else {
159
+ clearGlobals.value.dispose();
160
+ }
161
+ } catch (e) {}
162
+ for (let i = 0;i < 100; i++) {
163
+ if (!context.runtime.hasPendingJob())
164
+ break;
165
+ const result = context.runtime.executePendingJobs();
166
+ if (result.error)
167
+ result.error.dispose();
168
+ }
110
169
  core.dispose();
111
170
  }
112
171
  };
113
172
  }
114
173
  })
115
174
 
116
- //# debugId=C7F4ACB19831533264756E2164756E21
175
+ //# debugId=08932F65633CC6C364756E2164756E21
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
4
  "sourcesContent": [
5
- "// Re-export everything from sub-packages\nexport * from \"@ricsam/quickjs-core\";\nexport * from \"@ricsam/quickjs-console\";\nexport * from \"@ricsam/quickjs-encoding\";\nexport * from \"@ricsam/quickjs-fetch\";\nexport * from \"@ricsam/quickjs-fs\";\nexport * from \"@ricsam/quickjs-timers\";\n\nimport type { QuickJSContext } from \"quickjs-emscripten\";\nimport {\n setupCore,\n createStateMap,\n type StateMap,\n type CoreHandle,\n} from \"@ricsam/quickjs-core\";\nimport {\n setupFetch,\n type SetupFetchOptions,\n type FetchHandle,\n} from \"@ricsam/quickjs-fetch\";\nimport {\n setupFs,\n type SetupFsOptions,\n type FsHandle,\n} from \"@ricsam/quickjs-fs\";\nimport {\n setupConsole,\n type SetupConsoleOptions,\n type ConsoleHandle,\n} from \"@ricsam/quickjs-console\";\nimport {\n setupEncoding,\n type SetupEncodingOptions,\n type EncodingHandle,\n} from \"@ricsam/quickjs-encoding\";\nimport {\n setupTimers,\n type SetupTimersOptions,\n type TimersHandle,\n} from \"@ricsam/quickjs-timers\";\n\n/**\n * Options for setting up the runtime\n */\nexport interface SetupRuntimeOptions {\n /**\n * Fetch API options\n * Pass `true` to enable with defaults\n * Pass `false` or omit to disable\n */\n fetch?: SetupFetchOptions | boolean;\n\n /**\n * File system options\n * Pass options object to enable\n * Pass `false` or omit to disable\n */\n fs?: SetupFsOptions | false;\n\n /**\n * Console options\n * Pass `true` to enable with defaults\n * Pass handlers object to customize\n * Pass `false` or omit to disable\n */\n console?: SetupConsoleOptions | boolean;\n\n /**\n * Encoding options (atob/btoa)\n * Pass `true` to enable with defaults\n * Pass `false` or omit to disable\n */\n encoding?: SetupEncodingOptions | boolean;\n\n /**\n * Timer options (setTimeout, setInterval, clearTimeout, clearInterval)\n * Pass `true` to enable with defaults\n * Pass `false` or omit to disable\n */\n timers?: SetupTimersOptions | boolean;\n}\n\n/**\n * Handle returned from setupRuntime\n */\nexport interface RuntimeHandle {\n /** Core handle (always present) */\n core: CoreHandle;\n\n /** Fetch handle (if enabled) */\n fetch?: FetchHandle;\n\n /** File system handle (if enabled) */\n fs?: FsHandle;\n\n /** Console handle (if enabled) */\n console?: ConsoleHandle;\n\n /** Encoding handle (if enabled) */\n encoding?: EncodingHandle;\n\n /** Timers handle (if enabled) */\n timers?: TimersHandle;\n\n /** Shared state map */\n readonly stateMap: StateMap;\n\n /** Dispose all handles and cleanup */\n dispose(): void;\n}\n\n/**\n * Setup multiple APIs in a QuickJS context\n *\n * @example\n * const handle = setupRuntime(context, {\n * fetch: {\n * onFetch: async (req) => fetch(req),\n * },\n * fs: {\n * getDirectory: async (path) => createNodeDirectoryHandle(`/sandbox${path}`),\n * },\n * });\n *\n * // Use the context...\n *\n * handle.dispose();\n */\nexport function setupRuntime(\n context: QuickJSContext,\n options: SetupRuntimeOptions = {}\n): RuntimeHandle {\n const stateMap = createStateMap();\n\n // Always setup core\n const core = setupCore(context, { stateMap });\n\n let fetch: FetchHandle | undefined;\n let fs: FsHandle | undefined;\n let consoleHandle: ConsoleHandle | undefined;\n let encoding: EncodingHandle | undefined;\n let timers: TimersHandle | undefined;\n\n // Setup fetch if enabled\n if (options.fetch) {\n const fetchOptions: SetupFetchOptions =\n options.fetch === true\n ? { stateMap, coreHandle: core }\n : { ...options.fetch, stateMap, coreHandle: core };\n\n fetch = setupFetch(context, fetchOptions);\n }\n\n // Setup fs if provided\n if (options.fs) {\n fs = setupFs(context, {\n ...options.fs,\n stateMap,\n coreHandle: core,\n });\n }\n\n // Setup console if enabled\n if (options.console) {\n const consoleOptions: SetupConsoleOptions =\n options.console === true\n ? { stateMap, coreHandle: core }\n : { ...options.console, stateMap, coreHandle: core };\n\n consoleHandle = setupConsole(context, consoleOptions);\n }\n\n // Setup encoding if enabled\n if (options.encoding) {\n const encodingOptions: SetupEncodingOptions =\n options.encoding === true\n ? { stateMap, coreHandle: core }\n : { ...options.encoding, stateMap, coreHandle: core };\n\n encoding = setupEncoding(context, encodingOptions);\n }\n\n // Setup timers if enabled\n if (options.timers) {\n const timersOptions: SetupTimersOptions =\n options.timers === true\n ? { stateMap, coreHandle: core }\n : { ...options.timers, stateMap, coreHandle: core };\n\n timers = setupTimers(context, timersOptions);\n }\n\n return {\n core,\n fetch,\n fs,\n console: consoleHandle,\n encoding,\n timers,\n stateMap,\n dispose() {\n // Dispose in reverse order of setup\n timers?.dispose();\n encoding?.dispose();\n consoleHandle?.dispose();\n fs?.dispose();\n fetch?.dispose();\n core.dispose();\n },\n };\n}\n"
5
+ "// Re-export everything from sub-packages\nexport * from \"@ricsam/quickjs-core\";\nexport * from \"@ricsam/quickjs-console\";\nexport * from \"@ricsam/quickjs-encoding\";\nexport * from \"@ricsam/quickjs-fetch\";\nexport * from \"@ricsam/quickjs-fs\";\nexport * from \"@ricsam/quickjs-timers\";\n\nimport type { QuickJSContext } from \"quickjs-emscripten\";\nimport {\n setupCore,\n createStateMap,\n cleanupUnmarshaledHandles,\n clearAllInstanceState,\n type StateMap,\n type CoreHandle,\n} from \"@ricsam/quickjs-core\";\nimport {\n setupFetch,\n type SetupFetchOptions,\n type FetchHandle,\n} from \"@ricsam/quickjs-fetch\";\nimport {\n setupFs,\n type SetupFsOptions,\n type FsHandle,\n} from \"@ricsam/quickjs-fs\";\nimport {\n setupConsole,\n type SetupConsoleOptions,\n type ConsoleHandle,\n} from \"@ricsam/quickjs-console\";\nimport {\n setupEncoding,\n type SetupEncodingOptions,\n type EncodingHandle,\n} from \"@ricsam/quickjs-encoding\";\nimport {\n setupTimers,\n type SetupTimersOptions,\n type TimersHandle,\n} from \"@ricsam/quickjs-timers\";\n\n/**\n * Options for setting up the runtime\n */\nexport interface SetupRuntimeOptions {\n /**\n * Fetch API options\n * Pass `true` to enable with defaults\n * Pass `false` or omit to disable\n */\n fetch?: SetupFetchOptions | boolean;\n\n /**\n * File system options\n * Pass options object to enable\n * Pass `false` or omit to disable\n */\n fs?: SetupFsOptions | false;\n\n /**\n * Console options\n * Pass `true` to enable with defaults\n * Pass handlers object to customize\n * Pass `false` or omit to disable\n */\n console?: SetupConsoleOptions | boolean;\n\n /**\n * Encoding options (atob/btoa)\n * Pass `true` to enable with defaults\n * Pass `false` or omit to disable\n */\n encoding?: SetupEncodingOptions | boolean;\n\n /**\n * Timer options (setTimeout, setInterval, clearTimeout, clearInterval)\n * Pass `true` to enable with defaults\n * Pass `false` or omit to disable\n */\n timers?: SetupTimersOptions | boolean;\n}\n\n/**\n * Handle returned from setupRuntime\n */\nexport interface RuntimeHandle {\n /** Core handle (always present) */\n core: CoreHandle;\n\n /** Fetch handle (if enabled) */\n fetch?: FetchHandle;\n\n /** File system handle (if enabled) */\n fs?: FsHandle;\n\n /** Console handle (if enabled) */\n console?: ConsoleHandle;\n\n /** Encoding handle (if enabled) */\n encoding?: EncodingHandle;\n\n /** Timers handle (if enabled) */\n timers?: TimersHandle;\n\n /** Shared state map */\n readonly stateMap: StateMap;\n\n /** Get the underlying QuickJS context */\n readonly context: QuickJSContext;\n\n /**\n * Drain all pending jobs with timeout.\n * Call BEFORE dispose() to prevent GC assertion failures.\n * @param maxIterations - Maximum iterations (default: 1000)\n * @returns true if all jobs completed, false if timed out\n */\n drainPendingJobs(maxIterations?: number): boolean;\n\n /** Dispose all handles and cleanup */\n dispose(): void;\n}\n\n/**\n * Setup multiple APIs in a QuickJS context\n *\n * @example\n * const handle = setupRuntime(context, {\n * fetch: {\n * onFetch: async (req) => fetch(req),\n * },\n * fs: {\n * getDirectory: async (path) => createNodeDirectoryHandle(`/sandbox${path}`),\n * },\n * });\n *\n * // Use the context...\n *\n * handle.dispose();\n */\nexport function setupRuntime(\n context: QuickJSContext,\n options: SetupRuntimeOptions = {}\n): RuntimeHandle {\n const stateMap = createStateMap();\n\n // Always setup core\n const core = setupCore(context, { stateMap });\n\n let fetch: FetchHandle | undefined;\n let fs: FsHandle | undefined;\n let consoleHandle: ConsoleHandle | undefined;\n let encoding: EncodingHandle | undefined;\n let timers: TimersHandle | undefined;\n\n // Setup fetch if enabled\n if (options.fetch) {\n const fetchOptions: SetupFetchOptions =\n options.fetch === true\n ? { stateMap, coreHandle: core }\n : { ...options.fetch, stateMap, coreHandle: core };\n\n fetch = setupFetch(context, fetchOptions);\n }\n\n // Setup fs if provided\n if (options.fs) {\n fs = setupFs(context, {\n ...options.fs,\n stateMap,\n coreHandle: core,\n });\n }\n\n // Setup console if enabled\n if (options.console) {\n const consoleOptions: SetupConsoleOptions =\n options.console === true\n ? { stateMap, coreHandle: core }\n : { ...options.console, stateMap, coreHandle: core };\n\n consoleHandle = setupConsole(context, consoleOptions);\n }\n\n // Setup encoding if enabled\n if (options.encoding) {\n const encodingOptions: SetupEncodingOptions =\n options.encoding === true\n ? { stateMap, coreHandle: core }\n : { ...options.encoding, stateMap, coreHandle: core };\n\n encoding = setupEncoding(context, encodingOptions);\n }\n\n // Setup timers if enabled\n if (options.timers) {\n const timersOptions: SetupTimersOptions =\n options.timers === true\n ? { stateMap, coreHandle: core }\n : { ...options.timers, stateMap, coreHandle: core };\n\n timers = setupTimers(context, timersOptions);\n }\n\n return {\n core,\n fetch,\n fs,\n console: consoleHandle,\n encoding,\n timers,\n stateMap,\n get context() {\n return context;\n },\n drainPendingJobs(maxIterations = 1000): boolean {\n for (let i = 0; i < maxIterations; i++) {\n if (!context.runtime.hasPendingJob()) {\n return true;\n }\n const result = context.runtime.executePendingJobs();\n if (result.error) {\n result.error.dispose();\n }\n }\n return !context.runtime.hasPendingJob();\n },\n dispose() {\n // Drain pending jobs before disposal\n for (let i = 0; i < 1000; i++) {\n if (!context.runtime.hasPendingJob()) break;\n const result = context.runtime.executePendingJobs();\n if (result.error) result.error.dispose();\n }\n\n // Dispose sub-handles in reverse order\n timers?.dispose();\n encoding?.dispose();\n consoleHandle?.dispose();\n fs?.dispose();\n fetch?.dispose();\n\n // Drain pending jobs again after sub-handle disposal\n // (fetch?.dispose() calls evalCode which can create new jobs)\n for (let i = 0; i < 1000; i++) {\n if (!context.runtime.hasPendingJob()) break;\n const result = context.runtime.executePendingJobs();\n if (result.error) result.error.dispose();\n }\n\n // Clean up tracked handles\n cleanupUnmarshaledHandles(context);\n clearAllInstanceState();\n\n // Clear globals to help GC break circular references\n try {\n const clearGlobals = context.evalCode(`\n (function() {\n const keysToDelete = Object.keys(globalThis).filter(k =>\n k !== 'globalThis' && k !== 'undefined' && k !== 'NaN' && k !== 'Infinity'\n );\n for (const key of keysToDelete) {\n try { globalThis[key] = undefined; } catch (e) {}\n }\n for (const key of keysToDelete) {\n try { delete globalThis[key]; } catch (e) {}\n }\n return keysToDelete.length;\n })()\n `);\n if (clearGlobals.error) {\n clearGlobals.error.dispose();\n } else {\n clearGlobals.value.dispose();\n }\n } catch (e) {\n // Ignore errors during cleanup\n }\n\n // Final drain\n for (let i = 0; i < 100; i++) {\n if (!context.runtime.hasPendingJob()) break;\n const result = context.runtime.executePendingJobs();\n if (result.error) result.error.dispose();\n }\n\n core.dispose();\n },\n };\n}\n"
6
6
  ],
7
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA;AACA;AACA;AACA;AACA;AACA;AAQO,IALP;AAUO,IAJP;AASO,IAJP;AASO,IAJP;AASO,IAJP;AASO,IAJP;AA6FO,SAAS,YAAY,CAC1B,SACA,UAA+B,CAAC,GACjB;AAAA,EACf,MAAM,WAAW,mCAAe;AAAA,EAGhC,MAAM,OAAO,8BAAU,SAAS,EAAE,SAAS,CAAC;AAAA,EAE5C,IAAI;AAAA,EAC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AO,MAAM,QAAQ;AAAA,MACzC;AAAA,MAGA,8CAA0B,OAAO;AAAA,MACjC,0CAAsB;AAAA,MAGtB,IAAI;AAAA,QACF,MAAM,eAAe,QAAQ,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,SAarC;AAAA,QACD,IAAI,aAAa,OAAO;AAAA,UACtB,aAAa,MAAM,QAAQ;AAAA,QAC7B,EAAO;AAAA,UACL,aAAa,MAAM,QAAQ;AAAA;AAAA,QAE7B,OAAO,GAAG;AAAA,MAKZ,SAAS,IAAI,EAAG,IAAI,KAAK,KAAK;AAAA,QAC5B,IAAI,CAAC,QAAQ,QAAQ,cAAc;AAAA,UAAG;AAAA,QACtC,MAAM,SAAS,QAAQ,QAAQ,mBAAmB;AAAA,QAClD,IAAI,OAAO;AAAA,UAAO,OAAO,MAAM,QAAQ;AAAA,MACzC;AAAA,MAEA,KAAK,QAAQ;AAAA;AAAA,EAEjB;AAAA;",
8
+ "debugId": "08932F65633CC6C364756E2164756E21",
9
9
  "names": []
10
10
  }
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/quickjs-runtime",
3
- "version": "0.2.12",
3
+ "version": "0.2.14",
4
4
  "type": "commonjs"
5
5
  }
@@ -8,7 +8,9 @@ export * from "@ricsam/quickjs-fs";
8
8
  export * from "@ricsam/quickjs-timers";
9
9
  import {
10
10
  setupCore,
11
- createStateMap
11
+ createStateMap,
12
+ cleanupUnmarshaledHandles,
13
+ clearAllInstanceState
12
14
  } from "@ricsam/quickjs-core";
13
15
  import {
14
16
  setupFetch
@@ -64,12 +66,71 @@ function setupRuntime(context, options = {}) {
64
66
  encoding,
65
67
  timers,
66
68
  stateMap,
69
+ get context() {
70
+ return context;
71
+ },
72
+ drainPendingJobs(maxIterations = 1000) {
73
+ for (let i = 0;i < maxIterations; i++) {
74
+ if (!context.runtime.hasPendingJob()) {
75
+ return true;
76
+ }
77
+ const result = context.runtime.executePendingJobs();
78
+ if (result.error) {
79
+ result.error.dispose();
80
+ }
81
+ }
82
+ return !context.runtime.hasPendingJob();
83
+ },
67
84
  dispose() {
85
+ for (let i = 0;i < 1000; i++) {
86
+ if (!context.runtime.hasPendingJob())
87
+ break;
88
+ const result = context.runtime.executePendingJobs();
89
+ if (result.error)
90
+ result.error.dispose();
91
+ }
68
92
  timers?.dispose();
69
93
  encoding?.dispose();
70
94
  consoleHandle?.dispose();
71
95
  fs?.dispose();
72
96
  fetch?.dispose();
97
+ for (let i = 0;i < 1000; i++) {
98
+ if (!context.runtime.hasPendingJob())
99
+ break;
100
+ const result = context.runtime.executePendingJobs();
101
+ if (result.error)
102
+ result.error.dispose();
103
+ }
104
+ cleanupUnmarshaledHandles(context);
105
+ clearAllInstanceState();
106
+ try {
107
+ const clearGlobals = context.evalCode(`
108
+ (function() {
109
+ const keysToDelete = Object.keys(globalThis).filter(k =>
110
+ k !== 'globalThis' && k !== 'undefined' && k !== 'NaN' && k !== 'Infinity'
111
+ );
112
+ for (const key of keysToDelete) {
113
+ try { globalThis[key] = undefined; } catch (e) {}
114
+ }
115
+ for (const key of keysToDelete) {
116
+ try { delete globalThis[key]; } catch (e) {}
117
+ }
118
+ return keysToDelete.length;
119
+ })()
120
+ `);
121
+ if (clearGlobals.error) {
122
+ clearGlobals.error.dispose();
123
+ } else {
124
+ clearGlobals.value.dispose();
125
+ }
126
+ } catch (e) {}
127
+ for (let i = 0;i < 100; i++) {
128
+ if (!context.runtime.hasPendingJob())
129
+ break;
130
+ const result = context.runtime.executePendingJobs();
131
+ if (result.error)
132
+ result.error.dispose();
133
+ }
73
134
  core.dispose();
74
135
  }
75
136
  };
@@ -78,4 +139,4 @@ export {
78
139
  setupRuntime
79
140
  };
80
141
 
81
- //# debugId=64F451904941A71C64756E2164756E21
142
+ //# debugId=91E27187E6392B3064756E2164756E21
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
4
  "sourcesContent": [
5
- "// Re-export everything from sub-packages\nexport * from \"@ricsam/quickjs-core\";\nexport * from \"@ricsam/quickjs-console\";\nexport * from \"@ricsam/quickjs-encoding\";\nexport * from \"@ricsam/quickjs-fetch\";\nexport * from \"@ricsam/quickjs-fs\";\nexport * from \"@ricsam/quickjs-timers\";\n\nimport type { QuickJSContext } from \"quickjs-emscripten\";\nimport {\n setupCore,\n createStateMap,\n type StateMap,\n type CoreHandle,\n} from \"@ricsam/quickjs-core\";\nimport {\n setupFetch,\n type SetupFetchOptions,\n type FetchHandle,\n} from \"@ricsam/quickjs-fetch\";\nimport {\n setupFs,\n type SetupFsOptions,\n type FsHandle,\n} from \"@ricsam/quickjs-fs\";\nimport {\n setupConsole,\n type SetupConsoleOptions,\n type ConsoleHandle,\n} from \"@ricsam/quickjs-console\";\nimport {\n setupEncoding,\n type SetupEncodingOptions,\n type EncodingHandle,\n} from \"@ricsam/quickjs-encoding\";\nimport {\n setupTimers,\n type SetupTimersOptions,\n type TimersHandle,\n} from \"@ricsam/quickjs-timers\";\n\n/**\n * Options for setting up the runtime\n */\nexport interface SetupRuntimeOptions {\n /**\n * Fetch API options\n * Pass `true` to enable with defaults\n * Pass `false` or omit to disable\n */\n fetch?: SetupFetchOptions | boolean;\n\n /**\n * File system options\n * Pass options object to enable\n * Pass `false` or omit to disable\n */\n fs?: SetupFsOptions | false;\n\n /**\n * Console options\n * Pass `true` to enable with defaults\n * Pass handlers object to customize\n * Pass `false` or omit to disable\n */\n console?: SetupConsoleOptions | boolean;\n\n /**\n * Encoding options (atob/btoa)\n * Pass `true` to enable with defaults\n * Pass `false` or omit to disable\n */\n encoding?: SetupEncodingOptions | boolean;\n\n /**\n * Timer options (setTimeout, setInterval, clearTimeout, clearInterval)\n * Pass `true` to enable with defaults\n * Pass `false` or omit to disable\n */\n timers?: SetupTimersOptions | boolean;\n}\n\n/**\n * Handle returned from setupRuntime\n */\nexport interface RuntimeHandle {\n /** Core handle (always present) */\n core: CoreHandle;\n\n /** Fetch handle (if enabled) */\n fetch?: FetchHandle;\n\n /** File system handle (if enabled) */\n fs?: FsHandle;\n\n /** Console handle (if enabled) */\n console?: ConsoleHandle;\n\n /** Encoding handle (if enabled) */\n encoding?: EncodingHandle;\n\n /** Timers handle (if enabled) */\n timers?: TimersHandle;\n\n /** Shared state map */\n readonly stateMap: StateMap;\n\n /** Dispose all handles and cleanup */\n dispose(): void;\n}\n\n/**\n * Setup multiple APIs in a QuickJS context\n *\n * @example\n * const handle = setupRuntime(context, {\n * fetch: {\n * onFetch: async (req) => fetch(req),\n * },\n * fs: {\n * getDirectory: async (path) => createNodeDirectoryHandle(`/sandbox${path}`),\n * },\n * });\n *\n * // Use the context...\n *\n * handle.dispose();\n */\nexport function setupRuntime(\n context: QuickJSContext,\n options: SetupRuntimeOptions = {}\n): RuntimeHandle {\n const stateMap = createStateMap();\n\n // Always setup core\n const core = setupCore(context, { stateMap });\n\n let fetch: FetchHandle | undefined;\n let fs: FsHandle | undefined;\n let consoleHandle: ConsoleHandle | undefined;\n let encoding: EncodingHandle | undefined;\n let timers: TimersHandle | undefined;\n\n // Setup fetch if enabled\n if (options.fetch) {\n const fetchOptions: SetupFetchOptions =\n options.fetch === true\n ? { stateMap, coreHandle: core }\n : { ...options.fetch, stateMap, coreHandle: core };\n\n fetch = setupFetch(context, fetchOptions);\n }\n\n // Setup fs if provided\n if (options.fs) {\n fs = setupFs(context, {\n ...options.fs,\n stateMap,\n coreHandle: core,\n });\n }\n\n // Setup console if enabled\n if (options.console) {\n const consoleOptions: SetupConsoleOptions =\n options.console === true\n ? { stateMap, coreHandle: core }\n : { ...options.console, stateMap, coreHandle: core };\n\n consoleHandle = setupConsole(context, consoleOptions);\n }\n\n // Setup encoding if enabled\n if (options.encoding) {\n const encodingOptions: SetupEncodingOptions =\n options.encoding === true\n ? { stateMap, coreHandle: core }\n : { ...options.encoding, stateMap, coreHandle: core };\n\n encoding = setupEncoding(context, encodingOptions);\n }\n\n // Setup timers if enabled\n if (options.timers) {\n const timersOptions: SetupTimersOptions =\n options.timers === true\n ? { stateMap, coreHandle: core }\n : { ...options.timers, stateMap, coreHandle: core };\n\n timers = setupTimers(context, timersOptions);\n }\n\n return {\n core,\n fetch,\n fs,\n console: consoleHandle,\n encoding,\n timers,\n stateMap,\n dispose() {\n // Dispose in reverse order of setup\n timers?.dispose();\n encoding?.dispose();\n consoleHandle?.dispose();\n fs?.dispose();\n fetch?.dispose();\n core.dispose();\n },\n };\n}\n"
5
+ "// Re-export everything from sub-packages\nexport * from \"@ricsam/quickjs-core\";\nexport * from \"@ricsam/quickjs-console\";\nexport * from \"@ricsam/quickjs-encoding\";\nexport * from \"@ricsam/quickjs-fetch\";\nexport * from \"@ricsam/quickjs-fs\";\nexport * from \"@ricsam/quickjs-timers\";\n\nimport type { QuickJSContext } from \"quickjs-emscripten\";\nimport {\n setupCore,\n createStateMap,\n cleanupUnmarshaledHandles,\n clearAllInstanceState,\n type StateMap,\n type CoreHandle,\n} from \"@ricsam/quickjs-core\";\nimport {\n setupFetch,\n type SetupFetchOptions,\n type FetchHandle,\n} from \"@ricsam/quickjs-fetch\";\nimport {\n setupFs,\n type SetupFsOptions,\n type FsHandle,\n} from \"@ricsam/quickjs-fs\";\nimport {\n setupConsole,\n type SetupConsoleOptions,\n type ConsoleHandle,\n} from \"@ricsam/quickjs-console\";\nimport {\n setupEncoding,\n type SetupEncodingOptions,\n type EncodingHandle,\n} from \"@ricsam/quickjs-encoding\";\nimport {\n setupTimers,\n type SetupTimersOptions,\n type TimersHandle,\n} from \"@ricsam/quickjs-timers\";\n\n/**\n * Options for setting up the runtime\n */\nexport interface SetupRuntimeOptions {\n /**\n * Fetch API options\n * Pass `true` to enable with defaults\n * Pass `false` or omit to disable\n */\n fetch?: SetupFetchOptions | boolean;\n\n /**\n * File system options\n * Pass options object to enable\n * Pass `false` or omit to disable\n */\n fs?: SetupFsOptions | false;\n\n /**\n * Console options\n * Pass `true` to enable with defaults\n * Pass handlers object to customize\n * Pass `false` or omit to disable\n */\n console?: SetupConsoleOptions | boolean;\n\n /**\n * Encoding options (atob/btoa)\n * Pass `true` to enable with defaults\n * Pass `false` or omit to disable\n */\n encoding?: SetupEncodingOptions | boolean;\n\n /**\n * Timer options (setTimeout, setInterval, clearTimeout, clearInterval)\n * Pass `true` to enable with defaults\n * Pass `false` or omit to disable\n */\n timers?: SetupTimersOptions | boolean;\n}\n\n/**\n * Handle returned from setupRuntime\n */\nexport interface RuntimeHandle {\n /** Core handle (always present) */\n core: CoreHandle;\n\n /** Fetch handle (if enabled) */\n fetch?: FetchHandle;\n\n /** File system handle (if enabled) */\n fs?: FsHandle;\n\n /** Console handle (if enabled) */\n console?: ConsoleHandle;\n\n /** Encoding handle (if enabled) */\n encoding?: EncodingHandle;\n\n /** Timers handle (if enabled) */\n timers?: TimersHandle;\n\n /** Shared state map */\n readonly stateMap: StateMap;\n\n /** Get the underlying QuickJS context */\n readonly context: QuickJSContext;\n\n /**\n * Drain all pending jobs with timeout.\n * Call BEFORE dispose() to prevent GC assertion failures.\n * @param maxIterations - Maximum iterations (default: 1000)\n * @returns true if all jobs completed, false if timed out\n */\n drainPendingJobs(maxIterations?: number): boolean;\n\n /** Dispose all handles and cleanup */\n dispose(): void;\n}\n\n/**\n * Setup multiple APIs in a QuickJS context\n *\n * @example\n * const handle = setupRuntime(context, {\n * fetch: {\n * onFetch: async (req) => fetch(req),\n * },\n * fs: {\n * getDirectory: async (path) => createNodeDirectoryHandle(`/sandbox${path}`),\n * },\n * });\n *\n * // Use the context...\n *\n * handle.dispose();\n */\nexport function setupRuntime(\n context: QuickJSContext,\n options: SetupRuntimeOptions = {}\n): RuntimeHandle {\n const stateMap = createStateMap();\n\n // Always setup core\n const core = setupCore(context, { stateMap });\n\n let fetch: FetchHandle | undefined;\n let fs: FsHandle | undefined;\n let consoleHandle: ConsoleHandle | undefined;\n let encoding: EncodingHandle | undefined;\n let timers: TimersHandle | undefined;\n\n // Setup fetch if enabled\n if (options.fetch) {\n const fetchOptions: SetupFetchOptions =\n options.fetch === true\n ? { stateMap, coreHandle: core }\n : { ...options.fetch, stateMap, coreHandle: core };\n\n fetch = setupFetch(context, fetchOptions);\n }\n\n // Setup fs if provided\n if (options.fs) {\n fs = setupFs(context, {\n ...options.fs,\n stateMap,\n coreHandle: core,\n });\n }\n\n // Setup console if enabled\n if (options.console) {\n const consoleOptions: SetupConsoleOptions =\n options.console === true\n ? { stateMap, coreHandle: core }\n : { ...options.console, stateMap, coreHandle: core };\n\n consoleHandle = setupConsole(context, consoleOptions);\n }\n\n // Setup encoding if enabled\n if (options.encoding) {\n const encodingOptions: SetupEncodingOptions =\n options.encoding === true\n ? { stateMap, coreHandle: core }\n : { ...options.encoding, stateMap, coreHandle: core };\n\n encoding = setupEncoding(context, encodingOptions);\n }\n\n // Setup timers if enabled\n if (options.timers) {\n const timersOptions: SetupTimersOptions =\n options.timers === true\n ? { stateMap, coreHandle: core }\n : { ...options.timers, stateMap, coreHandle: core };\n\n timers = setupTimers(context, timersOptions);\n }\n\n return {\n core,\n fetch,\n fs,\n console: consoleHandle,\n encoding,\n timers,\n stateMap,\n get context() {\n return context;\n },\n drainPendingJobs(maxIterations = 1000): boolean {\n for (let i = 0; i < maxIterations; i++) {\n if (!context.runtime.hasPendingJob()) {\n return true;\n }\n const result = context.runtime.executePendingJobs();\n if (result.error) {\n result.error.dispose();\n }\n }\n return !context.runtime.hasPendingJob();\n },\n dispose() {\n // Drain pending jobs before disposal\n for (let i = 0; i < 1000; i++) {\n if (!context.runtime.hasPendingJob()) break;\n const result = context.runtime.executePendingJobs();\n if (result.error) result.error.dispose();\n }\n\n // Dispose sub-handles in reverse order\n timers?.dispose();\n encoding?.dispose();\n consoleHandle?.dispose();\n fs?.dispose();\n fetch?.dispose();\n\n // Drain pending jobs again after sub-handle disposal\n // (fetch?.dispose() calls evalCode which can create new jobs)\n for (let i = 0; i < 1000; i++) {\n if (!context.runtime.hasPendingJob()) break;\n const result = context.runtime.executePendingJobs();\n if (result.error) result.error.dispose();\n }\n\n // Clean up tracked handles\n cleanupUnmarshaledHandles(context);\n clearAllInstanceState();\n\n // Clear globals to help GC break circular references\n try {\n const clearGlobals = context.evalCode(`\n (function() {\n const keysToDelete = Object.keys(globalThis).filter(k =>\n k !== 'globalThis' && k !== 'undefined' && k !== 'NaN' && k !== 'Infinity'\n );\n for (const key of keysToDelete) {\n try { globalThis[key] = undefined; } catch (e) {}\n }\n for (const key of keysToDelete) {\n try { delete globalThis[key]; } catch (e) {}\n }\n return keysToDelete.length;\n })()\n `);\n if (clearGlobals.error) {\n clearGlobals.error.dispose();\n } else {\n clearGlobals.value.dispose();\n }\n } catch (e) {\n // Ignore errors during cleanup\n }\n\n // Final drain\n for (let i = 0; i < 100; i++) {\n if (!context.runtime.hasPendingJob()) break;\n const result = context.runtime.executePendingJobs();\n if (result.error) result.error.dispose();\n }\n\n core.dispose();\n },\n };\n}\n"
6
6
  ],
7
- "mappings": ";;AACA;AACA;AACA;AACA;AACA;AACA;AAGA;AAAA;AAAA;AAAA;AAMA;AAAA;AAAA;AAKA;AAAA;AAAA;AAKA;AAAA;AAAA;AAKA;AAAA;AAAA;AAKA;AAAA;AAAA;AA6FO,SAAS,YAAY,CAC1B,SACA,UAA+B,CAAC,GACjB;AAAA,EACf,MAAM,WAAW,eAAe;AAAA,EAGhC,MAAM,OAAO,UAAU,SAAS,EAAE,SAAS,CAAC;AAAA,EAE5C,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EAGJ,IAAI,QAAQ,OAAO;AAAA,IACjB,MAAM,eACJ,QAAQ,UAAU,OACd,EAAE,UAAU,YAAY,KAAK,IAC7B,KAAK,QAAQ,OAAO,UAAU,YAAY,KAAK;AAAA,IAErD,QAAQ,WAAW,SAAS,YAAY;AAAA,EAC1C;AAAA,EAGA,IAAI,QAAQ,IAAI;AAAA,IACd,KAAK,QAAQ,SAAS;AAAA,SACjB,QAAQ;AAAA,MAC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KZ,SAAS,IAAI,EAAG,IAAI,KAAK,KAAK;AAAA,QAC5B,IAAI,CAAC,QAAQ,QAAQ,cAAc;AAAA,UAAG;AAAA,QACtC,MAAM,SAAS,QAAQ,QAAQ,mBAAmB;AAAA,QAClD,IAAI,OAAO;AAAA,UAAO,OAAO,MAAM,QAAQ;AAAA,MACzC;AAAA,MAEA,KAAK,QAAQ;AAAA;AAAA,EAEjB;AAAA;",
8
+ "debugId": "91E27187E6392B3064756E2164756E21",
9
9
  "names": []
10
10
  }
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/quickjs-runtime",
3
- "version": "0.2.12",
3
+ "version": "0.2.14",
4
4
  "type": "module"
5
5
  }
@@ -65,6 +65,15 @@ export interface RuntimeHandle {
65
65
  timers?: TimersHandle;
66
66
  /** Shared state map */
67
67
  readonly stateMap: StateMap;
68
+ /** Get the underlying QuickJS context */
69
+ readonly context: QuickJSContext;
70
+ /**
71
+ * Drain all pending jobs with timeout.
72
+ * Call BEFORE dispose() to prevent GC assertion failures.
73
+ * @param maxIterations - Maximum iterations (default: 1000)
74
+ * @returns true if all jobs completed, false if timed out
75
+ */
76
+ drainPendingJobs(maxIterations?: number): boolean;
68
77
  /** Dispose all handles and cleanup */
69
78
  dispose(): void;
70
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ricsam/quickjs-runtime",
3
- "version": "0.2.12",
3
+ "version": "0.2.14",
4
4
  "main": "./dist/cjs/index.cjs",
5
5
  "types": "./dist/types/index.d.ts",
6
6
  "exports": {