C++ V8 javascript如何进行异步调用

C++ V8 javascript如何进行异步调用,c++,asynchronous,v8,embedded-v8,libv8,C++,Asynchronous,V8,Embedded V8,Libv8,我一直在试图弄清楚如何在V8中进行异步调用,但运气不好。我试图运行的示例javascript代码是: function test () { logMessage ('asynchronous call made!'); } saveFunc(test); SaveFunc函数应该保存测试函数,以便在运行脚本后C++代码调用它时使用。每次我尝试此操作时,当我尝试执行保存的函数时,它都会崩溃。我做错了什么 我已经在下面复制了我的全部示例代码。提前谢谢 示例代码: // Copyright

我一直在试图弄清楚如何在V8中进行异步调用,但运气不好。我试图运行的示例javascript代码是:

function test ()
{
    logMessage ('asynchronous call made!');
}
saveFunc(test);

SaveFunc函数应该保存测试函数,以便在运行脚本后C++代码调用它时使用。每次我尝试此操作时,当我尝试执行保存的函数时,它都会崩溃。我做错了什么

我已经在下面复制了我的全部示例代码。提前谢谢

示例代码:

// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <iostream>
#include <string>

#include "libplatform/libplatform.h"
#include "v8.h"

using namespace v8;

Local<Context> context;
v8::Local<v8::Function> savedFunc;

class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
    virtual void* Allocate(size_t length) {
        void* data = AllocateUninitialized(length);
        return data == NULL ? data : memset(data, 0, length);
    }
    virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
    virtual void Free(void* data, size_t) { free(data); }
};

std::string parseV8Value(v8::Local<v8::Value> str)
{
    if (str.IsEmpty() == true)
        return ("");

    v8::String::Utf8Value newStr(str);

    return (*newStr);
}

void logMessage(const v8::FunctionCallbackInfo<v8::Value> &args)
{
    std::string applicationSource = parseV8Value(args[0]);
    std::cout << applicationSource << "\n";
}

void getInput(const v8::FunctionCallbackInfo<v8::Value> &args)
{
    v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast (args[0]);
    std::string input = "";

    std::cin >> input;

    v8::Local<v8::Value> *args2 = new v8::Local<v8::Value>[1];
    args2[0] = v8::String::NewFromUtf8 (args.GetIsolate (), input.c_str ());

    func->Call (context->Global (), 1, args2);

    delete []args2;
    args2 = NULL;
}

void saveFunc(const v8::FunctionCallbackInfo<v8::Value> &args)
{
    savedFunc = v8::Local<v8::Function>::Cast(args[0]);
}

int main(int argc, char* argv[]) {
    // Initialize V8.
    V8::InitializeICU();
    V8::InitializeExternalStartupData(argv[0]);
    Platform* platform = platform::CreateDefaultPlatform();
    V8::InitializePlatform(platform);
    V8::Initialize();

    // Create a new Isolate and make it the current one.
    ArrayBufferAllocator allocator;
    Isolate::CreateParams create_params;
    create_params.array_buffer_allocator = &allocator;
    Isolate* isolate = Isolate::New(create_params);
    {
        Isolate::Scope isolate_scope(isolate);

        // Create a stack-allocated handle scope.
        HandleScope handle_scope(isolate);

        // Create a new context.
        v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
        global->Set(v8::String::NewFromUtf8(isolate, "logMessage"),
            v8::FunctionTemplate::New(isolate, logMessage));
        global->Set(v8::String::NewFromUtf8(isolate, "getInput"),
            v8::FunctionTemplate::New(isolate, getInput));
        global->Set(v8::String::NewFromUtf8(isolate, "saveFunc"),
            v8::FunctionTemplate::New(isolate, saveFunc));
        context = Context::New(isolate, NULL, global);

        // Enter the context for compiling and running the hello world script.
        Context::Scope context_scope(context);

        // Create a string containing the JavaScript source code.
        Local<String> source =
            String::NewFromUtf8(isolate, 
            "function test (){ logMessage ('asynchronous call made!'); }saveFunc(test);",,
                NewStringType::kNormal).ToLocalChecked();

        // Compile the source code.
        Local<Script> script = Script::Compile(context, source).ToLocalChecked();

        // Run the script to get the result.
        Local<Value> result = script->Run(context).ToLocalChecked();

        v8::Local<v8::Value> *args = new v8::Local<v8::Value>[0];
        savedFunc->Call(context->Global(), 0, args);

        delete []args;
        args = NULL;
    }

    // Dispose the isolate and tear down V8.
    isolate->Dispose();
    V8::Dispose();
    V8::ShutdownPlatform();
    delete platform;
    return 0;
}
//版权归V8项目作者所有。版权所有。
//此源代码的使用受BSD样式许可证的约束,该许可证可以
//在许可证文件中找到。
#包括
#包括
#包括“libplatform/libplatform.h”
#包括“v8.h”
使用名称空间v8;
地方背景;
v8::本地savedFunc;
类ArrayBufferAllocator:PublicV8::ArrayBuffer::Allocator{
公众:
虚拟空*分配(大小/长度){
void*data=AllocateUninitialized(长度);
返回数据==NULL?数据:memset(数据,0,长度);
}
虚拟void*AllocateUninitialized(size_t length){return malloc(length);}
虚拟无空(void*数据,大小_t){Free(数据);}
};
std::string parseV8Value(v8::Local str)
{
if(str.IsEmpty()==true)
返回(“”);
v8::String::Utf8Value newStr(str);
返回(*newStr);
}
void日志消息(const v8::FunctionCallbackInfo&args)
{
std::string applicationSource=parseV8Value(args[0]);
std::cout输入;
v8::Local*args2=新的v8::Local[1];
args2[0]=v8::String::NewFromUtf8(args.GetIsolate(),input.c_str());
func->Call(上下文->全局(),1,args2);
删除[]args2;
args2=NULL;
}
void saveFunc(const v8::FunctionCallbackInfo&args)
{
savedFunc=v8::Local::Cast(args[0]);
}
int main(int argc,char*argv[]){
//初始化V8。
V8::initializecu();
V8::InitializeExternalStartUpdatea(argv[0]);
平台*Platform=Platform::CreateDefaultPlatform();
V8::初始化平台(平台);
V8::Initialize();
//创建新隔离并使其成为当前隔离。
ArrayBufferAllocator分配器;
隔离::CreateParams创建_参数;
创建参数数组缓冲区分配器=&allocator;
隔离*隔离=隔离::新建(创建参数);
{
隔离::范围隔离\范围(隔离);
//创建堆栈分配的句柄范围。
手柄镜手柄范围(隔离);
//创建一个新的上下文。
v8::Local global=v8::ObjectTemplate::New(隔离);
全局->设置(v8::String::NewFromUtf8(隔离,“日志消息”),
v8::FunctionTemplate::New(隔离,日志消息));
全局->设置(v8::String::NewFromUtf8(隔离,“getInput”),
v8::FunctionTemplate::New(隔离,获取输入));
全局->设置(v8::String::NewFromUtf8(隔离“saveFunc”),
v8::FunctionTemplate::New(隔离,saveFunc));
context=context::New(隔离、空、全局);
//输入编译和运行hello world脚本的上下文。
上下文::范围上下文\范围(上下文);
//创建包含JavaScript源代码的字符串。
本地来源=
字符串::NewFromUtf8(隔离,
函数测试(){logMessage('asynchronous call make!');}saveFunc(test);“,”,,
newString类型::kNormal).ToLocalChecked();
//编译源代码。
本地脚本=脚本::编译(上下文,源).ToLocalChecked();
//运行脚本以获得结果。
本地结果=脚本->运行(上下文).ToLocalChecked();
v8::Local*args=新的v8::Local[0];
savedFunc->Call(上下文->全局(),0,args);
删除[]args;
args=NULL;
}
//处理隔离阀并拆下V8。
隔离->处置();
V8::Dispose();
V8::关闭平台();
删除平台;
返回0;
}

好的,因此在saveFunc中保存函数时必须使用持久句柄。此外,您必须确保当您访问context->Global时,您仍然在HandleScope中。以下是更正后的代码:

// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <iostream>
#include <string>

#include "libplatform/libplatform.h"
#include "v8.h"

using namespace v8;

Isolate* isolate = NULL;
Local<Context> context;
v8::Persistent<v8::Function> *savedFunc = NULL;

class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
    virtual void* Allocate(size_t length) {
        void* data = AllocateUninitialized(length);
        return data == NULL ? data : memset(data, 0, length);
    }
    virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
    virtual void Free(void* data, size_t) { free(data); }
};

std::string parseV8Value(v8::Local<v8::Value> str)
{
    if (str.IsEmpty() == true)
        return ("");

    v8::String::Utf8Value newStr(str);

    return (*newStr);
}

void logMessage(const v8::FunctionCallbackInfo<v8::Value> &args)
{
    std::string applicationSource = parseV8Value(args[0]);
    std::cout << applicationSource << "\n";
}

void getInput(const v8::FunctionCallbackInfo<v8::Value> &args)
{
    v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast (args[0]);
    std::string input = "";

    std::cin >> input;

    v8::Local<v8::Value> *args2 = new v8::Local<v8::Value>[1];
    args2[0] = v8::String::NewFromUtf8 (args.GetIsolate (), input.c_str ());

    func->Call (context->Global (), 1, args2);

    delete []args2;
    args2 = NULL;
}

void saveFunc(const v8::FunctionCallbackInfo<v8::Value> &args)
{
    v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(args[0]);
    savedFunc = new v8::Persistent<v8::Function>();
    savedFunc->Reset(isolate, func);
}

int main(int argc, char* argv[]) {
    // Initialize V8.
    V8::InitializeICU();
    V8::InitializeExternalStartupData(argv[0]);
    Platform* platform = platform::CreateDefaultPlatform();
    V8::InitializePlatform(platform);
    V8::Initialize();

    // Create a new Isolate and make it the current one.
    ArrayBufferAllocator allocator;
    Isolate::CreateParams create_params;
    create_params.array_buffer_allocator = &allocator;
    isolate = Isolate::New(create_params);
    {
        Isolate::Scope isolate_scope(isolate);

        // Create a stack-allocated handle scope.
        HandleScope handle_scope(isolate);

        // Create a new context.
        v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
        global->Set(v8::String::NewFromUtf8(isolate, "logMessage"),
            v8::FunctionTemplate::New(isolate, logMessage));
        global->Set(v8::String::NewFromUtf8(isolate, "getInput"),
            v8::FunctionTemplate::New(isolate, getInput));
        global->Set(v8::String::NewFromUtf8(isolate, "saveFunc"),
            v8::FunctionTemplate::New(isolate, saveFunc));
        context = Context::New(isolate, NULL, global);

        // Enter the context for compiling and running the hello world script.
        Context::Scope context_scope(context);

        // The "asynchronous" javascript call to make
        Local<String> source =
            String::NewFromUtf8(isolate, 
    "function test (){ logMessage ('asynchronous call made!'); }saveFunc(test);",
                NewStringType::kNormal).ToLocalChecked();

        // Compile the source code.
        Local<Script> script = Script::Compile(context, source).ToLocalChecked();

        // Run the script to get the result.
        Local<Value> result = script->Run(context).ToLocalChecked();

        v8::Local<v8::Value> *args = new v8::Local<v8::Value>[0];
        v8::Local<v8::Value> recv = context->Global();
        v8::Local<v8::Function> func = savedFunc->Get(isolate);
        func->Call (recv, 0, args);

        delete savedFunc;
        savedFunc = NULL;

        delete []args;
        args = NULL;
    }

    // Dispose the isolate and tear down V8.
    isolate->Dispose();
    V8::Dispose();
    V8::ShutdownPlatform();
    delete platform;
    return 0;
}
//版权归V8项目作者所有。版权所有。
//此源代码的使用受BSD样式许可证的约束,该许可证可以
//在许可证文件中找到。
#包括
#包括
#包括“libplatform/libplatform.h”
#包括“v8.h”
使用名称空间v8;
隔离*隔离=空;
地方背景;
v8::Persistent*savedFunc=NULL;
类ArrayBufferAllocator:PublicV8::ArrayBuffer::Allocator{
公众:
虚拟空*分配(大小/长度){
void*data=AllocateUninitialized(长度);
返回数据==NULL?数据:memset(数据,0,长度);
}
虚拟void*AllocateUninitialized(size_t length){return malloc(length);}
虚拟无空(void*数据,大小_t){Free(数据);}
};
std::string parseV8Value(v8::Local str)
{
if(str.IsEmpty()==true)
返回(“”);
v8::String::Utf8Value newStr(str);
返回(*newStr);
}
void日志消息(const v8::FunctionCallbackInfo&args)
{
std::string applicationSource=parseV8Value(args[0]);
std::cout输入;
v8::Local*args2=新的v8::Local[1];
args2[0]=v8::String::NewFromUtf8(args.GetIsolate(),input.c_str());
func->Call(上下文->全局(),1,args2);
删除[]args2;
args2=NULL;
}
void saveFunc(const v8::FunctionCallbackInfo&args)
{
v8::Local func=v8::Local::Cast(args[0]);
savedFunc=newv8::Persistent();
savedFunc->Reset(隔离,功能);
}
int main(int argc,char*argv[]){
//初始化V8。
V8::initializecu();
V8::InitializeExternalStartUpdatea(argv[0]);
平台*Platform=Platform::CreateDefaultPlatform();
V8::初始化平台(平台);
V8::Initialize();
//创建新隔离并使其成为当前隔离。
ArrayBufferAllocator分配器;
隔离::CreateParams创建_参数;
创建参数数组缓冲区分配器=&allocator;
隔离=隔离::新建