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

时间:2022-12-01 16:40:49

I'd like an example of how to access and call Javascript object properties and methods from C++ using the v8 engine. The documentation shows how to access C++ objects and functions through javascript but not the other way around.

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

Here's a simple object constructor and instance in JS:

这里有一个简单的对象构造函数和JS中的实例:

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

var globalObject = new myObj();

How would I access globalObject's properties and methods? Also a somewhat related question -- how can I populate the array (globalObject.myArray) from C++?

如何访问globalObject的属性和方法?还有一个相关的问题——如何从c++填充数组(globalObject.myArray) ?

Regards,

问候,

Pris

取了

3 个解决方案

#1


6  

I haven't tested the examples below.

我还没有测试下面的例子。

But I believe it gives an example of what you want.

但我相信它给出了一个你想要的例子。

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


}

As for how to modify the array I believe it would be using

至于如何修改数组,我相信它会使用

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

References

参考文献

CodeProject Using V8

CodeProject上使用V8

Connecting C++ to Javascript bungeeconnect

连接c++到Javascript bungeeconnect

Google V8 Shell Sample Code

谷歌V8 Shell示例代码

Google V8 Header File

谷歌V8头文件

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

V8用户邮件列表你能从c++中填充V8::数组吗?线程

#2


0  

As a follow-up to Appleman's thorough answer, for what it's worth, I had to use -> instead of ., and you don't have to allocate a new v8::Number for the first argument to Set:

作为对Appleman的完整答案的后续,我必须使用->而不是。

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

#3


0  

Sorry for refresh but I was searching exactly same thing and I dont't so maybe someone will need it.

抱歉,刷新一下,但是我搜索的是完全一样的东西,我没有,所以可能有人需要它。

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

You just need to add a filter :))

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

#1


6  

I haven't tested the examples below.

我还没有测试下面的例子。

But I believe it gives an example of what you want.

但我相信它给出了一个你想要的例子。

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


}

As for how to modify the array I believe it would be using

至于如何修改数组,我相信它会使用

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

References

参考文献

CodeProject Using V8

CodeProject上使用V8

Connecting C++ to Javascript bungeeconnect

连接c++到Javascript bungeeconnect

Google V8 Shell Sample Code

谷歌V8 Shell示例代码

Google V8 Header File

谷歌V8头文件

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

V8用户邮件列表你能从c++中填充V8::数组吗?线程

#2


0  

As a follow-up to Appleman's thorough answer, for what it's worth, I had to use -> instead of ., and you don't have to allocate a new v8::Number for the first argument to Set:

作为对Appleman的完整答案的后续,我必须使用->而不是。

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

#3


0  

Sorry for refresh but I was searching exactly same thing and I dont't so maybe someone will need it.

抱歉,刷新一下,但是我搜索的是完全一样的东西,我没有,所以可能有人需要它。

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

You just need to add a filter :))

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