1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
gin / wrappable_unittest.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gin/wrappable.h"
#include "base/check.h"
#include "gin/arguments.h"
#include "gin/handle.h"
#include "gin/object_template_builder.h"
#include "gin/per_isolate_data.h"
#include "gin/public/isolate_holder.h"
#include "gin/test/v8_test.h"
#include "gin/try_catch.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "v8/include/v8-function.h"
#include "v8/include/v8-message.h"
#include "v8/include/v8-script.h"
namespace gin {
namespace {
// A non-member function to be bound to an ObjectTemplateBuilder.
void NonMemberMethod() {}
// This useless base class ensures that the value of a pointer to a MyObject
// (below) is not the same as the value of that pointer cast to the object's
// WrappableBase base.
class BaseClass {
public:
BaseClass() : value_(23) {}
BaseClass(const BaseClass&) = delete;
BaseClass& operator=(const BaseClass&) = delete;
virtual ~BaseClass() = default;
// So the compiler doesn't complain that |value_| is unused.
int value() const { return value_; }
private:
int value_;
};
class MyObject : public BaseClass,
public Wrappable<MyObject> {
public:
MyObject(const MyObject&) = delete;
MyObject& operator=(const MyObject&) = delete;
static WrapperInfo kWrapperInfo;
static gin::Handle<MyObject> Create(v8::Isolate* isolate) {
return CreateHandle(isolate, new MyObject());
}
int value() const { return value_; }
void set_value(int value) { value_ = value; }
void Method() {}
protected:
MyObject() : value_(0) {}
ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) final {
return Wrappable<MyObject>::GetObjectTemplateBuilder(isolate)
.SetProperty("value", &MyObject::value, &MyObject::set_value)
.SetMethod("memberMethod", &MyObject::Method)
.SetMethod("nonMemberMethod", &NonMemberMethod);
}
~MyObject() override = default;
private:
int value_;
};
class MyObject2 : public Wrappable<MyObject2> {
public:
static WrapperInfo kWrapperInfo;
};
class MyNamedObject : public Wrappable<MyNamedObject> {
public:
MyNamedObject(const MyNamedObject&) = delete;
MyNamedObject& operator=(const MyNamedObject&) = delete;
static WrapperInfo kWrapperInfo;
static gin::Handle<MyNamedObject> Create(v8::Isolate* isolate) {
return CreateHandle(isolate, new MyNamedObject());
}
void Method() {}
protected:
MyNamedObject() = default;
ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) final {
return Wrappable<MyNamedObject>::GetObjectTemplateBuilder(isolate)
.SetMethod("memberMethod", &MyNamedObject::Method)
.SetMethod("nonMemberMethod", &NonMemberMethod);
}
const char* GetTypeName() final { return "MyNamedObject"; }
~MyNamedObject() override = default;
};
WrapperInfo MyObject::kWrapperInfo = { kEmbedderNativeGin };
WrapperInfo MyObject2::kWrapperInfo = { kEmbedderNativeGin };
WrapperInfo MyNamedObject::kWrapperInfo = {kEmbedderNativeGin};
} // namespace
typedef V8Test WrappableTest;
TEST_F(WrappableTest, WrapAndUnwrap) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
Handle<MyObject> obj = MyObject::Create(isolate);
v8::Local<v8::Value> wrapper =
ConvertToV8(isolate, obj.get()).ToLocalChecked();
EXPECT_FALSE(wrapper.IsEmpty());
MyObject* unwrapped = NULL;
EXPECT_TRUE(ConvertFromV8(isolate, wrapper, &unwrapped));
EXPECT_EQ(obj.get(), unwrapped);
}
TEST_F(WrappableTest, UnwrapFailures) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
// Something that isn't an object.
v8::Local<v8::Value> thing = v8::Number::New(isolate, 42);
MyObject* unwrapped = NULL;
EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped));
EXPECT_FALSE(unwrapped);
// An object that's not wrapping anything.
thing = v8::Object::New(isolate);
EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped));
EXPECT_FALSE(unwrapped);
// An object that's wrapping a C++ object of the wrong type.
thing.Clear();
thing = ConvertToV8(isolate, new MyObject2()).ToLocalChecked();
EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped));
EXPECT_FALSE(unwrapped);
}
TEST_F(WrappableTest, GetAndSetProperty) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
gin::Handle<MyObject> obj = MyObject::Create(isolate);
obj->set_value(42);
EXPECT_EQ(42, obj->value());
v8::Local<v8::String> source = StringToV8(isolate,
"(function (obj) {"
" if (obj.value !== 42) throw 'FAIL';"
" else obj.value = 191; })");
EXPECT_FALSE(source.IsEmpty());
gin::TryCatch try_catch(isolate);
v8::Local<v8::Script> script =
v8::Script::Compile(context_.Get(isolate), source).ToLocalChecked();
v8::Local<v8::Value> val =
script->Run(context_.Get(isolate)).ToLocalChecked();
v8::Local<v8::Function> func;
EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
v8::Local<v8::Value> argv[] = {
ConvertToV8(isolate, obj.get()).ToLocalChecked(),
};
func->Call(context_.Get(isolate), v8::Undefined(isolate), 1, argv)
.ToLocalChecked();
EXPECT_FALSE(try_catch.HasCaught());
EXPECT_EQ("", try_catch.GetStackTrace());
EXPECT_EQ(191, obj->value());
}
TEST_F(WrappableTest, MethodInvocationErrorsOnUnnamedObject) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = context_.Get(isolate);
gin::Handle<MyObject> obj = MyObject::Create(isolate);
v8::Local<v8::Object> v8_object =
ConvertToV8(isolate, obj.get()).ToLocalChecked().As<v8::Object>();
v8::Local<v8::Value> member_method =
v8_object->Get(context, StringToV8(isolate, "memberMethod"))
.ToLocalChecked();
ASSERT_TRUE(member_method->IsFunction());
v8::Local<v8::Value> non_member_method =
v8_object->Get(context, StringToV8(isolate, "nonMemberMethod"))
.ToLocalChecked();
ASSERT_TRUE(non_member_method->IsFunction());
auto get_error = [isolate, context](v8::Local<v8::Value> function_to_run,
v8::Local<v8::Value> context_object) {
constexpr char kScript[] =
"(function(toRun, contextObject) { toRun.apply(contextObject, []); })";
v8::Local<v8::String> source = StringToV8(isolate, kScript);
EXPECT_FALSE(source.IsEmpty());
v8::TryCatch try_catch(isolate);
v8::Local<v8::Script> script =
v8::Script::Compile(context, source).ToLocalChecked();
v8::Local<v8::Value> val = script->Run(context).ToLocalChecked();
v8::Local<v8::Function> func;
EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
v8::Local<v8::Value> argv[] = {function_to_run, context_object};
func->Call(context, v8::Undefined(isolate), std::size(argv), argv)
.FromMaybe(v8::Local<v8::Value>());
if (!try_catch.HasCaught())
return std::string();
return V8ToString(isolate, try_catch.Message()->Get());
};
EXPECT_EQ(std::string(), get_error(member_method, v8_object));
EXPECT_EQ(std::string(), get_error(non_member_method, v8_object));
EXPECT_EQ("Uncaught TypeError: Illegal invocation",
get_error(member_method, v8::Null(isolate)));
// A non-member function shouldn't throw errors for being applied on a
// null (or invalid) object.
EXPECT_EQ(std::string(), get_error(non_member_method, v8::Null(isolate)));
v8::Local<v8::Object> wrong_object = v8::Object::New(isolate);
// We should get an error for passing the wrong object.
EXPECT_EQ("Uncaught TypeError: Illegal invocation",
get_error(member_method, wrong_object));
// But again, not for a "static" method.
EXPECT_EQ(std::string(), get_error(non_member_method, v8::Null(isolate)));
}
TEST_F(WrappableTest, MethodInvocationErrorsOnNamedObject) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = context_.Get(isolate);
gin::Handle<MyNamedObject> obj = MyNamedObject::Create(isolate);
v8::Local<v8::Object> v8_object =
ConvertToV8(isolate, obj.get()).ToLocalChecked().As<v8::Object>();
v8::Local<v8::Value> member_method =
v8_object->Get(context, StringToV8(isolate, "memberMethod"))
.ToLocalChecked();
ASSERT_TRUE(member_method->IsFunction());
v8::Local<v8::Value> non_member_method =
v8_object->Get(context, StringToV8(isolate, "nonMemberMethod"))
.ToLocalChecked();
ASSERT_TRUE(non_member_method->IsFunction());
auto get_error = [isolate, context](v8::Local<v8::Value> function_to_run,
v8::Local<v8::Value> context_object) {
constexpr char kScript[] =
"(function(toRun, contextObject) { toRun.apply(contextObject, []); })";
v8::Local<v8::String> source = StringToV8(isolate, kScript);
EXPECT_FALSE(source.IsEmpty());
v8::TryCatch try_catch(isolate);
v8::Local<v8::Script> script =
v8::Script::Compile(context, source).ToLocalChecked();
v8::Local<v8::Value> val = script->Run(context).ToLocalChecked();
v8::Local<v8::Function> func;
EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
v8::Local<v8::Value> argv[] = {function_to_run, context_object};
func->Call(context, v8::Undefined(isolate), std::size(argv), argv)
.FromMaybe(v8::Local<v8::Value>());
if (!try_catch.HasCaught())
return std::string();
return V8ToString(isolate, try_catch.Message()->Get());
};
EXPECT_EQ(std::string(), get_error(member_method, v8_object));
EXPECT_EQ(std::string(), get_error(non_member_method, v8_object));
EXPECT_EQ(
"Uncaught TypeError: Illegal invocation: Function must be called on "
"an object of type MyNamedObject",
get_error(member_method, v8::Null(isolate)));
// A non-member function shouldn't throw errors for being applied on a
// null (or invalid) object.
EXPECT_EQ(std::string(), get_error(non_member_method, v8::Null(isolate)));
v8::Local<v8::Object> wrong_object = v8::Object::New(isolate);
// We should get an error for passing the wrong object.
EXPECT_EQ(
"Uncaught TypeError: Illegal invocation: Function must be called on "
"an object of type MyNamedObject",
get_error(member_method, wrong_object));
// But again, not for a "static" method.
EXPECT_EQ(std::string(), get_error(non_member_method, v8::Null(isolate)));
}
class MyObjectWithLazyProperties
: public Wrappable<MyObjectWithLazyProperties> {
public:
MyObjectWithLazyProperties(const MyObjectWithLazyProperties&) = delete;
MyObjectWithLazyProperties& operator=(const MyObjectWithLazyProperties&) =
delete;
static WrapperInfo kWrapperInfo;
static gin::Handle<MyObjectWithLazyProperties> Create(v8::Isolate* isolate) {
return CreateHandle(isolate, new MyObjectWithLazyProperties());
}
int access_count() const { return access_count_; }
private:
MyObjectWithLazyProperties() = default;
ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) final {
return Wrappable::GetObjectTemplateBuilder(isolate)
.SetLazyDataProperty("fortyTwo", &MyObjectWithLazyProperties::FortyTwo)
.SetLazyDataProperty("self",
base::BindRepeating([](gin::Arguments* arguments) {
v8::Local<v8::Value> holder;
CHECK(arguments->GetHolder(&holder));
return holder;
}));
}
int FortyTwo() {
access_count_++;
return 42;
}
int access_count_ = 0;
};
WrapperInfo MyObjectWithLazyProperties::kWrapperInfo = {kEmbedderNativeGin};
TEST_F(WrappableTest, LazyPropertyGetterIsCalledOnce) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = context_.Get(isolate);
auto handle = MyObjectWithLazyProperties::Create(isolate);
v8::Local<v8::Object> v8_object = handle.ToV8().As<v8::Object>();
v8::Local<v8::String> key = StringToSymbol(isolate, "fortyTwo");
v8::Local<v8::Value> value;
bool has_own_property = false;
ASSERT_TRUE(v8_object->HasOwnProperty(context, key).To(&has_own_property));
EXPECT_TRUE(has_own_property);
EXPECT_EQ(0, handle->access_count());
ASSERT_TRUE(v8_object->Get(context, key).ToLocal(&value));
EXPECT_TRUE(value->StrictEquals(v8::Int32::New(isolate, 42)));
EXPECT_EQ(1, handle->access_count());
ASSERT_TRUE(v8_object->Get(context, key).ToLocal(&value));
EXPECT_TRUE(value->StrictEquals(v8::Int32::New(isolate, 42)));
EXPECT_EQ(1, handle->access_count());
}
TEST_F(WrappableTest, LazyPropertyGetterCanBeSetFirst) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = context_.Get(isolate);
auto handle = MyObjectWithLazyProperties::Create(isolate);
v8::Local<v8::Object> v8_object = handle.ToV8().As<v8::Object>();
v8::Local<v8::String> key = StringToSymbol(isolate, "fortyTwo");
v8::Local<v8::Value> value;
EXPECT_EQ(0, handle->access_count());
bool set_ok = false;
ASSERT_TRUE(
v8_object->Set(context, key, v8::Int32::New(isolate, 1701)).To(&set_ok));
ASSERT_TRUE(set_ok);
ASSERT_TRUE(v8_object->Get(context, key).ToLocal(&value));
EXPECT_TRUE(value->StrictEquals(v8::Int32::New(isolate, 1701)));
EXPECT_EQ(0, handle->access_count());
}
TEST_F(WrappableTest, LazyPropertyGetterCanBindSpecialArguments) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = context_.Get(isolate);
auto handle = MyObjectWithLazyProperties::Create(isolate);
v8::Local<v8::Object> v8_object = handle.ToV8().As<v8::Object>();
v8::Local<v8::Value> value;
ASSERT_TRUE(
v8_object->Get(context, StringToSymbol(isolate, "self")).ToLocal(&value));
EXPECT_TRUE(v8_object == value);
}
TEST_F(WrappableTest, CannotConstruct) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = context_.Get(isolate);
Handle<MyObject> obj = MyObject::Create(isolate);
v8::Local<v8::Value> wrapper =
ConvertToV8(isolate, obj.get()).ToLocalChecked();
ASSERT_FALSE(wrapper.IsEmpty());
v8::Local<v8::String> source =
StringToV8(isolate, "(obj => new obj.constructor())");
v8::Local<v8::Script> script =
v8::Script::Compile(context, source).ToLocalChecked();
v8::Local<v8::Function> function =
script->Run(context).ToLocalChecked().As<v8::Function>();
{
TryCatch try_catch(isolate);
EXPECT_TRUE(function
->Call(context, v8::Undefined(isolate), 1,
(v8::Local<v8::Value>[]){wrapper})
.IsEmpty());
EXPECT_TRUE(try_catch.HasCaught());
}
}
} // namespace gin