Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfxjse_class.cpp
Go to the documentation of this file.
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
29using pdfium::fxjse::kClassTag;
30using pdfium::fxjse::kFuncTag;
31
32namespace {
33
34FXJSE_FUNCTION_DESCRIPTOR* AsFunctionDescriptor(void* ptr) {
35 auto* result = static_cast<FXJSE_FUNCTION_DESCRIPTOR*>(ptr);
36 return result && result->tag == kFuncTag ? result : nullptr;
37}
38
39FXJSE_CLASS_DESCRIPTOR* AsClassDescriptor(void* ptr) {
40 auto* result = static_cast<FXJSE_CLASS_DESCRIPTOR*>(ptr);
41 return result && result->tag == kClassTag ? result : nullptr;
42}
43
44void 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
54void 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
69void 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
90void 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
125std::unique_ptr<CFXJSE_Value> DynPropGetterAdapter(
126 v8::Isolate* pIsolate,
127 const FXJSE_CLASS_DESCRIPTOR* pClassDescriptor,
128 v8::Local<v8::Object> pObject,
129 ByteStringView szPropName) {
130 FXJSE_ClassPropType nPropType =
131 pClassDescriptor->dynPropTypeGetter
132 ? pClassDescriptor->dynPropTypeGetter(pIsolate, pObject, szPropName,
133 false)
135 if (nPropType == FXJSE_ClassPropType::kProperty) {
136 if (pClassDescriptor->dynPropGetter) {
137 return std::make_unique<CFXJSE_Value>(
138 pIsolate,
139 pClassDescriptor->dynPropGetter(pIsolate, pObject, szPropName));
140 }
141 } else if (nPropType == FXJSE_ClassPropType::kMethod) {
142 if (pClassDescriptor->dynMethodCall) {
143 v8::HandleScope hscope(pIsolate);
144 v8::Local<v8::ObjectTemplate> hCallBackInfoTemplate =
145 v8::ObjectTemplate::New(pIsolate);
146 hCallBackInfoTemplate->SetInternalFieldCount(2);
147 v8::Local<v8::Object> hCallBackInfo =
148 hCallBackInfoTemplate->NewInstance(pIsolate->GetCurrentContext())
149 .ToLocalChecked();
150 hCallBackInfo->SetAlignedPointerInInternalField(
151 0, const_cast<FXJSE_CLASS_DESCRIPTOR*>(pClassDescriptor));
152 hCallBackInfo->SetInternalField(
153 1, fxv8::NewStringHelper(pIsolate, szPropName));
154 return std::make_unique<CFXJSE_Value>(
155 pIsolate,
156 v8::Function::New(pIsolate->GetCurrentContext(),
157 DynPropGetterAdapter_MethodCallback, hCallBackInfo,
158 0, v8::ConstructorBehavior::kThrow)
159 .ToLocalChecked());
160 }
161 }
162 return std::make_unique<CFXJSE_Value>();
163}
164
165void 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)
176 if (nPropType != FXJSE_ClassPropType::kMethod) {
177 if (pClassDescriptor->dynPropSetter) {
178 pClassDescriptor->dynPropSetter(pIsolate, pObject, szPropName,
179 pValue->GetValue(pIsolate));
180 }
181 }
182}
183
184bool 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)
192 return nPropType != FXJSE_ClassPropType::kNone;
193}
194
195void 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
215void 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 std::unique_ptr<CFXJSE_Value> pNewValue = DynPropGetterAdapter(
226 info.GetIsolate(), pClass, info.Holder(), szFxPropName);
227 info.GetReturnValue().Set(pNewValue->DirectGetValue());
228}
229
230void NamedPropertySetterCallback(
231 v8::Local<v8::Name> property,
232 v8::Local<v8::Value> value,
233 const v8::PropertyCallbackInfo<v8::Value>& info) {
234 const FXJSE_CLASS_DESCRIPTOR* pClass =
235 AsClassDescriptor(info.Data().As<v8::External>()->Value());
236 if (!pClass)
237 return;
238
239 v8::String::Utf8Value szPropName(info.GetIsolate(), property);
240 ByteStringView szFxPropName(*szPropName, szPropName.length());
241 auto pNewValue = std::make_unique<CFXJSE_Value>(info.GetIsolate(), value);
242 DynPropSetterAdapter(info.GetIsolate(), pClass, info.Holder(), szFxPropName,
243 pNewValue.get());
244 info.GetReturnValue().Set(value);
245}
246
247void NamedPropertyEnumeratorCallback(
248 const v8::PropertyCallbackInfo<v8::Array>& info) {
249 info.GetReturnValue().Set(v8::Array::New(info.GetIsolate()));
250}
251
252void SetUpNamedPropHandler(v8::Isolate* pIsolate,
253 v8::Local<v8::ObjectTemplate> pObjectTemplate,
254 const FXJSE_CLASS_DESCRIPTOR* pClassDescriptor) {
255 v8::NamedPropertyHandlerConfiguration configuration(
256 pClassDescriptor->dynPropGetter ? NamedPropertyGetterCallback : nullptr,
257 pClassDescriptor->dynPropSetter ? NamedPropertySetterCallback : nullptr,
258 pClassDescriptor->dynPropTypeGetter ? NamedPropertyQueryCallback
259 : nullptr,
260 nullptr, NamedPropertyEnumeratorCallback,
261 v8::External::New(pIsolate,
262 const_cast<FXJSE_CLASS_DESCRIPTOR*>(pClassDescriptor)),
263 v8::PropertyHandlerFlags::kNonMasking);
264 pObjectTemplate->SetHandler(configuration);
265}
266
267} // namespace
268
269// static
271 CFXJSE_Context* pContext,
272 const FXJSE_CLASS_DESCRIPTOR* pClassDescriptor,
273 bool bIsJSGlobal) {
274 if (!pContext || !pClassDescriptor)
275 return nullptr;
276
277 CFXJSE_Class* pExistingClass =
278 pContext->GetClassByName(pClassDescriptor->name);
279 if (pExistingClass)
280 return pExistingClass;
281
282 v8::Isolate* pIsolate = pContext->GetIsolate();
283 auto pClass = std::make_unique<CFXJSE_Class>(pContext);
284 pClass->m_szClassName = pClassDescriptor->name;
285 pClass->m_pClassDescriptor = pClassDescriptor;
287 v8::Local<v8::FunctionTemplate> hFunctionTemplate = v8::FunctionTemplate::New(
288 pIsolate, bIsJSGlobal ? nullptr : V8ConstructorCallback_Wrapper,
289 v8::External::New(pIsolate,
290 const_cast<FXJSE_CLASS_DESCRIPTOR*>(pClassDescriptor)));
291 v8::Local<v8::String> classname =
292 fxv8::NewStringHelper(pIsolate, pClassDescriptor->name);
293 hFunctionTemplate->SetClassName(classname);
294 hFunctionTemplate->PrototypeTemplate()->Set(
295 v8::Symbol::GetToStringTag(pIsolate), classname,
296 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontEnum));
297 hFunctionTemplate->InstanceTemplate()->SetInternalFieldCount(2);
298 v8::Local<v8::ObjectTemplate> hObjectTemplate =
299 hFunctionTemplate->InstanceTemplate();
300 SetUpNamedPropHandler(pIsolate, hObjectTemplate, pClassDescriptor);
301
302 if (pClassDescriptor->methNum) {
303 for (int32_t i = 0; i < pClassDescriptor->methNum; i++) {
304 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
305 pIsolate, V8FunctionCallback_Wrapper,
306 v8::External::New(pIsolate, const_cast<FXJSE_FUNCTION_DESCRIPTOR*>(
307 pClassDescriptor->methods + i)));
308 fun->RemovePrototype();
309 hObjectTemplate->Set(
310 fxv8::NewStringHelper(pIsolate, pClassDescriptor->methods[i].name),
311 fun,
312 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
313 }
314 }
315
316 if (bIsJSGlobal) {
317 v8::Local<v8::FunctionTemplate> fn = v8::FunctionTemplate::New(
318 pIsolate, Context_GlobalObjToString,
319 v8::External::New(
320 pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(pClassDescriptor)));
321 fn->RemovePrototype();
322 hObjectTemplate->Set(fxv8::NewStringHelper(pIsolate, "toString"), fn);
323 }
324 pClass->m_hTemplate.Reset(pContext->GetIsolate(), hFunctionTemplate);
325 CFXJSE_Class* pResult = pClass.get();
326 pContext->AddClass(std::move(pClass));
327 return pResult;
328}
329
332
333CFXJSE_Class::~CFXJSE_Class() = default;
334
336 v8::Isolate* pIsolate) {
337 return v8::Local<v8::FunctionTemplate>::New(pIsolate, m_hTemplate);
338}
const FXJSE_CLASS_DESCRIPTOR kNormalClassDescriptor
const FXJSE_CLASS_DESCRIPTOR kGlobalClassDescriptor
const FXJSE_CLASS_DESCRIPTOR kVariablesClassDescriptor
const FXJSE_CLASS_DESCRIPTOR kFormCalcDescriptor
static CFXJSE_Class * Create(CFXJSE_Context *pContext, const FXJSE_CLASS_DESCRIPTOR *pClassDescriptor, bool bIsJSGlobal)
CFXJSE_Class(const CFXJSE_Context *pContext)
v8::Local< v8::FunctionTemplate > GetTemplate(v8::Isolate *pIsolate)
CFXJSE_Class * GetClassByName(ByteStringView szName) const
bool HasReturn() const
Definition cjs_result.h:44
bool HasError() const
Definition cjs_result.h:41
static ByteString Format(const char *pFormat,...)
FXJSE_ClassPropType
Definition fxjse.h:31
Definition fxv8.h:22
void ThrowExceptionHelper(v8::Isolate *pIsolate, WideStringView str)
Definition fxv8.cpp:333
const char kClassTag[]
Definition fxjse.cpp:18
const char kFuncTag[]
Definition fxjse.cpp:17
FXJSE_PropTypeGetter dynPropTypeGetter
Definition fxjse.h:84
FXJSE_PropSetter dynPropSetter
Definition fxjse.h:86
const char * tag
Definition fxjse.h:80
FXJSE_MethodCallback dynMethodCall
Definition fxjse.h:87
const char * name
Definition fxjse.h:81
FXJSE_FuncCallback callbackProc
Definition fxjse.h:76
const char * tag
Definition fxjse.h:74