|
这个模式里展示了如何创建并返回一个 JavaScript 函数,它是由 C++ 函数包装的 :
- // addon.cc
- #include <node.h>
- using namespace v8;
- void MyFunction(const FunctionCallbackInfo<Value>& args) {
- Isolate* isolate = Isolate::GetCurrent();
- HandleScope scope(isolate);
- args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello world"));
- }
- void CreateFunction(const FunctionCallbackInfo<Value>& args) {
- Isolate* isolate = Isolate::GetCurrent();
- HandleScope scope(isolate);
- Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, MyFunction);
- Local<Function> fn = tpl->GetFunction();
- // omit this to make it anonymous
- fn->SetName(String::NewFromUtf8(isolate, "theFunction"));
- args.GetReturnValue().Set(fn);
- }
- void Init(Handle<Object> exports, Handle<Object> module) {
- NODE_SET_METHOD(module, "exports", CreateFunction);
- }
- NODE_MODULE(addon, Init)
复制代码 测试:
- // test.js
- var addon = require('./build/Release/addon');
- var fn = addon();
- console.log(fn()); // 'hello world'
复制代码
|
|