如何使用V8从C++访问和调用Javascript对象属性和方法
How do I access and call Javascript Object properties and methods from C++ using 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)?
问候,
原始
我还没有测试下面的例子。
但我相信它提供了一个你想要什么的例子。
#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("%sn", *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("%sn", *ascii2);
printf("%sn", *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);
参考
使用V8 的CodeProject
将C++连接到Javascript bungeConnect
谷歌V8外壳示例代码
Google V8头文件
V8用户邮件列表你能从C++中填充V8::数组吗?线程
作为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));
}
很抱歉刷新,但我正在搜索完全相同的东西,我没有,所以也许有人会需要它。
targetObj->GetOwnPropertyNames(context,v8::PropertyFilter::ALL_PROPERTIES)
您只需要添加一个过滤器:))
相关文章:
- 如何使用EM_JS从带有参数的C++调用javascript方法
- 如何在 C++ 中从 V8 调用 Javascript 函数
- 从C++代码抛出错误调用 JavaScript 回调函数 - nbind
- node.js Nan:在函数C++调用 JavaScript 回调
- 如何使用 cocos creator 在 Cocos2d-X/Cocos2d-JS 中调用 javascript 函数并
- v8.从 C++ 保存和调用 JavaScript 函数
- 在WindBG断点命令中调用JavaScript函数
- 在V8中从C++回调调用Javascript函数
- 从QT调用JavaScript函数,并未显示输出
- 使用node.js从C++调用JavaScript
- 如何使用 WebKit 调用 JavaScript 中的C++函数
- NodeJS Addon从NAN AsyncWorker::Execute内部调用Javascript回调
- 使用 Emscripten 异步调用 JavaScript C++函数
- 从 C++ 节点调用 JavaScript 函数.js扩展
- ActiveX 调用 Javascript 函数
- 从C++调用javascript函数
- 通过c++调用javascript推送服务
- 如何使用V8从C++访问和调用Javascript对象属性和方法
- 如何在QWebengine中调用Javascript中的c++ /Qt函数
- v8,libuv,nodejs,win32api-如何调用EnumWindows并具有回调调用javascript函数