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