我如何使用std::string在一个c++插件的Node.js
How do I use std::string in a C++ Addon for Node.js?
我试图包装c++对象在javascript中使用按照这里的node.js文档:https://nodejs.org/api/addons.html#addons_wrapping_c_objects
插件将构建没有错误,并正常工作时,我的对象"AnObject"只有数字属性,即"int32_t年龄;"。
当我添加属性"std::string name;"到AnObject, "node-gyp configure"工作,然而"node-gyp build"给出以下错误:
Wills-MacBook-Pro:cppObjectWrapping willalley$ node-gyp build
[...]
In file included from ../personAddon.cc:3:
../AnObject.h:24:21: error: implicit instantiation of undefined template
'std::basic_string<char, std::char_traits<char>,
std::allocator<char> >'
std::string name;
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
/MacOSX10.12.sdk/usr/include/c++/4.2.1/bits/stringfwd.h:56:11: note:
template is declared here
class basic_string;
^
1 error generated.
[...]
Wills-MacBook-Pro:cppObjectWrapping willalley$
这是抛出错误的对象头文件,AnObject.h:
#pragma once
#include <node.h>
#include <node_object_wrap.h>
#include <string.h>
namespace demo {
class AnObject : public node::ObjectWrap {
public:
static void Init(v8::Local<v8::Object> exports);
private:
explicit AnObject(int32_t num = 19);
~AnObject();
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void setAge(const v8::FunctionCallbackInfo<v8::Value>& args);
static void getAge(const v8::FunctionCallbackInfo<v8::Value>& args);
static void setName(const v8::FunctionCallbackInfo<v8::Value>& args);
static void getName(const v8::FunctionCallbackInfo<v8::Value>& args);
static v8::Persistent<v8::Function> constructor;
int32_t age;
std::string name;
};
} // namespace demo
,如果你很好奇,实现文件AnObject.cc:
#include "AnObject.h"
#include <string.h>
namespace demo {
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::Persistent;
using v8::String;
using v8::Value;
Persistent<Function> AnObject::constructor;
AnObject::AnObject(int32_t age_) : age(age_) {
}
AnObject::~AnObject() {
}
void AnObject::Init(Local<Object> exports) {
Isolate* isolate = exports->GetIsolate();
// Prepare constructor template
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
tpl->SetClassName(String::NewFromUtf8(isolate, "AnObject"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
NODE_SET_PROTOTYPE_METHOD(tpl, "setAge", setAge);
NODE_SET_PROTOTYPE_METHOD(tpl, "setName", setName);
NODE_SET_PROTOTYPE_METHOD(tpl, "getAge", getAge);
NODE_SET_PROTOTYPE_METHOD(tpl, "getName", getName);
constructor.Reset(isolate, tpl->GetFunction());
exports->Set(String::NewFromUtf8(isolate, "AnObject"),
tpl->GetFunction());
}
void AnObject::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.IsConstructCall()) {
// Invoked as constructor: `new AnObject(...)`
int32_t value = args[0]->IsUndefined() ? 0 : args[0]->Int32Value();;
AnObject* obj = new AnObject(value);
obj->Wrap(args.This());
args.GetReturnValue().Set(args.This());
} else {
// Invoked as plain function `AnObject(...)`, turn into construct call.
const int argc = 1;
Local<Value> argv[argc] = { args[0] };
Local<Context> context = isolate->GetCurrentContext();
Local<Function> cons = Local<Function>::New(isolate, constructor);
Local<Object> result =
cons->NewInstance(context, argc, argv).ToLocalChecked();
args.GetReturnValue().Set(result);
}
}
void AnObject::setAge(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
AnObject* obj = ObjectWrap::Unwrap<AnObject>(args.Holder());
if (args[0]->IsInt32()){
obj->age = args[0]->Int32Value();
args.GetReturnValue().Set(Number::New(isolate, obj->age));
}
}
void AnObject::setName(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
AnObject* obj = ObjectWrap::Unwrap<AnObject>(args.Holder());
if (args[0]->isString()) {
std::string input(*v8::String::Utf8Value(args[0]->ToString()));
obj->name = input;
args.GetReturnValue().Set(Number::New(isolate, obj->name));
}
}
void AnObject::getAge(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
AnObject* obj = ObjectWrap::Unwrap<AnObject>(args.Holder());
args.GetReturnValue().Set(Number::New(isolate, obj->age));
}
void AnObject::getName(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
AnObject* obj = ObjectWrap::Unwrap<AnObject>(args.Holder());
v8::Local<v8::String> output = v8::String::NewFromUtf8(isolate, name.cstr());
args.GetReturnValue().Set(Number::New(isolate, output));
}
} // namespace demo
我找到的解决方案是:
改变:
#include <string.h>
:
#include <string>
AnObject.h和AnObject.cc中的相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 从链接列表c++中删除一个项目
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '