7#include "fxjs/xfa/cfxjse_class.h"
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"
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"
45void V8FunctionCallback_Wrapper(
46 const v8::FunctionCallbackInfo<v8::Value>& info) {
48 AsFunctionDescriptor(info.Data().As<v8::External>()->Value());
55void V8ConstructorCallback_Wrapper(
56 const v8::FunctionCallbackInfo<v8::Value>& info) {
57 if (!info.IsConstructCall())
61 AsClassDescriptor(info.Data().As<v8::External>()->Value());
62 if (!pClassDescriptor)
65 DCHECK_EQ(info.This()->InternalFieldCount(), 2);
66 info.This()->SetAlignedPointerInInternalField(0,
nullptr);
67 info.This()->SetAlignedPointerInInternalField(1,
nullptr);
70void Context_GlobalObjToString(
71 const v8::FunctionCallbackInfo<v8::Value>& info) {
73 AsClassDescriptor(info.Data().As<v8::External>()->Value());
74 if (!pClassDescriptor)
77 if (pClassDescriptor
->name) {
80 info.GetReturnValue().Set(
81 fxv8::NewStringHelper(info.GetIsolate(), szStringVal.AsStringView()));
84 v8::Local<v8::String> local_str =
86 ->ObjectProtoToString(info.GetIsolate()->GetCurrentContext())
87 .FromMaybe(v8::Local<v8::String>());
88 info.GetReturnValue().Set(local_str);
91void 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)
97 auto* pClassDescriptor =
static_cast<
const FXJSE_CLASS_DESCRIPTOR*>(
98 hCallBackInfo->GetAlignedPointerFromInternalField(0));
106 v8::Local<v8::String> hPropName =
107 hCallBackInfo->GetInternalField(1).As<v8::Value>().As<v8::String>();
108 if (hPropName.IsEmpty())
111 v8::String::Utf8Value szPropName(info.GetIsolate(), hPropName);
113 pClassDescriptor->dynMethodCall(info, WideString::FromUTF8(*szPropName));
116 WideString err = JSFormatErrorString(pClassDescriptor->name, *szPropName,
123 info.GetReturnValue().Set(result.Return());
126std::unique_ptr<CFXJSE_Value> DynPropGetterAdapter(
127 v8::Isolate* pIsolate,
129 v8::Local<v8::Object> pObject,
137 if (pClassDescriptor->dynPropGetter) {
140 pClassDescriptor->dynPropGetter(pIsolate, pObject, szPropName));
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())
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>(
157 v8::Function::New(pIsolate->GetCurrentContext(),
158 DynPropGetterAdapter_MethodCallback, hCallBackInfo,
159 0, v8::ConstructorBehavior::kThrow)
166void DynPropSetterAdapter(v8::Isolate* pIsolate,
168 v8::Local<v8::Object> pObject,
180 pValue->GetValue(pIsolate)
);
185bool DynPropQueryAdapter(v8::Isolate* pIsolate,
187 v8::Local<v8::Object> pObject,
191 pIsolate
, pObject
, szPropName
, true)
196v8::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());
202 return v8::Intercepted::kNo;
205 v8::HandleScope scope(info.GetIsolate());
206 v8::String::Utf8Value szPropName(info.GetIsolate(), property);
210 if (DynPropQueryAdapter(info.GetIsolate(), pClass, info.Holder(),
212 info.GetReturnValue().Set(v8::DontDelete);
213 return v8::Intercepted::kYes;
216 return v8::Intercepted::kNo;
219v8::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());
225 return v8::Intercepted::kNo;
228 v8::String::Utf8Value szPropName(info.GetIsolate(), property);
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;
238v8::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());
245 return v8::Intercepted::kNo;
248 v8::String::Utf8Value szPropName(info.GetIsolate(), property);
252 auto pNewValue = std::make_unique<CFXJSE_Value>(info.GetIsolate(), value);
253 DynPropSetterAdapter(info.GetIsolate(), pClass, info.Holder(), szFxPropName,
255 return v8::Intercepted::kYes;
258void NamedPropertyEnumeratorCallback(
259 const v8::PropertyCallbackInfo<v8::Array>& info) {
260 info.GetReturnValue().Set(v8::Array::New(info.GetIsolate()));
263void 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
271 nullptr, NamedPropertyEnumeratorCallback,
272 v8::External::New(pIsolate,
273 const_cast<FXJSE_CLASS_DESCRIPTOR*>(pClassDescriptor)),
274 v8::PropertyHandlerFlags::kNonMasking);
275 pObjectTemplate->SetHandler(configuration);
285 if (!pContext || !pClassDescriptor)
291 return pExistingClass;
293 v8::Isolate* pIsolate = pContext->GetIsolate();
295 pClass->m_szClassName = pClassDescriptor
->name;
296 pClass->m_pClassDescriptor = pClassDescriptor;
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);
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));
325 v8::Local<v8::FunctionTemplate> fn = v8::FunctionTemplate::New(
326 pIsolate, Context_GlobalObjToString,
328 pIsolate,
const_cast<FXJSE_CLASS_DESCRIPTOR*>(pClassDescriptor)));
329 fn->RemovePrototype();
330 hObjectTemplate->Set(fxv8::NewStringHelper(pIsolate,
"toString"), fn);
332 pClass->m_hTemplate.Reset(pContext->GetIsolate(), hFunctionTemplate);
334 pContext->AddClass(
std::move(pClass));
344 v8::Isolate* pIsolate) {
345 return v8::Local<v8::FunctionTemplate>::New(pIsolate, m_hTemplate);
fxcrt::ByteString ByteString
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,...)
#define UNSAFE_BUFFERS(...)
void ThrowExceptionHelper(v8::Isolate *pIsolate, WideStringView str)
fxcrt::ByteStringView ByteStringView
FXJSE_PropTypeGetter dynPropTypeGetter
FXJSE_PropSetter dynPropSetter
FXJSE_MethodCallback dynMethodCall
FXJSE_FuncCallback callbackProc
fxcrt::WideString WideString