原生 c++ Node.js 的扩展:“克隆”本地<值>在构造函数中值>
我读过教程“编写 Node.js 本机扩展”: https://www.cloudkick.com/blog/2010/aug/23/writing-nodejs-native-extensions
代码工作正常( https://github.com/pquerna/node-extension-examples /blob/master/helloworld/helloworld.cc )
现在我想更改:更改
class HelloWorld: ObjectWrap
{
private:
int m_count;
public:
(...)
HelloWorld() :
m_count(0)
{
}
(...)
static Handle<Value> Hello(const Arguments& args)
{
HandleScope scope;
HelloWorld* hw = ObjectWrap::Unwrap<HelloWorld>(args.This());
hw->m_count++;
Local<String> result = String::New("Hello World");
return scope.Close(result);
}
(...)
}
为类似的内容(在构造函数中复制参数并在 'Hello()' 中返回它):
class HelloWorld: ObjectWrap
{
private:
Local< Value > myval;/* <===================== */
public:
(...)
HelloWorld(const Local< Value >& v) :
myval(v) /* <===================== */
{
}
(...)
static Handle<Value> Hello(const Arguments& args)
{
HandleScope scope;
HelloWorld* hw = ObjectWrap::Unwrap<HelloWorld>(args.This());
return scope.Close(hw->myval);/* <===================== */
}
(...)
}
我的代码似乎没有工作, hello() 似乎返回一个整数
var h=require("helloworld");
var H=new h.HelloWorld("test");
console.log(H.hello());
在构造函数中复制 myval 并在函数 'Hello()' 中返回 myval 的正确方法是什么?我应该在析构函数中管理一些东西吗?
谢谢。
皮埃尔
I've read the tutorial "Writing Node.js Native Extensions": https://www.cloudkick.com/blog/2010/aug/23/writing-nodejs-native-extensions
The code worked fine ( https://github.com/pquerna/node-extension-examples/blob/master/helloworld/helloworld.cc )
Now I want to change:
class HelloWorld: ObjectWrap
{
private:
int m_count;
public:
(...)
HelloWorld() :
m_count(0)
{
}
(...)
static Handle<Value> Hello(const Arguments& args)
{
HandleScope scope;
HelloWorld* hw = ObjectWrap::Unwrap<HelloWorld>(args.This());
hw->m_count++;
Local<String> result = String::New("Hello World");
return scope.Close(result);
}
(...)
}
to something like that ( copy a parameter in the constructor and return it in 'Hello()' ):
class HelloWorld: ObjectWrap
{
private:
Local< Value > myval;/* <===================== */
public:
(...)
HelloWorld(const Local< Value >& v) :
myval(v) /* <===================== */
{
}
(...)
static Handle<Value> Hello(const Arguments& args)
{
HandleScope scope;
HelloWorld* hw = ObjectWrap::Unwrap<HelloWorld>(args.This());
return scope.Close(hw->myval);/* <===================== */
}
(...)
}
my code doesn't seem to work, hello() seems to return an integer
var h=require("helloworld");
var H=new h.HelloWorld("test");
console.log(H.hello());
What's the right way to copy myval in the constructor and return myval in the function 'Hello()' ? And should I manage something in the destructor ?
Thanks.
Pierre
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“本地”变量将被自动清除,因此您不能像这样保存它们的副本。您需要使用“持久”句柄。
'Local' variables will be automatically cleaned up, so you can't just save a copy of them like that. You need to use a 'Persistent' Handle.