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

a 夏天 提交于 2021-02-08 10:38:37

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/58219928/node-12-v8-converting-deprecated-code-to-the-maybe-version

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!