mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
util: improve text decoder performance
PR-URL: https://github.com/nodejs/node/pull/45388 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
committed by
Rich Trott
parent
4107ce0dad
commit
ca3ed363ff
@@ -18,7 +18,6 @@ const {
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
ERR_ENCODING_INVALID_ENCODED_DATA,
|
||||
ERR_ENCODING_NOT_SUPPORTED,
|
||||
ERR_INVALID_ARG_TYPE,
|
||||
ERR_INVALID_THIS,
|
||||
@@ -411,11 +410,6 @@ function makeTextDecoderICU() {
|
||||
|
||||
decode(input = empty, options = kEmptyObject) {
|
||||
validateDecoder(this);
|
||||
if (!isAnyArrayBuffer(input) && !isArrayBufferView(input)) {
|
||||
throw new ERR_INVALID_ARG_TYPE('input',
|
||||
['ArrayBuffer', 'ArrayBufferView'],
|
||||
input);
|
||||
}
|
||||
validateObject(options, 'options', {
|
||||
nullable: true,
|
||||
allowArray: true,
|
||||
@@ -426,11 +420,7 @@ function makeTextDecoderICU() {
|
||||
if (options !== null)
|
||||
flags |= options.stream ? 0 : CONVERTER_FLAGS_FLUSH;
|
||||
|
||||
const ret = _decode(this[kHandle], input, flags);
|
||||
if (typeof ret === 'number') {
|
||||
throw new ERR_ENCODING_INVALID_ENCODED_DATA(this.encoding, ret);
|
||||
}
|
||||
return ret;
|
||||
return _decode(this[kHandle], input, flags, this.encoding);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details);
|
||||
V(ERR_CRYPTO_JOB_INIT_FAILED, Error) \
|
||||
V(ERR_DLOPEN_DISABLED, Error) \
|
||||
V(ERR_DLOPEN_FAILED, Error) \
|
||||
V(ERR_ENCODING_INVALID_ENCODED_DATA, TypeError) \
|
||||
V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, Error) \
|
||||
V(ERR_INVALID_ADDRESS, Error) \
|
||||
V(ERR_INVALID_ARG_VALUE, TypeError) \
|
||||
|
||||
@@ -436,13 +436,25 @@ void ConverterObject::Create(const FunctionCallbackInfo<Value>& args) {
|
||||
void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
|
||||
CHECK_GE(args.Length(), 3); // Converter, Buffer, Flags
|
||||
CHECK_GE(args.Length(), 4); // Converter, Buffer, Flags, Encoding
|
||||
|
||||
ConverterObject* converter;
|
||||
ASSIGN_OR_RETURN_UNWRAP(&converter, args[0].As<Object>());
|
||||
|
||||
if (!(args[1]->IsArrayBuffer() || args[1]->IsSharedArrayBuffer() ||
|
||||
args[1]->IsArrayBufferView())) {
|
||||
return node::THROW_ERR_INVALID_ARG_TYPE(
|
||||
env->isolate(),
|
||||
"The \"input\" argument must be an instance of SharedArrayBuffer, "
|
||||
"ArrayBuffer or ArrayBufferView.");
|
||||
}
|
||||
|
||||
ArrayBufferViewContents<char> input(args[1]);
|
||||
int flags = args[2]->Uint32Value(env->context()).ToChecked();
|
||||
|
||||
CHECK(args[3]->IsString());
|
||||
Local<String> from_encoding = args[3].As<String>();
|
||||
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
MaybeStackBuffer<UChar> result;
|
||||
|
||||
@@ -524,14 +536,14 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
|
||||
Local<Value> ret;
|
||||
if (encoded.ToLocal(&ret)) {
|
||||
args.GetReturnValue().Set(ret);
|
||||
} else {
|
||||
args.GetReturnValue().Set(error);
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
args.GetReturnValue().Set(status);
|
||||
node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA(
|
||||
env->isolate(),
|
||||
"The encoded data was not valid for encoding %s",
|
||||
*node::Utf8Value(env->isolate(), from_encoding));
|
||||
}
|
||||
|
||||
ConverterObject::ConverterObject(
|
||||
|
||||
Reference in New Issue
Block a user