| 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" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 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 #include "../../include/fpdfxfa/fpdfxfa_app.h" | 29 #include "../../include/fpdfxfa/fpdfxfa_app.h" |
| 30 #include "../../../xfa/src/fxjse/src/value.h" | 30 #include "../../../xfa/src/fxjse/src/value.h" |
| 31 | 31 |
| 32 CJS_RuntimeFactory::~CJS_RuntimeFactory() | 32 CJS_RuntimeFactory::~CJS_RuntimeFactory() |
| 33 { | 33 { |
| 34 } | 34 } |
| 35 | 35 |
| 36 IFXJS_Runtime*» » » » » CJS_RuntimeFactory::NewJSRuntime
(CPDFDoc_Environment* pApp) | 36 IFXJS_Runtime* CJS_RuntimeFactory::NewJSRuntime(CPDFDoc_Environ
ment* pApp) |
| 37 { | 37 { |
| 38 if (!m_bInit) | 38 if (!m_bInit) |
| 39 { | 39 { |
| 40 JS_Initial(); | 40 JS_Initial(); |
| 41 m_bInit = TRUE; | 41 m_bInit = TRUE; |
| 42 } | 42 } |
| 43 return new CJS_Runtime(pApp); | 43 return new CJS_Runtime(pApp); |
| 44 } | 44 } |
| 45 void» » » » » » » CJS_RuntimeFactory::AddR
ef() | 45 void CJS_RuntimeFactory::AddRef() |
| 46 { | 46 { |
| 47 » //to do.Should be implemented as atom manipulation. | 47 //to do.Should be implemented as atom manipulation. |
| 48 » m_nRef++; | 48 m_nRef++; |
| 49 } | 49 } |
| 50 void» » » » » » » CJS_RuntimeFactory::Rele
ase() | 50 void CJS_RuntimeFactory::Release() |
| 51 { | 51 { |
| 52 » if(m_bInit) | 52 if(m_bInit) |
| 53 » { | 53 { |
| 54 » » //to do.Should be implemented as atom manipulation. | 54 //to do.Should be implemented as atom manipulation. |
| 55 » » if (--m_nRef == 0) | 55 if (--m_nRef == 0) |
| 56 » » { | 56 { |
| 57 » » » JS_Release(); | 57 JS_Release(); |
| 58 » » » ReleaseGlobalData(); | 58 ReleaseGlobalData(); |
| 59 » » » m_bInit = FALSE; | 59 m_bInit = FALSE; |
| 60 » » } | 60 } |
| 61 » } | 61 } |
| 62 } | 62 } |
| 63 | 63 |
| 64 void» » » » » » » CJS_RuntimeFactory::Dele
teJSRuntime(IFXJS_Runtime* pRuntime) | 64 void CJS_RuntimeFactory::DeleteJSRuntime(IFXJS_Runtim
e* pRuntime) |
| 65 { | 65 { |
| 66 delete (CJS_Runtime*)pRuntime; | 66 delete (CJS_Runtime*)pRuntime; |
| 67 } | 67 } |
| 68 | 68 |
| 69 CJS_GlobalData*»CJS_RuntimeFactory::NewGlobalData(CPDFDoc_Environment* pApp) | 69 CJS_GlobalData* CJS_RuntimeFactory::NewGlobalData(CPDFDoc_Environment* pApp) |
| 70 { | 70 { |
| 71 » if (m_pGlobalData) | 71 if (m_pGlobalData) |
| 72 » { | 72 { |
| 73 » » m_nGlobalDataCount++; | 73 m_nGlobalDataCount++; |
| 74 » » return m_pGlobalData; | 74 return m_pGlobalData; |
| 75 » } | 75 } |
| 76 » else | 76 m_nGlobalDataCount = 1; |
| 77 » { | 77 m_pGlobalData = new CJS_GlobalData(pApp); |
| 78 » » m_nGlobalDataCount = 1; | 78 return m_pGlobalData; |
| 79 » » m_pGlobalData = new CJS_GlobalData(pApp); | |
| 80 » » return m_pGlobalData; | |
| 81 » } | |
| 82 } | 79 } |
| 83 | 80 |
| 84 void CJS_RuntimeFactory::ReleaseGlobalData() | 81 void CJS_RuntimeFactory::ReleaseGlobalData() |
| 85 { | 82 { |
| 86 » m_nGlobalDataCount--; | 83 m_nGlobalDataCount--; |
| 87 | 84 |
| 88 » if (m_nGlobalDataCount <= 0) | 85 if (m_nGlobalDataCount <= 0) |
| 89 » { | 86 { |
| 90 » » delete m_pGlobalData; | 87 delete m_pGlobalData; |
| 91 » » m_pGlobalData = NULL; | 88 m_pGlobalData = NULL; |
| 92 » } | 89 } |
| 93 } | 90 } |
| 94 | 91 |
| 95 void* CJS_ArrayBufferAllocator::Allocate(size_t length) { | 92 void* CJS_ArrayBufferAllocator::Allocate(size_t length) { |
| 96 return calloc(1, length); | 93 return calloc(1, length); |
| 97 } | 94 } |
| 98 | 95 |
| 99 void* CJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { | 96 void* CJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { |
| 100 return malloc(length); | 97 return malloc(length); |
| 101 } | 98 } |
| 102 | 99 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 125 v8::Isolate::Scope isolate_scope(isolate); | 122 v8::Isolate::Scope isolate_scope(isolate); |
| 126 v8::Locker locker(isolate); | 123 v8::Locker locker(isolate); |
| 127 v8::HandleScope handle_scope(isolate); | 124 v8::HandleScope handle_scope(isolate); |
| 128 if (CPDFXFA_App::GetInstance()->InitRuntime(FALSE)) { | 125 if (CPDFXFA_App::GetInstance()->InitRuntime(FALSE)) { |
| 129 CJS_Context * pContext = (CJS_Context*)NewContext(); | 126 CJS_Context * pContext = (CJS_Context*)NewContext(); |
| 130 JS_InitialRuntime(*this, this, pContext, m_context); | 127 JS_InitialRuntime(*this, this, pContext, m_context); |
| 131 ReleaseContext(pContext); | 128 ReleaseContext(pContext); |
| 132 return; | 129 return; |
| 133 } | 130 } |
| 134 | 131 |
| 135 » InitJSObjects(); | 132 InitJSObjects(); |
| 136 | 133 |
| 137 » CJS_Context * pContext = (CJS_Context*)NewContext(); | 134 CJS_Context * pContext = (CJS_Context*)NewContext(); |
| 138 » JS_InitialRuntime(*this, this, pContext, m_context); | 135 JS_InitialRuntime(*this, this, pContext, m_context); |
| 139 » ReleaseContext(pContext); | 136 ReleaseContext(pContext); |
| 140 } | 137 } |
| 141 | 138 |
| 142 CJS_Runtime::~CJS_Runtime() | 139 CJS_Runtime::~CJS_Runtime() |
| 143 { | 140 { |
| 144 int size = m_ContextArray.GetSize(); | 141 int size = m_ContextArray.GetSize(); |
| 145 for (int i=0;i < size; i++) | 142 for (int i=0;i < size; i++) |
| 146 delete m_ContextArray.GetAt(i); | 143 delete m_ContextArray.GetAt(i); |
| 147 | 144 |
| 148 » m_ContextArray.RemoveAll(); | 145 m_ContextArray.RemoveAll(); |
| 149 | 146 |
| 150 » //JS_ReleaseRuntime(*this, m_context); | 147 RemoveEventsInLoop(m_pFieldEventPath); |
| 151 | 148 |
| 152 » RemoveEventsInLoop(m_pFieldEventPath); | 149 m_pApp = NULL; |
| 150 m_pDocument = NULL; |
| 151 m_pFieldEventPath = NULL; |
| 152 m_context.Reset(); |
| 153 | 153 |
| 154 m_pApp = NULL; | |
| 155 m_pDocument = NULL; | |
| 156 m_pFieldEventPath = NULL; | |
| 157 m_context.Reset(); | |
| 158 | |
| 159 //m_isolate->Exit(); | |
| 160 //m_isolate->Dispose(); | |
| 161 m_isolate = NULL; | 154 m_isolate = NULL; |
| 162 } | 155 } |
| 163 | 156 |
| 164 FX_BOOL CJS_Runtime::InitJSObjects() | 157 FX_BOOL CJS_Runtime::InitJSObjects() |
| 165 { | 158 { |
| 166 v8::Isolate::Scope isolate_scope(GetIsolate()); | 159 v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 167 v8::Locker locker(GetIsolate()); | 160 v8::Locker locker(GetIsolate()); |
| 168 v8::HandleScope handle_scope(GetIsolate()); | 161 v8::HandleScope handle_scope(GetIsolate()); |
| 169 v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); | 162 v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); |
| 170 v8::Context::Scope context_scope(context); | 163 v8::Context::Scope context_scope(context); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 199 if (CJS_GlobalArrays::Init(*this) < 0) return FALSE; | 192 if (CJS_GlobalArrays::Init(*this) < 0) return FALSE; |
| 200 | 193 |
| 201 if (CJS_TimerObj::Init(*this, JS_DYNAMIC) < 0) return FALSE; | 194 if (CJS_TimerObj::Init(*this, JS_DYNAMIC) < 0) return FALSE; |
| 202 if (CJS_PrintParamsObj::Init(*this, JS_DYNAMIC) <0) return FALSE; | 195 if (CJS_PrintParamsObj::Init(*this, JS_DYNAMIC) <0) return FALSE; |
| 203 | 196 |
| 204 return TRUE; | 197 return TRUE; |
| 205 } | 198 } |
| 206 | 199 |
| 207 IFXJS_Context* CJS_Runtime::NewContext() | 200 IFXJS_Context* CJS_Runtime::NewContext() |
| 208 { | 201 { |
| 209 » CJS_Context * p = new CJS_Context(this); | 202 CJS_Context * p = new CJS_Context(this); |
| 210 » m_ContextArray.Add(p); | 203 m_ContextArray.Add(p); |
| 211 » return p; | 204 return p; |
| 212 } | 205 } |
| 213 | 206 |
| 214 void CJS_Runtime::ReleaseContext(IFXJS_Context * pContext) | 207 void CJS_Runtime::ReleaseContext(IFXJS_Context * pContext) |
| 215 { | 208 { |
| 216 » CJS_Context* pJSContext = (CJS_Context*)pContext; | 209 CJS_Context* pJSContext = (CJS_Context*)pContext; |
| 217 | 210 |
| 218 » for (int i=0, sz=m_ContextArray.GetSize(); i<sz; i++) | 211 for (int i=0, sz=m_ContextArray.GetSize(); i<sz; i++) |
| 219 » { | 212 { |
| 220 » » if (pJSContext == m_ContextArray.GetAt(i)) | 213 if (pJSContext == m_ContextArray.GetAt(i)) |
| 221 » » { | 214 { |
| 222 » » » delete pJSContext; | 215 delete pJSContext; |
| 223 » » » m_ContextArray.RemoveAt(i); | 216 m_ContextArray.RemoveAt(i); |
| 224 » » » break; | 217 break; |
| 225 » » } | 218 } |
| 226 » } | 219 } |
| 227 } | 220 } |
| 228 | 221 |
| 229 IFXJS_Context*» CJS_Runtime::GetCurrentContext() | 222 IFXJS_Context* CJS_Runtime::GetCurrentContext() |
| 230 { | 223 { |
| 231 » if(!m_ContextArray.GetSize()) | 224 if(!m_ContextArray.GetSize()) |
| 232 » » return NULL; | 225 return NULL; |
| 233 » return m_ContextArray.GetAt(m_ContextArray.GetSize()-1); | 226 return m_ContextArray.GetAt(m_ContextArray.GetSize()-1); |
| 234 } | 227 } |
| 235 | 228 |
| 236 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) | 229 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) |
| 237 { | 230 { |
| 238 if (m_pDocument != pReaderDoc) | 231 if (m_pDocument != pReaderDoc) |
| 239 { | 232 { |
| 240 v8::Isolate::Scope isolate_scope(m_isolate); | 233 v8::Isolate::Scope isolate_scope(m_isolate); |
| 241 v8::Locker locker(m_isolate); | 234 v8::Locker locker(m_isolate); |
| 242 v8::HandleScope handle_scope(m_isolate); | 235 v8::HandleScope handle_scope(m_isolate); |
| 243 v8::Local<v8::Context> context =v8::Local<v8::Context>::New(m_is
olate, m_context); | 236 v8::Local<v8::Context> context =v8::Local<v8::Context>::New(m_is
olate, m_context); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 261 } | 254 } |
| 262 JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"Document")
); | 255 JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"Document")
); |
| 263 } | 256 } |
| 264 else | 257 else |
| 265 { | 258 { |
| 266 JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"app")); | 259 JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"app")); |
| 267 } | 260 } |
| 268 } | 261 } |
| 269 } | 262 } |
| 270 | 263 |
| 271 FX_BOOL»CJS_Runtime::AddEventToLoop(const CFX_WideString& sTargetName, JS_EVENT_
T eEventType) | 264 FX_BOOL CJS_Runtime::AddEventToLoop(const CFX_WideString& sTargetName, JS_EVENT_
T eEventType) |
| 272 { | 265 { |
| 273 » if (m_pFieldEventPath == NULL) | 266 if (m_pFieldEventPath == NULL) |
| 274 » { | 267 { |
| 275 » » m_pFieldEventPath = new CJS_FieldEvent; | 268 m_pFieldEventPath = new CJS_FieldEvent; |
| 276 » » m_pFieldEventPath->sTargetName = sTargetName; | 269 m_pFieldEventPath->sTargetName = sTargetName; |
| 277 » » m_pFieldEventPath->eEventType = eEventType; | 270 m_pFieldEventPath->eEventType = eEventType; |
| 278 » » m_pFieldEventPath->pNext = NULL; | 271 m_pFieldEventPath->pNext = NULL; |
| 279 | 272 |
| 280 » » return TRUE; | 273 return TRUE; |
| 281 » } | 274 } |
| 282 | 275 |
| 283 » //to search | 276 //to search |
| 284 » CJS_FieldEvent* p = m_pFieldEventPath; | 277 CJS_FieldEvent* p = m_pFieldEventPath; |
| 285 » CJS_FieldEvent* pLast = m_pFieldEventPath; | 278 CJS_FieldEvent* pLast = m_pFieldEventPath; |
| 286 » while (p) | 279 while (p) |
| 287 » { | 280 { |
| 288 » » if (p->eEventType == eEventType && p->sTargetName == sTargetName
) | 281 if (p->eEventType == eEventType && p->sTargetName == sTargetName) |
| 289 » » » return FALSE; | 282 return FALSE; |
| 290 | 283 |
| 291 » » pLast = p; | 284 pLast = p; |
| 292 » » p = p->pNext; | 285 p = p->pNext; |
| 293 » } | 286 } |
| 294 | 287 |
| 295 » //to add | 288 //to add |
| 296 » CJS_FieldEvent* pNew = new CJS_FieldEvent; | 289 CJS_FieldEvent* pNew = new CJS_FieldEvent; |
| 297 » pNew->sTargetName = sTargetName; | 290 pNew->sTargetName = sTargetName; |
| 298 » pNew->eEventType = eEventType; | 291 pNew->eEventType = eEventType; |
| 299 » pNew->pNext = NULL; | 292 pNew->pNext = NULL; |
| 300 | 293 |
| 301 » pLast->pNext = pNew; | 294 pLast->pNext = pNew; |
| 302 | 295 |
| 303 » return TRUE; | 296 return TRUE; |
| 304 } | 297 } |
| 305 | 298 |
| 306 void CJS_Runtime::RemoveEventInLoop(const CFX_WideString& sTargetName, JS_EVENT_
T eEventType) | 299 void CJS_Runtime::RemoveEventInLoop(const CFX_WideString& sTargetName, JS_EVENT_
T eEventType) |
| 307 { | 300 { |
| 308 » FX_BOOL bFind = FALSE; | 301 FX_BOOL bFind = FALSE; |
| 309 | 302 |
| 310 » CJS_FieldEvent* p = m_pFieldEventPath; | 303 CJS_FieldEvent* p = m_pFieldEventPath; |
| 311 » CJS_FieldEvent* pLast = NULL; | 304 CJS_FieldEvent* pLast = NULL; |
| 312 » while (p) | 305 while (p) |
| 313 » { | 306 { |
| 314 » » if (p->eEventType == eEventType && p->sTargetName == sTargetName
) | 307 if (p->eEventType == eEventType && p->sTargetName == sTargetName) |
| 315 » » { | 308 { |
| 316 » » » bFind = TRUE; | 309 bFind = TRUE; |
| 317 » » » break; | 310 break; |
| 318 » » } | 311 } |
| 319 | 312 |
| 320 » » pLast = p; | 313 pLast = p; |
| 321 » » p = p->pNext; | 314 p = p->pNext; |
| 322 » } | 315 } |
| 323 | 316 |
| 324 » if (bFind) | 317 if (bFind) |
| 325 » { | 318 { |
| 326 » » RemoveEventsInLoop(p); | 319 RemoveEventsInLoop(p); |
| 327 | 320 |
| 328 » » if (p == m_pFieldEventPath) | 321 if (p == m_pFieldEventPath) |
| 329 » » » m_pFieldEventPath = NULL; | 322 m_pFieldEventPath = NULL; |
| 330 | 323 |
| 331 » » if (pLast) | 324 if (pLast) |
| 332 » » » pLast->pNext = NULL; | 325 pLast->pNext = NULL; |
| 333 » } | 326 } |
| 334 } | 327 } |
| 335 | 328 |
| 336 void CJS_Runtime::RemoveEventsInLoop(CJS_FieldEvent* pStart) | 329 void CJS_Runtime::RemoveEventsInLoop(CJS_FieldEvent* pStart) |
| 337 { | 330 { |
| 338 » CJS_FieldEvent* p = pStart; | 331 CJS_FieldEvent* p = pStart; |
| 339 | 332 |
| 340 » while (p) | 333 while (p) |
| 341 » { | 334 { |
| 342 » » CJS_FieldEvent* pOld = p; | 335 CJS_FieldEvent* pOld = p; |
| 343 » » p = pOld->pNext; | 336 p = pOld->pNext; |
| 344 | 337 |
| 345 » » delete pOld; | 338 delete pOld; |
| 346 » } | 339 } |
| 347 } | 340 } |
| 348 | 341 |
| 349 v8::Local<v8::Context>» CJS_Runtime::NewJSContext() | 342 v8::Local<v8::Context> CJS_Runtime::NewJSContext() |
| 350 { | 343 { |
| 351 » return v8::Local<v8::Context>::New(m_isolate, m_context); | 344 return v8::Local<v8::Context>::New(m_isolate, m_context); |
| 352 } | 345 } |
| 353 | 346 |
| 354 CFX_WideString ChangeObjName(const CFX_WideString& str) | 347 CFX_WideString ChangeObjName(const CFX_WideString& str) |
| 355 { | 348 { |
| 356 » CFX_WideString sRet = str; | 349 CFX_WideString sRet = str; |
| 357 » sRet.Replace(L"_", L"."); | 350 sRet.Replace(L"_", L"."); |
| 358 » return sRet; | 351 return sRet; |
| 359 } | 352 } |
| 360 FX_BOOL CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name, FXJSE_HVAL
UE hValue) | 353 FX_BOOL CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name, FXJSE_HVAL
UE hValue) |
| 361 { | 354 { |
| 362 const FX_CHAR* name = utf8Name.GetCStr(); | 355 const FX_CHAR* name = utf8Name.GetCStr(); |
| 363 | 356 |
| 364 v8::Locker lock(GetIsolate()); | 357 v8::Locker lock(GetIsolate()); |
| 365 v8::Isolate::Scope isolate_scope(GetIsolate()); | 358 v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 366 v8::HandleScope handle_scope(GetIsolate()); | 359 v8::HandleScope handle_scope(GetIsolate()); |
| 367 v8::Local<v8::Context> context = | 360 v8::Local<v8::Context> context = |
| 368 v8::Local<v8::Context>::New(GetIsolate(), m_context); | 361 v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 392 v8::Local<v8::Context> context = | 385 v8::Local<v8::Context> context = |
| 393 v8::Local<v8::Context>::New(pIsolate, m_context); | 386 v8::Local<v8::Context>::New(pIsolate, m_context); |
| 394 v8::Context::Scope context_scope(context); | 387 v8::Context::Scope context_scope(context); |
| 395 | 388 |
| 396 //v8::Local<v8::Context> tmpCotext = v8::Local<v8::Context>::New(GetIsol
ate(), m_context); | 389 //v8::Local<v8::Context> tmpCotext = v8::Local<v8::Context>::New(GetIsol
ate(), m_context); |
| 397 v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New(GetIsolate(),
((CFXJSE_Value*)hValue)->DirectGetValue()); | 390 v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New(GetIsolate(),
((CFXJSE_Value*)hValue)->DirectGetValue()); |
| 398 context->Global()->Set(v8::String::NewFromUtf8(pIsolate, name, v8::Strin
g::kNormalString, utf8Name.GetLength()), propvalue); | 391 context->Global()->Set(v8::String::NewFromUtf8(pIsolate, name, v8::Strin
g::kNormalString, utf8Name.GetLength()), propvalue); |
| 399 | 392 |
| 400 return TRUE; | 393 return TRUE; |
| 401 } | 394 } |
| OLD | NEW |