Skip to content

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

@chjj

Description

@chjj

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions