diff options
-rw-r--r-- | src/bun.js/bindings/ZigConsoleClient.cpp | 4 | ||||
-rw-r--r-- | src/bun.js/bindings/ZigGlobalObject.cpp | 105 | ||||
-rw-r--r-- | src/bun.js/bindings/ZigGlobalObject.h | 14 | ||||
-rw-r--r-- | src/bun.js/builtins/BunBuiltinNames.h | 1 |
4 files changed, 73 insertions, 51 deletions
diff --git a/src/bun.js/bindings/ZigConsoleClient.cpp b/src/bun.js/bindings/ZigConsoleClient.cpp index e3556ee0b..66d24b129 100644 --- a/src/bun.js/bindings/ZigConsoleClient.cpp +++ b/src/bun.js/bindings/ZigConsoleClient.cpp @@ -57,8 +57,6 @@ void Zig::ConsoleClient::messageWithTypeAndLevel(MessageType type, MessageLevel Ref<ScriptArguments>&& arguments) { JSC::VM& vm = globalObject->vm(); - JSC::GCDeferralContext deferralContext(vm); - JSC::DisallowGC disallowGC; auto args = arguments.ptr(); JSC__JSValue jsArgs[255]; @@ -69,7 +67,7 @@ void Zig::ConsoleClient::messageWithTypeAndLevel(MessageType type, MessageLevel jsArgs[i] = JSC::JSValue::encode(val); } - auto scope = DECLARE_THROW_SCOPE(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); Zig__ConsoleClient__messageWithTypeAndLevel(this->m_client, static_cast<uint32_t>(type), static_cast<uint32_t>(level), globalObject, jsArgs, count); diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index 21caf5f2d..52c5d3f66 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -578,7 +578,6 @@ JSC_DEFINE_CUSTOM_SETTER(property_lazyProcessSetter, return false; } -static JSClassRef dot_env_class_ref; JSC_DEFINE_CUSTOM_GETTER(property_lazyProcessGetter, (JSC::JSGlobalObject * _globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) @@ -587,32 +586,24 @@ JSC_DEFINE_CUSTOM_GETTER(property_lazyProcessGetter, JSC::VM& vm = globalObject->vm(); auto clientData = WebCore::clientData(vm); - JSC::JSValue processPrivate = globalObject->getIfPropertyExists(globalObject, clientData->builtinNames().processPrivateName()); - if (LIKELY(processPrivate)) { - return JSC::JSValue::encode(processPrivate); - } - - auto* process = Zig::Process::create( - vm, Zig::Process::createStructure(vm, globalObject, globalObject->objectPrototype())); - - { - auto jsClass = dot_env_class_ref; - - JSC::JSCallbackObject<JSNonFinalObject>* object = JSC::JSCallbackObject<JSNonFinalObject>::create( - globalObject, globalObject->callbackObjectStructure(), jsClass, nullptr); - if (JSObject* prototype = jsClass->prototype(globalObject)) - object->setPrototypeDirect(vm, prototype); - - process->putDirect(vm, JSC::Identifier::fromString(vm, "env"_s), - JSC::JSValue(object), - JSC::PropertyAttribute::DontDelete | 0); + return JSC::JSValue::encode( + globalObject->processObject()); +} - JSC::gcProtect(JSC::JSValue(object)); - } - globalObject->putDirect(vm, clientData->builtinNames().processPrivateName(), JSC::JSValue(process), 0); - JSC::gcProtect(JSC::JSValue(process)); +JSC_DEFINE_CUSTOM_SETTER(lazyProcessEnvSetter, + (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, + JSC::EncodedJSValue value, JSC::PropertyName)) +{ + return false; +} - return JSC::JSValue::encode(JSC::JSValue(process)); +JSC_DEFINE_CUSTOM_GETTER(lazyProcessEnvGetter, + (JSC::JSGlobalObject * _globalObject, JSC::EncodedJSValue thisValue, + JSC::PropertyName)) +{ + Zig::GlobalObject* globalObject = reinterpret_cast<Zig::GlobalObject*>(_globalObject); + return JSC::JSValue::encode( + globalObject->processEnvObject()); } static JSC_DECLARE_HOST_FUNCTION(functionQueueMicrotask); @@ -1646,7 +1637,12 @@ static inline JSC__JSValue ZigGlobalObject__readableStreamToArrayBufferBody(Zig: auto& builtinNames = WebCore::builtinNames(vm); auto throwScope = DECLARE_THROW_SCOPE(vm); - auto function = globalObject->getDirect(vm, builtinNames.readableStreamToArrayPrivateName()).getObject(); + auto* function = globalObject->m_readableStreamToArrayBuffer.get(); + if (!function) { + function = JSFunction::create(vm, static_cast<JSC::FunctionExecutable*>(readableStreamReadableStreamToArrayBufferCodeGenerator(vm)), globalObject); + globalObject->m_readableStreamToArrayBuffer.set(vm, globalObject, function); + } + JSC::MarkedArgumentBuffer arguments = JSC::MarkedArgumentBuffer(); arguments.append(JSValue::decode(readableStreamValue)); @@ -1672,18 +1668,7 @@ static inline JSC__JSValue ZigGlobalObject__readableStreamToArrayBufferBody(Zig: return JSValue::encode(jsUndefined()); } - auto afterOngoingPromiseCapability = JSC::JSPromise::createNewPromiseCapability(globalObject, globalObject->promiseConstructor()); - auto data = JSC::JSPromise::convertCapabilityToDeferredData(globalObject, afterOngoingPromiseCapability); - - if (auto resolve = globalObject->m_readableStreamToArrayBufferResolve.get()) { - promise->performPromiseThen(globalObject, resolve, data.reject, afterOngoingPromiseCapability); - } else { - JSFunction* resolveFunction = JSFunction::create(vm, globalObject, 1, String(), functionConcatTypedArrays, NoIntrinsic); - globalObject->m_readableStreamToArrayBufferResolve.set(vm, globalObject, resolveFunction); - promise->performPromiseThen(globalObject, resolveFunction, data.reject, afterOngoingPromiseCapability); - } - - RELEASE_AND_RETURN(throwScope, JSC::JSValue::encode(data.promise)); + RELEASE_AND_RETURN(throwScope, JSC::JSValue::encode(promise)); } extern "C" JSC__JSValue ZigGlobalObject__readableStreamToArrayBuffer(Zig::GlobalObject* globalObject, JSC__JSValue readableStreamValue); @@ -1807,6 +1792,29 @@ void GlobalObject::finishCreation(VM& vm) init.set(prototype); }); + m_processEnvObject.initLater( + [](const JSC::LazyProperty<JSC::JSGlobalObject, JSC::JSObject>::Initializer& init) { + auto jsClass = reinterpret_cast<Zig::GlobalObject*>(init.owner)->m_dotEnvClassRef; + + JSC::JSCallbackObject<JSNonFinalObject>* object = JSC::JSCallbackObject<JSNonFinalObject>::create( + init.owner, init.owner->callbackObjectStructure(), jsClass, nullptr); + if (JSObject* prototype = jsClass->prototype(init.owner)) + object->setPrototypeDirect(init.vm, prototype); + init.set(object); + }); + + m_processObject.initLater( + [](const JSC::LazyProperty<JSC::JSGlobalObject, JSC::JSObject>::Initializer& init) { + auto* process = Zig::Process::create( + init.vm, Zig::Process::createStructure(init.vm, init.owner, init.owner->objectPrototype())); + process->putDirectCustomAccessor(init.vm, JSC::Identifier::fromString(init.vm, "env"_s), + JSC::CustomGetterSetter::create(init.vm, lazyProcessEnvGetter, lazyProcessEnvSetter), + JSC::PropertyAttribute::DontDelete + | JSC::PropertyAttribute::CustomValue + | 0); + init.set(process); + }); + m_importMetaObjectStructure.initLater( [](const JSC::LazyProperty<JSC::JSGlobalObject, JSC::JSObject>::Initializer& init) { JSC::JSObject* metaProperties = JSC::constructEmptyObject(init.owner, init.owner->objectPrototype(), 10); @@ -1902,7 +1910,7 @@ void GlobalObject::addBuiltinGlobals(JSC::VM& vm) auto& builtinNames = WebCore::builtinNames(vm); WTF::Vector<GlobalPropertyInfo> extraStaticGlobals; - extraStaticGlobals.reserveCapacity(31); + extraStaticGlobals.reserveCapacity(33); JSC::Identifier queueMicrotaskIdentifier = JSC::Identifier::fromString(vm, "queueMicrotask"_s); extraStaticGlobals.uncheckedAppend( @@ -2008,10 +2016,8 @@ void GlobalObject::addBuiltinGlobals(JSC::VM& vm) putDirectBuiltinFunction(vm, this, builtinNames.createNativeReadableStreamPrivateName(), readableStreamCreateNativeReadableStreamCodeGenerator(vm), PropertyAttribute::Builtin | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); putDirectBuiltinFunction(vm, this, builtinNames.createEmptyReadableStreamPrivateName(), readableStreamCreateEmptyReadableStreamCodeGenerator(vm), PropertyAttribute::Builtin | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); putDirectBuiltinFunction(vm, this, builtinNames.consumeReadableStreamPrivateName(), readableStreamConsumeReadableStreamCodeGenerator(vm), PropertyAttribute::Builtin | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); - putDirectBuiltinFunction(vm, this, builtinNames.readableStreamToArrayPrivateName(), readableStreamReadableStreamToArrayCodeGenerator(vm), PropertyAttribute::Builtin | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); putDirectBuiltinFunction(vm, this, builtinNames.assignToStreamPrivateName(), readableStreamInternalsAssignToStreamCodeGenerator(vm), PropertyAttribute::Builtin | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); putDirectBuiltinFunction(vm, this, builtinNames.createNativeReadableStreamPrivateName(), readableStreamCreateNativeReadableStreamCodeGenerator(vm), PropertyAttribute::Builtin | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); - putDirectBuiltinFunction(vm, this, builtinNames.initializeTextStreamPrivateName(), readableStreamInternalsInitializeTextStreamCodeGenerator(vm), PropertyAttribute::Builtin | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); // putDirectBuiltinFunction(vm, this, builtinNames.loadModulePrivateName(), jsZigGlobalObjectInternalsLoadModuleCodeGenerator(vm), PropertyAttribute::Builtin | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); // putDirectBuiltinFunction(vm, this, builtinNames.requireModulePrivateName(), jsZigGlobalObjectInternalsRequireModuleCodeGenerator(vm), PropertyAttribute::Builtin | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); @@ -2138,7 +2144,7 @@ void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm { JSC::Identifier identifier = JSC::Identifier::fromString(vm, "readableStreamToArrayBuffer"_s); - object->putDirectNativeFunction(vm, this, identifier, 1, functionReadableStreamToArrayBuffer, NoIntrinsic, + object->putDirectBuiltinFunction(vm, this, identifier, readableStreamReadableStreamToArrayBufferCodeGenerator(vm), JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DontDelete | 0); } @@ -2155,6 +2161,12 @@ void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm } { + JSC::Identifier identifier = JSC::Identifier::fromString(vm, "readableStreamToArray"_s); + object->putDirectBuiltinFunction(vm, this, identifier, readableStreamReadableStreamToArrayCodeGenerator(vm), + JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DontDelete | 0); + } + + { JSC::Identifier identifier = JSC::Identifier::fromString(vm, "readableStreamToJSON"_s); object->putDirectBuiltinFunction(vm, this, identifier, readableStreamReadableStreamToJSONCodeGenerator(vm), JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DontDelete | 0); @@ -2186,10 +2198,6 @@ void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm } { - object->putDirectBuiltinFunction(vm, this, builtinNames.readableStreamToArrayPublicName(), readableStreamReadableStreamToArrayCodeGenerator(vm), PropertyAttribute::Builtin | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); - } - - { JSC::Identifier identifier = JSC::Identifier::fromString(vm, "stringHashCode"_s); object->putDirectNativeFunction(vm, this, identifier, 1, functionHashCode, NoIntrinsic, JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DontDelete | 0); @@ -2215,7 +2223,7 @@ void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm // The last one must be "process.env" // Runtime-support is for if they change - dot_env_class_ref = globals[j]; + this->m_dotEnvClassRef = globals[j]; // // The last one must be "process.env" // // Runtime-support is for if they change @@ -2275,11 +2283,14 @@ void GlobalObject::visitChildrenImpl(JSCell* cell, Visitor& visitor) thisObject->m_importMetaObjectStructure.visit(visitor); thisObject->m_lazyReadableStreamPrototypeMap.visit(visitor); thisObject->m_requireMap.visit(visitor); + thisObject->m_processEnvObject.visit(visitor); + thisObject->m_processObject.visit(visitor); visitor.append(thisObject->m_readableStreamToArrayBufferResolve); visitor.append(thisObject->m_readableStreamToText); visitor.append(thisObject->m_readableStreamToJSON); visitor.append(thisObject->m_readableStreamToBlob); + visitor.append(thisObject->m_readableStreamToArrayBuffer); ScriptExecutionContext* context = thisObject->scriptExecutionContext(); visitor.addOpaqueRoot(context); diff --git a/src/bun.js/bindings/ZigGlobalObject.h b/src/bun.js/bindings/ZigGlobalObject.h index 24ac66ff1..0a0e0040a 100644 --- a/src/bun.js/bindings/ZigGlobalObject.h +++ b/src/bun.js/bindings/ZigGlobalObject.h @@ -173,6 +173,16 @@ public: JSC::JSMap* readableStreamNativeMap() { return m_lazyReadableStreamPrototypeMap.getInitializedOnMainThread(this); } JSC::JSMap* requireMap() { return m_requireMap.getInitializedOnMainThread(this); } + JSC::JSObject* processObject() + { + return m_processObject.getInitializedOnMainThread(this); + } + + JSC::JSObject* processEnvObject() + { + return m_processEnvObject.getInitializedOnMainThread(this); + } + void* bunVM() { return m_bunVM; } bool isThreadLocalDefaultGlobalObject = false; @@ -180,6 +190,7 @@ public: mutable WriteBarrier<JSFunction> m_readableStreamToText; mutable WriteBarrier<JSFunction> m_readableStreamToBlob; mutable WriteBarrier<JSFunction> m_readableStreamToJSON; + mutable WriteBarrier<JSFunction> m_readableStreamToArrayBuffer; private: void addBuiltinGlobals(JSC::VM&); @@ -203,8 +214,11 @@ private: LazyProperty<JSGlobalObject, JSObject> m_JSHTTPSResponseControllerPrototype; LazyProperty<JSGlobalObject, JSObject> m_JSHTTPResponseControllerPrototype; LazyProperty<JSGlobalObject, JSObject> m_importMetaObjectStructure; + LazyProperty<JSGlobalObject, JSObject> m_processObject; + LazyProperty<JSGlobalObject, JSObject> m_processEnvObject; LazyProperty<JSGlobalObject, JSMap> m_lazyReadableStreamPrototypeMap; LazyProperty<JSGlobalObject, JSMap> m_requireMap; + JSClassRef m_dotEnvClassRef; DOMGuardedObjectSet m_guardedObjects WTF_GUARDED_BY_LOCK(m_gcLock); void* m_bunVM; diff --git a/src/bun.js/builtins/BunBuiltinNames.h b/src/bun.js/builtins/BunBuiltinNames.h index 0014cb7de..8cc0369b4 100644 --- a/src/bun.js/builtins/BunBuiltinNames.h +++ b/src/bun.js/builtins/BunBuiltinNames.h @@ -115,7 +115,6 @@ using namespace JSC; macro(ignoreBOM) \ macro(inFlightCloseRequest) \ macro(inFlightWriteRequest) \ - macro(initializeTextStream) \ macro(initializeWith) \ macro(internalStream) \ macro(internalWritable) \ |