diff options
Diffstat (limited to 'src/javascript/jsc/bindings/ZigGlobalObject.cpp')
-rw-r--r-- | src/javascript/jsc/bindings/ZigGlobalObject.cpp | 484 |
1 files changed, 430 insertions, 54 deletions
diff --git a/src/javascript/jsc/bindings/ZigGlobalObject.cpp b/src/javascript/jsc/bindings/ZigGlobalObject.cpp index 9498d6b14..7d531c7e9 100644 --- a/src/javascript/jsc/bindings/ZigGlobalObject.cpp +++ b/src/javascript/jsc/bindings/ZigGlobalObject.cpp @@ -112,6 +112,26 @@ namespace JSCastingHelpers = JSC::JSCastingHelpers; using JSBuffer = WebCore::JSBuffer; #include <dlfcn.h> +#include "IDLTypes.h" + +#include "JSAbortAlgorithm.h" +#include "JSDOMAttribute.h" +#include "JSByteLengthQueuingStrategy.h" +#include "JSCountQueuingStrategy.h" +#include "JSReadableByteStreamController.h" +#include "JSReadableStream.h" +#include "JSReadableStreamBYOBReader.h" +#include "JSReadableStreamBYOBRequest.h" +#include "JSReadableStreamDefaultController.h" +#include "JSReadableStreamDefaultReader.h" +#include "JSTransformStream.h" +#include "JSTransformStreamDefaultController.h" +#include "JSWritableStream.h" +#include "JSWritableStreamDefaultController.h" +#include "JSWritableStreamDefaultWriter.h" +#include "JavaScriptCore/BuiltinNames.h" +#include "StructuredClone.h" + // #include <iostream> static bool has_loaded_jsc = false; @@ -307,6 +327,13 @@ GlobalObject::GlobalObject(JSC::VM& vm, JSC::Structure* structure) m_scriptExecutionContext = new WebCore::ScriptExecutionContext(&vm, this); } +GlobalObject::~GlobalObject() = default; + +void GlobalObject::destroy(JSCell* cell) +{ + static_cast<GlobalObject*>(cell)->GlobalObject::~GlobalObject(); +} + WebCore::ScriptExecutionContext* GlobalObject::scriptExecutionContext() { return m_scriptExecutionContext; @@ -827,6 +854,23 @@ static JSC_DEFINE_HOST_FUNCTION(functionReportError, return JSC::JSValue::encode(JSC::jsUndefined()); } +JSC_DEFINE_HOST_FUNCTION(functionNoop, (JSC::JSGlobalObject*, JSC::CallFrame*)) +{ + return JSC::JSValue::encode(JSC::jsUndefined()); +} + +JSC_DEFINE_CUSTOM_GETTER(noop_getter, (JSGlobalObject*, EncodedJSValue, PropertyName)) +{ + return JSC::JSValue::encode(JSC::jsUndefined()); +} + +JSC_DEFINE_CUSTOM_SETTER(noop_setter, + (JSC::JSGlobalObject*, JSC::EncodedJSValue, + JSC::EncodedJSValue, JSC::PropertyName)) +{ + return true; +} + // we're trying out a new way to do this lazy loading static JSC_DECLARE_HOST_FUNCTION(functionLazyLoad); static JSC_DEFINE_HOST_FUNCTION(functionLazyLoad, @@ -843,6 +887,7 @@ JSC: } default: { static NeverDestroyed<const String> sqliteString(MAKE_STATIC_STRING_IMPL("sqlite")); + static NeverDestroyed<const String> noopString(MAKE_STATIC_STRING_IMPL("noop")); JSC::JSValue moduleName = callFrame->argument(0); auto string = moduleName.toWTFString(globalObject); if (string.isNull()) { @@ -856,6 +901,14 @@ JSC: return JSC::JSValue::encode(JSSQLStatementConstructor::create(vm, globalObject, JSSQLStatementConstructor::createStructure(vm, globalObject, globalObject->m_functionPrototype.get()))); } + if (UNLIKELY(string == noopString)) { + auto* obj = constructEmptyObject(globalObject); + obj->putDirectCustomAccessor(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "getterSetter"_s)), JSC::CustomGetterSetter::create(vm, noop_getter, noop_setter), 0); + Zig::JSFFIFunction* function = Zig::JSFFIFunction::create(vm, reinterpret_cast<Zig::GlobalObject*>(globalObject), 0, String(), functionNoop, JSC::NoIntrinsic); + obj->putDirect(vm, JSC::PropertyName(JSC::Identifier::fromString(vm, "function"_s)), function, JSC::PropertyAttribute::Function | 0); + return JSC::JSValue::encode(obj); + } + return JSC::JSValue::encode(JSC::jsUndefined()); break; @@ -863,56 +916,277 @@ JSC: } } -// This is not a publicly exposed API currently. -// This is used by the bundler to make Response, Request, FetchEvent, -// and any other objects available globally. -void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm) +static inline JSValue jsServiceWorkerGlobalScope_ByteLengthQueuingStrategyConstructorGetter(JSGlobalObject& lexicalGlobalObject, Zig::GlobalObject& thisObject) { - auto clientData = WebCore::clientData(vm); - size_t constructor_count = 0; - JSC__JSValue const* constructors = Zig__getAPIConstructors(&constructor_count, this); - WTF::Vector<GlobalPropertyInfo> extraStaticGlobals; - extraStaticGlobals.reserveCapacity((size_t)count + constructor_count + 3 + 11); - int i = 0; - for (; i < constructor_count; i++) { - auto* object = JSC::jsDynamicCast<JSC::JSCallbackConstructor*>(JSC::JSValue::decode(constructors[i]).asCell()->getObject()); + UNUSED_PARAM(lexicalGlobalObject); + return JSByteLengthQueuingStrategy::getConstructor(JSC::getVM(&lexicalGlobalObject), &thisObject); +} - extraStaticGlobals.uncheckedAppend( - GlobalPropertyInfo { JSC::Identifier::fromString(vm, object->get(this, vm.propertyNames->name).toWTFString(this)), - JSC::JSValue(object), JSC::PropertyAttribute::DontDelete | 0 }); - } - int j = 0; - for (; j < count - 1; j++) { - auto jsClass = globals[j]; +JSC_DEFINE_CUSTOM_GETTER(jsServiceWorkerGlobalScope_ByteLengthQueuingStrategyConstructor, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + return IDLAttribute<Zig::GlobalObject>::get<jsServiceWorkerGlobalScope_ByteLengthQueuingStrategyConstructorGetter>(*lexicalGlobalObject, thisValue, attributeName); +} - JSC::JSCallbackObject<JSNonFinalObject>* object = JSC::JSCallbackObject<JSNonFinalObject>::create(this, this->callbackObjectStructure(), - jsClass, nullptr); - if (JSObject* prototype = object->classRef()->prototype(this)) - object->setPrototypeDirect(vm, prototype); +static inline JSValue jsServiceWorkerGlobalScope_CountQueuingStrategyConstructorGetter(JSGlobalObject& lexicalGlobalObject, Zig::GlobalObject& thisObject) +{ + UNUSED_PARAM(lexicalGlobalObject); + return JSCountQueuingStrategy::getConstructor(JSC::getVM(&lexicalGlobalObject), &thisObject); +} - extraStaticGlobals.uncheckedAppend( - GlobalPropertyInfo { JSC::Identifier::fromString(vm, jsClass->className()), - JSC::JSValue(object), JSC::PropertyAttribute::DontDelete | 0 }); - } +JSC_DEFINE_CUSTOM_GETTER(jsServiceWorkerGlobalScope_CountQueuingStrategyConstructor, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + return IDLAttribute<Zig::GlobalObject>::get<jsServiceWorkerGlobalScope_CountQueuingStrategyConstructorGetter>(*lexicalGlobalObject, thisValue, attributeName); +} - // The last one must be "process.env" - // Runtime-support is for if they change - dot_env_class_ref = globals[j]; +static inline JSValue jsServiceWorkerGlobalScope_ReadableByteStreamControllerConstructorGetter(JSGlobalObject& lexicalGlobalObject, Zig::GlobalObject& thisObject) +{ + UNUSED_PARAM(lexicalGlobalObject); + return JSReadableByteStreamController::getConstructor(JSC::getVM(&lexicalGlobalObject), &thisObject); +} - // // The last one must be "process.env" - // // Runtime-support is for if they change - // { - // auto jsClass = globals[i]; +JSC_DEFINE_CUSTOM_GETTER(jsServiceWorkerGlobalScope_ReadableByteStreamControllerConstructor, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + return IDLAttribute<Zig::GlobalObject>::get<jsServiceWorkerGlobalScope_ReadableByteStreamControllerConstructorGetter>(*lexicalGlobalObject, thisValue, attributeName); +} - // JSC::JSCallbackObject<JSNonFinalObject> *object = - // JSC::JSCallbackObject<JSNonFinalObject>::create(this, this->callbackObjectStructure(), - // jsClass, nullptr); - // if (JSObject *prototype = jsClass->prototype(this)) object->setPrototypeDirect(vm, - // prototype); +static inline JSValue jsServiceWorkerGlobalScope_ReadableStreamConstructorGetter(JSGlobalObject& lexicalGlobalObject, Zig::GlobalObject& thisObject) +{ + UNUSED_PARAM(lexicalGlobalObject); + return JSReadableStream::getConstructor(JSC::getVM(&lexicalGlobalObject), &thisObject); +} - // process->putDirect(this->vm, JSC::Identifier::fromString(this->vm, "env"), - // JSC::JSValue(object), JSC::PropertyAttribute::DontDelete | 0); - // } +JSC_DEFINE_CUSTOM_GETTER(jsServiceWorkerGlobalScope_ReadableStreamConstructor, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + return IDLAttribute<Zig::GlobalObject>::get<jsServiceWorkerGlobalScope_ReadableStreamConstructorGetter>(*lexicalGlobalObject, thisValue, attributeName); +} + +static inline JSValue jsServiceWorkerGlobalScope_ReadableStreamBYOBReaderConstructorGetter(JSGlobalObject& lexicalGlobalObject, Zig::GlobalObject& thisObject) +{ + UNUSED_PARAM(lexicalGlobalObject); + return JSReadableStreamBYOBReader::getConstructor(JSC::getVM(&lexicalGlobalObject), &thisObject); +} + +JSC_DEFINE_CUSTOM_GETTER(jsServiceWorkerGlobalScope_ReadableStreamBYOBReaderConstructor, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + return IDLAttribute<Zig::GlobalObject>::get<jsServiceWorkerGlobalScope_ReadableStreamBYOBReaderConstructorGetter>(*lexicalGlobalObject, thisValue, attributeName); +} + +static inline JSValue jsServiceWorkerGlobalScope_ReadableStreamBYOBRequestConstructorGetter(JSGlobalObject& lexicalGlobalObject, Zig::GlobalObject& thisObject) +{ + UNUSED_PARAM(lexicalGlobalObject); + return JSReadableStreamBYOBRequest::getConstructor(JSC::getVM(&lexicalGlobalObject), &thisObject); +} + +JSC_DEFINE_CUSTOM_GETTER(jsServiceWorkerGlobalScope_ReadableStreamBYOBRequestConstructor, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + return IDLAttribute<Zig::GlobalObject>::get<jsServiceWorkerGlobalScope_ReadableStreamBYOBRequestConstructorGetter>(*lexicalGlobalObject, thisValue, attributeName); +} + +static inline JSValue jsServiceWorkerGlobalScope_ReadableStreamDefaultControllerConstructorGetter(JSGlobalObject& lexicalGlobalObject, Zig::GlobalObject& thisObject) +{ + UNUSED_PARAM(lexicalGlobalObject); + return JSReadableStreamDefaultController::getConstructor(JSC::getVM(&lexicalGlobalObject), &thisObject); +} + +JSC_DEFINE_CUSTOM_GETTER(jsServiceWorkerGlobalScope_ReadableStreamDefaultControllerConstructor, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + return IDLAttribute<Zig::GlobalObject>::get<jsServiceWorkerGlobalScope_ReadableStreamDefaultControllerConstructorGetter>(*lexicalGlobalObject, thisValue, attributeName); +} + +static inline JSValue jsServiceWorkerGlobalScope_ReadableStreamDefaultReaderConstructorGetter(JSGlobalObject& lexicalGlobalObject, Zig::GlobalObject& thisObject) +{ + UNUSED_PARAM(lexicalGlobalObject); + return JSReadableStreamDefaultReader::getConstructor(JSC::getVM(&lexicalGlobalObject), &thisObject); +} + +JSC_DEFINE_CUSTOM_GETTER(jsServiceWorkerGlobalScope_ReadableStreamDefaultReaderConstructor, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + return IDLAttribute<Zig::GlobalObject>::get<jsServiceWorkerGlobalScope_ReadableStreamDefaultReaderConstructorGetter>(*lexicalGlobalObject, thisValue, attributeName); +} + +static inline JSValue jsServiceWorkerGlobalScope_TransformStreamConstructorGetter(JSGlobalObject& lexicalGlobalObject, Zig::GlobalObject& thisObject) +{ + UNUSED_PARAM(lexicalGlobalObject); + return JSTransformStream::getConstructor(JSC::getVM(&lexicalGlobalObject), &thisObject); +} + +JSC_DEFINE_CUSTOM_GETTER(jsServiceWorkerGlobalScope_TransformStreamConstructor, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + return IDLAttribute<Zig::GlobalObject>::get<jsServiceWorkerGlobalScope_TransformStreamConstructorGetter>(*lexicalGlobalObject, thisValue, attributeName); +} + +static inline JSValue jsServiceWorkerGlobalScope_TransformStreamDefaultControllerConstructorGetter(JSGlobalObject& lexicalGlobalObject, Zig::GlobalObject& thisObject) +{ + UNUSED_PARAM(lexicalGlobalObject); + return JSTransformStreamDefaultController::getConstructor(JSC::getVM(&lexicalGlobalObject), &thisObject); +} + +JSC_DEFINE_CUSTOM_GETTER(jsServiceWorkerGlobalScope_TransformStreamDefaultControllerConstructor, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + return IDLAttribute<Zig::GlobalObject>::get<jsServiceWorkerGlobalScope_TransformStreamDefaultControllerConstructorGetter>(*lexicalGlobalObject, thisValue, attributeName); +} + +static inline JSValue jsServiceWorkerGlobalScope_WritableStreamConstructorGetter(JSGlobalObject& lexicalGlobalObject, Zig::GlobalObject& thisObject) +{ + UNUSED_PARAM(lexicalGlobalObject); + return JSWritableStream::getConstructor(JSC::getVM(&lexicalGlobalObject), &thisObject); +} + +JSC_DEFINE_CUSTOM_GETTER(jsServiceWorkerGlobalScope_WritableStreamConstructor, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + return IDLAttribute<Zig::GlobalObject>::get<jsServiceWorkerGlobalScope_WritableStreamConstructorGetter>(*lexicalGlobalObject, thisValue, attributeName); +} + +static inline JSValue jsServiceWorkerGlobalScope_WritableStreamDefaultControllerConstructorGetter(JSGlobalObject& lexicalGlobalObject, Zig::GlobalObject& thisObject) +{ + UNUSED_PARAM(lexicalGlobalObject); + return JSWritableStreamDefaultController::getConstructor(JSC::getVM(&lexicalGlobalObject), &thisObject); +} + +JSC_DEFINE_CUSTOM_GETTER(jsServiceWorkerGlobalScope_WritableStreamDefaultControllerConstructor, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + return IDLAttribute<Zig::GlobalObject>::get<jsServiceWorkerGlobalScope_WritableStreamDefaultControllerConstructorGetter>(*lexicalGlobalObject, thisValue, attributeName); +} + +static inline JSValue jsServiceWorkerGlobalScope_WritableStreamDefaultWriterConstructorGetter(JSGlobalObject& lexicalGlobalObject, Zig::GlobalObject& thisObject) +{ + UNUSED_PARAM(lexicalGlobalObject); + return JSWritableStreamDefaultWriter::getConstructor(JSC::getVM(&lexicalGlobalObject), &thisObject); +} + +JSC_DEFINE_CUSTOM_GETTER(jsServiceWorkerGlobalScope_WritableStreamDefaultWriterConstructor, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + return IDLAttribute<Zig::GlobalObject>::get<jsServiceWorkerGlobalScope_WritableStreamDefaultWriterConstructorGetter>(*lexicalGlobalObject, thisValue, attributeName); +} + +JSC_DECLARE_HOST_FUNCTION(makeThisTypeErrorForBuiltins); +JSC_DECLARE_HOST_FUNCTION(makeGetterTypeErrorForBuiltins); +JSC_DECLARE_HOST_FUNCTION(makeDOMExceptionForBuiltins); +JSC_DECLARE_HOST_FUNCTION(createWritableStreamFromInternal); +JSC_DECLARE_HOST_FUNCTION(getInternalWritableStream); +JSC_DECLARE_HOST_FUNCTION(whenSignalAborted); +JSC_DECLARE_HOST_FUNCTION(isAbortSignal); +JSC_DEFINE_HOST_FUNCTION(makeThisTypeErrorForBuiltins, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + ASSERT(callFrame); + ASSERT(callFrame->argumentCount() == 2); + VM& vm = globalObject->vm(); + DeferTermination deferScope(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); + + auto interfaceName = callFrame->uncheckedArgument(0).getString(globalObject); + scope.assertNoException(); + auto functionName = callFrame->uncheckedArgument(1).getString(globalObject); + scope.assertNoException(); + return JSValue::encode(createTypeError(globalObject, makeThisTypeErrorMessage(interfaceName.utf8().data(), functionName.utf8().data()))); +} + +JSC_DEFINE_HOST_FUNCTION(makeGetterTypeErrorForBuiltins, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + ASSERT(callFrame); + ASSERT(callFrame->argumentCount() == 2); + VM& vm = globalObject->vm(); + DeferTermination deferScope(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); + + auto interfaceName = callFrame->uncheckedArgument(0).getString(globalObject); + scope.assertNoException(); + auto attributeName = callFrame->uncheckedArgument(1).getString(globalObject); + scope.assertNoException(); + + auto error = static_cast<ErrorInstance*>(createTypeError(globalObject, JSC::makeDOMAttributeGetterTypeErrorMessage(interfaceName.utf8().data(), attributeName))); + error->setNativeGetterTypeError(); + return JSValue::encode(error); +} + +JSC_DEFINE_HOST_FUNCTION(makeDOMExceptionForBuiltins, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + ASSERT(callFrame); + ASSERT(callFrame->argumentCount() == 2); + + auto& vm = globalObject->vm(); + DeferTermination deferScope(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); + + auto codeValue = callFrame->uncheckedArgument(0).getString(globalObject); + scope.assertNoException(); + + auto message = callFrame->uncheckedArgument(1).getString(globalObject); + scope.assertNoException(); + + ExceptionCode code { TypeError }; + if (codeValue == "AbortError") + code = AbortError; + auto value = createDOMException(globalObject, code, message); + + EXCEPTION_ASSERT(!scope.exception() || vm.hasPendingTerminationException()); + + return JSValue::encode(value); +} + +JSC_DEFINE_HOST_FUNCTION(getInternalWritableStream, (JSGlobalObject*, CallFrame* callFrame)) +{ + ASSERT(callFrame); + ASSERT(callFrame->argumentCount() == 1); + + auto* writableStream = jsDynamicCast<JSWritableStream*>(callFrame->uncheckedArgument(0)); + if (UNLIKELY(!writableStream)) + return JSValue::encode(jsUndefined()); + return JSValue::encode(writableStream->wrapped().internalWritableStream()); +} + +JSC_DEFINE_HOST_FUNCTION(createWritableStreamFromInternal, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + ASSERT(callFrame); + ASSERT(callFrame->argumentCount() == 1); + ASSERT(callFrame->uncheckedArgument(0).isObject()); + + auto* jsDOMGlobalObject = JSC::jsCast<JSDOMGlobalObject*>(globalObject); + auto internalWritableStream = InternalWritableStream::fromObject(*jsDOMGlobalObject, *callFrame->uncheckedArgument(0).toObject(globalObject)); + return JSValue::encode(toJSNewlyCreated(globalObject, jsDOMGlobalObject, WritableStream::create(WTFMove(internalWritableStream)))); +} + +JSC_DEFINE_HOST_FUNCTION(whenSignalAborted, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + ASSERT(callFrame); + ASSERT(callFrame->argumentCount() == 2); + + auto& vm = globalObject->vm(); + auto* abortSignal = jsDynamicCast<JSAbortSignal*>(callFrame->uncheckedArgument(0)); + if (UNLIKELY(!abortSignal)) + return JSValue::encode(JSValue(JSC::JSValue::JSFalse)); + + Ref<AbortAlgorithm> abortAlgorithm = JSAbortAlgorithm::create(vm, callFrame->uncheckedArgument(1).getObject()); + + bool result = AbortSignal::whenSignalAborted(abortSignal->wrapped(), WTFMove(abortAlgorithm)); + return JSValue::encode(result ? JSValue(JSC::JSValue::JSTrue) : JSValue(JSC::JSValue::JSFalse)); +} + +JSC_DEFINE_HOST_FUNCTION(isAbortSignal, (JSGlobalObject*, CallFrame* callFrame)) +{ + ASSERT(callFrame->argumentCount() == 1); + return JSValue::encode(jsBoolean(callFrame->uncheckedArgument(0).inherits<JSAbortSignal>())); +} + +void GlobalObject::finishCreation(VM& vm) +{ + Base::finishCreation(vm); + ASSERT(inherits(info())); + + addBuiltinGlobals(vm); + + RELEASE_ASSERT(classInfo()); +} + +void GlobalObject::addBuiltinGlobals(JSC::VM& vm) +{ + m_builtinInternalFunctions.initialize(*this); + + auto clientData = WebCore::clientData(vm); + auto& builtinNames = WebCore::builtinNames(vm); + + WTF::Vector<GlobalPropertyInfo> extraStaticGlobals; + extraStaticGlobals.reserveCapacity(26); JSC::Identifier queueMicrotaskIdentifier = JSC::Identifier::fromString(vm, "queueMicrotask"_s); extraStaticGlobals.uncheckedAppend( @@ -977,17 +1251,26 @@ void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm BunLazyString, functionLazyLoad), JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::Function | 0 }); - this->addStaticGlobals(extraStaticGlobals.data(), extraStaticGlobals.size()); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.makeThisTypeErrorPrivateName(), JSFunction::create(vm, this, 2, String(), makeThisTypeErrorForBuiltins), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.makeGetterTypeErrorPrivateName(), JSFunction::create(vm, this, 2, String(), makeGetterTypeErrorForBuiltins), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.makeDOMExceptionPrivateName(), JSFunction::create(vm, this, 2, String(), makeDOMExceptionForBuiltins), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.whenSignalAbortedPrivateName(), JSFunction::create(vm, this, 2, String(), whenSignalAborted), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.cloneArrayBufferPrivateName(), JSFunction::create(vm, this, 3, String(), cloneArrayBuffer), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.structuredCloneForStreamPrivateName(), JSFunction::create(vm, this, 1, String(), structuredCloneForStream), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(vm.propertyNames->builtinNames().ArrayBufferPrivateName(), arrayBufferConstructor(), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.streamClosedPrivateName(), jsNumber(1), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.streamClosingPrivateName(), jsNumber(2), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.streamErroredPrivateName(), jsNumber(3), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.streamReadablePrivateName(), jsNumber(4), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.streamWaitingPrivateName(), jsNumber(5), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.streamWritablePrivateName(), jsNumber(6), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.isAbortSignalPrivateName(), JSFunction::create(vm, this, 1, String(), isAbortSignal), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.getInternalWritableStreamPrivateName(), JSFunction::create(vm, this, 1, String(), getInternalWritableStream), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); + extraStaticGlobals.uncheckedAppend(GlobalPropertyInfo(builtinNames.createWritableStreamFromInternalPrivateName(), JSFunction::create(vm, this, 1, String(), createWritableStreamFromInternal), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)); - m_NapiClassStructure.initLater( - [](LazyClassStructure::Initializer& init) { - init.setStructure(Zig::NapiClass::createStructure(init.vm, init.global, init.global->m_functionPrototype.get())); - }); + this->addStaticGlobals(extraStaticGlobals.data(), extraStaticGlobals.size()); - m_JSFFIFunctionStructure.initLater( - [](LazyClassStructure::Initializer& init) { - init.setStructure(Zig::JSFFIFunction::createStructure(init.vm, init.global, init.global->m_functionPrototype.get())); - }); + extraStaticGlobals.releaseBuffer(); putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "process"_s), JSC::CustomGetterSetter::create(vm, property_lazyProcessGetter, property_lazyProcessSetter), JSC::PropertyAttribute::CustomAccessor | 0); @@ -1025,6 +1308,99 @@ void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "Buffer"_s), JSC::CustomGetterSetter::create(vm, JSBuffer_getter, nullptr), JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().TransformStreamPublicName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_TransformStreamConstructor, nullptr), attributesForStructure(static_cast<unsigned>(JSC::PropertyAttribute::DontEnum))); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().TransformStreamPrivateName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_TransformStreamConstructor, nullptr), attributesForStructure(static_cast<unsigned>(JSC::PropertyAttribute::DontEnum))); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().TransformStreamDefaultControllerPublicName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_TransformStreamDefaultControllerConstructor, nullptr), attributesForStructure(static_cast<unsigned>(JSC::PropertyAttribute::DontEnum))); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().TransformStreamDefaultControllerPrivateName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_TransformStreamDefaultControllerConstructor, nullptr), attributesForStructure(static_cast<unsigned>(JSC::PropertyAttribute::DontEnum))); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().ReadableByteStreamControllerPrivateName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_ReadableByteStreamControllerConstructor, nullptr), attributesForStructure(JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly)); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().ReadableStreamPrivateName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_ReadableStreamConstructor, nullptr), attributesForStructure(JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly)); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().ReadableStreamBYOBReaderPrivateName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_ReadableStreamBYOBReaderConstructor, nullptr), attributesForStructure(JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly)); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().ReadableStreamBYOBRequestPrivateName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_ReadableStreamBYOBRequestConstructor, nullptr), attributesForStructure(JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly)); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().ReadableStreamDefaultControllerPrivateName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_ReadableStreamDefaultControllerConstructor, nullptr), attributesForStructure(JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly)); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().ReadableStreamDefaultReaderPrivateName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_ReadableStreamDefaultReaderConstructor, nullptr), attributesForStructure(JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly)); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().WritableStreamPrivateName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_WritableStreamConstructor, nullptr), attributesForStructure(JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly)); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().WritableStreamDefaultControllerPrivateName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_WritableStreamDefaultControllerConstructor, nullptr), attributesForStructure(JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly)); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().WritableStreamDefaultWriterPrivateName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_WritableStreamDefaultWriterConstructor, nullptr), attributesForStructure(JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly)); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().AbortSignalPrivateName(), CustomGetterSetter::create(vm, JSDOMAbortSignal_getter, nullptr), JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().TransformStreamDefaultControllerPublicName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_TransformStreamDefaultControllerConstructor, nullptr), static_cast<unsigned>(JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DontEnum)); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().ReadableByteStreamControllerPublicName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_ReadableByteStreamControllerConstructor, nullptr), JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().ReadableStreamPublicName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_ReadableStreamConstructor, nullptr), JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().ReadableStreamBYOBReaderPublicName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_ReadableStreamBYOBReaderConstructor, nullptr), JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().ReadableStreamBYOBRequestPublicName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_ReadableStreamBYOBRequestConstructor, nullptr), JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().ReadableStreamDefaultControllerPublicName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_ReadableStreamDefaultControllerConstructor, nullptr), JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().ReadableStreamDefaultReaderPublicName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_ReadableStreamDefaultReaderConstructor, nullptr), JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().WritableStreamPublicName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_WritableStreamConstructor, nullptr), JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().WritableStreamDefaultControllerPublicName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_WritableStreamDefaultControllerConstructor, nullptr), JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + putDirectCustomAccessor(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().WritableStreamDefaultWriterPublicName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_WritableStreamDefaultWriterConstructor, nullptr), JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + + putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "ByteLengthQueuingStrategy"_s), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_ByteLengthQueuingStrategyConstructor, nullptr), JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "CountQueuingStrategy"_s), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_CountQueuingStrategyConstructor, nullptr), JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); +} + +// This is not a publicly exposed API currently. +// This is used by the bundler to make Response, Request, FetchEvent, +// and any other objects available globally. +void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm) +{ + auto clientData = WebCore::clientData(vm); + size_t constructor_count = 0; + JSC__JSValue const* constructors = Zig__getAPIConstructors(&constructor_count, this); + WTF::Vector<GlobalPropertyInfo> extraStaticGlobals; + extraStaticGlobals.reserveCapacity((size_t)count + constructor_count + 3 + 1); + int i = 0; + for (; i < constructor_count; i++) { + auto* object = JSC::jsDynamicCast<JSC::JSCallbackConstructor*>(JSC::JSValue::decode(constructors[i]).asCell()->getObject()); + + extraStaticGlobals.uncheckedAppend( + GlobalPropertyInfo { JSC::Identifier::fromString(vm, object->get(this, vm.propertyNames->name).toWTFString(this)), + JSC::JSValue(object), JSC::PropertyAttribute::DontDelete | 0 }); + } + int j = 0; + for (; j < count - 1; j++) { + auto jsClass = globals[j]; + + JSC::JSCallbackObject<JSNonFinalObject>* object = JSC::JSCallbackObject<JSNonFinalObject>::create(this, this->callbackObjectStructure(), + jsClass, nullptr); + if (JSObject* prototype = object->classRef()->prototype(this)) + object->setPrototypeDirect(vm, prototype); + + extraStaticGlobals.uncheckedAppend( + GlobalPropertyInfo { JSC::Identifier::fromString(vm, jsClass->className()), + JSC::JSValue(object), JSC::PropertyAttribute::DontDelete | 0 }); + } + + // The last one must be "process.env" + // Runtime-support is for if they change + dot_env_class_ref = globals[j]; + + // // The last one must be "process.env" + // // Runtime-support is for if they change + // { + // auto jsClass = globals[i]; + + // JSC::JSCallbackObject<JSNonFinalObject> *object = + // JSC::JSCallbackObject<JSNonFinalObject>::create(this, this->callbackObjectStructure(), + // jsClass, nullptr); + // if (JSObject *prototype = jsClass->prototype(this)) object->setPrototypeDirect(vm, + // prototype); + + // process->putDirect(this->vm, JSC::Identifier::fromString(this->vm, "env"), + // JSC::JSValue(object), JSC::PropertyAttribute::DontDelete | 0); + // } + + this->addStaticGlobals(extraStaticGlobals.data(), extraStaticGlobals.size()); + + m_NapiClassStructure.initLater( + [](LazyClassStructure::Initializer& init) { + init.setStructure(Zig::NapiClass::createStructure(init.vm, init.global, init.global->m_functionPrototype.get())); + }); + + m_JSFFIFunctionStructure.initLater( + [](LazyClassStructure::Initializer& init) { + init.setStructure(Zig::JSFFIFunction::createStructure(init.vm, init.global, init.global->m_functionPrototype.get())); + }); + // putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "SQL"_s), JSC::CustomGetterSetter::create(vm, JSSQLStatement_getter, nullptr), // JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); @@ -1050,14 +1426,14 @@ void GlobalObject::visitChildrenImpl(JSCell* cell, Visitor& visitor) for (auto& structure : thisObject->m_structures.values()) visitor.append(structure); - // for (auto& guarded : thisObject->m_guardedObjects) - // guarded->visitAggregate(visitor); + for (auto& guarded : thisObject->m_guardedObjects) + guarded->visitAggregate(visitor); } for (auto& constructor : thisObject->constructors().array()) visitor.append(constructor); - // thisObject->m_builtinInternalFunctions.visit(visitor); + thisObject->m_builtinInternalFunctions.visit(visitor); thisObject->m_JSFFIFunctionStructure.visit(visitor); ScriptExecutionContext* context = thisObject->scriptExecutionContext(); visitor.addOpaqueRoot(context); |