From 1924c27f1d4058076d2ab802af2dd0ed2a3dbf3e Mon Sep 17 00:00:00 2001 From: Dylan Conway Date: Thu, 24 Apr 2025 16:41:58 -0700 Subject: [PATCH] add process.ref and process.unref --- src/bun.js/bindings/BunProcess.cpp | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/bun.js/bindings/BunProcess.cpp b/src/bun.js/bindings/BunProcess.cpp index 6d53f32f07..a6b7fb5901 100644 --- a/src/bun.js/bindings/BunProcess.cpp +++ b/src/bun.js/bindings/BunProcess.cpp @@ -3073,6 +3073,60 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionOpenStdin, (JSGlobalObject * globalObje RELEASE_AND_RETURN(throwScope, JSValue::encode(jsUndefined())); } +JSC_DEFINE_HOST_FUNCTION(Process_ref, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + auto& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + JSValue maybeRefable = callFrame->argument(0); + + JSValue ref = maybeRefable.get(globalObject, Identifier::fromUid(vm.symbolRegistry().symbolForKey("nodejs.ref"_s))); + RETURN_IF_EXCEPTION(scope, {}); + + auto refBoolean = ref.toBoolean(globalObject); + RETURN_IF_EXCEPTION(scope, {}); + + if (!refBoolean) { + ref = maybeRefable.get(globalObject, Identifier::fromString(vm, "ref"_s)); + RETURN_IF_EXCEPTION(scope, {}); + } + + if (ref.isCallable()) { + CallData callData = getCallData(ref); + JSC::profiledCall(globalObject, ProfilingReason::API, ref, callData, maybeRefable, {}); + RETURN_IF_EXCEPTION(scope, {}); + } + + return JSValue::encode(jsUndefined()); +} + +JSC_DEFINE_HOST_FUNCTION(Process_unref, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + auto& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + JSValue maybeUnrefable = callFrame->argument(0); + + JSValue unref = maybeUnrefable.get(globalObject, Identifier::fromUid(vm.symbolRegistry().symbolForKey("nodejs.unref"_s))); + RETURN_IF_EXCEPTION(scope, {}); + + auto unrefBoolean = unref.toBoolean(globalObject); + RETURN_IF_EXCEPTION(scope, {}); + + if (!unrefBoolean) { + unref = maybeUnrefable.get(globalObject, Identifier::fromString(vm, "unref"_s)); + RETURN_IF_EXCEPTION(scope, {}); + } + + if (unref.isCallable()) { + CallData callData = getCallData(unref); + JSC::profiledCall(globalObject, ProfilingReason::API, unref, callData, maybeUnrefable, {}); + RETURN_IF_EXCEPTION(scope, {}); + } + + return JSValue::encode(jsUndefined()); +} + JSC_DEFINE_HOST_FUNCTION(Process_stubEmptyFunction, (JSGlobalObject * globalObject, CallFrame* callFrame)) { return JSValue::encode(jsUndefined()); @@ -3550,6 +3604,7 @@ extern "C" void Process__emitErrorEvent(Zig::GlobalObject* global, EncodedJSValu platform constructPlatform PropertyCallback ppid constructPpid PropertyCallback reallyExit Process_functionReallyExit Function 1 + ref Process_ref Function 1 release constructProcessReleaseObject PropertyCallback report constructProcessReportObject PropertyCallback revision constructRevision PropertyCallback @@ -3562,6 +3617,7 @@ extern "C" void Process__emitErrorEvent(Zig::GlobalObject* global, EncodedJSValu throwDeprecation processThrowDeprecation CustomAccessor title processTitle CustomAccessor umask Process_functionUmask Function 1 + unref Process_unref Function 1 uptime Process_functionUptime Function 1 version constructVersion PropertyCallback versions constructVersions PropertyCallback