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