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
3 // found in the LICENSE file.
5 #include "base/logging.h"
6 #include "gin/arguments.h"
7 #include "gin/handle.h"
8 #include "gin/object_template_builder.h"
9 #include "gin/per_isolate_data.h"
10 #include "gin/public/isolate_holder.h"
11 #include "gin/test/v8_test.h"
12 #include "gin/try_catch.h"
13 #include "gin/wrappable.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 BaseClass() : value_(23) {}
21 virtual ~BaseClass() {}
26 DISALLOW_COPY_AND_ASSIGN(BaseClass
);
29 class MyObject
: public BaseClass
,
30 public Wrappable
<MyObject
> {
32 static WrapperInfo kWrapperInfo
;
34 static gin::Handle
<MyObject
> Create(v8::Isolate
* isolate
) {
35 return CreateHandle(isolate
, new MyObject());
38 int value() const { return value_
; }
39 void set_value(int value
) { value_
= value
; }
42 MyObject() : value_(0) {}
43 ObjectTemplateBuilder
GetObjectTemplateBuilder(v8::Isolate
* isolate
) final
{
44 return Wrappable
<MyObject
>::GetObjectTemplateBuilder(isolate
)
45 .SetProperty("value", &MyObject::value
, &MyObject::set_value
);
47 ~MyObject() override
{}
53 class MyCallableObject
: public Wrappable
<MyCallableObject
> {
55 static WrapperInfo kWrapperInfo
;
57 static gin::Handle
<MyCallableObject
> Create(v8::Isolate
* isolate
) {
58 return CreateHandle(isolate
, new MyCallableObject());
61 int result() { return result_
; }
64 ObjectTemplateBuilder
GetObjectTemplateBuilder(v8::Isolate
* isolate
) final
{
65 return Wrappable
<MyCallableObject
>::GetObjectTemplateBuilder(isolate
)
66 .SetCallAsFunctionHandler(&MyCallableObject::Call
);
69 MyCallableObject() : result_(0) {
72 ~MyCallableObject() override
{}
74 void Call(int val1
, int val2
, int val3
, const gin::Arguments
& arguments
) {
75 if (arguments
.IsConstructCall())
76 arguments
.ThrowTypeError("Cannot be called as constructor.");
84 class MyObject2
: public Wrappable
<MyObject2
> {
86 static WrapperInfo kWrapperInfo
;
89 WrapperInfo
MyObject::kWrapperInfo
= { kEmbedderNativeGin
};
90 WrapperInfo
MyCallableObject::kWrapperInfo
= { kEmbedderNativeGin
};
91 WrapperInfo
MyObject2::kWrapperInfo
= { kEmbedderNativeGin
};
93 typedef V8Test WrappableTest
;
95 TEST_F(WrappableTest
, WrapAndUnwrap
) {
96 v8::Isolate
* isolate
= instance_
->isolate();
97 v8::HandleScope
handle_scope(isolate
);
99 Handle
<MyObject
> obj
= MyObject::Create(isolate
);
101 v8::Local
<v8::Value
> wrapper
= ConvertToV8(isolate
, obj
.get());
102 EXPECT_FALSE(wrapper
.IsEmpty());
104 MyObject
* unwrapped
= NULL
;
105 EXPECT_TRUE(ConvertFromV8(isolate
, wrapper
, &unwrapped
));
106 EXPECT_EQ(obj
.get(), unwrapped
);
109 TEST_F(WrappableTest
, UnwrapFailures
) {
110 v8::Isolate
* isolate
= instance_
->isolate();
111 v8::HandleScope
handle_scope(isolate
);
113 // Something that isn't an object.
114 v8::Local
<v8::Value
> thing
= v8::Number::New(isolate
, 42);
115 MyObject
* unwrapped
= NULL
;
116 EXPECT_FALSE(ConvertFromV8(isolate
, thing
, &unwrapped
));
117 EXPECT_FALSE(unwrapped
);
119 // An object that's not wrapping anything.
120 thing
= v8::Object::New(isolate
);
121 EXPECT_FALSE(ConvertFromV8(isolate
, thing
, &unwrapped
));
122 EXPECT_FALSE(unwrapped
);
124 // An object that's wrapping a C++ object of the wrong type.
126 thing
= ConvertToV8(isolate
, new MyObject2());
127 EXPECT_FALSE(ConvertFromV8(isolate
, thing
, &unwrapped
));
128 EXPECT_FALSE(unwrapped
);
131 TEST_F(WrappableTest
, GetAndSetProperty
) {
132 v8::Isolate
* isolate
= instance_
->isolate();
133 v8::HandleScope
handle_scope(isolate
);
135 gin::Handle
<MyObject
> obj
= MyObject::Create(isolate
);
138 EXPECT_EQ(42, obj
->value());
140 v8::Local
<v8::String
> source
= StringToV8(isolate
,
142 " if (obj.value !== 42) throw 'FAIL';"
143 " else obj.value = 191; })");
144 EXPECT_FALSE(source
.IsEmpty());
146 gin::TryCatch
try_catch(isolate
);
147 v8::Local
<v8::Script
> script
= v8::Script::Compile(source
);
148 EXPECT_FALSE(script
.IsEmpty());
149 v8::Local
<v8::Value
> val
= script
->Run();
150 EXPECT_FALSE(val
.IsEmpty());
151 v8::Local
<v8::Function
> func
;
152 EXPECT_TRUE(ConvertFromV8(isolate
, val
, &func
));
153 v8::Local
<v8::Value
> argv
[] = {
154 ConvertToV8(isolate
, obj
.get()),
156 func
->Call(v8::Undefined(isolate
), 1, argv
);
157 EXPECT_FALSE(try_catch
.HasCaught());
158 EXPECT_EQ("", try_catch
.GetStackTrace());
160 EXPECT_EQ(191, obj
->value());
163 TEST_F(WrappableTest
, CallAsFunction
) {
164 v8::Isolate
* isolate
= instance_
->isolate();
165 v8::HandleScope
handle_scope(isolate
);
167 gin::Handle
<MyCallableObject
> object(MyCallableObject::Create(isolate
));
168 EXPECT_EQ(0, object
->result());
169 v8::Local
<v8::String
> source
= StringToV8(isolate
,
173 gin::TryCatch
try_catch(isolate
);
174 v8::Local
<v8::Script
> script
= v8::Script::Compile(source
);
175 v8::Local
<v8::Value
> val
= script
->Run();
176 v8::Local
<v8::Function
> func
;
177 EXPECT_TRUE(ConvertFromV8(isolate
, val
, &func
));
178 v8::Local
<v8::Value
> argv
[] = {
179 ConvertToV8(isolate
, object
.get())
181 func
->Call(v8::Undefined(isolate
), 1, argv
);
182 EXPECT_FALSE(try_catch
.HasCaught());
183 EXPECT_EQ(42, object
->result());
186 TEST_F(WrappableTest
, CallAsConstructor
) {
187 v8::Isolate
* isolate
= instance_
->isolate();
188 v8::HandleScope
handle_scope(isolate
);
190 gin::Handle
<MyCallableObject
> object(MyCallableObject::Create(isolate
));
191 EXPECT_EQ(0, object
->result());
192 v8::Local
<v8::String
> source
= StringToV8(isolate
,
196 gin::TryCatch
try_catch(isolate
);
197 v8::Local
<v8::Script
> script
= v8::Script::Compile(source
);
198 v8::Local
<v8::Value
> val
= script
->Run();
199 v8::Local
<v8::Function
> func
;
200 EXPECT_TRUE(ConvertFromV8(isolate
, val
, &func
));
201 v8::Local
<v8::Value
> argv
[] = {
202 ConvertToV8(isolate
, object
.get())
204 func
->Call(v8::Undefined(isolate
), 1, argv
);
205 EXPECT_TRUE(try_catch
.HasCaught());