diff options
author | Anna Henningsen <anna@addaleax.net> | 2017-04-30 18:53:04 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2017-05-03 19:21:32 +0200 |
commit | d56a7e640f766f15980b28d15bbf02bf60975ab2 (patch) | |
tree | d50b7da36ca2331cabe23da90d55d4dcf77ea74f /src/node_buffer.cc | |
parent | 6c2daf0ce9cffbadf51359ddfb804f5a6107737c (diff) | |
download | android-node-v8-d56a7e640f766f15980b28d15bbf02bf60975ab2.tar.gz android-node-v8-d56a7e640f766f15980b28d15bbf02bf60975ab2.tar.bz2 android-node-v8-d56a7e640f766f15980b28d15bbf02bf60975ab2.zip |
src: do proper StringBytes error handling
- 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 <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Diffstat (limited to 'src/node_buffer.cc')
-rw-r--r-- | src/node_buffer.cc | 32 |
1 files changed, 28 insertions, 4 deletions
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<Value>& args) { SLICE_START_END(args[0], args[1], ts_obj_length) - args.GetReturnValue().Set( - StringBytes::Encode(isolate, ts_obj_data + start, length, encoding)); + Local<Value> error; + MaybeLocal<Value> 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<UCS2>(const FunctionCallbackInfo<Value>& 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<UCS2>(const FunctionCallbackInfo<Value>& args) { buf = reinterpret_cast<const uint16_t*>(data); } - args.GetReturnValue().Set(StringBytes::Encode(env->isolate(), buf, length)); + Local<Value> error; + MaybeLocal<Value> 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()); } |