Tomasz Kasperczyk
Tomasz Kasperczyk

Reputation: 2093

Node 12 (v8) - converting deprecated code to the "maybe" version

I decided to update Node to 12.11. Because of this, I'm getting gyp compilation errors in some node modules that I'm using. I have some experience with C++, but I've never used v8. I've managed to fix most errors related to previously deprecated and now removed functions, but there's one kind of a problem that I'm unable to solve:

v8::Local<v8::Object> obj = size->ToObject();
if(obj->Has(columns))
    w->ws_col = obj->Get(columns)->Uint32Value();
if(obj->Has(rows))
    w->ws_row = obj->Get(rows)->Uint32Value();

ToObject() has been removed, so I came up with this:

v8::Local<v8::Object> obj = Nan::To<v8::Object>(size).ToLocalChecked();

The problem is that there's no v8::Object::Has(v8::Local<v8::String>&) method. How can I make the Has calls compatible with the new version?

Upvotes: 0

Views: 1874

Answers (1)

jmrk
jmrk

Reputation: 40561

The non-deprecated replacement for the old ToObject() is MaybeLocal<Object> v8::Value::ToObject(Local<Context> context); using Nan is certainly possible but not required. Note that .ToLocalChecked will trigger a crash if an exception was thrown and there is no result value, this can happen e.g. if you call this function on null or undefined. The result type MaybeLocal is intended to make it obvious that embedder code needs to check for this, and handle exceptions as appropriate.

Similarly, the non-deprecated versions of Has() are the two that take context parameters: Maybe<bool> v8::Object::Has(Local<Context> context, Local<Value> key) and Maybe<bool> v8::Object::Has(Local<Context> context, uint32_t index). The Maybe<bool> they return is nothing (.IsNothing() == true) if an exception was thrown, otherwise it's a bool with the result.

Upvotes: 1

Related Questions