Pergunta

I get this error when I try to run some JS code (this + this) with V8 (tried master from two weaks ago, 3.23.17, 3.24.40, 3.25.5; 3.23.0 doesn't work anymore because of API changes):

#
# Fatal error in ..\..\src\runtime.cc, line 785
# CHECK(V8::ArrayBufferAllocator() != NULL) failed
#

A lot of other JS code has worked already, so I wonder what the problem is.

It's on Win8 with a x64 build. V8 has been build just as described in the official docs (using gyp + MSVC 2012). I don't think that there was a problem because it worked fine already with most other JS code.

I think that there might be an issue with V8 itself, but not sure...

I also asked on the mailing-list here.


Some C++ code, but I don't think that there is a problem with it because it worked fine with other JS code:

#include <string>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <boost/noncopyable.hpp>
#include <v8.h>


// Create a new isolate (completely isolated JS VM).
struct V8Isolate : boost::noncopyable {
    v8::Isolate* isolate;
    V8Isolate() : isolate(v8::Isolate::New()) {}
    ~V8Isolate() { isolate->Dispose(); }
    operator v8::Isolate*() { return isolate; }
    v8::Isolate* operator->() { return isolate; }
};

struct ReturnType {
    std::string err; // non-empty if there is an error

    ReturnType(bool success) {
        assert(success);
    }
    ReturnType(const std::string& _err) : err(_err) {
        assert(!err.empty());
    }
    ReturnType(const char* _err) : err(_err) {
        assert(!err.empty());
    }

    operator bool() const { return err.empty(); }
};

#define CHECK_RETURN(cmd) { ReturnType ret = (cmd); if(!ret) return ret; }

using namespace std;
using namespace v8;

ReturnType readFile(const std::string& filename, std::string& res) {
    res = "";

    FILE* f = fopen(filename.c_str(), "r");
    if(!f) return "File '" + filename + "' cannot be opened";

    while(!feof(f) && !ferror(f)) {
        char buffer[1024 * 8];
        size_t s = fread(buffer, 1, sizeof(buffer), f);
        if(s > 0)
            res.append(buffer, buffer + s);
    }

    auto err = ferror(f);
    fclose(f);

    if(err)
        return "Error while reading file '" + filename + "'";

    return true;
}



ReturnType execJsFile(const std::string& jsSourceDir, const std::string& extfilename) {
    v8::TryCatch try_catch;

    std::string sourceStr;
    CHECK_RETURN(readFile(jsSourceDir + "/" + extfilename, sourceStr));

    Local<String> origin = String::NewFromUtf8(Isolate::GetCurrent(), &extfilename[0], String::kNormalString, (int)extfilename.size());
    Local<String> source = String::NewFromUtf8(Isolate::GetCurrent(), &sourceStr[0], String::kNormalString, (int)sourceStr.size());

    Local<v8::Script> script = Script::Compile(source, origin);
    if(script.IsEmpty()) {
        assert(try_catch.HasCaught());
        return "JS compile failed: " + jsObjToString(try_catch.Exception());
    }

    // Run the script to setup its environment
    Local<Value> result = script->Run();
    if(result.IsEmpty()) {
        assert(try_catch.HasCaught());
        return "JS script execution failed: " + jsReportExceptionToString(Isolate::GetCurrent(), &try_catch);
    }

    return true;
}


ReturnType loadJsGit() {
    V8Isolate isolate;
    v8::Isolate::Scope isolateScope(isolate);
    HandleScope handleScope(isolate);
    Handle<Context> context = Context::New(isolate);
    Context::Scope contextScope(context);
    auto globalObj = context->Global();

    CHECK_RETURN(execJsFile(".", "global.js"));
    CHECK_RETURN(execJsFile(".", "jsgit.js"));

    return true;
}

int main(int argc, char** argv) {
    ReturnType ret = loadJsGit();
    if(!ret) cout << "error: " << ret.err << endl;
}
Foi útil?

Solução

You need to initialize array buffer allocator.

Use malloc, for example:

class MallocArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
  virtual void* Allocate(size_t length) { return malloc(length); }
  virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
  virtual void Free(void* data, size_t length) { free(data); }
};

Initialize:

v8::V8::SetArrayBufferAllocator(new MallocArrayBufferAllocator);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top