xref: /aosp_15_r20/external/pdfium/fxjs/xfa/cfxjse_class.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2016 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "fxjs/xfa/cfxjse_class.h"
8 
9 #include <memory>
10 #include <utility>
11 
12 #include "fxjs/cjs_result.h"
13 #include "fxjs/fxv8.h"
14 #include "fxjs/js_resources.h"
15 #include "fxjs/xfa/cfxjse_context.h"
16 #include "fxjs/xfa/cfxjse_isolatetracker.h"
17 #include "fxjs/xfa/cfxjse_value.h"
18 #include "third_party/base/check.h"
19 #include "third_party/base/check_op.h"
20 #include "v8/include/v8-container.h"
21 #include "v8/include/v8-external.h"
22 #include "v8/include/v8-function-callback.h"
23 #include "v8/include/v8-function.h"
24 #include "v8/include/v8-isolate.h"
25 #include "v8/include/v8-object.h"
26 #include "v8/include/v8-primitive.h"
27 #include "v8/include/v8-template.h"
28 
29 using pdfium::fxjse::kClassTag;
30 using pdfium::fxjse::kFuncTag;
31 
32 namespace {
33 
AsFunctionDescriptor(void * ptr)34 FXJSE_FUNCTION_DESCRIPTOR* AsFunctionDescriptor(void* ptr) {
35   auto* result = static_cast<FXJSE_FUNCTION_DESCRIPTOR*>(ptr);
36   return result && result->tag == kFuncTag ? result : nullptr;
37 }
38 
AsClassDescriptor(void * ptr)39 FXJSE_CLASS_DESCRIPTOR* AsClassDescriptor(void* ptr) {
40   auto* result = static_cast<FXJSE_CLASS_DESCRIPTOR*>(ptr);
41   return result && result->tag == kClassTag ? result : nullptr;
42 }
43 
V8FunctionCallback_Wrapper(const v8::FunctionCallbackInfo<v8::Value> & info)44 void V8FunctionCallback_Wrapper(
45     const v8::FunctionCallbackInfo<v8::Value>& info) {
46   const FXJSE_FUNCTION_DESCRIPTOR* pFunctionInfo =
47       AsFunctionDescriptor(info.Data().As<v8::External>()->Value());
48   if (!pFunctionInfo)
49     return;
50 
51   pFunctionInfo->callbackProc(CFXJSE_HostObject::FromV8(info.Holder()), info);
52 }
53 
V8ConstructorCallback_Wrapper(const v8::FunctionCallbackInfo<v8::Value> & info)54 void V8ConstructorCallback_Wrapper(
55     const v8::FunctionCallbackInfo<v8::Value>& info) {
56   if (!info.IsConstructCall())
57     return;
58 
59   const FXJSE_CLASS_DESCRIPTOR* pClassDescriptor =
60       AsClassDescriptor(info.Data().As<v8::External>()->Value());
61   if (!pClassDescriptor)
62     return;
63 
64   DCHECK_EQ(info.Holder()->InternalFieldCount(), 2);
65   info.Holder()->SetAlignedPointerInInternalField(0, nullptr);
66   info.Holder()->SetAlignedPointerInInternalField(1, nullptr);
67 }
68 
Context_GlobalObjToString(const v8::FunctionCallbackInfo<v8::Value> & info)69 void Context_GlobalObjToString(
70     const v8::FunctionCallbackInfo<v8::Value>& info) {
71   const FXJSE_CLASS_DESCRIPTOR* pClassDescriptor =
72       AsClassDescriptor(info.Data().As<v8::External>()->Value());
73   if (!pClassDescriptor)
74     return;
75 
76   if (info.This() == info.Holder() && pClassDescriptor->name) {
77     ByteString szStringVal =
78         ByteString::Format("[object %s]", pClassDescriptor->name);
79     info.GetReturnValue().Set(
80         fxv8::NewStringHelper(info.GetIsolate(), szStringVal.AsStringView()));
81     return;
82   }
83   v8::Local<v8::String> local_str =
84       info.Holder()
85           ->ObjectProtoToString(info.GetIsolate()->GetCurrentContext())
86           .FromMaybe(v8::Local<v8::String>());
87   info.GetReturnValue().Set(local_str);
88 }
89 
DynPropGetterAdapter_MethodCallback(const v8::FunctionCallbackInfo<v8::Value> & info)90 void DynPropGetterAdapter_MethodCallback(
91     const v8::FunctionCallbackInfo<v8::Value>& info) {
92   v8::Local<v8::Object> hCallBackInfo = info.Data().As<v8::Object>();
93   if (hCallBackInfo->InternalFieldCount() != 2)
94     return;
95 
96   auto* pClassDescriptor = static_cast<const FXJSE_CLASS_DESCRIPTOR*>(
97       hCallBackInfo->GetAlignedPointerFromInternalField(0));
98   if (pClassDescriptor != &kGlobalClassDescriptor &&
99       pClassDescriptor != &kNormalClassDescriptor &&
100       pClassDescriptor != &kVariablesClassDescriptor &&
101       pClassDescriptor != &kFormCalcDescriptor) {
102     return;
103   }
104 
105   v8::Local<v8::String> hPropName =
106       hCallBackInfo->GetInternalField(1).As<v8::Value>().As<v8::String>();
107   if (hPropName.IsEmpty())
108     return;
109 
110   v8::String::Utf8Value szPropName(info.GetIsolate(), hPropName);
111   CJS_Result result =
112       pClassDescriptor->dynMethodCall(info, WideString::FromUTF8(*szPropName));
113 
114   if (result.HasError()) {
115     WideString err = JSFormatErrorString(pClassDescriptor->name, *szPropName,
116                                          result.Error());
117     fxv8::ThrowExceptionHelper(info.GetIsolate(), err.AsStringView());
118     return;
119   }
120 
121   if (result.HasReturn())
122     info.GetReturnValue().Set(result.Return());
123 }
124 
DynPropGetterAdapter(v8::Isolate * pIsolate,const FXJSE_CLASS_DESCRIPTOR * pClassDescriptor,v8::Local<v8::Object> pObject,ByteStringView szPropName,CFXJSE_Value * pValue)125 void DynPropGetterAdapter(v8::Isolate* pIsolate,
126                           const FXJSE_CLASS_DESCRIPTOR* pClassDescriptor,
127                           v8::Local<v8::Object> pObject,
128                           ByteStringView szPropName,
129                           CFXJSE_Value* pValue) {
130   DCHECK(pClassDescriptor);
131 
132   FXJSE_ClassPropType nPropType =
133       pClassDescriptor->dynPropTypeGetter
134           ? pClassDescriptor->dynPropTypeGetter(pIsolate, pObject, szPropName,
135                                                 false)
136           : FXJSE_ClassPropType::kProperty;
137   if (nPropType == FXJSE_ClassPropType::kProperty) {
138     if (pClassDescriptor->dynPropGetter) {
139       pValue->ForceSetValue(pIsolate, pClassDescriptor->dynPropGetter(
140                                           pIsolate, pObject, szPropName));
141     }
142   } else if (nPropType == FXJSE_ClassPropType::kMethod) {
143     if (pClassDescriptor->dynMethodCall && pValue) {
144       v8::HandleScope hscope(pIsolate);
145       v8::Local<v8::ObjectTemplate> hCallBackInfoTemplate =
146           v8::ObjectTemplate::New(pIsolate);
147       hCallBackInfoTemplate->SetInternalFieldCount(2);
148       v8::Local<v8::Object> hCallBackInfo =
149           hCallBackInfoTemplate->NewInstance(pIsolate->GetCurrentContext())
150               .ToLocalChecked();
151       hCallBackInfo->SetAlignedPointerInInternalField(
152           0, const_cast<FXJSE_CLASS_DESCRIPTOR*>(pClassDescriptor));
153       hCallBackInfo->SetInternalField(
154           1, fxv8::NewStringHelper(pIsolate, szPropName));
155       pValue->ForceSetValue(
156           pIsolate,
157           v8::Function::New(pIsolate->GetCurrentContext(),
158                             DynPropGetterAdapter_MethodCallback, hCallBackInfo,
159                             0, v8::ConstructorBehavior::kThrow)
160               .ToLocalChecked());
161     }
162   }
163 }
164 
DynPropSetterAdapter(v8::Isolate * pIsolate,const FXJSE_CLASS_DESCRIPTOR * pClassDescriptor,v8::Local<v8::Object> pObject,ByteStringView szPropName,CFXJSE_Value * pValue)165 void DynPropSetterAdapter(v8::Isolate* pIsolate,
166                           const FXJSE_CLASS_DESCRIPTOR* pClassDescriptor,
167                           v8::Local<v8::Object> pObject,
168                           ByteStringView szPropName,
169                           CFXJSE_Value* pValue) {
170   DCHECK(pClassDescriptor);
171   FXJSE_ClassPropType nPropType =
172       pClassDescriptor->dynPropTypeGetter
173           ? pClassDescriptor->dynPropTypeGetter(pIsolate, pObject, szPropName,
174                                                 false)
175           : FXJSE_ClassPropType::kProperty;
176   if (nPropType != FXJSE_ClassPropType::kMethod) {
177     if (pClassDescriptor->dynPropSetter) {
178       pClassDescriptor->dynPropSetter(pIsolate, pObject, szPropName,
179                                       pValue->GetValue(pIsolate));
180     }
181   }
182 }
183 
DynPropQueryAdapter(v8::Isolate * pIsolate,const FXJSE_CLASS_DESCRIPTOR * pClassDescriptor,v8::Local<v8::Object> pObject,ByteStringView szPropName)184 bool DynPropQueryAdapter(v8::Isolate* pIsolate,
185                          const FXJSE_CLASS_DESCRIPTOR* pClassDescriptor,
186                          v8::Local<v8::Object> pObject,
187                          ByteStringView szPropName) {
188   FXJSE_ClassPropType nPropType = pClassDescriptor->dynPropTypeGetter
189                                       ? pClassDescriptor->dynPropTypeGetter(
190                                             pIsolate, pObject, szPropName, true)
191                                       : FXJSE_ClassPropType::kProperty;
192   return nPropType != FXJSE_ClassPropType::kNone;
193 }
194 
NamedPropertyQueryCallback(v8::Local<v8::Name> property,const v8::PropertyCallbackInfo<v8::Integer> & info)195 void NamedPropertyQueryCallback(
196     v8::Local<v8::Name> property,
197     const v8::PropertyCallbackInfo<v8::Integer>& info) {
198   const FXJSE_CLASS_DESCRIPTOR* pClass =
199       AsClassDescriptor(info.Data().As<v8::External>()->Value());
200   if (!pClass)
201     return;
202 
203   v8::HandleScope scope(info.GetIsolate());
204   v8::String::Utf8Value szPropName(info.GetIsolate(), property);
205   ByteStringView szFxPropName(*szPropName, szPropName.length());
206   if (DynPropQueryAdapter(info.GetIsolate(), pClass, info.Holder(),
207                           szFxPropName)) {
208     info.GetReturnValue().Set(v8::DontDelete);
209     return;
210   }
211   const int32_t iV8Absent = 64;
212   info.GetReturnValue().Set(iV8Absent);
213 }
214 
NamedPropertyGetterCallback(v8::Local<v8::Name> property,const v8::PropertyCallbackInfo<v8::Value> & info)215 void NamedPropertyGetterCallback(
216     v8::Local<v8::Name> property,
217     const v8::PropertyCallbackInfo<v8::Value>& info) {
218   const FXJSE_CLASS_DESCRIPTOR* pClass =
219       AsClassDescriptor(info.Data().As<v8::External>()->Value());
220   if (!pClass)
221     return;
222 
223   v8::String::Utf8Value szPropName(info.GetIsolate(), property);
224   ByteStringView szFxPropName(*szPropName, szPropName.length());
225   auto pNewValue = std::make_unique<CFXJSE_Value>();
226   DynPropGetterAdapter(info.GetIsolate(), pClass, info.Holder(), szFxPropName,
227                        pNewValue.get());
228   info.GetReturnValue().Set(pNewValue->DirectGetValue());
229 }
230 
NamedPropertySetterCallback(v8::Local<v8::Name> property,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<v8::Value> & info)231 void NamedPropertySetterCallback(
232     v8::Local<v8::Name> property,
233     v8::Local<v8::Value> value,
234     const v8::PropertyCallbackInfo<v8::Value>& info) {
235   const FXJSE_CLASS_DESCRIPTOR* pClass =
236       AsClassDescriptor(info.Data().As<v8::External>()->Value());
237   if (!pClass)
238     return;
239 
240   v8::String::Utf8Value szPropName(info.GetIsolate(), property);
241   ByteStringView szFxPropName(*szPropName, szPropName.length());
242   auto pNewValue = std::make_unique<CFXJSE_Value>(info.GetIsolate(), value);
243   DynPropSetterAdapter(info.GetIsolate(), pClass, info.Holder(), szFxPropName,
244                        pNewValue.get());
245   info.GetReturnValue().Set(value);
246 }
247 
NamedPropertyEnumeratorCallback(const v8::PropertyCallbackInfo<v8::Array> & info)248 void NamedPropertyEnumeratorCallback(
249     const v8::PropertyCallbackInfo<v8::Array>& info) {
250   info.GetReturnValue().Set(v8::Array::New(info.GetIsolate()));
251 }
252 
SetUpNamedPropHandler(v8::Isolate * pIsolate,v8::Local<v8::ObjectTemplate> pObjectTemplate,const FXJSE_CLASS_DESCRIPTOR * pClassDescriptor)253 void SetUpNamedPropHandler(v8::Isolate* pIsolate,
254                            v8::Local<v8::ObjectTemplate> pObjectTemplate,
255                            const FXJSE_CLASS_DESCRIPTOR* pClassDescriptor) {
256   v8::NamedPropertyHandlerConfiguration configuration(
257       pClassDescriptor->dynPropGetter ? NamedPropertyGetterCallback : nullptr,
258       pClassDescriptor->dynPropSetter ? NamedPropertySetterCallback : nullptr,
259       pClassDescriptor->dynPropTypeGetter ? NamedPropertyQueryCallback
260                                           : nullptr,
261       nullptr, NamedPropertyEnumeratorCallback,
262       v8::External::New(pIsolate,
263                         const_cast<FXJSE_CLASS_DESCRIPTOR*>(pClassDescriptor)),
264       v8::PropertyHandlerFlags::kNonMasking);
265   pObjectTemplate->SetHandler(configuration);
266 }
267 
268 }  // namespace
269 
270 // static
Create(CFXJSE_Context * pContext,const FXJSE_CLASS_DESCRIPTOR * pClassDescriptor,bool bIsJSGlobal)271 CFXJSE_Class* CFXJSE_Class::Create(
272     CFXJSE_Context* pContext,
273     const FXJSE_CLASS_DESCRIPTOR* pClassDescriptor,
274     bool bIsJSGlobal) {
275   if (!pContext || !pClassDescriptor)
276     return nullptr;
277 
278   CFXJSE_Class* pExistingClass =
279       pContext->GetClassByName(pClassDescriptor->name);
280   if (pExistingClass)
281     return pExistingClass;
282 
283   v8::Isolate* pIsolate = pContext->GetIsolate();
284   auto pClass = std::make_unique<CFXJSE_Class>(pContext);
285   pClass->m_szClassName = pClassDescriptor->name;
286   pClass->m_pClassDescriptor = pClassDescriptor;
287   CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
288   v8::Local<v8::FunctionTemplate> hFunctionTemplate = v8::FunctionTemplate::New(
289       pIsolate, bIsJSGlobal ? 0 : V8ConstructorCallback_Wrapper,
290       v8::External::New(pIsolate,
291                         const_cast<FXJSE_CLASS_DESCRIPTOR*>(pClassDescriptor)));
292   v8::Local<v8::String> classname =
293       fxv8::NewStringHelper(pIsolate, pClassDescriptor->name);
294   hFunctionTemplate->SetClassName(classname);
295   hFunctionTemplate->PrototypeTemplate()->Set(
296       v8::Symbol::GetToStringTag(pIsolate), classname,
297       static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontEnum));
298   hFunctionTemplate->InstanceTemplate()->SetInternalFieldCount(2);
299   v8::Local<v8::ObjectTemplate> hObjectTemplate =
300       hFunctionTemplate->InstanceTemplate();
301   SetUpNamedPropHandler(pIsolate, hObjectTemplate, pClassDescriptor);
302 
303   if (pClassDescriptor->methNum) {
304     for (int32_t i = 0; i < pClassDescriptor->methNum; i++) {
305       v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
306           pIsolate, V8FunctionCallback_Wrapper,
307           v8::External::New(pIsolate, const_cast<FXJSE_FUNCTION_DESCRIPTOR*>(
308                                           pClassDescriptor->methods + i)));
309       fun->RemovePrototype();
310       hObjectTemplate->Set(
311           fxv8::NewStringHelper(pIsolate, pClassDescriptor->methods[i].name),
312           fun,
313           static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
314     }
315   }
316 
317   if (bIsJSGlobal) {
318     v8::Local<v8::FunctionTemplate> fn = v8::FunctionTemplate::New(
319         pIsolate, Context_GlobalObjToString,
320         v8::External::New(
321             pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(pClassDescriptor)));
322     fn->RemovePrototype();
323     hObjectTemplate->Set(fxv8::NewStringHelper(pIsolate, "toString"), fn);
324   }
325   pClass->m_hTemplate.Reset(pContext->GetIsolate(), hFunctionTemplate);
326   CFXJSE_Class* pResult = pClass.get();
327   pContext->AddClass(std::move(pClass));
328   return pResult;
329 }
330 
CFXJSE_Class(const CFXJSE_Context * pContext)331 CFXJSE_Class::CFXJSE_Class(const CFXJSE_Context* pContext)
332     : m_pContext(pContext) {}
333 
334 CFXJSE_Class::~CFXJSE_Class() = default;
335 
GetTemplate(v8::Isolate * pIsolate)336 v8::Local<v8::FunctionTemplate> CFXJSE_Class::GetTemplate(
337     v8::Isolate* pIsolate) {
338   return v8::Local<v8::FunctionTemplate>::New(pIsolate, m_hTemplate);
339 }
340