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