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

Side by Side Diff: gin/interceptor_unittest.cc

Issue 352223008: Add a function template cache to the interceptor unittest (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 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 | « no previous file | 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 2014 The Chromium Authors. All rights reserved. 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 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/interceptor.h" 8 #include "gin/interceptor.h"
9 #include "gin/object_template_builder.h" 9 #include "gin/object_template_builder.h"
10 #include "gin/per_isolate_data.h" 10 #include "gin/per_isolate_data.h"
11 #include "gin/public/isolate_holder.h" 11 #include "gin/public/isolate_holder.h"
12 #include "gin/test/v8_test.h" 12 #include "gin/test/v8_test.h"
13 #include "gin/try_catch.h" 13 #include "gin/try_catch.h"
14 #include "gin/wrappable.h" 14 #include "gin/wrappable.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "v8/include/v8-util.h"
16 17
17 namespace gin { 18 namespace gin {
18 19
19 class MyInterceptor : public Wrappable<MyInterceptor>, 20 class MyInterceptor : public Wrappable<MyInterceptor>,
20 public NamedPropertyInterceptor, 21 public NamedPropertyInterceptor,
21 public IndexedPropertyInterceptor { 22 public IndexedPropertyInterceptor {
22 public: 23 public:
23 static WrapperInfo kWrapperInfo; 24 static WrapperInfo kWrapperInfo;
24 25
25 static gin::Handle<MyInterceptor> Create(v8::Isolate* isolate) { 26 static gin::Handle<MyInterceptor> Create(v8::Isolate* isolate) {
26 return CreateHandle(isolate, new MyInterceptor(isolate)); 27 return CreateHandle(isolate, new MyInterceptor(isolate));
27 } 28 }
28 29
29 int value() const { return value_; } 30 int value() const { return value_; }
30 void set_value(int value) { value_ = value; } 31 void set_value(int value) { value_ = value; }
31 32
32 // gin::NamedPropertyInterceptor 33 // gin::NamedPropertyInterceptor
33 virtual v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate, 34 virtual v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate,
34 const std::string& property) 35 const std::string& property)
35 OVERRIDE { 36 OVERRIDE {
36 if (property == "value") { 37 if (property == "value") {
37 return ConvertToV8(isolate, value_); 38 return ConvertToV8(isolate, value_);
38 } else if (property == "func") { 39 } else if (property == "func") {
39 return CreateFunctionTemplate(isolate, 40 return GetFunctionTemplate(isolate, "func")->GetFunction();
40 base::Bind(&MyInterceptor::Call),
41 HolderIsFirstArgument)->GetFunction();
42 } else { 41 } else {
43 return v8::Local<v8::Value>(); 42 return v8::Local<v8::Value>();
44 } 43 }
45 } 44 }
46 virtual void SetNamedProperty(v8::Isolate* isolate, 45 virtual void SetNamedProperty(v8::Isolate* isolate,
47 const std::string& property, 46 const std::string& property,
48 v8::Local<v8::Value> value) OVERRIDE { 47 v8::Local<v8::Value> value) OVERRIDE {
49 if (property != "value") 48 if (property != "value")
50 return; 49 return;
51 ConvertFromV8(isolate, value, &value_); 50 ConvertFromV8(isolate, value, &value_);
(...skipping 24 matching lines...) Expand all
76 OVERRIDE { 75 OVERRIDE {
77 std::vector<uint32_t> result; 76 std::vector<uint32_t> result;
78 result.push_back(0); 77 result.push_back(0);
79 return result; 78 return result;
80 } 79 }
81 80
82 private: 81 private:
83 explicit MyInterceptor(v8::Isolate* isolate) 82 explicit MyInterceptor(v8::Isolate* isolate)
84 : NamedPropertyInterceptor(isolate, this), 83 : NamedPropertyInterceptor(isolate, this),
85 IndexedPropertyInterceptor(isolate, this), 84 IndexedPropertyInterceptor(isolate, this),
86 value_(0) {} 85 value_(0),
86 template_cache_(isolate) {}
87 virtual ~MyInterceptor() {} 87 virtual ~MyInterceptor() {}
88 88
89 // gin::Wrappable 89 // gin::Wrappable
90 virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) 90 virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate)
91 OVERRIDE { 91 OVERRIDE {
92 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate) 92 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate)
93 .AddNamedPropertyInterceptor() 93 .AddNamedPropertyInterceptor()
94 .AddIndexedPropertyInterceptor(); 94 .AddIndexedPropertyInterceptor();
95 } 95 }
96 96
97 int Call(int value) { 97 int Call(int value) {
98 int tmp = value_; 98 int tmp = value_;
99 value_ = value; 99 value_ = value;
100 return tmp; 100 return tmp;
101 } 101 }
102 102
103 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(v8::Isolate* isolate,
104 const std::string& name) {
105 v8::Local<v8::FunctionTemplate> function_template =
106 template_cache_.Get(name);
107 if (!function_template.IsEmpty())
108 return function_template;
109 function_template = CreateFunctionTemplate(
110 isolate, base::Bind(&MyInterceptor::Call), HolderIsFirstArgument);
111 template_cache_.Set(name, function_template);
112 return function_template;
113 }
114
103 int value_; 115 int value_;
116
117 v8::StdPersistentValueMap<std::string, v8::FunctionTemplate> template_cache_;
118
119 DISALLOW_COPY_AND_ASSIGN(MyInterceptor);
104 }; 120 };
105 121
106 WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin}; 122 WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin};
107 123
108 class InterceptorTest : public V8Test { 124 class InterceptorTest : public V8Test {
109 public: 125 public:
110 void RunInterceptorTest(const std::string& script_source) { 126 void RunInterceptorTest(const std::string& script_source) {
111 v8::Isolate* isolate = instance_->isolate(); 127 v8::Isolate* isolate = instance_->isolate();
112 v8::HandleScope handle_scope(isolate); 128 v8::HandleScope handle_scope(isolate);
113 129
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 } 166 }
151 167
152 TEST_F(InterceptorTest, IndexedInterceptor) { 168 TEST_F(InterceptorTest, IndexedInterceptor) {
153 RunInterceptorTest( 169 RunInterceptorTest(
154 "(function (obj) {" 170 "(function (obj) {"
155 " if (obj[0] !== 42) throw 'FAIL';" 171 " if (obj[0] !== 42) throw 'FAIL';"
156 " else obj[0] = 191; })"); 172 " else obj[0] = 191; })");
157 } 173 }
158 174
159 } // namespace gin 175 } // namespace gin
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698