#include #include #include "v8pp/module.hpp" class DefaultV8ArrayBufferAllocator: 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); } }; int plusOne(int x) { return x+1; } int main(int argc, char** argv) { std::function plusOneStdFunction = [](int x) { return x+1; }; std::unique_ptr alloc_ (new DefaultV8ArrayBufferAllocator); v8::Platform* platform(nullptr); std::string flags("--expose_gc"); v8::V8::SetFlagsFromString(flags.c_str(), flags.size()); v8::V8::InitializeICU(); v8::V8::InitializeExternalStartupData(argv[0]); platform = v8::platform::CreateDefaultPlatform(); v8::V8::InitializePlatform(platform); v8::V8::Initialize(); { v8::Isolate::CreateParams createParams; createParams.array_buffer_allocator = alloc_.get(); v8::Isolate* isolate = v8::Isolate::New(createParams); //isolate->SetCaptureStackTraceForUncaughtExceptions(true); { v8::Isolate::Scope is(isolate); v8::HandleScope hs(isolate); v8::Local context = v8::Context::New(isolate); v8::Context::Scope cs(context); v8pp::module mod(isolate); // This doesn't result in assertion failure when we try to do // garbage collection later (if we uncomment it): // mod.set("plusOne", plusOne); // This is also OK if we uncomment it: // mod.set("plusOne", [](int x) { return x+1; }); // ...but this results in an assertion failure when we do // garbage collection later: "Check failed: node_->state() == Node::FREE." mod.set("plusOne", plusOneStdFunction); context->Global()->Set (v8::String::NewFromUtf8(isolate, "mod"), mod.new_instance()); } isolate->RequestGarbageCollectionForTesting (v8::Isolate::kFullGarbageCollection); isolate->Dispose(); } v8::V8::Dispose(); v8::V8::ShutdownPlatform(); delete platform; platform = nullptr; return 0; }