aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/JSEnvironmentVariableMap.cpp
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-11-28 15:55:02 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-11-28 15:55:02 -0800
commita6cadce6f6292b685cc4160052304b5dfc8cd3ad (patch)
treea1d4d676cfc04ddcf084e12b26c6b4c59093ebd1 /src/bun.js/bindings/JSEnvironmentVariableMap.cpp
parent51d0c3b79f63e5ca1965cb5280c58fc0a9bc4f73 (diff)
downloadbun-a6cadce6f6292b685cc4160052304b5dfc8cd3ad.tar.gz
bun-a6cadce6f6292b685cc4160052304b5dfc8cd3ad.tar.zst
bun-a6cadce6f6292b685cc4160052304b5dfc8cd3ad.zip
Fix process.env and Bun.env object spread
Fixes https://github.com/oven-sh/bun/issues/1512
Diffstat (limited to 'src/bun.js/bindings/JSEnvironmentVariableMap.cpp')
-rw-r--r--src/bun.js/bindings/JSEnvironmentVariableMap.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/bun.js/bindings/JSEnvironmentVariableMap.cpp b/src/bun.js/bindings/JSEnvironmentVariableMap.cpp
new file mode 100644
index 000000000..75114d791
--- /dev/null
+++ b/src/bun.js/bindings/JSEnvironmentVariableMap.cpp
@@ -0,0 +1,71 @@
+#include "root.h"
+#include "ZigGlobalObject.h"
+
+#include "helpers.h"
+
+#include "JavaScriptCore/JSObject.h"
+#include "JavaScriptCore/ObjectConstructor.h"
+
+extern "C" size_t Bun__getEnvNames(JSGlobalObject*, ZigString* names, size_t max);
+extern "C" bool Bun__getEnvValue(JSGlobalObject* globalObject, ZigString* name, ZigString* value);
+
+namespace Bun {
+
+JSC_DEFINE_CUSTOM_GETTER(jsGetterEnvironmentVariable, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName propertyName))
+{
+ VM& vm = globalObject->vm();
+ auto scope = DECLARE_THROW_SCOPE(vm);
+
+ auto* thisObject = jsDynamicCast<JSObject*>(JSValue::decode(thisValue));
+ if (UNLIKELY(!thisObject))
+ return JSValue::encode(jsUndefined());
+
+ ZigString name = gettoZigString(propertyName.publicName());
+ ZigString value = { nullptr, 0 };
+
+ if (UNLIKELY(name.len == 0))
+ return JSValue::encode(jsUndefined());
+
+ if (!Bun__getEnvValue(globalObject, &name, &value) || value.len == 0) {
+ return JSValue::encode(jsUndefined());
+ }
+
+ JSValue result = jsString(vm, Zig::toStringCopy(value));
+ thisObject->putDirect(vm, propertyName, result, 0);
+ return JSValue::encode(result);
+}
+
+JSC_DEFINE_CUSTOM_SETTER(jsSetterEnvironmentVariable, (JSGlobalObject * globalObject, EncodedJSValue thisValue, EncodedJSValue value, PropertyName propertyName))
+{
+ VM& vm = globalObject->vm();
+ JSC::JSObject* object = JSValue::decode(thisValue).getObject();
+ if (!object)
+ return false;
+
+ object->putDirect(vm, propertyName, JSValue::decode(value), 0);
+ return true;
+}
+
+JSValue createEnvironmentVariablesMap(Zig::GlobalObject* globalObject)
+{
+ VM& vm = globalObject->vm();
+ auto scope = DECLARE_THROW_SCOPE(vm);
+
+ size_t max = 768;
+ ZigString names[max];
+ size_t count = Bun__getEnvNames(globalObject, names, max);
+ JSC::JSObject* object = nullptr;
+ if (count < 63) {
+ object = constructEmptyObject(globalObject, globalObject->objectPrototype(), count);
+ } else {
+ object = constructEmptyObject(globalObject, globalObject->objectPrototype());
+ }
+
+ for (size_t i = 0; i < count; i++) {
+ auto name = Zig::toStringCopy(names[i]);
+ object->putDirectCustomAccessor(vm, Identifier::fromString(vm, name), JSC::CustomGetterSetter::create(vm, jsGetterEnvironmentVariable, jsSetterEnvironmentVariable), JSC::PropertyAttribute::CustomAccessor | 0);
+ }
+
+ return object;
+}
+} \ No newline at end of file