Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions bench/snippets/process-env.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { bench, run } from "../runner.mjs";

// What a program does before the code that is measured here runs:
// a copy for a child process, which reads every variable,
globalThis.childEnv = { ...process.env };
// a feature flag that is checked often,
for (let n = 0; n < 20_000; n++) globalThis.flag = process.env.NOT_SET_FLAG;
// and dotenv-style writes.
for (const key of ["BENCH_A", "BENCH_B", "BENCH_C"]) process.env[key] = "1";
delete process.env.BENCH_C;

const keys = ["HOME", "PATH", "BENCH_A", "NOT_SET_1", "NOT_SET_2", "USER", "NOT_SET_3", "BENCH_B"];
let i = 0;

bench("process.env.HOME", () => process.env.HOME);
bench("process.env.NOT_SET", () => process.env.NOT_SET);
bench("process.env[key]", () => process.env[keys[i++ & 7]]);
bench("process.env.BENCH_A = 'value'", () => {
process.env.BENCH_A = "value";
});
bench("{ ...process.env }", () => ({ ...process.env }));
bench("Object.keys(process.env)", () => Object.keys(process.env));
bench("process.argv", () => process.argv);
bench("process.argv[1]", () => process.argv[1]);
bench("process.argv.length", () => process.argv.length);
bench("process.execArgv", () => process.execArgv);

await run();
103 changes: 19 additions & 84 deletions src/jsc/bindings/BunProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3170,105 +3170,42 @@ static JSValue constructExecPath(VM& vm, JSObject* processObject)
return JSValue::decode(Bun__Process__getExecPath(globalObject));
}

extern "C" EncodedJSValue Bun__Process__getArgv(JSGlobalObject* lexicalGlobalObject)
static JSValue constructArgv(VM& vm, JSObject* processObject)
{
auto* globalObject = defaultGlobalObject(lexicalGlobalObject);
auto* process = globalObject->processObject();
if (!process) {
return JSValue::encode(jsUndefined());
}

return JSValue::encode(process->getArgv(globalObject));
return JSValue::decode(Bun__Process__createArgv(processObject->globalObject()));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

// get from js
JSC_DEFINE_CUSTOM_GETTER(processArgv, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName))
static JSValue constructExecArgv(VM& vm, JSObject* processObject)
{
Process* process = getProcessObject(globalObject, JSValue::decode(thisValue));
if (!process) {
return JSValue::encode(jsUndefined());
}

return JSValue::encode(process->getArgv(globalObject));
return JSValue::decode(Bun__Process__createExecArgv(processObject->globalObject()));
}

JSValue Process::getArgv(JSGlobalObject* globalObject)
{
if (auto argv = m_argv.get()) {
return argv;
}

JSValue argv = JSValue::decode(Bun__Process__createArgv(globalObject));
setArgv(globalObject, argv);
return argv;
return get(globalObject, Identifier::fromString(globalObject->vm(), "argv"_s));
}

void Process::setArgv(JSGlobalObject* globalObject, JSValue value)
JSValue Process::getExecArgv(JSGlobalObject* globalObject)
{
auto& vm = globalObject->vm();
m_argv.set(vm, this, value);
return get(globalObject, Identifier::fromString(globalObject->vm(), "execArgv"_s));
}

JSC_DEFINE_CUSTOM_SETTER(setProcessArgv, (JSGlobalObject * globalObject, EncodedJSValue thisValue, EncodedJSValue encodedValue, PropertyName))
extern "C" EncodedJSValue Bun__Process__getArgv(JSGlobalObject* lexicalGlobalObject)
{
Process* process = getProcessObject(globalObject, JSValue::decode(thisValue));
if (!process) {
return true;
}

JSValue value = JSValue::decode(encodedValue);
process->setArgv(globalObject, value);
return true;
auto* globalObject = defaultGlobalObject(lexicalGlobalObject);
auto scope = DECLARE_THROW_SCOPE(globalObject->vm());
JSValue argv = globalObject->processObject()->getArgv(globalObject);
Comment thread
robobun marked this conversation as resolved.
Resolved
RETURN_IF_EXCEPTION(scope, {});
return JSValue::encode(argv);
}

