当ObjectTemplate具有Date成员时,V8 NewInstance失败

V8 NewInstance failing when ObjectTemplate has a Date member

本文关键字:V8 NewInstance 失败 成员 ObjectTemplate 具有 Date      更新时间:2023-10-16

V8坏了还是我坏了?

我想将JS日期添加到全局对象中可用的对象中。这适用于旧版本(4.9.385.28),但在(5.0.71.33)时失败…请参阅输出

g++-I/usr/local-core.c-o testCore-ldl-pthread-std=c++0x-lv8-lv8_libplatform-lv8_libbase

core.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/libplatform/libplatform.h"
#include "include/v8.h"
#include "time.h"
using namespace v8;
class ArrayBufferAllocator : 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 main(int argc, char* argv[])
{
    // Initialize V8.
    V8::InitializeICU();
    V8::InitializeExternalStartupData(argv[0]);
    Platform* platform = platform::CreateDefaultPlatform();
    V8::InitializePlatform(platform);
    V8::Initialize();
    // Create a new Isolate and make it the current one.
    ArrayBufferAllocator allocator;
    Isolate::CreateParams create_params;
    create_params.array_buffer_allocator = &allocator;
    Isolate* isolate = Isolate::New(create_params);
    Isolate::Scope isolate_scope(isolate);
    // Create a stack-allocated handle scope.
    HandleScope handle_scope(isolate);
    Local<Context> context = Context::New(isolate,NULL,ObjectTemplate::New(isolate));
    Context::Scope context_scope(context);
    Local<ObjectTemplate> activity = ObjectTemplate::New(isolate);
    // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    // ************************ This line breaks the NewInstance() call (5.0.71.33) works in (4.9.385.28) *****************************
    // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    activity->Set        (String::NewFromUtf8(isolate, "createddate", NewStringType::kNormal).ToLocalChecked(),     Date::New(isolate,time(NULL)*1000.0));
    Local<Object> activityInst = activity->NewInstance();
    context->Global()->Set(String::NewFromUtf8(isolate, "activity", NewStringType::kNormal).ToLocalChecked(), activityInst);
    return 0;
}

输出:

#
# Fatal error in ../src/heap/heap.cc, line 3564
# Check failed: map->instance_type() == JS_REGEXP_TYPE || map->instance_type() == JS_OBJECT_TYPE || map->instance_type() == JS_ARRAY_TYPE.
#
==== C stack trace ===============================
1: V8_Fatal
2: v8::internal::Heap::CopyJSObject(v8::internal::JSObject*, v8::internal::AllocationSite*)
3: v8::internal::Factory::CopyJSObjectWithAllocationSite(v8::internal::Handle<v8::internal::JSObject>, v8::internal::Handle<v8::internal::AllocationSite>)
4: v8::internal::JSObjectWalkVisitor<v8::internal::DummyContextObject, (v8::internal::BoilerplateKind)1>::StructureWalk(v8::internal::Handle<v8::internal::JSObject>)
5: v8::internal::JSObjectWalkVisitor<v8::internal::DummyContextObject, (v8::internal::BoilerplateKind)1>::StructureWalk(v8::internal::Handle<v8::internal::JSObject>)
6: v8::internal::JSObject::DeepCopyApiBoilerplate(v8::internal::Handle<v8::internal::JSObject>)
7: v8::internal::(anonymous namespace)::InstantiateObject(v8::internal::Isolate*, v8::internal::Handle<v8::internal::ObjectTemplateInfo>, bool)
8: v8::internal::ApiNatives::InstantiateObject(v8::internal::Handle<v8::internal::ObjectTemplateInfo>)
9: v8::ObjectTemplate::NewInstance(v8::Local<v8::Context>)
10: main
11: start
12: 0x1
Illegal instruction

您不能将javascript对象添加到ObjectTemplates中。您只能添加模板。这在你提到的版本之间发生了变化。

我认为,这个想法是,一个模板会被实例化很多次,将同一个对象与每个对象关联起来是没有意义的。而关联的模板也将被重新实例化,为每个父对象提供不同的子对象。

不过,在调用NewInstance()之后,您可以自由地将任何内容与生成的对象相关联。