Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C+;访问和调用Javascript对象属性和方法+;使用V8? 我想一个例子,说明如何使用V8引擎从C++访问和调用JavaScript对象属性和方法。文档显示了如何通过JavaScript访问C++对象和函数,但不是相反的。_Javascript_C++_V8 - Fatal编程技术网

如何从C+;访问和调用Javascript对象属性和方法+;使用V8? 我想一个例子,说明如何使用V8引擎从C++访问和调用JavaScript对象属性和方法。文档显示了如何通过JavaScript访问C++对象和函数,但不是相反的。

如何从C+;访问和调用Javascript对象属性和方法+;使用V8? 我想一个例子,说明如何使用V8引擎从C++访问和调用JavaScript对象属性和方法。文档显示了如何通过JavaScript访问C++对象和函数,但不是相反的。,javascript,c++,v8,Javascript,C++,V8,下面是JS中的一个简单对象构造函数和实例: function MyObj() { this.myArray = []; this.myDouble = 0; this.myFunction = function(arg1,arg2) { return (myDouble + arg1 + arg2); } } var globalObject = new myObj(); 如何访问globalObject的属性和方法?还有一个相关的问题——我

下面是JS中的一个简单对象构造函数和实例:

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

var globalObject = new myObj();
如何访问globalObject的属性和方法?还有一个相关的问题——我如何从C++ +/p>>填充数组(GualAbjult.MyLaar)? 问候,


Pris

我没有测试下面的示例

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

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


}
#包括
使用名称空间v8;
int main(int argc,char*argv[]){
//创建句柄范围
手柄镜手柄镜;
//创建一个新的上下文。
Handle context=context::New();
//输入所创建的编译上下文,然后
//运行脚本。
上下文::范围上下文\范围(上下文);
//创建一个新脚本
const char*script=“function MyObj(){this.myArray=[];this.myDouble=0;this.myFunction=function(arg1,arg2){return(myDouble+arg1+arg2);}}}var globalObject=new MyObj();”
//创建包含JavaScript源代码的字符串。
handlesource=String::New(“脚本”);
//编译源代码。
Handle script=script::Compile(源代码);
//运行脚本
//运行脚本以获得结果。
Handle scriptresult=script->Run();
//将结果转换为ASCII字符串并打印。
字符串::ascivalue ascii(scriptresult);
printf(“%s\n”,*ascii);
//获取对象
Handle object=Local::Cast(context->Global()->Get(String::New(“globalObject”));
//获取属性
Handle arrayproperty=Handle::Cast(对象->获取(字符串::新建(“myArray”));
Handle doubleproperty=Handle::Cast(对象->获取(字符串::新建(“myDouble”));
字符串::ascivalue ascii2(arrayproperty);
字符串::ascivalue ascii3(doubleproperty);
printf(“%s\n”,*ascii2);
printf(“%s\n”,*ascii3);
//调用函数
Handle-fun\u-to\u-call=Handle::Cast(对象->获取(字符串::新建(“myFunction”));
int argcount=0;
句柄argarray[]=NULL;
Handle functionresult=fun\u to\u call->call(对象、argcount、argarray);
//argcount和argarray是函数的标准参数
返回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); 
//获取对象
Handle object=Local::Cast(context->Global()->Get(String::New(“globalObject”))1;
//初始化数组
int num[4]={1,2,3,4};
v8::Local arguments=v8::Array::New(num);
对于(int i=0;i设置(v8::String::New(“myArray”),参数);
参考资料


作为Appleman彻底回答的后续,我不得不使用
->
而不是
,并且您不必为
Set
的第一个参数分配新的
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));
}
v8::localr=v8::Array::New(10);
对于(uint32_t i=0;i<10;++i){
r->Set(i,v8::Number::New(i));
}

很抱歉刷新,但我正在搜索完全相同的东西,我没有,所以可能有人会需要它

targetObj->GetOwnPropertyNames(context,v8::PropertyFilter::ALL_PROPERTIES)
您只需要添加一个筛选器:))