WebKit之JS内嵌C++对象
来源:互联网 发布:win10怎么安装软件 编辑:程序博客网 时间:2024/05/20 19:48
3. JS 嵌入C++示例代码
一个Hello world!
- // Enter the created context for compiling and
- // running the hello world script.
- Context::Scope context_scope(context);
- // Create a string containing the JavaScript source code.
- Handle<String> source = String::New("'Hello' + ', World!'");
- // Compile the source code.
- Handle<Script> script = Script::Compile(source);
- // Run the script to get the result.
- Handle<Value> result = script->Run();
- // Dispose the persistent context.
- context.Dispose();
- // Convert the result to an ASCII string and print it.
- String::AsciiValue ascii(result);
- printf("%s\n", *ascii);
- return 0;
更多示例代码参考:V8\test\cctest\*.cc
4.支持C++类
CProxyV8提供便利的方式将C++类的属性和方法导出到JavaScript环境中。支持generic Functors,不需要为每个属性和方法设置回调函数。
- class Point
- {
- public:
- /** Constructor without params is required */
- Point(int px = 0, int py = 0) : x_(px), y_(py) {}
- Point(Point* p) : x_(p->x_), y_(p->y_) {}
- int x_;
- int y_;
- /** Example call to function without params */
- int GetManhattanLength() { return std::abs(x_) - std::abs(y_); }
- /** this will create a JS object, with JS destructor */
- Point* Copy() { return new Point(); }
- /** Example call to function with params */
- v8::Handle<v8::Value> Translate(const v8::Arguments& args)
- {
- // throw if we didn't get 2 args
- if (args.Length() != 2)
- return ThrowException(v8::String::New("Expected 2 arguments"));
- x_ += args[0]->Int32Value();
- y_ += args[1]->Int32Value();
- return v8::True();
- };
- };
- class Line
- {
- public:
- /** Constructor without params is required */
- /** Example of object property, it can be manipulate directly */
- Point start;
- /** this will create a JS object, without destructor and a reference to this
- * end instance
- *
- */
- Point& GetEnd() { return end; }
- private:
- Point end;
- };
- int main(int argc, char* argv[])
- {
- v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
- v8::HandleScope handle_scope;
- // Create a template for the global object.
- v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
- ProxyClass<Foo>* pPoint = ProxyClass<Foo>::instance(); //Get the proxy class for Point
- pPoint->Expose(&Point::x_,"x",true,true); //expose x_ for read/write
- pPoint->Expose(&Point::y_,"y",true,true); //expose y_ for read/write
- pPoint->Expose(&Point::Copy, "copy");
- pPoint->Expose(&Point::GetManhattanLength, "getManhattanLength");
- pPoint->Expose(&Point::Translate, "translate");
- ProxyClass<Line>* pLine = ProxyClass<Line>::instance(); //Get the proxy class for Line
- pPoint->Expose(&Line::start,"start",true,true); //expose object start for read/write
- pPoint->Expose(&Line::GetEnd, "getEnd");
- global->Set(v8::String::New("Point"), pPoint->GetFunctionTemplate()); // add Point to JS
- global->Set(v8::String::New("Line"), pLine->GetFunctionTemplate()); // add Line to JS
- ...
- int main(int argc, char* argv[])
- {
- v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
- v8::HandleScope handle_scope;
- // Create a template for the global object.
- v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
- CPROXYV8_PROPERTY_ID(Point,x_,x,true,true);
- CPROXYV8_PROPERTY_ID(Point,y_,y,true,true);
- CPROXYV8_METHOD_ID(Point,Copy, copy);
- CPROXYV8_METHOD_ID(Point,GetManhattanLength, getManhattanLength);
- CPROXYV8_METHOD(Point,Translate);
- CPROXYV8_PROPERTY(Line,start,true,true);
- CPROXYV8_METHOD_ID(Line,GetEnd,getEnd);
- global->Set(v8::String::New("Point"), CPROXYV8_CLASS(Point)->GetFunctionTemplate()); // add Point to JS
- global->Set(v8::String::New("Line"), CPROXYV8_CLASS(Line)->GetFunctionTemplate()); // add Line to JS
在Shell中使用这些类:
- V8 version 0.4.3.1
- > x = new Point()
- [object Point]
- > line = new Line()
- [object Line]
- > x.x
- 0
- > x.x = 10
- 10
- > line.getEnd().x
- 0
- > line.getEnd().x = 10
- 10
- > line.getEnd().x
- 10
- > end = line.getEnd();
- [object Point]
- > end.x
- 10
- > end.Translate(10,10)
- true
- > end.x
- 20
- > line.getEnd().x
- 20
- > x.x
- 10
- >_
0 0
- WebKit之JS内嵌C++对象
- WebKit之V8引擎(js调度C++)
- webkit js扩展方式之Binding添加新DOM对象
- GTK Webkit 添加JS对象
- QT Webkit 添加JS对象
- GTK Webkit 添加JS对象
- GTK Webkit 添加JS对象
- WebKit之Major对象介绍
- c#winform中使用WebKit传递js对象实现与网页交互
- c#winform中使用WebKit传递js对象实现与网页交互
- Webkit JS扩展对象 多实例实现
- js内建对象
- android 上 webkit js 扩展之全局本地对象实现步骤
- android 上 webkit js 本地扩展之全局本地对象实现步骤
- Webkit之常用对象API梳理
- webkit开发之JS扩展篇
- WebKit之js解析和执行初始
- Webkit之JS/C++互调关系
- BestCoder Round #64 (div.2) HDOJ5587 Array(dfs)
- 二叉搜索树 HDU 3791
- iOS开发常见问题(二)
- Windows 7 & Windows 8 简体中文旗舰版下载地址
- poj1562
- WebKit之JS内嵌C++对象
- StatePattern-HeadFirst学习
- 解决UIWebView 前进、后退刷新的坑
- 黑马程序员__Java接口定义
- C++ STL 学习 :for_each与仿函数(functor)
- 欢迎使用CSDN-markdown编辑器
- Longest Valid Parentheses
- WebKit之jsc和webcore的交互
- 优化含中文字符串占位计算