diff --git a/src/provideEnv.ts b/src/provideEnv.ts index ba68982..7ca1b55 100644 --- a/src/provideEnv.ts +++ b/src/provideEnv.ts @@ -14,6 +14,18 @@ export const provideEnv = (arena: Arena, options: RuntimeOptions) => { dangerousSync = arena.sync(options.dangerousSync) } + const dispose = () => { + for (const [_key, value] of timeouts) { + clearTimeout(value) + } + timeouts.clear() + + for (const [_key, value] of intervals) { + clearInterval(value) + } + intervals.clear() + } + arena.expose({ __dangerousSync: dangerousSync, env: options.env ?? {}, @@ -30,6 +42,7 @@ export const provideEnv = (arena: Arena, options: RuntimeOptions) => { const timeout = timeouts.get(id) if (timeout) { clearTimeout(timeout) + timeouts.delete(id) } }, setInterval: (fn: () => void, time: number) => { @@ -43,7 +56,10 @@ export const provideEnv = (arena: Arena, options: RuntimeOptions) => { const interval = intervals.get(id) if (interval) { clearInterval(interval) + intervals.delete(id) } }, }) + + return { dispose } } diff --git a/src/quickJS.ts b/src/quickJS.ts index 2e800cb..d252e99 100644 --- a/src/quickJS.ts +++ b/src/quickJS.ts @@ -53,7 +53,7 @@ export const quickJS = async (wasmVariantName = '@jitl/quickjs-ng-wasmfile-relea provideFs(arena, runtimeOptions, fs) provideConsole(arena, runtimeOptions) - provideEnv(arena, runtimeOptions) + const { dispose: disposeEnvironment } = provideEnv(arena, runtimeOptions) provideHttp(arena, runtimeOptions, { fs: runtimeOptions.allowFs ? fs : undefined }) await arena.evalCode(` @@ -64,6 +64,12 @@ export const quickJS = async (wasmVariantName = '@jitl/quickjs-ng-wasmfile-relea const dispose = () => { let err: unknown + try { + disposeEnvironment() + } catch (error) { + err = error + console.error('Failed to dispose environment') + } try { arena.dispose() } catch (error) {