From d56a7e640f766f15980b28d15bbf02bf60975ab2 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 30 Apr 2017 18:53:04 +0200 Subject: src: do proper StringBytes error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Return `MaybeLocal`s from `StringBytes::Encode` - Add an `error` out parameter to pass JS exceptions to the callers (instead of directly throwing) - Simplify some of the string generation methods in `string_bytes.cc` by unifying the `EXTERN_APEX` logic - Reduce usage of deprecated V8 APIs. - Remove error handling logic from JS, the `buffer.*Slice()` methods now throw errors themselves. - Left TODO comments for future semver-major error message improvements. This paves the way for better error messages coming out of the StringBytes methods. Ref: https://github.com/nodejs/node/issues/3175 PR-URL: https://github.com/nodejs/node/pull/12765 Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell Reviewed-By: Tobias Nießen --- src/node_buffer.cc | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'src/node_buffer.cc') diff --git a/src/node_buffer.cc b/src/node_buffer.cc index e8d4945194..367af6592f 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -465,14 +465,26 @@ void StringSlice(const FunctionCallbackInfo& args) { SLICE_START_END(args[0], args[1], ts_obj_length) - args.GetReturnValue().Set( - StringBytes::Encode(isolate, ts_obj_data + start, length, encoding)); + Local error; + MaybeLocal ret = + StringBytes::Encode(isolate, + ts_obj_data + start, + length, + encoding, + &error); + if (ret.IsEmpty()) { + CHECK(!error.IsEmpty()); + isolate->ThrowException(error); + return; + } + args.GetReturnValue().Set(ret.ToLocalChecked()); } template <> void StringSlice(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); + Isolate* isolate = args.GetIsolate(); + Environment* env = Environment::GetCurrent(isolate); THROW_AND_RETURN_UNLESS_BUFFER(env, args.This()); SPREAD_BUFFER_ARG(args.This(), ts_obj); @@ -509,10 +521,22 @@ void StringSlice(const FunctionCallbackInfo& args) { buf = reinterpret_cast(data); } - args.GetReturnValue().Set(StringBytes::Encode(env->isolate(), buf, length)); + Local error; + MaybeLocal ret = + StringBytes::Encode(isolate, + buf, + length, + &error); if (release) delete[] buf; + + if (ret.IsEmpty()) { + CHECK(!error.IsEmpty()); + isolate->ThrowException(error); + return; + } + args.GetReturnValue().Set(ret.ToLocalChecked()); } -- cgit v1.2.3