如何使用V8从C ++访问和调用Javascript对象属性和方法?

时间:2012-02-05 01:30:06

标签: javascript c++ v8

我想了解如何使用v8引擎从C ++访问和调用Javascript对象属性和方法的示例。该文档展示了如何通过javascript访问C ++对象和函数,但不是相反。

这是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)?

此致

的PRI

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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));
}

答案 2 :(得分:0)

很抱歉刷新但是我正在搜索完全相同的东西而且我不会这样,也许有人会需要它。

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

你只需要添加一个过滤器:))