| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "../../include/javascript/JavaScript.h" | 7 #include "../../include/javascript/JavaScript.h" |
| 8 #include "../../include/javascript/IJavaScript.h" | 8 #include "../../include/javascript/IJavaScript.h" |
| 9 #include "../../include/javascript/JS_EventHandler.h" | 9 #include "../../include/javascript/JS_EventHandler.h" |
| 10 #include "../../include/javascript/JS_Runtime.h" | 10 #include "../../include/javascript/JS_Runtime.h" |
| 11 #include "../../include/javascript/JS_Context.h" | 11 #include "../../include/javascript/JS_Context.h" |
| 12 #include "../../include/javascript/JS_Define.h" | 12 #include "../../include/javascript/JS_Define.h" |
| 13 #include "../../include/javascript/JS_Object.h" | 13 #include "../../include/javascript/JS_Object.h" |
| 14 #include "../../include/javascript/JS_Value.h" | 14 #include "../../include/javascript/JS_Value.h" |
| 15 #include "../../include/javascript/Document.h" | 15 #include "../../include/javascript/Document.h" |
| 16 #include "../../include/javascript/app.h" | 16 #include "../../include/javascript/app.h" |
| 17 #include "../../include/javascript/color.h" | 17 #include "../../include/javascript/color.h" |
| 18 #include "../../include/javascript/Consts.h" | 18 #include "../../include/javascript/Consts.h" |
| 19 #include "../../include/javascript/Document.h" | 19 #include "../../include/javascript/Document.h" |
| 20 #include "../../include/javascript/event.h" | 20 #include "../../include/javascript/event.h" |
| 21 #include "../../include/javascript/Field.h" | 21 #include "../../include/javascript/Field.h" |
| 22 #include "../../include/javascript/Icon.h" | 22 #include "../../include/javascript/Icon.h" |
| 23 #include "../../include/javascript/PublicMethods.h" | 23 #include "../../include/javascript/PublicMethods.h" |
| 24 #include "../../include/javascript/report.h" | 24 #include "../../include/javascript/report.h" |
| 25 #include "../../include/javascript/util.h" | 25 #include "../../include/javascript/util.h" |
| 26 #include "../../include/javascript/JS_GlobalData.h" | 26 #include "../../include/javascript/JS_GlobalData.h" |
| 27 #include "../../include/javascript/global.h" | 27 #include "../../include/javascript/global.h" |
| 28 #include "../../include/javascript/console.h" | 28 #include "../../include/javascript/console.h" |
| 29 | 29 |
| 30 CJS_RuntimeFactory::~CJS_RuntimeFactory() | 30 CJS_RuntimeFactory::~CJS_RuntimeFactory() {} |
| 31 { | 31 |
| 32 } | 32 IFXJS_Runtime* CJS_RuntimeFactory::NewJSRuntime(CPDFDoc_Environment* pApp) { |
| 33 | 33 if (!m_bInit) { |
| 34 IFXJS_Runtime* CJS_RuntimeFactory::NewJSRuntime(CPDFDoc_Environ
ment* pApp) | 34 unsigned int embedderDataSlot = 0; |
| 35 { | 35 if (pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) { |
| 36 if (!m_bInit) | 36 embedderDataSlot = |
| 37 { | 37 pApp->GetFormFillInfo()->m_pJsPlatform->m_v8EmbedderSlot; |
| 38 unsigned int embedderDataSlot = 0; | 38 } |
| 39 if (pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) { | 39 JS_Initial(embedderDataSlot); |
| 40 embedderDataSlot = pApp->GetFormFillInfo()->m_pJsPlatform->m_v8Embed
derSlot; | 40 m_bInit = TRUE; |
| 41 } | 41 } |
| 42 JS_Initial(embedderDataSlot); | 42 return new CJS_Runtime(pApp); |
| 43 m_bInit = TRUE; | 43 } |
| 44 } | 44 void CJS_RuntimeFactory::AddRef() { |
| 45 return new CJS_Runtime(pApp); | 45 // to do.Should be implemented as atom manipulation. |
| 46 } | 46 m_nRef++; |
| 47 void CJS_RuntimeFactory::AddRef() | 47 } |
| 48 { | 48 void CJS_RuntimeFactory::Release() { |
| 49 //to do.Should be implemented as atom manipulation. | 49 if (m_bInit) { |
| 50 m_nRef++; | 50 // to do.Should be implemented as atom manipulation. |
| 51 } | 51 if (--m_nRef == 0) { |
| 52 void CJS_RuntimeFactory::Release() | 52 JS_Release(); |
| 53 { | 53 ReleaseGlobalData(); |
| 54 if(m_bInit) | 54 m_bInit = FALSE; |
| 55 { | 55 } |
| 56 //to do.Should be implemented as atom manipulation. | 56 } |
| 57 if (--m_nRef == 0) | 57 } |
| 58 { | 58 |
| 59 JS_Release(); | 59 void CJS_RuntimeFactory::DeleteJSRuntime(IFXJS_Runtime* pRuntime) { |
| 60 ReleaseGlobalData(); | 60 delete (CJS_Runtime*)pRuntime; |
| 61 m_bInit = FALSE; | 61 } |
| 62 |
| 63 CJS_GlobalData* CJS_RuntimeFactory::NewGlobalData(CPDFDoc_Environment* pApp) { |
| 64 if (m_pGlobalData) { |
| 65 m_nGlobalDataCount++; |
| 66 return m_pGlobalData; |
| 67 } |
| 68 m_nGlobalDataCount = 1; |
| 69 m_pGlobalData = new CJS_GlobalData(pApp); |
| 70 return m_pGlobalData; |
| 71 } |
| 72 |
| 73 void CJS_RuntimeFactory::ReleaseGlobalData() { |
| 74 m_nGlobalDataCount--; |
| 75 |
| 76 if (m_nGlobalDataCount <= 0) { |
| 77 delete m_pGlobalData; |
| 78 m_pGlobalData = NULL; |
| 79 } |
| 80 } |
| 81 |
| 82 void* CJS_ArrayBufferAllocator::Allocate(size_t length) { |
| 83 return calloc(1, length); |
| 84 } |
| 85 |
| 86 void* CJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { |
| 87 return malloc(length); |
| 88 } |
| 89 |
| 90 void CJS_ArrayBufferAllocator::Free(void* data, size_t length) { |
| 91 free(data); |
| 92 } |
| 93 |
| 94 /* ------------------------------ CJS_Runtime ------------------------------ */ |
| 95 |
| 96 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) |
| 97 : m_pApp(pApp), |
| 98 m_pDocument(NULL), |
| 99 m_bBlocking(FALSE), |
| 100 m_pFieldEventPath(NULL), |
| 101 m_isolate(NULL) { |
| 102 if (m_pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) { |
| 103 m_isolate = reinterpret_cast<v8::Isolate*>( |
| 104 m_pApp->GetFormFillInfo()->m_pJsPlatform->m_isolate); |
| 105 } |
| 106 if (!m_isolate) { |
| 107 m_pArrayBufferAllocator.reset(new CJS_ArrayBufferAllocator()); |
| 108 |
| 109 v8::Isolate::CreateParams params; |
| 110 params.array_buffer_allocator = m_pArrayBufferAllocator.get(); |
| 111 m_isolate = v8::Isolate::New(params); |
| 112 } |
| 113 |
| 114 InitJSObjects(); |
| 115 |
| 116 CJS_Context* pContext = (CJS_Context*)NewContext(); |
| 117 JS_InitialRuntime(*this, this, pContext, m_context); |
| 118 ReleaseContext(pContext); |
| 119 } |
| 120 |
| 121 CJS_Runtime::~CJS_Runtime() { |
| 122 for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) |
| 123 delete m_ContextArray.GetAt(i); |
| 124 |
| 125 m_ContextArray.RemoveAll(); |
| 126 |
| 127 JS_ReleaseRuntime(*this, m_context); |
| 128 |
| 129 RemoveEventsInLoop(m_pFieldEventPath); |
| 130 |
| 131 m_pApp = NULL; |
| 132 m_pDocument = NULL; |
| 133 m_pFieldEventPath = NULL; |
| 134 m_context.Reset(); |
| 135 |
| 136 // m_isolate->Exit(); |
| 137 m_isolate->Dispose(); |
| 138 } |
| 139 |
| 140 FX_BOOL CJS_Runtime::InitJSObjects() { |
| 141 v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 142 v8::HandleScope handle_scope(GetIsolate()); |
| 143 v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); |
| 144 v8::Context::Scope context_scope(context); |
| 145 // 0 - 8 |
| 146 if (CJS_Border::Init(*this, JS_STATIC) < 0) |
| 147 return FALSE; |
| 148 if (CJS_Display::Init(*this, JS_STATIC) < 0) |
| 149 return FALSE; |
| 150 if (CJS_Font::Init(*this, JS_STATIC) < 0) |
| 151 return FALSE; |
| 152 if (CJS_Highlight::Init(*this, JS_STATIC) < 0) |
| 153 return FALSE; |
| 154 if (CJS_Position::Init(*this, JS_STATIC) < 0) |
| 155 return FALSE; |
| 156 if (CJS_ScaleHow::Init(*this, JS_STATIC) < 0) |
| 157 return FALSE; |
| 158 if (CJS_ScaleWhen::Init(*this, JS_STATIC) < 0) |
| 159 return FALSE; |
| 160 if (CJS_Style::Init(*this, JS_STATIC) < 0) |
| 161 return FALSE; |
| 162 if (CJS_Zoomtype::Init(*this, JS_STATIC) < 0) |
| 163 return FALSE; |
| 164 |
| 165 // 9 - 11 |
| 166 if (CJS_App::Init(*this, JS_STATIC) < 0) |
| 167 return FALSE; |
| 168 if (CJS_Color::Init(*this, JS_STATIC) < 0) |
| 169 return FALSE; |
| 170 if (CJS_Console::Init(*this, JS_STATIC) < 0) |
| 171 return FALSE; |
| 172 |
| 173 // 12 - 14 |
| 174 if (CJS_Document::Init(*this, JS_DYNAMIC) < 0) |
| 175 return FALSE; |
| 176 if (CJS_Event::Init(*this, JS_STATIC) < 0) |
| 177 return FALSE; |
| 178 if (CJS_Field::Init(*this, JS_DYNAMIC) < 0) |
| 179 return FALSE; |
| 180 |
| 181 // 15 - 17 |
| 182 if (CJS_Global::Init(*this, JS_STATIC) < 0) |
| 183 return FALSE; |
| 184 if (CJS_Icon::Init(*this, JS_DYNAMIC) < 0) |
| 185 return FALSE; |
| 186 if (CJS_Util::Init(*this, JS_STATIC) < 0) |
| 187 return FALSE; |
| 188 |
| 189 if (CJS_PublicMethods::Init(*this) < 0) |
| 190 return FALSE; |
| 191 if (CJS_GlobalConsts::Init(*this) < 0) |
| 192 return FALSE; |
| 193 if (CJS_GlobalArrays::Init(*this) < 0) |
| 194 return FALSE; |
| 195 |
| 196 if (CJS_TimerObj::Init(*this, JS_DYNAMIC) < 0) |
| 197 return FALSE; |
| 198 if (CJS_PrintParamsObj::Init(*this, JS_DYNAMIC) < 0) |
| 199 return FALSE; |
| 200 |
| 201 return TRUE; |
| 202 } |
| 203 |
| 204 IFXJS_Context* CJS_Runtime::NewContext() { |
| 205 CJS_Context* p = new CJS_Context(this); |
| 206 m_ContextArray.Add(p); |
| 207 return p; |
| 208 } |
| 209 |
| 210 void CJS_Runtime::ReleaseContext(IFXJS_Context* pContext) { |
| 211 CJS_Context* pJSContext = (CJS_Context*)pContext; |
| 212 |
| 213 for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) { |
| 214 if (pJSContext == m_ContextArray.GetAt(i)) { |
| 215 delete pJSContext; |
| 216 m_ContextArray.RemoveAt(i); |
| 217 break; |
| 218 } |
| 219 } |
| 220 } |
| 221 |
| 222 IFXJS_Context* CJS_Runtime::GetCurrentContext() { |
| 223 if (!m_ContextArray.GetSize()) |
| 224 return NULL; |
| 225 return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1); |
| 226 } |
| 227 |
| 228 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { |
| 229 if (m_pDocument != pReaderDoc) { |
| 230 v8::Isolate::Scope isolate_scope(m_isolate); |
| 231 v8::HandleScope handle_scope(m_isolate); |
| 232 v8::Local<v8::Context> context = |
| 233 v8::Local<v8::Context>::New(m_isolate, m_context); |
| 234 v8::Context::Scope context_scope(context); |
| 235 |
| 236 m_pDocument = pReaderDoc; |
| 237 |
| 238 if (pReaderDoc) { |
| 239 JSObject pThis = JS_GetThisObj(*this); |
| 240 if (!pThis.IsEmpty()) { |
| 241 if (JS_GetObjDefnID(pThis) == JS_GetObjDefnID(*this, L"Document")) { |
| 242 if (CJS_Document* pJSDocument = (CJS_Document*)JS_GetPrivate(pThis)) { |
| 243 if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject()) |
| 244 pDocument->AttachDoc(pReaderDoc); |
| 245 } |
| 62 } | 246 } |
| 63 } | 247 } |
| 64 } | 248 JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"Document")); |
| 65 | 249 } else { |
| 66 void CJS_RuntimeFactory::DeleteJSRuntime(IFXJS_Runtim
e* pRuntime) | 250 JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"app")); |
| 67 { | 251 } |
| 68 delete (CJS_Runtime*)pRuntime; | 252 } |
| 69 } | 253 } |
| 70 | 254 |
| 71 CJS_GlobalData* CJS_RuntimeFactory::NewGlobalData(CPDFDoc_Environment* pApp) | 255 FX_BOOL CJS_Runtime::AddEventToLoop(const CFX_WideString& sTargetName, |
| 72 { | 256 JS_EVENT_T eEventType) { |
| 73 if (m_pGlobalData) | 257 if (m_pFieldEventPath == NULL) { |
| 74 { | 258 m_pFieldEventPath = new CJS_FieldEvent; |
| 75 m_nGlobalDataCount++; | 259 m_pFieldEventPath->sTargetName = sTargetName; |
| 76 return m_pGlobalData; | 260 m_pFieldEventPath->eEventType = eEventType; |
| 77 } | 261 m_pFieldEventPath->pNext = NULL; |
| 78 m_nGlobalDataCount = 1; | |
| 79 m_pGlobalData = new CJS_GlobalData(pApp); | |
| 80 return m_pGlobalData; | |
| 81 } | |
| 82 | |
| 83 void CJS_RuntimeFactory::ReleaseGlobalData() | |
| 84 { | |
| 85 m_nGlobalDataCount--; | |
| 86 | |
| 87 if (m_nGlobalDataCount <= 0) | |
| 88 { | |
| 89 delete m_pGlobalData; | |
| 90 m_pGlobalData = NULL; | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 void* CJS_ArrayBufferAllocator::Allocate(size_t length) { | |
| 95 return calloc(1, length); | |
| 96 } | |
| 97 | |
| 98 void* CJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { | |
| 99 return malloc(length); | |
| 100 } | |
| 101 | |
| 102 void CJS_ArrayBufferAllocator::Free(void* data, size_t length) { | |
| 103 free(data); | |
| 104 } | |
| 105 | |
| 106 /* ------------------------------ CJS_Runtime ------------------------------ */ | |
| 107 | |
| 108 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment * pApp) : | |
| 109 » m_pApp(pApp), | |
| 110 » m_pDocument(NULL), | |
| 111 » m_bBlocking(FALSE), | |
| 112 » m_pFieldEventPath(NULL), | |
| 113 m_isolate(NULL) | |
| 114 { | |
| 115 if (m_pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) { | |
| 116 m_isolate = reinterpret_cast<v8::Isolate*>(m_pApp->GetFormFillInfo()->m_
pJsPlatform->m_isolate); | |
| 117 } | |
| 118 if (!m_isolate) { | |
| 119 m_pArrayBufferAllocator.reset(new CJS_ArrayBufferAllocator()); | |
| 120 | |
| 121 v8::Isolate::CreateParams params; | |
| 122 params.array_buffer_allocator = m_pArrayBufferAllocator.get(); | |
| 123 m_isolate = v8::Isolate::New(params); | |
| 124 } | |
| 125 | |
| 126 InitJSObjects(); | |
| 127 | |
| 128 CJS_Context * pContext = (CJS_Context*)NewContext(); | |
| 129 JS_InitialRuntime(*this, this, pContext, m_context); | |
| 130 ReleaseContext(pContext); | |
| 131 } | |
| 132 | |
| 133 CJS_Runtime::~CJS_Runtime() | |
| 134 { | |
| 135 for (int i=0, sz=m_ContextArray.GetSize(); i<sz; i++) | |
| 136 delete m_ContextArray.GetAt(i); | |
| 137 | |
| 138 m_ContextArray.RemoveAll(); | |
| 139 | |
| 140 JS_ReleaseRuntime(*this, m_context); | |
| 141 | |
| 142 RemoveEventsInLoop(m_pFieldEventPath); | |
| 143 | |
| 144 m_pApp = NULL; | |
| 145 m_pDocument = NULL; | |
| 146 m_pFieldEventPath = NULL; | |
| 147 m_context.Reset(); | |
| 148 | |
| 149 //m_isolate->Exit(); | |
| 150 m_isolate->Dispose(); | |
| 151 } | |
| 152 | |
| 153 FX_BOOL CJS_Runtime::InitJSObjects() | |
| 154 { | |
| 155 v8::Isolate::Scope isolate_scope(GetIsolate()); | |
| 156 v8::HandleScope handle_scope(GetIsolate()); | |
| 157 v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); | |
| 158 v8::Context::Scope context_scope(context); | |
| 159 //0 - 8 | |
| 160 if (CJS_Border::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 161 if (CJS_Display::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 162 if (CJS_Font::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 163 if (CJS_Highlight::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 164 if (CJS_Position::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 165 if (CJS_ScaleHow::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 166 if (CJS_ScaleWhen::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 167 if (CJS_Style::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 168 if (CJS_Zoomtype::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 169 | |
| 170 //9 - 11 | |
| 171 if (CJS_App::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 172 if (CJS_Color::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 173 if (CJS_Console::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 174 | |
| 175 //12 - 14 | |
| 176 if (CJS_Document::Init(*this, JS_DYNAMIC) < 0) return FALSE; | |
| 177 if (CJS_Event::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 178 if (CJS_Field::Init(*this, JS_DYNAMIC) < 0) return FALSE; | |
| 179 | |
| 180 //15 - 17 | |
| 181 if (CJS_Global::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 182 if (CJS_Icon::Init(*this, JS_DYNAMIC) < 0) return FALSE; | |
| 183 if (CJS_Util::Init(*this, JS_STATIC) < 0) return FALSE; | |
| 184 | |
| 185 if (CJS_PublicMethods::Init(*this) < 0) return FALSE; | |
| 186 if (CJS_GlobalConsts::Init(*this) < 0) return FALSE; | |
| 187 if (CJS_GlobalArrays::Init(*this) < 0) return FALSE; | |
| 188 | |
| 189 if (CJS_TimerObj::Init(*this, JS_DYNAMIC) < 0) return FALSE; | |
| 190 if (CJS_PrintParamsObj::Init(*this, JS_DYNAMIC) <0) return FALSE; | |
| 191 | 262 |
| 192 return TRUE; | 263 return TRUE; |
| 193 } | 264 } |
| 194 | 265 |
| 195 IFXJS_Context* CJS_Runtime::NewContext() | 266 // to search |
| 196 { | 267 CJS_FieldEvent* p = m_pFieldEventPath; |
| 197 CJS_Context * p = new CJS_Context(this); | 268 CJS_FieldEvent* pLast = m_pFieldEventPath; |
| 198 m_ContextArray.Add(p); | 269 while (p) { |
| 199 return p; | 270 if (p->eEventType == eEventType && p->sTargetName == sTargetName) |
| 200 } | 271 return FALSE; |
| 201 | 272 |
| 202 void CJS_Runtime::ReleaseContext(IFXJS_Context * pContext) | 273 pLast = p; |
| 203 { | 274 p = p->pNext; |
| 204 CJS_Context* pJSContext = (CJS_Context*)pContext; | 275 } |
| 205 | 276 |
| 206 for (int i=0, sz=m_ContextArray.GetSize(); i<sz; i++) | 277 // to add |
| 207 { | 278 CJS_FieldEvent* pNew = new CJS_FieldEvent; |
| 208 if (pJSContext == m_ContextArray.GetAt(i)) | 279 pNew->sTargetName = sTargetName; |
| 209 { | 280 pNew->eEventType = eEventType; |
| 210 delete pJSContext; | 281 pNew->pNext = NULL; |
| 211 m_ContextArray.RemoveAt(i); | 282 |
| 212 break; | 283 pLast->pNext = pNew; |
| 213 } | 284 |
| 214 } | 285 return TRUE; |
| 215 } | 286 } |
| 216 | 287 |
| 217 IFXJS_Context* CJS_Runtime::GetCurrentContext() | 288 void CJS_Runtime::RemoveEventInLoop(const CFX_WideString& sTargetName, |
| 218 { | 289 JS_EVENT_T eEventType) { |
| 219 if(!m_ContextArray.GetSize()) | 290 FX_BOOL bFind = FALSE; |
| 220 return NULL; | 291 |
| 221 return m_ContextArray.GetAt(m_ContextArray.GetSize()-1); | 292 CJS_FieldEvent* p = m_pFieldEventPath; |
| 222 } | 293 CJS_FieldEvent* pLast = NULL; |
| 223 | 294 while (p) { |
| 224 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) | 295 if (p->eEventType == eEventType && p->sTargetName == sTargetName) { |
| 225 { | 296 bFind = TRUE; |
| 226 if (m_pDocument != pReaderDoc) | 297 break; |
| 227 { | 298 } |
| 228 v8::Isolate::Scope isolate_scope(m_isolate); | 299 |
| 229 v8::HandleScope handle_scope(m_isolate); | 300 pLast = p; |
| 230 v8::Local<v8::Context> context =v8::Local<v8::Context>::New(m_isolate, m
_context); | 301 p = p->pNext; |
| 231 v8::Context::Scope context_scope(context); | 302 } |
| 232 | 303 |
| 233 m_pDocument = pReaderDoc; | 304 if (bFind) { |
| 234 | 305 RemoveEventsInLoop(p); |
| 235 if (pReaderDoc) | 306 |
| 236 { | 307 if (p == m_pFieldEventPath) |
| 237 JSObject pThis = JS_GetThisObj(*this); | 308 m_pFieldEventPath = NULL; |
| 238 if(!pThis.IsEmpty()) | 309 |
| 239 { | 310 if (pLast) |
| 240 if (JS_GetObjDefnID(pThis) == JS_GetObjDefnID(*this, L"Document"
)) | 311 pLast->pNext = NULL; |
| 241 { | 312 } |
| 242 if (CJS_Document* pJSDocument = (CJS_Document*)JS_GetPrivate
(pThis)) | 313 } |
| 243 { | 314 |
| 244 if (Document * pDocument = (Document*)pJSDocument->GetEm
bedObject()) | 315 void CJS_Runtime::RemoveEventsInLoop(CJS_FieldEvent* pStart) { |
| 245 pDocument->AttachDoc(pReaderDoc); | 316 CJS_FieldEvent* p = pStart; |
| 246 } | 317 |
| 247 } | 318 while (p) { |
| 248 } | 319 CJS_FieldEvent* pOld = p; |
| 249 JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"Document")); | 320 p = pOld->pNext; |
| 250 } | 321 |
| 251 else | 322 delete pOld; |
| 252 { | 323 } |
| 253 JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"app")); | 324 } |
| 254 } | 325 |
| 255 } | 326 v8::Local<v8::Context> CJS_Runtime::NewJSContext() { |
| 256 } | 327 return v8::Local<v8::Context>::New(m_isolate, m_context); |
| 257 | 328 } |
| 258 FX_BOOL CJS_Runtime::AddEventToLoop(const CFX_WideString& sTargetName, JS_EVENT_
T eEventType) | 329 |
| 259 { | 330 CFX_WideString ChangeObjName(const CFX_WideString& str) { |
| 260 if (m_pFieldEventPath == NULL) | 331 CFX_WideString sRet = str; |
| 261 { | 332 sRet.Replace(L"_", L"."); |
| 262 m_pFieldEventPath = new CJS_FieldEvent; | 333 return sRet; |
| 263 m_pFieldEventPath->sTargetName = sTargetName; | 334 } |
| 264 m_pFieldEventPath->eEventType = eEventType; | |
| 265 m_pFieldEventPath->pNext = NULL; | |
| 266 | |
| 267 return TRUE; | |
| 268 } | |
| 269 | |
| 270 //to search | |
| 271 CJS_FieldEvent* p = m_pFieldEventPath; | |
| 272 CJS_FieldEvent* pLast = m_pFieldEventPath; | |
| 273 while (p) | |
| 274 { | |
| 275 if (p->eEventType == eEventType && p->sTargetName == sTargetName) | |
| 276 return FALSE; | |
| 277 | |
| 278 pLast = p; | |
| 279 p = p->pNext; | |
| 280 } | |
| 281 | |
| 282 //to add | |
| 283 CJS_FieldEvent* pNew = new CJS_FieldEvent; | |
| 284 pNew->sTargetName = sTargetName; | |
| 285 pNew->eEventType = eEventType; | |
| 286 pNew->pNext = NULL; | |
| 287 | |
| 288 pLast->pNext = pNew; | |
| 289 | |
| 290 return TRUE; | |
| 291 } | |
| 292 | |
| 293 void CJS_Runtime::RemoveEventInLoop(const CFX_WideString& sTargetName, JS_EVENT_
T eEventType) | |
| 294 { | |
| 295 FX_BOOL bFind = FALSE; | |
| 296 | |
| 297 CJS_FieldEvent* p = m_pFieldEventPath; | |
| 298 CJS_FieldEvent* pLast = NULL; | |
| 299 while (p) | |
| 300 { | |
| 301 if (p->eEventType == eEventType && p->sTargetName == sTargetName) | |
| 302 { | |
| 303 bFind = TRUE; | |
| 304 break; | |
| 305 } | |
| 306 | |
| 307 pLast = p; | |
| 308 p = p->pNext; | |
| 309 } | |
| 310 | |
| 311 if (bFind) | |
| 312 { | |
| 313 RemoveEventsInLoop(p); | |
| 314 | |
| 315 if (p == m_pFieldEventPath) | |
| 316 m_pFieldEventPath = NULL; | |
| 317 | |
| 318 if (pLast) | |
| 319 pLast->pNext = NULL; | |
| 320 } | |
| 321 } | |
| 322 | |
| 323 void CJS_Runtime::RemoveEventsInLoop(CJS_FieldEvent* pStart) | |
| 324 { | |
| 325 CJS_FieldEvent* p = pStart; | |
| 326 | |
| 327 while (p) | |
| 328 { | |
| 329 CJS_FieldEvent* pOld = p; | |
| 330 p = pOld->pNext; | |
| 331 | |
| 332 delete pOld; | |
| 333 } | |
| 334 } | |
| 335 | |
| 336 v8::Local<v8::Context> CJS_Runtime::NewJSContext() | |
| 337 { | |
| 338 return v8::Local<v8::Context>::New(m_isolate, m_context); | |
| 339 } | |
| 340 | |
| 341 CFX_WideString ChangeObjName(const CFX_WideString& str) | |
| 342 { | |
| 343 CFX_WideString sRet = str; | |
| 344 sRet.Replace(L"_", L"."); | |
| 345 return sRet; | |
| 346 } | |
| OLD | NEW |