Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v8/node deprecated APIs and how to handle them #7

Closed
chjj opened this issue Nov 3, 2018 · 5 comments
Closed

v8/node deprecated APIs and how to handle them #7

chjj opened this issue Nov 3, 2018 · 5 comments

Comments

@chjj
Copy link
Member

chjj commented Nov 3, 2018

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:

Timeline:

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.

@chjj
Copy link
Member Author

chjj commented Nov 3, 2018

It looks like nan has upgraded for this, but only for strings: nodejs/nan@24a22c3

Corresponding PR: nodejs/nan#808

Nevermind, looks like it's still in the works: nodejs/nan#811

chjj added a commit that referenced this issue Nov 4, 2018
@chjj
Copy link
Member Author

chjj commented Dec 17, 2018

Finally fixed with 3a3bb28

@chjj
Copy link
Member Author

chjj commented Jun 5, 2019

This issue seems to be getting a lot of attention from people in the community. Please note that Nan::To can be used in place of the nasty v8 syntax.

For example:

Nan::To<int32_t>(info[0]).FromJust()

Instead of:

info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();

@cocoche007
Copy link

cocoche007 commented Jan 17, 2020

Hi all,

In Node 8, I had:

v8::String::Utf8Value textV8(args[4]->ToString());
std::string           text(*textV8);

But in Node 12, with:

v8::Local<v8::String> textV8(args[4]->ToString(context).FromMaybe(v8::Local<v8::String>()));

I don't figure out how concert in std::string

Edit:
For the moment, I found this solution but too complicated for me:

v8::Local<v8::String> textV8(args[4]->ToString(context).FromMaybe(v8::Local<v8::String>()));
Nan::Utf8String       textNan(textV8);
std::string           text(*textNan);

tomhicks pushed a commit to candide-com/node-svm that referenced this issue Feb 21, 2020
In node 12, a load of deprecated v8 APIs were removed:
bcoin-org/bcrypto#7
tomhicks pushed a commit to candide-com/node-svm that referenced this issue Feb 21, 2020
In node 12, a load of deprecated v8 APIs were removed:
bcoin-org/bcrypto#7
matburnham added a commit to matburnham/node-osmium that referenced this issue May 22, 2020
Stash attempts to fix node-osmium so that it compiles on node 12 with
v8 deprecatons before I got bored and decided to use 'n' to downgrade
node for this one bit of code I wanted to run instead. Ever feel you
start with somethihng simple and end up six problems deep?

No idea whether any of this actually works. I was stabbing at fixing
compiler errors before making it logically work before I gave up.

I was having particular trouble with fixing GetFunction
(see https://stackoverflow.com/questions/61946241)

Useful info:
- https://github.com/joyeecheung/node/blob/v8-maybe-doc/CPP_STYLE_GUIDE.md#use-maybe-version-of-v8-apis
- https://nodesource.com/blog/cpp-addons-for-nodejs-v4
- bcoin-org/bcrypto#7

(Vaguely) relevant issues:
- osmcode#108
@Rainmen-xia
Copy link

marked

FelipeGdM added a commit to FelipeGdM/gzweb that referenced this issue Jan 22, 2021
Several API calls from V8 were deprecated in version 7.0
(that ships w/ node 12), this commit replaces then

Refs:

nodejs/node#23122

nodejs/node#23159

bcoin-org/bcrypto#7
FelipeGdM added a commit to FelipeGdM/gzweb that referenced this issue Feb 1, 2021
Several API calls from V8 were deprecated in version 7.0
(that ships w/ node 12), this commit replaces then

Refs:

nodejs/node#23122

nodejs/node#23159

bcoin-org/bcrypto#7
FelipeGdM added a commit to FelipeGdM/gzweb that referenced this issue Feb 1, 2021
Several API calls from V8 were deprecated in version 7.0
(that ships w/ node 12), this commit replaces then

Refs:

nodejs/node#23122

nodejs/node#23159

bcoin-org/bcrypto#7
FelipeGdM added a commit to FelipeGdM/gzbridge that referenced this issue Feb 11, 2021
Several API calls from V8 were deprecated in version 7.0
(that ships w/ node 12), this commit replaces then

Refs:

nodejs/node#23122

nodejs/node#23159

bcoin-org/bcrypto#7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants