node-gyp使用指南

1.使用方法

1.1 hello world

#include 

namespace demo {
    using v8::FunctionCallbackInfo;
    using v8::Isolate;
    using v8::Local;
    using v8::Object;
    using v8::String;
    using v8::Value;

    void Method(const FunctionCallbackInfo& args) {
        Isolate* isolate = args.GetIsolate();
        args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
    }

    //初始化函数
    void init(Local exports) {
        //导出Method方法,重命名为hello
        NODE_SET_METHOD(exports,"hello",Method);
    }

    //模块名为addon
    NODE_MODULE(addon, init)
}

要把一个函数作为接口导出,需要:
1.为函数传入FunctionCallbackInfo类型的参数列表,这个参数列表是一个指针数组,可以通过args[index]来访问每一个传入参数,通过args[index]->ToString来调用参数对应的方法
2.在函数中创建一个js的数据类型都要通过

Isolate* isolate = args.GetIsolate()

这个隔离对象指针来创建,例如:

//创建基本数据类型并赋给一个本地变量
String::NewFromUtf8(isolate,"msg");
Local num=Number::New(isolate,value);
//创建一个object
Local obj=Object::New(isolate);
//读取一个回调函数
Local cb = Local::Cast(args[0]);

3.为函数创建返回值,函数的返回值必须时v8的变量类型

args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));

4.需要创建一个初始化函数,函数的第一个参数为module.exports,第二个参数为module,可以选择是同NODE_SET_METHOD宏来为module.exports添加导出的属性,也可以在module对象上完全重写exports属性

NODE_SET_METHOD(exports,"hello",Method);

5.最后,使用NODE_MODULE宏指定模块名和初始化函数,即可导出一个函数在js中调用

1.2 函数的参数

读取传入参数和返回结果需要在js数据类型和C++数据类型之间做转换,读取并转化为C++数据类型,即可通过C++进行计算

ars[0]->NumberValue();   //转化为number
args[1]->ToString();   //转化为string

1.3 导出对象

#ifndef MYOBJECT_H
#define MYOBJECT_H

#include 
#include 

namespace demo 
{

    class MyObject: public node::ObjectWrap
    {
    public:
        static void Init(v8::Local exports);

    private:
        explicit MyObject(double value = 0);
        ~MyObject();

        static void New(const v8::FunctionCallbackInfo& args);
        static void PlusOne(const v8::FunctionCallbackInfo& args);
        static v8::Persistent constructor;
        double value_;
    };

}//namespace demo

#endif
#include "myobject.h"

namespace demo {

    using namespace v8;

    Persistent MyObject::constructor;

    //初始化函数
    void MyObject::Init(v8::Local exports)
    {
        Isolate* isolate = exports->GetIsolate();

        //创建构造函数
        Local tpl = FunctionTemplate::New(isolate, New);
        tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
        tpl->InstanceTemplate()->SetInternalFieldCount(1);

        //在原型上添加方法
        NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);

        //设置构造函数
        constructor.Reset(isolate, tpl->GetFunction());
        exports->Set(String::NewFromUtf8(isolate, "MyObject"), tpl->GetFunction());
    }

    MyObject::MyObject(double value)
        :value_(value)
    {
    }

    MyObject::~MyObject()
    {
    }

    //v8构造函数
    void MyObject::New(const v8::FunctionCallbackInfo& args)
    {
        Isolate* isolate = args.GetIsolate();
        
        //构造函数调用
        if (args.IsConstructCall())
        {
            double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
            MyObject* obj = new MyObject(value);

            //this指向这个对象
            obj->Wrap(args.This());
            args.GetReturnValue().Set(args.This());
        }
        else {
            //否则调用v8构造函数,读取参数放入参数列表
            const int argc = 1;
            Local argv[argc] = { args[0] };
            Local context = isolate->GetCurrentContext();
            Local cons = Local::New(isolate, constructor);
            Local result = cons->NewInstance(context, argc, argv).ToLocalChecked();
            args.GetReturnValue().Set(result);
        }
    }

    //将成员变量加一
    void MyObject::PlusOne(const v8::FunctionCallbackInfo& args)
    {
        Isolate* isolate = args.GetIsolate();

        MyObject* obj = ObjectWrap::Unwrap(args.Holder());
        obj->value_ += 1;

        args.GetReturnValue().Set(Number::New(isolate, obj->value_));
    }

}//namespace demo







你可能感兴趣的:(node-gyp使用指南)