diff options
author | 2022-04-25 07:09:18 -0700 | |
---|---|---|
committer | 2022-04-25 07:09:18 -0700 | |
commit | 2c6e5c3fc4a7255eb29f7ae618d2826dd9a7d5e5 (patch) | |
tree | 1de65b5cc11b7a479f92f853980d6a8ee640c9e6 /src/javascript/jsc | |
parent | 4b4df5095ea756388df4c26683ab0bb222750ed3 (diff) | |
download | bun-2c6e5c3fc4a7255eb29f7ae618d2826dd9a7d5e5.tar.gz bun-2c6e5c3fc4a7255eb29f7ae618d2826dd9a7d5e5.tar.zst bun-2c6e5c3fc4a7255eb29f7ae618d2826dd9a7d5e5.zip |
some of buffer
Diffstat (limited to 'src/javascript/jsc')
23 files changed, 2428 insertions, 30 deletions
diff --git a/src/javascript/jsc/bindings/Buffer.cpp b/src/javascript/jsc/bindings/Buffer.cpp new file mode 100644 index 000000000..e20807b50 --- /dev/null +++ b/src/javascript/jsc/bindings/Buffer.cpp @@ -0,0 +1,52 @@ +// ----- THIS IS NOT WEBCORE ---- +// It reuses the namespace. +// ----- THIS IS NOT WEBCORE ---- + +// Node.js buffer + +#include "root.h" + +#include "Buffer.h" +#include "JavaScriptCore/JSArrayBufferViewInlines.h" + +namespace WebCore { + +Ref<Buffer> Buffer::create(JSC::JSGlobalObject* globalObject, RefPtr<ArrayBuffer>&& arrayBuffer, size_t byteOffset, size_t length) +{ + return adoptRef(*new Buffer(globalObject, WTFMove(arrayBuffer), byteOffset, length)); +} +Ref<Buffer> Buffer::create(JSC::JSGlobalObject* globalObject, RefPtr<ArrayBuffer>&& arrayBuffer) +{ + return create(globalObject, WTFMove(arrayBuffer), 0, arrayBuffer->byteLength()); +} + +int32_t static write(WTF::StringView view, size_t offset, size_t length, BufferEncodingType encodingType) +{ +} + +Buffer::~Buffer() +{ + m_arrayBuffer->deref(); +} + +Ref<Buffer> Buffer::createEmpty(JSC::JSGlobalObject* globalObject) +{ + return adoptRef(*new Buffer(globalObject, nullptr, 0, 0)); +} +Ref<Buffer> Buffer::create(JSC::JSGlobalObject* globalObject, UChar* ptr, size_t len, BufferEncodingType encoding) +{ +} +Ref<Buffer> Buffer::create(JSC::JSGlobalObject* globalObject, LChar* ptr, size_t len, BufferEncodingType encoding) +{ +} + +Ref<Buffer> Buffer::create(JSC::JSGlobalObject* globalObject, WTF::StringView& str, BufferEncodingType encoding) +{ + if (str.is8Bit()) { + } +} +Ref<Buffer> Buffer::create(JSC::JSGlobalObject* globalObject, WTF::String& str, BufferEncodingType encoding) +{ +} + +}
\ No newline at end of file diff --git a/src/javascript/jsc/bindings/Buffer.h b/src/javascript/jsc/bindings/Buffer.h new file mode 100644 index 000000000..ac57e4d25 --- /dev/null +++ b/src/javascript/jsc/bindings/Buffer.h @@ -0,0 +1,42 @@ +#pragma once +// ----- THIS IS NOT WEBCORE ---- +// It reuses the namespace. +// ----- THIS IS NOT WEBCORE ---- + +// Node.js buffer + +#include "root.h" + +#include "BufferEncodingType.h" +#include "JavaScriptCore/GenericTypedArrayView.h" + +namespace WebCore { + +class Buffer final : public RefCounted<Buffer> { +public: + using Adaptor = JSC::JSUint8Array::Adaptor; + ~Buffer(); + + static int32_t write(WTF::StringView view, size_t offset, size_t length, BufferEncodingType encodingType); + + static Ref<Buffer> create(JSC::JSGlobalObject* globalObject, RefPtr<ArrayBuffer>&&, size_t byteOffset, size_t length); + static Ref<Buffer> create(JSC::JSGlobalObject* globalObject, RefPtr<ArrayBuffer>&&); + + static Ref<Buffer> createEmpty(JSC::JSGlobalObject* globalObject); + static Ref<Buffer> create(JSC::JSGlobalObject* globalObject, UChar* ptr, size_t len, BufferEncodingType encoding); + static Ref<Buffer> create(JSC::JSGlobalObject* globalObject, LChar* ptr, size_t len, BufferEncodingType encoding); + + static Ref<Buffer> create(JSC::JSGlobalObject* globalObject, WTF::StringView&, BufferEncodingType encoding); + static Ref<Buffer> create(JSC::JSGlobalObject* globalObject, WTF::String&, BufferEncodingType encoding); + + Buffer(JSC::JSGlobalObject* globalObject, RefPtr<ArrayBuffer>&& arrayBuffer, size_t byteOffset, + size_t length) + : m_arrayBuffer(WTFMove(arrayBuffer)) + + { + } + + RefPtr<JSC::ArrayBuffer> m_arrayBuffer; +}; + +}
\ No newline at end of file diff --git a/src/javascript/jsc/bindings/BufferEncodingType.h b/src/javascript/jsc/bindings/BufferEncodingType.h new file mode 100644 index 000000000..0d5058712 --- /dev/null +++ b/src/javascript/jsc/bindings/BufferEncodingType.h @@ -0,0 +1,20 @@ +#pragma once + +namespace WebCore { + +enum class BufferEncodingType { + utf8 = 0, + ucs2 = 1, + utf16le = 2, + latin1 = 3, + ascii = 4, + base64 = 5, + base64url = 6, + hex = 7, + + /// Refer to the buffer's encoding + buffer = 8, + +}; + +}
\ No newline at end of file diff --git a/src/javascript/jsc/bindings/JSBuffer.cpp b/src/javascript/jsc/bindings/JSBuffer.cpp new file mode 100644 index 000000000..f45fd2a9a --- /dev/null +++ b/src/javascript/jsc/bindings/JSBuffer.cpp @@ -0,0 +1,1218 @@ +#include "root.h" +#include "JSBuffer.h" + +#include "ActiveDOMObject.h" +#include "ExtendedDOMClientIsoSubspaces.h" +#include "ExtendedDOMIsoSubspaces.h" +#include "IDLTypes.h" +// #include "JSBlob.h" +#include "JSDOMAttribute.h" +#include "JSDOMBinding.h" +#include "JSDOMConstructor.h" +#include "JSDOMConvertBase.h" +#include "JSDOMConvertInterface.h" +#include "JSDOMConvertStrings.h" +#include "JSDOMExceptionHandling.h" +#include "JSDOMGlobalObject.h" +#include "JSDOMGlobalObjectInlines.h" +#include "JSDOMOperation.h" +#include "JSDOMWrapperCache.h" +#include "ScriptExecutionContext.h" +#include "WebCoreJSClientData.h" +#include "JavaScriptCore/FunctionPrototype.h" +#include "JavaScriptCore/HeapAnalyzer.h" + +#include "JavaScriptCore/JSDestructibleObjectHeapCellType.h" +#include "JavaScriptCore/SlotVisitorMacros.h" +#include "JavaScriptCore/SubspaceInlines.h" +#include "wtf/GetPtr.h" +#include "wtf/PointerPreparations.h" +#include "wtf/URL.h" + +#include "JSBufferEncodingType.h" + +#if ENABLE(MEDIA_SOURCE) +#include "BufferMediaSource.h" +#include "JSMediaSource.h" +#endif + +// #include "JavaScriptCore/JSTypedArrayViewPrototype.h" +#include "JavaScriptCore/JSArrayBufferViewInlines.h" + +static JSC_DECLARE_HOST_FUNCTION(jsBufferConstructorFunction_alloc); +static JSC_DECLARE_HOST_FUNCTION(jsBufferConstructorFunction_allocUnsafe); +static JSC_DECLARE_HOST_FUNCTION(jsBufferConstructorFunction_allocUnsafeSlow); +static JSC_DECLARE_HOST_FUNCTION(jsBufferConstructorFunction_byteLength); +static JSC_DECLARE_HOST_FUNCTION(jsBufferConstructorFunction_compare); +static JSC_DECLARE_HOST_FUNCTION(jsBufferConstructorFunction_concat); +static JSC_DECLARE_HOST_FUNCTION(jsBufferConstructorFunction_from); +static JSC_DECLARE_HOST_FUNCTION(jsBufferConstructorFunction_isBuffer); +static JSC_DECLARE_HOST_FUNCTION(jsBufferConstructorFunction_isEncoding); + +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_compare); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_copy); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_equals); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_fill); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_includes); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_indexOf); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_lastIndexOf); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readBigInt64BE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readBigInt64LE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readBigUInt64BE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readBigUInt64LE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readDoubleBE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readDoubleLE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readFloatBE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readFloatLE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readInt16BE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readInt16LE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readInt32BE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readInt32LE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readInt8); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readIntBE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readIntLE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readUInt16BE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readUInt16LE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readUInt32BE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readUInt32LE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readUInt8); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readUIntBE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_readUIntLE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_swap16); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_swap32); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_swap64); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_toString); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_write); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeBigInt64BE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeBigInt64LE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeBigUInt64BE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeBigUInt64LE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeDoubleBE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeDoubleLE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeFloatBE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeFloatLE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeInt16BE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeInt16LE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeInt32BE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeInt32LE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeInt8); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeIntBE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeIntLE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeUInt16BE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeUInt16LE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeUInt32BE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeUInt32LE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeUInt8); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeUIntBE); +static JSC_DECLARE_HOST_FUNCTION(jsBufferPrototypeFunction_writeUIntLE); + +namespace WebCore { +using namespace JSC; + +template<> class IDLOperation<JSBuffer> { +public: + using ClassParameter = JSC::JSUint8Array*; + using Operation = JSC::EncodedJSValue(JSC::JSGlobalObject*, JSC::CallFrame*, ClassParameter); + + template<Operation operation, CastedThisErrorBehavior = CastedThisErrorBehavior::Throw> + static JSC::EncodedJSValue call(JSC::JSGlobalObject& lexicalGlobalObject, JSC::CallFrame& callFrame, const char* operationName) + { + auto& vm = JSC::getVM(&lexicalGlobalObject); + auto throwScope = DECLARE_THROW_SCOPE(vm); + + auto thisValue = callFrame.thisValue().toThis(&lexicalGlobalObject, JSC::ECMAMode::strict()); + if (thisValue.isUndefinedOrNull()) { + throwTypeError(&lexicalGlobalObject, throwScope, "Cannot convert undefined or null to object"); + return JSC::JSValue::encode(JSC::jsUndefined()); + } + + auto thisObject = JSC::jsCast<JSC::JSUint8Array*>(thisValue); + if (UNLIKELY(!thisObject)) + return throwThisTypeError(lexicalGlobalObject, throwScope, "Buffer", operationName); + + RELEASE_AND_RETURN(throwScope, (operation(&lexicalGlobalObject, &callFrame, thisObject))); + } +}; + +class JSBufferPrototype : public JSC::JSNonFinalObject { +public: + using Base = JSC::JSNonFinalObject; + static constexpr JSC::TypedArrayType TypedArrayStorageType = JSC::JSUint8Array::Adaptor::typeValue; + static JSBufferPrototype* create(JSC::VM& vm, JSDOMGlobalObject* globalObject, JSC::Structure* structure) + { + JSBufferPrototype* ptr = new (NotNull, JSC::allocateCell<JSBufferPrototype>(vm)) JSBufferPrototype(vm, globalObject, structure); + ptr->finishCreation(vm, globalObject); + return ptr; + } + + DECLARE_INFO; + template<typename CellType, JSC::SubspaceAccess> + static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm) + { + 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: + JSBufferPrototype(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure) + : Base(vm, structure) + { + } + + void finishCreation(JSC::VM&, JSC::JSGlobalObject*); +}; +STATIC_ASSERT_ISO_SUBSPACE_SHARABLE(JSBufferPrototype, JSBufferPrototype::Base); + +static inline JSC::EncodedJSValue jsBufferConstructorFunction_allocBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSValue::encode(jsUndefined()); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferConstructorFunction_alloc, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferConstructorFunction_allocBody>(*lexicalGlobalObject, *callFrame, "alloc"); +} +static inline JSC::EncodedJSValue jsBufferConstructorFunction_allocUnsafeBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSValue::encode(jsUndefined()); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferConstructorFunction_allocUnsafe, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferConstructorFunction_allocUnsafeBody>(*lexicalGlobalObject, *callFrame, "allocUnsafe"); +} +static inline JSC::EncodedJSValue jsBufferConstructorFunction_allocUnsafeSlowBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSValue::encode(jsUndefined()); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferConstructorFunction_allocUnsafeSlow, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferConstructorFunction_allocUnsafeSlowBody>(*lexicalGlobalObject, *callFrame, "allocUnsafeSlow"); +} +static inline JSC::EncodedJSValue jsBufferConstructorFunction_byteLengthBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSValue::encode(jsUndefined()); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferConstructorFunction_byteLength, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferConstructorFunction_byteLengthBody>(*lexicalGlobalObject, *callFrame, "byteLength"); +} +static inline JSC::EncodedJSValue jsBufferConstructorFunction_compareBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSValue::encode(jsUndefined()); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferConstructorFunction_compare, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferConstructorFunction_compareBody>(*lexicalGlobalObject, *callFrame, "compare"); +} +static inline JSC::EncodedJSValue jsBufferConstructorFunction_concatBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSValue::encode(jsUndefined()); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferConstructorFunction_concat, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferConstructorFunction_concatBody>(*lexicalGlobalObject, *callFrame, "concat"); +} +static inline JSC::EncodedJSValue jsBufferConstructorFunction_fromBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSValue::encode(jsUndefined()); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferConstructorFunction_from, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferConstructorFunction_fromBody>(*lexicalGlobalObject, *callFrame, "from"); +} +static inline JSC::EncodedJSValue jsBufferConstructorFunction_isBufferBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSValue::encode(jsUndefined()); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferConstructorFunction_isBuffer, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferConstructorFunction_isBufferBody>(*lexicalGlobalObject, *callFrame, "isBuffer"); +} +static inline JSC::EncodedJSValue jsBufferConstructorFunction_isEncodingBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSValue::encode(jsUndefined()); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferConstructorFunction_isEncoding, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferConstructorFunction_isEncodingBody>(*lexicalGlobalObject, *callFrame, "isEncoding"); +} + +using JSBufferConstructor = JSDOMConstructor<JSBuffer>; + +/* Hash table for constructor */ +static const HashTableValue JSBufferConstructorTableValues[] = { + { "alloc", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferConstructorFunction_alloc), (intptr_t)(3) } }, + { "allocUnsafe", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferConstructorFunction_allocUnsafe), (intptr_t)(1) } }, + { "allocUnsafeSlow", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferConstructorFunction_allocUnsafeSlow), (intptr_t)(1) } }, + { "byteLength", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferConstructorFunction_byteLength), (intptr_t)(2) } }, + { "compare", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferConstructorFunction_compare), (intptr_t)(2) } }, + { "concat", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferConstructorFunction_concat), (intptr_t)(2) } }, + { "from", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferConstructorFunction_from), (intptr_t)(3) } }, + { "isBuffer", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferConstructorFunction_isBuffer), (intptr_t)(1) } }, + { "isEncoding", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferConstructorFunction_isEncoding), (intptr_t)(1) } }, +}; + +// new Buffer() +static inline EncodedJSValue constructBufferEmpty(JSGlobalObject* lexicalGlobalObject, CallFrame* callFrame) +{ + VM& vm = lexicalGlobalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + auto* castedThis = jsCast<JSBufferConstructor*>(callFrame->jsCallee()); + ASSERT(castedThis); + auto object = Buffer::createEmpty(lexicalGlobalObject); + auto jsValue = toJSNewlyCreated<IDLInterface<Buffer>>(*lexicalGlobalObject, *castedThis->globalObject(), throwScope, WTFMove(object)); + if constexpr (IsExceptionOr<decltype(object)>) + RETURN_IF_EXCEPTION(throwScope, {}); + setSubclassStructureIfNeeded<Buffer>(lexicalGlobalObject, callFrame, asObject(jsValue)); + RETURN_IF_EXCEPTION(throwScope, {}); + return JSValue::encode(jsValue); +} + +// new Buffer(array) +static inline EncodedJSValue constructBufferFromArray(JSGlobalObject* lexicalGlobalObject, CallFrame* callFrame) +{ + VM& vm = lexicalGlobalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + auto* castedThis = jsCast<JSBufferConstructor*>(callFrame->jsCallee()); + ASSERT(castedThis); + EnsureStillAliveScope argument0 = callFrame->uncheckedArgument(0); +} + +// new Buffer(arrayBuffer[, byteOffset[, length]]) +static inline EncodedJSValue constructBufferFromArrayBuffer(JSGlobalObject* lexicalGlobalObject, CallFrame* callFrame) +{ + VM& vm = lexicalGlobalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + auto* castedThis = jsCast<JSBufferConstructor*>(callFrame->jsCallee()); + ASSERT(castedThis); + EnsureStillAliveScope argument0 = callFrame->uncheckedArgument(0); +} + +// new Buffer(buffer) +static inline EncodedJSValue constructBufferFromBuffer(JSGlobalObject* lexicalGlobalObject, CallFrame* callFrame) +{ + VM& vm = lexicalGlobalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + auto* castedThis = jsCast<JSBufferConstructor*>(callFrame->jsCallee()); + ASSERT(castedThis); + EnsureStillAliveScope argument0 = callFrame->uncheckedArgument(0); +} + +// new Buffer(size) +static inline EncodedJSValue constructBufferFromLength(JSGlobalObject* lexicalGlobalObject, CallFrame* callFrame) +{ + VM& vm = lexicalGlobalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + auto* castedThis = jsCast<JSBufferConstructor*>(callFrame->jsCallee()); + ASSERT(castedThis); + auto length = callFrame->uncheckedArgument(0).toInt32(lexicalGlobalObject); + if (length < 0) { + throwRangeError(lexicalGlobalObject, throwScope, "Invalid array length"); + return JSValue::encode(jsUndefined()); + } + + auto arrayBuffer = JSC::ArrayBuffer::tryCreate(length, 1); + if (!arrayBuffer) { + throwOutOfMemoryError(lexicalGlobalObject, throwScope); + return JSValue::encode(jsUndefined()); + } + + auto uint8Array = JSC::JSUint8Array::create(lexicalGlobalObject, lexicalGlobalObject->typedArrayStructure(JSC::TypeUint8), WTFMove(arrayBuffer), 0, length); + uint8Array->setPrototypeDirect(vm, JSBuffer::prototype(vm, *jsCast<JSDOMGlobalObject*>(lexicalGlobalObject))); + + return JSC::JSValue::encode(uint8Array); +} + +// new Buffer(string[, encoding]) +static inline EncodedJSValue constructBufferFromStringAndEncoding(JSGlobalObject* lexicalGlobalObject, CallFrame* callFrame) +{ + VM& vm = lexicalGlobalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + auto* castedThis = jsCast<JSBufferConstructor*>(callFrame->jsCallee()); + ASSERT(castedThis); + EnsureStillAliveScope string = callFrame->uncheckedArgument(0); + + JSC::JSValue optionalEncodingValue = jsUndefined(); + if (callFrame->argumentCount() > 1) { + optionalEncodingValue = callFrame->uncheckedArgument(1); + } +} + +template<> EncodedJSValue JSC_HOST_CALL_ATTRIBUTES JSBufferConstructor::construct(JSGlobalObject* lexicalGlobalObject, CallFrame* callFrame) +{ + VM& vm = lexicalGlobalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + UNUSED_PARAM(throwScope); + size_t argsCount = std::min<size_t>(3, callFrame->argumentCount()); + if (argsCount == 0) { + RELEASE_AND_RETURN(throwScope, (constructBufferEmpty(lexicalGlobalObject, callFrame))); + } + JSValue distinguishingArg = callFrame->uncheckedArgument(0); + if (distinguishingArg.isNumber()) { + RELEASE_AND_RETURN(throwScope, (constructBufferFromLength(lexicalGlobalObject, callFrame))); + } + // if (argsCount == 2) { + + // if (distinguishingArg.isUndefined()) + // RELEASE_AND_RETURN(throwScope, (constructJSBuffer1(lexicalGlobalObject, callFrame))); + // if (distinguishingArg.isObject() && asObject(distinguishingArg)->inherits<JSBuffer>(vm)) + // RELEASE_AND_RETURN(throwScope, (constructJSBuffer2(lexicalGlobalObject, callFrame))); + // RELEASE_AND_RETURN(throwScope, (constructJSBuffer1(lexicalGlobalObject, callFrame))); + // } + return argsCount < 1 ? throwVMError(lexicalGlobalObject, throwScope, createNotEnoughArgumentsError(lexicalGlobalObject)) : throwVMTypeError(lexicalGlobalObject, throwScope); +} +JSC_ANNOTATE_HOST_FUNCTION(JSBufferConstructorConstruct, JSBufferConstructor::construct); + +template<> const ClassInfo JSBufferConstructor::s_info = { "Buffer"_s, nullptr, nullptr, nullptr, CREATE_METHOD_TABLE(JSBufferConstructor) }; + +template<> JSValue JSBufferConstructor::prototypeForStructure(JSC::VM& vm, const JSDOMGlobalObject& globalObject) +{ + UNUSED_PARAM(vm); + return globalObject.functionPrototype(); +} + +template<> void JSBufferConstructor::initializeProperties(VM& vm, JSDOMGlobalObject& globalObject) +{ + putDirect(vm, vm.propertyNames->length, jsNumber(1), JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum); + JSString* nameString = jsNontrivialString(vm, "Buffer"_s); + m_originalName.set(vm, this, nameString); + putDirect(vm, vm.propertyNames->name, nameString, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum); + putDirect(vm, vm.propertyNames->prototype, JSBuffer::prototype(vm, globalObject), JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::DontDelete); + reifyStaticProperties(vm, JSBuffer::info(), JSBufferConstructorTableValues, *this); +} + +bool JSBuffer__isBuffer(JSC::JSGlobalObject* global, JSC::EncodedJSValue value) +{ + VM& vm = global->vm(); + auto* jsBuffer = jsDynamicCast<JSBuffer*>(vm, JSValue::decode(value)); + return !!jsBuffer; +} + +const ClassInfo JSBuffer::s_info = { "Buffer"_s, JSC::getUint8ArrayClassInfo(), nullptr, nullptr, CREATE_METHOD_TABLE(JSBuffer) }; + +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_compareBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_copyBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_equalsBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_fillBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_includesBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + auto throwScope = DECLARE_THROW_SCOPE(vm); + UNUSED_PARAM(throwScope); + UNUSED_PARAM(callFrame); + + return JSC::JSValue::encode(JSC::JSValue(reinterpret_cast<uint8_t*>(castedThis->vector())[0])); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_indexOfBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_lastIndexOfBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readBigInt64BEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readBigInt64LEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readBigUInt64BEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readBigUInt64LEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readDoubleBEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readDoubleLEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readFloatBEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readFloatLEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readInt16BEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readInt16LEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readInt32BEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readInt32LEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readInt8Body(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readIntBEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readIntLEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readUInt16BEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readUInt16LEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readUInt32BEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readUInt32LEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readUInt8Body(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readUIntBEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_readUIntLEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_swap16Body(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_swap32Body(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_swap64Body(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_toStringBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + uint32_t offset = 0; + uint32_t length = castedThis->length(); + WebCore::BufferEncodingType encoding = WebCore::BufferEncodingType::utf8; + + auto scope = DECLARE_THROW_SCOPE(vm); + + if (UNLIKELY(callFrame->argumentCount() == 0)) { + throwTypeError(lexicalGlobalObject, scope, "Not enough arguments"); + return JSC::JSValue::encode(jsUndefined()); + } + + EnsureStillAliveScope arg0 = callFrame->argument(0); + auto* str = arg0.value().toStringOrNull(lexicalGlobalObject); + if (!str) { + throwTypeError(lexicalGlobalObject, scope, "write() expects a string"); + return JSC::JSValue::encode(jsUndefined()); + } + + EnsureStillAliveScope arg1 = callFrame->argument(1); + + if (str->length() == 0) + return JSC::JSValue::encode(JSC::jsNumber(0)); + + if (callFrame->argumentCount() > 1) { + if (arg1.value().isAnyInt()) { + int32_t ioffset = arg1.value().toInt32(lexicalGlobalObject); + if (ioffset < 0) { + throwTypeError(lexicalGlobalObject, scope, "Offset must be a positive integer"); + return JSC::JSValue::encode(jsUndefined()); + } + offset = static_cast<uint32_t>(ioffset); + } else if (arg1.value().isString()) { + std::optional<BufferEncodingType> encoded = parseEnumeration<BufferEncodingType>(*lexicalGlobalObject, arg1.value()); + if (!encoded) { + throwTypeError(lexicalGlobalObject, scope, "Invalid encoding"); + return JSC::JSValue::encode(jsUndefined()); + } + + encoding = encoded.value(); + } + } + + if (callFrame->argumentCount() > 2) { + length = static_cast<uint32_t>(callFrame->argument(2).toInt32(lexicalGlobalObject)); + } + + length -= std::min(offset, length); + + if (UNLIKELY(length < offset)) { + RELEASE_AND_RETURN(scope, JSC::JSValue::encode(JSC::jsNumber(0))); + } + + if (callFrame->argumentCount() > 2) { + std::optional<BufferEncodingType> encoded = parseEnumeration<BufferEncodingType>(*lexicalGlobalObject, callFrame->argument(3)); + if (!encoded) { + throwTypeError(lexicalGlobalObject, scope, "Invalid encoding"); + return JSC::JSValue::encode(jsUndefined()); + } + + encoding = encoded.value(); + } + + auto view = str->tryGetValue(lexicalGlobalObject); + int64_t written = 0; + + switch (encoding) { + case WebCore::BufferEncodingType::utf8: { + if (view.is8Bit()) { + written = Bun__encoding__writeLatin1AsUTF8(view.characters8(), view.length(), castedThis->typedVector() + offset, length); + } else { + written = Bun__encoding__writeUTF16AsUTF8(view.characters16(), view.length(), castedThis->typedVector() + offset, length); + } + break; + } + + case WebCore::BufferEncodingType::latin1: + case WebCore::BufferEncodingType::ascii: { + if (view.is8Bit()) { + written = Bun__encoding__writeLatin1AsASCII(view.characters8(), view.length(), castedThis->typedVector() + offset, length); + } else { + written = Bun__encoding__writeUTF16AsASCII(view.characters16(), view.length(), castedThis->typedVector() + offset, length); + } + break; + } + case WebCore::BufferEncodingType::ucs2: + case WebCore::BufferEncodingType::utf16le: { + if (view.is8Bit()) { + written = Bun__encoding__writeLatin1AsUTF16(view.characters8(), view.length(), castedThis->typedVector() + offset, length); + } else { + written = Bun__encoding__writeUTF16AsUTF16(view.characters16(), view.length(), castedThis->typedVector() + offset, length); + } + break; + } + + case WebCore::BufferEncodingType::base64: { + if (view.is8Bit()) { + written = Bun__encoding__writeLatin1AsBase64(view.characters8(), view.length(), castedThis->typedVector() + offset, length); + } else { + written = Bun__encoding__writeUTF16AsBase64(view.characters16(), view.length(), castedThis->typedVector() + offset, length); + } + break; + } + + case WebCore::BufferEncodingType::base64url: { + if (view.is8Bit()) { + written = Bun__encoding__writeLatin1AsURLSafeBase64(view.characters8(), view.length(), castedThis->typedVector() + offset, length); + } else { + written = Bun__encoding__writeUTF16AsURLSafeBase64(view.characters16(), view.length(), castedThis->typedVector() + offset, length); + } + break; + } + + case WebCore::BufferEncodingType::hex: { + if (view.is8Bit()) { + written = Bun__encoding__writeLatin1AsHex(view.characters8(), view.length(), castedThis->typedVector() + offset, length); + } else { + written = Bun__encoding__writeUTF16AsHex(view.characters16(), view.length(), castedThis->typedVector() + offset, length); + } + break; + } + } + + RELEASE_AND_RETURN(scope, JSC::JSValue::encode(JSC::jsNumber(written))); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeBigInt64BEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeBigInt64LEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeBigUInt64BEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeBigUInt64LEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeDoubleBEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeDoubleLEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeFloatBEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeFloatLEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeInt16BEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeInt16LEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeInt32BEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeInt32LEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeInt8Body(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeIntBEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeIntLEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeUInt16BEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeUInt16LEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeUInt32BEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeUInt32LEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeUInt8Body(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeUIntBEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} +static inline JSC::EncodedJSValue jsBufferPrototypeFunction_writeUIntLEBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSBuffer>::ClassParameter castedThis) +{ + auto& vm = JSC::getVM(lexicalGlobalObject); + return JSC::JSValue::encode(jsUndefined()); +} + +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_compare, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_compareBody>(*lexicalGlobalObject, *callFrame, "compare"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_copy, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_copyBody>(*lexicalGlobalObject, *callFrame, "copy"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_equals, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_equalsBody>(*lexicalGlobalObject, *callFrame, "equals"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_fill, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_fillBody>(*lexicalGlobalObject, *callFrame, "fill"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_includes, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_includesBody>(*lexicalGlobalObject, *callFrame, "includes"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_indexOf, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_indexOfBody>(*lexicalGlobalObject, *callFrame, "indexOf"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_lastIndexOf, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_lastIndexOfBody>(*lexicalGlobalObject, *callFrame, "lastIndexOf"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readBigInt64BE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readBigInt64BEBody>(*lexicalGlobalObject, *callFrame, "readBigInt64BE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readBigInt64LE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readBigInt64LEBody>(*lexicalGlobalObject, *callFrame, "readBigInt64LE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readBigUInt64BE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readBigUInt64BEBody>(*lexicalGlobalObject, *callFrame, "readBigUInt64BE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readBigUInt64LE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readBigUInt64LEBody>(*lexicalGlobalObject, *callFrame, "readBigUInt64LE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readDoubleBE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readDoubleBEBody>(*lexicalGlobalObject, *callFrame, "readDoubleBE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readDoubleLE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readDoubleLEBody>(*lexicalGlobalObject, *callFrame, "readDoubleLE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readFloatBE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readFloatBEBody>(*lexicalGlobalObject, *callFrame, "readFloatBE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readFloatLE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readFloatLEBody>(*lexicalGlobalObject, *callFrame, "readFloatLE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readInt16BE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readInt16BEBody>(*lexicalGlobalObject, *callFrame, "readInt16BE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readInt16LE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readInt16LEBody>(*lexicalGlobalObject, *callFrame, "readInt16LE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readInt32BE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readInt32BEBody>(*lexicalGlobalObject, *callFrame, "readInt32BE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readInt32LE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readInt32LEBody>(*lexicalGlobalObject, *callFrame, "readInt32LE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readInt8, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readInt8Body>(*lexicalGlobalObject, *callFrame, "readInt8"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readIntBE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readIntBEBody>(*lexicalGlobalObject, *callFrame, "readIntBE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readIntLE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readIntLEBody>(*lexicalGlobalObject, *callFrame, "readIntLE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readUInt16BE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readUInt16BEBody>(*lexicalGlobalObject, *callFrame, "readUInt16BE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readUInt16LE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readUInt16LEBody>(*lexicalGlobalObject, *callFrame, "readUInt16LE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readUInt32BE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readUInt32BEBody>(*lexicalGlobalObject, *callFrame, "readUInt32BE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readUInt32LE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readUInt32LEBody>(*lexicalGlobalObject, *callFrame, "readUInt32LE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readUInt8, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readUInt8Body>(*lexicalGlobalObject, *callFrame, "readUInt8"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readUIntBE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readUIntBEBody>(*lexicalGlobalObject, *callFrame, "readUIntBE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_readUIntLE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_readUIntLEBody>(*lexicalGlobalObject, *callFrame, "readUIntLE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_swap16, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_swap16Body>(*lexicalGlobalObject, *callFrame, "swap16"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_swap32, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_swap32Body>(*lexicalGlobalObject, *callFrame, "swap32"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_swap64, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_swap64Body>(*lexicalGlobalObject, *callFrame, "swap64"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_toString, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_toStringBody>(*lexicalGlobalObject, *callFrame, "toString"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_write, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeBody>(*lexicalGlobalObject, *callFrame, "write"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeBigInt64BE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeBigInt64BEBody>(*lexicalGlobalObject, *callFrame, "writeBigInt64BE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeBigInt64LE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeBigInt64LEBody>(*lexicalGlobalObject, *callFrame, "writeBigInt64LE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeBigUInt64BE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeBigUInt64BEBody>(*lexicalGlobalObject, *callFrame, "writeBigUInt64BE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeBigUInt64LE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeBigUInt64LEBody>(*lexicalGlobalObject, *callFrame, "writeBigUInt64LE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeDoubleBE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeDoubleBEBody>(*lexicalGlobalObject, *callFrame, "writeDoubleBE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeDoubleLE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeDoubleLEBody>(*lexicalGlobalObject, *callFrame, "writeDoubleLE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeFloatBE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeFloatBEBody>(*lexicalGlobalObject, *callFrame, "writeFloatBE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeFloatLE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeFloatLEBody>(*lexicalGlobalObject, *callFrame, "writeFloatLE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeInt16BE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeInt16BEBody>(*lexicalGlobalObject, *callFrame, "writeInt16BE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeInt16LE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeInt16LEBody>(*lexicalGlobalObject, *callFrame, "writeInt16LE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeInt32BE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeInt32BEBody>(*lexicalGlobalObject, *callFrame, "writeInt32BE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeInt32LE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeInt32LEBody>(*lexicalGlobalObject, *callFrame, "writeInt32LE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeInt8, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeInt8Body>(*lexicalGlobalObject, *callFrame, "writeInt8"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeIntBE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeIntBEBody>(*lexicalGlobalObject, *callFrame, "writeIntBE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeIntLE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeIntLEBody>(*lexicalGlobalObject, *callFrame, "writeIntLE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeUInt16BE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeUInt16BEBody>(*lexicalGlobalObject, *callFrame, "writeUInt16BE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeUInt16LE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeUInt16LEBody>(*lexicalGlobalObject, *callFrame, "writeUInt16LE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeUInt32BE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeUInt32BEBody>(*lexicalGlobalObject, *callFrame, "writeUInt32BE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeUInt32LE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeUInt32LEBody>(*lexicalGlobalObject, *callFrame, "writeUInt32LE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeUInt8, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeUInt8Body>(*lexicalGlobalObject, *callFrame, "writeUInt8"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeUIntBE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeUIntBEBody>(*lexicalGlobalObject, *callFrame, "writeUIntBE"); +} +JSC_DEFINE_HOST_FUNCTION(jsBufferPrototypeFunction_writeUIntLE, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + return IDLOperation<JSBuffer>::call<jsBufferPrototypeFunction_writeUIntLEBody>(*lexicalGlobalObject, *callFrame, "writeUIntLE"); +} + +/* Hash table for prototype */ + +static const HashTableValue JSBufferPrototypeTableValues[] + = { + { "compare", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_compare), (intptr_t)(5) } }, + { "copy", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_copy), (intptr_t)(4) } }, + { "equals", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_equals), (intptr_t)(1) } }, + { "fill", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_fill), (intptr_t)(4) } }, + { "includes", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_includes), (intptr_t)(3) } }, + { "indexOf", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_indexOf), (intptr_t)(3) } }, + { "lastIndexOf", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_lastIndexOf), (intptr_t)(3) } }, + { "readBigInt64BE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readBigInt64BE), (intptr_t)(2) } }, + { "readBigInt64LE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readBigInt64LE), (intptr_t)(2) } }, + { "readBigUInt64BE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readBigUInt64BE), (intptr_t)(2) } }, + { "readBigUInt64LE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readBigUInt64LE), (intptr_t)(2) } }, + { "readDoubleBE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readDoubleBE), (intptr_t)(2) } }, + { "readDoubleLE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readDoubleLE), (intptr_t)(2) } }, + { "readFloatBE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readFloatBE), (intptr_t)(2) } }, + { "readFloatLE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readFloatLE), (intptr_t)(2) } }, + { "readInt16BE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readInt16BE), (intptr_t)(2) } }, + { "readInt16LE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readInt16LE), (intptr_t)(2) } }, + { "readInt32BE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readInt32BE), (intptr_t)(2) } }, + { "readInt32LE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readInt32LE), (intptr_t)(2) } }, + { "readInt8", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readInt8), (intptr_t)(2) } }, + { "readIntBE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readIntBE), (intptr_t)(2) } }, + { "readIntLE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readIntLE), (intptr_t)(2) } }, + { "readUInt16BE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readUInt16BE), (intptr_t)(2) } }, + { "readUInt16LE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readUInt16LE), (intptr_t)(2) } }, + { "readUInt32BE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readUInt32BE), (intptr_t)(2) } }, + { "readUInt32LE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readUInt32LE), (intptr_t)(2) } }, + { "readUInt8", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readUInt8), (intptr_t)(2) } }, + { "readUIntBE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readUIntBE), (intptr_t)(2) } }, + { "readUIntLE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_readUIntLE), (intptr_t)(2) } }, + { "swap16", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_swap16), (intptr_t)(0) } }, + { "swap32", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_swap32), (intptr_t)(0) } }, + { "swap64", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_swap64), (intptr_t)(0) } }, + { "toString", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_toString), (intptr_t)(4) } }, + { "write", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_write), (intptr_t)(4) } }, + { "writeBigInt64BE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeBigInt64BE), (intptr_t)(2) } }, + { "writeBigInt64LE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeBigInt64LE), (intptr_t)(2) } }, + { "writeBigUInt64BE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeBigUInt64BE), (intptr_t)(2) } }, + { "writeBigUInt64LE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeBigUInt64LE), (intptr_t)(2) } }, + { "writeDoubleBE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeDoubleBE), (intptr_t)(2) } }, + { "writeDoubleLE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeDoubleLE), (intptr_t)(2) } }, + { "writeFloatBE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeFloatBE), (intptr_t)(2) } }, + { "writeFloatLE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeFloatLE), (intptr_t)(2) } }, + { "writeInt16BE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeInt16BE), (intptr_t)(2) } }, + { "writeInt16LE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeInt16LE), (intptr_t)(2) } }, + { "writeInt32BE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeInt32BE), (intptr_t)(2) } }, + { "writeInt32LE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeInt32LE), (intptr_t)(2) } }, + { "writeInt8", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeInt8), (intptr_t)(2) } }, + { "writeIntBE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeIntBE), (intptr_t)(2) } }, + { "writeIntLE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeIntLE), (intptr_t)(2) } }, + { "writeUInt16BE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeUInt16BE), (intptr_t)(2) } }, + { "writeUInt16LE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeUInt16LE), (intptr_t)(2) } }, + { "writeUInt32BE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeUInt32BE), (intptr_t)(2) } }, + { "writeUInt32LE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeUInt32LE), (intptr_t)(2) } }, + { "writeUInt8", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeUInt8), (intptr_t)(2) } }, + { "writeUIntBE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeUIntBE), (intptr_t)(2) } }, + { "writeUIntLE", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t) static_cast<RawNativeFunction>(jsBufferPrototypeFunction_writeUIntLE), (intptr_t)(2) } }, + }; + +void JSBufferPrototype::finishCreation(VM& vm, JSC::JSGlobalObject* globalThis) +{ + Base::finishCreation(vm); + reifyStaticProperties(vm, JSBuffer::info(), JSBufferPrototypeTableValues, *this); + JSC_TO_STRING_TAG_WITHOUT_TRANSITION(); + this->setPrototypeDirect(vm, globalThis->m_typedArrayUint8.prototype(globalThis)); +} + +const ClassInfo JSBufferPrototype::s_info = { "Buffer"_s, nullptr, nullptr, nullptr, CREATE_METHOD_TABLE(JSBufferPrototype) }; + +JSObject* JSBuffer::createPrototype(VM& vm, JSDOMGlobalObject& globalObject) +{ + return JSBufferPrototype::create(vm, &globalObject, JSBufferPrototype::createStructure(vm, &globalObject, globalObject.m_typedArrayUint8.prototype(&globalObject))); +} + +JSObject* JSBuffer::prototype(VM& vm, JSDOMGlobalObject& globalObject) +{ + return getDOMPrototype<JSBuffer>(vm, globalObject); +} + +JSValue JSBuffer::getConstructor(VM& vm, const JSGlobalObject* globalObject) +{ + return getDOMConstructor<JSBufferConstructor, DOMConstructorID::Buffer>(vm, *jsCast<const JSDOMGlobalObject*>(globalObject)); +} + +void JSBuffer::destroy(JSC::JSCell* cell) +{ + JSBuffer* thisObject = static_cast<JSBuffer*>(cell); + thisObject->JSBuffer::~JSBuffer(); +} + +JSBuffer::JSBuffer(Structure* structure, JSDOMGlobalObject& globalObject, Ref<Buffer>&& impl) + : JSDOMWrapper<Buffer>(structure, globalObject, WTFMove(impl)) +{ +} + +void JSBuffer::finishCreation(VM& vm) +{ + Base::finishCreation(vm); + ASSERT(inherits(vm, info())); + + // static_assert(!std::is_base_of<ActiveDOMObject, DOMURL>::value, "Interface is not marked as [ActiveDOMObject] even though implementation class subclasses ActiveDOMObject."); +} + +JSC::GCClient::IsoSubspace* JSBuffer::subspaceForImpl(JSC::VM& vm) +{ + return WebCore::subspaceForImpl<JSBuffer, UseCustomHeapCellType::No>( + vm, + [](auto& spaces) { return spaces.m_clientSubspaceForBuffer.get(); }, + [](auto& spaces, auto&& space) { spaces.m_clientSubspaceForBuffer = WTFMove(space); }, + [](auto& spaces) { return spaces.m_subspaceForBuffer.get(); }, + [](auto& spaces, auto&& space) { spaces.m_subspaceForBuffer = WTFMove(space); }); +} + +// template<typename Visitor> +// void JSBuffer::visitChildrenImpl(JSCell* cell, Visitor& visitor) +// { +// auto* thisObject = jsCast<Buffer*>(cell); +// ASSERT_GC_OBJECT_INHERITS(thisObject, info()); +// Base::visitChildren(thisObject, visitor); +// } + +// DEFINE_VISIT_CHILDREN(JSBuffer); + +// template<typename Visitor> +// void JSBuffer::visitOutputConstraints(JSCell* cell, Visitor& visitor) +// { +// auto* thisObject = jsCast<Buffer*>(cell); +// ASSERT_GC_OBJECT_INHERITS(thisObject, info()); +// Base::visitOutputConstraints(thisObject, visitor); +// } + +// template void JSBuffer::visitOutputConstraints(JSCell*, AbstractSlotVisitor&); +// template void JSBuffer::visitOutputConstraints(JSCell*, SlotVisitor&); +// void JSBuffer::analyzeHeap(JSCell* cell, HeapAnalyzer& analyzer) +// { +// auto* thisObject = jsCast<Buffer*>(cell); +// analyzer.setWrappedObjectForCell(cell, &thisObject->wrapped()); +// // if (thisObject->scriptExecutionContext()) +// // analyzer.setLabelForCell(cell, "url " + thisObject->scriptExecutionContext()->url().string()); +// Base::analyzeHeap(cell, analyzer); +// } + +JSBufferOwner::~JSBufferOwner() +{ +} + +bool JSBufferOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, AbstractSlotVisitor& visitor, const char** reason) +{ + UNUSED_PARAM(handle); + UNUSED_PARAM(visitor); + UNUSED_PARAM(reason); + return false; +} + +void JSBufferOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context) +{ + auto* jsBuffer = static_cast<JSBuffer*>(handle.slot()->asCell()); + auto& world = *static_cast<DOMWrapperWorld*>(context); + uncacheWrapper(world, &jsBuffer->wrapped(), jsBuffer); +} + +JSC::JSValue toJSNewlyCreated(JSC::JSGlobalObject*, JSDOMGlobalObject* globalObject, Ref<Buffer>&& impl) +{ + + return createWrapper<Buffer>(globalObject, WTFMove(impl)); +} + +JSC::JSValue toJS(JSC::JSGlobalObject* lexicalGlobalObject, JSDOMGlobalObject* globalObject, Buffer& impl) +{ + return wrap(lexicalGlobalObject, globalObject, impl); +} + +Buffer* JSBuffer::toWrapped(JSC::VM& vm, JSC::JSValue value) +{ + if (auto* wrapper = jsDynamicCast<JSBuffer*>(vm, value)) + return &wrapper->wrapped(); + return nullptr; +} + +} // namespace WebCore
\ No newline at end of file diff --git a/src/javascript/jsc/bindings/JSBuffer.h b/src/javascript/jsc/bindings/JSBuffer.h new file mode 100644 index 000000000..52c48e945 --- /dev/null +++ b/src/javascript/jsc/bindings/JSBuffer.h @@ -0,0 +1,109 @@ +/* + This file is part of the WebKit open source project. + This file has been generated by generate-bindings.pl. DO NOT MODIFY! + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#pragma once + +#include "root.h" + +#include "JSDOMWrapper.h" +#include "wtf/NeverDestroyed.h" + +#include "BufferEncodingType.h" +#include "Buffer.h" + +extern "C" bool JSBuffer__isBuffer(JSC::JSGlobalObject*, JSC::EncodedJSValue); + +namespace WebCore { + +class WEBCORE_EXPORT JSBuffer final : public JSDOMWrapper<Buffer> { +public: + using Base = JSDOMWrapper<Buffer>; + + static constexpr JSC::TypedArrayType TypedArrayStorageType = JSC::JSUint8Array::Adaptor::typeValue; + static JSBuffer* create(JSC::Structure* structure, JSDOMGlobalObject* globalObject, Ref<Buffer>&& impl) + { + JSBuffer* ptr = new (NotNull, JSC::allocateCell<JSBuffer>(globalObject->vm())) JSBuffer(structure, *globalObject, WTFMove(impl)); + ptr->finishCreation(globalObject->vm()); + return ptr; + } + + static JSC::JSObject* createPrototype(JSC::VM&, JSDOMGlobalObject&); + static JSC::JSObject* prototype(JSC::VM&, JSDOMGlobalObject&); + static Buffer* toWrapped(JSC::VM&, JSC::JSValue); + static void destroy(JSC::JSCell*); + + using Adaptor = JSC::JSUint8Array::Adaptor; + + DECLARE_INFO; + + 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(), JSC::NonArray); + } + + static JSC::JSValue getConstructor(JSC::VM&, const JSC::JSGlobalObject*); + mutable JSC::WriteBarrier<JSC::Unknown> m_searchParams; + template<typename, JSC::SubspaceAccess mode> static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm) + { + if constexpr (mode == JSC::SubspaceAccess::Concurrently) + return nullptr; + return subspaceForImpl(vm); + } + static JSC::GCClient::IsoSubspace* subspaceForImpl(JSC::VM& vm); + // DECLARE_VISIT_CHILDREN; + // template<typename Visitor> void visitAdditionalChildren(Visitor&); + // template<typename Visitor> static void visitOutputConstraints(JSCell*, Visitor&); + // static void analyzeHeap(JSCell*, JSC::HeapAnalyzer&); + +protected: + JSBuffer(JSC::Structure*, JSDOMGlobalObject&, Ref<Buffer>&&); + + void finishCreation(JSC::VM&); +}; + +class JSBufferOwner final : public JSC::WeakHandleOwner { +public: + ~JSBufferOwner() final; + bool isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::AbstractSlotVisitor&, const char**) final; + void finalize(JSC::Handle<JSC::Unknown>, void* context) final; +}; + +inline JSC::WeakHandleOwner* wrapperOwner(DOMWrapperWorld&, Buffer*) +{ + static NeverDestroyed<JSBufferOwner> owner; + return &owner.get(); +} + +inline void* wrapperKey(Buffer* wrappableObject) +{ + return wrappableObject; +} + +WEBCORE_EXPORT JSC::JSValue toJS(JSC::JSGlobalObject*, JSDOMGlobalObject*, Buffer&); +inline JSC::JSValue toJS(JSC::JSGlobalObject* lexicalGlobalObject, JSDOMGlobalObject* globalObject, Buffer* impl) { return impl ? toJS(lexicalGlobalObject, globalObject, *impl) : JSC::jsNull(); } +JSC::JSValue toJSNewlyCreated(JSC::JSGlobalObject*, JSDOMGlobalObject*, Ref<Buffer>&&); +inline JSC::JSValue toJSNewlyCreated(JSC::JSGlobalObject* lexicalGlobalObject, JSDOMGlobalObject* globalObject, RefPtr<Buffer>&& impl) { return impl ? toJSNewlyCreated(lexicalGlobalObject, globalObject, impl.releaseNonNull()) : JSC::jsNull(); } + +template<> struct JSDOMWrapperConverterTraits<Buffer> { + using WrapperClass = JSBuffer; + using ToWrappedReturnType = Buffer*; +}; + +} // namespace WebCore diff --git a/src/javascript/jsc/bindings/JSBufferEncodingType.cpp b/src/javascript/jsc/bindings/JSBufferEncodingType.cpp new file mode 100644 index 000000000..5d972a419 --- /dev/null +++ b/src/javascript/jsc/bindings/JSBufferEncodingType.cpp @@ -0,0 +1,164 @@ +/* + This file is part of the WebKit open source project. + This file has been generated by generate-bindings.pl. DO NOT MODIFY! + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#pragma once + +#include "config.h" +#include "JSBufferEncodingType.h" + +#include <JavaScriptCore/JSCInlines.h> +#include <JavaScriptCore/JSString.h> +#include <wtf/NeverDestroyed.h> + +namespace WebCore { +using namespace JSC; + +String convertEnumerationToString(BufferEncodingType enumerationValue) +{ + static const NeverDestroyed<String> values[] = { + MAKE_STATIC_STRING_IMPL("utf8"), + MAKE_STATIC_STRING_IMPL("ucs2"), + MAKE_STATIC_STRING_IMPL("utf16le"), + MAKE_STATIC_STRING_IMPL("latin1"), + MAKE_STATIC_STRING_IMPL("ascii"), + MAKE_STATIC_STRING_IMPL("base64"), + MAKE_STATIC_STRING_IMPL("base64url"), + MAKE_STATIC_STRING_IMPL("hex"), + MAKE_STATIC_STRING_IMPL("buffer"), + }; + ASSERT(static_cast<size_t>(enumerationValue) < WTF_ARRAY_LENGTH(values)); + return values[static_cast<size_t>(enumerationValue)]; +} + +template<> JSString* convertEnumerationToJS(JSGlobalObject& lexicalGlobalObject, BufferEncodingType enumerationValue) +{ + return jsStringWithCache(lexicalGlobalObject.vm(), convertEnumerationToString(enumerationValue)); +} + +// this function is mostly copied from node +template<> std::optional<BufferEncodingType> parseEnumeration<BufferEncodingType>(JSGlobalObject& lexicalGlobalObject, JSValue value) +{ + JSC::JSString* str = value.toStringOrNull(&lexicalGlobalObject); + if (!str) + return std::nullopt; + + auto encoding = str->value(&lexicalGlobalObject); + if (encoding.length() < 3) + return std::nullopt; + + switch (encoding[0]) { + case 'u': + case 'U': + // utf8, utf16le + if (encoding[1] == 't' && encoding[2] == 'f') { + // Skip `-` + const size_t skip = encoding[3] == '-' ? 4 : 3; + if (encoding[skip] == '8' && encoding[skip + 1] == '\0') + return BufferEncodingType::utf8; + if (WTF::equalIgnoringASCIICase(encoding.substringSharingImpl(skip, 5), "16le")) + return BufferEncodingType::ucs2; + // ucs2 + } else if (encoding[1] == 'c' && encoding[2] == 's') { + const size_t skip = encoding[3] == '-' ? 4 : 3; + if (encoding[skip] == '2' && encoding[skip + 1] == '\0') + return BufferEncodingType::ucs2; + } + if (WTF::equalIgnoringASCIICase(encoding, "utf8")) + return BufferEncodingType::utf8; + if (WTF::equalIgnoringASCIICase(encoding, "utf-8")) + return BufferEncodingType::utf8; + if (WTF::equalIgnoringASCIICase(encoding, "ucs2")) + return BufferEncodingType::ucs2; + if (WTF::equalIgnoringASCIICase(encoding, "ucs-2")) + return BufferEncodingType::ucs2; + if (WTF::equalIgnoringASCIICase(encoding, "utf16le")) + return BufferEncodingType::ucs2; + if (WTF::equalIgnoringASCIICase(encoding, "utf-16le")) + return BufferEncodingType::ucs2; + break; + + case 'l': + case 'L': + // latin1 + if (encoding[1] == 'a') { + if (WTF::equalIgnoringASCIICase(encoding.substringSharingImpl(2, 4), "tin1")) + return BufferEncodingType::latin1; + } + if (WTF::equalIgnoringASCIICase(encoding, "latin1")) + return BufferEncodingType::latin1; + break; + + case 'b': + case 'B': + // binary is a deprecated alias of latin1 + if (encoding[1] == 'i') { + if (WTF::equalIgnoringASCIICase(encoding.substringSharingImpl(2, 5), "nary")) + return BufferEncodingType::latin1; + // buffer + } else if (encoding[1] == 'u') { + if (WTF::equalIgnoringASCIICase(encoding.substringSharingImpl(2, 5), "ffer")) + return BufferEncodingType::buffer; + // base64 + } else if (encoding[1] == 'a') { + if (WTF::equalIgnoringASCIICase(encoding.substringSharingImpl(2, 5), "se64")) + return BufferEncodingType::base64; + if (WTF::equalIgnoringASCIICase(encoding.substringSharingImpl(2, 8), "se64url")) + return BufferEncodingType::base64url; + } + if (WTF::equalIgnoringASCIICase(encoding, "binary")) + return BufferEncodingType::latin1; // BINARY is a deprecated alias of LATIN1. + if (WTF::equalIgnoringASCIICase(encoding, "buffer")) + return BufferEncodingType::buffer; + if (WTF::equalIgnoringASCIICase(encoding, "base64")) + return BufferEncodingType::base64; + if (WTF::equalIgnoringASCIICase(encoding, "base64url")) + return BufferEncodingType::base64url; + break; + + case 'a': + case 'A': + // ascii + if (encoding[1] == 's') { + if (WTF::equalIgnoringASCIICase(encoding.substringSharingImpl(2, 3), "cii")) + return BufferEncodingType::ascii; + } + if (WTF::equalIgnoringASCIICase(encoding, "ascii")) + return BufferEncodingType::ascii; + break; + + case 'h': + case 'H': + // hex + if (encoding[1] == 'e') + if (encoding[2] == 'x' && encoding[3] == '\0') + return BufferEncodingType::hex; + if (WTF::equalIgnoringASCIICase(encoding, "hex")) + return BufferEncodingType::hex; + break; + } + + return std::nullopt; +} +template<> const char* expectedEnumerationValues<BufferEncodingType>() +{ + return "\"utf8\", \"ucs2\", \"utf16le\", \"latin1\", \"ascii\", \"base64\", \"base64url\", \"hex\", \"buffer\""; +} + +} // namespace WebCore diff --git a/src/javascript/jsc/bindings/JSBufferEncodingType.h b/src/javascript/jsc/bindings/JSBufferEncodingType.h new file mode 100644 index 000000000..ee7880062 --- /dev/null +++ b/src/javascript/jsc/bindings/JSBufferEncodingType.h @@ -0,0 +1,13 @@ +#include "root.h" +#include "BufferEncodingType.h" +#include "JSDOMConvertEnumeration.h" + +namespace WebCore { + +String convertEnumerationToString(BufferEncodingType); +template<> JSC::JSString* convertEnumerationToJS(JSC::JSGlobalObject&, BufferEncodingType); + +template<> std::optional<BufferEncodingType> parseEnumeration<BufferEncodingType>(JSC::JSGlobalObject&, JSC::JSValue); +template<> const char* expectedEnumerationValues<BufferEncodingType>(); + +} // namespace WebCore
\ No newline at end of file diff --git a/src/javascript/jsc/bindings/JSDOMConvertBufferSource+JSBuffer.h b/src/javascript/jsc/bindings/JSDOMConvertBufferSource+JSBuffer.h new file mode 100644 index 000000000..76d05e1b1 --- /dev/null +++ b/src/javascript/jsc/bindings/JSDOMConvertBufferSource+JSBuffer.h @@ -0,0 +1,31 @@ +#pragma once + +#include "BufferSource.h" +#include "IDLTypes.h" +#include "JSDOMConvertBase.h" +#include "JSDOMWrapperCache.h" +#include "JSBuffer.h" + +namespace WebCore { + +struct IDLJSBuffer : IDLTypedArray<WebCore::JSBuffer> { +}; + +template<> struct JSConverter<IDLJSBuffer> { + static constexpr bool needsState = true; + static constexpr bool needsGlobalObject = true; + + template<typename U> + static JSC::JSValue convert(JSC::JSGlobalObject& lexicalGlobalObject, JSDOMGlobalObject& globalObject, const U& value) + { + return toJS(&lexicalGlobalObject, &globalObject, Detail::getPtrOrRef(value)); + } + + template<typename U> + static JSC::JSValue convertNewlyCreated(JSC::JSGlobalObject& lexicalGlobalObject, JSDOMGlobalObject& globalObject, U&& value) + { + return convert(lexicalGlobalObject, globalObject, std::forward<U>(value)); + } +}; + +}
\ No newline at end of file diff --git a/src/javascript/jsc/bindings/ZigGlobalObject.cpp b/src/javascript/jsc/bindings/ZigGlobalObject.cpp index a2e4fc42a..8e522ab85 100644 --- a/src/javascript/jsc/bindings/ZigGlobalObject.cpp +++ b/src/javascript/jsc/bindings/ZigGlobalObject.cpp @@ -88,6 +88,8 @@ #include "JavaScriptCore/RemoteInspectorServer.h" +#include "JSBuffer.h" + using JSGlobalObject = JSC::JSGlobalObject; using Exception = JSC::Exception; using JSValue = JSC::JSValue; @@ -99,6 +101,7 @@ using SourceOrigin = JSC::SourceOrigin; using JSObject = JSC::JSObject; using JSNonFinalObject = JSC::JSNonFinalObject; namespace JSCastingHelpers = JSC::JSCastingHelpers; +using JSBuffer = WebCore::JSBuffer; static bool has_loaded_jsc = false; @@ -325,6 +328,17 @@ void GlobalObject::setConsole(void* console) #pragma mark - Globals +JSC_DECLARE_CUSTOM_GETTER(JSBuffer_getter); + +JSC_DEFINE_CUSTOM_GETTER(JSBuffer_getter, + (JSC::JSGlobalObject * lexicalGlobalObject, JSC::EncodedJSValue thisValue, + JSC::PropertyName)) +{ + Zig::GlobalObject* thisObject = JSC::jsCast<Zig::GlobalObject*>(lexicalGlobalObject); + return JSC::JSValue::encode( + WebCore::JSBuffer::getConstructor(JSC::getVM(lexicalGlobalObject), thisObject)); +} + JSC_DECLARE_CUSTOM_GETTER(JSDOMURL_getter); JSC_DEFINE_CUSTOM_GETTER(JSDOMURL_getter, @@ -874,6 +888,9 @@ void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "ErrorEvent"_s), JSC::CustomGetterSetter::create(vm, JSErrorEvent_getter, nullptr), JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "Buffer"_s), JSC::CustomGetterSetter::create(vm, JSBuffer_getter, nullptr), + JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + extraStaticGlobals.releaseBuffer(); this->setRemoteDebuggingEnabled(true); diff --git a/src/javascript/jsc/bindings/bindings.zig b/src/javascript/jsc/bindings/bindings.zig index 3cec43447..f09d56e3e 100644 --- a/src/javascript/jsc/bindings/bindings.zig +++ b/src/javascript/jsc/bindings/bindings.zig @@ -3461,6 +3461,19 @@ pub const StringView = extern struct { }; }; +pub const WTF = struct { + extern fn WTF__copyLCharsFromUCharSource(dest: [*]u8, source: *const anyopaque, len: usize) void; + + /// This uses SSE2 instructions and/or ARM NEON to copy 16-bit characters efficiently + /// See wtf/Text/ASCIIFastPath.h for details + pub fn copyLCharsFromUCharSource(destination: [*]u8, comptime Source: type, source: Source) void { + if (comptime JSC.is_bindgen) unreachable; + + // This is any alignment + WTF__copyLCharsFromUCharSource(destination, source.ptr, source.len); + } +}; + pub const Callback = struct { // zig: Value, }; diff --git a/src/javascript/jsc/bindings/headers-cpp.h b/src/javascript/jsc/bindings/headers-cpp.h index bd0542757..a0393e454 100644 --- a/src/javascript/jsc/bindings/headers-cpp.h +++ b/src/javascript/jsc/bindings/headers-cpp.h @@ -1,4 +1,4 @@ -//-- AUTOGENERATED FILE -- 1650708320 +//-- AUTOGENERATED FILE -- 1650803913 // clang-format off #pragma once diff --git a/src/javascript/jsc/bindings/headers-handwritten.h b/src/javascript/jsc/bindings/headers-handwritten.h index a190d694a..d955e3138 100644 --- a/src/javascript/jsc/bindings/headers-handwritten.h +++ b/src/javascript/jsc/bindings/headers-handwritten.h @@ -205,4 +205,16 @@ extern "C" const char* Bun__versions_zig; extern "C" void ZigString__free_global(const unsigned char* ptr, size_t len); +extern "C" int64_t Bun__encoding__writeLatin1AsHex(const unsigned char* ptr, size_t len, unsigned char* to, size_t other_len); +extern "C" int64_t Bun__encoding__writeUTF16AsHex(const UChar* ptr, size_t len, unsigned char* to, size_t other_len); +extern "C" int64_t Bun__encoding__writeLatin1AsURLSafeBase64(const unsigned char* ptr, size_t len, unsigned char* to, size_t other_len); +extern "C" int64_t Bun__encoding__writeUTF16AsURLSafeBase64(const UChar* ptr, size_t len, unsigned char* to, size_t other_len); +extern "C" int64_t Bun__encoding__writeLatin1AsBase64(const unsigned char* ptr, size_t len, unsigned char* to, size_t other_len); +extern "C" int64_t Bun__encoding__writeUTF16AsBase64(const UChar* ptr, size_t len, unsigned char* to, size_t other_len); +extern "C" int64_t Bun__encoding__writeLatin1AsUTF16(const unsigned char* ptr, size_t len, unsigned char* to, size_t other_len); +extern "C" int64_t Bun__encoding__writeUTF16AsUTF16(const UChar* ptr, size_t len, unsigned char* to, size_t other_len); +extern "C" int64_t Bun__encoding__writeLatin1AsUTF8(const unsigned char* ptr, size_t len, unsigned char* to, size_t other_len); +extern "C" int64_t Bun__encoding__writeUTF16AsUTF8(const UChar* ptr, size_t len, unsigned char* to, size_t other_len); +extern "C" int64_t Bun__encoding__writeLatin1AsASCII(const unsigned char* ptr, size_t len, unsigned char* to, size_t other_len); +extern "C" int64_t Bun__encoding__writeUTF16AsASCII(const UChar* ptr, size_t len, unsigned char* to, size_t other_len); #endif diff --git a/src/javascript/jsc/bindings/headers.h b/src/javascript/jsc/bindings/headers.h index f45e6fee9..aad13bc5b 100644 --- a/src/javascript/jsc/bindings/headers.h +++ b/src/javascript/jsc/bindings/headers.h @@ -1,5 +1,5 @@ // clang-format: off -//-- AUTOGENERATED FILE -- 1650708320 +//-- AUTOGENERATED FILE -- 1650803913 #pragma once #include <stddef.h> diff --git a/src/javascript/jsc/bindings/webcore/DOMClientIsoSubspaces.h b/src/javascript/jsc/bindings/webcore/DOMClientIsoSubspaces.h index ad1b6a32b..b3f4e6e03 100644 --- a/src/javascript/jsc/bindings/webcore/DOMClientIsoSubspaces.h +++ b/src/javascript/jsc/bindings/webcore/DOMClientIsoSubspaces.h @@ -15,6 +15,10 @@ class DOMClientIsoSubspaces { public: DOMClientIsoSubspaces() = default; + /* --- bun --- */ + std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForBuffer; + /* --- bun --- */ + std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForGlobalObject; std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForDOMException; diff --git a/src/javascript/jsc/bindings/webcore/DOMConstructors.h b/src/javascript/jsc/bindings/webcore/DOMConstructors.h index c58be6584..b6bc299b7 100644 --- a/src/javascript/jsc/bindings/webcore/DOMConstructors.h +++ b/src/javascript/jsc/bindings/webcore/DOMConstructors.h @@ -843,9 +843,14 @@ enum class DOMConstructorID : uint16_t { XPathNSResolver, XPathResult, XSLTProcessor, + + // Bun extras + Buffer }; -static constexpr unsigned numberOfDOMConstructors = 836; +static constexpr unsigned bunExtraConstructors = 1; + +static constexpr unsigned numberOfDOMConstructors = 836 + bunExtraConstructors; class DOMConstructors { WTF_MAKE_NONCOPYABLE(DOMConstructors); diff --git a/src/javascript/jsc/bindings/webcore/DOMIsoSubspaces.h b/src/javascript/jsc/bindings/webcore/DOMIsoSubspaces.h index 75d91c0a2..8a32c3520 100644 --- a/src/javascript/jsc/bindings/webcore/DOMIsoSubspaces.h +++ b/src/javascript/jsc/bindings/webcore/DOMIsoSubspaces.h @@ -15,6 +15,10 @@ class DOMIsoSubspaces { public: DOMIsoSubspaces() = default; + /*-- BUN --*/ + std::unique_ptr<IsoSubspace> m_subspaceForBuffer; + /*-- BUN --*/ + // std::unique_ptr<IsoSubspace> m_subspaceForTouch; // std::unique_ptr<IsoSubspace> m_subspaceForTouchEvent; // std::unique_ptr<IsoSubspace> m_subspaceForTouchList; diff --git a/src/javascript/jsc/bindings/webcore/JSDOMConvert.h b/src/javascript/jsc/bindings/webcore/JSDOMConvert.h index 91cd5602c..f5c32823d 100644 --- a/src/javascript/jsc/bindings/webcore/JSDOMConvert.h +++ b/src/javascript/jsc/bindings/webcore/JSDOMConvert.h @@ -45,3 +45,5 @@ #include "JSDOMConvertStrings.h" #include "JSDOMConvertUnion.h" #include "JSDOMConvertWebGL.h" + +#include "JSDOMConvertBufferSource+JSBuffer.h"
\ No newline at end of file diff --git a/src/javascript/jsc/bindings/webcore/JSDOMConvertUnion.h b/src/javascript/jsc/bindings/webcore/JSDOMConvertUnion.h index db192f7f9..75ce6f86a 100644 --- a/src/javascript/jsc/bindings/webcore/JSDOMConvertUnion.h +++ b/src/javascript/jsc/bindings/webcore/JSDOMConvertUnion.h @@ -29,6 +29,7 @@ #include "JSDOMBinding.h" #include "JSDOMConvertBase.h" #include "JSDOMConvertBufferSource.h" +#include "JSDOMConvertBufferSource+JSBuffer.h" #include "JSDOMConvertInterface.h" #include "JSDOMConvertNull.h" #include "JavaScriptCore/IteratorOperations.h" diff --git a/src/javascript/jsc/bindings/wtf-bindings.cpp b/src/javascript/jsc/bindings/wtf-bindings.cpp new file mode 100644 index 000000000..5dae85930 --- /dev/null +++ b/src/javascript/jsc/bindings/wtf-bindings.cpp @@ -0,0 +1,6 @@ +#include "wtf-bindings.h" + +extern "C" void WTF__copyLCharsFromUCharSource(LChar* destination, const UChar* source, size_t length) +{ + WTF::copyLCharsFromUCharSource(destination, source, length); +}
\ No newline at end of file diff --git a/src/javascript/jsc/bindings/wtf-bindings.h b/src/javascript/jsc/bindings/wtf-bindings.h new file mode 100644 index 000000000..3f71ff0c2 --- /dev/null +++ b/src/javascript/jsc/bindings/wtf-bindings.h @@ -0,0 +1,6 @@ +#pragma once + +#include "root.h" +#include "wtf/text/ASCIIFastPath.h" + +extern "C" void WTF__copyLCharsFromUCharSource(LChar* destination, const UChar* source, size_t length);
\ No newline at end of file diff --git a/src/javascript/jsc/node/types.zig b/src/javascript/jsc/node/types.zig index 36978f7d4..8ba9a5901 100644 --- a/src/javascript/jsc/node/types.zig +++ b/src/javascript/jsc/node/types.zig @@ -187,6 +187,13 @@ pub const Encoding = enum(u8) { /// Refer to the buffer's encoding buffer, + pub fn isBinaryToText(this: Encoding) bool { + return switch (this) { + .hex, .base64, .base64url => true, + else => false, + }; + } + const Eight = strings.ExactSizeMatcher(8); /// Caller must verify the value is a string pub fn fromStringValue(value: JSC.JSValue, global: *JSC.JSGlobalObject) ?Encoding { diff --git a/src/javascript/jsc/webcore/base64.zig b/src/javascript/jsc/webcore/base64.zig new file mode 100644 index 000000000..50c1ac68d --- /dev/null +++ b/src/javascript/jsc/webcore/base64.zig @@ -0,0 +1,445 @@ +// this is ripped from zig's stdlib +const std = @import("std"); +const assert = std.debug.assert; +const testing = std.testing; +const mem = std.mem; + +pub const Error = error{ + InvalidCharacter, + InvalidPadding, + NoSpaceLeft, +}; + +/// Base64 codecs +pub const Codecs = struct { + alphabet_chars: [64]u8, + pad_char: ?u8, + decoderWithIgnore: fn (ignore: []const u8) Base64DecoderWithIgnore, + Encoder: Base64Encoder, + Decoder: Base64Decoder, +}; + +pub const standard_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".*; +fn standardBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore { + return Base64DecoderWithIgnore.init(standard_alphabet_chars, '=', ignore); +} + +/// Standard Base64 codecs, with padding +pub const standard = Codecs{ + .alphabet_chars = standard_alphabet_chars, + .pad_char = '=', + .decoderWithIgnore = standardBase64DecoderWithIgnore, + .Encoder = Base64Encoder.init(standard_alphabet_chars, '='), + .Decoder = Base64Decoder.init(standard_alphabet_chars, '='), +}; + +/// Standard Base64 codecs, without padding +pub const standard_no_pad = Codecs{ + .alphabet_chars = standard_alphabet_chars, + .pad_char = null, + .decoderWithIgnore = standardBase64DecoderWithIgnore, + .Encoder = Base64Encoder.init(standard_alphabet_chars, null), + .Decoder = Base64Decoder.init(standard_alphabet_chars, null), +}; + +pub const url_safe_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".*; +fn urlSafeBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore { + return Base64DecoderWithIgnore.init(url_safe_alphabet_chars, null, ignore); +} + +/// URL-safe Base64 codecs, with padding +pub const url_safe = Codecs{ + .alphabet_chars = url_safe_alphabet_chars, + .pad_char = '=', + .decoderWithIgnore = urlSafeBase64DecoderWithIgnore, + .Encoder = Base64Encoder.init(url_safe_alphabet_chars, '='), + .Decoder = Base64Decoder.init(url_safe_alphabet_chars, '='), +}; + +/// URL-safe Base64 codecs, without padding +pub const url_safe_no_pad = Codecs{ + .alphabet_chars = url_safe_alphabet_chars, + .pad_char = null, + .decoderWithIgnore = urlSafeBase64DecoderWithIgnore, + .Encoder = Base64Encoder.init(url_safe_alphabet_chars, null), + .Decoder = Base64Decoder.init(url_safe_alphabet_chars, null), +}; + +pub const standard_pad_char = @compileError("deprecated; use standard.pad_char"); +pub const standard_encoder = @compileError("deprecated; use standard.Encoder"); +pub const standard_decoder = @compileError("deprecated; use standard.Decoder"); + +pub const Base64Encoder = struct { + alphabet_chars: [64]u8, + pad_char: ?u8, + + /// A bunch of assertions, then simply pass the data right through. + pub fn init(alphabet_chars: [64]u8, pad_char: ?u8) Base64Encoder { + assert(alphabet_chars.len == 64); + var char_in_alphabet = [_]bool{false} ** 256; + for (alphabet_chars) |c| { + assert(!char_in_alphabet[c]); + assert(pad_char == null or c != pad_char.?); + char_in_alphabet[c] = true; + } + return Base64Encoder{ + .alphabet_chars = alphabet_chars, + .pad_char = pad_char, + }; + } + + /// Compute the encoded length + pub fn calcSize(encoder: *const Base64Encoder, source_len: usize) usize { + if (encoder.pad_char != null) { + return @divTrunc(source_len + 2, 3) * 4; + } else { + const leftover = source_len % 3; + return @divTrunc(source_len, 3) * 4 + @divTrunc(leftover * 4 + 2, 3); + } + } + + /// dest.len must at least be what you get from ::calcSize. + pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) []const u8 { + const out_len = encoder.calcSize(source.len); + assert(dest.len >= out_len); + + var acc: u12 = 0; + var acc_len: u4 = 0; + var out_idx: usize = 0; + for (source) |v| { + acc = (acc << 8) + v; + acc_len += 8; + while (acc_len >= 6) { + acc_len -= 6; + dest[out_idx] = encoder.alphabet_chars[@truncate(u6, (acc >> acc_len))]; + out_idx += 1; + } + } + if (acc_len > 0) { + dest[out_idx] = encoder.alphabet_chars[@truncate(u6, (acc << 6 - acc_len))]; + out_idx += 1; + } + if (encoder.pad_char) |pad_char| { + for (dest[out_idx..]) |*pad| { + pad.* = pad_char; + } + } + return dest[0..out_len]; + } +}; + +pub const Base64Decoder = struct { + const invalid_char: u8 = 0xff; + + /// e.g. 'A' => 0. + /// `invalid_char` for any value not in the 64 alphabet chars. + char_to_index: [256]u8, + pad_char: ?u8, + + pub fn init(alphabet_chars: [64]u8, pad_char: ?u8) Base64Decoder { + var result = Base64Decoder{ + .char_to_index = [_]u8{invalid_char} ** 256, + .pad_char = pad_char, + }; + + var char_in_alphabet = [_]bool{false} ** 256; + for (alphabet_chars) |c, i| { + assert(!char_in_alphabet[c]); + assert(pad_char == null or c != pad_char.?); + + result.char_to_index[c] = @intCast(u8, i); + char_in_alphabet[c] = true; + } + return result; + } + + /// Return the maximum possible decoded size for a given input length - The actual length may be less if the input includes padding. + /// `InvalidPadding` is returned if the input length is not valid. + pub fn calcSizeUpperBound(decoder: *const Base64Decoder, source_len: usize) Error!usize { + var result = source_len / 4 * 3; + const leftover = source_len % 4; + if (decoder.pad_char != null) { + if (leftover % 4 != 0) return error.InvalidPadding; + } else { + if (leftover % 4 == 1) return error.InvalidPadding; + result += leftover * 3 / 4; + } + return result; + } + + /// Return the exact decoded size for a slice. + /// `InvalidPadding` is returned if the input length is not valid. + pub fn calcSizeForSlice(decoder: *const Base64Decoder, source: []const u8) Error!usize { + const source_len = source.len; + var result = try decoder.calcSizeUpperBound(source_len); + if (decoder.pad_char) |pad_char| { + if (source_len >= 1 and source[source_len - 1] == pad_char) result -= 1; + if (source_len >= 2 and source[source_len - 2] == pad_char) result -= 1; + } + return result; + } + + /// dest.len must be what you get from ::calcSize. + /// invalid characters result in error.InvalidCharacter. + /// invalid padding results in error.InvalidPadding. + pub fn decode(decoder: *const Base64Decoder, dest: []u8, source: []const u8) Error!void { + if (decoder.pad_char != null and source.len % 4 != 0) return error.InvalidPadding; + var acc: u12 = 0; + var acc_len: u4 = 0; + var dest_idx: usize = 0; + var leftover_idx: ?usize = null; + for (source) |c, src_idx| { + const d = decoder.char_to_index[c]; + if (d == invalid_char) { + if (decoder.pad_char == null or c != decoder.pad_char.?) return error.InvalidCharacter; + leftover_idx = src_idx; + break; + } + acc = (acc << 6) + d; + acc_len += 6; + if (acc_len >= 8) { + acc_len -= 8; + dest[dest_idx] = @truncate(u8, acc >> acc_len); + dest_idx += 1; + } + } + if (acc_len > 4 or (acc & (@as(u12, 1) << acc_len) - 1) != 0) { + return error.InvalidPadding; + } + if (leftover_idx == null) return; + var leftover = source[leftover_idx.?..]; + if (decoder.pad_char) |pad_char| { + const padding_len = acc_len / 2; + var padding_chars: usize = 0; + for (leftover) |c| { + if (c != pad_char) { + return if (c == Base64Decoder.invalid_char) error.InvalidCharacter else error.InvalidPadding; + } + padding_chars += 1; + } + if (padding_chars != padding_len) return error.InvalidPadding; + } + } +}; + +pub const Base64DecoderWithIgnore = struct { + decoder: Base64Decoder, + char_is_ignored: [256]bool, + + pub fn init(alphabet_chars: [64]u8, pad_char: ?u8, ignore_chars: []const u8) Base64DecoderWithIgnore { + var result = Base64DecoderWithIgnore{ + .decoder = Base64Decoder.init(alphabet_chars, pad_char), + .char_is_ignored = [_]bool{false} ** 256, + }; + for (ignore_chars) |c| { + assert(result.decoder.char_to_index[c] == Base64Decoder.invalid_char); + assert(!result.char_is_ignored[c]); + assert(result.decoder.pad_char != c); + result.char_is_ignored[c] = true; + } + return result; + } + + /// Return the maximum possible decoded size for a given input length - The actual length may be less if the input includes padding + /// `InvalidPadding` is returned if the input length is not valid. + pub fn calcSizeUpperBound(decoder_with_ignore: *const Base64DecoderWithIgnore, source_len: usize) Error!usize { + var result = source_len / 4 * 3; + if (decoder_with_ignore.decoder.pad_char == null) { + const leftover = source_len % 4; + result += leftover * 3 / 4; + } + return result; + } + + /// Invalid characters that are not ignored result in error.InvalidCharacter. + /// Invalid padding results in error.InvalidPadding. + /// Decoding more data than can fit in dest results in error.NoSpaceLeft. See also ::calcSizeUpperBound. + /// Returns the number of bytes written to dest. + pub fn decode(decoder_with_ignore: *const Base64DecoderWithIgnore, dest: []u8, source: []const u8) Error!usize { + const decoder = &decoder_with_ignore.decoder; + var acc: u12 = 0; + var acc_len: u4 = 0; + var dest_idx: usize = 0; + var leftover_idx: ?usize = null; + for (source) |c, src_idx| { + if (decoder_with_ignore.char_is_ignored[c]) continue; + const d = decoder.char_to_index[c]; + if (d == Base64Decoder.invalid_char) { + if (decoder.pad_char == null or c != decoder.pad_char.?) return error.InvalidCharacter; + leftover_idx = src_idx; + break; + } + acc = (acc << 6) + d; + acc_len += 6; + if (acc_len >= 8) { + if (dest_idx == dest.len) return error.NoSpaceLeft; + acc_len -= 8; + dest[dest_idx] = @truncate(u8, acc >> acc_len); + dest_idx += 1; + } + } + if (acc_len > 4 or (acc & (@as(u12, 1) << acc_len) - 1) != 0) { + return error.InvalidPadding; + } + const padding_len = acc_len / 2; + if (leftover_idx == null) { + if (decoder.pad_char != null and padding_len != 0) return error.InvalidPadding; + return dest_idx; + } + var leftover = source[leftover_idx.?..]; + if (decoder.pad_char) |pad_char| { + var padding_chars: usize = 0; + for (leftover) |c| { + if (decoder_with_ignore.char_is_ignored[c]) continue; + if (c != pad_char) { + return if (c == Base64Decoder.invalid_char) error.InvalidCharacter else error.InvalidPadding; + } + padding_chars += 1; + } + if (padding_chars != padding_len) return error.InvalidPadding; + } + return dest_idx; + } +}; + +test "base64" { + @setEvalBranchQuota(8000); + try testBase64(); + comptime try testAllApis(standard, "comptime", "Y29tcHRpbWU="); +} + +test "base64 url_safe_no_pad" { + @setEvalBranchQuota(8000); + try testBase64UrlSafeNoPad(); + comptime try testAllApis(url_safe_no_pad, "comptime", "Y29tcHRpbWU"); +} + +fn testBase64() !void { + const codecs = standard; + + try testAllApis(codecs, "", ""); + try testAllApis(codecs, "f", "Zg=="); + try testAllApis(codecs, "fo", "Zm8="); + try testAllApis(codecs, "foo", "Zm9v"); + try testAllApis(codecs, "foob", "Zm9vYg=="); + try testAllApis(codecs, "fooba", "Zm9vYmE="); + try testAllApis(codecs, "foobar", "Zm9vYmFy"); + + try testDecodeIgnoreSpace(codecs, "", " "); + try testDecodeIgnoreSpace(codecs, "f", "Z g= ="); + try testDecodeIgnoreSpace(codecs, "fo", " Zm8="); + try testDecodeIgnoreSpace(codecs, "foo", "Zm9v "); + try testDecodeIgnoreSpace(codecs, "foob", "Zm9vYg = = "); + try testDecodeIgnoreSpace(codecs, "fooba", "Zm9v YmE="); + try testDecodeIgnoreSpace(codecs, "foobar", " Z m 9 v Y m F y "); + + // test getting some api errors + try testError(codecs, "A", error.InvalidPadding); + try testError(codecs, "AA", error.InvalidPadding); + try testError(codecs, "AAA", error.InvalidPadding); + try testError(codecs, "A..A", error.InvalidCharacter); + try testError(codecs, "AA=A", error.InvalidPadding); + try testError(codecs, "AA/=", error.InvalidPadding); + try testError(codecs, "A/==", error.InvalidPadding); + try testError(codecs, "A===", error.InvalidPadding); + try testError(codecs, "====", error.InvalidPadding); + + try testNoSpaceLeftError(codecs, "AA=="); + try testNoSpaceLeftError(codecs, "AAA="); + try testNoSpaceLeftError(codecs, "AAAA"); + try testNoSpaceLeftError(codecs, "AAAAAA=="); +} + +fn testBase64UrlSafeNoPad() !void { + const codecs = url_safe_no_pad; + + try testAllApis(codecs, "", ""); + try testAllApis(codecs, "f", "Zg"); + try testAllApis(codecs, "fo", "Zm8"); + try testAllApis(codecs, "foo", "Zm9v"); + try testAllApis(codecs, "foob", "Zm9vYg"); + try testAllApis(codecs, "fooba", "Zm9vYmE"); + try testAllApis(codecs, "foobar", "Zm9vYmFy"); + + try testDecodeIgnoreSpace(codecs, "", " "); + try testDecodeIgnoreSpace(codecs, "f", "Z g "); + try testDecodeIgnoreSpace(codecs, "fo", " Zm8"); + try testDecodeIgnoreSpace(codecs, "foo", "Zm9v "); + try testDecodeIgnoreSpace(codecs, "foob", "Zm9vYg "); + try testDecodeIgnoreSpace(codecs, "fooba", "Zm9v YmE"); + try testDecodeIgnoreSpace(codecs, "foobar", " Z m 9 v Y m F y "); + + // test getting some api errors + try testError(codecs, "A", error.InvalidPadding); + try testError(codecs, "AAA=", error.InvalidCharacter); + try testError(codecs, "A..A", error.InvalidCharacter); + try testError(codecs, "AA=A", error.InvalidCharacter); + try testError(codecs, "AA/=", error.InvalidCharacter); + try testError(codecs, "A/==", error.InvalidCharacter); + try testError(codecs, "A===", error.InvalidCharacter); + try testError(codecs, "====", error.InvalidCharacter); + + try testNoSpaceLeftError(codecs, "AA"); + try testNoSpaceLeftError(codecs, "AAA"); + try testNoSpaceLeftError(codecs, "AAAA"); + try testNoSpaceLeftError(codecs, "AAAAAA"); +} + +fn testAllApis(codecs: Codecs, expected_decoded: []const u8, expected_encoded: []const u8) !void { + // Base64Encoder + { + var buffer: [0x100]u8 = undefined; + const encoded = codecs.Encoder.encode(&buffer, expected_decoded); + try testing.expectEqualSlices(u8, expected_encoded, encoded); + } + + // Base64Decoder + { + var buffer: [0x100]u8 = undefined; + var decoded = buffer[0..try codecs.Decoder.calcSizeForSlice(expected_encoded)]; + try codecs.Decoder.decode(decoded, expected_encoded); + try testing.expectEqualSlices(u8, expected_decoded, decoded); + } + + // Base64DecoderWithIgnore + { + const decoder_ignore_nothing = codecs.decoderWithIgnore(""); + var buffer: [0x100]u8 = undefined; + var decoded = buffer[0..try decoder_ignore_nothing.calcSizeUpperBound(expected_encoded.len)]; + var written = try decoder_ignore_nothing.decode(decoded, expected_encoded); + try testing.expect(written <= decoded.len); + try testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]); + } +} + +fn testDecodeIgnoreSpace(codecs: Codecs, expected_decoded: []const u8, encoded: []const u8) !void { + const decoder_ignore_space = codecs.decoderWithIgnore(" "); + var buffer: [0x100]u8 = undefined; + var decoded = buffer[0..try decoder_ignore_space.calcSizeUpperBound(encoded.len)]; + var written = try decoder_ignore_space.decode(decoded, encoded); + try testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]); +} + +fn testError(codecs: Codecs, encoded: []const u8, expected_err: anyerror) !void { + const decoder_ignore_space = codecs.decoderWithIgnore(" "); + var buffer: [0x100]u8 = undefined; + if (codecs.Decoder.calcSizeForSlice(encoded)) |decoded_size| { + var decoded = buffer[0..decoded_size]; + if (codecs.Decoder.decode(decoded, encoded)) |_| { + return error.ExpectedError; + } else |err| if (err != expected_err) return err; + } else |err| if (err != expected_err) return err; + + if (decoder_ignore_space.decode(buffer[0..], encoded)) |_| { + return error.ExpectedError; + } else |err| if (err != expected_err) return err; +} + +fn testNoSpaceLeftError(codecs: Codecs, encoded: []const u8) !void { + const decoder_ignore_space = codecs.decoderWithIgnore(" "); + var buffer: [0x100]u8 = undefined; + var decoded = buffer[0 .. (try codecs.Decoder.calcSizeForSlice(encoded)) - 1]; + if (decoder_ignore_space.decode(decoded, encoded)) |_| { + return error.ExpectedError; + } else |err| if (err != error.NoSpaceLeft) return err; +} diff --git a/src/javascript/jsc/webcore/encoding.zig b/src/javascript/jsc/webcore/encoding.zig index cc90f3c9b..78d3f2877 100644 --- a/src/javascript/jsc/webcore/encoding.zig +++ b/src/javascript/jsc/webcore/encoding.zig @@ -12,7 +12,7 @@ const js = JSC.C; const Method = @import("../../../http/method.zig").Method; const ObjectPool = @import("../../../pool.zig").ObjectPool; - +const bun = @import("../../../global.zig"); const Output = @import("../../../global.zig").Output; const MutableString = @import("../../../global.zig").MutableString; const strings = @import("../../../global.zig").strings; @@ -457,23 +457,16 @@ pub const TextDecoder = struct { } var buffer = std.ArrayListAlignedUnmanaged(u16, @alignOf(@TypeOf(slice.ptr))){}; - buffer.ensureTotalCapacity(default_allocator, slice.len) catch unreachable; + // copy the allocator to reduce the number of threadlocal accesses + const allocator = VirtualMachine.vm.allocator; + buffer.ensureTotalCapacity(allocator, slice.len) catch unreachable; buffer.items.len = i; - defer buffer.deinit( - default_allocator, - ); - if (comptime Slice == []u16) { - @memcpy( - std.mem.sliceAsBytes(buffer.items).ptr, - std.mem.sliceAsBytes(slice).ptr, - std.mem.sliceAsBytes(slice[0..i]).len, - ); - } else { - for (slice[0..i]) |ch, j| { - buffer.items[j] = ch; - } - } + @memcpy( + std.mem.sliceAsBytes(buffer.items).ptr, + std.mem.sliceAsBytes(slice).ptr, + std.mem.sliceAsBytes(slice[0..i]).len, + ); const first_high_surrogate = 0xD800; const last_high_surrogate = 0xDBFF; @@ -484,20 +477,24 @@ pub const TextDecoder = struct { while (remainder.len > 0) { switch (remainder[0]) { 0...127 => { - var count: usize = 1; - while (remainder.len > count and remainder[count] <= 127) : (count += 1) {} - buffer.ensureUnusedCapacity(default_allocator, count) catch unreachable; + const count: usize = if (strings.firstNonASCII16CheckMin(Slice, remainder, false)) |index| index + 1 else remainder.len; + + buffer.ensureUnusedCapacity(allocator, count) catch unreachable; + const prev = buffer.items.len; buffer.items.len += count; - for (remainder[0..count]) |char, j| { - buffer.items[prev + j] = char; - } + // Since this string is freshly allocated, we know it's not going to overlap + @memcpy( + std.mem.sliceAsBytes(buffer.items[prev..]).ptr, + std.mem.sliceAsBytes(remainder).ptr, + std.mem.sliceAsBytes(remainder[0..count]).len, + ); remainder = remainder[count..]; }, first_high_surrogate...last_high_surrogate => |first| { if (remainder.len > 1) { if (remainder[1] >= first_low_surrogate and remainder[1] <= last_low_surrogate) { - buffer.ensureUnusedCapacity(default_allocator, 2) catch unreachable; + buffer.ensureUnusedCapacity(allocator, 2) catch unreachable; buffer.items.ptr[buffer.items.len] = first; buffer.items.ptr[buffer.items.len + 1] = remainder[1]; buffer.items.len += 2; @@ -505,7 +502,7 @@ pub const TextDecoder = struct { continue; } } - buffer.ensureUnusedCapacity(default_allocator, 1) catch unreachable; + buffer.ensureUnusedCapacity(allocator, 1) catch unreachable; buffer.items.ptr[buffer.items.len] = strings.unicode_replacement; buffer.items.len += 1; remainder = remainder[1..]; @@ -514,7 +511,7 @@ pub const TextDecoder = struct { // Is this an unpaired low surrogate or four-digit hex escape? else => { - buffer.ensureUnusedCapacity(default_allocator, 1) catch unreachable; + buffer.ensureUnusedCapacity(allocator, 1) catch unreachable; buffer.items.ptr[buffer.items.len] = strings.unicode_replacement; buffer.items.len += 1; remainder = remainder[1..]; @@ -522,9 +519,11 @@ pub const TextDecoder = struct { } } + var full = buffer.toOwnedSlice(allocator); + var out = ZigString.init(""); - out.ptr = @ptrCast([*]u8, buffer.items.ptr); - out.len = buffer.items.len; + out.ptr = @ptrCast([*]u8, full.ptr); + out.len = full.len; out.markUTF16(); return out.toValueGC(ctx.ptr()).asObjectRef(); } @@ -651,4 +650,232 @@ pub const TextDecoder = struct { } }; +pub const Encoder = struct { + export fn Bun__encoding__writeLatin1AsHex(input: [*]const u8, len: usize, to: [*]u8, to_len: usize) i64 { + return writeU8(input, len, to, to_len, .hex); + } + export fn Bun__encoding__writeLatin1AsASCII(input: [*]const u8, len: usize, to: [*]u8, to_len: usize) i64 { + return writeU8(input, len, to, to_len, .ascii); + } + export fn Bun__encoding__writeLatin1AsURLSafeBase64(input: [*]const u8, len: usize, to: [*]u8, to_len: usize) i64 { + return writeU8(input, len, to, to_len, .base64url); + } + export fn Bun__encoding__writeLatin1AsUTF16(input: [*]const u8, len: usize, to: [*]u8, to_len: usize) i64 { + return writeU8(input, len, to, to_len, .utf16le); + } + export fn Bun__encoding__writeLatin1AsUTF8(input: [*]const u8, len: usize, to: [*]u8, to_len: usize) i64 { + return writeU8(input, len, to, to_len, JSC.Node.Encoding.utf8); + } + export fn Bun__encoding__writeLatin1AsBase64(input: [*]const u8, len: usize, to: [*]u8, to_len: usize) i64 { + return writeU8(input, len, to, to_len, .base64); + } + export fn Bun__encoding__writeUTF16AsBase64(input: [*]const u16, len: usize, to: [*]u8, to_len: usize) i64 { + return writeU16(input, len, to, to_len, .base64); + } + export fn Bun__encoding__writeUTF16AsHex(input: [*]const u16, len: usize, to: [*]u8, to_len: usize) i64 { + return writeU16(input, len, to, to_len, .hex); + } + export fn Bun__encoding__writeUTF16AsURLSafeBase64(input: [*]const u16, len: usize, to: [*]u8, to_len: usize) i64 { + return writeU16(input, len, to, to_len, .base64url); + } + export fn Bun__encoding__writeUTF16AsUTF16(input: [*]const u16, len: usize, to: [*]u8, to_len: usize) i64 { + return writeU16(input, len, to, to_len, JSC.Node.Encoding.utf16le); + } + export fn Bun__encoding__writeUTF16AsUTF8(input: [*]const u16, len: usize, to: [*]u8, to_len: usize) i64 { + return writeU16(input, len, to, to_len, .utf8); + } + export fn Bun__encoding__writeUTF16AsASCII(input: [*]const u8, len: usize, to: [*]u8, to_len: usize) i64 { + return writeU8(input, len, to, to_len, .ascii); + } + + // pub fn writeUTF16AsUTF8(utf16: [*]const u16, len: usize, to: [*]u8, to_len: usize) callconv(.C) i32 { + // return @intCast(i32, strings.copyUTF16IntoUTF8(to[0..to_len], []const u16, utf16[0..len]).written); + // } + + // pub fn toString(input: [*]const u8, len: usize, zig_str: *ZigString, comptime encoding: JSC.Node.Encoding) callconv(.C) i64 {} + + pub fn writeU8(input: [*]const u8, len: usize, to: [*]u8, to_len: usize, comptime encoding: JSC.Node.Encoding) i64 { + if (len == 0 or to_len == 0) + return 0; + + // TODO: increase temporary buffer size for larger amounts of data + // defer { + // if (comptime encoding.isBinaryToText()) {} + // } + + // if (comptime encoding.isBinaryToText()) {} + + switch (comptime encoding) { + JSC.Node.Encoding.ascii => { + const written = @truncate(u32, @minimum(len, to_len)); + @memcpy(to, input, written); + return @intCast(i32, written); + }, + .utf8 => { + // need to encode + return @intCast(i32, strings.copyLatin1IntoUTF8(to[0..to_len], []const u8, input[0..len]).written); + }, + // encode latin1 into UTF16 + JSC.Node.Encoding.ucs2, JSC.Node.Encoding.utf16le => { + if (to_len < 2) + return 0; + + if (std.mem.isAligned(@ptrToInt(to), @alignOf([*]u16))) { + var buf = input[0..len]; + var output = @ptrCast([*]u16, @alignCast(@alignOf(u16), to))[0 .. to_len / 2]; + return strings.copyLatin1IntoUTF16([]u16, output, []const u8, buf).written; + } else { + var buf = input[0..len]; + var output = @ptrCast([*]align(1) u16, to)[0 .. to_len / 2]; + return strings.copyLatin1IntoUTF16([]align(1) u16, output, []const u8, buf).written; + } + }, + + JSC.Node.Encoding.hex => { + return @intCast(i64, strings.decodeHexToBytes(to[0..to_len], u8, input[0..len])); + }, + + JSC.Node.Encoding.base64url => { + var slice = strings.trim(input[0..len], "\r\n\t " ++ [_]u8{std.ascii.control_code.VT}); + if (slice.len == 0) + return 0; + + if (strings.eqlComptime(slice[slice.len - 2 ..][0..2], "==")) { + slice = slice[0 .. slice.len - 2]; + } else if (slice[slice.len - 1] == '=') { + slice = slice[0 .. slice.len - 1]; + } + + const wrote = bun.base64.urlsafe.decode(to[0..to_len], slice) catch |err| brk: { + if (err == error.NoSpaceLeft) { + break :brk to_len; + } + + return -1; + }; + return @intCast(i64, wrote); + }, + + JSC.Node.Encoding.base64 => { + var slice = strings.trim(input[0..len], "\r\n\t " ++ [_]u8{std.ascii.control_code.VT}); + var outlen = bun.base64.decodeLen(slice); + + return @intCast(i64, bun.base64.decode(to[0..outlen], slice).written); + }, + else => return 0, + } + } + + pub fn writeU16(input: [*]const u16, len: usize, to: [*]u8, to_len: usize, comptime encoding: JSC.Node.Encoding) i64 { + + // TODO: increase temporary buffer size for larger amounts of data + // defer { + // if (comptime encoding.isBinaryToText()) {} + // } + + // if (comptime encoding.isBinaryToText()) {} + + switch (comptime encoding) { + .utf8 => { + return @intCast(i32, strings.copyUTF16IntoUTF8(to[0..to_len], []const u16, input[0..len]).written); + }, + // string is already encoded, just need to copy the data + JSC.Node.Encoding.ucs2, JSC.Node.Encoding.ascii, JSC.Node.Encoding.utf16le => { + strings.copyU16IntoU8(to[0..to_len], []const u16, input[0..len]); + + return @intCast(i64, @minimum(len, to_len)); + }, + + JSC.Node.Encoding.hex => { + return @intCast(i64, strings.decodeHexToBytes(to[0..to_len], u16, input[0..len])); + }, + + JSC.Node.Encoding.base64, JSC.Node.Encoding.base64url => { + if (to_len < 2 or len == 0) + return 0; + + // very very slow case! + // shouldn't really happen though + var transcoded = strings.toUTF8Alloc(bun.default_allocator, input[0..len]) catch return 0; + defer bun.default_allocator.free(transcoded); + return writeU8(transcoded.ptr, transcoded.len, to, to_len, encoding); + }, + else => return 0, + } + } + // pub fn writeU8(input: [*]const u8, len: usize, to: [*]u8, to_len: usize, comptime encoding: JSC.Node.Encoding) callconv(.C) i32 {} + + // pub fn toUTF8FromU16(comptime Slice: type, slice: Slice, out_ptr: *[*]u8) i32 { + // var out = strings.toUTF8AllocWithType( + // default_allocator, + // Slice, + // slice, + // ) catch { + // return -1; + // }; + // out_ptr.* = out.ptr; + // return @intCast(isize, out.len); + // } + + // pub fn toUTF8FromLatin1(comptime Slice: type, slice: Slice, out_ptr: *[*]u8) i32 { + // var latin1 = strings.allocateLatin1IntoUTF8(default_allocator, Slice, slice) catch return -1; + // out_ptr.* = latin1.ptr; + // return @intCast(isize, latin1.len); + // } + + // pub fn toUTF16FromLatin1(comptime Slice: type, slice: Slice, out_ptr: *[*]u8) i32 { + // var latin1 = strings.toUTF(default_allocator, Slice, slice) catch return -1; + // out_ptr.* = latin1.ptr; + // return @intCast(isize, latin1.len); + // } + + // pub fn toUTF16FromU8(slice: []const u8, out_ptr: *[*]u16) i32 { + // if (strings.toUTF16Alloc(default_allocator, slice, true)) |result_| { + // if (result_) |result| { + // out_ptr.* = result.ptr; + // return @intCast(isize, result.len); + // } + // } else |err| { + // switch (err) { + // error.InvalidByteSequence => { + // return -2; + // }, + // error.OutOfMemory => { + // return -1; + // }, + // else => { + // return -3; + // }, + // } + // } + + // var out = default_allocator.alloc(u16, slice.len) catch return -1; + // strings.copyU8IntoU16(out, slice); + // out_ptr.* = out.ptr; + // return @intCast(isize, out.len); + // } + comptime { + if (!JSC.is_bindgen) { + _ = Bun__encoding__writeLatin1AsHex; + _ = Bun__encoding__writeLatin1AsURLSafeBase64; + _ = Bun__encoding__writeLatin1AsUTF16; + _ = Bun__encoding__writeLatin1AsUTF8; + _ = Bun__encoding__writeLatin1AsBase64; + _ = Bun__encoding__writeUTF16AsBase64; + _ = Bun__encoding__writeUTF16AsHex; + _ = Bun__encoding__writeUTF16AsURLSafeBase64; + _ = Bun__encoding__writeUTF16AsUTF16; + _ = Bun__encoding__writeUTF16AsUTF8; + _ = Bun__encoding__writeLatin1AsASCII; + _ = Bun__encoding__writeUTF16AsASCII; + } + } +}; + +comptime { + if (!JSC.is_bindgen) { + std.testing.refAllDecls(Encoder); + } +} + test "Vec" {} |