7#include "fxjs/xfa/cfxjse_class.h"
12#include "fxjs/cjs_result.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"
29using pdfium::
fxjse::kClassTag;
30using pdfium::
fxjse::kFuncTag;
44void V8FunctionCallback_Wrapper(
45 const v8::FunctionCallbackInfo<v8::Value>& info) {
47 AsFunctionDescriptor(info.Data().As<v8::External>()->Value());
54void V8ConstructorCallback_Wrapper(
55 const v8::FunctionCallbackInfo<v8::Value>& info) {
56 if (!info.IsConstructCall())
60 AsClassDescriptor(info.Data().As<v8::External>()->Value());
61 if (!pClassDescriptor)
64 DCHECK_EQ(info.Holder()->InternalFieldCount(), 2);
65 info.Holder()->SetAlignedPointerInInternalField(0,
nullptr);
66 info.Holder()->SetAlignedPointerInInternalField(1,
nullptr);
69void Context_GlobalObjToString(
70 const v8::FunctionCallbackInfo<v8::Value>& info) {
72 AsClassDescriptor(info.Data().As<v8::External>()->Value());
73 if (!pClassDescriptor)
76 if (info.This() == info.Holder() && pClassDescriptor
->name) {
77 ByteString szStringVal =
79 info.GetReturnValue().Set(
80 fxv8::NewStringHelper(info.GetIsolate(), szStringVal.AsStringView()));
83 v8::Local<v8::String> local_str =
85 ->ObjectProtoToString(info.GetIsolate()->GetCurrentContext())
86 .FromMaybe(v8::Local<v8::String>());
87 info.GetReturnValue().Set(local_str);
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)
96 auto* pClassDescriptor =
static_cast<
const FXJSE_CLASS_DESCRIPTOR*>(
97 hCallBackInfo->GetAlignedPointerFromInternalField(0));
105 v8::Local<v8::String> hPropName =
106 hCallBackInfo->GetInternalField(1).As<v8::Value>().As<v8::String>();
107 if (hPropName.IsEmpty())
110 v8::String::Utf8Value szPropName(info.GetIsolate(), hPropName);
112 pClassDescriptor->dynMethodCall(info, WideString::FromUTF8(*szPropName));
115 WideString err = JSFormatErrorString(pClassDescriptor->name, *szPropName,
122 info.GetReturnValue().Set(result.Return());
125std::unique_ptr<CFXJSE_Value> DynPropGetterAdapter(
126 v8::Isolate* pIsolate,
128 v8::Local<v8::Object> pObject,
129 ByteStringView szPropName) {
136 if (pClassDescriptor->dynPropGetter) {
139 pClassDescriptor->dynPropGetter(pIsolate, pObject, szPropName));
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())
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>(
156 v8::Function::New(pIsolate->GetCurrentContext(),
157 DynPropGetterAdapter_MethodCallback, hCallBackInfo,
158 0, v8::ConstructorBehavior::kThrow)
165void DynPropSetterAdapter(v8::Isolate* pIsolate,
167 v8::Local<v8::Object> pObject,
168 ByteStringView szPropName,
170 DCHECK(pClassDescriptor);
179 pValue->GetValue(pIsolate)
);
184bool DynPropQueryAdapter(v8::Isolate* pIsolate,
186 v8::Local<v8::Object> pObject,
187 ByteStringView szPropName) {
190 pIsolate
, pObject
, szPropName
, true)
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());
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(),
208 info.GetReturnValue().Set(v8::DontDelete);
211 const int32_t iV8Absent = 64;
212 info.GetReturnValue().Set(iV8Absent);
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());
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());
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());
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,
244 info.GetReturnValue().Set(value);
247void NamedPropertyEnumeratorCallback(
248 const v8::PropertyCallbackInfo<v8::Array>& info) {
249 info.GetReturnValue().Set(v8::Array::New(info.GetIsolate()));
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
260 nullptr, NamedPropertyEnumeratorCallback,
261 v8::External::New(pIsolate,
262 const_cast<FXJSE_CLASS_DESCRIPTOR*>(pClassDescriptor)),
263 v8::PropertyHandlerFlags::kNonMasking);
264 pObjectTemplate->SetHandler(configuration);
274 if (!pContext || !pClassDescriptor)
280 return pExistingClass;
282 v8::Isolate* pIsolate = pContext->GetIsolate();
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);
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),
312 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
317 v8::Local<v8::FunctionTemplate> fn = v8::FunctionTemplate::New(
318 pIsolate, Context_GlobalObjToString,
320 pIsolate,
const_cast<FXJSE_CLASS_DESCRIPTOR*>(pClassDescriptor)));
321 fn->RemovePrototype();
322 hObjectTemplate->Set(fxv8::NewStringHelper(pIsolate,
"toString"), fn);
324 pClass->m_hTemplate.Reset(pContext->GetIsolate(), hFunctionTemplate);
326 pContext->AddClass(
std::move(pClass));
336 v8::Isolate* pIsolate) {
337 return v8::Local<v8::FunctionTemplate>::New(pIsolate, m_hTemplate);
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
static ByteString Format(const char *pFormat,...)
void ThrowExceptionHelper(v8::Isolate *pIsolate, WideStringView str)
FXJSE_PropTypeGetter dynPropTypeGetter
FXJSE_PropSetter dynPropSetter
FXJSE_MethodCallback dynMethodCall
FXJSE_FuncCallback callbackProc