gpt4 book ai didi

c++ - 将对象传递给 Node.js C++ 插件

转载 作者:行者123 更新时间:2023-11-28 04:40:26 24 4
gpt4 key购买 nike

https://medium.com/@muehler.v/tutorial-to-node-js-native-c-modules-part-2-arrays-json-and-callbacks-9b81f09874cd

我正在阅读上面的文章,试图访问传递给 C++ Node 插件函数的对象中的字符串。我的功能在下面,但我无法将其编译成插件。当我这样做时,我得到了错误:

../addon.cpp:239:26: error: no viable conversion from 'MaybeLocal<v8::Value>' (aka 'v8::MaybeLocal<v8::Value>') to 'v8::Local<v8::Value>'
v8::Local<v8::Value> pwdValue = Nan::Get(obj, pwdProp);
^ ~~~~~~~~~~~~~~~~~~~~~~

关于如何解决此错误的任何想法?谢谢

NAN_METHOD(init) {
v8::Local<v8::Object> obj = info[0]->ToObject();
v8::Local<v8::String> pwdProp = Nan::New("PWD").ToLocalChecked();

std::string pwd = "";

if (Nan::HasOwnProperty(obj, pwdProp).FromJust()) {
v8::Local<v8::Value> pwdValue = Nan::Get(obj, pwdProp);
pwd = std::string(*Nan::Utf8String(pwdValue->ToString()));
}

std::cout << pwd << std::endl;
}

最佳答案

Nan::Get返回 MaybeLocal ,由于 docs 中提到的原因可能为空.

您可以获得Local来自 MaybeLocal 的句柄通过使用 .ToLocalChecked()如下:v8::Local<v8::Value> pwdValue = Nan::Get(obj, pwdProp).ToLocalChecked(); .

要检查 MaybeLocal 是否实际持有一个值,您可以检查 bool isEmpty = Nan::Get(obj, pwdProp).IsEmpty() .

关于c++ - 将对象传递给 Node.js C++ 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50284487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com