Avi Drissman | 468e51b6 | 2022-09-13 20:47:01 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Daniel Hosseinian | 68c0798d | 2021-04-16 08:16:07 | [diff] [blame] | 5 | #include"gin/interceptor.h" |
| 6 | |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 7 | #include<stdint.h> |
| 8 | |
Avi Drissman | 93a273dd | 2023-01-11 00:38:27 | [diff] [blame] | 9 | #include"base/functional/bind.h" |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 10 | #include"gin/arguments.h" |
| 11 | #include"gin/handle.h" |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 12 | #include"gin/object_template_builder.h" |
| 13 | #include"gin/per_isolate_data.h" |
| 14 | #include"gin/public/isolate_holder.h" |
| 15 | #include"gin/test/v8_test.h" |
| 16 | #include"gin/try_catch.h" |
| 17 | #include"gin/wrappable.h" |
| 18 | #include"testing/gtest/include/gtest/gtest.h" |
Dan Elphick | 05acd60 | 2021-08-30 15:22:07 | [diff] [blame] | 19 | #include"v8/include/v8-function.h" |
| 20 | #include"v8/include/v8-script.h" |
jochen@chromium.org | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 21 | #include"v8/include/v8-util.h" |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 22 | |
| 23 | namespace gin{ |
| 24 | |
Andreas Haas | 0bb6119 | 2025-07-04 12:30:04 | [diff] [blame] | 25 | classMyInterceptor |
| 26 | :publicDeprecatedWrappableWithNamedPropertyInterceptor<MyInterceptor>{ |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 27 | public: |
Daniel Hosseinian | 68c0798d | 2021-04-16 08:16:07 | [diff] [blame] | 28 | MyInterceptor(constMyInterceptor&)=delete; |
| 29 | MyInterceptor&operator=(constMyInterceptor&)=delete; |
| 30 | |
Andreas Haas | 3c15226 | 2025-07-02 12:57:48 | [diff] [blame] | 31 | staticDeprecatedWrapperInfo kWrapperInfo; |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 32 | |
| 33 | static gin::Handle<MyInterceptor>Create(v8::Isolate* isolate){ |
| 34 | returnCreateHandle(isolate,newMyInterceptor(isolate)); |
| 35 | } |
| 36 | |
Hyowon Kim | 0e7a84f | 2023-11-24 00:32:01 | [diff] [blame] | 37 | voidClear(){ |
Hyowon Kim | 0e7a84f | 2023-11-24 00:32:01 | [diff] [blame] | 38 | } |
| 39 | |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 40 | int value()const{return value_;} |
| 41 | void set_value(int value){ value_= value;} |
| 42 | |
| 43 | // gin::NamedPropertyInterceptor |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 44 | v8::Local<v8::Value>GetNamedProperty(v8::Isolate* isolate, |
| 45 | const std::string&property) override{ |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 46 | if(property=="value"){ |
| 47 | returnConvertToV8(isolate, value_); |
| 48 | }elseif(property=="func"){ |
Andreas Haas | c0715d3 | 2018-09-28 08:12:17 | [diff] [blame] | 49 | v8::Local<v8::Context> context= isolate->GetCurrentContext(); |
| 50 | returnGetFunctionTemplate(isolate,"func") |
| 51 | ->GetFunction(context) |
| 52 | .ToLocalChecked(); |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 53 | }else{ |
| 54 | return v8::Local<v8::Value>(); |
| 55 | } |
| 56 | } |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 57 | boolSetNamedProperty(v8::Isolate* isolate, |
| 58 | const std::string&property, |
| 59 | v8::Local<v8::Value> value) override{ |
jochen@chromium.org | d656f87 | 2014-08-13 17:12:55 | [diff] [blame] | 60 | if(property=="value"){ |
| 61 | ConvertFromV8(isolate, value,&value_); |
| 62 | returntrue; |
| 63 | } |
| 64 | returnfalse; |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 65 | } |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 66 | std::vector<std::string>EnumerateNamedProperties( |
anujk.sharma | e548191 | 2014-10-06 11:24:19 | [diff] [blame] | 67 | v8::Isolate* isolate) override{ |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 68 | std::vector<std::string> result; |
| 69 | result.push_back("func"); |
| 70 | result.push_back("value"); |
| 71 | return result; |
| 72 | } |
| 73 | |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 74 | private: |
| 75 | explicitMyInterceptor(v8::Isolate* isolate) |
Andreas Haas | 0bb6119 | 2025-07-04 12:30:04 | [diff] [blame] | 76 | : value_(0), template_cache_(isolate){} |
Chris Watkins | 756035a | 2017-12-01 03:03:27 | [diff] [blame] | 77 | ~MyInterceptor() override=default; |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 78 | |
Andreas Haas | 0bb6119 | 2025-07-04 12:30:04 | [diff] [blame] | 79 | // gin::Wrappable |
| 80 | gin::ObjectTemplateBuilderGetObjectTemplateBuilder( |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 81 | v8::Isolate* isolate) override{ |
Andreas Haas | 0bb6119 | 2025-07-04 12:30:04 | [diff] [blame] | 82 | returnDeprecatedWrappableWithNamedPropertyInterceptor< |
| 83 | MyInterceptor>::GetObjectTemplateBuilder(isolate) |
| 84 | .AddNamedPropertyInterceptor(); |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | intCall(int value){ |
| 88 | int tmp= value_; |
| 89 | value_= value; |
| 90 | return tmp; |
| 91 | } |
| 92 | |
jochen@chromium.org | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 93 | v8::Local<v8::FunctionTemplate>GetFunctionTemplate(v8::Isolate* isolate, |
| 94 | const std::string& name){ |
| 95 | v8::Local<v8::FunctionTemplate> function_template= |
| 96 | template_cache_.Get(name); |
| 97 | if(!function_template.IsEmpty()) |
| 98 | return function_template; |
| 99 | function_template=CreateFunctionTemplate( |
tzik | 6934a31 | 2018-03-08 01:03:16 | [diff] [blame] | 100 | isolate, base::BindRepeating(&MyInterceptor::Call), |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 101 | InvokerOptions{true,nullptr}); |
jochen@chromium.org | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 102 | template_cache_.Set(name, function_template); |
| 103 | return function_template; |
| 104 | } |
| 105 | |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 106 | int value_; |
jochen@chromium.org | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 107 | |
dcarney | 36f78b64 | 2015-04-24 10:22:49 | [diff] [blame] | 108 | v8::StdGlobalValueMap<std::string, v8::FunctionTemplate> template_cache_; |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 109 | }; |
| 110 | |
Andreas Haas | 3c15226 | 2025-07-02 12:57:48 | [diff] [blame] | 111 | DeprecatedWrapperInfoMyInterceptor::kWrapperInfo={kEmbedderNativeGin}; |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 112 | |
| 113 | classInterceptorTest:public V8Test{ |
| 114 | public: |
| 115 | voidRunInterceptorTest(const std::string& script_source){ |
| 116 | v8::Isolate* isolate= instance_->isolate(); |
| 117 | v8::HandleScope handle_scope(isolate); |
| 118 | |
| 119 | gin::Handle<MyInterceptor> obj=MyInterceptor::Create(isolate); |
| 120 | |
| 121 | obj->set_value(42); |
| 122 | EXPECT_EQ(42, obj->value()); |
| 123 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 124 | v8::Local<v8::String> source=StringToV8(isolate, script_source); |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 125 | EXPECT_FALSE(source.IsEmpty()); |
| 126 | |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 127 | gin::TryCatch try_catch(isolate); |
Adam Klein | 8a35b5b | 2018-01-17 00:51:00 | [diff] [blame] | 128 | v8::Local<v8::Script> script= |
| 129 | v8::Script::Compile(context_.Get(isolate), source).ToLocalChecked(); |
| 130 | v8::Local<v8::Value> val= |
| 131 | script->Run(context_.Get(isolate)).ToLocalChecked(); |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 132 | EXPECT_FALSE(val.IsEmpty()); |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 133 | v8::Local<v8::Function> func; |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 134 | EXPECT_TRUE(ConvertFromV8(isolate, val,&func)); |
Ken Rockot | 2b0f0765 | 2017-04-12 19:10:49 | [diff] [blame] | 135 | v8::Local<v8::Value> argv[]={ |
Jeremy Roman | eb8a2f17 | 2018-01-29 17:33:40 | [diff] [blame] | 136 | ConvertToV8(isolate, obj.get()).ToLocalChecked(), |
Ken Rockot | 2b0f0765 | 2017-04-12 19:10:49 | [diff] [blame] | 137 | }; |
Ross McIlroy | d298cced | 2018-11-23 16:14:13 | [diff] [blame] | 138 | func->Call(context_.Get(isolate), v8::Undefined(isolate),1, argv) |
| 139 | .ToLocalChecked(); |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 140 | EXPECT_FALSE(try_catch.HasCaught()); |
| 141 | EXPECT_EQ("", try_catch.GetStackTrace()); |
| 142 | |
| 143 | EXPECT_EQ(191, obj->value()); |
Hyowon Kim | 0e7a84f | 2023-11-24 00:32:01 | [diff] [blame] | 144 | obj->Clear(); |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 145 | } |
| 146 | }; |
| 147 | |
| 148 | TEST_F(InterceptorTest,NamedInterceptor){ |
| 149 | RunInterceptorTest( |
| 150 | "(function (obj) {" |
| 151 | " if (obj.value !== 42) throw 'FAIL';" |
| 152 | " else obj.value = 191; })"); |
| 153 | } |
| 154 | |
| 155 | TEST_F(InterceptorTest,NamedInterceptorCall){ |
| 156 | RunInterceptorTest( |
| 157 | "(function (obj) {" |
| 158 | " if (obj.func(191) !== 42) throw 'FAIL';" |
| 159 | " })"); |
| 160 | } |
| 161 | |
jochen@chromium.org | d656f87 | 2014-08-13 17:12:55 | [diff] [blame] | 162 | TEST_F(InterceptorTest,BypassInterceptorAllowed){ |
| 163 | RunInterceptorTest( |
| 164 | "(function (obj) {" |
| 165 | " obj.value = 191 /* make test happy */;" |
| 166 | " obj.foo = 23;" |
| 167 | " if (obj.foo !== 23) throw 'FAIL'; })"); |
| 168 | } |
| 169 | |
jochen@chromium.org | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 170 | }// namespace gin |