2012-02-05 12 views
8

v8エンジンを使用してC++からJavascriptオブジェクトのプロパティとメソッドにアクセスして呼び出す方法の例が必要です。このドキュメンテーションでは、C++のオブジェクトや関数にjavascriptでアクセスする方法を示しますが、逆もありません。V8を使用してC++からJavascriptオブジェクトのプロパティとメソッドにアクセスして呼び出すにはどうすればよいですか?

はここ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)を取り込むことができますか?

よろしく、

プリス

答えて

6

私は、以下の例をテストしていません。

しかし、私はそれがあなたが望むものの例を与えると信じています。私はそれが

// 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

を使用していると考えている配列を変更する方法については

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


} 

Applemanの徹底的な答えのフォローアップとして、

Google V8 Header File

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

0

、それは価値がある何のために、私は.の代わりに->を使用していた、とあなたは新しいv8::Numberを割り当てる必要はありません。

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:Setへの最初の引数 それ。

あなただけのフィルタを追加する必要があります:)
targetObj->GetOwnPropertyNames(context,v8::PropertyFilter::ALL_PROPERTIES) 

関連する問題