blob: 79499a8b91fe65fecee2c9913b05107348046ea3 [file] [log] [blame]
K. Moon832a6942022-10-31 20:11:31 +00001// Copyright 2014 The PDFium Authors
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclaire0345a42017-10-30 20:20:42 +00007#include "fxjs/cjs_runtime.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Lei Zhang15fef792021-08-04 17:52:39 +00009#include <math.h>
10
tsepez41a53ad2016-03-28 16:59:30 -070011#include <algorithm>
12
Lei Zhang91085882024-02-22 00:49:57 +000013#include "core/fxcrt/check_op.h"
dsinclair735606d2016-10-05 15:47:02 -070014#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
Tom Sepez41d04e12018-10-30 22:07:36 +000015#include "fxjs/cfx_globaldata.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000016#include "fxjs/cjs_annot.h"
17#include "fxjs/cjs_app.h"
18#include "fxjs/cjs_border.h"
19#include "fxjs/cjs_color.h"
20#include "fxjs/cjs_console.h"
21#include "fxjs/cjs_display.h"
22#include "fxjs/cjs_document.h"
23#include "fxjs/cjs_event.h"
24#include "fxjs/cjs_event_context.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000025#include "fxjs/cjs_field.h"
26#include "fxjs/cjs_font.h"
27#include "fxjs/cjs_global.h"
28#include "fxjs/cjs_globalarrays.h"
29#include "fxjs/cjs_globalconsts.h"
30#include "fxjs/cjs_highlight.h"
31#include "fxjs/cjs_icon.h"
32#include "fxjs/cjs_object.h"
33#include "fxjs/cjs_position.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000034#include "fxjs/cjs_publicmethods.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000035#include "fxjs/cjs_scalehow.h"
36#include "fxjs/cjs_scalewhen.h"
37#include "fxjs/cjs_style.h"
38#include "fxjs/cjs_timerobj.h"
39#include "fxjs/cjs_util.h"
40#include "fxjs/cjs_zoomtype.h"
Tom Sepez3466e272020-03-18 23:33:35 +000041#include "fxjs/fxv8.h"
Tom Sepez221f0b32018-06-04 22:11:27 +000042#include "fxjs/js_define.h"
Dan Elphick05673a32021-09-09 15:42:55 +000043#include "v8/include/v8-context.h"
44#include "v8/include/v8-exception.h"
45#include "v8/include/v8-isolate.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046
dsinclair82e17672016-10-11 12:38:01 -070047CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv)
Lei Zhangd5f42792018-08-29 23:18:08 +000048 : m_pFormFillEnv(pFormFillEnv) {
tsepezb4694242016-08-15 16:44:55 -070049 v8::Isolate* pIsolate = nullptr;
dsinclair82e17672016-10-11 12:38:01 -070050 IPDF_JSPLATFORM* pPlatform = m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform;
Tom Sepez51da0932015-11-25 16:05:49 -080051 if (pPlatform->version <= 2) {
Tom Sepeza7858e62020-06-10 00:38:19 +000052 // Backwards compatibility - JS now initialized earlier in more modern
53 // JSPLATFORM versions.
Tom Sepez51da0932015-11-25 16:05:49 -080054 unsigned int embedderDataSlot = 0;
55 v8::Isolate* pExternalIsolate = nullptr;
56 if (pPlatform->version == 2) {
Tom Sepez9b8b2172018-04-25 22:12:34 +000057 pExternalIsolate = static_cast<v8::Isolate*>(pPlatform->m_isolate);
Tom Sepez51da0932015-11-25 16:05:49 -080058 embedderDataSlot = pPlatform->m_v8EmbedderSlot;
dsinclaird71bae02016-06-09 14:21:20 -070059 }
Tom Sepezf954d132020-06-12 22:10:03 +000060 FXJS_Initialize(embedderDataSlot, pExternalIsolate);
dsinclaird71bae02016-06-09 14:21:20 -070061 }
tsepezb4694242016-08-15 16:44:55 -070062 m_isolateManaged = FXJS_GetIsolate(&pIsolate);
63 SetIsolate(pIsolate);
Tom Sepezd2cc1b92015-04-30 15:19:03 -070064
tsepezb4694242016-08-15 16:44:55 -070065 v8::Isolate::Scope isolate_scope(pIsolate);
66 v8::HandleScope handle_scope(pIsolate);
Lei Zhang3fa115b2015-10-08 12:04:47 -070067 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0)
68 DefineJSObjects();
69
Tom Sepezc22d6712018-06-05 22:33:31 +000070 ScopedEventContext pContext(this);
tsepezb4694242016-08-15 16:44:55 -070071 InitializeEngine();
dsinclair82e17672016-10-11 12:38:01 -070072 SetFormFillEnvToDocument();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070073}
74
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075CJS_Runtime::~CJS_Runtime() {
Tom Sepezc9aa2f82018-07-02 23:08:53 +000076 NotifyObservers();
tsepezb4694242016-08-15 16:44:55 -070077 ReleaseEngine();
Tom Sepez98b356a2018-07-16 21:35:06 +000078 if (m_isolateManaged)
79 DisposeIsolate();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080}
81
Tom Sepez142165e2015-09-11 13:21:50 -070082void CJS_Runtime::DefineJSObjects() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 v8::Isolate::Scope isolate_scope(GetIsolate());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 v8::HandleScope handle_scope(GetIsolate());
85 v8::Local<v8::Context> context = v8::Context::New(GetIsolate());
86 v8::Context::Scope context_scope(context);
Tom Sepez570875c2015-09-11 08:35:03 -070087
88 // The call order determines the "ObjDefID" assigned to each class.
89 // ObjDefIDs 0 - 2
Dan Sinclairbef4d3e2017-10-26 16:49:38 -040090 CJS_Border::DefineJSObjects(this);
91 CJS_Display::DefineJSObjects(this);
92 CJS_Font::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093
Tom Sepez570875c2015-09-11 08:35:03 -070094 // ObjDefIDs 3 - 5
Dan Sinclairbef4d3e2017-10-26 16:49:38 -040095 CJS_Highlight::DefineJSObjects(this);
96 CJS_Position::DefineJSObjects(this);
97 CJS_ScaleHow::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098
Tom Sepez570875c2015-09-11 08:35:03 -070099 // ObjDefIDs 6 - 8
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400100 CJS_ScaleWhen::DefineJSObjects(this);
101 CJS_Style::DefineJSObjects(this);
102 CJS_Zoomtype::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700103
Tom Sepez570875c2015-09-11 08:35:03 -0700104 // ObjDefIDs 9 - 11
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400105 CJS_App::DefineJSObjects(this);
106 CJS_Color::DefineJSObjects(this);
107 CJS_Console::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108
Tom Sepez570875c2015-09-11 08:35:03 -0700109 // ObjDefIDs 12 - 14
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400110 CJS_Document::DefineJSObjects(this);
111 CJS_Event::DefineJSObjects(this);
112 CJS_Field::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700113
Tom Sepez570875c2015-09-11 08:35:03 -0700114 // ObjDefIDs 15 - 17
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400115 CJS_Global::DefineJSObjects(this);
116 CJS_Icon::DefineJSObjects(this);
117 CJS_Util::DefineJSObjects(this);
Tom Sepez570875c2015-09-11 08:35:03 -0700118
Tom Sepez142165e2015-09-11 13:21:50 -0700119 // ObjDefIDs 18 - 20 (these can't fail, return void).
tsepezb4694242016-08-15 16:44:55 -0700120 CJS_PublicMethods::DefineJSObjects(this);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700121 CJS_GlobalConsts::DefineJSObjects(this);
122 CJS_GlobalArrays::DefineJSObjects(this);
Tom Sepez570875c2015-09-11 08:35:03 -0700123
Tom Sepez0e5bab12018-10-18 21:39:40 +0000124 // ObjDefIDs 21 - 22.
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400125 CJS_TimerObj::DefineJSObjects(this);
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400126 CJS_Annot::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700127}
128
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800129IJS_EventContext* CJS_Runtime::NewEventContext() {
Tom Sepez31d722d2020-05-15 22:03:46 +0000130 m_EventContextArray.push_back(std::make_unique<CJS_EventContext>(this));
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800131 return m_EventContextArray.back().get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132}
133
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800134void CJS_Runtime::ReleaseEventContext(IJS_EventContext* pContext) {
Lei Zhang45829202021-04-16 16:42:11 +0000135 DCHECK_EQ(pContext, m_EventContextArray.back().get());
Tom Sepezc22d6712018-06-05 22:33:31 +0000136 m_EventContextArray.pop_back();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137}
138
Tom Sepezb1670b52017-02-16 17:01:00 -0800139CJS_EventContext* CJS_Runtime::GetCurrentEventContext() const {
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800140 return m_EventContextArray.empty() ? nullptr
141 : m_EventContextArray.back().get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142}
143
Tom Sepez8a5699d2020-10-13 21:23:53 +0000144CFX_Timer::HandlerIface* CJS_Runtime::GetTimerHandler() const {
Tom Sepez7c2d1102019-08-07 22:25:50 +0000145 return m_pFormFillEnv ? m_pFormFillEnv->GetTimerHandler() : nullptr;
Tom Sepez24374a62019-08-07 21:26:02 +0000146}
147
dsinclair82e17672016-10-11 12:38:01 -0700148void CJS_Runtime::SetFormFillEnvToDocument() {
tsepezb4694242016-08-15 16:44:55 -0700149 v8::Isolate::Scope isolate_scope(GetIsolate());
150 v8::HandleScope handle_scope(GetIsolate());
Tom Sepez1258f7f2018-02-02 17:37:37 +0000151 v8::Local<v8::Context> context = GetV8Context();
tsepezb4694242016-08-15 16:44:55 -0700152 v8::Context::Scope context_scope(context);
153
tsepezb4694242016-08-15 16:44:55 -0700154 v8::Local<v8::Object> pThis = GetThisObj();
Lei Zhangb165ffb2018-07-11 17:23:53 +0000155 if (pThis.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700156 return;
Lei Zhangb165ffb2018-07-11 17:23:53 +0000157
Daniel Hosseinianb6a909b2021-10-22 18:21:53 +0000158 auto pJSDocument = JSGetObject<CJS_Document>(GetIsolate(), pThis);
tsepezb4694242016-08-15 16:44:55 -0700159 if (!pJSDocument)
160 return;
161
Dan Sinclairf7435522018-02-05 22:27:22 +0000162 pJSDocument->SetFormFillEnv(m_pFormFillEnv.Get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700163}
164
dsinclair82e17672016-10-11 12:38:01 -0700165CPDFSDK_FormFillEnvironment* CJS_Runtime::GetFormFillEnv() const {
Tom Sepez77f6d0f2017-02-23 12:14:10 -0800166 return m_pFormFillEnv.Get();
weili625ad662016-06-15 11:21:33 -0700167}
168
Lei Zhang24c6be62024-02-08 20:06:48 +0000169std::optional<IJS_Runtime::JS_Error> CJS_Runtime::ExecuteScript(
Dan Sinclairdc5d88b2018-05-17 13:53:52 +0000170 const WideString& script) {
171 return Execute(script);
Tom Sepez33420902015-10-13 15:00:10 -0700172}
173
Tom Sepez5d0e8432015-09-22 15:50:03 -0700174bool CJS_Runtime::AddEventToSet(const FieldEvent& event) {
175 return m_FieldEventSet.insert(event).second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700176}
177
Tom Sepez5d0e8432015-09-22 15:50:03 -0700178void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) {
179 m_FieldEventSet.erase(event);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180}
181
Tom Sepez2dd06eb2018-10-29 21:16:42 +0000182CJS_Runtime* CJS_Runtime::AsCJSRuntime() {
183 return this;
184}
185
Tom Sepez6858d512021-05-26 16:59:58 +0000186v8::Local<v8::Value> CJS_Runtime::GetValueByNameFromGlobalObject(
187 ByteStringView utf8Name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 v8::Isolate::Scope isolate_scope(GetIsolate());
Tom Sepez1258f7f2018-02-02 17:37:37 +0000189 v8::Local<v8::Context> context = GetV8Context();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 v8::Context::Scope context_scope(context);
Tom Sepez3466e272020-03-18 23:33:35 +0000191 v8::Local<v8::String> str = fxv8::NewStringHelper(GetIsolate(), utf8Name);
Tom Sepez6858d512021-05-26 16:59:58 +0000192 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();
Bo Xufdc00a72014-10-28 23:03:33 -0700196}
Tom Sepez33b42e42017-07-19 13:19:12 -0700197
Tom Sepezc839ac72018-12-14 20:34:11 +0000198bool CJS_Runtime::SetValueByNameInGlobalObject(ByteStringView utf8Name,
Tom Sepezfe8d4e32019-08-15 20:15:08 +0000199 v8::Local<v8::Value> pValue) {
200 if (utf8Name.IsEmpty() || pValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700201 return false;
Tom Sepez33b42e42017-07-19 13:19:12 -0700202
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 v8::Isolate* pIsolate = GetIsolate();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 v8::Isolate::Scope isolate_scope(pIsolate);
Tom Sepez1258f7f2018-02-02 17:37:37 +0000205 v8::Local<v8::Context> context = GetV8Context();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 v8::Context::Scope context_scope(context);
Tom Sepez3466e272020-03-18 23:33:35 +0000207 v8::Local<v8::String> str = fxv8::NewStringHelper(pIsolate, utf8Name);
Tom Sepezfe8d4e32019-08-15 20:15:08 +0000208 v8::Maybe<bool> result = context->Global()->Set(context, str, pValue);
Lei Zhang83458252019-03-14 20:07:20 +0000209 return result.IsJust() && result.FromJust();
Bo Xufdc00a72014-10-28 23:03:33 -0700210}
Dan Sinclaire85107b2017-10-24 15:29:22 -0400211
212v8::Local<v8::Value> CJS_Runtime::MaybeCoerceToNumber(
dan sinclair80435cb2017-10-24 21:40:24 -0400213 v8::Local<v8::Value> value) {
Dan Sinclaire85107b2017-10-24 15:29:22 -0400214 bool bAllowNaN = false;
215 if (value->IsString()) {
Tom Sepezc573bc72022-05-19 00:01:39 +0000216 ByteString bstr = fxv8::ToByteString(GetIsolate(), value.As<v8::String>());
Lei Zhangb0a4cc42020-01-27 22:39:25 +0000217 if (bstr.IsEmpty())
Dan Sinclaire85107b2017-10-24 15:29:22 -0400218 return value;
219 if (bstr == "NaN")
220 bAllowNaN = true;
221 }
222
Tom Sepezc573bc72022-05-19 00:01:39 +0000223 v8::TryCatch try_catch(GetIsolate());
Dan Sinclaire85107b2017-10-24 15:29:22 -0400224 v8::MaybeLocal<v8::Number> maybeNum =
Tom Sepezc573bc72022-05-19 00:01:39 +0000225 value->ToNumber(GetIsolate()->GetCurrentContext());
Dan Sinclaire85107b2017-10-24 15:29:22 -0400226 if (maybeNum.IsEmpty())
227 return value;
228
229 v8::Local<v8::Number> num = maybeNum.ToLocalChecked();
Lei Zhang15fef792021-08-04 17:52:39 +0000230 if (isnan(num->Value()) && !bAllowNaN)
Dan Sinclaire85107b2017-10-24 15:29:22 -0400231 return value;
232
233 return num;
234}