当ObjectTemplate具有Date成员时,V8 NewInstance失败
V8 NewInstance failing when ObjectTemplate has a Date member
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()之后,您可以自由地将任何内容与生成的对象相关联。
相关文章:
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 在 G++ v8.2.0 中使用 std::experimental::可选
- 如何为 v8::P ersistent<v8::Function> 创建到 c++ 函数的包装器
- v8::HandleScope::CreateHandle() # 中的致命错误无法在没有 HandleScope 的情
- 如何将节点 V8 字符串转换为 C++ 字符串
- 在 C++ 中嵌入 V8 会导致与 libcpp 相关的"unrecognized external symbol"错误
- 当我使用 V8 库中的 GetInternalField() 时出现分段错误
- 在 v8 JavaScript 中重复调用C++是否有巨大的开销?
- 我可以使用谷歌 v8 在回调函数中获取 JavaScript 函数C++源文本吗?
- 与纯 V8 相比,NodeJS 是否有任何性能缺陷或显著开销?
- V8垃圾收集器回调,用于测量GC活动
- 如何在 C++ 中从 V8 调用 Javascript 函数
- 如何将 v8::FunctionCallbackInfo<v8::Value> 数组从一个隔离复制到另一个隔离?
- 使用导入的函数从嵌入式v8调用webassembly
- v8 源代码中 ArrayMap 函数的回调 fn 参数是什么?
- 如何在 v8 Javascript 中的多个函数中使用相同的上下文?
- 如何使用 v8 本机插件将 C++ 数组交付到 Node.js
- 存储/传递 v8 承诺解析器供以后使用的最佳实践?(结合C++线程)
- v8 不再支持 v8::Value::ToNumber 了吗?
- 当ObjectTemplate具有Date成员时,V8 NewInstance失败