| 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 "xfa/fxfa/parser/xfa_script_imp.h" | 7 #include "xfa/fxfa/parser/xfa_script_imp.h" |
| 8 | 8 |
| 9 #include "core/fxcrt/include/fx_ext.h" | 9 #include "core/fxcrt/include/fx_ext.h" |
| 10 #include "xfa/fxfa/app/xfa_ffnotify.h" | 10 #include "xfa/fxfa/app/xfa_ffnotify.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 m_mapXFAToHValue.RemoveAll(); | 46 m_mapXFAToHValue.RemoveAll(); |
| 47 ReleaseVariablesMap(); | 47 ReleaseVariablesMap(); |
| 48 if (m_hFM2JSContext) { | 48 if (m_hFM2JSContext) { |
| 49 XFA_FM2JS_ContextRelease(m_hFM2JSContext); | 49 XFA_FM2JS_ContextRelease(m_hFM2JSContext); |
| 50 m_hFM2JSContext = NULL; | 50 m_hFM2JSContext = NULL; |
| 51 } | 51 } |
| 52 if (m_hJsContext) { | 52 if (m_hJsContext) { |
| 53 FXJSE_Context_Release(m_hJsContext); | 53 FXJSE_Context_Release(m_hJsContext); |
| 54 m_hJsContext = NULL; | 54 m_hJsContext = NULL; |
| 55 } | 55 } |
| 56 if (m_pResolveProcessor) { | 56 delete m_pResolveProcessor; |
| 57 delete m_pResolveProcessor; | |
| 58 m_pResolveProcessor = NULL; | |
| 59 } | |
| 60 m_upObjectArray.RemoveAll(); | 57 m_upObjectArray.RemoveAll(); |
| 61 for (int32_t i = 0; i < m_CacheListArray.GetSize(); i++) | 58 for (int32_t i = 0; i < m_CacheListArray.GetSize(); i++) |
| 62 delete m_CacheListArray[i]; | 59 delete m_CacheListArray[i]; |
| 63 } | 60 } |
| 64 void CXFA_ScriptContext::Initialize(FXJSE_HRUNTIME hRuntime) { | 61 void CXFA_ScriptContext::Initialize(FXJSE_HRUNTIME hRuntime) { |
| 65 m_hJsRuntime = hRuntime; | 62 m_hJsRuntime = hRuntime; |
| 66 DefineJsContext(); | 63 DefineJsContext(); |
| 67 DefineJsClass(); | 64 DefineJsClass(); |
| 68 m_pResolveProcessor = new CXFA_ResolveProcessor; | 65 m_pResolveProcessor = new CXFA_ResolveProcessor; |
| 69 } | 66 } |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 m_JsGlobalClass.dynMethodCall = CXFA_ScriptContext::NormalMethodCall; | 388 m_JsGlobalClass.dynMethodCall = CXFA_ScriptContext::NormalMethodCall; |
| 392 m_hJsContext = FXJSE_Context_Create(m_hJsRuntime, &m_JsGlobalClass, | 389 m_hJsContext = FXJSE_Context_Create(m_hJsRuntime, &m_JsGlobalClass, |
| 393 m_pDocument->GetRoot()); | 390 m_pDocument->GetRoot()); |
| 394 RemoveBuiltInObjs(m_hJsContext); | 391 RemoveBuiltInObjs(m_hJsContext); |
| 395 FXJSE_Context_EnableCompatibleMode( | 392 FXJSE_Context_EnableCompatibleMode( |
| 396 m_hJsContext, FXJSE_COMPATIBLEMODEFLAG_CONSTRUCTOREXTRAMETHODS); | 393 m_hJsContext, FXJSE_COMPATIBLEMODEFLAG_CONSTRUCTOREXTRAMETHODS); |
| 397 } | 394 } |
| 398 FXJSE_HCONTEXT CXFA_ScriptContext::CreateVariablesContext( | 395 FXJSE_HCONTEXT CXFA_ScriptContext::CreateVariablesContext( |
| 399 CXFA_Node* pScriptNode, | 396 CXFA_Node* pScriptNode, |
| 400 CXFA_Node* pSubform) { | 397 CXFA_Node* pSubform) { |
| 401 if (pScriptNode == NULL || pSubform == NULL) { | 398 if (!pScriptNode || !pSubform) |
| 402 return NULL; | 399 return nullptr; |
| 403 } | 400 |
| 404 if (m_mapVariableToHValue.GetCount() == 0) { | 401 if (m_mapVariableToHValue.GetCount() == 0) { |
| 405 m_JsGlobalVariablesClass.constructor = NULL; | 402 m_JsGlobalVariablesClass.constructor = nullptr; |
| 406 m_JsGlobalVariablesClass.name = "XFAScriptObject"; | 403 m_JsGlobalVariablesClass.name = "XFAScriptObject"; |
| 407 m_JsGlobalVariablesClass.propNum = 0; | 404 m_JsGlobalVariablesClass.propNum = 0; |
| 408 m_JsGlobalVariablesClass.properties = NULL; | 405 m_JsGlobalVariablesClass.properties = nullptr; |
| 409 m_JsGlobalVariablesClass.methNum = 0; | 406 m_JsGlobalVariablesClass.methNum = 0; |
| 410 m_JsGlobalVariablesClass.methods = NULL; | 407 m_JsGlobalVariablesClass.methods = nullptr; |
| 411 m_JsGlobalVariablesClass.dynPropGetter = | 408 m_JsGlobalVariablesClass.dynPropGetter = |
| 412 CXFA_ScriptContext::GlobalPropertyGetter; | 409 CXFA_ScriptContext::GlobalPropertyGetter; |
| 413 m_JsGlobalVariablesClass.dynPropSetter = | 410 m_JsGlobalVariablesClass.dynPropSetter = |
| 414 CXFA_ScriptContext::GlobalPropertySetter; | 411 CXFA_ScriptContext::GlobalPropertySetter; |
| 415 m_JsGlobalVariablesClass.dynPropTypeGetter = | 412 m_JsGlobalVariablesClass.dynPropTypeGetter = |
| 416 CXFA_ScriptContext::NormalPropTypeGetter; | 413 CXFA_ScriptContext::NormalPropTypeGetter; |
| 417 m_JsGlobalVariablesClass.dynPropDeleter = NULL; | 414 m_JsGlobalVariablesClass.dynPropDeleter = nullptr; |
| 418 m_JsGlobalVariablesClass.dynMethodCall = | 415 m_JsGlobalVariablesClass.dynMethodCall = |
| 419 CXFA_ScriptContext::NormalMethodCall; | 416 CXFA_ScriptContext::NormalMethodCall; |
| 420 } | 417 } |
| 421 CXFA_ThisProxy* lpVariableNode = new CXFA_ThisProxy(pSubform, pScriptNode); | 418 FXJSE_HCONTEXT hVariablesContext = |
| 422 FXJSE_HCONTEXT hVariablesContext = FXJSE_Context_Create( | 419 FXJSE_Context_Create(m_hJsRuntime, &m_JsGlobalVariablesClass, |
| 423 m_hJsRuntime, &m_JsGlobalVariablesClass, (CXFA_Object*)lpVariableNode); | 420 new CXFA_ThisProxy(pSubform, pScriptNode)); |
| 424 RemoveBuiltInObjs(hVariablesContext); | 421 RemoveBuiltInObjs(hVariablesContext); |
| 425 FXJSE_Context_EnableCompatibleMode( | 422 FXJSE_Context_EnableCompatibleMode( |
| 426 hVariablesContext, FXJSE_COMPATIBLEMODEFLAG_CONSTRUCTOREXTRAMETHODS); | 423 hVariablesContext, FXJSE_COMPATIBLEMODEFLAG_CONSTRUCTOREXTRAMETHODS); |
| 427 m_mapVariableToHValue.SetAt(pScriptNode, hVariablesContext); | 424 m_mapVariableToHValue.SetAt(pScriptNode, hVariablesContext); |
| 428 return hVariablesContext; | 425 return hVariablesContext; |
| 429 } | 426 } |
| 430 CXFA_Object* CXFA_ScriptContext::GetVariablesThis(CXFA_Object* pObject, | 427 CXFA_Object* CXFA_ScriptContext::GetVariablesThis(CXFA_Object* pObject, |
| 431 FX_BOOL bScriptNode) { | 428 FX_BOOL bScriptNode) { |
| 432 if (pObject->GetObjectType() == XFA_OBJECTTYPE_VariablesThis) { | 429 if (pObject->GetObjectType() == XFA_OBJECTTYPE_VariablesThis) { |
| 433 return bScriptNode ? ((CXFA_ThisProxy*)pObject)->GetScriptNode() | 430 return bScriptNode ? ((CXFA_ThisProxy*)pObject)->GetScriptNode() |
| 434 : ((CXFA_ThisProxy*)pObject)->GetThisNode(); | 431 : ((CXFA_ThisProxy*)pObject)->GetThisNode(); |
| 435 } | 432 } |
| 436 return pObject; | 433 return pObject; |
| 437 } | 434 } |
| 435 |
| 438 FX_BOOL CXFA_ScriptContext::RunVariablesScript(CXFA_Node* pScriptNode) { | 436 FX_BOOL CXFA_ScriptContext::RunVariablesScript(CXFA_Node* pScriptNode) { |
| 439 if (pScriptNode == NULL) { | 437 if (!pScriptNode) |
| 440 return FALSE; | 438 return FALSE; |
| 441 } | 439 |
| 442 if (pScriptNode->GetClassID() == XFA_ELEMENT_Script) { | 440 if (pScriptNode->GetClassID() != XFA_ELEMENT_Script) |
| 443 CXFA_Node* pParent = pScriptNode->GetNodeItem(XFA_NODEITEM_Parent); | 441 return TRUE; |
| 444 if (!pParent || pParent->GetClassID() != XFA_ELEMENT_Variables) { | 442 |
| 445 return FALSE; | 443 CXFA_Node* pParent = pScriptNode->GetNodeItem(XFA_NODEITEM_Parent); |
| 446 } | 444 if (!pParent || pParent->GetClassID() != XFA_ELEMENT_Variables) |
| 447 if (m_mapVariableToHValue.GetValueAt(pScriptNode)) { | 445 return FALSE; |
| 448 return TRUE; | 446 |
| 449 } | 447 if (m_mapVariableToHValue.GetValueAt(pScriptNode)) |
| 450 CXFA_Node* pTextNode = pScriptNode->GetNodeItem(XFA_NODEITEM_FirstChild); | 448 return TRUE; |
| 451 if (!pTextNode) { | 449 |
| 452 return FALSE; | 450 CXFA_Node* pTextNode = pScriptNode->GetNodeItem(XFA_NODEITEM_FirstChild); |
| 453 } | 451 if (!pTextNode) |
| 454 CFX_WideStringC wsScript; | 452 return FALSE; |
| 455 if (!pTextNode->TryCData(XFA_ATTRIBUTE_Value, wsScript)) { | 453 |
| 456 return FALSE; | 454 CFX_WideStringC wsScript; |
| 457 } | 455 if (!pTextNode->TryCData(XFA_ATTRIBUTE_Value, wsScript)) |
| 458 CFX_ByteString btScript = | 456 return FALSE; |
| 459 FX_UTF8Encode(wsScript.c_str(), wsScript.GetLength()); | 457 |
| 460 FXJSE_HVALUE hRetValue = FXJSE_Value_Create(m_hJsRuntime); | 458 CFX_ByteString btScript = |
| 461 CXFA_Node* pThisObject = pParent->GetNodeItem(XFA_NODEITEM_Parent); | 459 FX_UTF8Encode(wsScript.c_str(), wsScript.GetLength()); |
| 462 FXJSE_HCONTEXT hVariablesContext = | 460 FXJSE_HVALUE hRetValue = FXJSE_Value_Create(m_hJsRuntime); |
| 463 CreateVariablesContext(pScriptNode, pThisObject); | 461 CXFA_Node* pThisObject = pParent->GetNodeItem(XFA_NODEITEM_Parent); |
| 464 CXFA_Object* pOriginalObject = m_pThisObject; | 462 FXJSE_HCONTEXT hVariablesContext = |
| 465 m_pThisObject = pThisObject; | 463 CreateVariablesContext(pScriptNode, pThisObject); |
| 466 FX_BOOL bRet = | 464 CXFA_Object* pOriginalObject = m_pThisObject; |
| 467 FXJSE_ExecuteScript(hVariablesContext, btScript.c_str(), hRetValue); | 465 m_pThisObject = pThisObject; |
| 468 m_pThisObject = pOriginalObject; | 466 FX_BOOL bRet = |
| 469 FXJSE_Value_Release(hRetValue); | 467 FXJSE_ExecuteScript(hVariablesContext, btScript.c_str(), hRetValue); |
| 470 return bRet; | 468 m_pThisObject = pOriginalObject; |
| 471 } | 469 FXJSE_Value_Release(hRetValue); |
| 472 return TRUE; | 470 return bRet; |
| 473 } | 471 } |
| 472 |
| 474 FX_BOOL CXFA_ScriptContext::QueryVariableHValue( | 473 FX_BOOL CXFA_ScriptContext::QueryVariableHValue( |
| 475 CXFA_Node* pScriptNode, | 474 CXFA_Node* pScriptNode, |
| 476 const CFX_ByteStringC& szPropName, | 475 const CFX_ByteStringC& szPropName, |
| 477 FXJSE_HVALUE hValue, | 476 FXJSE_HVALUE hValue, |
| 478 FX_BOOL bGetter) { | 477 FX_BOOL bGetter) { |
| 479 if (!pScriptNode || pScriptNode->GetClassID() != XFA_ELEMENT_Script) { | 478 if (!pScriptNode || pScriptNode->GetClassID() != XFA_ELEMENT_Script) |
| 480 return FALSE; | 479 return FALSE; |
| 480 |
| 481 CXFA_Node* variablesNode = pScriptNode->GetNodeItem(XFA_NODEITEM_Parent); |
| 482 if (!variablesNode || variablesNode->GetClassID() != XFA_ELEMENT_Variables) |
| 483 return FALSE; |
| 484 |
| 485 void* lpVariables = m_mapVariableToHValue.GetValueAt(pScriptNode); |
| 486 if (!lpVariables) |
| 487 return FALSE; |
| 488 |
| 489 FX_BOOL bRes = FALSE; |
| 490 FXJSE_HCONTEXT hVariableContext = (FXJSE_HCONTEXT)lpVariables; |
| 491 FXJSE_HVALUE hObject = FXJSE_Context_GetGlobalObject(hVariableContext); |
| 492 FXJSE_HVALUE hVariableValue = FXJSE_Value_Create(m_hJsRuntime); |
| 493 if (!bGetter) { |
| 494 FXJSE_Value_SetObjectOwnProp(hObject, szPropName, hValue); |
| 495 bRes = TRUE; |
| 496 } else if (FXJSE_Value_ObjectHasOwnProp(hObject, szPropName, FALSE)) { |
| 497 FXJSE_Value_GetObjectProp(hObject, szPropName, hVariableValue); |
| 498 if (FXJSE_Value_IsFunction(hVariableValue)) |
| 499 FXJSE_Value_SetFunctionBind(hValue, hVariableValue, hObject); |
| 500 else if (bGetter) |
| 501 FXJSE_Value_Set(hValue, hVariableValue); |
| 502 else |
| 503 FXJSE_Value_Set(hVariableValue, hValue); |
| 504 bRes = TRUE; |
| 481 } | 505 } |
| 482 CXFA_Node* variablesNode = pScriptNode->GetNodeItem(XFA_NODEITEM_Parent); | 506 FXJSE_Value_Release(hVariableValue); |
| 483 if (!variablesNode || variablesNode->GetClassID() != XFA_ELEMENT_Variables) { | 507 FXJSE_Value_Release(hObject); |
| 484 return FALSE; | |
| 485 } | |
| 486 FX_BOOL bRes = FALSE; | |
| 487 void* lpVariables = m_mapVariableToHValue.GetValueAt(pScriptNode); | |
| 488 if (lpVariables) { | |
| 489 FXJSE_HCONTEXT hVariableContext = (FXJSE_HCONTEXT)lpVariables; | |
| 490 FXJSE_HVALUE hObject = FXJSE_Context_GetGlobalObject(hVariableContext); | |
| 491 FXJSE_HVALUE hVariableValue = FXJSE_Value_Create(m_hJsRuntime); | |
| 492 if (!bGetter) { | |
| 493 FXJSE_Value_SetObjectOwnProp(hObject, szPropName, hValue); | |
| 494 bRes = TRUE; | |
| 495 } else if (FXJSE_Value_ObjectHasOwnProp(hObject, szPropName, FALSE)) { | |
| 496 FXJSE_Value_GetObjectProp(hObject, szPropName, hVariableValue); | |
| 497 if (FXJSE_Value_IsFunction(hVariableValue)) { | |
| 498 FXJSE_Value_SetFunctionBind(hValue, hVariableValue, hObject); | |
| 499 } else if (bGetter) { | |
| 500 FXJSE_Value_Set(hValue, hVariableValue); | |
| 501 } else { | |
| 502 FXJSE_Value_Set(hVariableValue, hValue); | |
| 503 } | |
| 504 bRes = TRUE; | |
| 505 } | |
| 506 FXJSE_Value_Release(hVariableValue); | |
| 507 FXJSE_Value_Release(hObject); | |
| 508 } | |
| 509 return bRes; | 508 return bRes; |
| 510 } | 509 } |
| 510 |
| 511 void CXFA_ScriptContext::ReleaseVariablesMap() { | 511 void CXFA_ScriptContext::ReleaseVariablesMap() { |
| 512 FX_POSITION ps = m_mapVariableToHValue.GetStartPosition(); | 512 FX_POSITION ps = m_mapVariableToHValue.GetStartPosition(); |
| 513 while (ps) { | 513 while (ps) { |
| 514 CXFA_Object* pScriptNode; | 514 CXFA_Object* pScriptNode; |
| 515 FXJSE_HCONTEXT hVariableContext; | 515 FXJSE_HCONTEXT hVariableContext = nullptr; |
| 516 m_mapVariableToHValue.GetNextAssoc(ps, pScriptNode, hVariableContext); | 516 m_mapVariableToHValue.GetNextAssoc(ps, pScriptNode, hVariableContext); |
| 517 FXJSE_HVALUE hObject = FXJSE_Context_GetGlobalObject(hVariableContext); | 517 FXJSE_HVALUE hObject = FXJSE_Context_GetGlobalObject(hVariableContext); |
| 518 CXFA_Object* lpCurNode = (CXFA_Object*)FXJSE_Value_ToObject(hObject, NULL); | 518 delete static_cast<CXFA_ThisProxy*>(FXJSE_Value_ToObject(hObject, nullptr)); |
| 519 if (lpCurNode) { | |
| 520 delete (CXFA_ThisProxy*)lpCurNode; | |
| 521 lpCurNode = NULL; | |
| 522 } | |
| 523 FXJSE_Value_Release(hObject); | 519 FXJSE_Value_Release(hObject); |
| 524 FXJSE_Context_Release(hVariableContext); | 520 FXJSE_Context_Release(hVariableContext); |
| 525 hVariableContext = NULL; | |
| 526 } | 521 } |
| 527 m_mapVariableToHValue.RemoveAll(); | 522 m_mapVariableToHValue.RemoveAll(); |
| 528 } | 523 } |
| 524 |
| 529 void CXFA_ScriptContext::DefineJsClass() { | 525 void CXFA_ScriptContext::DefineJsClass() { |
| 530 m_JsNormalClass.constructor = NULL; | 526 m_JsNormalClass.constructor = NULL; |
| 531 m_JsNormalClass.name = "XFAObject"; | 527 m_JsNormalClass.name = "XFAObject"; |
| 532 m_JsNormalClass.propNum = 0; | 528 m_JsNormalClass.propNum = 0; |
| 533 m_JsNormalClass.properties = NULL; | 529 m_JsNormalClass.properties = NULL; |
| 534 m_JsNormalClass.methNum = 0; | 530 m_JsNormalClass.methNum = 0; |
| 535 m_JsNormalClass.methods = NULL; | 531 m_JsNormalClass.methods = NULL; |
| 536 m_JsNormalClass.dynPropGetter = CXFA_ScriptContext::NormalPropertyGetter; | 532 m_JsNormalClass.dynPropGetter = CXFA_ScriptContext::NormalPropertyGetter; |
| 537 m_JsNormalClass.dynPropSetter = CXFA_ScriptContext::NormalPropertySetter; | 533 m_JsNormalClass.dynPropSetter = CXFA_ScriptContext::NormalPropertySetter; |
| 538 m_JsNormalClass.dynPropTypeGetter = CXFA_ScriptContext::NormalPropTypeGetter; | 534 m_JsNormalClass.dynPropTypeGetter = CXFA_ScriptContext::NormalPropTypeGetter; |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 } | 760 } |
| 765 } | 761 } |
| 766 void CXFA_ScriptContext::AddNodesOfRunScript(CXFA_Node* pNode) { | 762 void CXFA_ScriptContext::AddNodesOfRunScript(CXFA_Node* pNode) { |
| 767 if (!m_pScriptNodeArray) { | 763 if (!m_pScriptNodeArray) { |
| 768 return; | 764 return; |
| 769 } | 765 } |
| 770 if (m_pScriptNodeArray->Find(pNode) == -1) { | 766 if (m_pScriptNodeArray->Find(pNode) == -1) { |
| 771 m_pScriptNodeArray->Add(pNode); | 767 m_pScriptNodeArray->Add(pNode); |
| 772 } | 768 } |
| 773 } | 769 } |
| OLD | NEW |