extern "C" EncodedJSValue Bun__Process__getExecArgv(JSGlobalObject* lexicalGlobalObject)
{
auto* globalObject = defaultGlobalObject(lexicalGlobalObject);
auto* process = globalObject->processObject();
if (!process) {
return JSValue::encode(jsUndefined());
}

return JSValue::encode(process->getExecArgv(globalObject));
}

JSC_DEFINE_CUSTOM_GETTER(processExecArgv, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName))
{
Process* process = getProcessObject(globalObject, JSValue::decode(thisValue));
if (!process) {
return JSValue::encode(jsUndefined());
}

return JSValue::encode(process->getExecArgv(globalObject));
}

JSValue Process::getExecArgv(JSGlobalObject* globalObject)
{
if (auto argv = m_execArgv.get()) {
return argv;
}

JSValue argv = JSValue::decode(Bun__Process__createExecArgv(globalObject));
setExecArgv(globalObject, argv);
return argv;
}

void Process::setExecArgv(JSGlobalObject* globalObject, JSValue value)
{
auto& vm = globalObject->vm();
m_execArgv.set(vm, this, value);
}

JSC_DEFINE_CUSTOM_SETTER(setProcessExecArgv, (JSGlobalObject * globalObject, EncodedJSValue thisValue, EncodedJSValue encodedValue, PropertyName))
{
Process* process = getProcessObject(globalObject, JSValue::decode(thisValue));
if (!process) {
return true;
}

JSValue value = JSValue::decode(encodedValue);
process->setExecArgv(globalObject, value);
return true;
auto scope = DECLARE_THROW_SCOPE(globalObject->vm());
JSValue execArgv = globalObject->processObject()->getExecArgv(globalObject);
RETURN_IF_EXCEPTION(scope, {});
return JSValue::encode(execArgv);
}

JSC_DEFINE_CUSTOM_GETTER(processGetEval, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName))
Expand Down Expand Up @@ -3776,8 +3713,6 @@ void Process::visitChildrenImpl(JSCell* cell, Visitor& visitor)
visitor.append(thisObject->m_uncaughtExceptionCaptureCallback);
visitor.append(thisObject->m_nextTickFunction);
visitor.append(thisObject->m_cachedCwd);
visitor.append(thisObject->m_argv);
visitor.append(thisObject->m_execArgv);
visitor.append(thisObject->m_onWarning);

