0

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

核心.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
4

1 回答 1

0

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

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

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

于 2016-09-16T05:18:54.997 回答