From 4ce3b44d40d03360dc36c23663c15a520b255f98 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Thu, 25 Sep 2025 18:07:03 -0700 Subject: [PATCH 1/2] support eager memory deallocation on hibernation --- worker-build/src/js/shim.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/worker-build/src/js/shim.js b/worker-build/src/js/shim.js index c6e65f0d6..e5fa0bcf4 100644 --- a/worker-build/src/js/shim.js +++ b/worker-build/src/js/shim.js @@ -15,14 +15,14 @@ function registerPanicHook() { registerPanicHook(); -let instanceId = 0; +let reinitId = 0; function checkReinitialize() { if (panicError) { console.log("Reinitializing Wasm application"); exports.__wbg_reset_state(); panicError = null; registerPanicHook(); - instanceId++; + reinitId++; } } @@ -30,20 +30,24 @@ export default class Entrypoint extends WorkerEntrypoint { $HANDLERS } +const instances = new Map(); const classProxyHooks = { construct(ctor, args, newTarget) { - const instance = { - instance: Reflect.construct(ctor, args, newTarget), - instanceId, + instances.get(ctor)?.free(); + const instance = Reflect.construct(ctor, args, newTarget); + instances.set(ctor, instance); + const target = { + instance, + reinitId, ctor, args, newTarget }; - return new Proxy(instance, { + return new Proxy(target, { get(target, prop, receiver) { - if (target.instanceId !== instanceId) { - target.instance = Reflect.construct(target.ctor, target.args, target.newTarget); - target.instanceId = instanceId; + if (target.reinitId !== reinitId) { + instances.set(target.ctor, target.instance = Reflect.construct(target.ctor, target.args, target.newTarget)); + target.reinitId = reinitId; } return Reflect.get(target.instance, prop, receiver); } From 9eb88e85e1e1a6632d75b635ed500291b5dd7e73 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Thu, 25 Sep 2025 18:12:07 -0700 Subject: [PATCH 2/2] simplify --- worker-build/src/js/shim.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/worker-build/src/js/shim.js b/worker-build/src/js/shim.js index e5fa0bcf4..21571ec6a 100644 --- a/worker-build/src/js/shim.js +++ b/worker-build/src/js/shim.js @@ -36,14 +36,13 @@ const classProxyHooks = { instances.get(ctor)?.free(); const instance = Reflect.construct(ctor, args, newTarget); instances.set(ctor, instance); - const target = { + return new Proxy({ instance, reinitId, ctor, args, newTarget - }; - return new Proxy(target, { + }, { get(target, prop, receiver) { if (target.reinitId !== reinitId) { instances.set(target.ctor, target.instance = Reflect.construct(target.ctor, target.args, target.newTarget));