Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 16 additions & 60 deletions src/jsc/bindings/BunProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1801,19 +1801,13 @@ static JSValue constructLoadEnvFile(VM& vm, JSObject* processObject)
return JSC::JSFunction::create(vm, globalObject, processObjectInternalsLoadEnvFileCodeGenerator(vm), globalObject);
}

// Lazy PropertyCallback builders that enter JS. reifyAllStaticProperties wraps these in
// DeferTerminationForAWhile; a non-termination throw is cleared+reported so the worker's
// reifyAllStaticProperties (node:worker_threads preload) doesn't leave a pending exception.
// TopExceptionScope, not ThrowScope: JSC checks a failed builder with vm.exceptionForInspection().
static JSValue callLazyProcessBuilder(VM& vm, JSC::JSGlobalObject* globalObject, JSC::FunctionExecutable* (*generator)(VM&), const JSC::ArgList& args)
{
auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm);
auto* function = JSC::JSFunction::create(vm, globalObject, generator(vm), globalObject);
auto result = JSC::profiledCall(globalObject, ProfilingReason::API, function, JSC::getCallData(function), globalObject->globalThis(), args);
if (auto* exception = scope.exception()) [[unlikely]] {
(void)scope.tryClearException();
Zig::GlobalObject::reportUncaughtExceptionAtEventLoop(globalObject, exception);
return jsUndefined();
}
RETURN_IF_EXCEPTION(scope, {});
return result;
}

Expand Down Expand Up @@ -2773,17 +2767,11 @@ static JSValue constructProcessConfigObject(VM& vm, JSObject* processObject)
// v8_use_snapshot: 1
// }
// }
// Lazy property builder: exceptions must not propagate into
// reifyStaticProperty, which performs no exception check.
auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm);
JSC::JSObject* config = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype(), 2);
JSC::JSObject* variables = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype(), 2);
JSC::JSArray* shareableBuiltins = JSC::constructEmptyArray(globalObject, nullptr);
if (auto* exception = scope.exception()) [[unlikely]] {
(void)scope.tryClearException();
Zig::GlobalObject::reportUncaughtExceptionAtEventLoop(globalObject, exception);
return JSC::jsUndefined();
}
RETURN_IF_EXCEPTION(scope, {});
variables->putDirect(vm, JSC::Identifier::fromString(vm, "v8_enable_i18n_support"_s), JSC::jsNumber(1), 0);
variables->putDirect(vm, JSC::Identifier::fromString(vm, "enable_lto"_s), JSC::jsBoolean(false), 0);
// Node 26's common.gypi evaluates enable_thin_lto/lto_jobs conditions; gyp
Expand Down Expand Up @@ -2920,11 +2908,7 @@ static JSValue constructStdioWriteStream(JSC::JSGlobalObject* globalObject, JSC:
JSC::CallData callData = JSC::getCallData(getStdioWriteStream);

auto result = JSC::profiledCall(globalObject, ProfilingReason::API, getStdioWriteStream, callData, globalObject->globalThis(), args);
if (auto* exception = scope.exception()) {
(void)scope.tryClearException();
Zig::GlobalObject::reportUncaughtExceptionAtEventLoop(globalObject, exception);
return jsUndefined();
}
RETURN_IF_EXCEPTION(scope, {});

ASSERT_WITH_MESSAGE(JSC::isJSArray(result), "Expected an array from getStdioWriteStream");
JSC::JSArray* resultObject = uncheckedDowncast<JSC::JSArray>(result);
Expand All @@ -2947,10 +2931,14 @@ static JSValue constructStdioWriteStream(JSC::JSGlobalObject* globalObject, JSC:
forceSync = true;
#endif
if (forceSync) {
Bun__ForceFileSinkToBeSynchronousForProcessObjectStdio(globalObject, JSValue::encode(resultObject->getIndex(globalObject, 1)));
JSValue sink = resultObject->getIndex(globalObject, 1);
RETURN_IF_EXCEPTION(scope, {});
Bun__ForceFileSinkToBeSynchronousForProcessObjectStdio(globalObject, JSValue::encode(sink));
}

return resultObject->getIndex(globalObject, 0);
JSValue stream = resultObject->getIndex(globalObject, 0);
RETURN_IF_EXCEPTION(scope, {});
return stream;
}

static JSValue constructStdout(VM& vm, JSObject* processObject)
Expand Down Expand Up @@ -2981,11 +2969,7 @@ static JSValue constructStdin(VM& vm, JSObject* processObject)
JSC::CallData callData = JSC::getCallData(getStdinStream);

auto result = JSC::profiledCall(globalObject, ProfilingReason::API, getStdinStream, callData, globalObject, args);
if (auto* exception = scope.exception()) {
(void)scope.tryClearException();
Zig::GlobalObject::reportUncaughtExceptionAtEventLoop(globalObject, exception);
return jsUndefined();
}
RETURN_IF_EXCEPTION(scope, {});
return result;
}

Expand Down Expand Up @@ -3218,15 +3202,9 @@ static JSValue constructRevision(VM& vm, JSObject* processObject)
static JSValue constructEnv(VM& vm, JSObject* processObject)
{
auto* globalObject = uncheckedDowncast<Zig::GlobalObject>(processObject->globalObject());
// Lazy property builder: exceptions must not propagate into
// reifyStaticProperty, which performs no exception check.
auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm);
JSValue env = globalObject->processEnvObject();
if (auto* exception = scope.exception()) [[unlikely]] {
(void)scope.tryClearException();
Zig::GlobalObject::reportUncaughtExceptionAtEventLoop(globalObject, exception);
return JSC::jsUndefined();
}
RETURN_IF_EXCEPTION(scope, {});
return env;
}

