Skip to content

Commit 419cde7

Browse files
LekoMylesBorins
authored andcommitted
src: use non-deprecated versions of ->To*() utils
Squashed from multiple commits: - src: replace ->To*(isolate) with ->To*(context).ToLocalChecked() - test: use .As<Object> on Exception::Error > Exception::Error always returns an object, so e.As<Object>() should also work fine See #17343 (comment) - test: use .As<Object> instead of ->ToObject we already checked that its a buffer - src: use FromMaybe instead of ToLocalChecked It fixed this test case: 19a1b2e - src: pass context to Get() Dont pass Local<Context> is deprecated soon. So we migrate to maybe version. - src: return if Get or ToObject return an empty before call ToLocalChecked Refs: #17244 PR-URL: #17343 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]>
1 parent 9c2f24e commit 419cde7

8 files changed

+32
-17
lines changed

src/node.cc

+20-7
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ Local<Value> ErrnoException(Isolate* isolate,
917917
}
918918
e = Exception::Error(cons);
919919

920-
Local<Object> obj = e->ToObject(env->isolate());
920+
Local<Object> obj = e.As<Object>();
921921
obj->Set(env->errno_string(), Integer::New(env->isolate(), errorno));
922922
obj->Set(env->code_string(), estring);
923923

@@ -1079,7 +1079,7 @@ Local<Value> WinapiErrnoException(Isolate* isolate,
10791079
e = Exception::Error(message);
10801080
}
10811081

1082-
Local<Object> obj = e->ToObject(env->isolate());
1082+
Local<Object> obj = e.As<Object>();
10831083
obj->Set(env->errno_string(), Integer::New(isolate, errorno));
10841084

