Javascript 编写异步CPP节点JS(0.5.3+;)模块 < >我正在寻找一种用当前版本(0.5.9)来为NoDEJS构建C++模块的方法。 通过使用以下教程并从node_file.cc中提取节点,我能够自己为node 0.5.3构建一个模块
但是对于节点0.5.4,一些API必须已经改变,因为我不再能够用eio_*扭曲函数 查看node_file.cc,我发现eio_*包装被新的ReqWrap类取代 例如,在此宏中:Javascript 编写异步CPP节点JS(0.5.3+;)模块 < >我正在寻找一种用当前版本(0.5.9)来为NoDEJS构建C++模块的方法。 通过使用以下教程并从node_file.cc中提取节点,我能够自己为node 0.5.3构建一个模块,javascript,c++,node.js,Javascript,C++,Node.js,但是对于节点0.5.4,一些API必须已经改变,因为我不再能够用eio_*扭曲函数 查看node_file.cc,我发现eio_*包装被新的ReqWrap类取代 例如,在此宏中: 不,我想知道编写异步扩展的最佳方式是什么?基本上,通过查看加密节点模块解决了我的问题。该模块没有像文件模块那样受到宏的干扰。我提出了一个简单的异步模块,用于计算两个整数之和: #include <v8.h> #include <node.h> #include <stdlib.h>
不,我想知道编写异步扩展的最佳方式是什么?基本上,通过查看加密节点模块解决了我的问题。该模块没有像文件模块那样受到宏的干扰。我提出了一个简单的异步模块,用于计算两个整数之和:
#include <v8.h>
#include <node.h>
#include <stdlib.h>
#include <errno.h>
using namespace node;
using namespace v8;
struct Test_req
{
ssize_t result;
ssize_t int1;
ssize_t int2;
Persistent<Function> callback;
};
void TestWorker(uv_work_t* req)
{
Test_req* request = (Test_req*)req->data;
request->result = request->int1 + request->int2;
}
void TestAfter(uv_work_t* req)
{
HandleScope scope;
Test_req* request = (Test_req*)req->data;
delete req;
Handle<Value> argv[2];
// XXX: Error handling
argv[0] = Undefined();
argv[1] = Integer::New(request->result);
TryCatch try_catch;
request->callback->Call(Context::GetCurrent()->Global(), 2, argv);
if (try_catch.HasCaught())
{
FatalException(try_catch);
}
request->callback.Dispose();
delete request;
}
static Handle<Value> Test(const Arguments& args)
{
HandleScope scope;
if ( args.Length() < 3 || !args[0]->IsNumber() || !args[1]->IsNumber() )
{
return ThrowException(Exception::TypeError(String::New("Bad argument")));
}
ssize_t int1 ( args[0]->Int32Value() );
ssize_t int2 ( args[1]->Int32Value() );
if ( args[2]->IsFunction() )
{
Local<Function> callback = Local<Function>::Cast(args[2]);
Test_req* request = new Test_req;
request->callback = Persistent<Function>::New(callback);
request->int1 = int1;
request->int2 = int2;
uv_work_t* req = new uv_work_t();
req->data = request;
uv_queue_work(uv_default_loop(), req, TestWorker, TestAfter);
}
else
{
return ThrowException(Exception::TypeError(String::New("Callback missing")));
}
return Undefined();
}
extern "C"
{
static void init(Handle<Object> target)
{
HandleScope scope;
}
}
NODE_MODULE(node_AsyncTest, init);
结果:
undefined 3
希望这有帮助;)
Ps:因为windows下缺少节点编译扩展。我正在使用Visual Studio构建解决方案将其直接构建到节点windows端口的核心。那么,我们可以实现自己的异步模块来执行复杂的计算吗?这会帮助我们阻止节点线程吗?
undefined 3