I'm trying to understand what I'm doing wrong here.
I have created an ObjectTemplate for the global object which contains a method, a property, and an internal field. After creating the context, I use Global()->SetAlignedPointerInInternalField() to set a pointer on the object, then I call the function and read the property.
In the function callback, I'm able to read the pointer from the internal field as expected.
InternalFieldCount(), though, still returns the actual number of internal fields I allocated. So it seems like it's *supposed* to be the right object.
OTOH, GetIdentityHash() returns something that doesn't match context.Global()->GetIdentityHash(), whereas in the function callback these do match.
I'm using v8 at commit 49d32849b3e67b1fa05f5f7aeea57dd83634adb9 (April 14).
Sample code and output below.
Surely people have created properties on the global object before, so I must be doing it wrong. What's the right way to do it?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libplatform/libplatform.h>
#include <v8.h>
void funcCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
printf("in func()\n");
printf(" this identity = %x\n", info.This()->GetIdentityHash());
printf(" holder identity = %x\n", info.Holder()->GetIdentityHash());
printf(" InternalFieldCount = %d\n", info.This()->InternalFieldCount());
// This works fine.
printf(" GetAlignedPointerFromInternalField(0) = %s\n",
(const char*)info.This()->GetAlignedPointerFromInternalField(0));
}
void propCallback(v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value>& info) {
printf("getting prop\n");
printf(" this identity = %x\n", info.This()->GetIdentityHash());
printf(" holder identity = %x\n", info.Holder()->GetIdentityHash());
printf(" InternalFieldCount = %d\n", info.This()->InternalFieldCount());
// THIS CRASHES
printf(" GetAlignedPointerFromInternalField(0) = %s\n",
(const char*)info.This()->GetAlignedPointerFromInternalField(0));
}
int main(int argc, char* argv[]) {
// Initialize V8.
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
v8::V8::InitializeICUDefaultLocation(argv[0]);
v8::V8::InitializeExternalStartupData(argv[0]);
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform);
v8::V8::Initialize();
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);
{
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
// Create global ObjectTemplate.
auto globalInstanceTmpl = v8::ObjectTemplate::New(isolate);
globalInstanceTmpl->SetInternalFieldCount(123);
globalInstanceTmpl->Set(isolate, "func", v8::FunctionTemplate::New(isolate, &funcCallback));
globalInstanceTmpl->SetAccessor(
v8::String::NewFromUtf8(isolate, "prop", v8::NewStringType::kInternalized).ToLocalChecked(),
&propCallback);
v8::Local<v8::Context> context = v8::Context::New(isolate, nullptr, globalInstanceTmpl);
// Set internal field pointer on global.
alignas(long long) const char TEXT[] = "internal-field-value";
context->Global()->SetAlignedPointerInInternalField(0, (void*)TEXT);
printf("global identity = %x\n", context->Global()->GetIdentityHash());
// Call func() then read prop.
v8::Context::Scope context_scope(context);
v8::Local<v8::String> source =
v8::String::NewFromUtf8(isolate, "func(); prop;",
v8::NewStringType::kNormal).ToLocalChecked();
v8::Local<v8::Script> script = v8::Script::Compile(context, source).ToLocalChecked();
(void)script->Run(context);
}
isolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete platform;
delete create_params.array_buffer_allocator;
return 0;
}