10851085
if (path != nullptr) {
@@ -1756,7 +1756,7 @@ static void ReportException(Environment* env,
17561756
if (er->IsUndefined() || er->IsNull()) {
17571757
trace_value = Undefined(env->isolate());
17581758
} else {
1759-
Local<Object> err_obj = er->ToObject(env->isolate());
1759+
Local<Object> err_obj = er->ToObject(env->context()).ToLocalChecked();
17601760

17611761
trace_value = err_obj->Get(env->stack_string());
17621762
arrow =
@@ -2579,7 +2579,8 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
25792579
return env->ThrowTypeError("flag argument must be an integer.");
25802580
}
25812581

2582-
Local<Object> module = args[0]->ToObject(env->isolate()); // Cast
2582+
Local<Object> module =
2583+
args[0]->ToObject(env->context()).ToLocalChecked(); // Cast
25832584
node::Utf8Value filename(env->isolate(), args[1]); // Cast
25842585
DLib dlib;
25852586
dlib.filename_ = *filename;
@@ -2597,7 +2598,8 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
25972598
dlib.Close();
25982599
#ifdef _WIN32
25992600
// Windows needs to add the filename into the error message
2600-
errmsg = String::Concat(errmsg, args[1]->ToString(env->isolate()));
2601+
errmsg = String::Concat(errmsg,
2602+
args[1]->ToString(env->context()).ToLocalChecked());
26012603
#endif // _WIN32
26022604
env->isolate()->ThrowException(Exception::Error(errmsg));
26032605
return;
@@ -2642,7 +2644,18 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
26422644
modlist_addon = mp;
26432645

26442646
Local<String> exports_string = env->exports_string();
2645-
Local<Object> exports = module->Get(exports_string)->ToObject(env->isolate());
2647+
MaybeLocal<Value> maybe_exports =
2648+
module->Get(env->context(), exports_string);
2649+
2650+
if (maybe_exports.IsEmpty() ||
2651+
maybe_exports.ToLocalChecked()->ToObject(env->context()).IsEmpty()) {
2652+
dlib.Close();
2653+
return;
2654+
}
2655+
2656+
Local<Object> exports =
2657+
maybe_exports.ToLocalChecked()->ToObject(env->context())
2658+
.FromMaybe(Local<Object>());
26462659

26472660
if (mp->nm_context_register_func != nullptr) {
26482661
mp->nm_context_register_func(exports, module, env->context(), mp->nm_priv);
@@ -4641,7 +4654,7 @@ void EmitBeforeExit(Environment* env) {
46414654
Local<String> exit_code = FIXED_ONE_BYTE_STRING(env->isolate(), "exitCode");
46424655
Local<Value> args[] = {
46434656
FIXED_ONE_BYTE_STRING(env->isolate(), "beforeExit"),
4644-
process_object->Get(exit_code)->ToInteger(env->isolate())
4657+
process_object->Get(exit_code)->ToInteger(env->context()).ToLocalChecked()
46454658
};
46464659
MakeCallback(env->isolate(),
46474660
process_object, "emit", arraysize(args), args,

src/node_buffer.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
608608
return;
609609
}
610610

611-
str_obj = args[1]->ToString(env->isolate());
611+
str_obj = args[1]->ToString(env->context()).ToLocalChecked();
612612
enc = ParseEncoding(env->isolate(), args[4], UTF8);
613613
str_length =
614614
enc == UTF8 ? str_obj->Utf8Length() :
@@ -681,7 +681,7 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) {
681681
if (!args[0]->IsString())
682682
return env->ThrowTypeError("Argument must be a string");
683683

684-
Local<String> str = args[0]->ToString(env->isolate());
684+
Local<String> str = args[0]->ToString(env->context()).ToLocalChecked();
685685

686686
size_t offset;
687687
size_t max_length;

src/node_contextify.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,8 @@ class ContextifyScript : public BaseObject {
654654
new ContextifyScript(env, args.This());
655655

656656
TryCatch try_catch(env->isolate());
657-
Local<String> code = args[0]->ToString(env->isolate());
657+
Local<String> code =
658+
args[0]->ToString(env->context()).FromMaybe(Local<String>());
658659

659660
Local<Value> options = args[1];
660661
MaybeLocal<String> filename = GetFilenameArg(env, options);

src/node_file.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ static void Read(const FunctionCallbackInfo<Value>& args) {
11691169

11701170
char * buf = nullptr;
11711171

1172-
Local<Object> buffer_obj = args[1]->ToObject(env->isolate());
1172+
Local<Object> buffer_obj = args[1].As<Object>();
11731173
char *buffer_data = Buffer::Data(buffer_obj);
11741174
size_t buffer_length = Buffer::Length(buffer_obj);
11751175

src/node_http_parser.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ class Parser : public AsyncWrap {
466466
enum http_errno err = HTTP_PARSER_ERRNO(&parser->parser_);
467467

468468
Local<Value> e = Exception::Error(env->parse_error_string());
469-
Local<Object> obj = e->ToObject(env->isolate());
469+
Local<Object> obj = e.As<Object>();
470470
obj->Set(env->bytes_parsed_string(), Integer::New(env->isolate(), 0));
471471
obj->Set(env->code_string(),
472472
OneByteString(env->isolate(), http_errno_name(err)));

src/node_zlib.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class ZCtx : public AsyncWrap {
178178
} else {
179179
CHECK(Buffer::HasInstance(args[1]));
180180
Local<Object> in_buf;
181-
in_buf = args[1]->ToObject(env->isolate());
181+
in_buf = args[1]->ToObject(env->context()).ToLocalChecked();
182182
in_off = args[2]->Uint32Value();
183183
in_len = args[3]->Uint32Value();
184184

@@ -187,7 +187,7 @@ class ZCtx : public AsyncWrap {
187187
}
188188

189189
CHECK(Buffer::HasInstance(args[4]));
190-
Local<Object> out_buf = args[4]->ToObject(env->isolate());
190+
Local<Object> out_buf = args[4]->ToObject(env->context()).ToLocalChecked();
191191
out_off = args[5]->Uint32Value();
192192
out_len = args[6]->Uint32Value();
193193
CHECK(Buffer::IsWithinBounds(out_off, out_len, Buffer::Length(out_buf)));

src/process_wrap.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ class ProcessWrap : public HandleWrap {
143143
ProcessWrap* wrap;
144144
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
145145

146-
Local<Object> js_options = args[0]->ToObject(env->isolate());
146+
Local<Object> js_options =
147+
args[0]->ToObject(env->context()).ToLocalChecked();
147148

148149
uv_process_options_t options;
149150
memset(&options, 0, sizeof(uv_process_options_t));

src/stream_base.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
127127
// Buffer chunk, no additional storage required
128128

129129
// String chunk
130-
Local<String> string = chunk->ToString(env->isolate());
130+
Local<String> string = chunk->ToString(env->context()).ToLocalChecked();
131131
enum encoding encoding = ParseEncoding(env->isolate(),
132132
chunks->Get(i * 2 + 1));
133133
size_t chunk_size;
@@ -179,7 +179,7 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
179179
char* str_storage = req_wrap->Extra(offset);
180180
size_t str_size = storage_size - offset;
181181

182-
Local<String> string = chunk->ToString(env->isolate());
182+
Local<String> string = chunk->ToString(env->context()).ToLocalChecked();
183183
enum encoding encoding = ParseEncoding(env->isolate(),
184184
chunks->Get(i * 2 + 1));
185185
str_size = StringBytes::Write(env->isolate(),

0 commit comments

Comments
 (0)