From 6618793e876a20ebe91a87eb20069ef4070280ed Mon Sep 17 00:00:00 2001 From: ben Date: Wed, 6 May 2015 07:10:38 -0700 Subject: [PATCH] Add ObjectTemplate::New() taking FunctionTemplate. I know the bug has been closed but this seems like a simple addition that may be useful in other ways as well. BUG=v8:2180 LOG=N Review URL: https://codereview.chromium.org/1128553002 Cr-Commit-Position: refs/heads/master@{#28261} --- include/v8.h | 4 +++- src/api.cc | 5 +++-- test/cctest/test-api.cc | 13 +++++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/include/v8.h b/include/v8.h index 73e5e5c4ee..910279b52e 100644 --- a/include/v8.h +++ b/include/v8.h @@ -4274,7 +4274,9 @@ struct IndexedPropertyHandlerConfiguration { class V8_EXPORT ObjectTemplate : public Template { public: /** Creates an ObjectTemplate. */ - static Local New(Isolate* isolate); + static Local New( + Isolate* isolate, + Handle constructor = Handle()); static V8_DEPRECATE_SOON("Use isolate version", Local New()); /** Creates a new instance of this template.*/ diff --git a/src/api.cc b/src/api.cc index 9986df8bf2..4f16120df3 100644 --- a/src/api.cc +++ b/src/api.cc @@ -1198,8 +1198,9 @@ void FunctionTemplate::RemovePrototype() { // --- O b j e c t T e m p l a t e --- -Local ObjectTemplate::New(Isolate* isolate) { - return New(reinterpret_cast(isolate), Local()); +Local ObjectTemplate::New( + Isolate* isolate, v8::Handle constructor) { + return New(reinterpret_cast(isolate), constructor); } diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index 12f481019b..2910cfcd95 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -1782,17 +1782,22 @@ THREADED_TEST(GlobalPrototype) { THREADED_TEST(ObjectTemplate) { v8::Isolate* isolate = CcTest::isolate(); v8::HandleScope scope(isolate); - Local templ1 = ObjectTemplate::New(isolate); + Local fun = v8::FunctionTemplate::New(isolate); + v8::Local class_name = + v8::String::NewFromUtf8(isolate, "the_class_name"); + fun->SetClassName(class_name); + Local templ1 = ObjectTemplate::New(isolate, fun); templ1->Set(isolate, "x", v8_num(10)); templ1->Set(isolate, "y", v8_num(13)); LocalContext env; Local instance1 = templ1->NewInstance(); + CHECK(class_name->StrictEquals(instance1->GetConstructorName())); env->Global()->Set(v8_str("p"), instance1); CHECK(v8_compile("(p.x == 10)")->Run()->BooleanValue()); CHECK(v8_compile("(p.y == 13)")->Run()->BooleanValue()); - Local fun = v8::FunctionTemplate::New(isolate); - fun->PrototypeTemplate()->Set(isolate, "nirk", v8_num(123)); - Local templ2 = fun->InstanceTemplate(); + Local fun2 = v8::FunctionTemplate::New(isolate); + fun2->PrototypeTemplate()->Set(isolate, "nirk", v8_num(123)); + Local templ2 = fun2->InstanceTemplate(); templ2->Set(isolate, "a", v8_num(12)); templ2->Set(isolate, "b", templ1); Local instance2 = templ2->NewInstance();