基於V8引擎的C++和JS的相互交互


基於什么原因略!

1. 腳本引擎的基本功能

V8只是一個JS引擎。去除它的特點功能出處,它必須要實現JS引擎的幾個基礎功能:

腳本執行:
腳本可能是一個表達式;一段js代碼;或者一個文件
執行表達式返回js表達式對應的值
C++來取設JS的內容
獲取JS內容的數據(包括基礎數據類型、數組、日期等)、對象(類的一個實例)、類或函數
設置JS內容的數據
JS來取設C++的內容
C++為js動態添加類(例如:Date,可以通過new Date()來創建任意多個對象)
C++為js動態添加全局對象(例如:Math,可以直接調用其全局方法如Math.min)
我們的目的是先學會怎么用,再去想為什么!

2. V8 腳本引擎基本功能實現

帶着這幾個目的去使用V8,找遍網上資料,發現很少有覆蓋周全的。其實翻來覆去就是那幾個資料,理論派居多;最可氣的是按照那些例子去跑,怎么都會運行錯誤;而且在關鍵的地方全按照google偽代碼例子照搬。不過理論派的闡述確實很一本正經;很精辟。也感謝先行者!

2. 1 V8之腳本運行
2.1.1 獲取字符串:

void test_String()
{
Handle<String> source = String::New("'Hello' + ', World!'");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();

String::AsciiValue ascii(result);
printf("%s\n", *ascii);
}
>>> Hello,World!

2.1.2 獲取數組:

void test_Array()
{
Handle<String> source = String::New("[1, 2, 'hello', 6+5]");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();

String::AsciiValue ascii(result);
printf("%s\n", *ascii);
}
>>> 1,2,hello,11

2. 2 V8之C++取設JS
2.2.1 獲取成員之數據

void test_getjs_data(Handle<Context> pContext)
{
Handle<String> source = String::New("var s1 = 8+5;");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();
Handle<String> js_data = String::New("s1");
Handle<Value> js_data_value = pContext->Global()->Get(js_data);

String::AsciiValue ascii(js_data_value);
printf("%s\n", *ascii);
}
>>> 13

2.2.2 獲取成員之全局對象

void test_getjs_dataofObject(Handle<Context> pContext)
{
Handle<String> source = String::New("function Point(x,y){this.x=x; this.y=y;} var pt=new Point(10,20);");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();

Handle<String> js_data = String::New("pt");
Handle<Value> js_data_value = pContext->Global()->Get(js_data);

// Convert the result to an ASCII string and print it.
{
String::AsciiValue ascii(js_data_value);
printf("pt = %s\n", *ascii);
}
Handle<Object> js_data_object = Handle<Object>::Cast(js_data_value);

Handle<Value> key = String::New("x");
Handle<Value> objX = js_data_object->Get(key);
{
String::AsciiValue ascii(objX);
printf("pt.x = %s\n", *ascii);
}
}
>>> pt = [object Object]
pt.x = 10

2.2.3 獲取js 類

void test_getjs_dataofObjectClass(Handle<Context> pContext)
{
Handle<String> source = String::New("function Point(x,y){this.x=x; this.y=y;} Point.prototype.show=function(){return '(x,y) = '+this.x+','+this.y;}");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();

Handle<String> js_data = String::New("Point");
Handle<Value> js_data_value = pContext->Global()->Get(js_data);

// Convert the result to an ASCII string and print it.
{
String::AsciiValue ascii(js_data_value);
printf("Point = %s\n", *ascii);
}

bool bIsFunction = js_data_value->IsFunction();
if(bIsFunction)
{
printf("Point is function\n");
}

bool bIsObject = js_data_value->IsObject();
if(bIsObject)
{
printf("Point is object\n");
}

Handle<Object> js_data_object = Handle<Object>::Cast(js_data_value);
// var newObj = new Point(1,2);
Handle<Value> argv[2] ;
argv[0] = Int32::New(1);
argv[1] = Int32::New(2);
Handle<Value> newObj = js_data_object->CallAsConstructor(2, argv);
{
bool bIsFunction = newObj->IsFunction();
if(bIsFunction) //-false-
{
printf("newObj is function\n");
}

bool bIsObject = newObj->IsObject();
if(bIsObject) //-true-
{
printf("newObj is object\n");
}
}

// newObj.show();
{
Handle<Object> obj = Handle<Object>::Cast(newObj);
Handle<String> js_func_name = String::New("show");
Handle<Value> js_func_ref = obj->Get(js_func_name);

Handle<Function> js_func = Handle<Function>::Cast(js_func_ref);
js_data_value = js_func->Call(obj, 0, NULL) ;

String::AsciiValue ascii(js_data_value);
printf("newObj.show() = %s\n", *ascii);
}
}
>>> Point = function Point(x,y){this.x=x; this.y=y;}
Point is function
Point is object
newObj is object
newObj.show() = (x,y) = 1,2

