在 Node Addons 中添加两个字符串参数
Adding two string arguments in Node Addons
我想在我的插件中添加两个字符串参数,但我无法让它工作。我总是得到关于两个参数类型的错误。
我的函数:
void Add(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
// Check the number of arguments passed.
if (args.Length() < 2) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")
.ToLocalChecked()));
return;
}
// Check the argument types
if (!args[0]->IsString() || !args[1]->IsString()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments").ToLocalChecked()));
return;
}
Local<String> arg1 = args[0]->ToString(context).ToLocalChecked();
Local<String> arg2 = args[1]->ToString(context).ToLocalChecked();
args.GetReturnValue().Set(arg1 + arg2);
}
整个文件:
#include <node.h>
#include <string>
namespace demo {
using v8::Context;
using v8::Exception;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;
void Add(const FunctionCallbackInfo<Value> &args) {...
void Init(Local<Object> exports) { NODE_SET_METHOD(exports, "add", Add); }
NODE_MODULE(NODE_GYP_MODULE_NAME, Init);
}
如有任何反馈,我们将不胜感激。
找到解决方案。唯一的问题是我应该将 Local<String>
类型转换为 std::string
。并且加在一起之后,返回值的时候再转换回Local<String>
。
代码:
void Add(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
// Check the number of arguments passed.
if (args.Length() < 2) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")
.ToLocalChecked()));
return;
}
// Check the argument types
if (!args[0]->IsString() || !args[1]->IsString()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments").ToLocalChecked()));
return;
}
v8::String::Utf8Value str1(isolate, args[0]);
v8::String::Utf8Value str2(isolate, args[1]);
std::string searchWord(*str1);
std::string body(*str2);
std::string res = searchWord + body;
args.GetReturnValue().Set(
String::NewFromUtf8(isolate, res.c_str()).ToLocalChecked());
}
我想在我的插件中添加两个字符串参数,但我无法让它工作。我总是得到关于两个参数类型的错误。
我的函数:
void Add(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
// Check the number of arguments passed.
if (args.Length() < 2) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")
.ToLocalChecked()));
return;
}
// Check the argument types
if (!args[0]->IsString() || !args[1]->IsString()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments").ToLocalChecked()));
return;
}
Local<String> arg1 = args[0]->ToString(context).ToLocalChecked();
Local<String> arg2 = args[1]->ToString(context).ToLocalChecked();
args.GetReturnValue().Set(arg1 + arg2);
}
整个文件:
#include <node.h>
#include <string>
namespace demo {
using v8::Context;
using v8::Exception;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;
void Add(const FunctionCallbackInfo<Value> &args) {...
void Init(Local<Object> exports) { NODE_SET_METHOD(exports, "add", Add); }
NODE_MODULE(NODE_GYP_MODULE_NAME, Init);
}
如有任何反馈,我们将不胜感激。
找到解决方案。唯一的问题是我应该将 Local<String>
类型转换为 std::string
。并且加在一起之后,返回值的时候再转换回Local<String>
。
代码:
void Add(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
// Check the number of arguments passed.
if (args.Length() < 2) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")
.ToLocalChecked()));
return;
}
// Check the argument types
if (!args[0]->IsString() || !args[1]->IsString()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments").ToLocalChecked()));
return;
}
v8::String::Utf8Value str1(isolate, args[0]);
v8::String::Utf8Value str2(isolate, args[1]);
std::string searchWord(*str1);
std::string body(*str2);
std::string res = searchWord + body;
args.GetReturnValue().Set(
String::NewFromUtf8(isolate, res.c_str()).ToLocalChecked());
}