Skip to content

Commit c4ef170

Browse files
committed
doc: remove usage of deprecated V8 APIs in addons.md
PR-URL: #22667 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Ujjwal Sharma <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c637d41 commit c4ef170

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

doc/api/addons.md

+14-10
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,8 @@ void Add(const FunctionCallbackInfo<Value>& args) {
491491
}
492492

493493
// Perform the operation
494-
double value = args[0]->NumberValue() + args[1]->NumberValue();
494+
double value =
495+
args[0].As<Number>()->Value() + args[1].As<Number>()->Value();
495496
Local<Number> num = Number::New(isolate, value);
496497

497498
// Set the return value (using the passed in
@@ -597,7 +598,7 @@ void CreateObject(const FunctionCallbackInfo<Value>& args) {
597598
Isolate* isolate = args.GetIsolate();
598599

599600
Local<Object> obj = Object::New(isolate);
600-
obj->Set(String::NewFromUtf8(isolate, "msg"), args[0]->ToString());
601+
obj->Set(String::NewFromUtf8(isolate, "msg"), args[0]->ToString(isolate));
601602

602603
args.GetReturnValue().Set(obj);
603604
}
@@ -783,18 +784,19 @@ void MyObject::Init(Local<Object> exports) {
783784

784785
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
785786
Isolate* isolate = args.GetIsolate();
787+
Local<Context> context = isolate->GetCurrentContext();
786788

787789
if (args.IsConstructCall()) {
788790
// Invoked as constructor: `new MyObject(...)`
789-
double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
791+
double value = args[0]->IsUndefined() ?
792+
0 : args[0]->NumberValue(context).FromMaybe(0);
790793
MyObject* obj = new MyObject(value);
791794
obj->Wrap(args.This());
792795
args.GetReturnValue().Set(args.This());
793796
} else {
794797
// Invoked as plain function `MyObject(...)`, turn into construct call.
795798
const int argc = 1;
796799
Local<Value> argv[argc] = { args[0] };
797-
Local<Context> context = isolate->GetCurrentContext();
798800
Local<Function> cons = Local<Function>::New(isolate, constructor);
799801
Local<Object> result =
800802
cons->NewInstance(context, argc, argv).ToLocalChecked();
@@ -965,10 +967,12 @@ void MyObject::Init(Isolate* isolate) {
965967

966968
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
967969
Isolate* isolate = args.GetIsolate();
970+
Local<Context> context = isolate->GetCurrentContext();
968971

969972
if (args.IsConstructCall()) {
970973
// Invoked as constructor: `new MyObject(...)`
971-
double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
974+
double value = args[0]->IsUndefined() ?
975+
0 : args[0]->NumberValue(context).FromMaybe(0);
972976
MyObject* obj = new MyObject(value);
973977
obj->Wrap(args.This());
974978
args.GetReturnValue().Set(args.This());
@@ -977,7 +981,6 @@ void MyObject::New(const FunctionCallbackInfo<Value>& args) {
977981
const int argc = 1;
978982
Local<Value> argv[argc] = { args[0] };
979983
Local<Function> cons = Local<Function>::New(isolate, constructor);
980-
Local<Context> context = isolate->GetCurrentContext();
981984
Local<Object> instance =
982985
cons->NewInstance(context, argc, argv).ToLocalChecked();
983986
args.GetReturnValue().Set(instance);
@@ -1080,9 +1083,9 @@ void Add(const FunctionCallbackInfo<Value>& args) {
10801083
Isolate* isolate = args.GetIsolate();
10811084

10821085
MyObject* obj1 = node::ObjectWrap::Unwrap<MyObject>(
1083-
args[0]->ToObject());
1086+
args[0]->ToObject(isolate));
10841087
MyObject* obj2 = node::ObjectWrap::Unwrap<MyObject>(
1085-
args[1]->ToObject());
1088+
args[1]->ToObject(isolate));
10861089

10871090
double sum = obj1->value() + obj2->value();
10881091
args.GetReturnValue().Set(Number::New(isolate, sum));
@@ -1172,18 +1175,19 @@ void MyObject::Init(Isolate* isolate) {
11721175

11731176
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
11741177
Isolate* isolate = args.GetIsolate();
1178+
Local<Context> context = isolate->GetCurrentContext();
11751179

11761180
if (args.IsConstructCall()) {
11771181
// Invoked as constructor: `new MyObject(...)`
1178-
double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
1182+
double value = args[0]->IsUndefined() ?
1183+
0 : args[0]->NumberValue(context).FromMaybe(0);
11791184
MyObject* obj = new MyObject(value);
11801185
obj->Wrap(args.This());
11811186
args.GetReturnValue().Set(args.This());
11821187
} else {
11831188
// Invoked as plain function `MyObject(...)`, turn into construct call.
11841189
const int argc = 1;
11851190
Local<Value> argv[argc] = { args[0] };
1186-
Local<Context> context = isolate->GetCurrentContext();
11871191
Local<Function> cons = Local<Function>::New(isolate, constructor);
11881192
Local<Object> instance =
11891193
cons->NewInstance(context, argc, argv).ToLocalChecked();

0 commit comments

Comments
 (0)