#include <v8.h>
using namespace v8;
int main()
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
Handle<Context> context = Context::New(isolate);
Context::Scope context_scope(context);
Handle<String> source = String::New("var str = 1 + 2");
Handle<Script> script = Script::Compile(source);
script->Run();
Handle<String> data = String::New("str");
Handle<Value> str = context->Global()->Get(data);
String::AsciiValue ascii(str);
printf("%s\n", *ascii);
return 0;
}
留意context->Global()->Get(data);的用法,和data参数的类型是Handle<String>
v8学习---获取js变量的值
最新推荐文章于 2020-11-28 00:23:03 发布
本文展示了一个使用V8 JavaScript引擎执行简单JavaScript脚本的C++示例。通过创建上下文、编译并运行脚本,最后从全局环境中获取变量值。
434

被折叠的 条评论
为什么被折叠?



