Google V8扩展利器发布 v8-native-binding-generator

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       

用C++扩展Google V8很简单,但是类比较多时还是很烦的。前段时间开发cantk-runtime-v8时,我写了一个代码产生器v8-native-binding-generator,让扩展Google V8变得非常方便,甚至无需要了解V8本身。具体用法如下:

先写一个JSON的类描述文件,下面这段JSON是我用来模拟XMLHttpRequest的:

{    "className":"HttpClient",    "functions":[        {            "name":"send",            "returnType" : "bool",            "args"   : [                {"name":"onProgress", "type":"function"},                {"name":"onDone", "type":"function"}            ]        }    ],    "attributes" : [        {"name":"url", "type":"string"},        {"name":"returnType", "type":"string"},        {"name":"method", "type":"string"},        {"name":"requestHeaders", "type":"string"},        {"name":"requestData", "type":"string"},        {"name":"status", "type":"int32_t"},        {"name":"statusText", "type":"string"},        {"name":"responseHeaders", "type":"string"},        {"name":"responseText", "type":"string"}    ]}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

运行代码产生器:

node gen-v8-binding.js idl/http_client.json
  
  • 1

生成4个文件,依次是HttpClient类的头文件和CPP文件,HttpClientBinding类的头文件和CPP文件:

HttpClient.h

#ifndef _HTTPCLIENT_H#define _HTTPCLIENT_H#include <assert.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <string>#include <v8.h>#include <nan/nan.h>using namespace std;using namespace v8;class HttpClient: public ObjectWrap {public:    HttpClient();    ~HttpClient();    bool send(NanCallback*  onProgress, NanCallback*  onDone);    string getUrl() const;    void setUrl(string url);    string getReturnType() const;    void setReturnType(string returnType);    string getMethod() const;    void setMethod(string method);    string getRequestHeaders() const;    void setRequestHeaders(string requestHeaders);    string getRequestData() const;    void setRequestData(string requestData);    int32_t getStatus() const;    void setStatus(int32_t status);    string getStatusText() const;    void setStatusText(string statusText);    string getResponseHeaders() const;    void setResponseHeaders(string responseHeaders);    string getResponseText() const;    void setResponseText(string responseText);private:    string _url;    string _returnType;    string _method;    string _requestHeaders;    string _requestData;    int32_t _status;    string _statusText;    string _responseHeaders;    string _responseText;};#endif
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

HttpClient.cpp

#include "HttpClient.h"HttpClient::HttpClient(){}HttpClient::~HttpClient(){}bool HttpClient::send(NanCallback*  onProgress, NanCallback*  onDone) {}string HttpClient::getUrl() const {    return this->_url;}void HttpClient::setUrl(string url) {    this->_url = url;}string HttpClient::getReturnType() const {    return this->_returnType;}void HttpClient::setReturnType(string returnType) {    this->_returnType = returnType;}string HttpClient::getMethod() const {    return this->_method;}void HttpClient::setMethod(string method) {    this->_method = method;}string HttpClient::getRequestHeaders() const {    return this->_requestHeaders;}void HttpClient::setRequestHeaders(string requestHeaders) {    this->_requestHeaders = requestHeaders;}string HttpClient::getRequestData() const {    return this->_requestData;}void HttpClient::setRequestData(string requestData) {    this->_requestData = requestData;}int32_t HttpClient::getStatus() const {    return this->_status;}void HttpClient::setStatus(int32_t status) {    this->_status = status;}string HttpClient::getStatusText() const {    return this->_statusText;}void HttpClient::setStatusText(string statusText) {    this->_statusText = statusText;}string HttpClient::getResponseHeaders() const {    return this->_responseHeaders;}void HttpClient::setResponseHeaders(string responseHeaders) {    this->_responseHeaders = responseHeaders;}string HttpClient::getResponseText() const {    return this->_responseText;}void HttpClient::setResponseText(string responseText) {    this->_responseText = responseText;}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92

HttpClientBinding.h

#ifndef _HTTPCLIENTBINDING_H#define _HTTPCLIENTBINDING_H#include <assert.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string>#include <string.h>#include <v8.h>#include <nan/nan.h>using namespace v8;void HttpClientInitBinding(Handle<Object> target);#endif
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

HttpClientBinding.cpp

#include "HttpClient.h"#include "HttpClientBinding.h"NAN_METHOD(newHttpClient) {    NanScope();    HttpClient* obj = new HttpClient();    obj->Wrap(args.This());    NanReturnValue(args.This());}NAN_METHOD(HttpClientSend) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    if(args.Length() == 2) {        NanCallback* onProgress = new NanCallback(args[0].As<Function>());        NanCallback* onDone = new NanCallback(args[1].As<Function>());        bool retVal = obj->send(onProgress, onDone);        NanReturnValue(NanNew<Boolean>(retVal));        return;    }}NAN_GETTER(HttpClientGetUrl) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    NanReturnValue(NanNew<String>(obj->getUrl()));}NAN_SETTER(HttpClientSetUrl) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    if (value->IsString()) {        v8::String::Utf8Value nativeValue(value);        obj->setUrl(*nativeValue);    }else{        printf("invalid data type for HttpClient.url\n");    }}NAN_GETTER(HttpClientGetReturnType) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    NanReturnValue(NanNew<String>(obj->getReturnType()));}NAN_SETTER(HttpClientSetReturnType) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    if (value->IsString()) {        v8::String::Utf8Value nativeValue(value);        obj->setReturnType(*nativeValue);    }else{        printf("invalid data type for HttpClient.returnType\n");    }}NAN_GETTER(HttpClientGetMethod) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    NanReturnValue(NanNew<String>(obj->getMethod()));}NAN_SETTER(HttpClientSetMethod) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    if (value->IsString()) {        v8::String::Utf8Value nativeValue(value);        obj->setMethod(*nativeValue);    }else{        printf("invalid data type for HttpClient.method\n");    }}NAN_GETTER(HttpClientGetRequestHeaders) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    NanReturnValue(NanNew<String>(obj->getRequestHeaders()));}NAN_SETTER(HttpClientSetRequestHeaders) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    if (value->IsString()) {        v8::String::Utf8Value nativeValue(value);        obj->setRequestHeaders(*nativeValue);    }else{        printf("invalid data type for HttpClient.requestHeaders\n");    }}NAN_GETTER(HttpClientGetRequestData) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    NanReturnValue(NanNew<String>(obj->getRequestData()));}NAN_SETTER(HttpClientSetRequestData) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    if (value->IsString()) {        v8::String::Utf8Value nativeValue(value);        obj->setRequestData(*nativeValue);    }else{        printf("invalid data type for HttpClient.requestData\n");    }}NAN_GETTER(HttpClientGetStatus) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    NanReturnValue(NanNew<Int32>(obj->getStatus()));}NAN_SETTER(HttpClientSetStatus) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    if (value->IsInt32()) {        int32_t nativeValue = value->Int32Value();        obj->setStatus(nativeValue);    }else{        printf("invalid data type for HttpClient.status\n");    }}NAN_GETTER(HttpClientGetStatusText) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    NanReturnValue(NanNew<String>(obj->getStatusText()));}NAN_SETTER(HttpClientSetStatusText) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    if (value->IsString()) {        v8::String::Utf8Value nativeValue(value);        obj->setStatusText(*nativeValue);    }else{        printf("invalid data type for HttpClient.statusText\n");    }}NAN_GETTER(HttpClientGetResponseHeaders) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    NanReturnValue(NanNew<String>(obj->getResponseHeaders()));}NAN_SETTER(HttpClientSetResponseHeaders) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    if (value->IsString()) {        v8::String::Utf8Value nativeValue(value);        obj->setResponseHeaders(*nativeValue);    }else{        printf("invalid data type for HttpClient.responseHeaders\n");    }}NAN_GETTER(HttpClientGetResponseText) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    NanReturnValue(NanNew<String>(obj->getResponseText()));}NAN_SETTER(HttpClientSetResponseText) {    NanScope();    HttpClient* obj = ObjectWrap::Unwrap<HttpClient>(args.This());    if (value->IsString()) {        v8::String::Utf8Value nativeValue(value);        obj->setResponseText(*nativeValue);    }else{        printf("invalid data type for HttpClient.responseText\n");    }}static Persistent<FunctionTemplate> constructor;void HttpClientInitBinding(Handle<Object> target) {    NanScope();    Local<FunctionTemplate> ctor = NanNew<FunctionTemplate>(newHttpClient);    NanAssignPersistent(constructor, ctor);    ctor->InstanceTemplate()->SetInternalFieldCount(1);    ctor->SetClassName(NanNew("HttpClient"));    Local<ObjectTemplate> proto = ctor->PrototypeTemplate();    proto->SetAccessor(NanNew("url"), HttpClientGetUrl, HttpClientSetUrl);    proto->SetAccessor(NanNew("returnType"), HttpClientGetReturnType, HttpClientSetReturnType);    proto->SetAccessor(NanNew("method"), HttpClientGetMethod, HttpClientSetMethod);    proto->SetAccessor(NanNew("requestHeaders"), HttpClientGetRequestHeaders, HttpClientSetRequestHeaders);    proto->SetAccessor(NanNew("requestData"), HttpClientGetRequestData, HttpClientSetRequestData);    proto->SetAccessor(NanNew("status"), HttpClientGetStatus, HttpClientSetStatus);    proto->SetAccessor(NanNew("statusText"), HttpClientGetStatusText, HttpClientSetStatusText);    proto->SetAccessor(NanNew("responseHeaders"), HttpClientGetResponseHeaders, HttpClientSetResponseHeaders);    proto->SetAccessor(NanNew("responseText"), HttpClientGetResponseText, HttpClientSetResponseText);    NAN_SET_PROTOTYPE_METHOD(ctor, "send", HttpClientSend);    target->Set(NanNew("HttpClient"), ctor->GetFunction());}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208

目前支持的数据类型有:
* 1.string 字符串
* 2.int32_t 整数
* 3.int64_t 整数
* 4.double 浮点数
* 5.bool 布尔变量
* 6.function 回调函数(目前只能用于参数)
* 7.其它对象指针(如Image*),要求对象的类也是用本工具产生出来的。

更多例子请参考:https://github.com/drawapp8/cantk-runtime-v8

1.v8-native-binding-generator源码

           

给我老师的人工智能教程打call!http://blog.youkuaiyun.com/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值