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