2012-02-05 85 views
8

我想要一個如何訪問和使用v8引擎從C++調用JavaScript對象屬性和方法的示例。該文檔展示瞭如何通過javascript訪問C++對象和函數,但不是其他方式。如何從C++使用V8訪問和調用JavaScript Object屬性和方法?

這裏有一個簡單的對象構造和實例在JS:

function MyObj() 
{ 
    this.myArray = []; 
    this.myDouble = 0; 
    this.myFunction = function(arg1,arg2) 
     { return (myDouble + arg1 + arg2); } 
} 

var globalObject = new myObj(); 

我將如何訪問globalObject的屬性和方法?還有一個相關的問題 - 我如何從C++填充數組(globalObject.myArray)?

問候,

PRIS

回答

6

我沒有測試下面的實施例。

但我相信它舉了一個你想要的例子。

#include <v8.h> 
using namespace v8; 
int main(int argc, char* argv[]) { 
    // Create a handle scope 
    HandleScope handle_scope; 
    // Create a new context. 
    Handle<Context> context = Context::New(); 
    // Enter the created context for compiling and 
    // running the script. 
    Context::Scope context_scope(context); 
    // Create a new script 
    const char* script = "function MyObj() { this.myArray = []; this.myDouble = 0; this.myFunction = function(arg1,arg2) { return (myDouble + arg1 + arg2); } } var globalObject = new myObj();" 
    // Create a string containing the JavaScript source code. 
    Handle<String> source = String::New("script); 
    // Compile the source code. 
    Handle<Script> script = Script::Compile(source); 
    // Running the script 
    // Run the script to get the result. 
    Handle<Value> scriptresult = script->Run(); 
    // Convert the result to an ASCII string and print it. 
    String::AsciiValue ascii(scriptresult); 
    printf("%s\n", *ascii); 

    // Get the object 
    Handle<Object> object = Local::Cast(context->Global()->Get(String::New("globalObject"))); 
    // Get the Properties 
    Handle<Value> arrayproperty = Handle::Cast(object->Get(String::New("myArray"))); 
    Handle<Value> doubleproperty = Handle::Cast(object->Get(String::New("myDouble"))); 
    String::AsciiValue ascii2(arrayproperty); 
    String::AsciiValue ascii3(doubleproperty); 
    printf("%s\n", *ascii2); 
    printf("%s\n", *ascii3); 
    // Call the function 
    Handle fun_to_call = Handle::Cast(object->Get(String::New("myFunction"))); 
    int argcount = 0; 
    Handle argarray[] = NULL; 

    Handle functionresult = fun_to_call->Call(object, argcount, argarray); 
// argcount and argarray are your standard arguments to a function 

    return 0; 


} 

至於如何修改數組,我相信它會使用

// Get the object 
Handle<Object> object = Local::Cast(context->Global()->Get(String::New("globalObject")))1; 

//Initialise array 
int num[4] = {1,2,3,4}; 
v8::Local<v8::Array> arguments = v8::Array::New(num); 
for (int i = 0; i < args; i++) { 
    arguments.Set(v8::Number::New(i), v8::String::New(args[i])); 
} 

// Set Array 
object->Set(v8::String::New("myArray"), arguments); 

參考

CodeProject Using V8

Connecting C++ to Javascript bungeeconnect

Google V8 Shell Sample Code

Google V8 Header File

V8 Users Mailing List Can you populate a v8::Array from C++? Thread

0

作爲後續Appleman的完整的答案,對於它的價值,我不得不使用->代替.,你不必分配新v8::Number對於第一個參數Set

v8::Local<v8::Array> r = v8::Array::New(10); 
for (uint32_t i = 0; i < 10; ++i) { 
    r->Set(i, v8::Number::New(i)); 
} 
0

對不起,刷新,但是我正在尋找完全相同的事情,我dont't所以也許有人會需要它。

targetObj->GetOwnPropertyNames(context,v8::PropertyFilter::ALL_PROPERTIES) 

你只需要添加一個過濾器:))

相關問題