John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 7 | #include "JS_Runtime.h" |
| 8 | |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 9 | #include "Consts.h" |
| 10 | #include "Document.h" |
| 11 | #include "Field.h" |
| 12 | #include "Icon.h" |
| 13 | #include "JS_Context.h" |
| 14 | #include "JS_Define.h" |
| 15 | #include "JS_EventHandler.h" |
| 16 | #include "JS_GlobalData.h" |
| 17 | #include "JS_Object.h" |
| 18 | #include "JS_Value.h" |
| 19 | #include "PublicMethods.h" |
| 20 | #include "app.h" |
| 21 | #include "color.h" |
| 22 | #include "console.h" |
| 23 | #include "event.h" |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 24 | #include "fpdfsdk/include/fsdk_mgr.h" // For CPDFDoc_Environment. |
| 25 | #include "fpdfsdk/include/javascript/IJavaScript.h" |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 26 | #include "global.h" |
| 27 | #include "report.h" |
| 28 | #include "util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 29 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 30 | #ifdef PDF_ENABLE_XFA |
| 31 | #include "../../../xfa/src/fxjse/src/value.h" |
| 32 | #include "../../include/fpdfxfa/fpdfxfa_app.h" |
| 33 | #endif // PDF_ENABLE_XFA |
| 34 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 35 | // static |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 36 | void IJS_Runtime::Initialize(unsigned int slot, void* isolate) { |
| 37 | FXJS_Initialize(slot, reinterpret_cast<v8::Isolate*>(isolate)); |
| 38 | } |
| 39 | |
| 40 | // static |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 41 | IJS_Runtime* IJS_Runtime::Create(CPDFDoc_Environment* pEnv) { |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 42 | return new CJS_Runtime(pEnv); |
| 43 | } |
| 44 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 45 | // static |
| 46 | CJS_Runtime* CJS_Runtime::FromContext(const IJS_Context* cc) { |
| 47 | const CJS_Context* pContext = static_cast<const CJS_Context*>(cc); |
| 48 | return pContext->GetJSRuntime(); |
| 49 | } |
| 50 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 51 | CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) |
| 52 | : m_pApp(pApp), |
| 53 | m_pDocument(NULL), |
| 54 | m_bBlocking(FALSE), |
Jochen Eisinger | 2900784 | 2015-08-05 09:02:13 +0200 | [diff] [blame] | 55 | m_isolate(NULL), |
| 56 | m_isolateManaged(false) { |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 57 | #ifndef PDF_ENABLE_XFA |
| 58 | IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; |
| 59 | if (pPlatform->version <= 2) { |
| 60 | unsigned int embedderDataSlot = 0; |
| 61 | v8::Isolate* pExternalIsolate = nullptr; |
| 62 | if (pPlatform->version == 2) { |
| 63 | pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
| 64 | embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
| 65 | #else |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 66 | if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { |
| 67 | // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. |
| 68 | m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime(); |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 69 | } else { |
| 70 | IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; |
| 71 | if (pPlatform->version <= 2) { |
| 72 | unsigned int embedderDataSlot = 0; |
| 73 | v8::Isolate* pExternalIsolate = nullptr; |
| 74 | if (pPlatform->version == 2) { |
| 75 | pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
| 76 | embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
| 77 | } |
| 78 | FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 79 | #endif |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 80 | } |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 81 | #ifndef PDF_ENABLE_XFA |
| 82 | FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
| 83 | #else |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 84 | m_isolateManaged = FXJS_GetIsolate(&m_isolate); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 85 | } |
Tom Sepez | d2cc1b9 | 2015-04-30 15:19:03 -0700 | [diff] [blame] | 86 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 87 | v8::Isolate* isolate = m_isolate; |
| 88 | v8::Isolate::Scope isolate_scope(isolate); |
| 89 | v8::Locker locker(isolate); |
| 90 | v8::HandleScope handle_scope(isolate); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 91 | if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 92 | CJS_Context* pContext = (CJS_Context*)NewContext(); |
Tom Sepez | 4237aed | 2015-11-10 15:19:17 -0800 | [diff] [blame] | 93 | FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 94 | ReleaseContext(pContext); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 95 | return; |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 96 | #endif |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 97 | } |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 98 | #ifndef PDF_ENABLE_XFA |
| 99 | m_isolateManaged = FXJS_GetIsolate(&m_isolate); |
| 100 | #else |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 101 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 102 | #endif |
Lei Zhang | 3fa115b | 2015-10-08 12:04:47 -0700 | [diff] [blame] | 103 | if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) |
| 104 | DefineJSObjects(); |
| 105 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 106 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 107 | CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 108 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 109 | #endif |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 110 | CJS_Context* pContext = (CJS_Context*)NewContext(); |
Tom Sepez | 4237aed | 2015-11-10 15:19:17 -0800 | [diff] [blame] | 111 | FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 112 | ReleaseContext(pContext); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 115 | CJS_Runtime::~CJS_Runtime() { |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 116 | for (auto* obs : m_observers) |
| 117 | obs->OnDestroyed(); |
| 118 | |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 119 | for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 120 | delete m_ContextArray.GetAt(i); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 121 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 122 | m_ContextArray.RemoveAll(); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 123 | #ifndef PDF_ENABLE_XFA |
| 124 | FXJS_ReleaseRuntime(GetIsolate(), &m_context, &m_StaticObjects); |
| 125 | #endif |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 126 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 127 | m_pApp = NULL; |
| 128 | m_pDocument = NULL; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 129 | m_context.Reset(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 130 | |
Jochen Eisinger | 2900784 | 2015-08-05 09:02:13 +0200 | [diff] [blame] | 131 | if (m_isolateManaged) |
| 132 | m_isolate->Dispose(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 135 | void CJS_Runtime::DefineJSObjects() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 137 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 138 | v8::Locker locker(GetIsolate()); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 139 | #endif |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 140 | v8::HandleScope handle_scope(GetIsolate()); |
| 141 | v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); |
| 142 | v8::Context::Scope context_scope(context); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 143 | |
| 144 | // The call order determines the "ObjDefID" assigned to each class. |
| 145 | // ObjDefIDs 0 - 2 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 146 | CJS_Border::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 147 | CJS_Display::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 148 | CJS_Font::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 149 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 150 | // ObjDefIDs 3 - 5 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 151 | CJS_Highlight::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 152 | CJS_Position::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 153 | CJS_ScaleHow::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 154 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 155 | // ObjDefIDs 6 - 8 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 156 | CJS_ScaleWhen::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 157 | CJS_Style::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 158 | CJS_Zoomtype::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 159 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 160 | // ObjDefIDs 9 - 11 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 161 | CJS_App::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 162 | CJS_Color::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 163 | CJS_Console::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 164 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 165 | // ObjDefIDs 12 - 14 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 166 | CJS_Document::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_GLOBAL); |
| 167 | CJS_Event::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 168 | CJS_Field::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 169 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 170 | // ObjDefIDs 15 - 17 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 171 | CJS_Global::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 172 | CJS_Icon::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); |
| 173 | CJS_Util::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 174 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 175 | // ObjDefIDs 18 - 20 (these can't fail, return void). |
| 176 | CJS_PublicMethods::DefineJSObjects(GetIsolate()); |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 177 | CJS_GlobalConsts::DefineJSObjects(this); |
| 178 | CJS_GlobalArrays::DefineJSObjects(this); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 179 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 180 | // ObjDefIDs 21 - 22. |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 181 | CJS_TimerObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); |
| 182 | CJS_PrintParamsObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 185 | IJS_Context* CJS_Runtime::NewContext() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | CJS_Context* p = new CJS_Context(this); |
| 187 | m_ContextArray.Add(p); |
| 188 | return p; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 191 | void CJS_Runtime::ReleaseContext(IJS_Context* pContext) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 192 | CJS_Context* pJSContext = (CJS_Context*)pContext; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 193 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 194 | for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) { |
| 195 | if (pJSContext == m_ContextArray.GetAt(i)) { |
| 196 | delete pJSContext; |
| 197 | m_ContextArray.RemoveAt(i); |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 203 | IJS_Context* CJS_Runtime::GetCurrentContext() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 204 | if (!m_ContextArray.GetSize()) |
| 205 | return NULL; |
| 206 | return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1); |
| 207 | } |
| 208 | |
| 209 | void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { |
| 210 | if (m_pDocument != pReaderDoc) { |
| 211 | v8::Isolate::Scope isolate_scope(m_isolate); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 212 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 213 | v8::Locker locker(m_isolate); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 214 | #endif |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 215 | v8::HandleScope handle_scope(m_isolate); |
| 216 | v8::Local<v8::Context> context = |
| 217 | v8::Local<v8::Context>::New(m_isolate, m_context); |
| 218 | v8::Context::Scope context_scope(context); |
| 219 | |
| 220 | m_pDocument = pReaderDoc; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 221 | if (pReaderDoc) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 222 | v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 223 | if (!pThis.IsEmpty()) { |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 224 | if (FXJS_GetObjDefnID(pThis) == CJS_Document::g_nObjDefnID) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 225 | if (CJS_Document* pJSDocument = |
Tom Sepez | d5a0e95 | 2015-09-17 15:40:06 -0700 | [diff] [blame] | 226 | (CJS_Document*)FXJS_GetPrivate(GetIsolate(), pThis)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 227 | if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject()) |
| 228 | pDocument->AttachDoc(pReaderDoc); |
| 229 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 230 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 231 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 232 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 233 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Tom Sepez | 3342090 | 2015-10-13 15:00:10 -0700 | [diff] [blame] | 236 | int CJS_Runtime::Execute(IJS_Context* cc, |
| 237 | const wchar_t* script, |
| 238 | CFX_WideString* info) { |
| 239 | FXJSErr error = {}; |
| 240 | int nRet = FXJS_Execute(m_isolate, cc, script, &error); |
| 241 | if (nRet < 0) { |
| 242 | info->Format(L"[ Line: %05d { %s } ] : %s", error.linnum - 1, error.srcline, |
| 243 | error.message); |
| 244 | } |
| 245 | return nRet; |
| 246 | } |
| 247 | |
Tom Sepez | 5d0e843 | 2015-09-22 15:50:03 -0700 | [diff] [blame] | 248 | bool CJS_Runtime::AddEventToSet(const FieldEvent& event) { |
| 249 | return m_FieldEventSet.insert(event).second; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Tom Sepez | 5d0e843 | 2015-09-22 15:50:03 -0700 | [diff] [blame] | 252 | void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) { |
| 253 | m_FieldEventSet.erase(event); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 256 | v8::Local<v8::Context> CJS_Runtime::NewJSContext() { |
| 257 | return v8::Local<v8::Context>::New(m_isolate, m_context); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 260 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 261 | CFX_WideString ChangeObjName(const CFX_WideString& str) { |
| 262 | CFX_WideString sRet = str; |
| 263 | sRet.Replace(L"_", L"."); |
| 264 | return sRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 265 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 266 | FX_BOOL CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name, |
| 267 | FXJSE_HVALUE hValue) { |
Tom Sepez | a8a39e2 | 2015-10-12 15:47:07 -0700 | [diff] [blame] | 268 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 269 | const FX_CHAR* name = utf8Name.GetCStr(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 270 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 271 | v8::Locker lock(GetIsolate()); |
| 272 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 273 | v8::HandleScope handle_scope(GetIsolate()); |
Tom Sepez | 4f4603c | 2015-11-10 15:03:12 -0800 | [diff] [blame] | 274 | v8::Local<v8::Context> old_context = GetIsolate()->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 275 | v8::Local<v8::Context> context = |
| 276 | v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 277 | v8::Context::Scope context_scope(context); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 278 | |
Tom Sepez | 4f4603c | 2015-11-10 15:03:12 -0800 | [diff] [blame] | 279 | // Caution: We're about to hand to XFA an object that in order to invoke |
| 280 | // methods will require that the current v8::Context always has a pointer |
| 281 | // to a CJS_Runtime in its embedder data slot. Unfortunately, XFA creates |
| 282 | // its own v8::Context which has not initialized the embedder data slot. |
| 283 | // Do so now. |
| 284 | // TODO(tsepez): redesign PDF-side objects to not rely on v8::Context's |
| 285 | // embedder data slots, and/or to always use the right context. |
| 286 | FXJS_SetRuntimeForV8Context(old_context, this); |
| 287 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 288 | v8::Local<v8::Value> propvalue = |
| 289 | context->Global()->Get(v8::String::NewFromUtf8( |
| 290 | GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 291 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 292 | if (propvalue.IsEmpty()) { |
| 293 | FXJSE_Value_SetUndefined(hValue); |
| 294 | return FALSE; |
| 295 | } |
| 296 | ((CFXJSE_Value*)hValue)->ForceSetValue(propvalue); |
Tom Sepez | a8a39e2 | 2015-10-12 15:47:07 -0700 | [diff] [blame] | 297 | #endif |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 298 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 299 | return TRUE; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 300 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 301 | FX_BOOL CJS_Runtime::SetHValueByName(const CFX_ByteStringC& utf8Name, |
| 302 | FXJSE_HVALUE hValue) { |
Tom Sepez | a8a39e2 | 2015-10-12 15:47:07 -0700 | [diff] [blame] | 303 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 304 | if (utf8Name.IsEmpty() || hValue == NULL) |
| 305 | return FALSE; |
| 306 | const FX_CHAR* name = utf8Name.GetCStr(); |
| 307 | v8::Isolate* pIsolate = GetIsolate(); |
| 308 | v8::Locker lock(pIsolate); |
| 309 | v8::Isolate::Scope isolate_scope(pIsolate); |
| 310 | v8::HandleScope handle_scope(pIsolate); |
| 311 | v8::Local<v8::Context> context = |
| 312 | v8::Local<v8::Context>::New(pIsolate, m_context); |
| 313 | v8::Context::Scope context_scope(context); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 314 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 315 | // v8::Local<v8::Context> tmpCotext = |
| 316 | // v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 317 | v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New( |
| 318 | GetIsolate(), ((CFXJSE_Value*)hValue)->DirectGetValue()); |
| 319 | context->Global()->Set( |
| 320 | v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, |
| 321 | utf8Name.GetLength()), |
| 322 | propvalue); |
Tom Sepez | a8a39e2 | 2015-10-12 15:47:07 -0700 | [diff] [blame] | 323 | #endif |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 324 | return TRUE; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 325 | } |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 326 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 327 | #endif |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 328 | void CJS_Runtime::AddObserver(Observer* observer) { |
| 329 | ASSERT(m_observers.find(observer) == m_observers.end()); |
| 330 | m_observers.insert(observer); |
| 331 | } |
| 332 | |
| 333 | void CJS_Runtime::RemoveObserver(Observer* observer) { |
| 334 | ASSERT(m_observers.find(observer) != m_observers.end()); |
| 335 | m_observers.erase(observer); |
| 336 | } |