Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: gin/wrappable_unittest.cc

Issue 192693002: gin: Add ability to install call-as-function handlers on gin::Wrappable (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: updates Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gin/object_template_builder.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 .SetMethod("sayHello", &MyObjectSubclass::SayHello); 67 .SetMethod("sayHello", &MyObjectSubclass::SayHello);
68 } 68 }
69 69
70 MyObjectSubclass() { 70 MyObjectSubclass() {
71 } 71 }
72 72
73 virtual ~MyObjectSubclass() { 73 virtual ~MyObjectSubclass() {
74 } 74 }
75 }; 75 };
76 76
77 class MyCallableObject : public Wrappable<MyCallableObject> {
78 public:
79 static WrapperInfo kWrapperInfo;
80
81 static gin::Handle<MyCallableObject> Create(v8::Isolate* isolate) {
82 return CreateHandle(isolate, new MyCallableObject());
83 }
84
85 int result() { return result_; }
86
87 private:
88 virtual ObjectTemplateBuilder GetObjectTemplateBuilder(
89 v8::Isolate* isolate) OVERRIDE {
90 return Wrappable<MyCallableObject>::GetObjectTemplateBuilder(isolate)
91 .SetCallAsFunctionHandler(&MyCallableObject::Call);
92 }
93
94 MyCallableObject() : result_(0) {
95 }
96
97 virtual ~MyCallableObject() {
98 }
99
100 void Call(int val) {
101 result_ = val;
102 }
103
104 int result_;
105 };
106
77 class MyObject2 : public Wrappable<MyObject2> { 107 class MyObject2 : public Wrappable<MyObject2> {
78 public: 108 public:
79 static WrapperInfo kWrapperInfo; 109 static WrapperInfo kWrapperInfo;
80 }; 110 };
81 111
82 class MyObjectBlink : public Wrappable<MyObjectBlink> { 112 class MyObjectBlink : public Wrappable<MyObjectBlink> {
83 public: 113 public:
84 static WrapperInfo kWrapperInfo; 114 static WrapperInfo kWrapperInfo;
85 }; 115 };
86 116
87 WrapperInfo MyObject::kWrapperInfo = { kEmbedderNativeGin }; 117 WrapperInfo MyObject::kWrapperInfo = { kEmbedderNativeGin };
88 ObjectTemplateBuilder MyObject::GetObjectTemplateBuilder(v8::Isolate* isolate) { 118 ObjectTemplateBuilder MyObject::GetObjectTemplateBuilder(v8::Isolate* isolate) {
89 return Wrappable<MyObject>::GetObjectTemplateBuilder(isolate) 119 return Wrappable<MyObject>::GetObjectTemplateBuilder(isolate)
90 .SetProperty("value", &MyObject::value, &MyObject::set_value); 120 .SetProperty("value", &MyObject::value, &MyObject::set_value);
91 } 121 }
92 122
123 WrapperInfo MyCallableObject::kWrapperInfo = { kEmbedderNativeGin };
93 WrapperInfo MyObject2::kWrapperInfo = { kEmbedderNativeGin }; 124 WrapperInfo MyObject2::kWrapperInfo = { kEmbedderNativeGin };
94 WrapperInfo MyObjectBlink::kWrapperInfo = { kEmbedderNativeGin }; 125 WrapperInfo MyObjectBlink::kWrapperInfo = { kEmbedderNativeGin };
95 126
96 typedef V8Test WrappableTest; 127 typedef V8Test WrappableTest;
97 128
98 TEST_F(WrappableTest, WrapAndUnwrap) { 129 TEST_F(WrappableTest, WrapAndUnwrap) {
99 v8::Isolate* isolate = instance_->isolate(); 130 v8::Isolate* isolate = instance_->isolate();
100 v8::HandleScope handle_scope(isolate); 131 v8::HandleScope handle_scope(isolate);
101 132
102 Handle<MyObject> obj = MyObject::Create(isolate); 133 Handle<MyObject> obj = MyObject::Create(isolate);
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 EXPECT_FALSE(source.IsEmpty()); 237 EXPECT_FALSE(source.IsEmpty());
207 v8::Handle<v8::Script> script = v8::Script::New(source); 238 v8::Handle<v8::Script> script = v8::Script::New(source);
208 script->Run(); 239 script->Run();
209 240
210 gin::TryCatch try_catch; 241 gin::TryCatch try_catch;
211 gin::Handle<MyObject> obj = MyObject::Create(isolate); 242 gin::Handle<MyObject> obj = MyObject::Create(isolate);
212 EXPECT_TRUE(obj.IsEmpty()); 243 EXPECT_TRUE(obj.IsEmpty());
213 EXPECT_TRUE(try_catch.HasCaught()); 244 EXPECT_TRUE(try_catch.HasCaught());
214 } 245 }
215 246
247 TEST_F(WrappableTest, CallAsFunction) {
248 v8::Isolate* isolate = instance_->isolate();
249 v8::HandleScope handle_scope(isolate);
250
251 gin::Handle<MyCallableObject> object(MyCallableObject::Create(isolate));
252 EXPECT_EQ(0, object->result());
253 v8::Handle<v8::String> source = StringToV8(isolate,
254 "(function(obj) {"
255 "obj(42);"
256 "})");
257 gin::TryCatch try_catch;
258 v8::Handle<v8::Script> script = v8::Script::Compile(source);
259 v8::Handle<v8::Value> val = script->Run();
260 v8::Handle<v8::Function> func;
261 EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
262 v8::Handle<v8::Value> argv[] = {
263 ConvertToV8(isolate, object.get())
264 };
265 func->Call(v8::Undefined(isolate), 1, argv);
266 EXPECT_FALSE(try_catch.HasCaught());
267 EXPECT_EQ(42, object->result());
268 }
269
216 } // namespace gin 270 } // namespace gin
OLDNEW
« no previous file with comments | « gin/object_template_builder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698