Now write a function that calls the JS function in C + +,
int V8_manager::js_load_player_data (Block_buffer *buf) {
Before executing the V8 code, you must first enter the V8 isolate, initialize the V8 run environment
Isolate::scope Isolate_scope (Isolate_);
Handlescope Handle_scope (Isolate_);
local<context> Context = local<context>::new (Isolate_, Context_);
Context::scope Context_scope (context);
Get JS function
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;
}
Convert to JS function object
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;
}
C + + calls JS function via V8