我正在使用c ++和v8,并遇到了以下挑战:我希望能够使用v8在javascript中定义一个函数,然后通过c ++调用该函数.另外,我希望能够从c ++将参数传递给javascript函数.我认为以下示例源代码最能解释它.检查示例代码的末尾,看看我想要完成什么.
#include <v8.h>
#include <iostream>
#include <string>
#include <array>
using namespace v8;
int main(int argc, char* argv[]) {
// Create a stack-allocated handle scope.
HandleScope handle_scope;
// Create a new context.
Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
Handle<String> source;
Handle<Script> script;
Handle<Value> result;
// Create a string containing the JavaScript source code.
source = String::New("function test_function(test_arg) { var match = 0;if(test_arg[0] == test_arg[1]) { match = 1; }");
// Compile the source code.
script = Script::Compile(source);
// …Run Code Online (Sandbox Code Playgroud)