国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

c++編寫node的addon(8) --factory wrap

2019-11-08 18:41:04
字體:
來源:轉載
供稿:網友

1.環境

Ubuntu 14.04

node 4.5.0

node-gyp 3.4.0

2.項目

新建項目,加入組件nan和bindings

方法一、在項目文件的node_modules中復制組件nan和bindings的全部代碼包;

方法二、在package.json的dependencies中加入這兩個組件,用nmp安裝

3.c++源碼

//myobject.h#ifndef MYOBJECT_H#define MYOBJECT_H#include <nan.h>class MyObject : public Nan::ObjectWrap {	public:		static void Init();		static v8::Local<v8::Object> NewInstance(v8::Local<v8::Value> arg);			PRivate:		MyObject();		~MyObject();				static Nan::Persistent<v8::Function> constructor;				static void New(const Nan::FunctionCallbackInfo<v8::Value>& info);		static void PlusOne(const Nan::FunctionCallbackInfo<v8::Value>& info);				double counter_;};#endif
//myobject.cc#include <nan.h>#include "myobject.h"using namespace v8;MyObject::MyObject(){};MyObject::~MyObject(){};Nan::Persistent<v8::Function> MyObject::constructor;void MyObject::Init() {	Nan::HandleScope scope;		//Prepare constructor template 	v8::Local<v8::FunctionTemplate> tp1 = Nan::New<v8::FunctionTemplate>(New);	tp1->SetClassName(Nan::New("MyObject").ToLocalChecked());	tp1->InstanceTemplate()->SetInternalFieldCount(1);		//Prototype	tp1->PrototypeTemplate()->Set(Nan::New("plusOne").ToLocalChecked(), Nan::New<v8::FunctionTemplate>(PlusOne)->GetFunction());		constructor.Reset(tp1->GetFunction());}void MyObject::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {	MyObject* obj = new MyObject();	obj->counter_ = info[0]->IsUndefined()?0:info[0]->NumberValue();	obj->Wrap(info.This());		info.GetReturnValue().Set(info.This());}v8::Local<v8::Object> MyObject::NewInstance(v8::Local<v8::Value> arg) {	Nan::EscapableHandleScope scope;		const unsigned argc = 1;	v8::Local<v8::Value> argv[argc] = {arg};	v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);	v8::Local<v8::Object> instance = cons->NewInstance(argc, argv);		return scope.Escape(instance);}void MyObject::PlusOne(const Nan::FunctionCallbackInfo<v8::Value>& info) {	MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.This());	obj->counter_ += 1;		info.GetReturnValue().Set(Nan::New(obj->counter_));}
//addon.cc#include <nan.h>#include "myobject.h"void CreateObject(const Nan::FunctionCallbackInfo<v8::Value>& info) {  info.GetReturnValue().Set(MyObject::NewInstance(info[0]));}void InitAll(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {  Nan::HandleScope scope;  MyObject::Init();  module->Set(Nan::New("exports").ToLocalChecked(),      Nan::New<v8::FunctionTemplate>(CreateObject)->GetFunction());}NODE_MODULE(addon, InitAll)4.binding.gyp

{	"targets":[		{			"target_name" : "addon",			"sources" : ["addon.cc", "myobject.cc"],			"include_dirs" : [				"<!(node -e /"require('nan')/")"			]		}			]}5.js源碼

//addon.jsvar addon = require('bindings')('addon');var obj = addon(10);console.log(obj.plusOne());console.log(obj.plusOne());console.log(obj.plusOne());var obj2 = addon(20);console.log(obj2.plusOne());console.log(obj2.plusOne());console.log(obj2.plusOne());6.編譯addon

cd到源碼目錄下

node-gyp configure build     7.執行

cd 到源碼目錄下

node hello.js
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 信阳市| 宁国市| 离岛区| 灵寿县| 淅川县| 达日县| 尼勒克县| 佛冈县| 慈利县| 龙岩市| 溆浦县| 南乐县| 手游| 张家界市| 南投市| 磴口县| 济宁市| 大埔区| 报价| 吉林省| 苍梧县| 拉萨市| 彭泽县| 赤壁市| 青岛市| 临朐县| 高尔夫| 砀山县| 克什克腾旗| 资兴市| 板桥市| 永仁县| 白山市| 聂拉木县| 武平县| 垦利县| 黑龙江省| 武川县| 穆棱市| 吉林市| 永德县|