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

时间:2016-05-06 05:23:25

标签: c++ google-chrome embedded-v8 libv8

V8是破了还是我?

我想将JS Date添加到全局对象中可用的Object。这适用于旧版本(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

1 个答案:

答案 0 :(得分:0)

您无法将JavaScript对象添加到ObjectTemplates。您只能添加模板。这在您提到的版本之间发生了变化。

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

但是,在调用NewInstance()之后,您可以自由地将任何内容与结果对象相关联。