C + + calls JS function via V8

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.