1 // Copyright 2014 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/interceptor.h"
9 #include "gin/object_template_builder.h"
10 #include "gin/per_isolate_data.h"
11 #include "gin/public/isolate_holder.h"
12 #include "gin/test/v8_test.h"
13 #include "gin/try_catch.h"
14 #include "gin/wrappable.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 class MyInterceptor
: public Wrappable
<MyInterceptor
>,
20 public NamedPropertyInterceptor
,
21 public IndexedPropertyInterceptor
{
23 static WrapperInfo kWrapperInfo
;
25 static gin::Handle
<MyInterceptor
> Create(v8::Isolate
* isolate
) {
26 return CreateHandle(isolate
, new MyInterceptor(isolate
));
29 int value() const { return value_
; }
30 void set_value(int value
) { value_
= value
; }
32 // gin::NamedPropertyInterceptor
33 virtual v8::Local
<v8::Value
> GetNamedProperty(v8::Isolate
* isolate
,
34 const std::string
& property
)
36 if (property
== "value") {
37 return ConvertToV8(isolate
, value_
);
38 } else if (property
== "func") {
39 return CreateFunctionTemplate(isolate
,
40 base::Bind(&MyInterceptor::Call
),
41 HolderIsFirstArgument
)->GetFunction();
43 return v8::Local
<v8::Value
>();
46 virtual void SetNamedProperty(v8::Isolate
* isolate
,
47 const std::string
& property
,
48 v8::Local
<v8::Value
> value
) OVERRIDE
{
49 if (property
!= "value")
51 ConvertFromV8(isolate
, value
, &value_
);
53 virtual std::vector
<std::string
> EnumerateNamedProperties(
54 v8::Isolate
* isolate
) OVERRIDE
{
55 std::vector
<std::string
> result
;
56 result
.push_back("func");
57 result
.push_back("value");
61 // gin::IndexedPropertyInterceptor
62 virtual v8::Local
<v8::Value
> GetIndexedProperty(v8::Isolate
* isolate
,
63 uint32_t index
) OVERRIDE
{
65 return ConvertToV8(isolate
, value_
);
66 return v8::Local
<v8::Value
>();
68 virtual void SetIndexedProperty(v8::Isolate
* isolate
,
70 v8::Local
<v8::Value
> value
) OVERRIDE
{
73 ConvertFromV8(isolate
, value
, &value_
);
75 virtual std::vector
<uint32_t> EnumerateIndexedProperties(v8::Isolate
* isolate
)
77 std::vector
<uint32_t> result
;
83 explicit MyInterceptor(v8::Isolate
* isolate
)
84 : NamedPropertyInterceptor(isolate
, this),
85 IndexedPropertyInterceptor(isolate
, this),
87 virtual ~MyInterceptor() {}
90 virtual ObjectTemplateBuilder
GetObjectTemplateBuilder(v8::Isolate
* isolate
)
92 return Wrappable
<MyInterceptor
>::GetObjectTemplateBuilder(isolate
)
93 .AddNamedPropertyInterceptor()
94 .AddIndexedPropertyInterceptor();
106 WrapperInfo
MyInterceptor::kWrapperInfo
= {kEmbedderNativeGin
};
108 class InterceptorTest
: public V8Test
{
110 void RunInterceptorTest(const std::string
& script_source
) {
111 v8::Isolate
* isolate
= instance_
->isolate();
112 v8::HandleScope
handle_scope(isolate
);
114 gin::Handle
<MyInterceptor
> obj
= MyInterceptor::Create(isolate
);
117 EXPECT_EQ(42, obj
->value());
119 v8::Handle
<v8::String
> source
= StringToV8(isolate
, script_source
);
120 EXPECT_FALSE(source
.IsEmpty());
122 gin::TryCatch try_catch
;
123 v8::Handle
<v8::Script
> script
= v8::Script::Compile(source
);
124 EXPECT_FALSE(script
.IsEmpty());
125 v8::Handle
<v8::Value
> val
= script
->Run();
126 EXPECT_FALSE(val
.IsEmpty());
127 v8::Handle
<v8::Function
> func
;
128 EXPECT_TRUE(ConvertFromV8(isolate
, val
, &func
));
129 v8::Handle
<v8::Value
> argv
[] = {ConvertToV8(isolate
, obj
.get()), };
130 func
->Call(v8::Undefined(isolate
), 1, argv
);
131 EXPECT_FALSE(try_catch
.HasCaught());
132 EXPECT_EQ("", try_catch
.GetStackTrace());
134 EXPECT_EQ(191, obj
->value());
138 TEST_F(InterceptorTest
, NamedInterceptor
) {
141 " if (obj.value !== 42) throw 'FAIL';"
142 " else obj.value = 191; })");
145 TEST_F(InterceptorTest
, NamedInterceptorCall
) {
148 " if (obj.func(191) !== 42) throw 'FAIL';"
152 TEST_F(InterceptorTest
, IndexedInterceptor
) {
155 " if (obj[0] !== 42) throw 'FAIL';"
156 " else obj[0] = 191; })");