-
Notifications
You must be signed in to change notification settings - Fork 41
Description
Putting this here to document for my own sanity.
V8 removed a bunch of calls and the node.js devs have decided to maintain them for the time being, but have marked them as deprecated. We need to switch away from them.
Deprecated APIs:
Uint32Value()
Int32Value()
IntegerValue()
NumberValue()
BooleanValue()
ToString()
Example migration:
Originally:
int32_t arg = info[0]->Int32Value();
Now must be:
int32_t arg = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
Or:
int32_t arg = info[0]->Int32Value(Nan::GetCurrentContext()).ToChecked();
Or:
int32_t arg = info[0]->ToInt32(Nan::GetCurrentContext()).ToLocalChecked()->Value();
Int32Value
returns Maybe<uint32_t>
.
ToInt32
returns MaybeLocal<Int32>
.
Note that ToChecked()
is an alias for FromJust()
.
ToString()
was also deprecated, but nan.h should be handling this. Not us, but for completeness, heres the migration.
From:
v8::Local<v8::String> str = info[0]->ToString();
To:
v8::Local<v8::String> str = info[0]->ToString(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::String>());
Note that anything that returns MaybeLocal
instead of Maybe
should have an IsEmpty()
check.
Example (from the style guide below):
v8::MaybeLocal<v8::Value> maybe = fn->Call(...);
if (maybe.IsEmpty()) { // Exception occurs during function call
// Do some clean up
return; // Back to JavaScript and let it throw
}
v8::Local<v8::Value> result = maybe.ToLocalChecked();
References:
- Value Class:
https://v8docs.nodesource.com/node-10.6/dc/d0a/classv8_1_1_value.html - Int32 Class:
https://v8docs.nodesource.com/node-10.6/de/da6/classv8_1_1_int32.html - Maybe Class:
https://v8docs.nodesource.com/node-10.6/d9/d4b/classv8_1_1_maybe.html - MaybeLocal Class:
https://v8docs.nodesource.com/node-10.6/d8/d7d/classv8_1_1_maybe_local.html - Maybe Code: https://v8docs.nodesource.com/node-10.6/d4/da0/v8_8h_source.html
- v8.h (after the deprecation):
https://github.com/addaleax/node/blob/78b888c6b1ca11e578be007a79476a792f726532/deps/v8/include/v8.h - v8.h (master):
https://github.com/nodejs/node/blob/master/deps/v8/include/v8.h
Timeline:
- Node Devs talk about the old APIs being removed (Sep. 27th):
APIs removed in V8 7.0 and native addons nodejs/node#23122 - Node Devs decide to maintain old APIs but deprecate them (Sep. 28th):
[v10.x] deps: increase V8 deprecation levels nodejs/node#23159
Style Guide (mentioning the changes):
I'm not sure how this will impact node versions prior to node v10. This maybe needs to be added as a part of nan? Not sure. Going to do more research.
Update:
Just discovered the magic of Nan::To
(1, 2, 3).
We can do Nan::To<int32_t>(info[0]).FromJust()
.
Nan::To
seems undocumented, but it seems to be the inverse of Nan::New
.