7#include "fxjs/cjs_runtime.h"
13#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
14#include "fxjs/cfx_globaldata.h"
15#include "fxjs/cjs_annot.h"
16#include "fxjs/cjs_app.h"
17#include "fxjs/cjs_border.h"
18#include "fxjs/cjs_color.h"
19#include "fxjs/cjs_console.h"
20#include "fxjs/cjs_display.h"
21#include "fxjs/cjs_document.h"
22#include "fxjs/cjs_event.h"
23#include "fxjs/cjs_event_context.h"
24#include "fxjs/cjs_field.h"
25#include "fxjs/cjs_font.h"
26#include "fxjs/cjs_global.h"
27#include "fxjs/cjs_globalarrays.h"
28#include "fxjs/cjs_globalconsts.h"
29#include "fxjs/cjs_highlight.h"
30#include "fxjs/cjs_icon.h"
31#include "fxjs/cjs_object.h"
32#include "fxjs/cjs_position.h"
33#include "fxjs/cjs_publicmethods.h"
34#include "fxjs/cjs_scalehow.h"
35#include "fxjs/cjs_scalewhen.h"
36#include "fxjs/cjs_style.h"
37#include "fxjs/cjs_timerobj.h"
38#include "fxjs/cjs_util.h"
39#include "fxjs/cjs_zoomtype.h"
41#include "fxjs/js_define.h"
42#include "third_party/base/check_op.h"
43#include "v8/include/v8-context.h"
44#include "v8/include/v8-exception.h"
45#include "v8/include/v8-isolate.h"
47CJS_Runtime::
CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv)
49 v8::Isolate* pIsolate =
nullptr;
50 IPDF_JSPLATFORM* pPlatform = m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform;
54 unsigned int embedderDataSlot = 0;
55 v8::Isolate* pExternalIsolate =
nullptr;
57 pExternalIsolate =
static_cast<v8::Isolate*>(pPlatform
->m_isolate);
60 FXJS_Initialize(embedderDataSlot, pExternalIsolate);
62 m_isolateManaged = FXJS_GetIsolate(&pIsolate);
65 v8::Isolate::Scope isolate_scope(pIsolate);
66 v8::HandleScope handle_scope(pIsolate);
67 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0)
72 SetFormFillEnvToDocument();
82void CJS_Runtime::DefineJSObjects() {
83 v8::Isolate::Scope isolate_scope(GetIsolate());
84 v8::HandleScope handle_scope(GetIsolate());
85 v8::Local<v8::Context> context = v8::Context::New(GetIsolate());
86 v8::Context::Scope context_scope(context);
130 m_EventContextArray.push_back(std::make_unique<CJS_EventContext>(
this));
131 return m_EventContextArray.back().get();
135 DCHECK_EQ(pContext, m_EventContextArray.back().get());
136 m_EventContextArray.pop_back();
140 return m_EventContextArray.empty() ?
nullptr
141 : m_EventContextArray.back().get();
145 return m_pFormFillEnv ? m_pFormFillEnv->GetTimerHandler() :
nullptr;
148void CJS_Runtime::SetFormFillEnvToDocument() {
149 v8::Isolate::Scope isolate_scope(GetIsolate());
150 v8::HandleScope handle_scope(GetIsolate());
151 v8::Local<v8::Context> context = GetV8Context();
152 v8::Context::Scope context_scope(context);
154 v8::Local<v8::Object> pThis = GetThisObj();
158 auto pJSDocument = JSGetObject<CJS_Document>(GetIsolate(), pThis);
162 pJSDocument->SetFormFillEnv(m_pFormFillEnv.Get());
166 return m_pFormFillEnv.Get();
170 const WideString& script) {
171 return Execute(script);
175 return m_FieldEventSet.insert(event).second;
179 m_FieldEventSet.erase(event);
187 ByteStringView utf8Name) {
188 v8::Isolate::Scope isolate_scope(GetIsolate());
189 v8::Local<v8::Context> context = GetV8Context();
190 v8::Context::Scope context_scope(context);
191 v8::Local<v8::String> str = fxv8::NewStringHelper(GetIsolate(), utf8Name);
192 v8::MaybeLocal<v8::Value> maybe_value = context->Global()->Get(context, str);
193 if (maybe_value.IsEmpty())
194 return v8::Local<v8::Value>();
195 return maybe_value.ToLocalChecked();
199 v8::Local<v8::Value> pValue) {
200 if (utf8Name.IsEmpty() || pValue.IsEmpty())
203 v8::Isolate* pIsolate = GetIsolate();
204 v8::Isolate::Scope isolate_scope(pIsolate);
205 v8::Local<v8::Context> context = GetV8Context();
206 v8::Context::Scope context_scope(context);
207 v8::Local<v8::String> str = fxv8::NewStringHelper(pIsolate, utf8Name);
208 v8::Maybe<
bool> result = context->Global()->Set(context, str, pValue);
209 return result.IsJust() && result.FromJust();
212v8::Local<v8::Value> CJS_Runtime::MaybeCoerceToNumber(
213 v8::Local<v8::Value> value) {
214 bool bAllowNaN =
false;
215 if (value->IsString()) {
216 ByteString bstr = fxv8::ToByteString(GetIsolate(), value.As<v8::String>());
223 v8::TryCatch try_catch(GetIsolate());
224 v8::MaybeLocal<v8::Number> maybeNum =
225 value->ToNumber(GetIsolate()->GetCurrentContext());
226 if (maybeNum.IsEmpty())
229 v8::Local<v8::Number> num = maybeNum.ToLocalChecked();
230 if (isnan(num->Value()) && !bAllowNaN)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CJS_Runtime *pRuntmie)
static void DefineJSObjects(CJS_Runtime *pRuntime)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
v8::Local< v8::Value > GetValueByNameFromGlobalObject(ByteStringView utf8Name)
bool AddEventToSet(const FieldEvent &event)
absl::optional< IJS_Runtime::JS_Error > ExecuteScript(const WideString &script) override
CJS_Runtime(CPDFSDK_FormFillEnvironment *pFormFillEnv)
CFX_Timer::HandlerIface * GetTimerHandler() const
void ReleaseEventContext(IJS_EventContext *pContext) override
void RemoveEventFromSet(const FieldEvent &event)
CJS_Runtime * AsCJSRuntime() override
bool SetValueByNameInGlobalObject(ByteStringView utf8Name, v8::Local< v8::Value > pValue)
CJS_EventContext * GetCurrentEventContext() const
IJS_EventContext * NewEventContext() override
CPDFSDK_FormFillEnvironment * GetFormFillEnv() const override
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineJSObjects(CFXJS_Engine *pEngine)