| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "gin/arguments.h" | 6 #include "gin/arguments.h" |
| 7 #include "gin/handle.h" | 7 #include "gin/handle.h" |
| 8 #include "gin/object_template_builder.h" | 8 #include "gin/object_template_builder.h" |
| 9 #include "gin/per_isolate_data.h" | 9 #include "gin/per_isolate_data.h" |
| 10 #include "gin/public/isolate_holder.h" | 10 #include "gin/public/isolate_holder.h" |
| 11 #include "gin/test/v8_test.h" | 11 #include "gin/test/v8_test.h" |
| 12 #include "gin/try_catch.h" | 12 #include "gin/try_catch.h" |
| 13 #include "gin/wrappable.h" | 13 #include "gin/wrappable.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace gin { | 16 namespace gin { |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 class MyObject : public Wrappable { | 19 class MyObject : public Wrappable<MyObject> { |
| 20 public: | 20 public: |
| 21 static gin::Handle<MyObject> Create(v8::Isolate* isolate); | 21 static gin::Handle<MyObject> Create(v8::Isolate* isolate); |
| 22 | 22 |
| 23 int value() const { return value_; } | 23 int value() const { return value_; } |
| 24 void set_value(int value) { value_ = value; } | 24 void set_value(int value) { value_ = value; } |
| 25 | 25 |
| 26 static WrapperInfo kWrapperInfo; | |
| 27 virtual WrapperInfo* GetWrapperInfo() OVERRIDE; | |
| 28 | |
| 29 private: | 26 private: |
| 30 MyObject() : value_(0) {} | 27 MyObject() : value_(0) {} |
| 31 virtual ~MyObject() {} | 28 virtual ~MyObject() {} |
| 32 | 29 |
| 33 int value_; | 30 int value_; |
| 34 }; | 31 }; |
| 35 | 32 |
| 36 WrapperInfo MyObject::kWrapperInfo = { kEmbedderNativeGin }; | |
| 37 | |
| 38 gin::Handle<MyObject> MyObject::Create(v8::Isolate* isolate) { | 33 gin::Handle<MyObject> MyObject::Create(v8::Isolate* isolate) { |
| 39 return CreateHandle(isolate, new MyObject()); | 34 return CreateHandle(isolate, new MyObject()); |
| 40 } | 35 } |
| 41 | 36 |
| 42 WrapperInfo* MyObject::GetWrapperInfo() { | 37 class MyObject2 : public Wrappable<MyObject2> { |
| 43 return &kWrapperInfo; | |
| 44 } | |
| 45 | |
| 46 | |
| 47 class MyObject2 : public Wrappable { | |
| 48 public: | |
| 49 MyObject2() { | |
| 50 } | |
| 51 static WrapperInfo kWrapperInfo; | |
| 52 virtual WrapperInfo* GetWrapperInfo() OVERRIDE; | |
| 53 }; | 38 }; |
| 54 | 39 |
| 55 WrapperInfo MyObject2::kWrapperInfo = { kEmbedderNativeGin }; | 40 class MyObjectBlink : public Wrappable<MyObjectBlink> { |
| 56 | |
| 57 WrapperInfo* MyObject2::GetWrapperInfo() { | |
| 58 return &kWrapperInfo; | |
| 59 } | |
| 60 | |
| 61 | |
| 62 class MyObjectBlink : public Wrappable { | |
| 63 public: | |
| 64 MyObjectBlink() { | |
| 65 } | |
| 66 static WrapperInfo kWrapperInfo; | |
| 67 virtual WrapperInfo* GetWrapperInfo() OVERRIDE; | |
| 68 }; | 41 }; |
| 69 | 42 |
| 70 WrapperInfo MyObjectBlink::kWrapperInfo = { kEmbedderBlink }; | |
| 71 | |
| 72 WrapperInfo* MyObjectBlink::GetWrapperInfo() { | |
| 73 return &kWrapperInfo; | |
| 74 } | |
| 75 | |
| 76 | 43 |
| 77 void RegisterTemplates(v8::Isolate* isolate) { | 44 void RegisterTemplates(v8::Isolate* isolate) { |
| 78 PerIsolateData* data = PerIsolateData::From(isolate); | 45 PerIsolateData* data = PerIsolateData::From(isolate); |
| 79 DCHECK(data->GetObjectTemplate(&MyObject::kWrapperInfo).IsEmpty()); | 46 DCHECK(data->GetObjectTemplate( |
| 47 &WrappableTraits<MyObject>::kWrapperInfo).IsEmpty()); |
| 80 | 48 |
| 81 v8::Handle<v8::ObjectTemplate> templ = ObjectTemplateBuilder(isolate) | 49 v8::Handle<v8::ObjectTemplate> templ = ObjectTemplateBuilder(isolate) |
| 82 .SetProperty("value", &MyObject::value, &MyObject::set_value) | 50 .SetProperty("value", &MyObject::value, &MyObject::set_value) |
| 83 .Build(); | 51 .Build(); |
| 84 templ->SetInternalFieldCount(kNumberOfInternalFields); | 52 templ->SetInternalFieldCount(kNumberOfInternalFields); |
| 85 data->SetObjectTemplate(&MyObject::kWrapperInfo, templ); | 53 data->SetObjectTemplate(&WrappableTraits<MyObject>::kWrapperInfo, templ); |
| 86 | 54 |
| 87 templ = v8::ObjectTemplate::New(isolate); | 55 templ = v8::ObjectTemplate::New(isolate); |
| 88 templ->SetInternalFieldCount(kNumberOfInternalFields); | 56 templ->SetInternalFieldCount(kNumberOfInternalFields); |
| 89 data->SetObjectTemplate(&MyObject2::kWrapperInfo, templ); | 57 data->SetObjectTemplate(&WrappableTraits<MyObject2>::kWrapperInfo, templ); |
| 90 | 58 |
| 91 templ = v8::ObjectTemplate::New(isolate); | 59 templ = v8::ObjectTemplate::New(isolate); |
| 92 templ->SetInternalFieldCount(kNumberOfInternalFields); | 60 templ->SetInternalFieldCount(kNumberOfInternalFields); |
| 93 data->SetObjectTemplate(&MyObjectBlink::kWrapperInfo, templ); | 61 data->SetObjectTemplate(&WrappableTraits<MyObjectBlink>::kWrapperInfo, templ); |
| 94 } | 62 } |
| 95 | 63 |
| 96 typedef V8Test WrappableTest; | 64 typedef V8Test WrappableTest; |
| 97 | 65 |
| 98 TEST_F(WrappableTest, WrapAndUnwrap) { | 66 TEST_F(WrappableTest, WrapAndUnwrap) { |
| 99 v8::Isolate* isolate = instance_->isolate(); | 67 v8::Isolate* isolate = instance_->isolate(); |
| 100 v8::HandleScope handle_scope(isolate); | 68 v8::HandleScope handle_scope(isolate); |
| 101 | 69 |
| 102 RegisterTemplates(isolate); | 70 RegisterTemplates(isolate); |
| 103 Handle<MyObject> obj = MyObject::Create(isolate); | 71 Handle<MyObject> obj = MyObject::Create(isolate); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 }; | 136 }; |
| 169 func->Call(v8::Undefined(isolate), 1, argv); | 137 func->Call(v8::Undefined(isolate), 1, argv); |
| 170 EXPECT_FALSE(try_catch.HasCaught()); | 138 EXPECT_FALSE(try_catch.HasCaught()); |
| 171 EXPECT_EQ("", try_catch.GetStackTrace()); | 139 EXPECT_EQ("", try_catch.GetStackTrace()); |
| 172 | 140 |
| 173 EXPECT_EQ(191, obj->value()); | 141 EXPECT_EQ(191, obj->value()); |
| 174 } | 142 } |
| 175 | 143 |
| 176 } // namespace | 144 } // namespace |
| 177 } // namespace gin | 145 } // namespace gin |
| OLD | NEW |