| 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/per_isolate_data.h" | 9 #include "gin/per_isolate_data.h" |
| 9 #include "gin/public/isolate_holder.h" | 10 #include "gin/public/isolate_holder.h" |
| 10 #include "gin/test/v8_test.h" | 11 #include "gin/test/v8_test.h" |
| 12 #include "gin/try_catch.h" |
| 11 #include "gin/wrappable.h" | 13 #include "gin/wrappable.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 15 |
| 14 namespace gin { | 16 namespace gin { |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 class MyObject : public Wrappable { | 19 class MyObject : public Wrappable { |
| 18 public: | 20 public: |
| 19 static gin::Handle<MyObject> Create(v8::Isolate* isolate); | 21 static gin::Handle<MyObject> Create(v8::Isolate* isolate); |
| 20 | 22 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 41 return &kWrapperInfo; | 43 return &kWrapperInfo; |
| 42 } | 44 } |
| 43 | 45 |
| 44 } // namespace | 46 } // namespace |
| 45 | 47 |
| 46 template<> | 48 template<> |
| 47 struct Converter<MyObject*> : public WrappableConverter<MyObject> {}; | 49 struct Converter<MyObject*> : public WrappableConverter<MyObject> {}; |
| 48 | 50 |
| 49 namespace { | 51 namespace { |
| 50 | 52 |
| 51 // TODO(abarth): This is too much typing. | |
| 52 | |
| 53 void MyObjectGetValue(const v8::FunctionCallbackInfo<v8::Value>& info) { | |
| 54 Arguments args(info); | |
| 55 | |
| 56 MyObject* obj = 0; | |
| 57 CHECK(args.Holder(&obj)); | |
| 58 | |
| 59 args.Return(obj->value()); | |
| 60 } | |
| 61 | |
| 62 void MyObjectSetValue(const v8::FunctionCallbackInfo<v8::Value>& info) { | |
| 63 Arguments args(info); | |
| 64 | |
| 65 MyObject* obj = 0; | |
| 66 CHECK(args.Holder(&obj)); | |
| 67 | |
| 68 int val = 0; | |
| 69 if (!args.GetNext(&val)) | |
| 70 return args.ThrowError(); | |
| 71 | |
| 72 obj->set_value(val); | |
| 73 } | |
| 74 | |
| 75 void RegisterTemplate(v8::Isolate* isolate) { | 53 void RegisterTemplate(v8::Isolate* isolate) { |
| 76 PerIsolateData* data = PerIsolateData::From(isolate); | 54 PerIsolateData* data = PerIsolateData::From(isolate); |
| 77 DCHECK(data->GetObjectTemplate(&MyObject::kWrapperInfo).IsEmpty()); | 55 DCHECK(data->GetObjectTemplate(&MyObject::kWrapperInfo).IsEmpty()); |
| 78 | 56 |
| 79 v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(isolate); | 57 v8::Handle<v8::ObjectTemplate> templ = ObjectTemplateBuilder(isolate) |
| 58 .SetProperty("value", &MyObject::value, &MyObject::set_value) |
| 59 .Build(); |
| 80 templ->SetInternalFieldCount(kNumberOfInternalFields); | 60 templ->SetInternalFieldCount(kNumberOfInternalFields); |
| 81 templ->SetAccessorProperty( | |
| 82 StringToSymbol(isolate, "value"), | |
| 83 v8::FunctionTemplate::New(isolate, MyObjectGetValue), | |
| 84 v8::FunctionTemplate::New(isolate, MyObjectSetValue)); | |
| 85 | |
| 86 data->SetObjectTemplate(&MyObject::kWrapperInfo, templ); | 61 data->SetObjectTemplate(&MyObject::kWrapperInfo, templ); |
| 87 } | 62 } |
| 88 | 63 |
| 89 typedef V8Test WrappableTest; | 64 typedef V8Test WrappableTest; |
| 90 | 65 |
| 91 TEST_F(WrappableTest, WrapAndUnwrap) { | 66 TEST_F(WrappableTest, WrapAndUnwrap) { |
| 92 v8::Isolate* isolate = instance_->isolate(); | 67 v8::Isolate* isolate = instance_->isolate(); |
| 93 v8::HandleScope handle_scope(isolate); | 68 v8::HandleScope handle_scope(isolate); |
| 94 | 69 |
| 95 RegisterTemplate(isolate); | 70 RegisterTemplate(isolate); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 112 | 87 |
| 113 obj->set_value(42); | 88 obj->set_value(42); |
| 114 EXPECT_EQ(42, obj->value()); | 89 EXPECT_EQ(42, obj->value()); |
| 115 | 90 |
| 116 v8::Handle<v8::String> source = StringToV8(isolate, | 91 v8::Handle<v8::String> source = StringToV8(isolate, |
| 117 "(function (obj) {" | 92 "(function (obj) {" |
| 118 " if (obj.value !== 42) throw 'FAIL';" | 93 " if (obj.value !== 42) throw 'FAIL';" |
| 119 " else obj.value = 191; })"); | 94 " else obj.value = 191; })"); |
| 120 EXPECT_FALSE(source.IsEmpty()); | 95 EXPECT_FALSE(source.IsEmpty()); |
| 121 | 96 |
| 122 v8::TryCatch try_catch; | 97 gin::TryCatch try_catch; |
| 123 v8::Handle<v8::Script> script = v8::Script::New(source); | 98 v8::Handle<v8::Script> script = v8::Script::New(source); |
| 124 EXPECT_FALSE(script.IsEmpty()); | 99 EXPECT_FALSE(script.IsEmpty()); |
| 125 v8::Handle<v8::Value> val = script->Run(); | 100 v8::Handle<v8::Value> val = script->Run(); |
| 126 EXPECT_FALSE(val.IsEmpty()); | 101 EXPECT_FALSE(val.IsEmpty()); |
| 127 v8::Handle<v8::Function> func; | 102 v8::Handle<v8::Function> func; |
| 128 EXPECT_TRUE(ConvertFromV8(isolate, val, &func)); | 103 EXPECT_TRUE(ConvertFromV8(isolate, val, &func)); |
| 129 v8::Handle<v8::Value> argv[] = { | 104 v8::Handle<v8::Value> argv[] = { |
| 130 ConvertToV8(isolate, obj.get()), | 105 ConvertToV8(isolate, obj.get()), |
| 131 }; | 106 }; |
| 132 func->Call(v8::Undefined(isolate), 1, argv); | 107 func->Call(v8::Undefined(isolate), 1, argv); |
| 108 EXPECT_FALSE(try_catch.HasCaught()); |
| 109 EXPECT_EQ("", try_catch.GetStackTrace()); |
| 133 | 110 |
| 134 EXPECT_EQ(191, obj->value()); | 111 EXPECT_EQ(191, obj->value()); |
| 135 } | 112 } |
| 136 | 113 |
| 137 } // namespace | 114 } // namespace |
| 138 } // namespace gin | 115 } // namespace gin |
| OLD | NEW |