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