aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/ZigGlobalObject.cpp
diff options
context:
space:
mode:
authorGravatar dave caruso <me@paperdave.net> 2023-09-20 21:28:07 -0400
committerGravatar GitHub <noreply@github.com> 2023-09-20 18:28:07 -0700
commit34d191be67e821284e6e2a241a17731f2d646e2f (patch)
tree9fbb16c5d34965f702c125d1099b0e0eb4e54819 /src/bun.js/bindings/ZigGlobalObject.cpp
parent5c6d7760a5e706bcfd0421680bfa148fc50aec63 (diff)
downloadbun-34d191be67e821284e6e2a241a17731f2d646e2f.tar.gz
bun-34d191be67e821284e6e2a241a17731f2d646e2f.tar.zst
bun-34d191be67e821284e6e2a241a17731f2d646e2f.zip
feat(runtime): implement `console._stdout` (#5842)
* implement console._stdout * nonenum
Diffstat (limited to 'src/bun.js/bindings/ZigGlobalObject.cpp')
-rw-r--r--src/bun.js/bindings/ZigGlobalObject.cpp36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp
index 2f3aa2209..f3abd0f2d 100644
--- a/src/bun.js/bindings/ZigGlobalObject.cpp
+++ b/src/bun.js/bindings/ZigGlobalObject.cpp
@@ -3439,6 +3439,38 @@ JSC_DEFINE_CUSTOM_GETTER(getConsoleConstructor, (JSGlobalObject * globalObject,
return JSValue::encode(result);
}
+// `console._stdout` is equal to `process.stdout`
+JSC_DEFINE_CUSTOM_GETTER(getConsoleStdout, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName property))
+{
+ auto& vm = globalObject->vm();
+ auto console = JSValue::decode(thisValue).getObject();
+ auto global = static_cast<Zig::GlobalObject*>(globalObject);
+
+ // instead of calling the constructor builtin, go through the process.stdout getter to ensure it's only created once.
+ auto stdout = global->processObject()->get(globalObject, Identifier::fromString(vm, "stdout"_s));
+ if (!stdout)
+ return JSValue::encode({});
+
+ console->putDirect(vm, property, stdout, PropertyAttribute::DontEnum | 0);
+ return JSValue::encode(stdout);
+}
+
+// `console._stderr` is equal to `process.stderr`
+JSC_DEFINE_CUSTOM_GETTER(getConsoleStderr, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName property))
+{
+ auto& vm = globalObject->vm();
+ auto console = JSValue::decode(thisValue).getObject();
+ auto global = static_cast<Zig::GlobalObject*>(globalObject);
+
+ // instead of calling the constructor builtin, go through the process.stdout getter to ensure it's only created once.
+ auto stdout = global->processObject()->get(globalObject, Identifier::fromString(vm, "stderr"_s));
+ if (!stdout)
+ return JSValue::encode({});
+
+ console->putDirect(vm, property, stdout, PropertyAttribute::DontEnum | 0);
+ return JSValue::encode(stdout);
+}
+
JSC_DEFINE_CUSTOM_SETTER(EventSource_setter,
(JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue,
JSC::EncodedJSValue value, JSC::PropertyName property))
@@ -3676,7 +3708,9 @@ void GlobalObject::addBuiltinGlobals(JSC::VM& vm)
JSC::JSObject* consoleObject = this->get(this, JSC::Identifier::fromString(vm, "console"_s)).getObject();
consoleObject->putDirectBuiltinFunction(vm, this, vm.propertyNames->asyncIteratorSymbol, consoleObjectAsyncIteratorCodeGenerator(vm), PropertyAttribute::Builtin | 0);
consoleObject->putDirectBuiltinFunction(vm, this, clientData->builtinNames().writePublicName(), consoleObjectWriteCodeGenerator(vm), PropertyAttribute::Builtin | 0);
- consoleObject->putDirectCustomAccessor(vm, Identifier::fromString(vm, "Console"_s), CustomGetterSetter::create(vm, getConsoleConstructor, noop_setter), 0);
+ consoleObject->putDirectCustomAccessor(vm, Identifier::fromString(vm, "Console"_s), CustomGetterSetter::create(vm, getConsoleConstructor, nullptr), 0);
+ consoleObject->putDirectCustomAccessor(vm, Identifier::fromString(vm, "_stdout"_s), CustomGetterSetter::create(vm, getConsoleStdout, nullptr), PropertyAttribute::DontEnum | 0);
+ consoleObject->putDirectCustomAccessor(vm, Identifier::fromString(vm, "_stderr"_s), CustomGetterSetter::create(vm, getConsoleStderr, nullptr), PropertyAttribute::DontEnum | 0);
}
extern "C" bool JSC__JSGlobalObject__startRemoteInspector(JSC__JSGlobalObject* globalObject, unsigned char* host, uint16_t arg1)