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 | |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 9 | #include "../../../xfa/src/fxjse/src/value.h" |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 10 | #include "../../include/fpdfxfa/fpdfxfa_app.h" |
| 11 | #include "../../include/fsdk_mgr.h" // For CPDFDoc_Environment. |
| 12 | #include "../../include/javascript/IJavaScript.h" |
| 13 | #include "Consts.h" |
| 14 | #include "Document.h" |
| 15 | #include "Field.h" |
| 16 | #include "Icon.h" |
| 17 | #include "JS_Context.h" |
| 18 | #include "JS_Define.h" |
| 19 | #include "JS_EventHandler.h" |
| 20 | #include "JS_GlobalData.h" |
| 21 | #include "JS_Object.h" |
| 22 | #include "JS_Value.h" |
| 23 | #include "PublicMethods.h" |
| 24 | #include "app.h" |
| 25 | #include "color.h" |
| 26 | #include "console.h" |
| 27 | #include "event.h" |
| 28 | #include "global.h" |
| 29 | #include "report.h" |
| 30 | #include "util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 31 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 32 | /* ------------------------------ CJS_Runtime ------------------------------ */ |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 33 | v8::Global<v8::ObjectTemplate>& _getGlobalObjectTemplate(v8::Isolate* pIsolate); |
| 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) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 57 | if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { |
| 58 | // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. |
| 59 | m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime(); |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 60 | } else { |
| 61 | IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; |
| 62 | if (pPlatform->version <= 2) { |
| 63 | unsigned int embedderDataSlot = 0; |
| 64 | v8::Isolate* pExternalIsolate = nullptr; |
| 65 | if (pPlatform->version == 2) { |
| 66 | pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
| 67 | embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
| 68 | } |
| 69 | FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
| 70 | } |
| 71 | m_isolateManaged = FXJS_GetIsolate(&m_isolate); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 72 | } |
Tom Sepez | d2cc1b9 | 2015-04-30 15:19:03 -0700 | [diff] [blame] | 73 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 74 | v8::Isolate* isolate = m_isolate; |
| 75 | v8::Isolate::Scope isolate_scope(isolate); |
| 76 | v8::Locker locker(isolate); |
| 77 | v8::HandleScope handle_scope(isolate); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 78 | if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 79 | CJS_Context* pContext = (CJS_Context*)NewContext(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 80 | FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 81 | ReleaseContext(pContext); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
Lei Zhang | 3fa115b | 2015-10-08 12:04:47 -0700 | [diff] [blame] | 85 | if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) |
| 86 | DefineJSObjects(); |
| 87 | |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 88 | CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 89 | |
| 90 | CJS_Context* pContext = (CJS_Context*)NewContext(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 91 | FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 92 | ReleaseContext(pContext); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 95 | CJS_Runtime::~CJS_Runtime() { |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 96 | for (auto* obs : m_observers) |
| 97 | obs->OnDestroyed(); |
| 98 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 99 | int size = m_ContextArray.GetSize(); |
| 100 | for (int i = 0; i < size; i++) |
| 101 | delete m_ContextArray.GetAt(i); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 102 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 103 | m_ContextArray.RemoveAll(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 104 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 105 | m_pApp = NULL; |
| 106 | m_pDocument = NULL; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 107 | m_context.Reset(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 108 | |
Jochen Eisinger | 2900784 | 2015-08-05 09:02:13 +0200 | [diff] [blame] | 109 | if (m_isolateManaged) |
| 110 | m_isolate->Dispose(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 111 | m_isolate = NULL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 114 | void CJS_Runtime::DefineJSObjects() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 115 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 116 | v8::Locker locker(GetIsolate()); |
| 117 | v8::HandleScope handle_scope(GetIsolate()); |
| 118 | v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); |
| 119 | v8::Context::Scope context_scope(context); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 120 | |
| 121 | // The call order determines the "ObjDefID" assigned to each class. |
| 122 | // ObjDefIDs 0 - 2 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 123 | CJS_Border::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 124 | CJS_Display::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 125 | CJS_Font::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 126 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 127 | // ObjDefIDs 3 - 5 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 128 | CJS_Highlight::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 129 | CJS_Position::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 130 | CJS_ScaleHow::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 131 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 132 | // ObjDefIDs 6 - 8 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 133 | CJS_ScaleWhen::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 134 | CJS_Style::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 135 | CJS_Zoomtype::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 136 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 137 | // ObjDefIDs 9 - 11 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 138 | CJS_App::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 139 | CJS_Color::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 140 | CJS_Console::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 141 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 142 | // ObjDefIDs 12 - 14 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 143 | CJS_Document::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_GLOBAL); |
| 144 | CJS_Event::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 145 | CJS_Field::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 146 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 147 | // ObjDefIDs 15 - 17 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 148 | CJS_Global::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 149 | CJS_Icon::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); |
| 150 | CJS_Util::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 151 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 152 | // ObjDefIDs 18 - 20 (these can't fail, return void). |
| 153 | CJS_PublicMethods::DefineJSObjects(GetIsolate()); |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 154 | CJS_GlobalConsts::DefineJSObjects(this); |
| 155 | CJS_GlobalArrays::DefineJSObjects(this); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 156 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 157 | // ObjDefIDs 21 - 22. |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 158 | CJS_TimerObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); |
| 159 | CJS_PrintParamsObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 162 | IJS_Context* CJS_Runtime::NewContext() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 163 | CJS_Context* p = new CJS_Context(this); |
| 164 | m_ContextArray.Add(p); |
| 165 | return p; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 168 | void CJS_Runtime::ReleaseContext(IJS_Context* pContext) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 169 | CJS_Context* pJSContext = (CJS_Context*)pContext; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 170 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 171 | for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) { |
| 172 | if (pJSContext == m_ContextArray.GetAt(i)) { |
| 173 | delete pJSContext; |
| 174 | m_ContextArray.RemoveAt(i); |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 180 | IJS_Context* CJS_Runtime::GetCurrentContext() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 181 | if (!m_ContextArray.GetSize()) |
| 182 | return NULL; |
| 183 | return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1); |
| 184 | } |
| 185 | |
| 186 | void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { |
| 187 | if (m_pDocument != pReaderDoc) { |
| 188 | v8::Isolate::Scope isolate_scope(m_isolate); |
| 189 | v8::Locker locker(m_isolate); |
| 190 | v8::HandleScope handle_scope(m_isolate); |
| 191 | v8::Local<v8::Context> context = |
| 192 | v8::Local<v8::Context>::New(m_isolate, m_context); |
| 193 | v8::Context::Scope context_scope(context); |
| 194 | |
| 195 | m_pDocument = pReaderDoc; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | if (pReaderDoc) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 197 | v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 198 | if (!pThis.IsEmpty()) { |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 199 | if (FXJS_GetObjDefnID(pThis) == CJS_Document::g_nObjDefnID) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 200 | if (CJS_Document* pJSDocument = |
Tom Sepez | d5a0e95 | 2015-09-17 15:40:06 -0700 | [diff] [blame] | 201 | (CJS_Document*)FXJS_GetPrivate(GetIsolate(), pThis)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 202 | if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject()) |
| 203 | pDocument->AttachDoc(pReaderDoc); |
| 204 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 205 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 206 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 207 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 208 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Tom Sepez | 5d0e843 | 2015-09-22 15:50:03 -0700 | [diff] [blame] | 211 | bool CJS_Runtime::AddEventToSet(const FieldEvent& event) { |
| 212 | return m_FieldEventSet.insert(event).second; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Tom Sepez | 5d0e843 | 2015-09-22 15:50:03 -0700 | [diff] [blame] | 215 | void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) { |
| 216 | m_FieldEventSet.erase(event); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 219 | v8::Local<v8::Context> CJS_Runtime::NewJSContext() { |
| 220 | return v8::Local<v8::Context>::New(m_isolate, m_context); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 223 | CFX_WideString ChangeObjName(const CFX_WideString& str) { |
| 224 | CFX_WideString sRet = str; |
| 225 | sRet.Replace(L"_", L"."); |
| 226 | return sRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 227 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 228 | FX_BOOL CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name, |
| 229 | FXJSE_HVALUE hValue) { |
Tom Sepez | a8a39e2 | 2015-10-12 15:47:07 -0700 | [diff] [blame] | 230 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 231 | const FX_CHAR* name = utf8Name.GetCStr(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 232 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 233 | v8::Locker lock(GetIsolate()); |
| 234 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 235 | v8::HandleScope handle_scope(GetIsolate()); |
| 236 | v8::Local<v8::Context> context = |
| 237 | v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 238 | v8::Context::Scope context_scope(context); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 239 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 240 | // v8::Local<v8::Context> tmpCotext = |
| 241 | // v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 242 | v8::Local<v8::Value> propvalue = |
| 243 | context->Global()->Get(v8::String::NewFromUtf8( |
| 244 | GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 245 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 246 | if (propvalue.IsEmpty()) { |
| 247 | FXJSE_Value_SetUndefined(hValue); |
| 248 | return FALSE; |
| 249 | } |
| 250 | ((CFXJSE_Value*)hValue)->ForceSetValue(propvalue); |
Tom Sepez | a8a39e2 | 2015-10-12 15:47:07 -0700 | [diff] [blame] | 251 | #endif |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 252 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 253 | return TRUE; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 254 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 255 | FX_BOOL CJS_Runtime::SetHValueByName(const CFX_ByteStringC& utf8Name, |
| 256 | FXJSE_HVALUE hValue) { |
Tom Sepez | a8a39e2 | 2015-10-12 15:47:07 -0700 | [diff] [blame] | 257 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 258 | if (utf8Name.IsEmpty() || hValue == NULL) |
| 259 | return FALSE; |
| 260 | const FX_CHAR* name = utf8Name.GetCStr(); |
| 261 | v8::Isolate* pIsolate = GetIsolate(); |
| 262 | v8::Locker lock(pIsolate); |
| 263 | v8::Isolate::Scope isolate_scope(pIsolate); |
| 264 | v8::HandleScope handle_scope(pIsolate); |
| 265 | v8::Local<v8::Context> context = |
| 266 | v8::Local<v8::Context>::New(pIsolate, m_context); |
| 267 | v8::Context::Scope context_scope(context); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 268 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 269 | // v8::Local<v8::Context> tmpCotext = |
| 270 | // v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 271 | v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New( |
| 272 | GetIsolate(), ((CFXJSE_Value*)hValue)->DirectGetValue()); |
| 273 | context->Global()->Set( |
| 274 | v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, |
| 275 | utf8Name.GetLength()), |
| 276 | propvalue); |
Tom Sepez | a8a39e2 | 2015-10-12 15:47:07 -0700 | [diff] [blame] | 277 | #endif |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 278 | return TRUE; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 279 | } |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 280 | |
| 281 | void CJS_Runtime::AddObserver(Observer* observer) { |
| 282 | ASSERT(m_observers.find(observer) == m_observers.end()); |
| 283 | m_observers.insert(observer); |
| 284 | } |
| 285 | |
| 286 | void CJS_Runtime::RemoveObserver(Observer* observer) { |
| 287 | ASSERT(m_observers.find(observer) != m_observers.end()); |
| 288 | m_observers.erase(observer); |
| 289 | } |