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