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