From 20c8bb177759d62841cdd3bd4092969695d114a1 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Thu, 19 Dec 2024 19:30:08 -0800 Subject: [PATCH] More files --- .../BunTransformStreamDefaultController.cpp | 58 ---------- .../BunTransformStreamDefaultController.h | 52 +++------ ...formStreamDefaultControllerConstructor.cpp | 46 ++++++++ ...nsformStreamDefaultControllerConstructor.h | 35 ++++++ ...nsformStreamDefaultControllerPrototype.cpp | 107 ++++++++++++++++++ ...ransformStreamDefaultControllerPrototype.h | 31 +++++ 6 files changed, 236 insertions(+), 93 deletions(-) create mode 100644 src/bun.js/bindings/BunTransformStreamDefaultControllerConstructor.cpp create mode 100644 src/bun.js/bindings/BunTransformStreamDefaultControllerConstructor.h create mode 100644 src/bun.js/bindings/BunTransformStreamDefaultControllerPrototype.cpp create mode 100644 src/bun.js/bindings/BunTransformStreamDefaultControllerPrototype.h diff --git a/src/bun.js/bindings/BunTransformStreamDefaultController.cpp b/src/bun.js/bindings/BunTransformStreamDefaultController.cpp index af6d726a23..e5a5235856 100644 --- a/src/bun.js/bindings/BunTransformStreamDefaultController.cpp +++ b/src/bun.js/bindings/BunTransformStreamDefaultController.cpp @@ -177,62 +177,4 @@ void JSTransformStreamDefaultController::terminate(JSC::JSGlobalObject* globalOb // This would close the readable side and error the writable side } -// JavaScript binding implementations - -JSC_DEFINE_CUSTOM_GETTER(jsTransformStreamDefaultControllerDesiredSize, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName)) -{ - VM& vm = globalObject->vm(); - auto scope = DECLARE_THROW_SCOPE(vm); - - auto* thisObject = jsDynamicCast(JSValue::decode(thisValue)); - if (UNLIKELY(!thisObject)) - return throwVMTypeError(globalObject, scope, "Receiver must be a TransformStreamDefaultController"_s); - - // Return the desired size per spec - return JSValue::encode(jsNumber(0)); // Placeholder -} - -JSC_DEFINE_HOST_FUNCTION(jsTransformStreamDefaultControllerEnqueue, (JSGlobalObject * globalObject, CallFrame* callFrame)) -{ - VM& vm = globalObject->vm(); - auto scope = DECLARE_THROW_SCOPE(vm); - - auto* controller = jsDynamicCast(callFrame->thisValue()); - if (UNLIKELY(!controller)) - return throwVMTypeError(globalObject, scope, "Receiver must be a TransformStreamDefaultController"_s); - - JSValue chunk = callFrame->argument(0); - - if (!controller->enqueue(globalObject, chunk)) - return JSValue::encode(jsUndefined()); - - return JSValue::encode(jsUndefined()); -} - -JSC_DEFINE_HOST_FUNCTION(jsTransformStreamDefaultControllerError, (JSGlobalObject * globalObject, CallFrame* callFrame)) -{ - VM& vm = globalObject->vm(); - auto scope = DECLARE_THROW_SCOPE(vm); - - auto* controller = jsDynamicCast(callFrame->thisValue()); - if (UNLIKELY(!controller)) - return throwVMTypeError(globalObject, scope, "Receiver must be a TransformStreamDefaultController"_s); - - controller->error(globalObject, callFrame->argument(0)); - return JSValue::encode(jsUndefined()); -} - -JSC_DEFINE_HOST_FUNCTION(jsTransformStreamDefaultControllerTerminate, (JSGlobalObject * globalObject, CallFrame* callFrame)) -{ - VM& vm = globalObject->vm(); - auto scope = DECLARE_THROW_SCOPE(vm); - - auto* controller = jsDynamicCast(callFrame->thisValue()); - if (UNLIKELY(!controller)) - return throwVMTypeError(globalObject, scope, "Receiver must be a TransformStreamDefaultController"_s); - - controller->terminate(globalObject); - return JSValue::encode(jsUndefined()); -} - } // namespace Bun diff --git a/src/bun.js/bindings/BunTransformStreamDefaultController.h b/src/bun.js/bindings/BunTransformStreamDefaultController.h index 8ebf3f36a0..988fb0e20b 100644 --- a/src/bun.js/bindings/BunTransformStreamDefaultController.h +++ b/src/bun.js/bindings/BunTransformStreamDefaultController.h @@ -1,62 +1,44 @@ +#pragma once + #include "root.h" namespace Bun { -using namespace JSC; - -// JSTransformStreamDefaultController.h class JSTransformStream; -class JSTransformStreamDefaultController final : public JSC::JSDestructibleObject { - using Base = JSC::JSDestructibleObject; +class JSTransformStreamDefaultController final : public JSC::JSNonFinalObject { + using Base = JSC::JSNonFinalObject; public: - static constexpr bool needsDestruction = true; - - template - static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm); - - static JSTransformStreamDefaultController* create( - JSC::VM& vm, - JSC::JSGlobalObject* globalObject, - JSC::Structure* structure, - JSTransformStream* transformStream); + static JSTransformStreamDefaultController* create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure, JSTransformStream* transformStream); DECLARE_INFO; + template + static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm) + { + STATIC_ASSERT_ISO_SUBSPACE_SHARABLE(JSTransformStreamDefaultController, Base); + return &vm.plainObjectSpace(); + } static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype) { return JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), info()); } - // C++ methods for direct manipulation - bool enqueue(JSC::JSGlobalObject*, JSC::JSValue chunk); - void error(JSC::JSGlobalObject*, JSC::JSValue error); - void terminate(JSC::JSGlobalObject*); - JSC::JSValue desiredSize(JSC::JSGlobalObject*); + bool enqueue(JSC::JSGlobalObject* globalObject, JSC::JSValue chunk); + void error(JSC::JSGlobalObject* globalObject, JSC::JSValue error); + void terminate(JSC::JSGlobalObject* globalObject); - // For garbage collection - DECLARE_VISIT_CHILDREN; + template void visitChildrenImpl(JSCell*, Visitor&); private: - JSTransformStreamDefaultController(JSC::VM& vm, JSC::Structure* structure) - : Base(vm, structure) - { - } - + JSTransformStreamDefaultController(JSC::VM& vm, JSC::Structure* structure); void finishCreation(JSC::VM&, JSC::JSGlobalObject*, JSTransformStream* transformStream); - // Member variables JSC::WriteBarrier m_stream; - JSC::WriteBarrier m_flushPromise; + JSC::WriteBarrier m_flushPromise; JSC::WriteBarrier m_transformAlgorithm; JSC::WriteBarrier m_flushAlgorithm; }; -// Function declarations for JavaScript bindings -JSC_DECLARE_CUSTOM_GETTER(jsTransformStreamDefaultControllerDesiredSize); -JSC_DECLARE_HOST_FUNCTION(jsTransformStreamDefaultControllerEnqueue); -JSC_DECLARE_HOST_FUNCTION(jsTransformStreamDefaultControllerError); -JSC_DECLARE_HOST_FUNCTION(jsTransformStreamDefaultControllerTerminate); - } // namespace Bun diff --git a/src/bun.js/bindings/BunTransformStreamDefaultControllerConstructor.cpp b/src/bun.js/bindings/BunTransformStreamDefaultControllerConstructor.cpp new file mode 100644 index 0000000000..08a3da9133 --- /dev/null +++ b/src/bun.js/bindings/BunTransformStreamDefaultControllerConstructor.cpp @@ -0,0 +1,46 @@ +#include "root.h" + +#include "ErrorCode.h" +#include "BunTransformStreamDefaultControllerConstructor.h" +#include "BunTransformStreamDefaultControllerPrototype.h" + +namespace Bun { + +const JSC::ClassInfo JSTransformStreamDefaultControllerConstructor::s_info = { "Function"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSTransformStreamDefaultControllerConstructor) }; + +JSTransformStreamDefaultControllerConstructor::JSTransformStreamDefaultControllerConstructor(JSC::VM& vm, JSC::Structure* structure) + : Base(vm, structure, call, construct) +{ +} + +JSTransformStreamDefaultControllerConstructor* JSTransformStreamDefaultControllerConstructor::create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure, JSTransformStreamDefaultControllerPrototype* prototype) +{ + JSTransformStreamDefaultControllerConstructor* constructor = new (NotNull, JSC::allocateCell(vm)) + JSTransformStreamDefaultControllerConstructor(vm, structure); + constructor->finishCreation(vm, globalObject, prototype); + return constructor; +} + +void JSTransformStreamDefaultControllerConstructor::finishCreation(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSTransformStreamDefaultControllerPrototype* prototype) +{ + Base::finishCreation(vm, 2, "TransformStreamDefaultController"_s, PropertyAdditionMode::WithoutStructureTransition); + ASSERT(inherits(info())); + + putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly | 0); +} + +JSC::EncodedJSValue JSC_HOST_CALL_ATTRIBUTES JSTransformStreamDefaultControllerConstructor::call(JSC::JSGlobalObject* globalObject, JSC::CallFrame* callFrame) +{ + VM& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + return Bun::throwError(globalObject, scope, Bun::ErrorCode::ERR_ILLEGAL_CONSTRUCTOR, "TransformStreamDefaultController constructor cannot be called directly"_s); +} + +JSC::EncodedJSValue JSC_HOST_CALL_ATTRIBUTES JSTransformStreamDefaultControllerConstructor::construct(JSC::JSGlobalObject* globalObject, JSC::CallFrame* callFrame) +{ + VM& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + return Bun::throwError(globalObject, scope, Bun::ErrorCode::ERR_ILLEGAL_CONSTRUCTOR, "TransformStreamDefaultController constructor cannot be constructed directly"_s); +} + +} // namespace Bun diff --git a/src/bun.js/bindings/BunTransformStreamDefaultControllerConstructor.h b/src/bun.js/bindings/BunTransformStreamDefaultControllerConstructor.h new file mode 100644 index 0000000000..0edfc8ebc1 --- /dev/null +++ b/src/bun.js/bindings/BunTransformStreamDefaultControllerConstructor.h @@ -0,0 +1,35 @@ +#pragma once + +#include "root.h" + +namespace Bun { + +class JSTransformStreamDefaultControllerPrototype; + +class JSTransformStreamDefaultControllerConstructor final : public JSC::InternalFunction { + using Base = JSC::InternalFunction; + +public: + static JSTransformStreamDefaultControllerConstructor* create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure, JSTransformStreamDefaultControllerPrototype* prototype); + + DECLARE_INFO; + template + static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm) + { + return &vm.internalFunctionSpace(); + } + + static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype) + { + return JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::InternalFunctionType, StructureFlags), info()); + } + +private: + JSTransformStreamDefaultControllerConstructor(JSC::VM& vm, JSC::Structure* structure); + void finishCreation(JSC::VM&, JSC::JSGlobalObject*, JSTransformStreamDefaultControllerPrototype*); + + static JSC::EncodedJSValue JSC_HOST_CALL_ATTRIBUTES call(JSC::JSGlobalObject*, JSC::CallFrame*); + static JSC::EncodedJSValue JSC_HOST_CALL_ATTRIBUTES construct(JSC::JSGlobalObject*, JSC::CallFrame*); +}; + +} // namespace Bun diff --git a/src/bun.js/bindings/BunTransformStreamDefaultControllerPrototype.cpp b/src/bun.js/bindings/BunTransformStreamDefaultControllerPrototype.cpp new file mode 100644 index 0000000000..086580d8b7 --- /dev/null +++ b/src/bun.js/bindings/BunTransformStreamDefaultControllerPrototype.cpp @@ -0,0 +1,107 @@ +#include "root.h" + +#include +#include "BunTransformStreamDefaultControllerPrototype.h" +#include "BunTransformStreamDefaultController.h" + +namespace Bun { + +using namespace JSC; + +static JSC_DECLARE_CUSTOM_GETTER(jsTransformStreamDefaultControllerDesiredSize); +static JSC_DECLARE_HOST_FUNCTION(jsTransformStreamDefaultControllerEnqueue); +static JSC_DECLARE_HOST_FUNCTION(jsTransformStreamDefaultControllerError); +static JSC_DECLARE_HOST_FUNCTION(jsTransformStreamDefaultControllerTerminate); + +static const JSC::HashTableValue JSTransformStreamDefaultControllerPrototypeTableValues[] = { + { "enqueue"_s, static_cast(JSC::PropertyAttribute::Function), NoIntrinsic, + { HashTableValue::NativeFunctionType, jsTransformStreamDefaultControllerEnqueue, 1 } }, + { "error"_s, static_cast(JSC::PropertyAttribute::Function), NoIntrinsic, + { HashTableValue::NativeFunctionType, jsTransformStreamDefaultControllerError, 1 } }, + { "terminate"_s, static_cast(JSC::PropertyAttribute::Function), NoIntrinsic, + { HashTableValue::NativeFunctionType, jsTransformStreamDefaultControllerTerminate, 0 } }, + { "desiredSize"_s, static_cast(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor), NoIntrinsic, + { HashTableValue::GetterSetterType, jsTransformStreamDefaultControllerDesiredSize, 0 } }, +}; + +const JSC::ClassInfo JSTransformStreamDefaultControllerPrototype::s_info = { "Function"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSTransformStreamDefaultControllerPrototype) }; + +JSTransformStreamDefaultControllerPrototype::JSTransformStreamDefaultControllerPrototype(JSC::VM& vm, JSC::Structure* structure) + : Base(vm, structure) +{ +} + +JSTransformStreamDefaultControllerPrototype* JSTransformStreamDefaultControllerPrototype::create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure) +{ + JSTransformStreamDefaultControllerPrototype* ptr = new (NotNull, JSC::allocateCell(vm)) + JSTransformStreamDefaultControllerPrototype(vm, structure); + ptr->finishCreation(vm, globalObject); + return ptr; +} + +void JSTransformStreamDefaultControllerPrototype::finishCreation(JSC::VM& vm, JSC::JSGlobalObject* globalObject) +{ + Base::finishCreation(vm); + ASSERT(inherits(info())); + + reifyStaticProperties(vm, info(), JSTransformStreamDefaultControllerPrototypeTableValues, *this); + JSC_TO_STRING_TAG_WITHOUT_TRANSITION(); +} + +JSC_DEFINE_CUSTOM_GETTER(jsTransformStreamDefaultControllerDesiredSize, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName)) +{ + VM& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + auto* thisObject = jsDynamicCast(JSValue::decode(thisValue)); + if (UNLIKELY(!thisObject)) + return throwVMTypeError(globalObject, scope, "Receiver must be a TransformStreamDefaultController"_s); + + // Return the desired size per spec + return JSValue::encode(jsNumber(0)); // Placeholder +} + +JSC_DEFINE_HOST_FUNCTION(jsTransformStreamDefaultControllerEnqueue, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + VM& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + auto* controller = jsDynamicCast(callFrame->thisValue()); + if (UNLIKELY(!controller)) + return throwVMTypeError(globalObject, scope, "Receiver must be a TransformStreamDefaultController"_s); + + JSValue chunk = callFrame->argument(0); + + if (!controller->enqueue(globalObject, chunk)) + return JSValue::encode(jsUndefined()); + + return JSValue::encode(jsUndefined()); +} + +JSC_DEFINE_HOST_FUNCTION(jsTransformStreamDefaultControllerError, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + VM& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + auto* controller = jsDynamicCast(callFrame->thisValue()); + if (UNLIKELY(!controller)) + return throwVMTypeError(globalObject, scope, "Receiver must be a TransformStreamDefaultController"_s); + + controller->error(globalObject, callFrame->argument(0)); + return JSValue::encode(jsUndefined()); +} + +JSC_DEFINE_HOST_FUNCTION(jsTransformStreamDefaultControllerTerminate, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + VM& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + auto* controller = jsDynamicCast(callFrame->thisValue()); + if (UNLIKELY(!controller)) + return throwVMTypeError(globalObject, scope, "Receiver must be a TransformStreamDefaultController"_s); + + controller->terminate(globalObject); + return JSValue::encode(jsUndefined()); +} + +} // namespace Bun diff --git a/src/bun.js/bindings/BunTransformStreamDefaultControllerPrototype.h b/src/bun.js/bindings/BunTransformStreamDefaultControllerPrototype.h new file mode 100644 index 0000000000..96d3fb8b92 --- /dev/null +++ b/src/bun.js/bindings/BunTransformStreamDefaultControllerPrototype.h @@ -0,0 +1,31 @@ +#pragma once + +#include "root.h" + +namespace Bun { + +class JSTransformStreamDefaultControllerPrototype final : public JSC::JSNonFinalObject { + using Base = JSC::JSNonFinalObject; + +public: + static JSTransformStreamDefaultControllerPrototype* create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure); + + DECLARE_INFO; + template + static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm) + { + STATIC_ASSERT_ISO_SUBSPACE_SHARABLE(JSTransformStreamDefaultControllerPrototype, Base); + return &vm.plainObjectSpace(); + } + + static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype) + { + return JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), info()); + } + +private: + JSTransformStreamDefaultControllerPrototype(JSC::VM& vm, JSC::Structure* structure); + void finishCreation(JSC::VM&, JSC::JSGlobalObject*); +}; + +} // namespace Bun