2. 3 V8之JS取設C++
2.3.0 js 和 C++的關聯

c++ 和 js對應。特別是為達到能在Js中使用var obj = new CObject(),參照CreateObjectToJs方法

void test_getc_loadObjectTemplate(Handle<ObjectTemplate> pObj)
{
//-load c++'s data-
pObj->SetAccessor(String::New("x"), XGetter, XSetter);

//-load c++'s function-
pObj->Set(String::New("setColor"),FunctionTemplate::New(set_color));

//-load c++'s class-
CreateObjectToJs(pObj);
}


Point* NewPointFunction(const Arguments & args)
{
if(args.Length()==2)
{
Local<Value> v1 = args[0];
Local<Value> v2 = args[1];

return new Point( v1->Int32Value(), v2->Int32Value() );
}
else
return new Point();
}

void PointWeakExternalReferenceCallback(Persistent<Value>, void* parameter)
{
if (Point* cpp_object = static_cast<Point*>(parameter))
delete cpp_object;
}

Persistent<External> NewWeakExternalPoint(void* parameter)
{
Persistent<External> ret = Persistent<External>::New(External::New(parameter));
ret.MakeWeak(parameter, PointWeakExternalReferenceCallback);
return ret;
}

Handle<Value> PointFunctionInvocationCallback(const Arguments &args)
{
if (!args.IsConstructCall())
return Undefined();

Point* cpp_object = NewPointFunction(args);
if (!cpp_object)
return ThrowException(String::New("Can not create Object in C++"));

args.Holder()->SetInternalField(0, NewWeakExternalPoint(cpp_object));
return Undefined();
}


void CreateObjectToJs(Handle<ObjectTemplate> pObj)
{
Point* p = new Point(0, 0);
Handle<FunctionTemplate> point_templ = FunctionTemplate::New(&PointFunctionInvocationCallback, External::New(p));
point_templ->SetClassName(String::New("Point"));
point_templ->InstanceTemplate()->SetInternalFieldCount(1);

Handle<ObjectTemplate> point_proto = point_templ->PrototypeTemplate();
point_proto->SetAccessor(String::New("x"), GetPointX, SetPointX);
point_proto->SetAccessor(String::New("y"), GetPointY, SetPointY);
point_proto->Set(String::New("show"), FunctionTemplate::New(ShowPoint));

pObj->Set(String::New("Point"), point_templ);
}

2.3.1 全局函數

c++:
Handle<Value> set_color(const Arguments & args)
{
Handle<Value> rtn;
if(args.Length() == 3)
{
int r = args[0]->Int32Value();
int g = args[1]->Int32Value();
int b = args[2]->Int32Value();

char szTmp[80] = "";
_stprintf(szTmp,"RGB%2X%02X%02X", r,g,b);
rtn = String::New(szTmp);
}
else
rtn = Undefined();

return rtn;
}

js:
void test_getc_function(Handle<Context> pContext, Handle<ObjectTemplate> pObj)
{
Handle<String> source = String::New("var data = setColor(255,128,0);");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();

Handle<String> js_data = String::New("data");
Handle<Value> js_data_value = pContext->Global()->Get(js_data);

// Convert the result to an ASCII string and print it.
String::AsciiValue ascii(js_data_value);
printf("%s\n", *ascii);
}
>>> RGBFF8000

2.3.2 全局數據

c++:

int x = 0;
Handle<Value> XGetter(Local<String> key,const AccessorInfo& info)
{
return Integer::New(x);
}

