diff options
author | 2022-09-02 03:56:41 +0800 | |
---|---|---|
committer | 2022-09-01 12:56:41 -0700 | |
commit | 700c31dd131bd839c2cf28d6b34915fa111cead4 (patch) | |
tree | 9e2efce1a4efeefe62abbf7514e8d125a60ef797 /src/bun.js/bindings/JSStringDecoder.h | |
parent | f023b89b732db0aff24445acbbe39c366d13118d (diff) | |
download | bun-700c31dd131bd839c2cf28d6b34915fa111cead4.tar.gz bun-700c31dd131bd839c2cf28d6b34915fa111cead4.tar.zst bun-700c31dd131bd839c2cf28d6b34915fa111cead4.zip |
Add native StringDecoder (#1188)
* Add native StringDecoder
* fix upon reviews
* add Constructor and use LazyClassStructure
Diffstat (limited to 'src/bun.js/bindings/JSStringDecoder.h')
-rw-r--r-- | src/bun.js/bindings/JSStringDecoder.h | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/src/bun.js/bindings/JSStringDecoder.h b/src/bun.js/bindings/JSStringDecoder.h new file mode 100644 index 000000000..de2c54209 --- /dev/null +++ b/src/bun.js/bindings/JSStringDecoder.h @@ -0,0 +1,129 @@ +#pragma once + +#include "root.h" +#include "BufferEncodingType.h" + +namespace WebCore { +using namespace JSC; + +class JSStringDecoder : public JSC::JSDestructibleObject { + using Base = JSC::JSDestructibleObject; + +public: + JSStringDecoder(JSC::VM& vm, JSC::Structure* structure, BufferEncodingType encoding) + : Base(vm, structure), m_lastNeed(0), m_lastTotal(0), m_encoding(encoding) + { + } + + DECLARE_INFO; + + static constexpr unsigned StructureFlags = Base::StructureFlags; + + 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); + + 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()); + } + + static JSStringDecoder* create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure, BufferEncodingType encoding) + { + JSStringDecoder* accessor = new (NotNull, JSC::allocateCell<JSStringDecoder>(vm)) JSStringDecoder(vm, structure, encoding); + accessor->finishCreation(vm, globalObject); + return accessor; + } + + void finishCreation(JSC::VM& vm, JSC::JSGlobalObject* globalObject); + static void destroy(JSCell*) {} + + JSC::JSValue write(JSC::VM&, JSC::JSGlobalObject*, uint8_t*, uint32_t); + JSC::JSValue end(JSC::VM&, JSC::JSGlobalObject*, uint8_t*, uint32_t); + + uint8_t m_lastNeed; + uint8_t m_lastTotal; + uint8_t m_lastChar[4]; + +private: + JSC::JSValue fillLast(JSC::VM&, JSC::JSGlobalObject*, uint8_t*, uint32_t); + JSC::JSValue text(JSC::VM&, JSC::JSGlobalObject*, uint8_t*, uint32_t, uint32_t); + uint8_t utf8CheckIncomplete(uint8_t*, uint32_t, uint32_t); + + BufferEncodingType m_encoding; +}; + +class JSStringDecoderPrototype : public JSC::JSNonFinalObject { +public: + using Base = JSC::JSNonFinalObject; + static JSStringDecoderPrototype* create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure) + { + JSStringDecoderPrototype* ptr = new (NotNull, JSC::allocateCell<JSStringDecoderPrototype>(vm)) JSStringDecoderPrototype(vm, 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: + JSStringDecoderPrototype(JSC::VM& vm, JSC::Structure* structure) + : Base(vm, structure) + { + } + + void finishCreation(JSC::VM&, JSC::JSGlobalObject*); +}; + +class JSStringDecoderConstructor final : public JSC::InternalFunction { +public: + using Base = JSC::InternalFunction; + static JSStringDecoderConstructor* create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure, JSStringDecoderPrototype* prototype); + + static constexpr unsigned StructureFlags = Base::StructureFlags; + static constexpr bool needsDestruction = false; + + static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype) + { + return JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::InternalFunctionType, StructureFlags), info()); + } + + 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); + + void initializeProperties(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSStringDecoderPrototype* prototype); + + // Must be defined for each specialization class. + static JSC::EncodedJSValue JSC_HOST_CALL_ATTRIBUTES construct(JSC::JSGlobalObject*, JSC::CallFrame*); + DECLARE_EXPORT_INFO; +private: + JSStringDecoderConstructor(JSC::VM& vm, JSC::Structure* structure, JSC::NativeFunction nativeFunction) + : Base(vm, structure, nativeFunction, nativeFunction) + { + } + + void finishCreation(JSC::VM&, JSC::JSGlobalObject* globalObject, JSStringDecoderPrototype* prototype); +}; + +} |