WebKit之JS内嵌C++对象

来源:互联网 发布:win10怎么安装软件 编辑:程序博客网 时间:2024/05/20 19:48

3. JS 嵌入C++示例代码

一个Hello world!

[cpp] view plaincopy
  1. // Enter the created context for compiling and   
  2.    // running the hello world script.    
  3.    Context::Scope context_scope(context);   
  4.   
  5.    // Create a string containing the JavaScript source code.   
  6.    Handle<String> source = String::New("'Hello' + ', World!'");   
  7.   
  8.    // Compile the source code.   
  9.    Handle<Script> script = Script::Compile(source);   
  10.   
  11.    // Run the script to get the result.   
  12.    Handle<Value> result = script->Run();   
  13.   
  14.    // Dispose the persistent context.   
  15.    context.Dispose();   
  16.   
  17.    // Convert the result to an ASCII string and print it.   
  18.    String::AsciiValue ascii(result);   
  19.    printf("%s\n", *ascii);   
  20.    return 0;   

更多示例代码参考:V8\test\cctest\*.cc

4.支持C++类

CProxyV8提供便利的方式将C++类的属性和方法导出到JavaScript环境中。支持generic Functors,不需要为每个属性和方法设置回调函数。


[cpp] view plaincopy
  1. class Point    
  2.    {   
  3.    public:   
  4.       /** Constructor without params is required */   
  5.        Point(int px = 0, int py = 0) : x_(px), y_(py) {}   
  6.        Point(Point* p) : x_(p->x_), y_(p->y_) {}   
  7.    
  8.        int x_;   
  9.        int y_;   
  10.    
  11.       /** Example call to function without params */   
  12.       int GetManhattanLength()  { return std::abs(x_) - std::abs(y_); }   
  13.    
  14.       /** this will create a JS object, with JS destructor */   
  15.        Point* Copy() { return new Point(); }   
  16.    
  17.       /** Example call to function with params */   
  18.        v8::Handle<v8::Value> Translate(const v8::Arguments& args)    
  19.        {    
  20.          // throw if we didn't get 2 args   
  21.          if (args.Length() != 2)   
  22.            return ThrowException(v8::String::New("Expected 2 arguments"));   
  23.    
  24.          x_ += args[0]->Int32Value();   
  25.          y_ += args[1]->Int32Value();   
  26.          return v8::True();    
  27.        };   
  28.    };   
  29.    
  30.    class Line   
  31.    {   
  32.    public:   
  33.      /** Constructor without params is required */   
  34.      
  35.      /** Example of object property, it can be manipulate directly */   
  36.      Point start;     
  37.    
  38.      /** this will create a JS object, without destructor and a reference to this   
  39.       *  end instance   
  40.       *  
  41.       */   
  42.      Point& GetEnd() { return end; }   
  43.    
  44.    private:   
  45.      Point end;   
  46.    };  
  47.   
  48.  int main(int argc, char* argv[])    
  49.    {   
  50.      v8::V8::SetFlagsFromCommandLine(&argc, argv, true);   
  51.      v8::HandleScope handle_scope;   
  52.    
  53.      // Create a template for the global object.   
  54.      v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();   
  55.    
  56.      ProxyClass<Foo>* pPoint = ProxyClass<Foo>::instance(); //Get the proxy class for Point   
  57.    
  58.      pPoint->Expose(&Point::x_,"x",true,true); //expose x_ for read/write   
  59.      pPoint->Expose(&Point::y_,"y",true,true); //expose y_ for read/write   
  60.    
  61.      pPoint->Expose(&Point::Copy, "copy");   
  62.      pPoint->Expose(&Point::GetManhattanLength, "getManhattanLength");   
  63.      pPoint->Expose(&Point::Translate, "translate");   
  64.      
  65.      ProxyClass<Line>* pLine = ProxyClass<Line>::instance(); //Get the proxy class for Line   
  66.      pPoint->Expose(&Line::start,"start",true,true); //expose object start for read/write   
  67.      pPoint->Expose(&Line::GetEnd, "getEnd");   
  68.    
  69.      global->Set(v8::String::New("Point"), pPoint->GetFunctionTemplate()); // add Point to JS   
  70.      global->Set(v8::String::New("Line"), pLine->GetFunctionTemplate()); // add Line to JS   
  71.      ...  
如果借助宏,就较简单:

[cpp] view plaincopy
  1. int main(int argc, char* argv[])    
  2.    {   
  3.      v8::V8::SetFlagsFromCommandLine(&argc, argv, true);   
  4.      v8::HandleScope handle_scope;   
  5.    
  6.      // Create a template for the global object.   
  7.      v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();   
  8.    
  9.      CPROXYV8_PROPERTY_ID(Point,x_,x,true,true);   
  10.      CPROXYV8_PROPERTY_ID(Point,y_,y,true,true);   
  11.    
  12.      CPROXYV8_METHOD_ID(Point,Copy, copy);   
  13.      CPROXYV8_METHOD_ID(Point,GetManhattanLength, getManhattanLength);   
  14.      CPROXYV8_METHOD(Point,Translate);   
  15.      
  16.      CPROXYV8_PROPERTY(Line,start,true,true);   
  17.    
  18.      CPROXYV8_METHOD_ID(Line,GetEnd,getEnd);   
  19.    
  20.      global->Set(v8::String::New("Point"), CPROXYV8_CLASS(Point)->GetFunctionTemplate()); // add Point to JS   
  21.      global->Set(v8::String::New("Line"), CPROXYV8_CLASS(Line)->GetFunctionTemplate()); // add Line to JS   

在Shell中使用这些类:

[cpp] view plaincopy
  1. V8 version 0.4.3.1   
  2.       > x = new Point()   
  3.       [object Point]   
  4.       > line = new Line()   
  5.       [object Line]   
  6.       > x.x   
  7.       0   
  8.       > x.x = 10   
  9.       10   
  10.       > line.getEnd().x   
  11.       0   
  12.       > line.getEnd().x = 10   
  13.       10   
  14.       > line.getEnd().x   
  15.       10   
  16.       > end = line.getEnd();   
  17.       [object Point]   
  18.       > end.x   
  19.       10   
  20.       > end.Translate(10,10)   
  21.       true   
  22.       > end.x   
  23.       20   
  24.       > line.getEnd().x   
  25.       20   
  26.       > x.x   
  27.       10   
  28.       >_  
0 0
原创粉丝点击