void XSetter(Local<String> key, Local<Value> value,const AccessorInfo& info)
{
x = value->Int32Value();
}

js:

void test_getc_data(Handle<Context> pContext, Handle<ObjectTemplate> pObj)
{
Handle<String> source = String::New("var data1 = x; x=200; var data2=x;");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();

{
Handle<String> js_data = String::New("data1");
Handle<Value> js_data_value = pContext->Global()->Get(js_data);
String::AsciiValue ascii(js_data_value);
printf("data1 = %s\n", *ascii);
}

{
Handle<String> js_data = String::New("data2");
Handle<Value> js_data_value = pContext->Global()->Get(js_data);
String::AsciiValue ascii(js_data_value);
printf("data2 = %s\n", *ascii);
}
}
>>> data1 = 0
data2 = 200

2.3.3 類

c++

struct Point
{
Point()
{
x_ = 0;
y_ = 0;
}
Point(int x, int y)
{
x_ = x;
y_ = y;
}

int getX() const {
return x_;
}
int getY() const { return y_; }
void setX(int value) { x_ = value; }
void setY(int value) { y_ = value; }
bool isNull() const { return x_ == 0 && y_ == 0; }
void show()
{
char szTmp[80] = "";
_stprintf(szTmp, "x,y = %d,%d\n", x_, y_);
printf(szTmp);
}
int x_, y_;
};


Handle<Value> GetPointX(Local<String> key,const AccessorInfo &info)
{
Handle<Object> obj = info.This ();
//Local<Object> self = info.Holder(); //使用此種方法會死!!!-
Point& point = *static_cast<Point*> (Local<External>::Cast(obj->GetInternalField(0))->Value ());
int value = point.x_;
return Integer::New(value);
}

void SetPointX(Local<String> key, Local<Value> value,const AccessorInfo& info)
{
Handle<Object> obj = info.This ();
Point& point = *static_cast<Point*> (Local<External>::Cast(obj->GetInternalField(0))->Value ());
point.x_ = value->Int32Value();
}

Handle<Value> GetPointY(Local<String> key,const AccessorInfo &info)
{
Local<Object> self = info.Holder();
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();
int value = static_cast<Point*>(ptr)->y_;
return Integer::New(value);
}

void SetPointY(Local<String> key, Local<Value> value,const AccessorInfo& info)
{
Local<Object> self = info.Holder();
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();
static_cast<Point*>(ptr)->y_ = value->Int32Value();
}

Handle<Value> ShowPoint(const Arguments& args)
{
Local<Object> self = args.Holder();
//Local<Object> self = info.Holder();
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();
static_cast<Point*>(ptr)->show();

return Undefined();
}

js

void test_getc_object(Handle<Context> pContext, Handle<ObjectTemplate> pObj)
{
test_getobject(pContext);return;

// Create a string containing the JavaScript source code.
Handle<String> source = String::New("var pt=new Point(10,20);");

// Compile the source code.
Handle<Script> script = Script::Compile(source);

// Run the script to get the result.
Handle<Value> result = script->Run();

Handle<String> js_data = String::New("pt");
Handle<Value> js_data_value = pContext->Global()->Get(js_data);

// Convert the result to an ASCII string and print it.
{
String::AsciiValue ascii(js_data_value);
printf("pt = %s\n", *ascii);
}

Handle<Object> js_data_object = Handle<Object>::Cast(js_data_value);

Handle<Value> key = String::New("x");
Handle<Value> objX = js_data_object->Get(key);
{
String::AsciiValue ascii(objX);
printf("pt.x = %s\n", *ascii);
}
}
>>> Point = function Point() { [native code] }
Point is function
Point is object
newObj is object
src obj.x = 3
last obj.x = 30
begin newObj.show() : x,y = 30,4
newObj.show() = undefined

3. 教訓

在VS200X C++中,請都使用/MDD 編譯選項
對於類,需要綁定一個構造函數,主要目的是用來生成C++對象。該對象千萬不要使用局部變量,而要是指針
對於C++擴展的類,通過FunctionTemplate來實現;而不要使用ObjectTemplate
對於JS中的全局對象,可以通過ObjectTemplate來實現。實際上就是綁定全局API和變量


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM