c++ - V8 javascript如何进行异步调用

标签 c++ asynchronous v8 embedded-v8 libv8

我一直在尝试找出如何在 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;
}

最佳答案

好的,所以在 saveFunc 中保存函数时必须使用 Persistent 句柄。此外,您必须确保当您访问 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;
}

关于c++ - V8 javascript如何进行异步调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37615799/

相关文章:

node.js - Nodejs 中的集合查找似乎没有 O(1)

c++ - 均匀实数分布的范围限制是多少?

c++ - 显示二进制加法后的最后一位

android - cmake 失败与 android ndk

javascript - 我是否需要在异步函数中的每个语句之前放置await?

node.js - 使用 webfont 生成字体 : FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

c++ - if 赋值/语句在 C++ 中意味着什么

android - 如何处理 loopj/android-async-http 中的后按 Action

javascript - 自己线程中的 Selenium 异步脚本会阻止其他脚本

javascript - 如何在 Javascript 中释放前一个堆栈帧的内存