diff --git a/src/bun.js/api/BunObject.zig b/src/bun.js/api/BunObject.zig index df88c72e5d..d53a4af459 100644 --- a/src/bun.js/api/BunObject.zig +++ b/src/bun.js/api/BunObject.zig @@ -72,9 +72,6 @@ pub const BunObject = struct { pub const inspect = toJSLazyPropertyCallback(Bun.getInspect); pub const origin = toJSLazyPropertyCallback(Bun.getOrigin); pub const semver = toJSLazyPropertyCallback(Bun.getSemver); - pub const stderr = toJSLazyPropertyCallback(Bun.getStderr); - pub const stdin = toJSLazyPropertyCallback(Bun.getStdin); - pub const stdout = toJSLazyPropertyCallback(Bun.getStdout); pub const unsafe = toJSLazyPropertyCallback(Bun.getUnsafe); pub const S3Client = toJSLazyPropertyCallback(Bun.getS3ClientConstructor); pub const s3 = toJSLazyPropertyCallback(Bun.getS3DefaultClient); @@ -139,9 +136,6 @@ pub const BunObject = struct { @export(&BunObject.hash, .{ .name = lazyPropertyCallbackName("hash") }); @export(&BunObject.inspect, .{ .name = lazyPropertyCallbackName("inspect") }); @export(&BunObject.origin, .{ .name = lazyPropertyCallbackName("origin") }); - @export(&BunObject.stderr, .{ .name = lazyPropertyCallbackName("stderr") }); - @export(&BunObject.stdin, .{ .name = lazyPropertyCallbackName("stdin") }); - @export(&BunObject.stdout, .{ .name = lazyPropertyCallbackName("stdout") }); @export(&BunObject.unsafe, .{ .name = lazyPropertyCallbackName("unsafe") }); @export(&BunObject.semver, .{ .name = lazyPropertyCallbackName("semver") }); @export(&BunObject.embeddedFiles, .{ .name = lazyPropertyCallbackName("embeddedFiles") }); @@ -188,6 +182,12 @@ pub const BunObject = struct { @export(&BunObject.zstdDecompress, .{ .name = callbackName("zstdDecompress") }); // --- Callbacks --- + // --- LazyProperty initializers --- + @export(&createBunStdin, .{ .name = "BunObject__createBunStdin" }); + @export(&createBunStderr, .{ .name = "BunObject__createBunStderr" }); + @export(&createBunStdout, .{ .name = "BunObject__createBunStdout" }); + // --- LazyProperty initializers --- + // --- Getters --- @export(&BunObject.main, .{ .name = "BunObject_getter_main" }); // --- Getters --- @@ -559,39 +559,6 @@ pub fn getOrigin(globalThis: *jsc.JSGlobalObject, _: *jsc.JSObject) jsc.JSValue return ZigString.init(VirtualMachine.get().origin.origin).toJS(globalThis); } -pub fn getStdin(globalThis: *jsc.JSGlobalObject, _: *jsc.JSObject) jsc.JSValue { - var rare_data = globalThis.bunVM().rareData(); - var store = rare_data.stdin(); - store.ref(); - var blob = jsc.WebCore.Blob.new( - jsc.WebCore.Blob.initWithStore(store, globalThis), - ); - blob.allocator = bun.default_allocator; - return blob.toJS(globalThis); -} - -pub fn getStderr(globalThis: *jsc.JSGlobalObject, _: *jsc.JSObject) jsc.JSValue { - var rare_data = globalThis.bunVM().rareData(); - var store = rare_data.stderr(); - store.ref(); - var blob = jsc.WebCore.Blob.new( - jsc.WebCore.Blob.initWithStore(store, globalThis), - ); - blob.allocator = bun.default_allocator; - return blob.toJS(globalThis); -} - -pub fn getStdout(globalThis: *jsc.JSGlobalObject, _: *jsc.JSObject) jsc.JSValue { - var rare_data = globalThis.bunVM().rareData(); - var store = rare_data.stdout(); - store.ref(); - var blob = jsc.WebCore.Blob.new( - jsc.WebCore.Blob.initWithStore(store, globalThis), - ); - blob.allocator = bun.default_allocator; - return blob.toJS(globalThis); -} - pub fn enableANSIColors(globalThis: *jsc.JSGlobalObject, _: *jsc.JSObject) jsc.JSValue { _ = globalThis; return JSValue.jsBoolean(Output.enable_ansi_colors); @@ -2068,6 +2035,40 @@ comptime { const string = []const u8; +// LazyProperty initializers for stdin/stderr/stdout +pub fn createBunStdin(globalThis: *jsc.JSGlobalObject) callconv(.C) jsc.JSValue { + var rare_data = globalThis.bunVM().rareData(); + var store = rare_data.stdin(); + store.ref(); + var blob = jsc.WebCore.Blob.new( + jsc.WebCore.Blob.initWithStore(store, globalThis), + ); + blob.allocator = bun.default_allocator; + return blob.toJS(globalThis); +} + +pub fn createBunStderr(globalThis: *jsc.JSGlobalObject) callconv(.C) jsc.JSValue { + var rare_data = globalThis.bunVM().rareData(); + var store = rare_data.stderr(); + store.ref(); + var blob = jsc.WebCore.Blob.new( + jsc.WebCore.Blob.initWithStore(store, globalThis), + ); + blob.allocator = bun.default_allocator; + return blob.toJS(globalThis); +} + +pub fn createBunStdout(globalThis: *jsc.JSGlobalObject) callconv(.C) jsc.JSValue { + var rare_data = globalThis.bunVM().rareData(); + var store = rare_data.stdout(); + store.ref(); + var blob = jsc.WebCore.Blob.new( + jsc.WebCore.Blob.initWithStore(store, globalThis), + ); + blob.allocator = bun.default_allocator; + return blob.toJS(globalThis); +} + const Braces = @import("../../shell/braces.zig"); const Which = @import("../../which.zig"); const options = @import("../../options.zig"); diff --git a/src/bun.js/bindings/BunObject+exports.h b/src/bun.js/bindings/BunObject+exports.h index 44d72c07a3..6f1dbf252c 100644 --- a/src/bun.js/bindings/BunObject+exports.h +++ b/src/bun.js/bindings/BunObject+exports.h @@ -31,9 +31,6 @@ macro(origin) \ macro(s3) \ macro(semver) \ - macro(stderr) \ - macro(stdin) \ - macro(stdout) \ macro(unsafe) \ macro(valkey) \ diff --git a/src/bun.js/bindings/BunObject.cpp b/src/bun.js/bindings/BunObject.cpp index 3bb97087a5..9d0fd7eea1 100644 --- a/src/bun.js/bindings/BunObject.cpp +++ b/src/bun.js/bindings/BunObject.cpp @@ -875,6 +875,25 @@ static JSC_DEFINE_CUSTOM_SETTER(setBunObjectMain, (JSC::JSGlobalObject * globalO #define bunObjectReadableStreamToJSONCodeGenerator WebCore::readableStreamReadableStreamToJSONCodeGenerator #define bunObjectReadableStreamToTextCodeGenerator WebCore::readableStreamReadableStreamToTextCodeGenerator +// LazyProperty wrappers for stdin/stderr/stdout +static JSValue BunObject_lazyPropCb_wrap_stdin(VM& vm, JSObject* bunObject) +{ + auto* zigGlobalObject = jsCast(bunObject->globalObject()); + return zigGlobalObject->m_bunStdin.getInitializedOnMainThread(zigGlobalObject); +} + +static JSValue BunObject_lazyPropCb_wrap_stderr(VM& vm, JSObject* bunObject) +{ + auto* zigGlobalObject = jsCast(bunObject->globalObject()); + return zigGlobalObject->m_bunStderr.getInitializedOnMainThread(zigGlobalObject); +} + +static JSValue BunObject_lazyPropCb_wrap_stdout(VM& vm, JSObject* bunObject) +{ + auto* zigGlobalObject = jsCast(bunObject->globalObject()); + return zigGlobalObject->m_bunStdout.getInitializedOnMainThread(zigGlobalObject); +} + #include "BunObject.lut.h" #undef bunObjectReadableStreamToArrayCodeGenerator diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index a0bcf876fa..7e7a3c0270 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -330,6 +330,11 @@ extern "C" void* Bun__getVM(); extern "C" void Bun__setDefaultGlobalObject(Zig::GlobalObject* globalObject); +// Declare the Zig functions for LazyProperty initializers +extern "C" JSC::EncodedJSValue BunObject__createBunStdin(JSC::JSGlobalObject*); +extern "C" JSC::EncodedJSValue BunObject__createBunStderr(JSC::JSGlobalObject*); +extern "C" JSC::EncodedJSValue BunObject__createBunStdout(JSC::JSGlobalObject*); + static JSValue formatStackTraceToJSValue(JSC::VM& vm, Zig::GlobalObject* globalObject, JSC::JSGlobalObject* lexicalGlobalObject, JSC::JSObject* errorObject, JSC::JSArray* callSites) { auto scope = DECLARE_THROW_SCOPE(vm); @@ -3463,6 +3468,17 @@ void GlobalObject::finishCreation(VM& vm) init.set(JSC::JSBigInt64Array::create(init.owner, JSC::JSBigInt64Array::createStructure(init.vm, init.owner, init.owner->objectPrototype()), 7)); }); + // Initialize LazyProperties for stdin/stderr/stdout + m_bunStdin.initLater([](const LazyProperty::Initializer& init) { + init.set(JSC::JSValue::decode(BunObject__createBunStdin(init.owner)).getObject()); + }); + m_bunStderr.initLater([](const LazyProperty::Initializer& init) { + init.set(JSC::JSValue::decode(BunObject__createBunStderr(init.owner)).getObject()); + }); + m_bunStdout.initLater([](const LazyProperty::Initializer& init) { + init.set(JSC::JSValue::decode(BunObject__createBunStdout(init.owner)).getObject()); + }); + configureNodeVM(vm, this); #if ENABLE(REMOTE_INSPECTOR) diff --git a/src/bun.js/bindings/ZigGlobalObject.h b/src/bun.js/bindings/ZigGlobalObject.h index 545f9bdd32..55fb45fd77 100644 --- a/src/bun.js/bindings/ZigGlobalObject.h +++ b/src/bun.js/bindings/ZigGlobalObject.h @@ -621,6 +621,10 @@ public: V(public, LazyPropertyOfGlobalObject, m_JSBunRequestStructure) \ V(public, LazyPropertyOfGlobalObject, m_JSBunRequestParamsPrototype) \ \ + V(public, LazyPropertyOfGlobalObject, m_bunStdin) \ + V(public, LazyPropertyOfGlobalObject, m_bunStderr) \ + V(public, LazyPropertyOfGlobalObject, m_bunStdout) \ + \ V(public, LazyPropertyOfGlobalObject, m_JSNodeHTTPServerSocketStructure) \ V(public, LazyPropertyOfGlobalObject, m_statValues) \ V(public, LazyPropertyOfGlobalObject, m_bigintStatValues) \ @@ -654,6 +658,11 @@ public: JSObject* nodeErrorCache() const { return m_nodeErrorCache.getInitializedOnMainThread(this); } + // LazyProperty accessors for stdin/stderr/stdout + JSC::JSObject* bunStdin() const { return m_bunStdin.getInitializedOnMainThread(this); } + JSC::JSObject* bunStderr() const { return m_bunStderr.getInitializedOnMainThread(this); } + JSC::JSObject* bunStdout() const { return m_bunStdout.getInitializedOnMainThread(this); } + Structure* memoryFootprintStructure() { return m_memoryFootprintStructure.getInitializedOnMainThread(this);