From 19221d1d6f8a4371a3cef76b0aa9f5d5c7b5a2a9 Mon Sep 17 00:00:00 2001 From: Leko Date: Mon, 27 Nov 2017 16:16:32 +0900 Subject: src: use non-deprecated versions of `->To*()` utils MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Squashed from multiple commits: - src: replace ->To*(isolate) with ->To*(context).ToLocalChecked() - test: use .As on Exception::Error > Exception::Error always returns an object, so e.As() should also work fine See https://github.com/nodejs/node/pull/17343#discussion_r153232027 - test: use .As instead of ->ToObject we already checked that its a buffer - src: use FromMaybe instead of ToLocalChecked It fixed this test case: 19a1b2e414 - src: pass context to Get() Dont pass Local is deprecated soon. So we migrate to maybe version. - src: return if Get or ToObject return an empty before call ToLocalChecked Refs: https://github.com/nodejs/node/issues/17244 PR-URL: https://github.com/nodejs/node/pull/17343 Reviewed-By: Ben Noordhuis Reviewed-By: Michaƫl Zasso Reviewed-By: Joyee Cheung Reviewed-By: Yuta Hiroto Reviewed-By: Anna Henningsen Reviewed-By: Daijiro Wachi --- src/node.cc | 27 ++++++++++++++++++++------- src/node_buffer.cc | 4 ++-- src/node_contextify.cc | 3 ++- src/node_file.cc | 2 +- src/node_http_parser.cc | 2 +- src/node_zlib.cc | 4 ++-- src/process_wrap.cc | 3 ++- src/stream_base.cc | 4 ++-- 8 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/node.cc b/src/node.cc index 481db24e94..8176c25c53 100644 --- a/src/node.cc +++ b/src/node.cc @@ -589,7 +589,7 @@ Local ErrnoException(Isolate* isolate, } e = Exception::Error(cons); - Local obj = e->ToObject(env->isolate()); + Local obj = e.As(); obj->Set(env->errno_string(), Integer::New(env->isolate(), errorno)); obj->Set(env->code_string(), estring); @@ -751,7 +751,7 @@ Local WinapiErrnoException(Isolate* isolate, e = Exception::Error(message); } - Local obj = e->ToObject(env->isolate()); + Local obj = e.As(); obj->Set(env->errno_string(), Integer::New(isolate, errorno)); if (path != nullptr) { @@ -1428,7 +1428,7 @@ static void ReportException(Environment* env, if (er->IsUndefined() || er->IsNull()) { trace_value = Undefined(env->isolate()); } else { - Local err_obj = er->ToObject(env->isolate()); + Local err_obj = er->ToObject(env->context()).ToLocalChecked(); trace_value = err_obj->Get(env->stack_string()); arrow = @@ -2251,7 +2251,8 @@ static void DLOpen(const FunctionCallbackInfo& args) { return env->ThrowTypeError("flag argument must be an integer."); } - Local module = args[0]->ToObject(env->isolate()); // Cast + Local module = + args[0]->ToObject(env->context()).ToLocalChecked(); // Cast node::Utf8Value filename(env->isolate(), args[1]); // Cast DLib dlib; dlib.filename_ = *filename; @@ -2269,7 +2270,8 @@ static void DLOpen(const FunctionCallbackInfo& args) { dlib.Close(); #ifdef _WIN32 // Windows needs to add the filename into the error message - errmsg = String::Concat(errmsg, args[1]->ToString(env->isolate())); + errmsg = String::Concat(errmsg, + args[1]->ToString(env->context()).ToLocalChecked()); #endif // _WIN32 env->isolate()->ThrowException(Exception::Error(errmsg)); return; @@ -2314,7 +2316,18 @@ static void DLOpen(const FunctionCallbackInfo& args) { modlist_addon = mp; Local exports_string = env->exports_string(); - Local exports = module->Get(exports_string)->ToObject(env->isolate()); + MaybeLocal maybe_exports = + module->Get(env->context(), exports_string); + + if (maybe_exports.IsEmpty() || + maybe_exports.ToLocalChecked()->ToObject(env->context()).IsEmpty()) { + dlib.Close(); + return; + } + + Local exports = + maybe_exports.ToLocalChecked()->ToObject(env->context()) + .FromMaybe(Local()); if (mp->nm_context_register_func != nullptr) { mp->nm_context_register_func(exports, module, env->context(), mp->nm_priv); @@ -4281,7 +4294,7 @@ void EmitBeforeExit(Environment* env) { Local exit_code = FIXED_ONE_BYTE_STRING(env->isolate(), "exitCode"); Local args[] = { FIXED_ONE_BYTE_STRING(env->isolate(), "beforeExit"), - process_object->Get(exit_code)->ToInteger(env->isolate()) + process_object->Get(exit_code)->ToInteger(env->context()).ToLocalChecked() }; MakeCallback(env->isolate(), process_object, "emit", arraysize(args), args, diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 138c26685f..e810760790 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -606,7 +606,7 @@ void Fill(const FunctionCallbackInfo& args) { return; } - str_obj = args[1]->ToString(env->isolate()); + str_obj = args[1]->ToString(env->context()).ToLocalChecked(); enc = ParseEncoding(env->isolate(), args[4], UTF8); str_length = enc == UTF8 ? str_obj->Utf8Length() : @@ -677,7 +677,7 @@ void StringWrite(const FunctionCallbackInfo& args) { if (!args[0]->IsString()) return env->ThrowTypeError("Argument must be a string"); - Local str = args[0]->ToString(env->isolate()); + Local str = args[0]->ToString(env->context()).ToLocalChecked(); size_t offset; size_t max_length; diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 584d2dc6f9..73c5fe08ab 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -621,7 +621,8 @@ class ContextifyScript : public BaseObject { new ContextifyScript(env, args.This()); TryCatch try_catch(env->isolate()); - Local code = args[0]->ToString(env->isolate()); + Local code = + args[0]->ToString(env->context()).FromMaybe(Local()); Local options = args[1]; MaybeLocal filename = GetFilenameArg(env, options); diff --git a/src/node_file.cc b/src/node_file.cc index c178ec9ac1..e0b31ea5a6 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1219,7 +1219,7 @@ static void Read(const FunctionCallbackInfo& args) { char * buf = nullptr; - Local buffer_obj = args[1]->ToObject(env->isolate()); + Local buffer_obj = args[1].As(); char *buffer_data = Buffer::Data(buffer_obj); size_t buffer_length = Buffer::Length(buffer_obj); diff --git a/src/node_http_parser.cc b/src/node_http_parser.cc index c990b763be..f378a0475a 100644 --- a/src/node_http_parser.cc +++ b/src/node_http_parser.cc @@ -466,7 +466,7 @@ class Parser : public AsyncWrap { enum http_errno err = HTTP_PARSER_ERRNO(&parser->parser_); Local e = Exception::Error(env->parse_error_string()); - Local obj = e->ToObject(env->isolate()); + Local obj = e.As(); obj->Set(env->bytes_parsed_string(), Integer::New(env->isolate(), 0)); obj->Set(env->code_string(), OneByteString(env->isolate(), http_errno_name(err))); diff --git a/src/node_zlib.cc b/src/node_zlib.cc index 21145a0d5b..8ef4383e03 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -178,7 +178,7 @@ class ZCtx : public AsyncWrap { } else { CHECK(Buffer::HasInstance(args[1])); Local in_buf; - in_buf = args[1]->ToObject(env->isolate()); + in_buf = args[1]->ToObject(env->context()).ToLocalChecked(); in_off = args[2]->Uint32Value(); in_len = args[3]->Uint32Value(); @@ -187,7 +187,7 @@ class ZCtx : public AsyncWrap { } CHECK(Buffer::HasInstance(args[4])); - Local out_buf = args[4]->ToObject(env->isolate()); + Local out_buf = args[4]->ToObject(env->context()).ToLocalChecked(); out_off = args[5]->Uint32Value(); out_len = args[6]->Uint32Value(); CHECK(Buffer::IsWithinBounds(out_off, out_len, Buffer::Length(out_buf))); diff --git a/src/process_wrap.cc b/src/process_wrap.cc index 3667b0449e..b01ef56270 100644 --- a/src/process_wrap.cc +++ b/src/process_wrap.cc @@ -143,7 +143,8 @@ class ProcessWrap : public HandleWrap { ProcessWrap* wrap; ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder()); - Local js_options = args[0]->ToObject(env->isolate()); + Local js_options = + args[0]->ToObject(env->context()).ToLocalChecked(); uv_process_options_t options; memset(&options, 0, sizeof(uv_process_options_t)); diff --git a/src/stream_base.cc b/src/stream_base.cc index c6aca1694f..922b277c58 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -127,7 +127,7 @@ int StreamBase::Writev(const FunctionCallbackInfo& args) { // Buffer chunk, no additional storage required // String chunk - Local string = chunk->ToString(env->isolate()); + Local string = chunk->ToString(env->context()).ToLocalChecked(); enum encoding encoding = ParseEncoding(env->isolate(), chunks->Get(i * 2 + 1)); size_t chunk_size; @@ -179,7 +179,7 @@ int StreamBase::Writev(const FunctionCallbackInfo& args) { char* str_storage = req_wrap->Extra(offset); size_t str_size = storage_size - offset; - Local string = chunk->ToString(env->isolate()); + Local string = chunk->ToString(env->context()).ToLocalChecked(); enum encoding encoding = ParseEncoding(env->isolate(), chunks->Get(i * 2 + 1)); str_size = StringBytes::Write(env->isolate(), -- cgit v1.2.3