From 664119841a92d13a297e88032f9985fe7e35f77c Mon Sep 17 00:00:00 2001 From: dave caruso Date: Mon, 21 Aug 2023 16:26:07 -0700 Subject: Implement `napi_ref_threadsafe_function` (#4156) * Implement napi_ref_threadsafe_function * work on this * i hate event loops * little better * clean --- src/bun.js/bindings/napi.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/bun.js/bindings/napi.cpp') diff --git a/src/bun.js/bindings/napi.cpp b/src/bun.js/bindings/napi.cpp index 8a77723e2..2562242a8 100644 --- a/src/bun.js/bindings/napi.cpp +++ b/src/bun.js/bindings/napi.cpp @@ -46,6 +46,8 @@ #include "JavaScriptCore/JSSourceCode.h" #include "JavaScriptCore/JSNativeStdFunction.h" #include "JavaScriptCore/BigIntObject.h" +#include "ScriptExecutionContext.h" +#include "Strong.h" #include "../modules/ObjectModule.h" @@ -1763,7 +1765,16 @@ extern "C" napi_status napi_create_external(napi_env env, void* data, auto* structure = Bun::NapiExternal::createStructure(vm, globalObject, globalObject->objectPrototype()); JSValue value = JSValue(Bun::NapiExternal::create(vm, structure, data, finalize_hint, finalize_cb)); - JSC::EnsureStillAliveScope ensureStillAlive(value); + + // With `fsevents`, their napi_create_external seems to get immediatly garbage + // collected for some unknown reason. + // See https://github.com/oven-sh/bun/issues/3978 and `fsevents.test.ts` + JSC::Strong* strong = new JSC::Strong(vm, value); + globalObject->scriptExecutionContext()->postTask([strong](auto& context) -> void { + strong->clear(); + delete strong; + }); + *result = toNapi(value); return napi_ok; } -- cgit v1.2.3