OLD | NEW |
| (Empty) |
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. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 #include "xfa/src/fxfa/parser/xfa_script_imp.h" | |
8 | |
9 #include "core/include/fxcrt/fx_ext.h" | |
10 #include "xfa/src/fxfa/fm2js/xfa_fm2jsapi.h" | |
11 #include "xfa/src/fxfa/parser/xfa_docdata.h" | |
12 #include "xfa/src/fxfa/parser/xfa_doclayout.h" | |
13 #include "xfa/src/fxfa/parser/xfa_document.h" | |
14 #include "xfa/src/fxfa/parser/xfa_script_nodehelper.h" | |
15 #include "xfa/src/fxfa/parser/xfa_script_resolveprocessor.h" | |
16 #include "xfa/src/fxfa/parser/xfa_localemgr.h" | |
17 #include "xfa/src/fxfa/parser/xfa_object.h" | |
18 #include "xfa/src/fxfa/parser/xfa_parser.h" | |
19 #include "xfa/src/fxfa/parser/xfa_script.h" | |
20 #include "xfa/src/fxfa/parser/xfa_utils.h" | |
21 | |
22 CXFA_ScriptContext::CXFA_ScriptContext(CXFA_Document* pDocument) | |
23 : m_pDocument(pDocument), | |
24 m_hJsContext(nullptr), | |
25 m_hJsRuntime(nullptr), | |
26 m_hJsClass(nullptr), | |
27 m_eScriptType(XFA_SCRIPTLANGTYPE_Unkown), | |
28 m_pScriptNodeArray(nullptr), | |
29 m_pResolveProcessor(nullptr), | |
30 m_hFM2JSContext(nullptr), | |
31 m_pThisObject(nullptr), | |
32 m_dwBuiltInInFlags(0), | |
33 m_eRunAtType(XFA_ATTRIBUTEENUM_Client) { | |
34 FXSYS_memset(&m_JsGlobalClass, 0, sizeof(FXJSE_CLASS)); | |
35 FXSYS_memset(&m_JsNormalClass, 0, sizeof(FXJSE_CLASS)); | |
36 } | |
37 CXFA_ScriptContext::~CXFA_ScriptContext() { | |
38 FX_POSITION ps = m_mapXFAToHValue.GetStartPosition(); | |
39 while (ps) { | |
40 CXFA_Object* pXFAObj; | |
41 FXJSE_HVALUE pValue; | |
42 m_mapXFAToHValue.GetNextAssoc(ps, pXFAObj, pValue); | |
43 FXJSE_Value_Release(pValue); | |
44 } | |
45 m_mapXFAToHValue.RemoveAll(); | |
46 ReleaseVariablesMap(); | |
47 if (m_hFM2JSContext) { | |
48 XFA_FM2JS_ContextRelease(m_hFM2JSContext); | |
49 m_hFM2JSContext = NULL; | |
50 } | |
51 if (m_hJsContext) { | |
52 FXJSE_Context_Release(m_hJsContext); | |
53 m_hJsContext = NULL; | |
54 } | |
55 if (m_pResolveProcessor) { | |
56 delete m_pResolveProcessor; | |
57 m_pResolveProcessor = NULL; | |
58 } | |
59 m_upObjectArray.RemoveAll(); | |
60 for (int32_t i = 0; i < m_CacheListArray.GetSize(); i++) { | |
61 delete ((CXFA_NodeList*)m_CacheListArray[i]); | |
62 } | |
63 m_CacheListArray.RemoveAll(); | |
64 } | |
65 void CXFA_ScriptContext::Initialize(FXJSE_HRUNTIME hRuntime) { | |
66 m_hJsRuntime = hRuntime; | |
67 DefineJsContext(); | |
68 DefineJsClass(); | |
69 m_pResolveProcessor = new CXFA_ResolveProcessor; | |
70 } | |
71 void CXFA_ScriptContext::Release() { | |
72 delete this; | |
73 } | |
74 FX_BOOL CXFA_ScriptContext::RunScript(XFA_SCRIPTLANGTYPE eScriptType, | |
75 const CFX_WideStringC& wsScript, | |
76 FXJSE_HVALUE hRetValue, | |
77 CXFA_Object* pThisObject) { | |
78 CFX_ByteString btScript; | |
79 XFA_SCRIPTLANGTYPE eSaveType = m_eScriptType; | |
80 m_eScriptType = eScriptType; | |
81 if (eScriptType == XFA_SCRIPTLANGTYPE_Formcalc) { | |
82 if (!m_hFM2JSContext) { | |
83 m_hFM2JSContext = XFA_FM2JS_ContextCreate(); | |
84 XFA_FM2JS_ContextInitialize(m_hFM2JSContext, m_hJsRuntime, m_hJsContext, | |
85 m_pDocument); | |
86 } | |
87 CFX_WideTextBuf wsJavaScript; | |
88 CFX_WideString wsErrorInfo; | |
89 int32_t iFlags = XFA_FM2JS_Translate(wsScript, wsJavaScript, wsErrorInfo); | |
90 if (iFlags) { | |
91 FXJSE_Value_SetUndefined(hRetValue); | |
92 return FALSE; | |
93 } | |
94 btScript = | |
95 FX_UTF8Encode(wsJavaScript.GetBuffer(), wsJavaScript.GetLength()); | |
96 } else { | |
97 btScript = FX_UTF8Encode(wsScript.GetPtr(), wsScript.GetLength()); | |
98 } | |
99 CXFA_Object* pOriginalObject = m_pThisObject; | |
100 m_pThisObject = pThisObject; | |
101 FXJSE_HVALUE pValue = pThisObject ? GetJSValueFromMap(pThisObject) : NULL; | |
102 FX_BOOL bRet = FXJSE_ExecuteScript(m_hJsContext, btScript, hRetValue, pValue); | |
103 m_pThisObject = pOriginalObject; | |
104 m_eScriptType = eSaveType; | |
105 return bRet; | |
106 } | |
107 void CXFA_ScriptContext::GlobalPropertySetter(FXJSE_HOBJECT hObject, | |
108 const CFX_ByteStringC& szPropName, | |
109 FXJSE_HVALUE hValue) { | |
110 CXFA_Object* lpOrginalNode = | |
111 (CXFA_Object*)FXJSE_Value_ToObject(hObject, NULL); | |
112 CXFA_Document* pDoc = lpOrginalNode->GetDocument(); | |
113 CXFA_ScriptContext* lpScriptContext = | |
114 (CXFA_ScriptContext*)pDoc->GetScriptContext(); | |
115 CXFA_Object* lpCurNode = lpScriptContext->GetVariablesThis(lpOrginalNode); | |
116 CFX_WideString wsPropName = CFX_WideString::FromUTF8( | |
117 (const FX_CHAR*)szPropName.GetPtr(), szPropName.GetLength()); | |
118 FX_DWORD dwFlag = XFA_RESOLVENODE_Parent | XFA_RESOLVENODE_Siblings | | |
119 XFA_RESOLVENODE_Children | XFA_RESOLVENODE_Properties | | |
120 XFA_RESOLVENODE_Attributes; | |
121 CXFA_Node* pRefNode = ToNode(lpScriptContext->GetThisObject()); | |
122 if (lpOrginalNode->GetObjectType() == XFA_OBJECTTYPE_VariablesThis) { | |
123 pRefNode = ToNode(lpCurNode); | |
124 } | |
125 if (lpScriptContext->QueryNodeByFlag(pRefNode, wsPropName, hValue, dwFlag, | |
126 TRUE)) { | |
127 return; | |
128 } | |
129 if (lpOrginalNode->GetObjectType() == XFA_OBJECTTYPE_VariablesThis) { | |
130 if (FXJSE_Value_IsUndefined(hValue)) { | |
131 FXJSE_Value_SetObjectOwnProp(hObject, szPropName, hValue); | |
132 return; | |
133 } | |
134 } | |
135 IXFA_Notify* pNotify = pDoc->GetNotify(); | |
136 if (!pNotify) { | |
137 return; | |
138 } | |
139 pNotify->GetDocProvider()->SetGlobalProperty(pNotify->GetHDOC(), szPropName, | |
140 hValue); | |
141 } | |
142 FX_BOOL CXFA_ScriptContext::QueryNodeByFlag(CXFA_Node* refNode, | |
143 const CFX_WideStringC& propname, | |
144 FXJSE_HVALUE hValue, | |
145 FX_DWORD dwFlag, | |
146 FX_BOOL bSetting) { | |
147 if (!refNode) | |
148 return false; | |
149 XFA_RESOLVENODE_RS resolveRs; | |
150 if (ResolveObjects(refNode, propname, resolveRs, dwFlag) <= 0) | |
151 return false; | |
152 if (resolveRs.dwFlags == XFA_RESOVENODE_RSTYPE_Nodes) { | |
153 FXJSE_HVALUE pValue = GetJSValueFromMap(resolveRs.nodes[0]); | |
154 FXJSE_Value_Set(hValue, pValue); | |
155 return true; | |
156 } | |
157 if (resolveRs.dwFlags == XFA_RESOVENODE_RSTYPE_Attribute) { | |
158 const XFA_SCRIPTATTRIBUTEINFO* lpAttributeInfo = resolveRs.pScriptAttribute; | |
159 if (lpAttributeInfo) { | |
160 (resolveRs.nodes[0]->*(lpAttributeInfo->lpfnCallback))( | |
161 hValue, bSetting, (XFA_ATTRIBUTE)lpAttributeInfo->eAttribute); | |
162 } | |
163 } | |
164 return true; | |
165 } | |
166 void CXFA_ScriptContext::GlobalPropertyGetter(FXJSE_HOBJECT hObject, | |
167 const CFX_ByteStringC& szPropName, | |
168 FXJSE_HVALUE hValue) { | |
169 CXFA_Object* pOrginalObject = | |
170 (CXFA_Object*)FXJSE_Value_ToObject(hObject, NULL); | |
171 CXFA_Document* pDoc = pOrginalObject->GetDocument(); | |
172 CXFA_ScriptContext* lpScriptContext = | |
173 (CXFA_ScriptContext*)pDoc->GetScriptContext(); | |
174 CXFA_Object* lpCurNode = lpScriptContext->GetVariablesThis(pOrginalObject); | |
175 CFX_WideString wsPropName = CFX_WideString::FromUTF8( | |
176 (const FX_CHAR*)szPropName.GetPtr(), szPropName.GetLength()); | |
177 if (lpScriptContext->GetType() == XFA_SCRIPTLANGTYPE_Formcalc) { | |
178 if (szPropName == FOXIT_XFA_FM2JS_FORMCALC_RUNTIME) { | |
179 XFA_FM2JS_GlobalPropertyGetter(lpScriptContext->m_hFM2JSContext, hValue); | |
180 return; | |
181 } | |
182 uint32_t uHashCode = | |
183 FX_HashCode_String_GetW(wsPropName, wsPropName.GetLength()); | |
184 if (uHashCode != XFA_HASHCODE_Layout) { | |
185 CXFA_Object* pObject = | |
186 lpScriptContext->GetDocument()->GetXFAObject(uHashCode); | |
187 if (pObject) { | |
188 FXJSE_Value_Set(hValue, lpScriptContext->GetJSValueFromMap(pObject)); | |
189 return; | |
190 } | |
191 } | |
192 } | |
193 FX_DWORD dwFlag = XFA_RESOLVENODE_Children | XFA_RESOLVENODE_Properties | | |
194 XFA_RESOLVENODE_Attributes; | |
195 CXFA_Node* pRefNode = ToNode(lpScriptContext->GetThisObject()); | |
196 if (pOrginalObject->GetObjectType() == XFA_OBJECTTYPE_VariablesThis) { | |
197 pRefNode = ToNode(lpCurNode); | |
198 } | |
199 if (lpScriptContext->QueryNodeByFlag(pRefNode, wsPropName, hValue, dwFlag, | |
200 FALSE)) { | |
201 return; | |
202 } | |
203 dwFlag = XFA_RESOLVENODE_Parent | XFA_RESOLVENODE_Siblings; | |
204 if (lpScriptContext->QueryNodeByFlag(pRefNode, wsPropName, hValue, dwFlag, | |
205 FALSE)) { | |
206 return; | |
207 } | |
208 CXFA_Object* pScriptObject = | |
209 lpScriptContext->GetVariablesThis(pOrginalObject, TRUE); | |
210 if (pScriptObject && | |
211 lpScriptContext->QueryVariableHValue(pScriptObject->AsNode(), szPropName, | |
212 hValue, TRUE)) { | |
213 return; | |
214 } | |
215 IXFA_Notify* pNotify = pDoc->GetNotify(); | |
216 if (!pNotify) { | |
217 return; | |
218 } | |
219 pNotify->GetDocProvider()->GetGlobalProperty(pNotify->GetHDOC(), szPropName, | |
220 hValue); | |
221 } | |
222 void CXFA_ScriptContext::NormalPropertyGetter(FXJSE_HOBJECT hObject, | |
223 const CFX_ByteStringC& szPropName, | |
224 FXJSE_HVALUE hValue) { | |
225 CXFA_Object* pOrginalObject = | |
226 (CXFA_Object*)FXJSE_Value_ToObject(hObject, NULL); | |
227 if (pOrginalObject == NULL) { | |
228 FXJSE_Value_SetUndefined(hValue); | |
229 return; | |
230 } | |
231 CFX_WideString wsPropName = CFX_WideString::FromUTF8( | |
232 (const FX_CHAR*)szPropName.GetPtr(), szPropName.GetLength()); | |
233 CXFA_ScriptContext* lpScriptContext = | |
234 (CXFA_ScriptContext*)pOrginalObject->GetDocument()->GetScriptContext(); | |
235 CXFA_Object* pObject = lpScriptContext->GetVariablesThis(pOrginalObject); | |
236 if (wsPropName == FX_WSTRC(L"xfa")) { | |
237 FXJSE_HVALUE pValue = lpScriptContext->GetJSValueFromMap( | |
238 lpScriptContext->GetDocument()->GetRoot()); | |
239 FXJSE_Value_Set(hValue, pValue); | |
240 return; | |
241 } | |
242 FX_DWORD dwFlag = XFA_RESOLVENODE_Children | XFA_RESOLVENODE_Properties | | |
243 XFA_RESOLVENODE_Attributes; | |
244 FX_BOOL bRet = lpScriptContext->QueryNodeByFlag(ToNode(pObject), wsPropName, | |
245 hValue, dwFlag, FALSE); | |
246 if (bRet) { | |
247 return; | |
248 } | |
249 if (pObject == lpScriptContext->GetThisObject() || | |
250 (lpScriptContext->GetType() == XFA_SCRIPTLANGTYPE_Javascript && | |
251 !lpScriptContext->IsStrictScopeInJavaScript())) { | |
252 dwFlag = XFA_RESOLVENODE_Parent | XFA_RESOLVENODE_Siblings; | |
253 bRet = lpScriptContext->QueryNodeByFlag(ToNode(pObject), wsPropName, hValue, | |
254 dwFlag, FALSE); | |
255 } | |
256 if (bRet) { | |
257 return; | |
258 } | |
259 CXFA_Object* pScriptObject = | |
260 lpScriptContext->GetVariablesThis(pOrginalObject, TRUE); | |
261 if (pScriptObject) { | |
262 bRet = lpScriptContext->QueryVariableHValue(ToNode(pScriptObject), | |
263 szPropName, hValue, TRUE); | |
264 } | |
265 if (!bRet) { | |
266 FXJSE_Value_SetUndefined(hValue); | |
267 } | |
268 } | |
269 void CXFA_ScriptContext::NormalPropertySetter(FXJSE_HOBJECT hObject, | |
270 const CFX_ByteStringC& szPropName, | |
271 FXJSE_HVALUE hValue) { | |
272 CXFA_Object* pOrginalObject = | |
273 (CXFA_Object*)FXJSE_Value_ToObject(hObject, NULL); | |
274 if (pOrginalObject == NULL) { | |
275 return; | |
276 } | |
277 CXFA_ScriptContext* lpScriptContext = | |
278 (CXFA_ScriptContext*)pOrginalObject->GetDocument()->GetScriptContext(); | |
279 CXFA_Object* pObject = lpScriptContext->GetVariablesThis(pOrginalObject); | |
280 CFX_WideString wsPropName = CFX_WideString::FromUTF8( | |
281 (const FX_CHAR*)szPropName.GetPtr(), szPropName.GetLength()); | |
282 const XFA_SCRIPTATTRIBUTEINFO* lpAttributeInfo = | |
283 XFA_GetScriptAttributeByName(pObject->GetClassID(), wsPropName); | |
284 if (lpAttributeInfo) { | |
285 (pObject->*(lpAttributeInfo->lpfnCallback))( | |
286 hValue, TRUE, (XFA_ATTRIBUTE)lpAttributeInfo->eAttribute); | |
287 } else { | |
288 if (pObject->IsNode()) { | |
289 if (wsPropName.GetAt(0) == '#') { | |
290 wsPropName = wsPropName.Right(wsPropName.GetLength() - 1); | |
291 } | |
292 CXFA_Node* pNode = ToNode(pObject); | |
293 CXFA_Node* pPropOrChild = NULL; | |
294 const XFA_ELEMENTINFO* lpElementInfo = XFA_GetElementByName(wsPropName); | |
295 if (lpElementInfo) { | |
296 pPropOrChild = pNode->GetProperty(0, lpElementInfo->eName); | |
297 } else { | |
298 pPropOrChild = pNode->GetFirstChildByName(wsPropName); | |
299 } | |
300 if (pPropOrChild) { | |
301 CFX_WideString wsDefaultName = FX_WSTRC(L"{default}"); | |
302 const XFA_SCRIPTATTRIBUTEINFO* lpAttributeInfo = | |
303 XFA_GetScriptAttributeByName(pPropOrChild->GetClassID(), | |
304 wsDefaultName); | |
305 if (lpAttributeInfo) { | |
306 (pPropOrChild->*(lpAttributeInfo->lpfnCallback))( | |
307 hValue, TRUE, (XFA_ATTRIBUTE)lpAttributeInfo->eAttribute); | |
308 return; | |
309 } | |
310 } | |
311 } | |
312 CXFA_Object* pScriptObject = | |
313 lpScriptContext->GetVariablesThis(pOrginalObject, TRUE); | |
314 if (pScriptObject) { | |
315 lpScriptContext->QueryVariableHValue(ToNode(pScriptObject), szPropName, | |
316 hValue, FALSE); | |
317 } | |
318 } | |
319 } | |
320 int32_t CXFA_ScriptContext::NormalPropTypeGetter( | |
321 FXJSE_HOBJECT hObject, | |
322 const CFX_ByteStringC& szPropName, | |
323 FX_BOOL bQueryIn) { | |
324 CXFA_Object* pObject = (CXFA_Object*)FXJSE_Value_ToObject(hObject, NULL); | |
325 if (pObject == NULL) { | |
326 return FXJSE_ClassPropType_None; | |
327 } | |
328 CXFA_ScriptContext* lpScriptContext = | |
329 (CXFA_ScriptContext*)pObject->GetDocument()->GetScriptContext(); | |
330 pObject = lpScriptContext->GetVariablesThis(pObject); | |
331 XFA_ELEMENT objElement = pObject->GetClassID(); | |
332 CFX_WideString wsPropName = CFX_WideString::FromUTF8( | |
333 (const FX_CHAR*)szPropName.GetPtr(), szPropName.GetLength()); | |
334 if (XFA_GetMethodByName(objElement, wsPropName)) { | |
335 return FXJSE_ClassPropType_Method; | |
336 } | |
337 if (bQueryIn && !XFA_GetScriptAttributeByName(objElement, wsPropName)) { | |
338 return FXJSE_ClassPropType_None; | |
339 } | |
340 return FXJSE_ClassPropType_Property; | |
341 } | |
342 int32_t CXFA_ScriptContext::GlobalPropTypeGetter( | |
343 FXJSE_HOBJECT hObject, | |
344 const CFX_ByteStringC& szPropName, | |
345 FX_BOOL bQueryIn) { | |
346 CXFA_Object* pObject = (CXFA_Object*)FXJSE_Value_ToObject(hObject, NULL); | |
347 if (pObject == NULL) { | |
348 return FXJSE_ClassPropType_None; | |
349 } | |
350 CXFA_ScriptContext* lpScriptContext = | |
351 (CXFA_ScriptContext*)pObject->GetDocument()->GetScriptContext(); | |
352 pObject = lpScriptContext->GetVariablesThis(pObject); | |
353 XFA_ELEMENT objElement = pObject->GetClassID(); | |
354 CFX_WideString wsPropName = CFX_WideString::FromUTF8( | |
355 (const FX_CHAR*)szPropName.GetPtr(), szPropName.GetLength()); | |
356 if (XFA_GetMethodByName(objElement, wsPropName)) { | |
357 return FXJSE_ClassPropType_Method; | |
358 } | |
359 return FXJSE_ClassPropType_Property; | |
360 } | |
361 void CXFA_ScriptContext::NormalMethodCall(FXJSE_HOBJECT hThis, | |
362 const CFX_ByteStringC& szFuncName, | |
363 CFXJSE_Arguments& args) { | |
364 CXFA_Object* pObject = (CXFA_Object*)FXJSE_Value_ToObject(hThis, NULL); | |
365 if (pObject == NULL) { | |
366 return; | |
367 } | |
368 CXFA_ScriptContext* lpScriptContext = | |
369 (CXFA_ScriptContext*)pObject->GetDocument()->GetScriptContext(); | |
370 pObject = lpScriptContext->GetVariablesThis(pObject); | |
371 CFX_WideString wsFunName = CFX_WideString::FromUTF8( | |
372 (const FX_CHAR*)szFuncName.GetPtr(), szFuncName.GetLength()); | |
373 const XFA_METHODINFO* lpMethodInfo = | |
374 XFA_GetMethodByName(pObject->GetClassID(), wsFunName); | |
375 if (NULL == lpMethodInfo) { | |
376 return; | |
377 } | |
378 (pObject->*(lpMethodInfo->lpfnCallback))(&args); | |
379 } | |
380 FX_BOOL CXFA_ScriptContext::IsStrictScopeInJavaScript() { | |
381 return m_pDocument->HasFlag(XFA_DOCFLAG_StrictScoping); | |
382 } | |
383 XFA_SCRIPTLANGTYPE CXFA_ScriptContext::GetType() { | |
384 return m_eScriptType; | |
385 } | |
386 void CXFA_ScriptContext::DefineJsContext() { | |
387 m_JsGlobalClass.constructor = NULL; | |
388 m_JsGlobalClass.name = "Root"; | |
389 m_JsGlobalClass.propNum = 0; | |
390 m_JsGlobalClass.properties = NULL; | |
391 m_JsGlobalClass.methNum = 0; | |
392 m_JsGlobalClass.methods = NULL; | |
393 m_JsGlobalClass.dynPropGetter = CXFA_ScriptContext::GlobalPropertyGetter; | |
394 m_JsGlobalClass.dynPropSetter = CXFA_ScriptContext::GlobalPropertySetter; | |
395 m_JsGlobalClass.dynPropTypeGetter = CXFA_ScriptContext::GlobalPropTypeGetter; | |
396 m_JsGlobalClass.dynPropDeleter = NULL; | |
397 m_JsGlobalClass.dynMethodCall = CXFA_ScriptContext::NormalMethodCall; | |
398 m_hJsContext = FXJSE_Context_Create(m_hJsRuntime, &m_JsGlobalClass, | |
399 m_pDocument->GetRoot()); | |
400 RemoveBuiltInObjs(m_hJsContext); | |
401 FXJSE_Context_EnableCompatibleMode( | |
402 m_hJsContext, FXJSE_COMPATIBLEMODEFLAG_CONSTRUCTOREXTRAMETHODS); | |
403 } | |
404 FXJSE_HCONTEXT CXFA_ScriptContext::CreateVariablesContext( | |
405 CXFA_Node* pScriptNode, | |
406 CXFA_Node* pSubform) { | |
407 if (pScriptNode == NULL || pSubform == NULL) { | |
408 return NULL; | |
409 } | |
410 if (m_mapVariableToHValue.GetCount() == 0) { | |
411 m_JsGlobalVariablesClass.constructor = NULL; | |
412 m_JsGlobalVariablesClass.name = "XFAScriptObject"; | |
413 m_JsGlobalVariablesClass.propNum = 0; | |
414 m_JsGlobalVariablesClass.properties = NULL; | |
415 m_JsGlobalVariablesClass.methNum = 0; | |
416 m_JsGlobalVariablesClass.methods = NULL; | |
417 m_JsGlobalVariablesClass.dynPropGetter = | |
418 CXFA_ScriptContext::GlobalPropertyGetter; | |
419 m_JsGlobalVariablesClass.dynPropSetter = | |
420 CXFA_ScriptContext::GlobalPropertySetter; | |
421 m_JsGlobalVariablesClass.dynPropTypeGetter = | |
422 CXFA_ScriptContext::NormalPropTypeGetter; | |
423 m_JsGlobalVariablesClass.dynPropDeleter = NULL; | |
424 m_JsGlobalVariablesClass.dynMethodCall = | |
425 CXFA_ScriptContext::NormalMethodCall; | |
426 } | |
427 CXFA_ThisProxy* lpVariableNode = new CXFA_ThisProxy(pSubform, pScriptNode); | |
428 FXJSE_HCONTEXT hVariablesContext = FXJSE_Context_Create( | |
429 m_hJsRuntime, &m_JsGlobalVariablesClass, (CXFA_Object*)lpVariableNode); | |
430 RemoveBuiltInObjs(hVariablesContext); | |
431 FXJSE_Context_EnableCompatibleMode( | |
432 hVariablesContext, FXJSE_COMPATIBLEMODEFLAG_CONSTRUCTOREXTRAMETHODS); | |
433 m_mapVariableToHValue.SetAt(pScriptNode, hVariablesContext); | |
434 return hVariablesContext; | |
435 } | |
436 CXFA_Object* CXFA_ScriptContext::GetVariablesThis(CXFA_Object* pObject, | |
437 FX_BOOL bScriptNode) { | |
438 if (pObject->GetObjectType() == XFA_OBJECTTYPE_VariablesThis) { | |
439 return bScriptNode ? ((CXFA_ThisProxy*)pObject)->GetScriptNode() | |
440 : ((CXFA_ThisProxy*)pObject)->GetThisNode(); | |
441 } | |
442 return pObject; | |
443 } | |
444 FX_BOOL CXFA_ScriptContext::RunVariablesScript(CXFA_Node* pScriptNode) { | |
445 if (pScriptNode == NULL) { | |
446 return FALSE; | |
447 } | |
448 if (pScriptNode->GetClassID() == XFA_ELEMENT_Script) { | |
449 CXFA_Node* pParent = pScriptNode->GetNodeItem(XFA_NODEITEM_Parent); | |
450 if (!pParent || pParent->GetClassID() != XFA_ELEMENT_Variables) { | |
451 return FALSE; | |
452 } | |
453 if (m_mapVariableToHValue.GetValueAt(pScriptNode)) { | |
454 return TRUE; | |
455 } | |
456 CXFA_Node* pTextNode = pScriptNode->GetNodeItem(XFA_NODEITEM_FirstChild); | |
457 if (!pTextNode) { | |
458 return FALSE; | |
459 } | |
460 CFX_WideStringC wsScript; | |
461 if (!pTextNode->TryCData(XFA_ATTRIBUTE_Value, wsScript)) { | |
462 return FALSE; | |
463 } | |
464 CFX_ByteString btScript = | |
465 FX_UTF8Encode(wsScript.GetPtr(), wsScript.GetLength()); | |
466 FXJSE_HVALUE hRetValue = FXJSE_Value_Create(m_hJsRuntime); | |
467 CXFA_Node* pThisObject = pParent->GetNodeItem(XFA_NODEITEM_Parent); | |
468 FXJSE_HCONTEXT hVariablesContext = | |
469 CreateVariablesContext(pScriptNode, pThisObject); | |
470 CXFA_Object* pOriginalObject = m_pThisObject; | |
471 m_pThisObject = pThisObject; | |
472 FX_BOOL bRet = FXJSE_ExecuteScript(hVariablesContext, btScript, hRetValue); | |
473 m_pThisObject = pOriginalObject; | |
474 FXJSE_Value_Release(hRetValue); | |
475 return bRet; | |
476 } | |
477 return TRUE; | |
478 } | |
479 FX_BOOL CXFA_ScriptContext::QueryVariableHValue( | |
480 CXFA_Node* pScriptNode, | |
481 const CFX_ByteStringC& szPropName, | |
482 FXJSE_HVALUE hValue, | |
483 FX_BOOL bGetter) { | |
484 if (!pScriptNode || pScriptNode->GetClassID() != XFA_ELEMENT_Script) { | |
485 return FALSE; | |
486 } | |
487 CXFA_Node* variablesNode = pScriptNode->GetNodeItem(XFA_NODEITEM_Parent); | |
488 if (!variablesNode || variablesNode->GetClassID() != XFA_ELEMENT_Variables) { | |
489 return FALSE; | |
490 } | |
491 FX_BOOL bRes = FALSE; | |
492 void* lpVariables = m_mapVariableToHValue.GetValueAt(pScriptNode); | |
493 if (lpVariables) { | |
494 FXJSE_HCONTEXT hVariableContext = (FXJSE_HCONTEXT)lpVariables; | |
495 FXJSE_HVALUE hObject = FXJSE_Context_GetGlobalObject(hVariableContext); | |
496 FXJSE_HVALUE hVariableValue = FXJSE_Value_Create(m_hJsRuntime); | |
497 if (!bGetter) { | |
498 FXJSE_Value_SetObjectOwnProp(hObject, szPropName, hValue); | |
499 bRes = TRUE; | |
500 } else if (FXJSE_Value_ObjectHasOwnProp(hObject, szPropName, FALSE)) { | |
501 FXJSE_Value_GetObjectProp(hObject, szPropName, hVariableValue); | |
502 if (FXJSE_Value_IsFunction(hVariableValue)) { | |
503 FXJSE_Value_SetFunctionBind(hValue, hVariableValue, hObject); | |
504 } else if (bGetter) { | |
505 FXJSE_Value_Set(hValue, hVariableValue); | |
506 } else { | |
507 FXJSE_Value_Set(hVariableValue, hValue); | |
508 } | |
509 bRes = TRUE; | |
510 } | |
511 FXJSE_Value_Release(hVariableValue); | |
512 FXJSE_Value_Release(hObject); | |
513 } | |
514 return bRes; | |
515 } | |
516 void CXFA_ScriptContext::ReleaseVariablesMap() { | |
517 FX_POSITION ps = m_mapVariableToHValue.GetStartPosition(); | |
518 while (ps) { | |
519 CXFA_Object* pScriptNode; | |
520 FXJSE_HCONTEXT hVariableContext; | |
521 m_mapVariableToHValue.GetNextAssoc(ps, pScriptNode, hVariableContext); | |
522 FXJSE_HVALUE hObject = FXJSE_Context_GetGlobalObject(hVariableContext); | |
523 CXFA_Object* lpCurNode = (CXFA_Object*)FXJSE_Value_ToObject(hObject, NULL); | |
524 if (lpCurNode) { | |
525 delete (CXFA_ThisProxy*)lpCurNode; | |
526 lpCurNode = NULL; | |
527 } | |
528 FXJSE_Value_Release(hObject); | |
529 FXJSE_Context_Release(hVariableContext); | |
530 hVariableContext = NULL; | |
531 } | |
532 m_mapVariableToHValue.RemoveAll(); | |
533 } | |
534 void CXFA_ScriptContext::DefineJsClass() { | |
535 m_JsNormalClass.constructor = NULL; | |
536 m_JsNormalClass.name = "XFAObject"; | |
537 m_JsNormalClass.propNum = 0; | |
538 m_JsNormalClass.properties = NULL; | |
539 m_JsNormalClass.methNum = 0; | |
540 m_JsNormalClass.methods = NULL; | |
541 m_JsNormalClass.dynPropGetter = CXFA_ScriptContext::NormalPropertyGetter; | |
542 m_JsNormalClass.dynPropSetter = CXFA_ScriptContext::NormalPropertySetter; | |
543 m_JsNormalClass.dynPropTypeGetter = CXFA_ScriptContext::NormalPropTypeGetter; | |
544 m_JsNormalClass.dynPropDeleter = NULL; | |
545 m_JsNormalClass.dynMethodCall = CXFA_ScriptContext::NormalMethodCall; | |
546 m_hJsClass = FXJSE_DefineClass(m_hJsContext, &m_JsNormalClass); | |
547 } | |
548 void CXFA_ScriptContext::RemoveBuiltInObjs(FXJSE_HCONTEXT jsContext) const { | |
549 static const CFX_ByteStringC OBJ_NAME[2] = {"Number", "Date"}; | |
550 FXJSE_HVALUE hObject = FXJSE_Context_GetGlobalObject(jsContext); | |
551 FXJSE_HVALUE hProp = FXJSE_Value_Create(m_hJsRuntime); | |
552 for (int i = 0; i < 2; ++i) { | |
553 if (FXJSE_Value_GetObjectProp(hObject, OBJ_NAME[i], hProp)) | |
554 FXJSE_Value_DeleteObjectProp(hObject, OBJ_NAME[i]); | |
555 } | |
556 FXJSE_Value_Release(hProp); | |
557 FXJSE_Value_Release(hObject); | |
558 } | |
559 FXJSE_HCLASS CXFA_ScriptContext::GetJseNormalClass() { | |
560 return m_hJsClass; | |
561 } | |
562 int32_t CXFA_ScriptContext::ResolveObjects(CXFA_Object* refNode, | |
563 const CFX_WideStringC& wsExpression, | |
564 XFA_RESOLVENODE_RS& resolveNodeRS, | |
565 FX_DWORD dwStyles, | |
566 CXFA_Node* bindNode) { | |
567 if (wsExpression.IsEmpty()) { | |
568 return 0; | |
569 } | |
570 if (m_eScriptType != XFA_SCRIPTLANGTYPE_Formcalc || | |
571 (dwStyles & (XFA_RESOLVENODE_Parent | XFA_RESOLVENODE_Siblings))) { | |
572 m_upObjectArray.RemoveAll(); | |
573 } | |
574 if (refNode && refNode->IsNode() && | |
575 (dwStyles & (XFA_RESOLVENODE_Parent | XFA_RESOLVENODE_Siblings))) { | |
576 m_upObjectArray.Add(refNode->AsNode()); | |
577 } | |
578 FX_BOOL bNextCreate = FALSE; | |
579 if (dwStyles & XFA_RESOLVENODE_CreateNode) { | |
580 m_pResolveProcessor->GetNodeHelper()->XFA_SetCreateNodeType(bindNode); | |
581 } | |
582 m_pResolveProcessor->GetNodeHelper()->m_pCreateParent = NULL; | |
583 m_pResolveProcessor->GetNodeHelper()->m_iCurAllStart = -1; | |
584 CXFA_ResolveNodesData rndFind; | |
585 int32_t nStart = 0; | |
586 int32_t nLevel = 0; | |
587 int32_t nRet = -1; | |
588 rndFind.m_pSC = this; | |
589 CXFA_ObjArray findNodes; | |
590 findNodes.Add(refNode ? refNode : m_pDocument->GetRoot()); | |
591 int32_t nNodes = 0; | |
592 while (TRUE) { | |
593 nNodes = findNodes.GetSize(); | |
594 int32_t i = 0; | |
595 rndFind.m_dwStyles = dwStyles; | |
596 m_pResolveProcessor->m_iCurStart = nStart; | |
597 nStart = m_pResolveProcessor->XFA_ResolveNodes_GetFilter(wsExpression, | |
598 nStart, rndFind); | |
599 if (nStart < 1) { | |
600 if ((dwStyles & XFA_RESOLVENODE_CreateNode) && !bNextCreate) { | |
601 CXFA_Node* pDataNode = NULL; | |
602 nStart = m_pResolveProcessor->GetNodeHelper()->m_iCurAllStart; | |
603 if (nStart != -1) { | |
604 pDataNode = m_pDocument->GetNotBindNode(findNodes); | |
605 if (pDataNode) { | |
606 findNodes.RemoveAll(); | |
607 findNodes.Add(pDataNode); | |
608 break; | |
609 } | |
610 } else { | |
611 pDataNode = findNodes[0]->AsNode(); | |
612 findNodes.RemoveAll(); | |
613 findNodes.Add(pDataNode); | |
614 break; | |
615 } | |
616 dwStyles |= XFA_RESOLVENODE_Bind; | |
617 findNodes.RemoveAll(); | |
618 findNodes.Add(m_pResolveProcessor->GetNodeHelper()->m_pAllStartParent); | |
619 continue; | |
620 } else { | |
621 break; | |
622 } | |
623 } | |
624 if (bNextCreate) { | |
625 FX_BOOL bCreate = | |
626 m_pResolveProcessor->GetNodeHelper()->XFA_ResolveNodes_CreateNode( | |
627 rndFind.m_wsName, rndFind.m_wsCondition, | |
628 nStart == wsExpression.GetLength(), this); | |
629 if (bCreate) { | |
630 continue; | |
631 } else { | |
632 break; | |
633 } | |
634 } | |
635 CXFA_ObjArray retNodes; | |
636 while (i < nNodes) { | |
637 FX_BOOL bDataBind = FALSE; | |
638 if (((dwStyles & XFA_RESOLVENODE_Bind) || | |
639 (dwStyles & XFA_RESOLVENODE_CreateNode)) && | |
640 nNodes > 1) { | |
641 CXFA_ResolveNodesData rndBind; | |
642 m_pResolveProcessor->XFA_ResolveNodes_GetFilter(wsExpression, nStart, | |
643 rndBind); | |
644 m_pResolveProcessor->XFA_ResolveNode_SetIndexDataBind( | |
645 rndBind.m_wsCondition, i, nNodes); | |
646 bDataBind = TRUE; | |
647 } | |
648 rndFind.m_CurNode = findNodes[i++]; | |
649 rndFind.m_nLevel = nLevel; | |
650 rndFind.m_dwFlag = XFA_RESOVENODE_RSTYPE_Nodes; | |
651 nRet = m_pResolveProcessor->XFA_ResolveNodes(rndFind); | |
652 if (nRet < 1) { | |
653 continue; | |
654 } | |
655 if (rndFind.m_dwFlag == XFA_RESOVENODE_RSTYPE_Attribute && | |
656 rndFind.m_pScriptAttribute && nStart < wsExpression.GetLength()) { | |
657 FXJSE_HVALUE hValue = FXJSE_Value_Create(m_hJsRuntime); | |
658 (rndFind.m_Nodes[0]->*(rndFind.m_pScriptAttribute->lpfnCallback))( | |
659 hValue, FALSE, | |
660 (XFA_ATTRIBUTE)rndFind.m_pScriptAttribute->eAttribute); | |
661 rndFind.m_Nodes.SetAt(0, | |
662 (CXFA_Object*)FXJSE_Value_ToObject(hValue, NULL)); | |
663 FXJSE_Value_Release(hValue); | |
664 } | |
665 int32_t iSize = m_upObjectArray.GetSize(); | |
666 if (iSize) { | |
667 m_upObjectArray.RemoveAt(iSize - 1); | |
668 } | |
669 retNodes.Append(rndFind.m_Nodes); | |
670 rndFind.m_Nodes.RemoveAll(); | |
671 if (bDataBind) { | |
672 break; | |
673 } | |
674 } | |
675 findNodes.RemoveAll(); | |
676 nNodes = retNodes.GetSize(); | |
677 if (nNodes < 1) { | |
678 if (dwStyles & XFA_RESOLVENODE_CreateNode) { | |
679 bNextCreate = TRUE; | |
680 if (m_pResolveProcessor->GetNodeHelper()->m_pCreateParent == NULL) { | |
681 m_pResolveProcessor->GetNodeHelper()->m_pCreateParent = | |
682 ToNode(rndFind.m_CurNode); | |
683 m_pResolveProcessor->GetNodeHelper()->m_iCreateCount = 1; | |
684 } | |
685 FX_BOOL bCreate = | |
686 m_pResolveProcessor->GetNodeHelper()->XFA_ResolveNodes_CreateNode( | |
687 rndFind.m_wsName, rndFind.m_wsCondition, | |
688 nStart == wsExpression.GetLength(), this); | |
689 if (bCreate) { | |
690 continue; | |
691 } else { | |
692 break; | |
693 } | |
694 } else { | |
695 break; | |
696 } | |
697 } | |
698 findNodes.Copy(retNodes); | |
699 rndFind.m_Nodes.RemoveAll(); | |
700 if (nLevel == 0) { | |
701 dwStyles &= ~(XFA_RESOLVENODE_Parent | XFA_RESOLVENODE_Siblings); | |
702 } | |
703 nLevel++; | |
704 } | |
705 if (!bNextCreate) { | |
706 resolveNodeRS.dwFlags = rndFind.m_dwFlag; | |
707 if (nNodes > 0) { | |
708 resolveNodeRS.nodes.Append(findNodes); | |
709 } | |
710 if (rndFind.m_dwFlag == XFA_RESOVENODE_RSTYPE_Attribute) { | |
711 resolveNodeRS.pScriptAttribute = rndFind.m_pScriptAttribute; | |
712 return 1; | |
713 } | |
714 } | |
715 if (dwStyles & (XFA_RESOLVENODE_CreateNode | XFA_RESOLVENODE_Bind | | |
716 XFA_RESOLVENODE_BindNew)) { | |
717 m_pResolveProcessor->XFA_ResolveNode_SetResultCreateNode( | |
718 resolveNodeRS, rndFind.m_wsCondition); | |
719 if (!bNextCreate && (dwStyles & XFA_RESOLVENODE_CreateNode)) { | |
720 resolveNodeRS.dwFlags = XFA_RESOVENODE_RSTYPE_ExistNodes; | |
721 } | |
722 return resolveNodeRS.nodes.GetSize(); | |
723 } | |
724 return nNodes; | |
725 } | |
726 FXJSE_HVALUE CXFA_ScriptContext::GetJSValueFromMap(CXFA_Object* pObject) { | |
727 if (!pObject) { | |
728 return NULL; | |
729 } | |
730 if (pObject->IsNode()) { | |
731 RunVariablesScript(pObject->AsNode()); | |
732 } | |
733 void* pValue = m_mapXFAToHValue.GetValueAt(pObject); | |
734 if (pValue == NULL) { | |
735 FXJSE_HVALUE jsHvalue = FXJSE_Value_Create(m_hJsRuntime); | |
736 FXJSE_Value_SetObject(jsHvalue, pObject, m_hJsClass); | |
737 m_mapXFAToHValue.SetAt(pObject, jsHvalue); | |
738 pValue = jsHvalue; | |
739 } | |
740 return (FXJSE_HVALUE)pValue; | |
741 } | |
742 int32_t CXFA_ScriptContext::GetIndexByName(CXFA_Node* refNode) { | |
743 CXFA_NodeHelper* lpNodeHelper = m_pResolveProcessor->GetNodeHelper(); | |
744 return lpNodeHelper->XFA_GetIndex(refNode, XFA_LOGIC_Transparent, | |
745 lpNodeHelper->XFA_NodeIsProperty(refNode), | |
746 FALSE); | |
747 } | |
748 int32_t CXFA_ScriptContext::GetIndexByClassName(CXFA_Node* refNode) { | |
749 CXFA_NodeHelper* lpNodeHelper = m_pResolveProcessor->GetNodeHelper(); | |
750 return lpNodeHelper->XFA_GetIndex(refNode, XFA_LOGIC_Transparent, | |
751 lpNodeHelper->XFA_NodeIsProperty(refNode), | |
752 TRUE); | |
753 } | |
754 void CXFA_ScriptContext::GetSomExpression(CXFA_Node* refNode, | |
755 CFX_WideString& wsExpression) { | |
756 CXFA_NodeHelper* lpNodeHelper = m_pResolveProcessor->GetNodeHelper(); | |
757 lpNodeHelper->XFA_GetNameExpression(refNode, wsExpression, TRUE, | |
758 XFA_LOGIC_Transparent); | |
759 } | |
760 void CXFA_ScriptContext::SetNodesOfRunScript(CXFA_NodeArray* pArray) { | |
761 m_pScriptNodeArray = pArray; | |
762 } | |
763 void CXFA_ScriptContext::AddNodesOfRunScript(const CXFA_NodeArray& nodes) { | |
764 if (!m_pScriptNodeArray) { | |
765 return; | |
766 } | |
767 if (nodes.GetSize() > 0) { | |
768 m_pScriptNodeArray->Copy(nodes); | |
769 } | |
770 } | |
771 void CXFA_ScriptContext::AddNodesOfRunScript(CXFA_Node* pNode) { | |
772 if (!m_pScriptNodeArray) { | |
773 return; | |
774 } | |
775 if (m_pScriptNodeArray->Find(pNode) == -1) { | |
776 m_pScriptNodeArray->Add(pNode); | |
777 } | |
778 } | |
779 IXFA_ScriptContext* XFA_ScriptContext_Create(CXFA_Document* pDocument) { | |
780 return new CXFA_ScriptContext(pDocument); | |
781 } | |
OLD | NEW |