I'm trying to use v8::NativeWeakMap on the global object, and I'm getting inconsistent results. It looks like "this" (on the outer scope)
and just using plain variables (on the outer scope) get me to two different places. How is that possible? Here is a sample program:
#include "libplatform/libplatform.h"
void runit(v8::Isolate *isolate, const char *script_text)
std::cout << "------------------------------------------" << std::endl;
std::cout << "/" << script_text << "/ <==" << std::endl;
v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate,script_text);
v8::Local<v8::Script> script = v8::Script::Compile(source);
v8::Local<v8::Value> result = script->Run();
v8::String::Utf8Value ascii(result);
std::cout << "/" << script_text << "/ ==> " << *ascii << std::endl;
v8::Persistent<v8::NativeWeakMap, v8::CopyablePersistentTraits<v8::NativeWeakMap> > gWeakMap;
void GetNamedAccessor(v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info)
std::cout << "[" << std::hex << info.This()->GetIdentityHash() << "] GetNamedAccessor, name=" << *v8::String::Utf8Value(name) << std::endl;
void SetNamedAccessor(v8::Local<v8::Name> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info)
std::cout << "[" << std::hex << info.This()->GetIdentityHash() << "] SetNamedAccessor, name=" << *v8::String::Utf8Value(name)
<< ", value=" << *v8::String::Utf8Value(value) << std::endl;
auto myWeakMap = v8::Local<v8::NativeWeakMap>::New(info.GetIsolate(),gWeakMap);
v8::Local<v8::Value> val = myWeakMap->Get(info.This());
std::cout << "[ weak map holds " << *v8::String::Utf8Value(val) << " ]" << std::endl;
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
virtual void* Allocate(size_t length) {
std::cout << "!!! Allocate runs !!! length = " << length << std::endl;
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 main(int argc, char* argv[])
v8::V8::InitializeExternalStartupData(argv[0]);
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform);
ArrayBufferAllocator allocator;
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = &allocator;
auto isolate = v8::Isolate::New(create_params);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
auto context = v8::Context::New(isolate);
auto glob = context->Global();
v8::Context::Scope context_scope(context);
auto myWeakMap = v8::NativeWeakMap::New(isolate);
auto vvvStr = v8::String::NewFromUtf8(isolate,"vvv");
myWeakMap->Set(glob, vvvStr);
gWeakMap.Reset(isolate,myWeakMap);
auto onStr = v8::String::NewFromUtf8(isolate,"on");
glob->SetAccessor(onStr, GetNamedAccessor, SetNamedAccessor);
runit(isolate,"this.on = 1");
As you can see, the hash value for the second case is different, and theNativeWeakMap doesn't work at all.
Please note, that the named accessor works properly in both cases.