thisObject->m_cpuUsageStructure.visit(visitor);
Expand Down Expand Up @@ -5026,7 +4961,7 @@ extern "C" void Process__emitErrorEvent(Zig::GlobalObject* global, EncodedJSValu
loadEnvFile constructLoadEnvFile PropertyCallback
finalization constructFinalization PropertyCallback
arch constructArch PropertyCallback
argv processArgv CustomAccessor
argv constructArgv PropertyCallback
Comment thread
robobun marked this conversation as resolved.
Resolved
argv0 constructArgv0 PropertyCallback
assert Process_functionAssert Function 1
availableMemory Process_availableMemory Function 0
Expand All @@ -5045,7 +4980,7 @@ extern "C" void Process__emitErrorEvent(Zig::GlobalObject* global, EncodedJSValu
dlopen Process_functionDlopen Function 1
emitWarning Process_emitWarning Function 1
env constructEnv PropertyCallback
execArgv processExecArgv CustomAccessor
execArgv constructExecArgv PropertyCallback
execPath constructExecPath PropertyCallback
execve Process_functionExecve Function 3
exit Process_functionExit Function 1
Expand Down
5 changes: 0 additions & 5 deletions src/jsc/bindings/BunProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class Process : public WebCore::JSEventEmitter {
WriteBarrier<JSObject> m_nextTickFunction;
// https://github.com/nodejs/node/blob/2eff28fb7a93d3f672f80b582f664a7c701569fb/lib/internal/bootstrap/switches/does_own_process_state.js#L113-L116
WriteBarrier<JSString> m_cachedCwd;
WriteBarrier<Unknown> m_argv;
WriteBarrier<Unknown> m_execArgv;
// The JS warning printer (ProcessObjectInternals createOnWarning), built on the first warning.
WriteBarrier<JSObject> m_onWarning;

Expand Down Expand Up @@ -92,10 +90,7 @@ class Process : public WebCore::JSEventEmitter {
void clearCachedCwd() { m_cachedCwd.clear(); }

JSValue getArgv(JSGlobalObject* globalObject);
void setArgv(JSGlobalObject* globalObject, JSValue argv);

JSValue getExecArgv(JSGlobalObject* globalObject);
void setExecArgv(JSGlobalObject* globalObject, JSValue execArgv);

static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject,
JSC::JSValue prototype)
Expand Down
5 changes: 3 additions & 2 deletions src/jsc/bindings/InspectorLifecycleAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ Protocol::ErrorStringOr<ModuleGraph> InspectorLifecycleAgent::getModuleGraph()
Ref<JSON::ArrayOf<String>> argv = JSON::ArrayOf<String>::create();
{

auto* array = uncheckedDowncast<JSC::JSArray>(process->getArgv(global));
JSC::JSValue argvValue = process->getArgv(global);
RETURN_IF_EXCEPTION(scope, fail("Failed to get argv"_s));
for (size_t i = 0, length = array->length(); i < length; i++) {
auto* array = dynamicDowncast<JSC::JSArray>(argvValue);
for (size_t i = 0, length = array ? array->length() : 0; i < length; i++) {
auto value = array->getIndex(global, i);
RETURN_IF_EXCEPTION(scope, fail("Failed to get value at index"_s));
auto string = value.toWTFString(global);
Expand Down
129 changes: 51 additions & 78 deletions src/jsc/bindings/JSEnvironmentVariableMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ using namespace JSC;

extern "C" size_t Bun__getEnvCount(JSGlobalObject* globalObject, void** list_ptr);
extern "C" size_t Bun__getEnvKey(void* list, size_t index, unsigned char** out);
extern "C" void Bun__getEnvValueAt(JSGlobalObject* globalObject, size_t index, EncodedSlice* value);

extern "C" bool Bun__getEnvValue(JSGlobalObject* globalObject, const EncodedSlice* name, EncodedSlice* value);
extern "C" void Bun__setEnvValue(JSGlobalObject* globalObject, const BunString* name, const BunString* value);
Expand Down Expand Up @@ -216,7 +217,18 @@ bool JSEnvironmentVariableMap::put(JSCell* cell, JSGlobalObject* globalObject, P
static_cast<JSEnvironmentVariableMap*>(cell)->putDirect(vm, propertyName, string, 0);
Comment thread
robobun marked this conversation as resolved.
Resolved
return true;
}
RELEASE_AND_RETURN(scope, Base::put(cell, globalObject, propertyName, string, slot));
// Not `slot`: an inline cache would store `value` uncoerced. PutById: a dictionary after 512 properties, not 128.
PutPropertySlot ownSlot(cell, slot.isStrictMode(), PutPropertySlot::PutById);
RELEASE_AND_RETURN(scope, Base::put(cell, globalObject, propertyName, string, ownSlot));
Comment thread
robobun marked this conversation as resolved.
Resolved
}

void JSEnvironmentVariableMap::putInitialValue(JSGlobalObject* globalObject, const Identifier& name, JSValue value)
{
if (auto index = parseIndex(name)) [[unlikely]] {
putDirectIndex(globalObject, *index, value, 0, PutDirectIndexLikePutDirect);
return;
}
putDirectWithoutTransition(globalObject->vm(), name, value, 0);
}

bool JSEnvironmentVariableMap::putByIndex(JSCell* cell, JSGlobalObject* globalObject, unsigned index, JSValue value, bool shouldThrow)
Expand Down Expand Up @@ -257,30 +269,6 @@ bool JSEnvironmentVariableMap::defineOwnProperty(JSObject* object, JSGlobalObjec
RELEASE_AND_RETURN(scope, put(object, globalObject, propertyName, descriptor.value(), slot));
}

JSC_DEFINE_CUSTOM_GETTER(jsGetterEnvironmentVariable, (JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, PropertyName propertyName))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);

auto* thisObject = dynamicDowncast<JSObject>(JSValue::decode(thisValue));
if (!thisObject) [[unlikely]]
return JSValue::encode(jsUndefined());

EncodedSlice name = toEncodedSlice(propertyName.publicName());
EncodedSlice value = { nullptr, 0 };

if (name.len == 0) [[unlikely]]
return JSValue::encode(jsUndefined());

if (!Bun__getEnvValue(globalObject, &name, &value)) {
return JSValue::encode(jsUndefined());
}

JSValue result = jsString(vm, Zig::toStringCopy(value));
thisObject->putDirect(vm, propertyName, result, 0);
return JSValue::encode(result);
}

JSC_DEFINE_CUSTOM_GETTER(jsTimeZoneEnvironmentVariableGetter, (JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, PropertyName propertyName))
{
VM& vm = globalObject->vm();
Expand All @@ -305,10 +293,7 @@ JSC_DEFINE_CUSTOM_GETTER(jsTimeZoneEnvironmentVariableGetter, (JSGlobalObject *
return JSValue::encode(jsUndefined());
}

JSValue out = jsString(vm, Zig::toStringCopy(value));
thisObject->putDirect(vm, clientData->builtinNames().dataPrivateName(), out, 0);

return JSValue::encode(out);
return JSValue::encode(jsString(vm, Zig::toStringCopy(value)));
Comment thread
robobun marked this conversation as resolved.
Resolved
}

// Store-only: the TZ side effect fires from put() / jsProcessEnvCoerceForWrite on every
Expand Down Expand Up @@ -994,17 +979,28 @@ JSValue createEnvironmentVariablesMap(Zig::GlobalObject* globalObject)
RETURN_IF_EXCEPTION(scope, {});
#else
auto* structure = JSEnvironmentVariableMap::createStructure(vm, globalObject, globalObject->objectPrototype());
JSC::JSObject* object = JSEnvironmentVariableMap::create(vm, structure);
auto* object = JSEnvironmentVariableMap::create(vm, structure);
#endif

static NeverDestroyed<String> TZ = MAKE_STATIC_STRING_IMPL("TZ");
String NODE_TLS_REJECT_UNAUTHORIZED = String("NODE_TLS_REJECT_UNAUTHORIZED"_s);
String BUN_CONFIG_VERBOSE_FETCH = String("BUN_CONFIG_VERBOSE_FETCH"_s);
bool hasTZ = false;
JSString* tz = nullptr;
bool hasNodeTLSRejectUnauthorized = false;
bool hasBunConfigVerboseFetch = false;

auto* cached_getter_setter = JSC::CustomGetterSetter::create(vm, jsGetterEnvironmentVariable, nullptr);
auto valueAt = [&](size_t index) {
EncodedSlice value = { nullptr, 0 };
Bun__getEnvValueAt(globalObject, index, &value);
return jsString(vm, Zig::toStringCopy(value));
};
auto putValue = [&](const Identifier& name, JSValue value) {
#if OS(WINDOWS)
object->putDirectMayBeIndex(globalObject, name, value);
#else
object->putInitialValue(globalObject, name, value);
#endif
};

for (size_t i = 0; i < count; i++) {
unsigned char* chars;
Expand All @@ -1015,7 +1011,7 @@ JSValue createEnvironmentVariablesMap(Zig::GlobalObject* globalObject)
keyArray->putByIndexInline(globalObject, (unsigned)i, jsString(vm, name), false);
#endif
if (name == TZ) {
hasTZ = true;
tz = valueAt(i);
continue;
}
if (name == NODE_TLS_REJECT_UNAUTHORIZED) {
Expand All @@ -1033,53 +1029,30 @@ JSValue createEnvironmentVariablesMap(Zig::GlobalObject* globalObject)
String idName = name;
#endif
Identifier identifier = Identifier::fromString(vm, idName);

// CustomGetterSetter doesn't support indexed properties yet.
// This causes strange issues when the environment variable name is an integer.
if (chars[0] >= '0' && chars[0] <= '9') [[unlikely]] {
if (auto index = parseIndex(identifier)) {
EncodedSlice valueString = { nullptr, 0 };
EncodedSlice nameStr = toEncodedSlice(name);
if (Bun__getEnvValue(globalObject, &nameStr, &valueString)) {
JSValue value = jsString(vm, Zig::toStringCopy(valueString));
RETURN_IF_EXCEPTION(scope, {});
object->putDirectIndex(globalObject, *index, value, 0, PutDirectIndexLikePutDirect);
RETURN_IF_EXCEPTION(scope, {});
}
continue;
}
}

// JSC::PropertyAttribute::CustomValue calls the getter ONCE (the first
// time) and then sets it onto the object, subsequent calls to the
// getter will not go through the getter and instead will just do the
// property lookup.
object->putDirectCustomAccessor(vm, identifier, cached_getter_setter, JSC::PropertyAttribute::CustomValue | 0);
}

unsigned int TZAttrs = JSC::PropertyAttribute::CustomAccessor | 0;
if (!hasTZ) {
TZAttrs |= JSC::PropertyAttribute::DontEnum;
}
object->putDirectCustomAccessor(
vm,
Identifier::fromString(vm, TZ), JSC::CustomGetterSetter::create(vm, jsTimeZoneEnvironmentVariableGetter, jsTimeZoneEnvironmentVariableSetter), TZAttrs);

unsigned int NODE_TLS_REJECT_UNAUTHORIZED_Attrs = JSC::PropertyAttribute::CustomAccessor | 0;
if (!hasNodeTLSRejectUnauthorized) {
NODE_TLS_REJECT_UNAUTHORIZED_Attrs |= JSC::PropertyAttribute::DontEnum;
// Two names that are not valid UTF-8 can decode to the same string.
if (!idName.is8Bit() && isValidOffset(object->getDirectOffset(vm, identifier))) [[unlikely]]
continue;
putValue(identifier, valueAt(i));
RETURN_IF_EXCEPTION(scope, {});
}
object->putDirectCustomAccessor(
vm,
Identifier::fromString(vm, NODE_TLS_REJECT_UNAUTHORIZED), JSC::CustomGetterSetter::create(vm, jsNodeTLSRejectUnauthorizedGetter, jsNodeTLSRejectUnauthorizedSetter), NODE_TLS_REJECT_UNAUTHORIZED_Attrs);

unsigned int BUN_CONFIG_VERBOSE_FETCH_Attrs = JSC::PropertyAttribute::CustomAccessor | 0;
if (!hasBunConfigVerboseFetch) {
BUN_CONFIG_VERBOSE_FETCH_Attrs |= JSC::PropertyAttribute::DontEnum;
}
object->putDirectCustomAccessor(
vm,
Identifier::fromString(vm, BUN_CONFIG_VERBOSE_FETCH), JSC::CustomGetterSetter::create(vm, jsBunConfigVerboseFetchGetter, jsBunConfigVerboseFetchSetter), BUN_CONFIG_VERBOSE_FETCH_Attrs);
auto putAccessor = [&](const String& name, GetValueFunc getter, PutValueFunc setter, bool isSet) {
unsigned attributes = JSC::PropertyAttribute::CustomAccessor | 0;
if (!isSet)
attributes |= JSC::PropertyAttribute::DontEnum;
auto* accessor = JSC::CustomGetterSetter::create(vm, getter, setter);
#if OS(WINDOWS)
object->putDirectCustomAccessor(vm, Identifier::fromString(vm, name), accessor, attributes);
#else
object->putDirectCustomGetterSetterWithoutTransition(vm, Identifier::fromString(vm, name), accessor, attributes);
#endif
};
putAccessor(TZ, jsTimeZoneEnvironmentVariableGetter, jsTimeZoneEnvironmentVariableSetter, !!tz);
// The getter must not add this on the first read: that would change the structure.
if (tz && tz->length())
putValue(WebCore::clientData(vm)->builtinNames().dataPrivateName(), tz);
putAccessor(NODE_TLS_REJECT_UNAUTHORIZED, jsNodeTLSRejectUnauthorizedGetter, jsNodeTLSRejectUnauthorizedSetter, hasNodeTLSRejectUnauthorized);
putAccessor(BUN_CONFIG_VERBOSE_FETCH, jsBunConfigVerboseFetchGetter, jsBunConfigVerboseFetchSetter, hasBunConfigVerboseFetch);

#if OS(WINDOWS)
auto editWindowsEnvVar = JSC::JSFunction::create(vm, globalObject, 0, String("editWindowsEnvVar"_s), jsEditWindowsEnvVar, ImplementationVisibility::Public);
Expand Down
Loading
Loading