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