blob: 37d52a9d4d2bb144b7e9ba8c0c568a0419a15184 [file] [log] [blame]
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "xfa/fxfa/fm2js/xfa_fm2jscontext.h"
#include <time.h>
#include "core/fxcrt/include/fx_ext.h"
#include "fxjse/include/cfxjse_arguments.h"
#include "fxjse/include/cfxjse_class.h"
#include "fxjse/include/cfxjse_value.h"
#include "xfa/fgas/localization/fgas_locale.h"
#include "xfa/fxfa/app/xfa_ffnotify.h"
#include "xfa/fxfa/fm2js/xfa_program.h"
#include "xfa/fxfa/parser/xfa_document.h"
#include "xfa/fxfa/parser/xfa_localevalue.h"
#include "xfa/fxfa/parser/xfa_parser.h"
#include "xfa/fxfa/parser/xfa_parser_imp.h"
#include "xfa/fxfa/parser/xfa_script_imp.h"
namespace {
const double kFinancialPrecision = 0.00000001;
struct XFA_FMHtmlReserveCode {
uint32_t m_uCode;
const FX_WCHAR* m_htmlReserve;
};
struct XFA_FMHtmlHashedReserveCode {
uint32_t m_uHash;
uint32_t m_uCode;
};
const XFA_FMHtmlHashedReserveCode reservesForDecode[] = {
{0x00018b62, /*L"Mu",*/ 924}, {0x00019083, /*L"Nu",*/ 925},
{0x00019ab9, /*L"Pi",*/ 928}, {0x0001c3c1, /*L"Xi",*/ 926},
{0x000210ac, /*L"ge",*/ 8805}, {0x000210bb, /*L"gt",*/ 62},
{0x00022a51, /*L"le",*/ 8804}, {0x00022a60, /*L"lt",*/ 60},
{0x00022f82, /*L"mu",*/ 956}, {0x00023493, /*L"ne",*/ 8800},
{0x00023497, /*L"ni",*/ 8715}, {0x000234a3, /*L"nu",*/ 957},
{0x000239c1, /*L"or",*/ 8744}, {0x00023ed9, /*L"pi",*/ 960},
{0x000267e1, /*L"xi",*/ 958}, {0x00c41789, /*L"lceil",*/ 8968},
{0x00eef34f, /*L"thetasym",*/ 977}, {0x012d7ead, /*L"lcirc",*/ 206},
{0x01637b56, /*L"agrave",*/ 224}, {0x020856da, /*L"crarr",*/ 8629},
{0x022188c3, /*L"gamma",*/ 947}, {0x033586d3, /*L"nbsp",*/ 160},
{0x04f4c358, /*L"nsub",*/ 8836}, {0x0581466a, /*L"dagger",*/ 8224},
{0x06b1f790, /*L"oelig",*/ 339}, {0x06e490d4, /*L"Chi",*/ 935},
{0x0718c6a1, /*L"ETH",*/ 208}, {0x07196ada, /*L"Eta",*/ 919},
{0x07f667ca, /*L"Ugrave",*/ 217}, {0x083a8a21, /*L"Phi",*/ 934},
{0x083ac28c, /*L"Psi",*/ 936}, {0x086f26a9, /*L"Rho",*/ 929},
{0x089b5b51, /*L"aring",*/ 229}, {0x08a39f4a, /*L"Tau",*/ 932},
{0x08b6188b, /*L"THORN",*/ 222}, {0x09ce792a, /*L"icirc",*/ 238},
{0x09f9d61e, /*L"amp",*/ 38}, {0x09f9db33, /*L"and",*/ 8743},
{0x09f9db36, /*L"ang",*/ 8736}, {0x0a2e3514, /*L"cap",*/ 8745},
{0x0a2e58f4, /*L"chi",*/ 967}, {0x0a2e9ba8, /*L"cup",*/ 8746},
{0x0a4897d0, /*L"deg",*/ 176}, {0x0a6332fa, /*L"eta",*/ 951},
{0x0a633301, /*L"eth",*/ 240}, {0x0acc4d4b, /*L"int",*/ 8747},
{0x0b1b3d35, /*L"loz",*/ 9674}, {0x0b1b4c8b, /*L"lrm",*/ 8206},
{0x0b4fd9b1, /*L"not",*/ 172}, {0x0b845241, /*L"phi",*/ 966},
{0x0b84576f, /*L"piv",*/ 982}, {0x0b848aac, /*L"psi",*/ 968},
{0x0bb8df5e, /*L"reg",*/ 174}, {0x0bb8eec9, /*L"rho",*/ 961},
{0x0bb9034b, /*L"rlm",*/ 8207}, {0x0bd33d14, /*L"shy",*/ 173},
{0x0bd34229, /*L"sim",*/ 8764}, {0x0bd37faa, /*L"sub",*/ 8834},
{0x0bd37fb5, /*L"sum",*/ 8721}, {0x0bd37fb8, /*L"sup",*/ 8835},
{0x0bed676a, /*L"tau",*/ 964}, {0x0c07f32e, /*L"uml",*/ 168},
{0x0c71032c, /*L"yen",*/ 165}, {0x0c7f2889, /*L"szlig",*/ 223},
{0x0c8badbb, /*L"zwj",*/ 8205}, {0x10ba4dba, /*L"Egrave",*/ 200},
{0x10f1ea24, /*L"para",*/ 182}, {0x10f1ea37, /*L"part",*/ 8706},
{0x115b2337, /*L"perp",*/ 8869}, {0x12b10d15, /*L"prod",*/ 8719},
{0x12b10d21, /*L"prop",*/ 8733}, {0x12dfa9f4, /*L"rfloor",*/ 8971},
{0x12eb4736, /*L"Agrave",*/ 192}, {0x12fff2b7, /*L"pund",*/ 163},
{0x13fda9f2, /*L"tilde",*/ 732}, {0x1417fd62, /*L"times",*/ 215},
{0x154fc726, /*L"ecirc",*/ 234}, {0x165aa451, /*L"sigma",*/ 963},
{0x1709124a, /*L"Dagger",*/ 8225}, {0x192f78d5, /*L"iexcl",*/ 161},
{0x1b7ed8d7, /*L"rArr",*/ 8658}, {0x1ec88c68, /*L"rang",*/ 9002},
{0x1ec8a0f7, /*L"rarr",*/ 8594}, {0x1eda07f3, /*L"atilde",*/ 227},
{0x1f3182c4, /*L"real",*/ 8476}, {0x1fc34f8b, /*L"yacute",*/ 253},
{0x20d11522, /*L"acirc",*/ 226}, {0x21933a9b, /*L"rsaquo",*/ 8250},
{0x21f44907, /*L"uacute",*/ 250}, {0x220cca72, /*L"acute",*/ 180},
{0x242cded1, /*L"alefsym",*/ 8501}, {0x2655c66a, /*L"delta",*/ 948},
{0x269e4b4d, /*L"exist",*/ 8707}, {0x273379fa, /*L"micro",*/ 181},
{0x27a37440, /*L"forall",*/ 8704}, {0x2854e62c, /*L"minus",*/ 8722},
{0x28636f81, /*L"cedil",*/ 184}, {0x2887357b, /*L"iacute",*/ 237},
{0x2994d5ff, /*L"frac12",*/ 189}, {0x2994d601, /*L"frac14",*/ 188},
{0x2994e043, /*L"frac34",*/ 190}, {0x2a1feb41, /*L"lambda",*/ 955},
{0x2ab215f3, /*L"apos",*/ 39}, {0x2ab82ef7, /*L"eacute",*/ 233},
{0x2b3592ef, /*L"auml",*/ 228}, {0x2ce92873, /*L"aacute",*/ 225},
{0x2daff48a, /*L"oslash",*/ 248}, {0x2ef68882, /*L"aelig",*/ 230},
{0x3061d3d3, /*L"Atilde",*/ 195}, {0x314b1b6b, /*L"Yacute",*/ 221},
{0x337c14e7, /*L"Uacute",*/ 218}, {0x37676aca, /*L"cent",*/ 162},
{0x37d0b841, /*L"circ",*/ 710}, {0x386e7947, /*L"cong",*/ 8773},
{0x386e839b, /*L"copy",*/ 169}, {0x3a0e225a, /*L"Epsilon",*/ 917},
{0x3ba7b721, /*L"Lambda",*/ 923}, {0x3bd9abe6, /*L"Alpha",*/ 913},
{0x3c3ffad7, /*L"Eacute",*/ 201}, {0x3cfaf69f, /*L"brvbar",*/ 166},
{0x3d54a489, /*L"omega",*/ 969}, {0x3e70f453, /*L"Aacute",*/ 193},
{0x3f37c06a, /*L"Oslash",*/ 216}, {0x40e1b34e, /*L"diams",*/ 9830},
{0x416596df, /*L"plusmn",*/ 177}, {0x4354ff16, /*L"Ucirc",*/ 219},
{0x454fce6a, /*L"Upsilon",*/ 933}, {0x4610ad35, /*L"emsp",*/ 8195},
{0x462afb76, /*L"ensp",*/ 8194}, {0x46e30073, /*L"euml",*/ 235},
{0x46e31a1b, /*L"euro",*/ 8364}, {0x46f2eada, /*L"lowast",*/ 8727},
{0x4dca26cf, /*L"Auml",*/ 196}, {0x4e2d6083, /*L"image",*/ 8465},
{0x4f964ee8, /*L"notin",*/ 8713}, {0x50917a7a, /*L"epsilon",*/ 949},
{0x52f9a4cd, /*L"Kappa",*/ 922}, {0x5496f410, /*L"Ocirc",*/ 212},
{0x568cbf34, /*L"zeta",*/ 950}, {0x57badd20, /*L"ntilde",*/ 241},
{0x58662109, /*L"zwnj",*/ 8204}, {0x5b39870f, /*L"empty",*/ 8709},
{0x5bd3268a, /*L"upsilon",*/ 965}, {0x5e2bf8a3, /*L"Gamma",*/ 915},
{0x5f73c13a, /*L"rsquo",*/ 8217}, {0x61f2bc4d, /*L"iota",*/ 953},
{0x625bbcf3, /*L"isin",*/ 8712}, {0x62906df7, /*L"iuml",*/ 239},
{0x64a5cb31, /*L"Aring",*/ 197}, {0x66f25c4a, /*L"sbquo",*/ 8218},
{0x6851ab60, /*L"spades",*/ 9824}, {0x6942a900, /*L"Ntilde",*/ 209},
{0x69779453, /*L"Euml",*/ 203}, {0x6cda6e23, /*L"current",*/ 164},
{0x70b5b634, /*L"lsquo",*/ 8216}, {0x715a3706, /*L"Ecirc",*/ 202},
{0x71e8bf8d, /*L"tdquo",*/ 8221}, {0x72651431, /*L"Sigma",*/ 931},
{0x7569813b, /*L"iquest",*/ 191}, {0x776a436a, /*L"equiv",*/ 8801},
{0x79215314, /*L"Zeta",*/ 918}, {0x79b81224, /*L"ograve",*/ 242},
{0x7c2f8b23, /*L"macr",*/ 175}, {0x7cdb8502, /*L"Acirc",*/ 194},
{0x8185c62e, /*L"ndash",*/ 8211}, {0x8260364a, /*L"Delta",*/ 916},
{0x846619ad, /*L"mdash",*/ 8212}, {0x8550fb50, /*L"OElig",*/ 338},
{0x88eb5b85, /*L"ldquo",*/ 8220}, {0x8b3fde04, /*L"Ograve",*/ 210},
{0x8bc5794b, /*L"ordf",*/ 170}, {0x8bc57952, /*L"ordm",*/ 186},
{0x8c14923d, /*L"ouml",*/ 246}, {0x8c5a7cd6, /*L"theta",*/ 952},
{0x8d61812b, /*L"thorn",*/ 254}, {0x912b95aa, /*L"asymp",*/ 8776},
{0x947faf81, /*L"middot",*/ 183}, {0x9629202e, /*L"lfloor",*/ 8970},
{0x972e9ec1, /*L"otilde",*/ 245}, {0x9748f231, /*L"otimes",*/ 8855},
{0x995f1469, /*L"Omega",*/ 937}, {0x99eb5349, /*L"quot",*/ 34},
{0x9aeb639e, /*L"hellip",*/ 8230}, {0xa0ae2f86, /*L"Scaron",*/ 352},
{0xa4dcb0d5, /*L"lsaquo",*/ 8249}, {0xa53dbf41, /*L"oacute",*/ 243},
{0xa5ae9e7b, /*L"bdquo",*/ 8222}, {0xa602d7ba, /*L"sdot",*/ 8901},
{0xa61ce86f, /*L"sect",*/ 167}, {0xa6e4c3d7, /*L"sigmaf",*/ 962},
{0xa7c1c74f, /*L"sube",*/ 8838}, {0xa7c20ee9, /*L"sup1",*/ 185},
{0xa7c20eea, /*L"sup2",*/ 178}, {0xa7c20eeb, /*L"sup3",*/ 179},
{0xa7c20f1d, /*L"supe",*/ 8839}, {0xa8b66aa1, /*L"Otilde",*/ 213},
{0xad958c42, /*L"AElig",*/ 198}, {0xaea9261d, /*L"Ouml",*/ 214},
{0xb040eafa, /*L"uArr",*/ 8657}, {0xb07c2e1c, /*L"beta",*/ 946},
{0xb220e92f, /*L"bull",*/ 8226}, {0xb22750c4, /*L"ccedil",*/ 231},
{0xb38ab31a, /*L"uarr",*/ 8593}, {0xb598b683, /*L"uuml",*/ 252},
{0xb6c58b21, /*L"Oacute",*/ 211}, {0xb6d2a617, /*L"oline",*/ 8254},
{0xba9fd989, /*L"dArr",*/ 8659}, {0xbb5ccd41, /*L"lgrave",*/ 204},
{0xbd39b44c, /*L"weierp",*/ 8472}, {0xbde9a1a9, /*L"darr",*/ 8595},
{0xc027e329, /*L"permil",*/ 8240}, {0xc2451389, /*L"upsih",*/ 978},
{0xc3af1ca4, /*L"Ccedil",*/ 199}, {0xcd164249, /*L"fnof",*/ 402},
{0xcf6c8467, /*L"hearts",*/ 9829}, {0xd1228390, /*L"trade",*/ 8482},
{0xd1462407, /*L"yuml",*/ 255}, {0xd2cf2253, /*L"oplus",*/ 8853},
{0xd310c1fc, /*L"Beta",*/ 914}, {0xd59c4d74, /*L"infin",*/ 8734},
{0xd64d470d, /*L"hArr",*/ 8660}, {0xd67d9c75, /*L"divide",*/ 247},
{0xd698dd37, /*L"Omicron",*/ 927}, {0xd82d4a63, /*L"Uuml",*/ 220},
{0xd9970f2d, /*L"harr",*/ 8596}, {0xda91fd99, /*L"clubs",*/ 9827},
{0xdbe5bdcc, /*L"there4",*/ 8756}, {0xdd7671bd, /*L"prime",*/ 8242},
{0xdfcf3c06, /*L"alpha",*/ 945}, {0xe0213063, /*L"saron",*/ 353},
{0xe1911d83, /*L"radic",*/ 8730}, {0xe2e75468, /*L"raquo",*/ 187},
{0xe6e27a5e, /*L"lacute",*/ 205}, {0xe74a8f36, /*L"ucirc",*/ 251},
{0xe864ecb6, /*L"Theta",*/ 920}, {0xecddde5e, /*L"nabla",*/ 8711},
{0xed1c3557, /*L"omicron",*/ 959}, {0xef82228f, /*L"rceil",*/ 8969},
{0xf1fab491, /*L"lArr",*/ 8656}, {0xf3dab7e7, /*L"Yuml",*/ 376},
{0xf4294962, /*L"laquo",*/ 171}, {0xf5446822, /*L"lang",*/ 9001},
{0xf5447cb1, /*L"larr",*/ 8592}, {0xf66e9bea, /*L"ugrave",*/ 249},
{0xf6b4ce70, /*L"lota",*/ 921}, {0xf6ef34ed, /*L"kappa",*/ 954},
{0xf72a3a56, /*L"thinsp",*/ 8201}, {0xf752801a, /*L"luml",*/ 207},
{0xf88c8430, /*L"ocirc",*/ 244}, {0xf9676178, /*L"frasl",*/ 8260},
{0xfd01885e, /*L"igrave",*/ 236}, {0xff3281da, /*L"egrave",*/ 232},
};
const XFA_FMHtmlReserveCode reservesForEncode[] = {
{34, L"quot"}, {38, L"amp"}, {39, L"apos"},
{60, L"lt"}, {62, L"gt"}, {160, L"nbsp"},
{161, L"iexcl"}, {162, L"cent"}, {163, L"pund"},
{164, L"current"}, {165, L"yen"}, {166, L"brvbar"},
{167, L"sect"}, {168, L"uml"}, {169, L"copy"},
{170, L"ordf"}, {171, L"laquo"}, {172, L"not"},
{173, L"shy"}, {174, L"reg"}, {175, L"macr"},
{176, L"deg"}, {177, L"plusmn"}, {178, L"sup2"},
{179, L"sup3"}, {180, L"acute"}, {181, L"micro"},
{182, L"para"}, {183, L"middot"}, {184, L"cedil"},
{185, L"sup1"}, {186, L"ordm"}, {187, L"raquo"},
{188, L"frac14"}, {189, L"frac12"}, {190, L"frac34"},
{191, L"iquest"}, {192, L"Agrave"}, {193, L"Aacute"},
{194, L"Acirc"}, {195, L"Atilde"}, {196, L"Auml"},
{197, L"Aring"}, {198, L"AElig"}, {199, L"Ccedil"},
{200, L"Egrave"}, {201, L"Eacute"}, {202, L"Ecirc"},
{203, L"Euml"}, {204, L"lgrave"}, {205, L"lacute"},
{206, L"lcirc"}, {207, L"luml"}, {208, L"ETH"},
{209, L"Ntilde"}, {210, L"Ograve"}, {211, L"Oacute"},
{212, L"Ocirc"}, {213, L"Otilde"}, {214, L"Ouml"},
{215, L"times"}, {216, L"Oslash"}, {217, L"Ugrave"},
{218, L"Uacute"}, {219, L"Ucirc"}, {220, L"Uuml"},
{221, L"Yacute"}, {222, L"THORN"}, {223, L"szlig"},
{224, L"agrave"}, {225, L"aacute"}, {226, L"acirc"},
{227, L"atilde"}, {228, L"auml"}, {229, L"aring"},
{230, L"aelig"}, {231, L"ccedil"}, {232, L"egrave"},
{233, L"eacute"}, {234, L"ecirc"}, {235, L"euml"},
{236, L"igrave"}, {237, L"iacute"}, {238, L"icirc"},
{239, L"iuml"}, {240, L"eth"}, {241, L"ntilde"},
{242, L"ograve"}, {243, L"oacute"}, {244, L"ocirc"},
{245, L"otilde"}, {246, L"ouml"}, {247, L"divide"},
{248, L"oslash"}, {249, L"ugrave"}, {250, L"uacute"},
{251, L"ucirc"}, {252, L"uuml"}, {253, L"yacute"},
{254, L"thorn"}, {255, L"yuml"}, {338, L"OElig"},
{339, L"oelig"}, {352, L"Scaron"}, {353, L"saron"},
{376, L"Yuml"}, {402, L"fnof"}, {710, L"circ"},
{732, L"tilde"}, {913, L"Alpha"}, {914, L"Beta"},
{915, L"Gamma"}, {916, L"Delta"}, {917, L"Epsilon"},
{918, L"Zeta"}, {919, L"Eta"}, {920, L"Theta"},
{921, L"lota"}, {922, L"Kappa"}, {923, L"Lambda"},
{924, L"Mu"}, {925, L"Nu"}, {926, L"Xi"},
{927, L"Omicron"}, {928, L"Pi"}, {929, L"Rho"},
{931, L"Sigma"}, {932, L"Tau"}, {933, L"Upsilon"},
{934, L"Phi"}, {935, L"Chi"}, {936, L"Psi"},
{937, L"Omega"}, {945, L"alpha"}, {946, L"beta"},
{947, L"gamma"}, {948, L"delta"}, {949, L"epsilon"},
{950, L"zeta"}, {951, L"eta"}, {952, L"theta"},
{953, L"iota"}, {954, L"kappa"}, {955, L"lambda"},
{956, L"mu"}, {957, L"nu"}, {958, L"xi"},
{959, L"omicron"}, {960, L"pi"}, {961, L"rho"},
{962, L"sigmaf"}, {963, L"sigma"}, {964, L"tau"},
{965, L"upsilon"}, {966, L"phi"}, {967, L"chi"},
{968, L"psi"}, {969, L"omega"}, {977, L"thetasym"},
{978, L"upsih"}, {982, L"piv"}, {8194, L"ensp"},
{8195, L"emsp"}, {8201, L"thinsp"}, {8204, L"zwnj"},
{8205, L"zwj"}, {8206, L"lrm"}, {8207, L"rlm"},
{8211, L"ndash"}, {8212, L"mdash"}, {8216, L"lsquo"},
{8217, L"rsquo"}, {8218, L"sbquo"}, {8220, L"ldquo"},
{8221, L"tdquo"}, {8222, L"bdquo"}, {8224, L"dagger"},
{8225, L"Dagger"}, {8226, L"bull"}, {8230, L"hellip"},
{8240, L"permil"}, {8242, L"prime"}, {8249, L"lsaquo"},
{8250, L"rsaquo"}, {8254, L"oline"}, {8260, L"frasl"},
{8364, L"euro"}, {8465, L"image"}, {8472, L"weierp"},
{8476, L"real"}, {8482, L"trade"}, {8501, L"alefsym"},
{8592, L"larr"}, {8593, L"uarr"}, {8594, L"rarr"},
{8595, L"darr"}, {8596, L"harr"}, {8629, L"crarr"},
{8656, L"lArr"}, {8657, L"uArr"}, {8658, L"rArr"},
{8659, L"dArr"}, {8660, L"hArr"}, {8704, L"forall"},
{8706, L"part"}, {8707, L"exist"}, {8709, L"empty"},
{8711, L"nabla"}, {8712, L"isin"}, {8713, L"notin"},
{8715, L"ni"}, {8719, L"prod"}, {8721, L"sum"},
{8722, L"minus"}, {8727, L"lowast"}, {8730, L"radic"},
{8733, L"prop"}, {8734, L"infin"}, {8736, L"ang"},
{8743, L"and"}, {8744, L"or"}, {8745, L"cap"},
{8746, L"cup"}, {8747, L"int"}, {8756, L"there4"},
{8764, L"sim"}, {8773, L"cong"}, {8776, L"asymp"},
{8800, L"ne"}, {8801, L"equiv"}, {8804, L"le"},
{8805, L"ge"}, {8834, L"sub"}, {8835, L"sup"},
{8836, L"nsub"}, {8838, L"sube"}, {8839, L"supe"},
{8853, L"oplus"}, {8855, L"otimes"}, {8869, L"perp"},
{8901, L"sdot"}, {8968, L"lceil"}, {8969, L"rceil"},
{8970, L"lfloor"}, {8971, L"rfloor"}, {9001, L"lang"},
{9002, L"rang"}, {9674, L"loz"}, {9824, L"spades"},
{9827, L"clubs"}, {9829, L"hearts"}, {9830, L"diams"},
};
const FXJSE_FUNCTION_DESCRIPTOR formcalc_fm2js_functions[] = {
{"Abs", CXFA_FM2JSContext::Abs},
{"Avg", CXFA_FM2JSContext::Avg},
{"Ceil", CXFA_FM2JSContext::Ceil},
{"Count", CXFA_FM2JSContext::Count},
{"Floor", CXFA_FM2JSContext::Floor},
{"Max", CXFA_FM2JSContext::Max},
{"Min", CXFA_FM2JSContext::Min},
{"Mod", CXFA_FM2JSContext::Mod},
{"Round", CXFA_FM2JSContext::Round},
{"Sum", CXFA_FM2JSContext::Sum},
{"Date", CXFA_FM2JSContext::Date},
{"Date2Num", CXFA_FM2JSContext::Date2Num},
{"DateFmt", CXFA_FM2JSContext::DateFmt},
{"IsoDate2Num", CXFA_FM2JSContext::IsoDate2Num},
{"IsoTime2Num", CXFA_FM2JSContext::IsoTime2Num},
{"LocalDateFmt", CXFA_FM2JSContext::LocalDateFmt},
{"LocalTimeFmt", CXFA_FM2JSContext::LocalTimeFmt},
{"Num2Date", CXFA_FM2JSContext::Num2Date},
{"Num2GMTime", CXFA_FM2JSContext::Num2GMTime},
{"Num2Time", CXFA_FM2JSContext::Num2Time},
{"Time", CXFA_FM2JSContext::Time},
{"Time2Num", CXFA_FM2JSContext::Time2Num},
{"TimeFmt", CXFA_FM2JSContext::TimeFmt},
{"Apr", CXFA_FM2JSContext::Apr},
{"Cterm", CXFA_FM2JSContext::CTerm},
{"FV", CXFA_FM2JSContext::FV},
{"Ipmt", CXFA_FM2JSContext::IPmt},
{"NPV", CXFA_FM2JSContext::NPV},
{"Pmt", CXFA_FM2JSContext::Pmt},
{"PPmt", CXFA_FM2JSContext::PPmt},
{"PV", CXFA_FM2JSContext::PV},
{"Rate", CXFA_FM2JSContext::Rate},
{"Term", CXFA_FM2JSContext::Term},
{"Choose", CXFA_FM2JSContext::Choose},
{"Exists", CXFA_FM2JSContext::Exists},
{"HasValue", CXFA_FM2JSContext::HasValue},
{"Oneof", CXFA_FM2JSContext::Oneof},
{"Within", CXFA_FM2JSContext::Within},
{"If", CXFA_FM2JSContext::If},
{"Eval", CXFA_FM2JSContext::Eval},
{"Translate", CXFA_FM2JSContext::eval_translation},
{"Ref", CXFA_FM2JSContext::Ref},
{"UnitType", CXFA_FM2JSContext::UnitType},
{"UnitValue", CXFA_FM2JSContext::UnitValue},
{"At", CXFA_FM2JSContext::At},
{"Concat", CXFA_FM2JSContext::Concat},
{"Decode", CXFA_FM2JSContext::Decode},
{"Encode", CXFA_FM2JSContext::Encode},
{"Format", CXFA_FM2JSContext::Format},
{"Left", CXFA_FM2JSContext::Left},
{"Len", CXFA_FM2JSContext::Len},
{"Lower", CXFA_FM2JSContext::Lower},
{"Ltrim", CXFA_FM2JSContext::Ltrim},
{"Parse", CXFA_FM2JSContext::Parse},
{"Replace", CXFA_FM2JSContext::Replace},
{"Right", CXFA_FM2JSContext::Right},
{"Rtrim", CXFA_FM2JSContext::Rtrim},
{"Space", CXFA_FM2JSContext::Space},
{"Str", CXFA_FM2JSContext::Str},
{"Stuff", CXFA_FM2JSContext::Stuff},
{"Substr", CXFA_FM2JSContext::Substr},
{"Uuid", CXFA_FM2JSContext::Uuid},
{"Upper", CXFA_FM2JSContext::Upper},
{"WordNum", CXFA_FM2JSContext::WordNum},
{"Get", CXFA_FM2JSContext::Get},
{"Post", CXFA_FM2JSContext::Post},
{"Put", CXFA_FM2JSContext::Put},
{"positive_operator", CXFA_FM2JSContext::positive_operator},
{"negative_operator", CXFA_FM2JSContext::negative_operator},
{"logical_or_operator", CXFA_FM2JSContext::logical_or_operator},
{"logical_and_operator", CXFA_FM2JSContext::logical_and_operator},
{"logical_not_operator", CXFA_FM2JSContext::logical_not_operator},
{"equality_operator", CXFA_FM2JSContext::equality_operator},
{"notequality_operator", CXFA_FM2JSContext::notequality_operator},
{"less_operator", CXFA_FM2JSContext::less_operator},
{"lessequal_operator", CXFA_FM2JSContext::lessequal_operator},
{"greater_operator", CXFA_FM2JSContext::greater_operator},
{"greaterequal_operator", CXFA_FM2JSContext::greaterequal_operator},
{"plus_operator", CXFA_FM2JSContext::plus_operator},
{"minus_operator", CXFA_FM2JSContext::minus_operator},
{"multiple_operator", CXFA_FM2JSContext::multiple_operator},
{"divide_operator", CXFA_FM2JSContext::divide_operator},
{"assign_value_operator", CXFA_FM2JSContext::assign_value_operator},
{"dot_accessor", CXFA_FM2JSContext::dot_accessor},
{"dotdot_accessor", CXFA_FM2JSContext::dotdot_accessor},
{"concat_fm_object", CXFA_FM2JSContext::concat_fm_object},
{"is_fm_object", CXFA_FM2JSContext::is_fm_object},
{"is_fm_array", CXFA_FM2JSContext::is_fm_array},
{"get_fm_value", CXFA_FM2JSContext::get_fm_value},
{"get_fm_jsobj", CXFA_FM2JSContext::get_fm_jsobj},
{"fm_var_filter", CXFA_FM2JSContext::fm_var_filter},
};
const FXJSE_CLASS_DESCRIPTOR formcalc_fm2js_descriptor = {
"XFA_FM2JS_FormCalcClass", // name
nullptr, // constructor
nullptr, // properties
formcalc_fm2js_functions, // methods
0, // number of properties
FX_ArraySize(formcalc_fm2js_functions), // number of methods
nullptr, // dynamic prop type
nullptr, // dynamic prop getter
nullptr, // dynamic prop setter
nullptr, // dynamic prop deleter
nullptr, // dynamic prop method call
};
const uint8_t g_sAltTable_Date[] = {
255, 255, 255, 3, 9, 255, 255, 255, 255, 255, 255,
255, 2, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 1, 255, 255, 255, 255, 255, 255, 255, 255,
};
static_assert(FX_ArraySize(g_sAltTable_Date) == L'a' - L'A' + 1,
"Invalid g_sAltTable_Date size.");
const uint8_t g_sAltTable_Time[] = {
14, 255, 255, 3, 9, 255, 255, 15, 255, 255, 255,
255, 6, 255, 255, 255, 255, 255, 7, 255, 255, 255,
255, 255, 1, 17, 255, 255, 255, 255, 255, 255, 255,
};
static_assert(FX_ArraySize(g_sAltTable_Time) == L'a' - L'A' + 1,
"Invalid g_sAltTable_Time size.");
void AlternateDateTimeSymbols(CFX_WideString& wsPattern,
const CFX_WideString& wsAltSymbols,
const uint8_t* pAltTable) {
int32_t nLength = wsPattern.GetLength();
FX_BOOL bInConstRange = FALSE;
FX_BOOL bEscape = FALSE;
int32_t i = 0;
while (i < nLength) {
FX_WCHAR wc = wsPattern[i];
if (wc == L'\'') {
bInConstRange = !bInConstRange;
if (bEscape) {
i++;
} else {
wsPattern.Delete(i);
nLength--;
}
bEscape = !bEscape;
continue;
}
if (!bInConstRange && wc >= L'A' && wc <= L'a') {
uint8_t nAlt = pAltTable[wc - L'A'];
if (nAlt != 255)
wsPattern.SetAt(i, wsAltSymbols[nAlt]);
}
i++;
bEscape = FALSE;
}
}
bool PatternStringType(const CFX_ByteStringC& szPattern,
uint32_t& patternType) {
CFX_WideString wsPattern = CFX_WideString::FromUTF8(szPattern);
if (FX_WSTRC(L"datetime") == wsPattern.Left(8)) {
patternType = XFA_VT_DATETIME;
return true;
}
if (FX_WSTRC(L"date") == wsPattern.Left(4)) {
patternType = wsPattern.Find(L"time") > 0 ? XFA_VT_DATETIME : XFA_VT_DATE;
return true;
}
if (FX_WSTRC(L"time") == wsPattern.Left(4)) {
patternType = XFA_VT_TIME;
return true;
}
if (FX_WSTRC(L"text") == wsPattern.Left(4)) {
patternType = XFA_VT_TEXT;
return true;
}
if (FX_WSTRC(L"num") == wsPattern.Left(3)) {
if (FX_WSTRC(L"integer") == wsPattern.Mid(4, 7)) {
patternType = XFA_VT_INTEGER;
} else if (FX_WSTRC(L"decimal") == wsPattern.Mid(4, 7)) {
patternType = XFA_VT_DECIMAL;
} else if (FX_WSTRC(L"currency") == wsPattern.Mid(4, 8)) {
patternType = XFA_VT_FLOAT;
} else if (FX_WSTRC(L"percent") == wsPattern.Mid(4, 7)) {
patternType = XFA_VT_FLOAT;
} else {
patternType = XFA_VT_FLOAT;
}
return true;
}
patternType = XFA_VT_NULL;
wsPattern.MakeLower();
const FX_WCHAR* pData = wsPattern.c_str();
int32_t iLength = wsPattern.GetLength();
int32_t iIndex = 0;
FX_BOOL bSingleQuotation = FALSE;
FX_WCHAR patternChar;
while (iIndex < iLength) {
patternChar = pData[iIndex];
if (patternChar == 0x27) {
bSingleQuotation = !bSingleQuotation;
} else if (!bSingleQuotation &&
(patternChar == 'y' || patternChar == 'j')) {
patternType = XFA_VT_DATE;
iIndex++;
FX_WCHAR timePatternChar;
while (iIndex < iLength) {
timePatternChar = pData[iIndex];
if (timePatternChar == 0x27) {
bSingleQuotation = !bSingleQuotation;
} else if (!bSingleQuotation && timePatternChar == 't') {
patternType = XFA_VT_DATETIME;
break;
}
iIndex++;
}
break;
} else if (!bSingleQuotation &&
(patternChar == 'h' || patternChar == 'k')) {
patternType = XFA_VT_TIME;
break;
} else if (!bSingleQuotation &&
(patternChar == 'a' || patternChar == 'x' ||
patternChar == 'o' || patternChar == '0')) {
patternType = XFA_VT_TEXT;
if (patternChar == 'x' || patternChar == 'o' || patternChar == '0') {
break;
}
} else if (!bSingleQuotation &&
(patternChar == 'z' || patternChar == 's' ||
patternChar == 'e' || patternChar == 'v' ||
patternChar == '8' || patternChar == ',' ||
patternChar == '.' || patternChar == '$')) {
patternType = XFA_VT_FLOAT;
if (patternChar == 'v' || patternChar == '8' || patternChar == '$') {
break;
}
}
iIndex++;
}
if (patternType == XFA_VT_NULL) {
patternType = XFA_VT_TEXT | XFA_VT_FLOAT;
}
return false;
}
CXFA_FM2JSContext* ToJSContext(CFXJSE_Value* pValue, CFXJSE_Class* pClass) {
return static_cast<CXFA_FM2JSContext*>(pValue->ToHostObject(pClass));
}
bool IsWhitespace(char c) {
return c == 0x20 || c == 0x09 || c == 0x0B || c == 0x0C || c == 0x0A ||
c == 0x0D;
}
} // namespace
// static
void CXFA_FM2JSContext::Abs(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
if (args.GetLength() != 1) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Abs");
return;
}
std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0);
if (ValueIsNull(pThis, argOne.get())) {
args.GetReturnValue()->SetNull();
return;
}
FX_DOUBLE dValue = ValueToDouble(pThis, argOne.get());
if (dValue < 0)
dValue = -dValue;
args.GetReturnValue()->SetDouble(dValue);
}
// static
void CXFA_FM2JSContext::Avg(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
int32_t argc = args.GetLength();
if (argc < 1) {
args.GetReturnValue()->SetNull();
return;
}
v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime();
uint32_t uCount = 0;
FX_DOUBLE dSum = 0.0;
for (int32_t i = 0; i < argc; i++) {
std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i);
if (argValue->IsNull())
continue;
if (!argValue->IsArray()) {
dSum += ValueToDouble(pThis, argValue.get());
uCount++;
continue;
}
std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate));
argValue->GetObjectProperty("length", lengthValue.get());
int32_t iLength = lengthValue->ToInteger();
if (iLength > 2) {
std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate));
argValue->GetObjectPropertyByIdx(1, propertyValue.get());
std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate));
if (propertyValue->IsNull()) {
for (int32_t j = 2; j < iLength; j++) {
argValue->GetObjectPropertyByIdx(j, jsObjectValue.get());
std::unique_ptr<CFXJSE_Value> defaultPropValue(
new CFXJSE_Value(pIsolate));
GetObjectDefaultValue(jsObjectValue.get(), defaultPropValue.get());
if (defaultPropValue->IsNull())
continue;
dSum += ValueToDouble(pThis, defaultPropValue.get());
uCount++;
}
} else {
for (int32_t j = 2; j < iLength; j++) {
argValue->GetObjectPropertyByIdx(j, jsObjectValue.get());
std::unique_ptr<CFXJSE_Value> newPropertyValue(
new CFXJSE_Value(pIsolate));
jsObjectValue->GetObjectProperty(
propertyValue->ToString().AsStringC(), newPropertyValue.get());
if (newPropertyValue->IsNull())
continue;
dSum += ValueToDouble(pThis, newPropertyValue.get());
uCount++;
}
}
}
}
if (uCount == 0) {
args.GetReturnValue()->SetNull();
return;
}
args.GetReturnValue()->SetDouble(dSum / uCount);
}
// static
void CXFA_FM2JSContext::Ceil(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
if (args.GetLength() != 1) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Ceil");
return;
}
std::unique_ptr<CFXJSE_Value> argValue = GetSimpleValue(pThis, args, 0);
if (ValueIsNull(pThis, argValue.get())) {
args.GetReturnValue()->SetNull();
return;
}
args.GetReturnValue()->SetFloat(
FXSYS_ceil(ValueToFloat(pThis, argValue.get())));
}
// static
void CXFA_FM2JSContext::Count(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr);
v8::Isolate* pIsolate = pContext->GetScriptRuntime();
int32_t iCount = 0;
for (int32_t i = 0; i < args.GetLength(); i++) {
std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i);
if (argValue->IsNull())
continue;
if (argValue->IsArray()) {
std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate));
argValue->GetObjectProperty("length", lengthValue.get());
int32_t iLength = lengthValue->ToInteger();
if (iLength <= 2) {
pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH);
return;
}
std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate));
std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate));
std::unique_ptr<CFXJSE_Value> newPropertyValue(
new CFXJSE_Value(pIsolate));
argValue->GetObjectPropertyByIdx(1, propertyValue.get());
argValue->GetObjectPropertyByIdx(2, jsObjectValue.get());
if (propertyValue->IsNull()) {
for (int32_t j = 2; j < iLength; j++) {
argValue->GetObjectPropertyByIdx(j, jsObjectValue.get());
GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get());
if (!newPropertyValue->IsNull())
iCount++;
}
} else {
for (int32_t j = 2; j < iLength; j++) {
argValue->GetObjectPropertyByIdx(j, jsObjectValue.get());
jsObjectValue->GetObjectProperty(
propertyValue->ToString().AsStringC(), newPropertyValue.get());
iCount += newPropertyValue->IsNull() ? 0 : 1;
}
}
} else if (argValue->IsObject()) {
std::unique_ptr<CFXJSE_Value> newPropertyValue(
new CFXJSE_Value(pIsolate));
GetObjectDefaultValue(argValue.get(), newPropertyValue.get());
if (!newPropertyValue->IsNull())
iCount++;
} else {
iCount++;
}
}
args.GetReturnValue()->SetInteger(iCount);
}
// static
void CXFA_FM2JSContext::Floor(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
if (args.GetLength() != 1) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Floor");
return;
}
std::unique_ptr<CFXJSE_Value> argValue = GetSimpleValue(pThis, args, 0);
if (ValueIsNull(pThis, argValue.get())) {
args.GetReturnValue()->SetNull();
return;
}
args.GetReturnValue()->SetFloat(
FXSYS_floor(ValueToFloat(pThis, argValue.get())));
}
// static
void CXFA_FM2JSContext::Max(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr);
v8::Isolate* pIsolate = pContext->GetScriptRuntime();
uint32_t uCount = 0;
FX_DOUBLE dMaxValue = 0.0;
for (int32_t i = 0; i < args.GetLength(); i++) {
std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i);
if (argValue->IsNull())
continue;
if (argValue->IsArray()) {
std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate));
argValue->GetObjectProperty("length", lengthValue.get());
int32_t iLength = lengthValue->ToInteger();
if (iLength <= 2) {
pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH);
return;
}
std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate));
std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate));
std::unique_ptr<CFXJSE_Value> newPropertyValue(
new CFXJSE_Value(pIsolate));
argValue->GetObjectPropertyByIdx(1, propertyValue.get());
argValue->GetObjectPropertyByIdx(2, jsObjectValue.get());
if (propertyValue->IsNull()) {
for (int32_t j = 2; j < iLength; j++) {
argValue->GetObjectPropertyByIdx(j, jsObjectValue.get());
GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get());
if (newPropertyValue->IsNull())
continue;
uCount++;
FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get());
dMaxValue = (uCount == 1) ? dValue : std::max(dMaxValue, dValue);
}
} else {
for (int32_t j = 2; j < iLength; j++) {
argValue->GetObjectPropertyByIdx(j, jsObjectValue.get());
jsObjectValue->GetObjectProperty(
propertyValue->ToString().AsStringC(), newPropertyValue.get());
if (newPropertyValue->IsNull())
continue;
uCount++;
FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get());
dMaxValue = (uCount == 1) ? dValue : std::max(dMaxValue, dValue);
}
}
} else if (argValue->IsObject()) {
std::unique_ptr<CFXJSE_Value> newPropertyValue(
new CFXJSE_Value(pIsolate));
GetObjectDefaultValue(argValue.get(), newPropertyValue.get());
if (newPropertyValue->IsNull())
continue;
uCount++;
FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get());
dMaxValue = (uCount == 1) ? dValue : std::max(dMaxValue, dValue);
} else {
uCount++;
FX_DOUBLE dValue = ValueToDouble(pThis, argValue.get());
dMaxValue = (uCount == 1) ? dValue : std::max(dMaxValue, dValue);
}
}
if (uCount == 0) {
args.GetReturnValue()->SetNull();
return;
}
args.GetReturnValue()->SetDouble(dMaxValue);
}
// static
void CXFA_FM2JSContext::Min(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr);
v8::Isolate* pIsolate = pContext->GetScriptRuntime();
uint32_t uCount = 0;
FX_DOUBLE dMinValue = 0.0;
for (int32_t i = 0; i < args.GetLength(); i++) {
std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i);
if (argValue->IsNull())
continue;
if (argValue->IsArray()) {
std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate));
argValue->GetObjectProperty("length", lengthValue.get());
int32_t iLength = lengthValue->ToInteger();
if (iLength <= 2) {
pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH);
return;
}
std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate));
std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate));
std::unique_ptr<CFXJSE_Value> newPropertyValue(
new CFXJSE_Value(pIsolate));
argValue->GetObjectPropertyByIdx(1, propertyValue.get());
argValue->GetObjectPropertyByIdx(2, jsObjectValue.get());
if (propertyValue->IsNull()) {
for (int32_t j = 2; j < iLength; j++) {
argValue->GetObjectPropertyByIdx(j, jsObjectValue.get());
GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get());
if (newPropertyValue->IsNull())
continue;
uCount++;
FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get());
dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue);
}
} else {
for (int32_t j = 2; j < iLength; j++) {
argValue->GetObjectPropertyByIdx(j, jsObjectValue.get());
jsObjectValue->GetObjectProperty(
propertyValue->ToString().AsStringC(), newPropertyValue.get());
if (newPropertyValue->IsNull())
continue;
uCount++;
FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get());
dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue);
}
}
} else if (argValue->IsObject()) {
std::unique_ptr<CFXJSE_Value> newPropertyValue(
new CFXJSE_Value(pIsolate));
GetObjectDefaultValue(argValue.get(), newPropertyValue.get());
if (newPropertyValue->IsNull())
continue;
uCount++;
FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get());
dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue);
} else {
uCount++;
FX_DOUBLE dValue = ValueToDouble(pThis, argValue.get());
dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue);
}
}
if (uCount == 0) {
args.GetReturnValue()->SetNull();
return;
}
args.GetReturnValue()->SetDouble(dMinValue);
}
// static
void CXFA_FM2JSContext::Mod(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr);
if (args.GetLength() != 2) {
pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Mod");
return;
}
std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0);
std::unique_ptr<CFXJSE_Value> argTwo = args.GetValue(1);
if (argOne->IsNull() || argTwo->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
bool argOneResult;
FX_DOUBLE dDividend = ExtractDouble(pThis, argOne.get(), &argOneResult);
bool argTwoResult;
FX_DOUBLE dDivisor = ExtractDouble(pThis, argTwo.get(), &argTwoResult);
if (!argOneResult || !argTwoResult) {
pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH);
return;
}
if (dDivisor == 0.0) {
pContext->ThrowException(XFA_IDS_DIVIDE_ZERO);
return;
}
args.GetReturnValue()->SetDouble(dDividend -
dDivisor * (int32_t)(dDividend / dDivisor));
}
// static
void CXFA_FM2JSContext::Round(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr);
int32_t argc = args.GetLength();
if (argc < 1 || argc > 2) {
pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Round");
return;
}
std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0);
if (argOne->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
bool dValueRet;
FX_DOUBLE dValue = ExtractDouble(pThis, argOne.get(), &dValueRet);
if (!dValueRet) {
pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH);
return;
}
uint8_t uPrecision = 0;
if (argc > 1) {
std::unique_ptr<CFXJSE_Value> argTwo = args.GetValue(1);
if (argTwo->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
bool dPrecisionRet;
FX_DOUBLE dPrecision = ExtractDouble(pThis, argTwo.get(), &dPrecisionRet);
if (!dPrecisionRet) {
pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH);
return;
}
uPrecision =
static_cast<uint8_t>(std::min(std::max(dPrecision, 0.0), 12.0));
}
CFX_Decimal decimalValue((FX_FLOAT)dValue, uPrecision);
CFX_WideString wsValue = decimalValue;
args.GetReturnValue()->SetString(wsValue.UTF8Encode().AsStringC());
}
// static
void CXFA_FM2JSContext::Sum(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
int32_t argc = args.GetLength();
if (argc == 0) {
args.GetReturnValue()->SetNull();
return;
}
CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr);
v8::Isolate* pIsolate = pContext->GetScriptRuntime();
uint32_t uCount = 0;
FX_DOUBLE dSum = 0.0;
for (int32_t i = 0; i < argc; i++) {
std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i);
if (argValue->IsNull())
continue;
if (argValue->IsArray()) {
std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate));
argValue->GetObjectProperty("length", lengthValue.get());
int32_t iLength = lengthValue->ToInteger();
if (iLength <= 2) {
pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH);
return;
}
std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate));
argValue->GetObjectPropertyByIdx(1, propertyValue.get());
std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate));
std::unique_ptr<CFXJSE_Value> newPropertyValue(
new CFXJSE_Value(pIsolate));
if (propertyValue->IsNull()) {
for (int32_t j = 2; j < iLength; j++) {
argValue->GetObjectPropertyByIdx(j, jsObjectValue.get());
GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get());
if (newPropertyValue->IsNull())
continue;
dSum += ValueToDouble(pThis, jsObjectValue.get());
uCount++;
}
} else {
for (int32_t j = 2; j < iLength; j++) {
argValue->GetObjectPropertyByIdx(j, jsObjectValue.get());
jsObjectValue->GetObjectProperty(
propertyValue->ToString().AsStringC(), newPropertyValue.get());
if (newPropertyValue->IsNull())
continue;
dSum += ValueToDouble(pThis, newPropertyValue.get());
uCount++;
}
}
} else if (argValue->IsObject()) {
std::unique_ptr<CFXJSE_Value> newPropertyValue(
new CFXJSE_Value(pIsolate));
GetObjectDefaultValue(argValue.get(), newPropertyValue.get());
if (newPropertyValue->IsNull())
continue;
dSum += ValueToDouble(pThis, argValue.get());
uCount++;
} else {
dSum += ValueToDouble(pThis, argValue.get());
uCount++;
}
}
if (uCount == 0) {
args.GetReturnValue()->SetNull();
return;
}
args.GetReturnValue()->SetDouble(dSum);
}
// static
void CXFA_FM2JSContext::Date(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
if (args.GetLength() != 0) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Date");
return;
}
time_t currentTime;
time(&currentTime);
struct tm* pTmStruct = gmtime(&currentTime);
CFX_ByteString bufferYear;
CFX_ByteString bufferMon;
CFX_ByteString bufferDay;
bufferYear.Format("%d", pTmStruct->tm_year + 1900);
bufferMon.Format("%02d", pTmStruct->tm_mon + 1);
bufferDay.Format("%02d", pTmStruct->tm_mday);
CFX_ByteString bufferCurrent = bufferYear + bufferMon + bufferDay;
args.GetReturnValue()->SetInteger(DateString2Num(bufferCurrent.AsStringC()));
}
// static
void CXFA_FM2JSContext::Date2Num(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
int32_t argc = args.GetLength();
if (argc < 1 || argc > 3) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Date2Num");
return;
}
std::unique_ptr<CFXJSE_Value> dateValue = GetSimpleValue(pThis, args, 0);
if (ValueIsNull(pThis, dateValue.get())) {
args.GetReturnValue()->SetNull();
return;
}
CFX_ByteString dateString;
ValueToUTF8String(dateValue.get(), dateString);
CFX_ByteString formatString;
if (argc > 1) {
std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1);
if (ValueIsNull(pThis, formatValue.get())) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(formatValue.get(), formatString);
}
CFX_ByteString localString;
if (argc > 2) {
std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2);
if (ValueIsNull(pThis, localValue.get())) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(localValue.get(), localString);
}
CFX_ByteString szIsoDateString;
if (!Local2IsoDate(pThis, dateString.AsStringC(), formatString.AsStringC(),
localString.AsStringC(), szIsoDateString)) {
args.GetReturnValue()->SetInteger(0);
return;
}
args.GetReturnValue()->SetInteger(
DateString2Num(szIsoDateString.AsStringC()));
}
// static
void CXFA_FM2JSContext::DateFmt(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
int32_t argc = args.GetLength();
if (argc > 2) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Date2Num");
return;
}
int32_t iStyle = 0;
if (argc > 0) {
std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0);
if (argStyle->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
iStyle = (int32_t)ValueToFloat(pThis, argStyle.get());
if (iStyle < 0 || iStyle > 4)
iStyle = 0;
}
CFX_ByteString szLocal;
if (argc > 1) {
std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1);
if (argLocal->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(argLocal.get(), szLocal);
}
CFX_ByteString formatStr;
GetStandardDateFormat(pThis, iStyle, szLocal.AsStringC(), formatStr);
args.GetReturnValue()->SetString(formatStr.AsStringC());
}
// static
void CXFA_FM2JSContext::IsoDate2Num(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
if (args.GetLength() != 1) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"IsoDate2Num");
return;
}
std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0);
if (argOne->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
CFX_ByteString szArgString;
ValueToUTF8String(argOne.get(), szArgString);
args.GetReturnValue()->SetInteger(DateString2Num(szArgString.AsStringC()));
}
// static
void CXFA_FM2JSContext::IsoTime2Num(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr);
if (args.GetLength() != 1) {
pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD,
L"IsoTime2Num");
return;
}
std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0);
if (ValueIsNull(pThis, argOne.get())) {
args.GetReturnValue()->SetNull();
return;
}
CXFA_Document* pDoc = pContext->GetDocument();
CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr();
CFX_ByteString szArgString;
ValueToUTF8String(argOne.get(), szArgString);
szArgString = szArgString.Mid(szArgString.Find('T', 0) + 1);
if (szArgString.IsEmpty()) {
args.GetReturnValue()->SetInteger(0);
return;
}
CXFA_LocaleValue timeValue(
XFA_VT_TIME, CFX_WideString::FromUTF8(szArgString.AsStringC()), pMgr);
if (!timeValue.IsValid()) {
args.GetReturnValue()->SetInteger(0);
return;
}
CFX_Unitime uniTime = timeValue.GetTime();
int32_t hour = uniTime.GetHour();
int32_t min = uniTime.GetMinute();
int32_t second = uniTime.GetSecond();
int32_t milSecond = uniTime.GetMillisecond();
FX_TIMEZONE tzLocale;
pMgr->GetDefLocale()->GetTimeZone(tzLocale);
// TODO(dsinclair): See if there is other time conversion code in pdfium and
// consolidate.
int32_t mins = hour * 60 + min;
mins -= (tzLocale.tzHour * 60);
while (mins > 1440)
mins -= 1440;
while (mins < 0)
mins += 1440;
hour = mins / 60;
min = mins % 60;
args.GetReturnValue()->SetInteger(hour * 3600000 + min * 60000 +
second * 1000 + milSecond + 1);
}
// static
void CXFA_FM2JSContext::LocalDateFmt(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
int32_t argc = args.GetLength();
if (argc > 2) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"LocalDateFmt");
return;
}
int32_t iStyle = 0;
if (argc > 0) {
std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0);
if (argStyle->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
iStyle = (int32_t)ValueToFloat(pThis, argStyle.get());
if (iStyle > 4 || iStyle < 0)
iStyle = 0;
}
CFX_ByteString szLocal;
if (argc > 1) {
std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1);
if (argLocal->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(argLocal.get(), szLocal);
}
CFX_ByteString formatStr;
GetLocalDateFormat(pThis, iStyle, szLocal.AsStringC(), formatStr, FALSE);
args.GetReturnValue()->SetString(formatStr.AsStringC());
}
// static
void CXFA_FM2JSContext::LocalTimeFmt(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
int32_t argc = args.GetLength();
if (argc > 2) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"LocalTimeFmt");
return;
}
int32_t iStyle = 0;
if (argc > 0) {
std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0);
if (argStyle->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
iStyle = (int32_t)ValueToFloat(pThis, argStyle.get());
if (iStyle > 4 || iStyle < 0)
iStyle = 0;
}
CFX_ByteString szLocal;
if (argc > 1) {
std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1);
if (argLocal->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(argLocal.get(), szLocal);
}
CFX_ByteString formatStr;
GetLocalTimeFormat(pThis, iStyle, szLocal.AsStringC(), formatStr, FALSE);
args.GetReturnValue()->SetString(formatStr.AsStringC());
}
// static
void CXFA_FM2JSContext::Num2Date(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
int32_t argc = args.GetLength();
if (argc < 1 || argc > 3) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Num2Date");
return;
}
std::unique_ptr<CFXJSE_Value> dateValue = GetSimpleValue(pThis, args, 0);
if (ValueIsNull(pThis, dateValue.get())) {
args.GetReturnValue()->SetNull();
return;
}
int32_t dDate = (int32_t)ValueToFloat(pThis, dateValue.get());
if (dDate < 1) {
args.GetReturnValue()->SetNull();
return;
}
CFX_ByteString formatString;
if (argc > 1) {
std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1);
if (ValueIsNull(pThis, formatValue.get())) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(formatValue.get(), formatString);
}
CFX_ByteString localString;
if (argc > 2) {
std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2);
if (ValueIsNull(pThis, localValue.get())) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(localValue.get(), localString);
}
int32_t iYear = 1900;
int32_t iMonth = 1;
int32_t iDay = 1;
int32_t i = 0;
while (dDate > 0) {
if (iMonth == 2) {
if ((!((iYear + i) % 4) && ((iYear + i) % 100)) || !((iYear + i) % 400)) {
if (dDate > 29) {
++iMonth;
if (iMonth > 12) {
iMonth = 1;
++i;
}
iDay = 1;
dDate -= 29;
} else {
iDay += static_cast<int32_t>(dDate) - 1;
dDate = 0;
}
} else {
if (dDate > 28) {
++iMonth;
if (iMonth > 12) {
iMonth = 1;
++i;
}
iDay = 1;
dDate -= 28;
} else {
iDay += static_cast<int32_t>(dDate) - 1;
dDate = 0;
}
}
} else if (iMonth < 8) {
if ((iMonth % 2 == 0)) {
if (dDate > 30) {
++iMonth;
if (iMonth > 12) {
iMonth = 1;
++i;
}
iDay = 1;
dDate -= 30;
} else {
iDay += static_cast<int32_t>(dDate) - 1;
dDate = 0;
}
} else {
if (dDate > 31) {
++iMonth;
if (iMonth > 12) {
iMonth = 1;
++i;
}
iDay = 1;
dDate -= 31;
} else {
iDay += static_cast<int32_t>(dDate) - 1;
dDate = 0;
}
}
} else {
if (iMonth % 2 != 0) {
if (dDate > 30) {
++iMonth;
if (iMonth > 12) {
iMonth = 1;
++i;
}
iDay = 1;
dDate -= 30;
} else {
iDay += static_cast<int32_t>(dDate) - 1;
dDate = 0;
}
} else {
if (dDate > 31) {
++iMonth;
if (iMonth > 12) {
iMonth = 1;
++i;
}
iDay = 1;
dDate -= 31;
} else {
iDay += static_cast<int32_t>(dDate) - 1;
dDate = 0;
}
}
}
}
CFX_ByteString szIsoDateString;
szIsoDateString.Format("%d%02d%02d", iYear + i, iMonth, iDay);
CFX_ByteString szLocalDateString;
IsoDate2Local(pThis, szIsoDateString.AsStringC(), formatString.AsStringC(),
localString.AsStringC(), szLocalDateString);
args.GetReturnValue()->SetString(szLocalDateString.AsStringC());
}
// static
void CXFA_FM2JSContext::Num2GMTime(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
int32_t argc = args.GetLength();
if (argc < 1 || argc > 3) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Num2GMTime");
return;
}
std::unique_ptr<CFXJSE_Value> timeValue = GetSimpleValue(pThis, args, 0);
if (timeValue->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
int32_t iTime = (int32_t)ValueToFloat(pThis, timeValue.get());
if (FXSYS_abs(iTime) < 1.0) {
args.GetReturnValue()->SetNull();
return;
}
CFX_ByteString formatString;
if (argc > 1) {
std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1);
if (formatValue->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(formatValue.get(), formatString);
}
CFX_ByteString localString;
if (argc > 2) {
std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2);
if (localValue->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(localValue.get(), localString);
}
CFX_ByteString szGMTTimeString;
Num2AllTime(pThis, iTime, formatString.AsStringC(), localString.AsStringC(),
TRUE, szGMTTimeString);
args.GetReturnValue()->SetString(szGMTTimeString.AsStringC());
}
// static
void CXFA_FM2JSContext::Num2Time(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
int32_t argc = args.GetLength();
if (argc < 1 || argc > 3) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Num2Time");
return;
}
std::unique_ptr<CFXJSE_Value> timeValue = GetSimpleValue(pThis, args, 0);
if (timeValue->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
FX_FLOAT fTime = ValueToFloat(pThis, timeValue.get());
if (FXSYS_fabs(fTime) < 1.0) {
args.GetReturnValue()->SetNull();
return;
}
CFX_ByteString formatString;
if (argc > 1) {
std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1);
if (formatValue->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(formatValue.get(), formatString);
}
CFX_ByteString localString;
if (argc > 2) {
std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2);
if (localValue->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(localValue.get(), localString);
}
CFX_ByteString szLocalTimeString;
Num2AllTime(pThis, (int32_t)fTime, formatString.AsStringC(),
localString.AsStringC(), FALSE, szLocalTimeString);
args.GetReturnValue()->SetString(szLocalTimeString.AsStringC());
}
// static
void CXFA_FM2JSContext::Time(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
if (args.GetLength() != 0) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Time");
return;
}
time_t now;
time(&now);
struct tm* pGmt = gmtime(&now);
args.GetReturnValue()->SetInteger(
(pGmt->tm_hour * 3600 + pGmt->tm_min * 60 + pGmt->tm_sec) * 1000);
}
// static
void CXFA_FM2JSContext::Time2Num(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
int32_t argc = args.GetLength();
if (argc < 1 || argc > 3) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Time2Num");
return;
}
CFX_ByteString timeString;
std::unique_ptr<CFXJSE_Value> timeValue = GetSimpleValue(pThis, args, 0);
if (ValueIsNull(pThis, timeValue.get())) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(timeValue.get(), timeString);
CFX_ByteString formatString;
if (argc > 1) {
std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1);
if (ValueIsNull(pThis, formatValue.get())) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(formatValue.get(), formatString);
}
CFX_ByteString localString;
if (argc > 2) {
std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2);
if (ValueIsNull(pThis, localValue.get())) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(localValue.get(), localString);
}
CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument();
CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr();
IFX_Locale* pLocale = nullptr;
if (localString.IsEmpty()) {
CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject());
ASSERT(pThisNode);
CXFA_WidgetData widgetData(pThisNode);
pLocale = widgetData.GetLocal();
} else {
pLocale = pMgr->GetLocaleByName(
CFX_WideString::FromUTF8(localString.AsStringC()));
}
CFX_WideString wsFormat;
if (formatString.IsEmpty())
pLocale->GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY_Default, wsFormat);
else
wsFormat = CFX_WideString::FromUTF8(formatString.AsStringC());
wsFormat = L"time{" + wsFormat + L"}";
CXFA_LocaleValue localeValue(XFA_VT_TIME,
CFX_WideString::FromUTF8(timeString.AsStringC()),
wsFormat, pLocale, pMgr);
if (!localeValue.IsValid()) {
args.GetReturnValue()->SetInteger(0);
return;
}
CFX_Unitime uniTime = localeValue.GetTime();
int32_t hour = uniTime.GetHour();
int32_t min = uniTime.GetMinute();
int32_t second = uniTime.GetSecond();
int32_t milSecond = uniTime.GetMillisecond();
int32_t mins = hour * 60 + min;
CXFA_TimeZoneProvider* pProvider = CXFA_TimeZoneProvider::Get();
if (pProvider) {
FX_TIMEZONE tz;
pProvider->GetTimeZone(tz);
mins -= (tz.tzHour * 60);
while (mins > 1440)
mins -= 1440;
while (mins < 0)
mins += 1440;
hour = mins / 60;
min = mins % 60;
}
args.GetReturnValue()->SetInteger(hour * 3600000 + min * 60000 +
second * 1000 + milSecond + 1);
}
// static
void CXFA_FM2JSContext::TimeFmt(CFXJSE_Value* pThis,
const CFX_ByteStringC& szFuncName,
CFXJSE_Arguments& args) {
int32_t argc = args.GetLength();
if (argc > 2) {
ToJSContext(pThis, nullptr)
->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"TimeFmt");
return;
}
int32_t iStyle = 0;
if (argc > 0) {
std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0);
if (argStyle->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
iStyle = (int32_t)ValueToFloat(pThis, argStyle.get());
if (iStyle > 4 || iStyle < 0)
iStyle = 0;
}
CFX_ByteString szLocal;
if (argc > 1) {
std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1);
if (argLocal->IsNull()) {
args.GetReturnValue()->SetNull();
return;
}
ValueToUTF8String(argLocal.get(), szLocal);
}
CFX_ByteString formatStr;
GetStandardTimeFormat(pThis, iStyle, szLocal.AsStringC(), formatStr);
args.GetReturnValue()->SetString(formatStr.AsStringC());
}
// static
FX_BOOL CXFA_FM2JSContext::IsIsoDateFormat(const FX_CHAR* pData,
int32_t iLength,
int32_t& iStyle,
int32_t& iYear,
int32_t& iMonth,
int32_t& iDay) {
iYear = 0;
iMonth = 1;
iDay = 1;
if (iLength < 4)
return FALSE;
FX_CHAR strYear[5];
strYear[4] = '\0';
for (int32_t i = 0; i < 4; ++i) {
if (pData[i] > '9' || pData[i] < '0')
return FALSE;
strYear[i] = pData[i];
}
iYear = FXSYS_atoi(strYear);
iStyle = 0;
if (iLength == 4)
return TRUE;
iStyle = pData[4] == '-' ? 1 : 0;
FX_CHAR strTemp[3];
strTemp[2] = '\0';
int32_t iPosOff = iStyle == 0 ? 4 : 5;
if ((pData[iPosOff] > '9' || pData[iPosOff] < '0') ||
(pData[iPosOff + 1] > '9' || pData[iPosOff + 1] < '0'))
return FALSE;
strTemp[0] = pData[iPosOff];
strTemp[1] = pData[iPosOff + 1];
iMonth = FXSYS_atoi(strTemp);
if (iMonth > 12 || iMonth < 1)
return FALSE;
if (iStyle == 0) {
iPosOff += 2;
if (iLength == 6)
return TRUE;
} else {
iPosOff += 3;
if (iLength == 7)
return TRUE;
}
if ((pData[iPosOff] > '9' || pData[iPosOff] < '0') ||
(pData[iPosOff + 1] > '9' || pData[iPosOff + 1] < '0'))
return FALSE;
strTemp[0] = pData[iPosOff];
strTemp[1] = pData[iPosOff + 1];
iDay = FXSYS_atoi(strTemp);
if (iPosOff + 2 < iLength)
return FALSE;
if ((!(iYear % 4) && (iYear % 100)) || !(iYear % 400)) {
if (iMonth == 2 && iDay > 29)
return FALSE;
} else {
if (iMonth == 2 && iDay > 28)
return FALSE;
}
if (iMonth != 2) {
if (iMonth < 8) {
if (iDay > (iMonth % 2 == 0 ? 30 : 31))
return FALSE;
} else if (iDay > (iMonth % 2 == 0 ? 31 : 30)) {
return FALSE;
}
}
return TRUE;
}
// static
FX_BOOL CXFA_FM2JSContext::IsIsoTimeFormat(const FX_CHAR* pData,
int32_t iLength,
int32_t& iHour,
int32_t& iMinute,
int32_t& iSecond,
int32_t& iMilliSecond,
int32_t& iZoneHour,
int32_t& iZoneMinute) {
iHour = 0;
iMinute = 0;
iSecond = 0;
iMilliSecond = 0;
iZoneHour = 0;
iZoneMinute = 0;
if (!pData)
return FALSE;
FX_CHAR strTemp[3];
strTemp[2] = '\0';
int32_t iZone = 0;
int32_t i = 0;
while (i < iLength) {
if ((pData[i] > '9' || pData[i] < '0') && pData[i] != ':') {
iZone = i;
break;
}
++i;
}
if (i == iLength)
iZone = iLength;
int32_t iPos = 0;
int32_t iIndex = 0;
while (iIndex < iZone) {
if (iIndex >= iZone)
break;
if (pData[iIndex] > '9' || pData[iIndex] < '0')
return FALSE;
strTemp[0] = pData[iIndex];
if (pData[iIndex + 1] > '9' || pData[iIndex + 1] < '0')
return FALSE;
strTemp[1] = pData[iIndex + 1];
if (FXSYS_atoi(strTemp) > 60)
return FALSE;
if (pData[2] == ':') {
if (iPos == 0) {
iHour = FXSYS_atoi(strTemp);
++iPos;
} else if (iPos == 1) {
iMinute = FXSYS_atoi(strTemp);
++iPos;
} else {
iSecond = FXSYS_atoi(strTemp);
}
iIndex += 3;
} else {
if (iPos == 0) {
iHour = FXSYS_atoi(strTemp);
++iPos;
} else if (iPos == 1) {
iMinute = FXSYS_atoi(strTemp);
++iPos;
} else if (iPos == 2) {
iSecond = FXSYS_atoi(strTemp);
++iPos;
}
iIndex += 2;
}
}
if (pData[iIndex] == '.') {
++iIndex;
FX_CHAR strSec[4];
strSec[3] = '\0';
if (pData[iIndex] > '9' || pData[iIndex] < '0')
return FALSE;
strSec[0] = pData[iIndex];
if (pData[iIndex + 1] > '9' || pData[iIndex + 1] < '0')
return FALSE;
strSec[1] = pData[iIndex + 1];
if (pData[iIndex + 2] > '9' || pData[iIndex + 2] < '0')
return FALSE;
strSec[2] = pData[iIndex + 2];
iMilliSecond = FXSYS_atoi(strSec);
if (iMilliSecond > 100) {
iMilliSecond = 0;
return FALSE;
}
iIndex += 3;
}
if (pData[iIndex] == 'z' || pData[iIndex] == 'Z')
return TRUE;
int32_t iSign = 1;
if (pData[iIndex] == '+') {
++iIndex;
} else if (pData[iIndex] == '-') {
iSign = -1;
++iIndex;
}
iPos = 0;
while (iIndex < iLength) {
if (iIndex >= iLength)
return FALSE;
if (pData[iIndex] > '9' || pData[iIndex] < '0')
return FALSE;
strTemp[0] = pData[iIndex];
if (pData[iIndex + 1] > '9' || pData[iIndex + 1] < '0')
return FALSE;
strTemp[1] = pData[iIndex + 1];
if (FXSYS_atoi(strTemp) > 60)
return FALSE;
if (pData[2] == ':') {
if (iPos == 0) {
iZoneHour = FXSYS_atoi(strTemp);
} else if (iPos == 1) {
iZoneMinute = FXSYS_atoi(strTemp);
}
iIndex += 3;
} else {
if (!iPos) {
iZoneHour = FXSYS_atoi(strTemp);
++iPos;
} else if (iPos == 1) {
iZoneMinute = FXSYS_atoi(strTemp);
++iPos;
}
iIndex += 2;
}
}
if (iIndex < iLength)
return FALSE;
iZoneHour *= iSign;
return TRUE;
}
// static
FX_BOOL CXFA_FM2JSContext::IsIsoDateTimeFormat(const FX_CHAR* pData,
int32_t iLength,
int32_t& iYear,
int32_t& iMonth,
int32_t& iDay,
int32_t& iHour,
int32_t& iMinute,
int32_t& iSecond,
int32_t& iMillionSecond,
int32_t& iZoneHour,
int32_t& iZoneMinute) {
iYear = 0;
iMonth = 0;
iDay = 0;
iHour = 0;
iMinute = 0;
iSecond = 0;
if (!pData)
return FALSE;
int32_t iIndex = 0;
while (pData[iIndex] != 'T' && pData[iIndex] != 't') {
if (iIndex >= iLength)
return FALSE;
++iIndex;
}
if (iIndex != 8 && iIndex != 10)
return FALSE;
int32_t iStyle = -1;
if (!IsIsoDateFormat(pData, iIndex, iStyle, iYear, iMonth, iDay))
return FALSE;
if (pData[iIndex] != 'T' && pData[iIndex] != 't')
return TRUE;
++iIndex;
if (((iLength - iIndex > 13) && (iLength - iIndex < 6)) &&
(iLength - iIndex != 15)) {
return TRUE;
}
if (!IsIsoTimeFormat(pData + iIndex, iLength - iIndex, iHour, iMinute,
iSecond, iMillionSecond, iZoneHour, iZoneMinute)) {
return FALSE;
}
return TRUE;
}
// static
FX_BOOL CXFA_FM2JSContext::Local2IsoDate(CFXJSE_Value* pThis,
const CFX_ByteStringC& szDate,
const CFX_ByteStringC& szFormat,
const CFX_ByteStringC& szLocale,
CFX_ByteString& strIsoDate) {
CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument();
if (!pDoc)
return FALSE;
CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr();
IFX_Locale* pLocale = nullptr;
if (szLocale.IsEmpty()) {
CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject());
ASSERT(pThisNode);
CXFA_WidgetData widgetData(pThisNode);
pLocale = widgetData.GetLocal();
} else {
pLocale = pMgr->GetLocaleByName(CFX_WideString::FromUTF8(szLocale));
}
if (!pLocale)
return FALSE;
CFX_WideString wsFormat;
if (szFormat.IsEmpty())
pLocale->GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY_Default, wsFormat);
else
wsFormat = CFX_WideString::FromUTF8(szFormat);
CXFA_LocaleValue widgetValue(XFA_VT_DATE, CFX_WideString::FromUTF8(szDate),
wsFormat, pLocale, pMgr);
CFX_Unitime dt = widgetValue.GetDate();
strIsoDate.Format("%4d-%02d-%02d", dt.GetYear(), dt.GetMonth(), dt.GetDay());
return TRUE;
}
// static
FX_BOOL CXFA_FM2JSContext::Local2IsoTime(CFXJSE_Value* pThis,
const CFX_ByteStringC& szTime,
const CFX_ByteStringC& szFormat,
const CFX_ByteStringC& szLocale,
CFX_ByteString& strIsoTime) {
CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument();
if (!pDoc)
return FALSE;
CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr();
IFX_Locale* pLocale = nullptr;
if (szLocale.IsEmpty()) {
CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject());
ASSERT(pThisNode);
CXFA_WidgetData widgetData(pThisNode);
pLocale = widgetData.GetLocal();
} else {
pLocale = pMgr->GetLocaleByName(CFX_WideString::FromUTF8(szLocale));
}
if (!pLocale)
return FALSE;
CFX_WideString wsFormat;
if (szFormat.IsEmpty())
pLocale->GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY_Default, wsFormat);
else
wsFormat = CFX_WideString::FromUTF8(szFormat);
wsFormat = L"time{" + wsFormat + L"}";
CXFA_LocaleValue widgetValue(XFA_VT_TIME, CFX_WideString::FromUTF8(szTime),
wsFormat, pLocale, pMgr);
CFX_Unitime utime = widgetValue.GetTime();
strIsoTime.Format("%02d:%02d:%02d.%03d", utime.GetHour(), utime.GetMinute(),
utime.GetSecond(), utime.GetMillisecond());
return TRUE;
}
// static
FX_BOOL CXFA_FM2JSContext::IsoDate2Local(CFXJSE_Value* pThis,
const CFX_ByteStringC& szDate,
const CFX_ByteStringC& szFormat,
const CFX_ByteStringC& szLocale,
CFX_ByteString& strLocalDate) {
CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument();
if (!pDoc)
return FALSE;
CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr();
IFX_Locale* pLocale = nullptr;
if (szLocale.IsEmpty()) {
CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject());
ASSERT(pThisNode);
CXFA_WidgetData widgetData(pThisNode);
pLocale = widgetData.GetLocal();
} else {
pLocale = pMgr->GetLocaleByName(CFX_WideString::FromUTF8(szLocale));
}
if (!pLocale)
return FALSE;
CFX_WideString wsFormat;
if (szFormat.IsEmpty())
pLocale->GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY_Default, wsFormat);
else
wsFormat = CFX_WideString::FromUTF8(szFormat);
CXFA_LocaleValue widgetValue(XFA_VT_DATE, CFX_WideString::FromUTF8(szDate),
pMgr);
CFX_WideString wsRet;
widgetValue.FormatPatterns(wsRet, wsFormat, pLocale,
XFA_VALUEPICTURE_Display);
strLocalDate = FX_UTF8Encode(wsRet.c_str(), wsRet.GetLength());
return TRUE;
}
// static
FX_BOOL CXFA_FM2JSContext::IsoTime2Local(CFXJSE_Value* pThis,
const CFX_ByteStringC& szTime,
const CFX_ByteStringC& szFormat,
const CFX_ByteStringC& szLocale,
CFX_ByteString& strLocalTime) {
CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument();
if (!pDoc)
return FALSE;
CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr();
IFX_Locale* pLocale = nullptr;
if (szLocale.IsEmpty()) {
CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject());
ASSERT(pThisNode);
CXFA_WidgetData widgetData(pThisNode);
pLocale = widgetData.GetLocal();
} else {
pLocale = pMgr->GetLocaleByName(CFX_WideString::FromUTF8(szLocale));
}
if (!pLocale)
return FALSE;
CFX_WideString wsFormat;
if (szFormat.IsEmpty())
pLocale->GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY_Default, wsFormat);
else
wsFormat = CFX_WideString::FromUTF8(szFormat);
wsFormat = L"time{" + wsFormat + L"}";
CXFA_LocaleValue widgetValue(XFA_VT_TIME, CFX_WideString::FromUTF8(szTime),
pMgr);
CFX_WideString wsRet;
widgetValue.FormatPatterns(wsRet, wsFormat, pLocale,
XFA_VALUEPICTURE_Display);
strLocalTime = FX_UTF8Encode(wsRet.c_str(), wsRet.GetLength());
return TRUE;
}
// static
FX_BOOL CXFA_FM2JSContext::GetGMTTime(CFXJSE_Value* pThis,
const CFX_ByteStringC& szTime,
const CFX_ByteStringC& szFormat,
const CFX_ByteStringC& szLocale,
CFX_ByteString& strGMTTime) {
CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument();
if (!pDoc)
return FALSE;
CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr();
IFX_Locale* pLocale = nullptr;
if (szLocale.IsEmpty()) {
CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject());
ASSERT(pThisNode);
CXFA_WidgetData widgetData(pThisNode);
pLocale = widgetData.GetLocal();
} else {
pLocale = pMgr->GetLocaleByName(CFX_WideString::FromUTF8(szLocale));
}
if (!pLocale)
return FALSE;
CFX_WideString wsFormat;
if (szFormat.IsEmpty())
pLocale->GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY_Default, wsFormat);
else
wsFormat = CFX_WideString::FromUTF8(szFormat);
wsFormat = L"time{" + wsFormat + L"}";
CXFA_LocaleValue widgetValue(XFA_VT_TIME, CFX_WideString::FromUTF8(szTime),
pMgr);
CFX_WideString wsRet;
widgetValue.FormatPatterns(wsRet, wsFormat, pLocale,
XFA_VALUEPICTURE_Display);
strGMTTime = FX_UTF8Encode(wsRet.c_str(), wsRet.GetLength());
return TRUE;
}
// static
int32_t CXFA_FM2JSContext::DateString2Num(const CFX_ByteStringC& szDateString) {
int32_t iLength = szDateString.GetLength();
int32_t iYear = 0;
int32_t iMonth = 0;
int32_t iDay = 0;
if (iLength <= 10) {
int32_t iStyle = -1;
if (!IsIsoDateFormat(szDateString.c_str(), iLength, iStyle, iYear, iMonth,
iDay)) {
return 0;
}
} else {
int32_t iHour = 0;
int32_t iMinute = 0;
int32_t iSecond = 0;
int32_t iMilliSecond = 0;
int32_t iZoneHour = 0;
int32_t iZoneMinute = 0;
if (!IsIsoDateTimeFormat(szDateString.c_str(), iLength, iYear, iMonth, iDay,
iHour, iMinute, iSecond, iMilliSecond, iZoneHour,
iZoneMinute)) {
return 0;
}
}
FX_FLOAT dDays = 0;
int32_t i = 1;
if (iYear < 1900)
return 0;
while (iYear - i >= 1900) {
dDays +=
((!((iYear - i) % 4) && ((iYear - i) % 100)) || !((iYear - i) % 400))
? 366
: 365;
++i;
}
i = 1;
while (i < iMonth) {
if (i == 2)
dDays += ((!(iYear % 4) && (iYear % 100)) || !(iYear % 400)) ? 29 : 28;
else if (i <= 7)
dDays += (i % 2 == 0) ? 30 : 31;
else
dDays += (i % 2 == 0) ? 31 : 30;
++i;
}
i = 0;
while (iDay - i > 0) {
dDays += 1;
++i;
}
return (int32_t)dDays;
}
// static
void CXFA_FM2JSContext::GetLocalDateFormat(CFXJSE_Value* pThis,
int32_t iStyle,
const CFX_ByteStringC& szLocalStr,
CFX_ByteString& strFormat,
FX_BOOL bStandard) {
FX_LOCALEDATETIMESUBCATEGORY strStyle;
switch (iStyle) {
case 1:
strStyle = FX_LOCALEDATETIMESUBCATEGORY_Short;
break;
case 3:
strStyle = FX_LOCALEDATETIMESUBCATEGORY_Long;
break;
case 4:
strStyle = FX_LOCALEDATETIMESUBCATEGORY_Full;
break;
case 0:
case 2:
default:
strStyle = FX_LOCALEDATETIMESUBCATEGORY_Medium;
break;
}
CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument();
if (!pDoc)
return;
IFX_Locale* pLocale = nullptr;
if (szLocalStr.IsEmpty()) {
CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject());
ASSERT(pThisNode);
CXFA_WidgetData widgetData(pThisNode);
pLocale = widgetData.GetLocal();
} else {
pLocale = pDoc->GetLocalMgr()->GetLocaleByName(
CFX_WideString::FromUTF8(szLocalStr));
}
if (!pLocale)
return;
CFX_WideString strRet;
pLocale->GetDatePattern(strStyle, strRet);
if (!bStandard) {
CFX_WideString wsSymbols;
pLocale->GetDateTimeSymbols(wsSymbols);
AlternateDateTimeSymbols(strRet, wsSymbols, g_sAltTable_Date);
}
strFormat = FX_UTF8Encode(strRet.c_str(), strRet.GetLength());
}
// static
void CXFA_FM2JSContext::GetLocalTimeFormat(CFXJSE_Value* pThis,
int32_t iStyle,
const CFX_ByteStringC& szLocalStr,
CFX_ByteString& strFormat,
FX_BOOL bStandard) {
FX_LOCALEDATETIMESUBCATEGORY strStyle;
switch (iStyle) {
case 1:
strStyle = FX_LOCALEDATETIMESUBCATEGORY_Short;
break;
case 3:
strStyle = FX_LOCALEDATETIMESUBCATEGORY_Long;
break;
case 4:
strStyle = FX_LOCALEDATETIMESUBCATEGORY_Full;
break;
case 0:
case 2:
default:
strStyle = FX_LOCALEDATETIMESUBCATEGORY_Medium;
break;
}
CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument();
if (!pDoc)
return;
IFX_Locale* pLocale = nullptr;
if (szLocalStr.IsEmpty()) {
CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject());
ASSERT(pThisNode);
CXFA_WidgetData widgetData(pThisNode);
pLocale = widgetData.GetLocal();
} else {
pLocale = pDoc->GetLocalMgr()->GetLocaleByName(
CFX_WideString::FromUTF8(szLocalStr));
}
if (!pLocale)
return;
CFX_WideString strRet;
pLocale->GetTimePattern(strStyle, strRet);
if (!bStandard) {
CFX_WideString wsSymbols;