• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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_XFA_CFXJSE_FORMCALC_CONTEXT_H_
8 #define FXJS_XFA_CFXJSE_FORMCALC_CONTEXT_H_
9 
10 #include <stdint.h>
11 
12 #include <functional>
13 
14 #include "core/fxcrt/widetext_buffer.h"
15 #include "fxjs/xfa/fxjse.h"
16 #include "third_party/abseil-cpp/absl/types/optional.h"
17 #include "v8/include/cppgc/persistent.h"
18 #include "v8/include/v8-forward.h"
19 #include "v8/include/v8-persistent-handle.h"
20 
21 class CFXJSE_Context;
22 class CXFA_Document;
23 
24 namespace cppgc {
25 class Heap;
26 }  // namespace cppgc
27 
28 class CFXJSE_FormCalcContext final : public CFXJSE_HostObject {
29  public:
30   CFXJSE_FormCalcContext(v8::Isolate* pIsolate,
31                          CFXJSE_Context* pScriptContext,
32                          CXFA_Document* pDoc);
33   ~CFXJSE_FormCalcContext() override;
34 
35   // CFXJSE_HostObject:
36   CFXJSE_FormCalcContext* AsFormCalcContext() override;
37 
38   static void Abs(CFXJSE_HostObject* pThis,
39                   const v8::FunctionCallbackInfo<v8::Value>& info);
40   static void Avg(CFXJSE_HostObject* pThis,
41                   const v8::FunctionCallbackInfo<v8::Value>& info);
42   static void Ceil(CFXJSE_HostObject* pThis,
43                    const v8::FunctionCallbackInfo<v8::Value>& info);
44   static void Count(CFXJSE_HostObject* pThis,
45                     const v8::FunctionCallbackInfo<v8::Value>& info);
46   static void Floor(CFXJSE_HostObject* pThis,
47                     const v8::FunctionCallbackInfo<v8::Value>& info);
48   static void Max(CFXJSE_HostObject* pThis,
49                   const v8::FunctionCallbackInfo<v8::Value>& info);
50   static void Min(CFXJSE_HostObject* pThis,
51                   const v8::FunctionCallbackInfo<v8::Value>& info);
52   static void Mod(CFXJSE_HostObject* pThis,
53                   const v8::FunctionCallbackInfo<v8::Value>& info);
54   static void Round(CFXJSE_HostObject* pThis,
55                     const v8::FunctionCallbackInfo<v8::Value>& info);
56   static void Sum(CFXJSE_HostObject* pThis,
57                   const v8::FunctionCallbackInfo<v8::Value>& info);
58   static void Date(CFXJSE_HostObject* pThis,
59                    const v8::FunctionCallbackInfo<v8::Value>& info);
60   static void Date2Num(CFXJSE_HostObject* pThis,
61                        const v8::FunctionCallbackInfo<v8::Value>& info);
62   static void DateFmt(CFXJSE_HostObject* pThis,
63                       const v8::FunctionCallbackInfo<v8::Value>& info);
64   static void IsoDate2Num(CFXJSE_HostObject* pThis,
65                           const v8::FunctionCallbackInfo<v8::Value>& info);
66   static void IsoTime2Num(CFXJSE_HostObject* pThis,
67                           const v8::FunctionCallbackInfo<v8::Value>& info);
68   static void LocalDateFmt(CFXJSE_HostObject* pThis,
69                            const v8::FunctionCallbackInfo<v8::Value>& info);
70   static void LocalTimeFmt(CFXJSE_HostObject* pThis,
71                            const v8::FunctionCallbackInfo<v8::Value>& info);
72   static void Num2Date(CFXJSE_HostObject* pThis,
73                        const v8::FunctionCallbackInfo<v8::Value>& info);
74   static void Num2GMTime(CFXJSE_HostObject* pThis,
75                          const v8::FunctionCallbackInfo<v8::Value>& info);
76   static void Num2Time(CFXJSE_HostObject* pThis,
77                        const v8::FunctionCallbackInfo<v8::Value>& info);
78   static void Time(CFXJSE_HostObject* pThis,
79                    const v8::FunctionCallbackInfo<v8::Value>& info);
80   static void Time2Num(CFXJSE_HostObject* pThis,
81                        const v8::FunctionCallbackInfo<v8::Value>& info);
82   static void TimeFmt(CFXJSE_HostObject* pThis,
83                       const v8::FunctionCallbackInfo<v8::Value>& info);
84 
85   static ByteString Local2IsoDate(CFXJSE_HostObject* pThis,
86                                   ByteStringView bsDate,
87                                   ByteStringView bsFormat,
88                                   ByteStringView bsLocale);
89   static ByteString IsoDate2Local(CFXJSE_HostObject* pThis,
90                                   ByteStringView bsDate,
91                                   ByteStringView bsFormat,
92                                   ByteStringView bsLocale);
93   static ByteString IsoTime2Local(CFXJSE_HostObject* pThis,
94                                   ByteStringView bsTime,
95                                   ByteStringView bsFormat,
96                                   ByteStringView bsLocale);
97   static ByteString GetLocalDateFormat(CFXJSE_HostObject* pThis,
98                                        int32_t iStyle,
99                                        ByteStringView bsLocale,
100                                        bool bStandard);
101   static ByteString GetLocalTimeFormat(CFXJSE_HostObject* pThis,
102                                        int32_t iStyle,
103                                        ByteStringView bsLocale,
104                                        bool bStandard);
105   static ByteString GetStandardDateFormat(CFXJSE_HostObject* pThis,
106                                           int32_t iStyle,
107                                           ByteStringView bsLocale);
108   static ByteString GetStandardTimeFormat(CFXJSE_HostObject* pThis,
109                                           int32_t iStyle,
110                                           ByteStringView bsLocale);
111   static ByteString Num2AllTime(CFXJSE_HostObject* pThis,
112                                 int32_t iTime,
113                                 ByteStringView bsFormat,
114                                 ByteStringView bsLocale,
115                                 bool bGM);
116 
117   static void Apr(CFXJSE_HostObject* pThis,
118                   const v8::FunctionCallbackInfo<v8::Value>& info);
119   static void CTerm(CFXJSE_HostObject* pThis,
120                     const v8::FunctionCallbackInfo<v8::Value>& info);
121   static void FV(CFXJSE_HostObject* pThis,
122                  const v8::FunctionCallbackInfo<v8::Value>& info);
123   static void IPmt(CFXJSE_HostObject* pThis,
124                    const v8::FunctionCallbackInfo<v8::Value>& info);
125   static void NPV(CFXJSE_HostObject* pThis,
126                   const v8::FunctionCallbackInfo<v8::Value>& info);
127   static void Pmt(CFXJSE_HostObject* pThis,
128                   const v8::FunctionCallbackInfo<v8::Value>& info);
129   static void PPmt(CFXJSE_HostObject* pThis,
130                    const v8::FunctionCallbackInfo<v8::Value>& info);
131   static void PV(CFXJSE_HostObject* pThis,
132                  const v8::FunctionCallbackInfo<v8::Value>& info);
133   static void Rate(CFXJSE_HostObject* pThis,
134                    const v8::FunctionCallbackInfo<v8::Value>& info);
135   static void Term(CFXJSE_HostObject* pThis,
136                    const v8::FunctionCallbackInfo<v8::Value>& info);
137   static void Choose(CFXJSE_HostObject* pThis,
138                      const v8::FunctionCallbackInfo<v8::Value>& info);
139   static void Exists(CFXJSE_HostObject* pThis,
140                      const v8::FunctionCallbackInfo<v8::Value>& info);
141   static void HasValue(CFXJSE_HostObject* pThis,
142                        const v8::FunctionCallbackInfo<v8::Value>& info);
143   static void Oneof(CFXJSE_HostObject* pThis,
144                     const v8::FunctionCallbackInfo<v8::Value>& info);
145   static void Within(CFXJSE_HostObject* pThis,
146                      const v8::FunctionCallbackInfo<v8::Value>& info);
147   static void If(CFXJSE_HostObject* pThis,
148                  const v8::FunctionCallbackInfo<v8::Value>& info);
149   static void Eval(CFXJSE_HostObject* pThis,
150                    const v8::FunctionCallbackInfo<v8::Value>& info);
151   static void Ref(CFXJSE_HostObject* pThis,
152                   const v8::FunctionCallbackInfo<v8::Value>& info);
153   static void UnitType(CFXJSE_HostObject* pThis,
154                        const v8::FunctionCallbackInfo<v8::Value>& info);
155   static void UnitValue(CFXJSE_HostObject* pThis,
156                         const v8::FunctionCallbackInfo<v8::Value>& info);
157 
158   static void At(CFXJSE_HostObject* pThis,
159                  const v8::FunctionCallbackInfo<v8::Value>& info);
160   static void Concat(CFXJSE_HostObject* pThis,
161                      const v8::FunctionCallbackInfo<v8::Value>& info);
162   static void Decode(CFXJSE_HostObject* pThis,
163                      const v8::FunctionCallbackInfo<v8::Value>& info);
164   static void Encode(CFXJSE_HostObject* pThis,
165                      const v8::FunctionCallbackInfo<v8::Value>& info);
166   static void Format(CFXJSE_HostObject* pThis,
167                      const v8::FunctionCallbackInfo<v8::Value>& info);
168   static void Left(CFXJSE_HostObject* pThis,
169                    const v8::FunctionCallbackInfo<v8::Value>& info);
170   static void Len(CFXJSE_HostObject* pThis,
171                   const v8::FunctionCallbackInfo<v8::Value>& info);
172   static void Lower(CFXJSE_HostObject* pThis,
173                     const v8::FunctionCallbackInfo<v8::Value>& info);
174   static void Ltrim(CFXJSE_HostObject* pThis,
175                     const v8::FunctionCallbackInfo<v8::Value>& info);
176   static void Parse(CFXJSE_HostObject* pThis,
177                     const v8::FunctionCallbackInfo<v8::Value>& info);
178   static void Replace(CFXJSE_HostObject* pThis,
179                       const v8::FunctionCallbackInfo<v8::Value>& info);
180   static void Right(CFXJSE_HostObject* pThis,
181                     const v8::FunctionCallbackInfo<v8::Value>& info);
182   static void Rtrim(CFXJSE_HostObject* pThis,
183                     const v8::FunctionCallbackInfo<v8::Value>& info);
184   static void Space(CFXJSE_HostObject* pThis,
185                     const v8::FunctionCallbackInfo<v8::Value>& info);
186   static void Str(CFXJSE_HostObject* pThis,
187                   const v8::FunctionCallbackInfo<v8::Value>& info);
188   static void Stuff(CFXJSE_HostObject* pThis,
189                     const v8::FunctionCallbackInfo<v8::Value>& info);
190   static void Substr(CFXJSE_HostObject* pThis,
191                      const v8::FunctionCallbackInfo<v8::Value>& info);
192   static void Uuid(CFXJSE_HostObject* pThis,
193                    const v8::FunctionCallbackInfo<v8::Value>& info);
194   static void Upper(CFXJSE_HostObject* pThis,
195                     const v8::FunctionCallbackInfo<v8::Value>& info);
196   static void WordNum(CFXJSE_HostObject* pThis,
197                       const v8::FunctionCallbackInfo<v8::Value>& info);
198 
199   static void Get(CFXJSE_HostObject* pThis,
200                   const v8::FunctionCallbackInfo<v8::Value>& info);
201   static void Post(CFXJSE_HostObject* pThis,
202                    const v8::FunctionCallbackInfo<v8::Value>& info);
203   static void Put(CFXJSE_HostObject* pThis,
204                   const v8::FunctionCallbackInfo<v8::Value>& info);
205   static void assign_value_operator(
206       CFXJSE_HostObject* pThis,
207       const v8::FunctionCallbackInfo<v8::Value>& info);
208   static void logical_or_operator(
209       CFXJSE_HostObject* pThis,
210       const v8::FunctionCallbackInfo<v8::Value>& info);
211   static void logical_and_operator(
212       CFXJSE_HostObject* pThis,
213       const v8::FunctionCallbackInfo<v8::Value>& info);
214   static void equality_operator(
215       CFXJSE_HostObject* pThis,
216       const v8::FunctionCallbackInfo<v8::Value>& info);
217   static void notequality_operator(
218       CFXJSE_HostObject* pThis,
219       const v8::FunctionCallbackInfo<v8::Value>& info);
220   static bool fm_ref_equal(CFXJSE_HostObject* pThis,
221                            const v8::FunctionCallbackInfo<v8::Value>& info);
222   static void less_operator(CFXJSE_HostObject* pThis,
223                             const v8::FunctionCallbackInfo<v8::Value>& info);
224   static void lessequal_operator(
225       CFXJSE_HostObject* pThis,
226       const v8::FunctionCallbackInfo<v8::Value>& info);
227   static void greater_operator(CFXJSE_HostObject* pThis,
228                                const v8::FunctionCallbackInfo<v8::Value>& info);
229   static void greaterequal_operator(
230       CFXJSE_HostObject* pThis,
231       const v8::FunctionCallbackInfo<v8::Value>& info);
232   static void plus_operator(CFXJSE_HostObject* pThis,
233                             const v8::FunctionCallbackInfo<v8::Value>& info);
234   static void minus_operator(CFXJSE_HostObject* pThis,
235                              const v8::FunctionCallbackInfo<v8::Value>& info);
236   static void multiple_operator(
237       CFXJSE_HostObject* pThis,
238       const v8::FunctionCallbackInfo<v8::Value>& info);
239   static void divide_operator(CFXJSE_HostObject* pThis,
240                               const v8::FunctionCallbackInfo<v8::Value>& info);
241   static void positive_operator(
242       CFXJSE_HostObject* pThis,
243       const v8::FunctionCallbackInfo<v8::Value>& info);
244   static void negative_operator(
245       CFXJSE_HostObject* pThis,
246       const v8::FunctionCallbackInfo<v8::Value>& info);
247   static void logical_not_operator(
248       CFXJSE_HostObject* pThis,
249       const v8::FunctionCallbackInfo<v8::Value>& info);
250   static void dot_accessor(CFXJSE_HostObject* pThis,
251                            const v8::FunctionCallbackInfo<v8::Value>& info);
252   static void dotdot_accessor(CFXJSE_HostObject* pThis,
253                               const v8::FunctionCallbackInfo<v8::Value>& info);
254   static void eval_translation(CFXJSE_HostObject* pThis,
255                                const v8::FunctionCallbackInfo<v8::Value>& info);
256   static void is_fm_object(CFXJSE_HostObject* pThis,
257                            const v8::FunctionCallbackInfo<v8::Value>& info);
258   static void is_fm_array(CFXJSE_HostObject* pThis,
259                           const v8::FunctionCallbackInfo<v8::Value>& info);
260   static void get_fm_value(CFXJSE_HostObject* pThis,
261                            const v8::FunctionCallbackInfo<v8::Value>& info);
262   static void get_fm_jsobj(CFXJSE_HostObject* pThis,
263                            const v8::FunctionCallbackInfo<v8::Value>& info);
264   static void fm_var_filter(CFXJSE_HostObject* pThis,
265                             const v8::FunctionCallbackInfo<v8::Value>& info);
266   static void concat_fm_object(CFXJSE_HostObject* pThis,
267                                const v8::FunctionCallbackInfo<v8::Value>& info);
268 
269   static ByteString GenerateSomExpression(ByteStringView bsName,
270                                           int32_t iIndexFlags,
271                                           int32_t iIndexValue,
272                                           bool bIsStar);
273   static absl::optional<WideTextBuffer> Translate(cppgc::Heap* pHeap,
274                                                   WideStringView wsFormcalc);
275 
276   v8::Local<v8::Value> GlobalPropertyGetter();
GetIsolate()277   v8::Isolate* GetIsolate() const { return m_pIsolate; }
GetDocument()278   CXFA_Document* GetDocument() const { return m_pDocument.Get(); }
279 
280  private:
281   static void DotAccessorCommon(CFXJSE_HostObject* pThis,
282                                 const v8::FunctionCallbackInfo<v8::Value>& info,
283                                 bool bDotAccessor);
284 
285   bool ApplyToExpansion(
286       std::function<void(v8::Isolate*, v8::Local<v8::Value>)> fn,
287       const v8::FunctionCallbackInfo<v8::Value>& info,
288       bool bStrict);
289 
290   bool ApplyToArray(v8::Isolate* pIsolate,
291                     std::function<void(v8::Isolate*, v8::Local<v8::Value>)> fn,
292                     v8::Local<v8::Array> pArray);
293 
294   void ApplyToObject(v8::Isolate* pIsolate,
295                      std::function<void(v8::Isolate*, v8::Local<v8::Value>)> fn,
296                      v8::Local<v8::Object> pObject);
297 
298   void ThrowArgumentMismatchException() const;
299   void ThrowNoDefaultPropertyException(ByteStringView name) const;
300   void ThrowCompilerErrorException() const;
301   void ThrowDivideByZeroException() const;
302   void ThrowServerDeniedException() const;
303   void ThrowPropertyNotInObjectException(ByteStringView name,
304                                          ByteStringView exp) const;
305   void ThrowParamCountMismatchException(ByteStringView method) const;
306   void ThrowException(ByteStringView str) const;
307 
308   UnownedPtr<v8::Isolate> const m_pIsolate;
309   v8::Global<v8::Value> m_Value;
310   cppgc::WeakPersistent<CXFA_Document> const m_pDocument;
311 };
312 
313 #endif  // FXJS_XFA_CFXJSE_FORMCALC_CONTEXT_H_
314