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 _XFA_FM2JS_CONTEXT_H
8 #define _XFA_FM2JS_CONTEXT_H
9 class CXFA_FM2JSContext {
10  public:
11   static void Abs(FXJSE_HOBJECT hThis,
12                   const CFX_ByteStringC& szFuncName,
13                   CFXJSE_Arguments& args);
14   static void Avg(FXJSE_HOBJECT hThis,
15                   const CFX_ByteStringC& szFuncName,
16                   CFXJSE_Arguments& args);
17   static void Ceil(FXJSE_HOBJECT hThis,
18                    const CFX_ByteStringC& szFuncName,
19                    CFXJSE_Arguments& args);
20   static void Count(FXJSE_HOBJECT hThis,
21                     const CFX_ByteStringC& szFuncName,
22                     CFXJSE_Arguments& args);
23   static void Floor(FXJSE_HOBJECT hThis,
24                     const CFX_ByteStringC& szFuncName,
25                     CFXJSE_Arguments& args);
26   static void Max(FXJSE_HOBJECT hThis,
27                   const CFX_ByteStringC& szFuncName,
28                   CFXJSE_Arguments& args);
29   static void Min(FXJSE_HOBJECT hThis,
30                   const CFX_ByteStringC& szFuncName,
31                   CFXJSE_Arguments& args);
32   static void Mod(FXJSE_HOBJECT hThis,
33                   const CFX_ByteStringC& szFuncName,
34                   CFXJSE_Arguments& args);
35   static void Round(FXJSE_HOBJECT hThis,
36                     const CFX_ByteStringC& szFuncName,
37                     CFXJSE_Arguments& args);
38   static void Sum(FXJSE_HOBJECT hThis,
39                   const CFX_ByteStringC& szFuncName,
40                   CFXJSE_Arguments& args);
41   static void Date(FXJSE_HOBJECT hThis,
42                    const CFX_ByteStringC& szFuncName,
43                    CFXJSE_Arguments& args);
44   static void Date2Num(FXJSE_HOBJECT hThis,
45                        const CFX_ByteStringC& szFuncName,
46                        CFXJSE_Arguments& args);
47   static void DateFmt(FXJSE_HOBJECT hThis,
48                       const CFX_ByteStringC& szFuncName,
49                       CFXJSE_Arguments& args);
50   static void IsoDate2Num(FXJSE_HOBJECT hThis,
51                           const CFX_ByteStringC& szFuncName,
52                           CFXJSE_Arguments& args);
53   static void IsoTime2Num(FXJSE_HOBJECT hThis,
54                           const CFX_ByteStringC& szFuncName,
55                           CFXJSE_Arguments& args);
56   static void LocalDateFmt(FXJSE_HOBJECT hThis,
57                            const CFX_ByteStringC& szFuncName,
58                            CFXJSE_Arguments& args);
59   static void LocalTimeFmt(FXJSE_HOBJECT hThis,
60                            const CFX_ByteStringC& szFuncName,
61                            CFXJSE_Arguments& args);
62   static void Num2Date(FXJSE_HOBJECT hThis,
63                        const CFX_ByteStringC& szFuncName,
64                        CFXJSE_Arguments& args);
65   static void Num2GMTime(FXJSE_HOBJECT hThis,
66                          const CFX_ByteStringC& szFuncName,
67                          CFXJSE_Arguments& args);
68   static void Num2Time(FXJSE_HOBJECT hThis,
69                        const CFX_ByteStringC& szFuncName,
70                        CFXJSE_Arguments& args);
71   static void Time(FXJSE_HOBJECT hThis,
72                    const CFX_ByteStringC& szFuncName,
73                    CFXJSE_Arguments& args);
74   static void Time2Num(FXJSE_HOBJECT hThis,
75                        const CFX_ByteStringC& szFuncName,
76                        CFXJSE_Arguments& args);
77   static void TimeFmt(FXJSE_HOBJECT hThis,
78                       const CFX_ByteStringC& szFuncName,
79                       CFXJSE_Arguments& args);
80 
81   static FX_BOOL IsIsoDateFormat(const FX_CHAR* pData,
82                                  int32_t iLength,
83                                  int32_t& iStyle,
84                                  int32_t& iYear,
85                                  int32_t& iMonth,
86                                  int32_t& iDay);
87   static FX_BOOL IsIsoTimeFormat(const FX_CHAR* pData,
88                                  int32_t iLength,
89                                  int32_t& iHour,
90                                  int32_t& iMinute,
91                                  int32_t& iSecond,
92                                  int32_t& iMilliSecond,
93                                  int32_t& iZoneHour,
94                                  int32_t& iZoneMinute);
95   static FX_BOOL IsIsoDateTimeFormat(const FX_CHAR* pData,
96                                      int32_t iLength,
97                                      int32_t& iYear,
98                                      int32_t& iMonth,
99                                      int32_t& iDay,
100                                      int32_t& iHour,
101                                      int32_t& iMinute,
102                                      int32_t& iSecond,
103                                      int32_t& iMillionSecond,
104                                      int32_t& iZoneHour,
105                                      int32_t& iZoneMinute);
106   static FX_BOOL Local2IsoDate(FXJSE_HOBJECT hThis,
107                                const CFX_ByteStringC& szDate,
108                                const CFX_ByteStringC& szFormat,
109                                const CFX_ByteStringC& szLocale,
110                                CFX_ByteString& strIsoDate);
111   static FX_BOOL Local2IsoTime(FXJSE_HOBJECT hThis,
112                                const CFX_ByteStringC& szTime,
113                                const CFX_ByteStringC& szFormat,
114                                const CFX_ByteStringC& szLocale,
115                                CFX_ByteString& strIsoTime);
116   static FX_BOOL IsoDate2Local(FXJSE_HOBJECT hThis,
117                                const CFX_ByteStringC& szDate,
118                                const CFX_ByteStringC& szFormat,
119                                const CFX_ByteStringC& szLocale,
120                                CFX_ByteString& strLocalDate);
121   static FX_BOOL IsoTime2Local(FXJSE_HOBJECT hThis,
122                                const CFX_ByteStringC& szTime,
123                                const CFX_ByteStringC& szFormat,
124                                const CFX_ByteStringC& szLocale,
125                                CFX_ByteString& strLocalTime);
126   static FX_BOOL GetGMTTime(FXJSE_HOBJECT hThis,
127                             const CFX_ByteStringC& szTime,
128                             const CFX_ByteStringC& szFormat,
129                             const CFX_ByteStringC& szLocale,
130                             CFX_ByteString& strGMTTime);
131   static int32_t DateString2Num(const CFX_ByteStringC& szDateString);
132   static void GetLocalDateFormat(FXJSE_HOBJECT hThis,
133                                  int32_t iStyle,
134                                  const CFX_ByteStringC& szLocalStr,
135                                  CFX_ByteString& strFormat,
136                                  FX_BOOL bStandard);
137   static void GetLocalTimeFormat(FXJSE_HOBJECT hThis,
138                                  int32_t iStyle,
139                                  const CFX_ByteStringC& szLocalStr,
140                                  CFX_ByteString& strFormat,
141                                  FX_BOOL bStandard);
142   static void GetStandardDateFormat(FXJSE_HOBJECT hThis,
143                                     int32_t iStyle,
144                                     const CFX_ByteStringC& szLocalStr,
145                                     CFX_ByteString& strFormat);
146   static void GetStandardTimeFormat(FXJSE_HOBJECT hThis,
147                                     int32_t iStyle,
148                                     const CFX_ByteStringC& szLocalStr,
149                                     CFX_ByteString& strFormat);
150 
151   static void Num2AllTime(FXJSE_HOBJECT hThis,
152                           int32_t iTime,
153                           const CFX_ByteStringC& szFormat,
154                           const CFX_ByteStringC& szLocale,
155                           FX_BOOL bGM,
156                           CFX_ByteString& strTime);
157   static void GetLocalTimeZone(int32_t& iHour, int32_t& iMin, int32_t& iSec);
158 
159   static void Apr(FXJSE_HOBJECT hThis,
160                   const CFX_ByteStringC& szFuncName,
161                   CFXJSE_Arguments& args);
162   static void CTerm(FXJSE_HOBJECT hThis,
163                     const CFX_ByteStringC& szFuncName,
164                     CFXJSE_Arguments& args);
165   static void FV(FXJSE_HOBJECT hThis,
166                  const CFX_ByteStringC& szFuncName,
167                  CFXJSE_Arguments& args);
168   static void IPmt(FXJSE_HOBJECT hThis,
169                    const CFX_ByteStringC& szFuncName,
170                    CFXJSE_Arguments& args);
171   static void NPV(FXJSE_HOBJECT hThis,
172                   const CFX_ByteStringC& szFuncName,
173                   CFXJSE_Arguments& args);
174   static void Pmt(FXJSE_HOBJECT hThis,
175                   const CFX_ByteStringC& szFuncName,
176                   CFXJSE_Arguments& args);
177   static void PPmt(FXJSE_HOBJECT hThis,
178                    const CFX_ByteStringC& szFuncName,
179                    CFXJSE_Arguments& args);
180   static void PV(FXJSE_HOBJECT hThis,
181                  const CFX_ByteStringC& szFuncName,
182                  CFXJSE_Arguments& args);
183   static void Rate(FXJSE_HOBJECT hThis,
184                    const CFX_ByteStringC& szFuncName,
185                    CFXJSE_Arguments& args);
186   static void Term(FXJSE_HOBJECT hThis,
187                    const CFX_ByteStringC& szFuncName,
188                    CFXJSE_Arguments& args);
189   static void Choose(FXJSE_HOBJECT hThis,
190                      const CFX_ByteStringC& szFuncName,
191                      CFXJSE_Arguments& args);
192   static void Exists(FXJSE_HOBJECT hThis,
193                      const CFX_ByteStringC& szFuncName,
194                      CFXJSE_Arguments& args);
195   static void HasValue(FXJSE_HOBJECT hThis,
196                        const CFX_ByteStringC& szFuncName,
197                        CFXJSE_Arguments& args);
198   static void Oneof(FXJSE_HOBJECT hThis,
199                     const CFX_ByteStringC& szFuncName,
200                     CFXJSE_Arguments& args);
201   static void Within(FXJSE_HOBJECT hThis,
202                      const CFX_ByteStringC& szFuncName,
203                      CFXJSE_Arguments& args);
204   static void If(FXJSE_HOBJECT hThis,
205                  const CFX_ByteStringC& szFuncName,
206                  CFXJSE_Arguments& args);
207   static void Eval(FXJSE_HOBJECT hThis,
208                    const CFX_ByteStringC& szFuncName,
209                    CFXJSE_Arguments& args);
210   static void Ref(FXJSE_HOBJECT hThis,
211                   const CFX_ByteStringC& szFuncName,
212                   CFXJSE_Arguments& args);
213   static void UnitType(FXJSE_HOBJECT hThis,
214                        const CFX_ByteStringC& szFuncName,
215                        CFXJSE_Arguments& args);
216   static void UnitValue(FXJSE_HOBJECT hThis,
217                         const CFX_ByteStringC& szFuncName,
218                         CFXJSE_Arguments& args);
219 
220   static void At(FXJSE_HOBJECT hThis,
221                  const CFX_ByteStringC& szFuncName,
222                  CFXJSE_Arguments& args);
223   static void Concat(FXJSE_HOBJECT hThis,
224                      const CFX_ByteStringC& szFuncName,
225                      CFXJSE_Arguments& args);
226   static void Decode(FXJSE_HOBJECT hThis,
227                      const CFX_ByteStringC& szFuncName,
228                      CFXJSE_Arguments& args);
229   static void DecodeURL(const CFX_ByteStringC& szURLString,
230                         CFX_ByteTextBuf& szResultBuf);
231   static void DecodeHTML(const CFX_ByteStringC& szHTMLString,
232                          CFX_ByteTextBuf& szResultBuf);
233   static void DecodeXML(const CFX_ByteStringC& szXMLString,
234                         CFX_ByteTextBuf& szResultBuf);
235   static void Encode(FXJSE_HOBJECT hThis,
236                      const CFX_ByteStringC& szFuncName,
237                      CFXJSE_Arguments& args);
238   static void EncodeURL(const CFX_ByteStringC& szURLString,
239                         CFX_ByteTextBuf& szResultBuf);
240   static void EncodeHTML(const CFX_ByteStringC& szHTMLString,
241                          CFX_ByteTextBuf& szResultBuf);
242   static void EncodeXML(const CFX_ByteStringC& szXMLString,
243                         CFX_ByteTextBuf& szResultBuf);
244   static FX_BOOL HTMLSTR2Code(const CFX_WideStringC& pData, uint32_t& iCode);
245   static FX_BOOL HTMLCode2STR(uint32_t iCode, CFX_WideString& wsHTMLReserve);
246   static void Format(FXJSE_HOBJECT hThis,
247                      const CFX_ByteStringC& szFuncName,
248                      CFXJSE_Arguments& args);
249   static void Left(FXJSE_HOBJECT hThis,
250                    const CFX_ByteStringC& szFuncName,
251                    CFXJSE_Arguments& args);
252   static void Len(FXJSE_HOBJECT hThis,
253                   const CFX_ByteStringC& szFuncName,
254                   CFXJSE_Arguments& args);
255   static void Lower(FXJSE_HOBJECT hThis,
256                     const CFX_ByteStringC& szFuncName,
257                     CFXJSE_Arguments& args);
258   static void Ltrim(FXJSE_HOBJECT hThis,
259                     const CFX_ByteStringC& szFuncName,
260                     CFXJSE_Arguments& args);
261   static void Parse(FXJSE_HOBJECT hThis,
262                     const CFX_ByteStringC& szFuncName,
263                     CFXJSE_Arguments& args);
264   static void Replace(FXJSE_HOBJECT hThis,
265                       const CFX_ByteStringC& szFuncName,
266                       CFXJSE_Arguments& args);
267   static void Right(FXJSE_HOBJECT hThis,
268                     const CFX_ByteStringC& szFuncName,
269                     CFXJSE_Arguments& args);
270   static void Rtrim(FXJSE_HOBJECT hThis,
271                     const CFX_ByteStringC& szFuncName,
272                     CFXJSE_Arguments& args);
273   static void Space(FXJSE_HOBJECT hThis,
274                     const CFX_ByteStringC& szFuncName,
275                     CFXJSE_Arguments& args);
276   static void Str(FXJSE_HOBJECT hThis,
277                   const CFX_ByteStringC& szFuncName,
278                   CFXJSE_Arguments& args);
279   static void Stuff(FXJSE_HOBJECT hThis,
280                     const CFX_ByteStringC& szFuncName,
281                     CFXJSE_Arguments& args);
282   static void Substr(FXJSE_HOBJECT hThis,
283                      const CFX_ByteStringC& szFuncName,
284                      CFXJSE_Arguments& args);
285   static void Uuid(FXJSE_HOBJECT hThis,
286                    const CFX_ByteStringC& szFuncName,
287                    CFXJSE_Arguments& args);
288   static void Upper(FXJSE_HOBJECT hThis,
289                     const CFX_ByteStringC& szFuncName,
290                     CFXJSE_Arguments& args);
291   static void WordNum(FXJSE_HOBJECT hThis,
292                       const CFX_ByteStringC& szFuncName,
293                       CFXJSE_Arguments& args);
294   static void TrillionUS(const CFX_ByteStringC& szData,
295                          CFX_ByteTextBuf& strBuf);
296   static void WordUS(const CFX_ByteStringC& szData,
297                      int32_t iStyle,
298                      CFX_ByteTextBuf& strBuf);
299 
300   static void Get(FXJSE_HOBJECT hThis,
301                   const CFX_ByteStringC& szFuncName,
302                   CFXJSE_Arguments& args);
303   static void Post(FXJSE_HOBJECT hThis,
304                    const CFX_ByteStringC& szFuncName,
305                    CFXJSE_Arguments& args);
306   static void Put(FXJSE_HOBJECT hThis,
307                   const CFX_ByteStringC& szFuncName,
308                   CFXJSE_Arguments& args);
309   static void assign_value_operator(FXJSE_HOBJECT hThis,
310                                     const CFX_ByteStringC& szFuncName,
311                                     CFXJSE_Arguments& args);
312   static void logical_or_operator(FXJSE_HOBJECT hThis,
313                                   const CFX_ByteStringC& szFuncName,
314                                   CFXJSE_Arguments& args);
315   static void logical_and_operator(FXJSE_HOBJECT hThis,
316                                    const CFX_ByteStringC& szFuncName,
317                                    CFXJSE_Arguments& args);
318   static void equality_operator(FXJSE_HOBJECT hThis,
319                                 const CFX_ByteStringC& szFuncName,
320                                 CFXJSE_Arguments& args);
321   static void notequality_operator(FXJSE_HOBJECT hThis,
322                                    const CFX_ByteStringC& szFuncName,
323                                    CFXJSE_Arguments& args);
324   static FX_BOOL fm_ref_equal(FXJSE_HOBJECT hThis, CFXJSE_Arguments& args);
325   static void less_operator(FXJSE_HOBJECT hThis,
326                             const CFX_ByteStringC& szFuncName,
327                             CFXJSE_Arguments& args);
328   static void lessequal_operator(FXJSE_HOBJECT hThis,
329                                  const CFX_ByteStringC& szFuncName,
330                                  CFXJSE_Arguments& args);
331   static void greater_operator(FXJSE_HOBJECT hThis,
332                                const CFX_ByteStringC& szFuncName,
333                                CFXJSE_Arguments& args);
334   static void greaterequal_operator(FXJSE_HOBJECT hThis,
335                                     const CFX_ByteStringC& szFuncName,
336                                     CFXJSE_Arguments& args);
337   static void plus_operator(FXJSE_HOBJECT hThis,
338                             const CFX_ByteStringC& szFuncName,
339                             CFXJSE_Arguments& args);
340   static void minus_operator(FXJSE_HOBJECT hThis,
341                              const CFX_ByteStringC& szFuncName,
342                              CFXJSE_Arguments& args);
343   static void multiple_operator(FXJSE_HOBJECT hThis,
344                                 const CFX_ByteStringC& szFuncName,
345                                 CFXJSE_Arguments& args);
346   static void divide_operator(FXJSE_HOBJECT hThis,
347                               const CFX_ByteStringC& szFuncName,
348                               CFXJSE_Arguments& args);
349   static void positive_operator(FXJSE_HOBJECT hThis,
350                                 const CFX_ByteStringC& szFuncName,
351                                 CFXJSE_Arguments& args);
352   static void negative_operator(FXJSE_HOBJECT hThis,
353                                 const CFX_ByteStringC& szFuncName,
354                                 CFXJSE_Arguments& args);
355   static void logical_not_operator(FXJSE_HOBJECT hThis,
356                                    const CFX_ByteStringC& szFuncName,
357                                    CFXJSE_Arguments& args);
358   static void dot_accessor(FXJSE_HOBJECT hThis,
359                            const CFX_ByteStringC& szFuncName,
360                            CFXJSE_Arguments& args);
361   static void dotdot_accessor(FXJSE_HOBJECT hThis,
362                               const CFX_ByteStringC& szFuncName,
363                               CFXJSE_Arguments& args);
364   static void eval_translation(FXJSE_HOBJECT hThis,
365                                const CFX_ByteStringC& szFuncName,
366                                CFXJSE_Arguments& args);
367   static void is_fm_object(FXJSE_HOBJECT hThis,
368                            const CFX_ByteStringC& szFuncName,
369                            CFXJSE_Arguments& args);
370   static void is_fm_array(FXJSE_HOBJECT hThis,
371                           const CFX_ByteStringC& szFuncName,
372                           CFXJSE_Arguments& args);
373   static void get_fm_value(FXJSE_HOBJECT hThis,
374                            const CFX_ByteStringC& szFuncName,
375                            CFXJSE_Arguments& args);
376   static void get_fm_jsobj(FXJSE_HOBJECT hThis,
377                            const CFX_ByteStringC& szFuncName,
378                            CFXJSE_Arguments& args);
379   static void fm_var_filter(FXJSE_HOBJECT hThis,
380                             const CFX_ByteStringC& szFuncName,
381                             CFXJSE_Arguments& args);
382   static void concat_fm_object(FXJSE_HOBJECT hThis,
383                                const CFX_ByteStringC& szFuncName,
384                                CFXJSE_Arguments& args);
385 
386   static int32_t hvalue_get_array_length(FXJSE_HOBJECT hThis, FXJSE_HVALUE arg);
387   static FX_BOOL simpleValueCompare(FXJSE_HOBJECT hThis,
388                                     FXJSE_HVALUE firstValue,
389                                     FXJSE_HVALUE secondValue);
390   static void unfoldArgs(FXJSE_HOBJECT hThis,
391                          CFXJSE_Arguments& args,
392                          FXJSE_HVALUE*& resultValues,
393                          int32_t& iCount,
394                          int32_t iStart = 0);
395   static void GetObjectDefaultValue(FXJSE_HVALUE hObjectValue,
396                                     FXJSE_HVALUE hDefaultValue);
397   static FX_BOOL SetObjectDefaultValue(FXJSE_HVALUE hObjectValue,
398                                        FXJSE_HVALUE hNewValue);
399   static void GenerateSomExpression(const CFX_ByteStringC& szName,
400                                     int32_t iIndexFlags,
401                                     int32_t iIndexValue,
402                                     FX_BOOL bIsStar,
403                                     CFX_ByteString& szSomExp);
404   static FX_BOOL GetObjectByName(FXJSE_HOBJECT hThis,
405                                  FXJSE_HVALUE accessorValue,
406                                  const CFX_ByteStringC& szAccessorName);
407   static int32_t ResolveObjects(FXJSE_HOBJECT hThis,
408                                 FXJSE_HVALUE hParentValue,
409                                 const CFX_ByteStringC& bsSomExp,
410                                 XFA_RESOLVENODE_RS& resoveNodeRS,
411                                 FX_BOOL bdotAccessor = TRUE,
412                                 FX_BOOL bHasNoResolveName = FALSE);
413   static void ParseResolveResult(FXJSE_HOBJECT hThis,
414                                  const XFA_RESOLVENODE_RS& resoveNodeRS,
415                                  FXJSE_HVALUE hParentValue,
416                                  FXJSE_HVALUE*& resultValues,
417                                  int32_t& iSize,
418                                  FX_BOOL& bAttribute);
419 
420   static FXJSE_HVALUE GetSimpleHValue(FXJSE_HOBJECT hThis,
421                                       CFXJSE_Arguments& args,
422                                       uint32_t index);
423   static FX_BOOL HValueIsNull(FXJSE_HOBJECT hThis, FXJSE_HVALUE hValue);
424   static int32_t HValueToInteger(FXJSE_HOBJECT hThis, FXJSE_HVALUE hValue);
425   static FX_DOUBLE StringToDouble(const CFX_ByteStringC& szStringVal);
426   static FX_FLOAT HValueToFloat(FXJSE_HOBJECT hThis, FXJSE_HVALUE hValue);
427   static FX_DOUBLE HValueToDouble(FXJSE_HOBJECT hThis, FXJSE_HVALUE hValue);
428   static void HValueToUTF8String(FXJSE_HVALUE hValue,
429                                  CFX_ByteString& outputValue);
430   CXFA_FM2JSContext();
431   ~CXFA_FM2JSContext();
432   static CXFA_FM2JSContext* Create();
433   void Initialize(FXJSE_HRUNTIME hScriptRuntime,
434                   FXJSE_HCONTEXT hScriptContext,
435                   CXFA_Document* pDoc);
436   void GlobalPropertyGetter(FXJSE_HVALUE hValue);
437   void Release();
GetScriptRuntime()438   FXJSE_HRUNTIME GetScriptRuntime() const { return m_hScriptRuntime; }
GetDocument()439   CXFA_Document* GetDocument() const { return m_pDocument; }
440   void ThrowScriptErrorMessage(int32_t iStringID, ...);
441 
442  private:
443   FXJSE_HRUNTIME m_hScriptRuntime;
444   FXJSE_CLASS m_fmClass;
445   FXJSE_HCLASS m_hFMClass;
446   FXJSE_HVALUE m_hValue;
447   CXFA_Document* m_pDocument;
448 };
449 #endif
450