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 | |
| 7 | #include "../../include/javascript/JavaScript.h" |
| 8 | #include "../../include/javascript/IJavaScript.h" |
| 9 | #include "../../include/javascript/JS_EventHandler.h" |
| 10 | #include "../../include/javascript/JS_Runtime.h" |
| 11 | #include "../../include/javascript/JS_Context.h" |
| 12 | #include "../../include/javascript/JS_Define.h" |
| 13 | #include "../../include/javascript/JS_Object.h" |
| 14 | #include "../../include/javascript/JS_Value.h" |
| 15 | #include "../../include/javascript/Document.h" |
| 16 | #include "../../include/javascript/app.h" |
| 17 | #include "../../include/javascript/color.h" |
| 18 | #include "../../include/javascript/Consts.h" |
| 19 | #include "../../include/javascript/Document.h" |
| 20 | #include "../../include/javascript/event.h" |
| 21 | #include "../../include/javascript/Field.h" |
| 22 | #include "../../include/javascript/Icon.h" |
| 23 | #include "../../include/javascript/PublicMethods.h" |
| 24 | #include "../../include/javascript/report.h" |
| 25 | #include "../../include/javascript/util.h" |
| 26 | #include "../../include/javascript/JS_GlobalData.h" |
| 27 | #include "../../include/javascript/global.h" |
| 28 | #include "../../include/javascript/console.h" |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 29 | #include "../../include/fpdfxfa/fpdfxfa_app.h" |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 30 | #include "../../../xfa/src/fxjse/src/value.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 31 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 32 | CJS_RuntimeFactory::~CJS_RuntimeFactory() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 33 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 34 | IFXJS_Runtime* CJS_RuntimeFactory::NewJSRuntime(CPDFDoc_Environment* pApp) { |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame^] | 35 | m_bInit = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 36 | return new CJS_Runtime(pApp); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 37 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 38 | void CJS_RuntimeFactory::AddRef() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 39 | m_nRef++; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 40 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 41 | void CJS_RuntimeFactory::Release() { |
| 42 | if (m_bInit) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 43 | if (--m_nRef == 0) { |
| 44 | JS_Release(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 45 | m_bInit = FALSE; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 46 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
| 50 | void CJS_RuntimeFactory::DeleteJSRuntime(IFXJS_Runtime* pRuntime) { |
| 51 | delete (CJS_Runtime*)pRuntime; |
| 52 | } |
| 53 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 54 | /* ------------------------------ CJS_Runtime ------------------------------ */ |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 55 | v8::Global<v8::ObjectTemplate>& _getGlobalObjectTemplate(v8::Isolate* pIsolate); |
| 56 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 57 | CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) |
| 58 | : m_pApp(pApp), |
| 59 | m_pDocument(NULL), |
| 60 | m_bBlocking(FALSE), |
| 61 | m_pFieldEventPath(NULL), |
Jochen Eisinger | 2900784 | 2015-08-05 09:02:13 +0200 | [diff] [blame] | 62 | m_isolate(NULL), |
| 63 | m_isolateManaged(false) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 64 | if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { |
| 65 | // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. |
| 66 | m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime(); |
| 67 | } else if (m_pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) { |
| 68 | m_isolate = reinterpret_cast<v8::Isolate*>( |
| 69 | m_pApp->GetFormFillInfo()->m_pJsPlatform->m_isolate); |
| 70 | } |
| 71 | if (!m_isolate) { |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame^] | 72 | m_pArrayBufferAllocator.reset(new JS_ArrayBufferAllocator()); |
Jochen Eisinger | 06b6002 | 2015-07-30 17:44:35 +0200 | [diff] [blame] | 73 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 74 | v8::Isolate::CreateParams params; |
| 75 | params.array_buffer_allocator = m_pArrayBufferAllocator.get(); |
| 76 | m_isolate = v8::Isolate::New(params); |
Jochen Eisinger | 2900784 | 2015-08-05 09:02:13 +0200 | [diff] [blame] | 77 | m_isolateManaged = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 78 | } |
Tom Sepez | d2cc1b9 | 2015-04-30 15:19:03 -0700 | [diff] [blame] | 79 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 80 | v8::Isolate* isolate = m_isolate; |
| 81 | v8::Isolate::Scope isolate_scope(isolate); |
| 82 | v8::Locker locker(isolate); |
| 83 | v8::HandleScope handle_scope(isolate); |
| 84 | if (CPDFXFA_App::GetInstance()->InitRuntime(FALSE)) { |
| 85 | CJS_Context* pContext = (CJS_Context*)NewContext(); |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame^] | 86 | JS_InitializeRuntime(GetIsolate(), this, pContext, m_context); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 87 | ReleaseContext(pContext); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame^] | 91 | unsigned int embedderDataSlot = 0; |
| 92 | if (m_pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) |
| 93 | embedderDataSlot = pApp->GetFormFillInfo()->m_pJsPlatform->m_v8EmbedderSlot; |
| 94 | JS_Initialize(embedderDataSlot); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 95 | DefineJSObjects(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 96 | |
| 97 | CJS_Context* pContext = (CJS_Context*)NewContext(); |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame^] | 98 | JS_InitializeRuntime(GetIsolate(), this, pContext, m_context); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 99 | ReleaseContext(pContext); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 102 | CJS_Runtime::~CJS_Runtime() { |
| 103 | int size = m_ContextArray.GetSize(); |
| 104 | for (int i = 0; i < size; i++) |
| 105 | delete m_ContextArray.GetAt(i); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 106 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 107 | m_ContextArray.RemoveAll(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 108 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 109 | RemoveEventsInLoop(m_pFieldEventPath); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 110 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 111 | m_pApp = NULL; |
| 112 | m_pDocument = NULL; |
| 113 | m_pFieldEventPath = NULL; |
| 114 | m_context.Reset(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 115 | |
Jochen Eisinger | 2900784 | 2015-08-05 09:02:13 +0200 | [diff] [blame] | 116 | if (m_isolateManaged) |
| 117 | m_isolate->Dispose(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 118 | m_isolate = NULL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 121 | void CJS_Runtime::DefineJSObjects() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 122 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 123 | v8::Locker locker(GetIsolate()); |
| 124 | v8::HandleScope handle_scope(GetIsolate()); |
| 125 | v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); |
| 126 | v8::Context::Scope context_scope(context); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 127 | |
| 128 | // The call order determines the "ObjDefID" assigned to each class. |
| 129 | // ObjDefIDs 0 - 2 |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 130 | CJS_Border::DefineJSObjects(GetIsolate(), JS_STATIC); |
| 131 | CJS_Display::DefineJSObjects(GetIsolate(), JS_STATIC); |
| 132 | CJS_Font::DefineJSObjects(GetIsolate(), JS_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 133 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 134 | // ObjDefIDs 3 - 5 |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 135 | CJS_Highlight::DefineJSObjects(GetIsolate(), JS_STATIC); |
| 136 | CJS_Position::DefineJSObjects(GetIsolate(), JS_STATIC); |
| 137 | CJS_ScaleHow::DefineJSObjects(GetIsolate(), JS_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 138 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 139 | // ObjDefIDs 6 - 8 |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 140 | CJS_ScaleWhen::DefineJSObjects(GetIsolate(), JS_STATIC); |
| 141 | CJS_Style::DefineJSObjects(GetIsolate(), JS_STATIC); |
| 142 | CJS_Zoomtype::DefineJSObjects(GetIsolate(), JS_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 143 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 144 | // ObjDefIDs 9 - 11 |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 145 | CJS_App::DefineJSObjects(GetIsolate(), JS_STATIC); |
| 146 | CJS_Color::DefineJSObjects(GetIsolate(), JS_STATIC); |
| 147 | CJS_Console::DefineJSObjects(GetIsolate(), JS_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 148 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 149 | // ObjDefIDs 12 - 14 |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 150 | CJS_Document::DefineJSObjects(GetIsolate(), JS_DYNAMIC); |
| 151 | CJS_Event::DefineJSObjects(GetIsolate(), JS_STATIC); |
| 152 | CJS_Field::DefineJSObjects(GetIsolate(), JS_DYNAMIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 153 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 154 | // ObjDefIDs 15 - 17 |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 155 | CJS_Global::DefineJSObjects(GetIsolate(), JS_STATIC); |
| 156 | CJS_Icon::DefineJSObjects(GetIsolate(), JS_DYNAMIC); |
| 157 | CJS_Util::DefineJSObjects(GetIsolate(), JS_STATIC); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 158 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 159 | // ObjDefIDs 18 - 20 (these can't fail, return void). |
| 160 | CJS_PublicMethods::DefineJSObjects(GetIsolate()); |
| 161 | CJS_GlobalConsts::DefineJSObjects(GetIsolate()); |
| 162 | CJS_GlobalArrays::DefineJSObjects(GetIsolate()); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 163 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 164 | // ObjDefIDs 21 - 22. |
| 165 | CJS_TimerObj::DefineJSObjects(GetIsolate(), JS_DYNAMIC); |
| 166 | CJS_PrintParamsObj::DefineJSObjects(GetIsolate(), JS_DYNAMIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 169 | IFXJS_Context* CJS_Runtime::NewContext() { |
| 170 | CJS_Context* p = new CJS_Context(this); |
| 171 | m_ContextArray.Add(p); |
| 172 | return p; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 175 | void CJS_Runtime::ReleaseContext(IFXJS_Context* pContext) { |
| 176 | CJS_Context* pJSContext = (CJS_Context*)pContext; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 177 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 178 | for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) { |
| 179 | if (pJSContext == m_ContextArray.GetAt(i)) { |
| 180 | delete pJSContext; |
| 181 | m_ContextArray.RemoveAt(i); |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | IFXJS_Context* CJS_Runtime::GetCurrentContext() { |
| 188 | if (!m_ContextArray.GetSize()) |
| 189 | return NULL; |
| 190 | return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1); |
| 191 | } |
| 192 | |
| 193 | void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { |
| 194 | if (m_pDocument != pReaderDoc) { |
| 195 | v8::Isolate::Scope isolate_scope(m_isolate); |
| 196 | v8::Locker locker(m_isolate); |
| 197 | v8::HandleScope handle_scope(m_isolate); |
| 198 | v8::Local<v8::Context> context = |
| 199 | v8::Local<v8::Context>::New(m_isolate, m_context); |
| 200 | v8::Context::Scope context_scope(context); |
| 201 | |
| 202 | m_pDocument = pReaderDoc; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 203 | if (pReaderDoc) { |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 204 | v8::Local<v8::Object> pThis = JS_GetThisObj(GetIsolate()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 205 | if (!pThis.IsEmpty()) { |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 206 | if (JS_GetObjDefnID(pThis) == |
| 207 | JS_GetObjDefnID(GetIsolate(), L"Document")) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 208 | if (CJS_Document* pJSDocument = (CJS_Document*)JS_GetPrivate(pThis)) { |
| 209 | if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject()) |
| 210 | pDocument->AttachDoc(pReaderDoc); |
| 211 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 212 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 213 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 214 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 215 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 218 | FX_BOOL CJS_Runtime::AddEventToLoop(const CFX_WideString& sTargetName, |
| 219 | JS_EVENT_T eEventType) { |
| 220 | if (m_pFieldEventPath == NULL) { |
| 221 | m_pFieldEventPath = new CJS_FieldEvent; |
| 222 | m_pFieldEventPath->sTargetName = sTargetName; |
| 223 | m_pFieldEventPath->eEventType = eEventType; |
| 224 | m_pFieldEventPath->pNext = NULL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 225 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 226 | return TRUE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // to search |
| 230 | CJS_FieldEvent* p = m_pFieldEventPath; |
| 231 | CJS_FieldEvent* pLast = m_pFieldEventPath; |
| 232 | while (p) { |
| 233 | if (p->eEventType == eEventType && p->sTargetName == sTargetName) |
| 234 | return FALSE; |
| 235 | |
| 236 | pLast = p; |
| 237 | p = p->pNext; |
| 238 | } |
| 239 | |
| 240 | // to add |
| 241 | CJS_FieldEvent* pNew = new CJS_FieldEvent; |
| 242 | pNew->sTargetName = sTargetName; |
| 243 | pNew->eEventType = eEventType; |
| 244 | pNew->pNext = NULL; |
| 245 | |
| 246 | pLast->pNext = pNew; |
| 247 | |
| 248 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 251 | void CJS_Runtime::RemoveEventInLoop(const CFX_WideString& sTargetName, |
| 252 | JS_EVENT_T eEventType) { |
| 253 | FX_BOOL bFind = FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 254 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 255 | CJS_FieldEvent* p = m_pFieldEventPath; |
| 256 | CJS_FieldEvent* pLast = NULL; |
| 257 | while (p) { |
| 258 | if (p->eEventType == eEventType && p->sTargetName == sTargetName) { |
| 259 | bFind = TRUE; |
| 260 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 261 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 262 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 263 | pLast = p; |
| 264 | p = p->pNext; |
| 265 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 266 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 267 | if (bFind) { |
| 268 | RemoveEventsInLoop(p); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 269 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 270 | if (p == m_pFieldEventPath) |
| 271 | m_pFieldEventPath = NULL; |
| 272 | |
| 273 | if (pLast) |
| 274 | pLast->pNext = NULL; |
| 275 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 278 | void CJS_Runtime::RemoveEventsInLoop(CJS_FieldEvent* pStart) { |
| 279 | CJS_FieldEvent* p = pStart; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 280 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 281 | while (p) { |
| 282 | CJS_FieldEvent* pOld = p; |
| 283 | p = pOld->pNext; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 284 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 285 | delete pOld; |
| 286 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 289 | v8::Local<v8::Context> CJS_Runtime::NewJSContext() { |
| 290 | return v8::Local<v8::Context>::New(m_isolate, m_context); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 293 | CFX_WideString ChangeObjName(const CFX_WideString& str) { |
| 294 | CFX_WideString sRet = str; |
| 295 | sRet.Replace(L"_", L"."); |
| 296 | return sRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 297 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 298 | FX_BOOL CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name, |
| 299 | FXJSE_HVALUE hValue) { |
| 300 | const FX_CHAR* name = utf8Name.GetCStr(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 301 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 302 | v8::Locker lock(GetIsolate()); |
| 303 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 304 | v8::HandleScope handle_scope(GetIsolate()); |
| 305 | v8::Local<v8::Context> context = |
| 306 | v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 307 | v8::Context::Scope context_scope(context); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 308 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 309 | // v8::Local<v8::Context> tmpCotext = |
| 310 | // v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 311 | v8::Local<v8::Value> propvalue = |
| 312 | context->Global()->Get(v8::String::NewFromUtf8( |
| 313 | GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); |
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 | if (propvalue.IsEmpty()) { |
| 316 | FXJSE_Value_SetUndefined(hValue); |
| 317 | return FALSE; |
| 318 | } |
| 319 | ((CFXJSE_Value*)hValue)->ForceSetValue(propvalue); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 320 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 321 | return TRUE; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 322 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 323 | FX_BOOL CJS_Runtime::SetHValueByName(const CFX_ByteStringC& utf8Name, |
| 324 | FXJSE_HVALUE hValue) { |
| 325 | if (utf8Name.IsEmpty() || hValue == NULL) |
| 326 | return FALSE; |
| 327 | const FX_CHAR* name = utf8Name.GetCStr(); |
| 328 | v8::Isolate* pIsolate = GetIsolate(); |
| 329 | v8::Locker lock(pIsolate); |
| 330 | v8::Isolate::Scope isolate_scope(pIsolate); |
| 331 | v8::HandleScope handle_scope(pIsolate); |
| 332 | v8::Local<v8::Context> context = |
| 333 | v8::Local<v8::Context>::New(pIsolate, m_context); |
| 334 | v8::Context::Scope context_scope(context); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 335 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 336 | // v8::Local<v8::Context> tmpCotext = |
| 337 | // v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 338 | v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New( |
| 339 | GetIsolate(), ((CFXJSE_Value*)hValue)->DirectGetValue()); |
| 340 | context->Global()->Set( |
| 341 | v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, |
| 342 | utf8Name.GetLength()), |
| 343 | propvalue); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 344 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 345 | return TRUE; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 346 | } |