Expand Down Expand Up @@ -4272,15 +4250,9 @@ JSC_DEFINE_HOST_FUNCTION(Process_stubFunctionReturningArray, (JSGlobalObject * g

static JSValue Process_stubEmptyArray(VM& vm, JSObject* processObject)
{
// Lazy property builder: exceptions must not propagate into
// reifyStaticProperty, which performs no exception check.
auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm);
JSC::JSArray* array = JSC::constructEmptyArray(processObject->globalObject(), nullptr);
if (auto* exception = scope.exception()) [[unlikely]] {
(void)scope.tryClearException();
Zig::GlobalObject::reportUncaughtExceptionAtEventLoop(processObject->globalObject(), exception);
return JSC::jsUndefined();
}
RETURN_IF_EXCEPTION(scope, {});
return array;
}

Expand Down Expand Up @@ -4407,25 +4379,15 @@ extern "C" void Bun__Process__queueNextTick2(GlobalObject* globalObject, Encoded
// return require.cache.get(Bun.main)
static JSValue constructMainModuleProperty(VM& vm, JSObject* processObject)
{
// Lazy property builder: exceptions must not propagate into
// reifyStaticProperty, which performs no exception check.
auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm);
auto* globalObject = defaultGlobalObject(processObject->globalObject());
auto* bun = globalObject->bunObject();
auto& builtinNames = Bun::builtinNames(vm);
JSValue mainValue = bun->get(globalObject, builtinNames.mainPublicName());
if (auto* exception = scope.exception()) [[unlikely]] {
(void)scope.tryClearException();
Zig::GlobalObject::reportUncaughtExceptionAtEventLoop(globalObject, exception);
return JSC::jsUndefined();
}
RETURN_IF_EXCEPTION(scope, {});
auto* requireMap = globalObject->requireMap();
JSValue mainModule = requireMap->get(globalObject, mainValue);
if (auto* exception = scope.exception()) [[unlikely]] {
(void)scope.tryClearException();
Zig::GlobalObject::reportUncaughtExceptionAtEventLoop(globalObject, exception);
return JSC::jsUndefined();
}
RETURN_IF_EXCEPTION(scope, {});
return mainModule;
}

Expand All @@ -4447,15 +4409,9 @@ JSValue Process::constructNextTickFn(JSC::VM& vm, Zig::GlobalObject* globalObjec
args.append(JSC::JSFunction::create(vm, globalObject, 1, String(), jsFunctionDrainMicrotaskQueue, ImplementationVisibility::Private));
args.append(JSC::JSFunction::create(vm, globalObject, 1, String(), jsFunctionReportUncaughtException, ImplementationVisibility::Private));

// Lazy property builder: exceptions must not propagate into
// reifyStaticProperty, which performs no exception check.
auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm);
JSValue nextTickFunction = JSC::profiledCall(globalObject, ProfilingReason::API, initializer, JSC::getCallData(initializer), globalObject->globalThis(), args);
if (auto* exception = scope.exception()) [[unlikely]] {
(void)scope.tryClearException();
Zig::GlobalObject::reportUncaughtExceptionAtEventLoop(globalObject, exception);
return JSC::jsUndefined();
}
RETURN_IF_EXCEPTION(scope, {});
if (nextTickFunction && nextTickFunction.isObject()) {
this->m_nextTickFunction.set(vm, this, nextTickFunction.getObject());
}
Expand Down
8 changes: 5 additions & 3 deletions src/jsc/bindings/ZigGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2930,12 +2930,13 @@ JSC_DEFINE_CUSTOM_GETTER(getConsoleConstructor, (JSGlobalObject * globalObject,
JSC_DEFINE_CUSTOM_GETTER(getConsoleStdout, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName property))
{
auto& vm = JSC::getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
auto console = JSValue::decode(thisValue).getObject();
auto global = uncheckedDowncast<Zig::GlobalObject>(globalObject);

// instead of calling the constructor builtin, go through the process.stdout getter to ensure it's only created once.
auto stdoutValue = global->processObject()->get(globalObject, Identifier::fromString(vm, "stdout"_s));
if (!stdoutValue) return {};
RETURN_IF_EXCEPTION(scope, {});

console->putDirect(vm, property, stdoutValue, PropertyAttribute::DontEnum | 0);
return JSValue::encode(stdoutValue);
Expand All @@ -2945,12 +2946,13 @@ JSC_DEFINE_CUSTOM_GETTER(getConsoleStdout, (JSGlobalObject * globalObject, Encod
JSC_DEFINE_CUSTOM_GETTER(getConsoleStderr, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName property))
{
auto& vm = JSC::getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
auto console = JSValue::decode(thisValue).getObject();
auto global = uncheckedDowncast<Zig::GlobalObject>(globalObject);

// instead of calling the constructor builtin, go through the process.stdout getter to ensure it's only created once.
// instead of calling the constructor builtin, go through the process.stderr getter to ensure it's only created once.
auto stderrValue = global->processObject()->get(globalObject, Identifier::fromString(vm, "stderr"_s));
if (!stderrValue) return {};
RETURN_IF_EXCEPTION(scope, {});

console->putDirect(vm, property, stderrValue, PropertyAttribute::DontEnum | 0);
return JSValue::encode(stderrValue);
Expand Down
10 changes: 5 additions & 5 deletions test/js/node/process/process-stdio-stack-overflow-fixture.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading