用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"}
]
}

运行代码产生器:

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

生成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

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;
}

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

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.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源码

Google V8扩展利器发布:v8-native-binding-generator的更多相关文章

  1. 解决高版本 Google Chrome 扩展程序强制停用问题 -摘自网络

    1]前往这里下载你喜欢的语言的组策略模板 后缀为.adm (其他的文件自己看 https://docs.google.com/viewer?a=v&pid=sites&srcid=Y2 ...

  2. Chrome浏览器扩展开发系列之一:初识Google Chrome扩展

    1.       Google Chrome扩展简介 Google Chrome扩展是一种软件,以增强Chrome浏览器的功能. Google Chrome扩展使用HTML.JavaScript.CS ...

  3. V8世界探险 (1) - v8 API概览

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/lusing/article/detai ...

  4. 15个网页设计必备的Google Chrome 扩展

    2011年第一篇,翻译自freelancefolder的一篇文章.以下为译文内容: 最近,我将Google Chrome作为了我的主力浏览器,同时,将其作为我设计和开发网页的工具,尽管我还时常会去Fi ...

  5. 使用CoreRT将.NET Core发布为Native应用程序

    在上一篇文章<使用.NET Core快速开发一个较正规的命令行应用程序>中我们看到了使用自包含方式发布的.NET Core应用中包含了216个文件.我就写一个cat命令用得着这么动真格.. ...

  6. Chrome V8系列--浅析Chrome V8引擎中的垃圾回收机制和内存泄露优化策略

    V8 实现了准确式 GC,GC 算法采用了分代式垃圾回收机制.因此,V8 将内存(堆)分为新生代和老生代两部分. 一.前言 V8的垃圾回收机制:JavaScript使用垃圾回收机制来自动管理内存.垃圾 ...

  7. SPI:Java的高可扩展利器

    摘要:JAVA SPI,基于接口的编程+策略模式+配置文件的动态加载机制. 本文分享自华为云社区<一文讲透Java核心技术之高可扩展利器SPI>,作者: 冰 河. SPI的概念 JAVA ...

  8. Aspose.Pdf v8.4.1 发布

    .NET v8.4.1: 修复的错误及漏洞: PDF到JPEG时,内容从最终JPEG文件中丢失. 书签缩放识别错误.  Java v4.2: 新特性: 搜索分离超过两行的文本. 修复的异常: PDF到 ...

  9. 添加了有道生词本的 chrome google翻译扩展和有道翻译扩展

    在chrome发布项目,需要先花美金认证,还得要美国ID,无奈. 直接上源码,需手动导入. 原始项目源码并未开源,个人是从chrome本地文件里拿出来的,拓展来的,侵删(本来想着自已写一个,业余时间, ...

随机推荐

  1. 公共控件Listview

    ListView属性中,Items是行的总集合,Items集合中的每一个是一行,Items集合里面有ListViewItem集合,这个集合实例化:ListViewItem li=new ListVie ...

  2. SqlSever基础 一个条件group by 一列有两个内容,分组并查看每个内容有多少行,并用as起名

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  3. eclipse中LogCat有时不显示信息的简单解决办法

    android的开发中,经常需要使用到LogCat查看打印的信息. 但是eclipse中的LagCat,有时会刷新不出打印的日志. 特别是在点击ClearLog按钮后,经常会出现上面说的这种情况. 一 ...

  4. 我的android学习经历15

    利用Intent实现有返回结果的页面跳转 主要用的方法: (1)Intent的构造方法:intent(当前界面对象,要跳转的界面.class); (2)接受结果的方法onActivityResult( ...

  5. 编写一个类A,该类创建的对象可以调用方法f输出小写的英文字母表。然 后再编写一个A类的子类B,要求子类B必须继承类A的方法f(不允许重写), 子类B创建的对象不仅可以调用方法f输出小写的英文字母表,而且可以调用子 类新增的方法g输出大写的英文字母表。最后编写主类C,在主类的main方法 中测试类A与类B。

    package zimu; public class A { public void f() { for (int i = 97; i <123; i++) { System.out.print ...

  6. UVA 10453 十七 Make Palindrome

    Make Palindrome Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit St ...

  7. Difference between Char.IsDigit() and Char.IsNumber() in C#

    http://stackoverflow.com/questions/228532/difference-between-char-isdigit-and-char-isnumber-in-c-sha ...

  8. Nessus基本命令

    /etc/init.d/nessusd start 启动nessusd服务 默认端口8834 添加用户(未知是什么用户) sudo /opt/nessus/sbin/nessus-adduser

  9. Servlet&jsp基础:第四部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  10. ABAP Util代码

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...