help setting an object prototype

37 views
Skip to first unread message

Liam Goodacre

unread,
May 3, 2022, 1:46:14 PM5/3/22
to v8-users

I am trying to set the prototype of a v8::Object, but I can’t seem to get it to work.

First I tried the following:

        v8::Local<v8::Object> prot = v8::Object::New(isolate);

        prot->Set(context, v8::String::NewFromUtf8(isolate, "f").ToLocalChecked(), v8::Int32::New(isolate, 66));

        context->Global()->SetPrototype(context, prot);

 

But I can’t access property f in Javascript:

// javascript:

f;           // undefined

__proto__.f; // undefined

 

Then I tried constructing the prototype in place with the overloaded Object::New(…) function:

    v8::Local<v8::Name> n = v8::String::NewFromUtf8(isolate, "what").ToLocalChecked();

    v8::Local<v8::Value> v = v8::Number::New(isolate, 4.8);

    auto ob = v8::Object::New(isolate);

    auto glob = v8::Object::New(isolate, ob, &n, &v, 0);

The idea was to then emplace ‘glob’ into a Context::NewRemoteContext(). But I can’t get that far, because the Object::New() function crashes. This might have something to do with the extra property arguments—I’m not really sure what they do, and the documentation doesn’t say anything about them. But I’ve tried many different combinations with them, and I get the same crash. I do get a stack trace here:

==== C stack trace ===============================

 

        v8::base::debug::StackTrace::StackTrace [0x00007FFEEF84CB9B+27]

        v8::platform::DefaultPlatform::GetStackTracePrinter [0x00007FFEEFD5C508+72]

        V8_Fatal [0x00007FFEEF82E8E2+210]

        v8::base::SetDcheckFunction [0x00007FFEEF82E133+51]

        v8::Object::New [0x00007FFE5E5B0DCC+492]

        main [0x00007FF7E244AE4E+1566] (C:\Users\lgoodacre\source\repos\v8_sandbox\v8_sandbox\Source.cpp:99)

        invoke_main [0x00007FF7E244C999+57] (d:\a01\_work\20\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:79)

        __scrt_common_main_seh [0x00007FF7E244C83E+302] (d:\a01\_work\20\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288)

        __scrt_common_main [0x00007FF7E244C6FE+14] (d:\a01\_work\20\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:331)

        mainCRTStartup [0x00007FF7E244CA2E+14] (d:\a01\_work\20\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp:17)

        BaseThreadInitThunk [0x00007FFF39677034+20]

        RtlUserThreadStart [0x00007FFF39A02651+33]

 

Can anyone tell me how I can set the prototype from within C++? I feel like it shouldn’t be this hard.

(Note that I know how to do it using a FunctionTemplate. But I don’t think that this will work in my case, as the I want to set the prototype dynamically, so different instances have different prototypes).

Reply all
Reply to author
Forward
0 new messages