标签:
现在写个函数,在C++中调用js函数,
int V8_Manager::js_load_player_data(Block_Buffer *buf) {
//执行V8代码前,必须先进入V8的Isolate,初始化V8运行环境
Isolate::Scope isolate_scope(isolate_);
HandleScope handle_scope(isolate_);
Local<Context> context = Local<Context>::New(isolate_, context_);
Context::Scope context_scope(context);
//获取js函数
Local<String> func_name = String::NewFromUtf8(isolate_, "load_data", NewStringType::kNormal).ToLocalChecked();
Local<Value> func_value;
if (!context->Global()->Get(context, func_name).ToLocal(&func_value) || !func_value->IsFunction()) {
return -1;
}
//转换成js函数对象
Local<Function> js_func = Local<Function>::Cast(func_value);
// Invoke the process function, giving the global object as ‘this‘
TryCatch try_catch(isolate_);
Local<Object> buf_obj = wrap_buffer(isolate_, buf);
const int argc = 1;
Local<Value> argv[argc] = {buf_obj};
Local<Value> result;
if (!js_func->Call(context, context->Global(), argc, argv).ToLocal(&result)) {
String::Utf8Value error(try_catch.Exception());
printf("js_load_player_data error, : %s", *error);
return -1;
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/jice1990/p/5322544.html