diff --git a/lib/internal/encoding.js b/lib/internal/encoding.js index 2ab85d9d9acb06..40c87a0a8719ff 100644 --- a/lib/internal/encoding.js +++ b/lib/internal/encoding.js @@ -411,15 +411,7 @@ function makeTextDecoderICU() { decode(input = empty, options = kEmptyObject) { validateDecoder(this); - if (isAnyArrayBuffer(input)) { - try { - input = lazyBuffer().from(input); - } catch { - // If the buffer is detached, - // use an empty Uint8Array to avoid TypeError - input = empty; - } - } else if (!isArrayBufferView(input)) { + if (!isAnyArrayBuffer(input) && !isArrayBufferView(input)) { throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'ArrayBufferView'], input); diff --git a/src/node_i18n.cc b/src/node_i18n.cc index ed7b72c31f975e..37044beb3dbe52 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -453,12 +453,10 @@ void ConverterObject::Decode(const FunctionCallbackInfo& args) { // characters times the min char size, multiplied by 2 as unicode may // take up to 2 UChars to encode a character size_t limit = 2 * converter->min_char_size() * - (!flush ? - input.length() : - std::max( - input.length(), - static_cast( - ucnv_toUCountPending(converter->conv(), &status)))); + (!flush ? input.length() + : std::max(input.length(), + static_cast(ucnv_toUCountPending( + converter->conv(), &status)))); status = U_ZERO_ERROR; if (limit > 0) diff --git a/src/util-inl.h b/src/util-inl.h index bdf9732e853485..ccae3f612c4905 100644 --- a/src/util-inl.h +++ b/src/util-inl.h @@ -513,8 +513,9 @@ SlicedArguments::SlicedArguments( template ArrayBufferViewContents::ArrayBufferViewContents( v8::Local value) { - CHECK(value->IsArrayBufferView()); - Read(value.As()); + CHECK(value->IsArrayBufferView() || value->IsSharedArrayBuffer() || + value->IsArrayBuffer()); + ReadValue(value); } template @@ -542,6 +543,23 @@ void ArrayBufferViewContents::Read(v8::Local abv) { } } +template +void ArrayBufferViewContents::ReadValue(v8::Local buf) { + static_assert(sizeof(T) == 1, "Only supports one-byte data at the moment"); + + if (buf->IsArrayBufferView()) { + Read(buf.As()); + } else if (buf->IsArrayBuffer()) { + auto ab = buf.As(); + length_ = ab->ByteLength(); + data_ = reinterpret_cast(ab->Data()); + } else { + auto sab = buf.As(); + length_ = sab->ByteLength(); + data_ = reinterpret_cast(sab->Data()); + } +} + // ECMA262 20.1.2.5 inline bool IsSafeJsInt(v8::Local v) { if (!v->IsNumber()) return false; diff --git a/src/util.h b/src/util.h index 7a41eae4aa77df..6094f07f201f2f 100644 --- a/src/util.h +++ b/src/util.h @@ -506,6 +506,7 @@ class ArrayBufferViewContents { explicit inline ArrayBufferViewContents(v8::Local value); explicit inline ArrayBufferViewContents(v8::Local abv); inline void Read(v8::Local abv); + inline void ReadValue(v8::Local buf); inline const T* data() const { return data_; } inline size_t length() const { return length_; }