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#ifndef FXJS_CFXJSE_ENGINE_H_
8#define FXJS_CFXJSE_ENGINE_H_
9
10#include <map>
11#include <memory>
12#include <vector>
13
14#include "fxjs/cfxjse_formcalc_context.h"
15#include "fxjs/cjs_v8.h"
16#include "xfa/fxfa/cxfa_eventparam.h"
17#include "xfa/fxfa/parser/cxfa_document.h"
18#include "xfa/fxfa/parser/cxfa_script.h"
19#include "xfa/fxfa/parser/xfa_resolvenode_rs.h"
20
21#define XFA_RESOLVENODE_TagName 0x0002
22
23class CXFA_List;
24class CFXJSE_ResolveProcessor;
25
26class CFXJSE_Engine : public CJS_V8 {
27 public:
28  static CXFA_Object* ToObject(const v8::FunctionCallbackInfo<v8::Value>& info);
29  static CXFA_Object* ToObject(CFXJSE_Value* pValue, CFXJSE_Class* pClass);
30  static void GlobalPropertyGetter(CFXJSE_Value* pObject,
31                                   const ByteStringView& szPropName,
32                                   CFXJSE_Value* pValue);
33  static void GlobalPropertySetter(CFXJSE_Value* pObject,
34                                   const ByteStringView& szPropName,
35                                   CFXJSE_Value* pValue);
36  static void NormalPropertyGetter(CFXJSE_Value* pObject,
37                                   const ByteStringView& szPropName,
38                                   CFXJSE_Value* pValue);
39  static void NormalPropertySetter(CFXJSE_Value* pObject,
40                                   const ByteStringView& szPropName,
41                                   CFXJSE_Value* pValue);
42  static CJS_Return NormalMethodCall(
43      const v8::FunctionCallbackInfo<v8::Value>& info,
44      const WideString& functionName);
45  static int32_t NormalPropTypeGetter(CFXJSE_Value* pObject,
46                                      const ByteStringView& szPropName,
47                                      bool bQueryIn);
48  static int32_t GlobalPropTypeGetter(CFXJSE_Value* pObject,
49                                      const ByteStringView& szPropName,
50                                      bool bQueryIn);
51
52  explicit CFXJSE_Engine(CXFA_Document* pDocument, v8::Isolate* pIsolate);
53  ~CFXJSE_Engine() override;
54
55  void SetEventParam(CXFA_EventParam param) { m_eventParam = param; }
56  CXFA_EventParam* GetEventParam() { return &m_eventParam; }
57  bool RunScript(CXFA_Script::Type eScriptType,
58                 const WideStringView& wsScript,
59                 CFXJSE_Value* pRetValue,
60                 CXFA_Object* pThisObject);
61
62  bool ResolveObjects(CXFA_Object* refObject,
63                      const WideStringView& wsExpression,
64                      XFA_RESOLVENODE_RS* resolveNodeRS,
65                      uint32_t dwStyles,
66                      CXFA_Node* bindNode);
67  CFXJSE_Value* GetJSValueFromMap(CXFA_Object* pObject);
68  void AddToCacheList(std::unique_ptr<CXFA_List> pList);
69  CXFA_Object* GetThisObject() const { return m_pThisObject; }
70
71  int32_t GetIndexByName(CXFA_Node* refNode);
72  int32_t GetIndexByClassName(CXFA_Node* refNode);
73  WideString GetSomExpression(CXFA_Node* refNode);
74
75  void SetNodesOfRunScript(std::vector<CXFA_Node*>* pArray);
76  void AddNodesOfRunScript(CXFA_Node* pNode);
77  CFXJSE_Class* GetJseNormalClass();
78
79  void SetRunAtType(XFA_AttributeEnum eRunAt) { m_eRunAtType = eRunAt; }
80  bool IsRunAtClient() { return m_eRunAtType != XFA_AttributeEnum::Server; }
81
82  CXFA_Script::Type GetType();
83  std::vector<CXFA_Node*>* GetUpObjectArray() { return &m_upObjectArray; }
84  CXFA_Document* GetDocument() const { return m_pDocument.Get(); }
85
86 private:
87  CFXJSE_Context* CreateVariablesContext(CXFA_Node* pScriptNode,
88                                         CXFA_Node* pSubform);
89  void RemoveBuiltInObjs(CFXJSE_Context* pContext) const;
90  bool QueryNodeByFlag(CXFA_Node* refNode,
91                       const WideStringView& propname,
92                       CFXJSE_Value* pValue,
93                       uint32_t dwFlag,
94                       bool bSetting);
95  bool IsStrictScopeInJavaScript();
96  CXFA_Object* GetVariablesThis(CXFA_Object* pObject, bool bScriptNode = false);
97  bool QueryVariableValue(CXFA_Node* pScriptNode,
98                          const ByteStringView& szPropName,
99                          CFXJSE_Value* pValue,
100                          bool bGetter);
101  bool RunVariablesScript(CXFA_Node* pScriptNode);
102
103  UnownedPtr<CXFA_Document> const m_pDocument;
104  std::unique_ptr<CFXJSE_Context> m_JsContext;
105  CFXJSE_Class* m_pJsClass;
106  CXFA_Script::Type m_eScriptType;
107  std::map<CXFA_Object*, std::unique_ptr<CFXJSE_Value>> m_mapObjectToValue;
108  std::map<CXFA_Object*, std::unique_ptr<CFXJSE_Context>>
109      m_mapVariableToContext;
110  CXFA_EventParam m_eventParam;
111  std::vector<CXFA_Node*> m_upObjectArray;
112  // CacheList holds the List items so we can clean them up when we're done.
113  std::vector<std::unique_ptr<CXFA_List>> m_CacheList;
114  std::vector<CXFA_Node*>* m_pScriptNodeArray;
115  std::unique_ptr<CFXJSE_ResolveProcessor> m_ResolveProcessor;
116  std::unique_ptr<CFXJSE_FormCalcContext> m_FM2JSContext;
117  CXFA_Object* m_pThisObject;
118  XFA_AttributeEnum m_eRunAtType;
119};
120
121#endif  //  FXJS_CFXJSE_ENGINE_H_
122