Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /gin /interceptor_unittest.cc
blob: 7f38b77e76f8265ea9662e244b34884b63838f37 [file] [log] [blame]
Avi Drissman468e51b62022-09-13 20:47:01[diff] [blame]1// Copyright 2014 The Chromium Authors
jochen@chromium.org5c969b82014-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 Hosseinian68c0798d2021-04-16 08:16:07[diff] [blame]5#include"gin/interceptor.h"
6
avi90e658dd2015-12-21 07:16:19[diff] [blame]7#include<stdint.h>
8
Avi Drissman93a273dd2023-01-11 00:38:27[diff] [blame]9#include"base/functional/bind.h"
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]10#include"gin/arguments.h"
11#include"gin/handle.h"
jochen@chromium.org5c969b82014-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 Elphick05acd602021-08-30 15:22:07[diff] [blame]19#include"v8/include/v8-function.h"
20#include"v8/include/v8-script.h"
jochen@chromium.orgdda52e482014-06-27 17:08:16[diff] [blame]21#include"v8/include/v8-util.h"
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]22
23namespace gin{
24
Andreas Haas0bb61192025-07-04 12:30:04[diff] [blame]25classMyInterceptor
26:publicDeprecatedWrappableWithNamedPropertyInterceptor<MyInterceptor>{
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]27public:
Daniel Hosseinian68c0798d2021-04-16 08:16:07[diff] [blame]28MyInterceptor(constMyInterceptor&)=delete;
29MyInterceptor&operator=(constMyInterceptor&)=delete;
30
Andreas Haas3c152262025-07-02 12:57:48[diff] [blame]31staticDeprecatedWrapperInfo kWrapperInfo;
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]32
33static gin::Handle<MyInterceptor>Create(v8::Isolate* isolate){
34returnCreateHandle(isolate,newMyInterceptor(isolate));
35}
36
Hyowon Kim0e7a84f2023-11-24 00:32:01[diff] [blame]37voidClear(){
Hyowon Kim0e7a84f2023-11-24 00:32:01[diff] [blame]38}
39
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]40int value()const{return value_;}
41void set_value(int value){ value_= value;}
42
43// gin::NamedPropertyInterceptor
dcheng074c0392014-10-23 19:08:25[diff] [blame]44 v8::Local<v8::Value>GetNamedProperty(v8::Isolate* isolate,
45const std::string&property) override{
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]46if(property=="value"){
47returnConvertToV8(isolate, value_);
48}elseif(property=="func"){
Andreas Haasc0715d32018-09-28 08:12:17[diff] [blame]49 v8::Local<v8::Context> context= isolate->GetCurrentContext();
50returnGetFunctionTemplate(isolate,"func")
51->GetFunction(context)
52.ToLocalChecked();
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]53}else{
54return v8::Local<v8::Value>();
55}
56}
dcheng074c0392014-10-23 19:08:25[diff] [blame]57boolSetNamedProperty(v8::Isolate* isolate,
58const std::string&property,
59 v8::Local<v8::Value> value) override{
jochen@chromium.orgd656f872014-08-13 17:12:55[diff] [blame]60if(property=="value"){
61ConvertFromV8(isolate, value,&value_);
62returntrue;
63}
64returnfalse;
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]65}
dcheng074c0392014-10-23 19:08:25[diff] [blame]66 std::vector<std::string>EnumerateNamedProperties(
anujk.sharmae5481912014-10-06 11:24:19[diff] [blame]67 v8::Isolate* isolate) override{
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]68 std::vector<std::string> result;
69 result.push_back("func");
70 result.push_back("value");
71return result;
72}
73
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]74private:
75explicitMyInterceptor(v8::Isolate* isolate)
Andreas Haas0bb61192025-07-04 12:30:04[diff] [blame]76: value_(0), template_cache_(isolate){}
Chris Watkins756035a2017-12-01 03:03:27[diff] [blame]77~MyInterceptor() override=default;
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]78
Andreas Haas0bb61192025-07-04 12:30:04[diff] [blame]79// gin::Wrappable
80 gin::ObjectTemplateBuilderGetObjectTemplateBuilder(
dcheng074c0392014-10-23 19:08:25[diff] [blame]81 v8::Isolate* isolate) override{
Andreas Haas0bb61192025-07-04 12:30:04[diff] [blame]82returnDeprecatedWrappableWithNamedPropertyInterceptor<
83MyInterceptor>::GetObjectTemplateBuilder(isolate)
84.AddNamedPropertyInterceptor();
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]85}
86
87intCall(int value){
88int tmp= value_;
89 value_= value;
90return tmp;
91}
92
jochen@chromium.orgdda52e482014-06-27 17:08:16[diff] [blame]93 v8::Local<v8::FunctionTemplate>GetFunctionTemplate(v8::Isolate* isolate,
94const std::string& name){
95 v8::Local<v8::FunctionTemplate> function_template=
96 template_cache_.Get(name);
97if(!function_template.IsEmpty())
98return function_template;
99 function_template=CreateFunctionTemplate(
tzik6934a312018-03-08 01:03:16[diff] [blame]100 isolate, base::BindRepeating(&MyInterceptor::Call),
Devlin Cronine9db9842018-04-09 17:51:05[diff] [blame]101InvokerOptions{true,nullptr});
jochen@chromium.orgdda52e482014-06-27 17:08:16[diff] [blame]102 template_cache_.Set(name, function_template);
103return function_template;
104}
105
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]106int value_;
jochen@chromium.orgdda52e482014-06-27 17:08:16[diff] [blame]107
dcarney36f78b642015-04-24 10:22:49[diff] [blame]108 v8::StdGlobalValueMap<std::string, v8::FunctionTemplate> template_cache_;
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]109};
110
Andreas Haas3c152262025-07-02 12:57:48[diff] [blame]111DeprecatedWrapperInfoMyInterceptor::kWrapperInfo={kEmbedderNativeGin};
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]112
113classInterceptorTest:public V8Test{
114public:
115voidRunInterceptorTest(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.sfaaa1b62015-04-30 07:30:48[diff] [blame]124 v8::Local<v8::String> source=StringToV8(isolate, script_source);
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]125 EXPECT_FALSE(source.IsEmpty());
126
bashidbd2ef9bb2015-06-02 01:39:32[diff] [blame]127 gin::TryCatch try_catch(isolate);
Adam Klein8a35b5b2018-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.org5c969b82014-03-12 04:59:05[diff] [blame]132 EXPECT_FALSE(val.IsEmpty());
deepak.sfaaa1b62015-04-30 07:30:48[diff] [blame]133 v8::Local<v8::Function> func;
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]134 EXPECT_TRUE(ConvertFromV8(isolate, val,&func));
Ken Rockot2b0f07652017-04-12 19:10:49[diff] [blame]135 v8::Local<v8::Value> argv[]={
Jeremy Romaneb8a2f172018-01-29 17:33:40[diff] [blame]136ConvertToV8(isolate, obj.get()).ToLocalChecked(),
Ken Rockot2b0f07652017-04-12 19:10:49[diff] [blame]137};
Ross McIlroyd298cced2018-11-23 16:14:13[diff] [blame]138 func->Call(context_.Get(isolate), v8::Undefined(isolate),1, argv)
139.ToLocalChecked();
jochen@chromium.org5c969b82014-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 Kim0e7a84f2023-11-24 00:32:01[diff] [blame]144 obj->Clear();
jochen@chromium.org5c969b82014-03-12 04:59:05[diff] [blame]145}
146};
147
148TEST_F(InterceptorTest,NamedInterceptor){
149RunInterceptorTest(
150"(function (obj) {"
151" if (obj.value !== 42) throw 'FAIL';"
152" else obj.value = 191; })");
153}
154
155TEST_F(InterceptorTest,NamedInterceptorCall){
156RunInterceptorTest(
157"(function (obj) {"
158" if (obj.func(191) !== 42) throw 'FAIL';"
159" })");
160}
161
jochen@chromium.orgd656f872014-08-13 17:12:55[diff] [blame]162TEST_F(InterceptorTest,BypassInterceptorAllowed){
163RunInterceptorTest(
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.org5c969b82014-03-12 04:59:05[diff] [blame]170}// namespace gin

[8]ページ先頭

©2009-2025 Movatter.jp