Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | #include "xfa/fxfa/fm2js/xfa_fm2jscontext.h" |
| 8 | |
| 9 | #include <time.h> |
| 10 | |
dsinclair | a52ab74 | 2016-09-29 13:59:29 -0700 | [diff] [blame] | 11 | #include "core/fxcrt/fx_ext.h" |
dsinclair | 4355468 | 2016-09-29 17:29:48 -0700 | [diff] [blame] | 12 | #include "fxjs/cfxjse_arguments.h" |
| 13 | #include "fxjs/cfxjse_class.h" |
| 14 | #include "fxjs/cfxjse_value.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 15 | #include "xfa/fgas/localization/fgas_locale.h" |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 16 | #include "xfa/fxfa/app/xfa_ffnotify.h" |
dsinclair | e80e9f8 | 2016-06-01 06:10:04 -0700 | [diff] [blame] | 17 | #include "xfa/fxfa/fm2js/xfa_program.h" |
dsinclair | 1628024 | 2016-07-21 12:03:47 -0700 | [diff] [blame] | 18 | #include "xfa/fxfa/parser/cxfa_document.h" |
dsinclair | 31f8740 | 2016-07-20 06:34:45 -0700 | [diff] [blame] | 19 | #include "xfa/fxfa/parser/cxfa_scriptcontext.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 20 | #include "xfa/fxfa/parser/xfa_localevalue.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 21 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 22 | namespace { |
| 23 | |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 24 | const double kFinancialPrecision = 0.00000001; |
| 25 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 26 | struct XFA_FMHtmlReserveCode { |
| 27 | uint32_t m_uCode; |
| 28 | const FX_WCHAR* m_htmlReserve; |
| 29 | }; |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 30 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 31 | struct XFA_FMHtmlHashedReserveCode { |
| 32 | uint32_t m_uHash; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 33 | uint32_t m_uCode; |
| 34 | }; |
| 35 | |
| 36 | const XFA_FMHtmlHashedReserveCode reservesForDecode[] = { |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 37 | {0x00018b62, /*L"Mu",*/ 924}, {0x00019083, /*L"Nu",*/ 925}, |
| 38 | {0x00019ab9, /*L"Pi",*/ 928}, {0x0001c3c1, /*L"Xi",*/ 926}, |
| 39 | {0x000210ac, /*L"ge",*/ 8805}, {0x000210bb, /*L"gt",*/ 62}, |
| 40 | {0x00022a51, /*L"le",*/ 8804}, {0x00022a60, /*L"lt",*/ 60}, |
| 41 | {0x00022f82, /*L"mu",*/ 956}, {0x00023493, /*L"ne",*/ 8800}, |
| 42 | {0x00023497, /*L"ni",*/ 8715}, {0x000234a3, /*L"nu",*/ 957}, |
| 43 | {0x000239c1, /*L"or",*/ 8744}, {0x00023ed9, /*L"pi",*/ 960}, |
| 44 | {0x000267e1, /*L"xi",*/ 958}, {0x00c41789, /*L"lceil",*/ 8968}, |
| 45 | {0x00eef34f, /*L"thetasym",*/ 977}, {0x012d7ead, /*L"lcirc",*/ 206}, |
| 46 | {0x01637b56, /*L"agrave",*/ 224}, {0x020856da, /*L"crarr",*/ 8629}, |
| 47 | {0x022188c3, /*L"gamma",*/ 947}, {0x033586d3, /*L"nbsp",*/ 160}, |
| 48 | {0x04f4c358, /*L"nsub",*/ 8836}, {0x0581466a, /*L"dagger",*/ 8224}, |
| 49 | {0x06b1f790, /*L"oelig",*/ 339}, {0x06e490d4, /*L"Chi",*/ 935}, |
| 50 | {0x0718c6a1, /*L"ETH",*/ 208}, {0x07196ada, /*L"Eta",*/ 919}, |
| 51 | {0x07f667ca, /*L"Ugrave",*/ 217}, {0x083a8a21, /*L"Phi",*/ 934}, |
| 52 | {0x083ac28c, /*L"Psi",*/ 936}, {0x086f26a9, /*L"Rho",*/ 929}, |
| 53 | {0x089b5b51, /*L"aring",*/ 229}, {0x08a39f4a, /*L"Tau",*/ 932}, |
| 54 | {0x08b6188b, /*L"THORN",*/ 222}, {0x09ce792a, /*L"icirc",*/ 238}, |
| 55 | {0x09f9d61e, /*L"amp",*/ 38}, {0x09f9db33, /*L"and",*/ 8743}, |
| 56 | {0x09f9db36, /*L"ang",*/ 8736}, {0x0a2e3514, /*L"cap",*/ 8745}, |
| 57 | {0x0a2e58f4, /*L"chi",*/ 967}, {0x0a2e9ba8, /*L"cup",*/ 8746}, |
| 58 | {0x0a4897d0, /*L"deg",*/ 176}, {0x0a6332fa, /*L"eta",*/ 951}, |
| 59 | {0x0a633301, /*L"eth",*/ 240}, {0x0acc4d4b, /*L"int",*/ 8747}, |
| 60 | {0x0b1b3d35, /*L"loz",*/ 9674}, {0x0b1b4c8b, /*L"lrm",*/ 8206}, |
| 61 | {0x0b4fd9b1, /*L"not",*/ 172}, {0x0b845241, /*L"phi",*/ 966}, |
| 62 | {0x0b84576f, /*L"piv",*/ 982}, {0x0b848aac, /*L"psi",*/ 968}, |
| 63 | {0x0bb8df5e, /*L"reg",*/ 174}, {0x0bb8eec9, /*L"rho",*/ 961}, |
| 64 | {0x0bb9034b, /*L"rlm",*/ 8207}, {0x0bd33d14, /*L"shy",*/ 173}, |
| 65 | {0x0bd34229, /*L"sim",*/ 8764}, {0x0bd37faa, /*L"sub",*/ 8834}, |
| 66 | {0x0bd37fb5, /*L"sum",*/ 8721}, {0x0bd37fb8, /*L"sup",*/ 8835}, |
| 67 | {0x0bed676a, /*L"tau",*/ 964}, {0x0c07f32e, /*L"uml",*/ 168}, |
| 68 | {0x0c71032c, /*L"yen",*/ 165}, {0x0c7f2889, /*L"szlig",*/ 223}, |
| 69 | {0x0c8badbb, /*L"zwj",*/ 8205}, {0x10ba4dba, /*L"Egrave",*/ 200}, |
| 70 | {0x10f1ea24, /*L"para",*/ 182}, {0x10f1ea37, /*L"part",*/ 8706}, |
| 71 | {0x115b2337, /*L"perp",*/ 8869}, {0x12b10d15, /*L"prod",*/ 8719}, |
| 72 | {0x12b10d21, /*L"prop",*/ 8733}, {0x12dfa9f4, /*L"rfloor",*/ 8971}, |
| 73 | {0x12eb4736, /*L"Agrave",*/ 192}, {0x12fff2b7, /*L"pund",*/ 163}, |
| 74 | {0x13fda9f2, /*L"tilde",*/ 732}, {0x1417fd62, /*L"times",*/ 215}, |
| 75 | {0x154fc726, /*L"ecirc",*/ 234}, {0x165aa451, /*L"sigma",*/ 963}, |
| 76 | {0x1709124a, /*L"Dagger",*/ 8225}, {0x192f78d5, /*L"iexcl",*/ 161}, |
| 77 | {0x1b7ed8d7, /*L"rArr",*/ 8658}, {0x1ec88c68, /*L"rang",*/ 9002}, |
| 78 | {0x1ec8a0f7, /*L"rarr",*/ 8594}, {0x1eda07f3, /*L"atilde",*/ 227}, |
| 79 | {0x1f3182c4, /*L"real",*/ 8476}, {0x1fc34f8b, /*L"yacute",*/ 253}, |
| 80 | {0x20d11522, /*L"acirc",*/ 226}, {0x21933a9b, /*L"rsaquo",*/ 8250}, |
| 81 | {0x21f44907, /*L"uacute",*/ 250}, {0x220cca72, /*L"acute",*/ 180}, |
| 82 | {0x242cded1, /*L"alefsym",*/ 8501}, {0x2655c66a, /*L"delta",*/ 948}, |
| 83 | {0x269e4b4d, /*L"exist",*/ 8707}, {0x273379fa, /*L"micro",*/ 181}, |
| 84 | {0x27a37440, /*L"forall",*/ 8704}, {0x2854e62c, /*L"minus",*/ 8722}, |
| 85 | {0x28636f81, /*L"cedil",*/ 184}, {0x2887357b, /*L"iacute",*/ 237}, |
| 86 | {0x2994d5ff, /*L"frac12",*/ 189}, {0x2994d601, /*L"frac14",*/ 188}, |
| 87 | {0x2994e043, /*L"frac34",*/ 190}, {0x2a1feb41, /*L"lambda",*/ 955}, |
| 88 | {0x2ab215f3, /*L"apos",*/ 39}, {0x2ab82ef7, /*L"eacute",*/ 233}, |
| 89 | {0x2b3592ef, /*L"auml",*/ 228}, {0x2ce92873, /*L"aacute",*/ 225}, |
| 90 | {0x2daff48a, /*L"oslash",*/ 248}, {0x2ef68882, /*L"aelig",*/ 230}, |
| 91 | {0x3061d3d3, /*L"Atilde",*/ 195}, {0x314b1b6b, /*L"Yacute",*/ 221}, |
| 92 | {0x337c14e7, /*L"Uacute",*/ 218}, {0x37676aca, /*L"cent",*/ 162}, |
| 93 | {0x37d0b841, /*L"circ",*/ 710}, {0x386e7947, /*L"cong",*/ 8773}, |
| 94 | {0x386e839b, /*L"copy",*/ 169}, {0x3a0e225a, /*L"Epsilon",*/ 917}, |
| 95 | {0x3ba7b721, /*L"Lambda",*/ 923}, {0x3bd9abe6, /*L"Alpha",*/ 913}, |
| 96 | {0x3c3ffad7, /*L"Eacute",*/ 201}, {0x3cfaf69f, /*L"brvbar",*/ 166}, |
| 97 | {0x3d54a489, /*L"omega",*/ 969}, {0x3e70f453, /*L"Aacute",*/ 193}, |
| 98 | {0x3f37c06a, /*L"Oslash",*/ 216}, {0x40e1b34e, /*L"diams",*/ 9830}, |
| 99 | {0x416596df, /*L"plusmn",*/ 177}, {0x4354ff16, /*L"Ucirc",*/ 219}, |
| 100 | {0x454fce6a, /*L"Upsilon",*/ 933}, {0x4610ad35, /*L"emsp",*/ 8195}, |
| 101 | {0x462afb76, /*L"ensp",*/ 8194}, {0x46e30073, /*L"euml",*/ 235}, |
| 102 | {0x46e31a1b, /*L"euro",*/ 8364}, {0x46f2eada, /*L"lowast",*/ 8727}, |
| 103 | {0x4dca26cf, /*L"Auml",*/ 196}, {0x4e2d6083, /*L"image",*/ 8465}, |
| 104 | {0x4f964ee8, /*L"notin",*/ 8713}, {0x50917a7a, /*L"epsilon",*/ 949}, |
| 105 | {0x52f9a4cd, /*L"Kappa",*/ 922}, {0x5496f410, /*L"Ocirc",*/ 212}, |
| 106 | {0x568cbf34, /*L"zeta",*/ 950}, {0x57badd20, /*L"ntilde",*/ 241}, |
| 107 | {0x58662109, /*L"zwnj",*/ 8204}, {0x5b39870f, /*L"empty",*/ 8709}, |
| 108 | {0x5bd3268a, /*L"upsilon",*/ 965}, {0x5e2bf8a3, /*L"Gamma",*/ 915}, |
| 109 | {0x5f73c13a, /*L"rsquo",*/ 8217}, {0x61f2bc4d, /*L"iota",*/ 953}, |
| 110 | {0x625bbcf3, /*L"isin",*/ 8712}, {0x62906df7, /*L"iuml",*/ 239}, |
| 111 | {0x64a5cb31, /*L"Aring",*/ 197}, {0x66f25c4a, /*L"sbquo",*/ 8218}, |
| 112 | {0x6851ab60, /*L"spades",*/ 9824}, {0x6942a900, /*L"Ntilde",*/ 209}, |
| 113 | {0x69779453, /*L"Euml",*/ 203}, {0x6cda6e23, /*L"current",*/ 164}, |
| 114 | {0x70b5b634, /*L"lsquo",*/ 8216}, {0x715a3706, /*L"Ecirc",*/ 202}, |
| 115 | {0x71e8bf8d, /*L"tdquo",*/ 8221}, {0x72651431, /*L"Sigma",*/ 931}, |
| 116 | {0x7569813b, /*L"iquest",*/ 191}, {0x776a436a, /*L"equiv",*/ 8801}, |
| 117 | {0x79215314, /*L"Zeta",*/ 918}, {0x79b81224, /*L"ograve",*/ 242}, |
| 118 | {0x7c2f8b23, /*L"macr",*/ 175}, {0x7cdb8502, /*L"Acirc",*/ 194}, |
| 119 | {0x8185c62e, /*L"ndash",*/ 8211}, {0x8260364a, /*L"Delta",*/ 916}, |
| 120 | {0x846619ad, /*L"mdash",*/ 8212}, {0x8550fb50, /*L"OElig",*/ 338}, |
| 121 | {0x88eb5b85, /*L"ldquo",*/ 8220}, {0x8b3fde04, /*L"Ograve",*/ 210}, |
| 122 | {0x8bc5794b, /*L"ordf",*/ 170}, {0x8bc57952, /*L"ordm",*/ 186}, |
| 123 | {0x8c14923d, /*L"ouml",*/ 246}, {0x8c5a7cd6, /*L"theta",*/ 952}, |
| 124 | {0x8d61812b, /*L"thorn",*/ 254}, {0x912b95aa, /*L"asymp",*/ 8776}, |
| 125 | {0x947faf81, /*L"middot",*/ 183}, {0x9629202e, /*L"lfloor",*/ 8970}, |
| 126 | {0x972e9ec1, /*L"otilde",*/ 245}, {0x9748f231, /*L"otimes",*/ 8855}, |
| 127 | {0x995f1469, /*L"Omega",*/ 937}, {0x99eb5349, /*L"quot",*/ 34}, |
| 128 | {0x9aeb639e, /*L"hellip",*/ 8230}, {0xa0ae2f86, /*L"Scaron",*/ 352}, |
| 129 | {0xa4dcb0d5, /*L"lsaquo",*/ 8249}, {0xa53dbf41, /*L"oacute",*/ 243}, |
| 130 | {0xa5ae9e7b, /*L"bdquo",*/ 8222}, {0xa602d7ba, /*L"sdot",*/ 8901}, |
| 131 | {0xa61ce86f, /*L"sect",*/ 167}, {0xa6e4c3d7, /*L"sigmaf",*/ 962}, |
| 132 | {0xa7c1c74f, /*L"sube",*/ 8838}, {0xa7c20ee9, /*L"sup1",*/ 185}, |
| 133 | {0xa7c20eea, /*L"sup2",*/ 178}, {0xa7c20eeb, /*L"sup3",*/ 179}, |
| 134 | {0xa7c20f1d, /*L"supe",*/ 8839}, {0xa8b66aa1, /*L"Otilde",*/ 213}, |
| 135 | {0xad958c42, /*L"AElig",*/ 198}, {0xaea9261d, /*L"Ouml",*/ 214}, |
| 136 | {0xb040eafa, /*L"uArr",*/ 8657}, {0xb07c2e1c, /*L"beta",*/ 946}, |
| 137 | {0xb220e92f, /*L"bull",*/ 8226}, {0xb22750c4, /*L"ccedil",*/ 231}, |
| 138 | {0xb38ab31a, /*L"uarr",*/ 8593}, {0xb598b683, /*L"uuml",*/ 252}, |
| 139 | {0xb6c58b21, /*L"Oacute",*/ 211}, {0xb6d2a617, /*L"oline",*/ 8254}, |
| 140 | {0xba9fd989, /*L"dArr",*/ 8659}, {0xbb5ccd41, /*L"lgrave",*/ 204}, |
| 141 | {0xbd39b44c, /*L"weierp",*/ 8472}, {0xbde9a1a9, /*L"darr",*/ 8595}, |
| 142 | {0xc027e329, /*L"permil",*/ 8240}, {0xc2451389, /*L"upsih",*/ 978}, |
| 143 | {0xc3af1ca4, /*L"Ccedil",*/ 199}, {0xcd164249, /*L"fnof",*/ 402}, |
| 144 | {0xcf6c8467, /*L"hearts",*/ 9829}, {0xd1228390, /*L"trade",*/ 8482}, |
| 145 | {0xd1462407, /*L"yuml",*/ 255}, {0xd2cf2253, /*L"oplus",*/ 8853}, |
| 146 | {0xd310c1fc, /*L"Beta",*/ 914}, {0xd59c4d74, /*L"infin",*/ 8734}, |
| 147 | {0xd64d470d, /*L"hArr",*/ 8660}, {0xd67d9c75, /*L"divide",*/ 247}, |
| 148 | {0xd698dd37, /*L"Omicron",*/ 927}, {0xd82d4a63, /*L"Uuml",*/ 220}, |
| 149 | {0xd9970f2d, /*L"harr",*/ 8596}, {0xda91fd99, /*L"clubs",*/ 9827}, |
| 150 | {0xdbe5bdcc, /*L"there4",*/ 8756}, {0xdd7671bd, /*L"prime",*/ 8242}, |
| 151 | {0xdfcf3c06, /*L"alpha",*/ 945}, {0xe0213063, /*L"saron",*/ 353}, |
| 152 | {0xe1911d83, /*L"radic",*/ 8730}, {0xe2e75468, /*L"raquo",*/ 187}, |
| 153 | {0xe6e27a5e, /*L"lacute",*/ 205}, {0xe74a8f36, /*L"ucirc",*/ 251}, |
| 154 | {0xe864ecb6, /*L"Theta",*/ 920}, {0xecddde5e, /*L"nabla",*/ 8711}, |
| 155 | {0xed1c3557, /*L"omicron",*/ 959}, {0xef82228f, /*L"rceil",*/ 8969}, |
| 156 | {0xf1fab491, /*L"lArr",*/ 8656}, {0xf3dab7e7, /*L"Yuml",*/ 376}, |
| 157 | {0xf4294962, /*L"laquo",*/ 171}, {0xf5446822, /*L"lang",*/ 9001}, |
| 158 | {0xf5447cb1, /*L"larr",*/ 8592}, {0xf66e9bea, /*L"ugrave",*/ 249}, |
| 159 | {0xf6b4ce70, /*L"lota",*/ 921}, {0xf6ef34ed, /*L"kappa",*/ 954}, |
| 160 | {0xf72a3a56, /*L"thinsp",*/ 8201}, {0xf752801a, /*L"luml",*/ 207}, |
| 161 | {0xf88c8430, /*L"ocirc",*/ 244}, {0xf9676178, /*L"frasl",*/ 8260}, |
| 162 | {0xfd01885e, /*L"igrave",*/ 236}, {0xff3281da, /*L"egrave",*/ 232}, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | const XFA_FMHtmlReserveCode reservesForEncode[] = { |
| 166 | {34, L"quot"}, {38, L"amp"}, {39, L"apos"}, |
| 167 | {60, L"lt"}, {62, L"gt"}, {160, L"nbsp"}, |
| 168 | {161, L"iexcl"}, {162, L"cent"}, {163, L"pund"}, |
| 169 | {164, L"current"}, {165, L"yen"}, {166, L"brvbar"}, |
| 170 | {167, L"sect"}, {168, L"uml"}, {169, L"copy"}, |
| 171 | {170, L"ordf"}, {171, L"laquo"}, {172, L"not"}, |
| 172 | {173, L"shy"}, {174, L"reg"}, {175, L"macr"}, |
| 173 | {176, L"deg"}, {177, L"plusmn"}, {178, L"sup2"}, |
| 174 | {179, L"sup3"}, {180, L"acute"}, {181, L"micro"}, |
| 175 | {182, L"para"}, {183, L"middot"}, {184, L"cedil"}, |
| 176 | {185, L"sup1"}, {186, L"ordm"}, {187, L"raquo"}, |
| 177 | {188, L"frac14"}, {189, L"frac12"}, {190, L"frac34"}, |
| 178 | {191, L"iquest"}, {192, L"Agrave"}, {193, L"Aacute"}, |
| 179 | {194, L"Acirc"}, {195, L"Atilde"}, {196, L"Auml"}, |
| 180 | {197, L"Aring"}, {198, L"AElig"}, {199, L"Ccedil"}, |
| 181 | {200, L"Egrave"}, {201, L"Eacute"}, {202, L"Ecirc"}, |
| 182 | {203, L"Euml"}, {204, L"lgrave"}, {205, L"lacute"}, |
| 183 | {206, L"lcirc"}, {207, L"luml"}, {208, L"ETH"}, |
| 184 | {209, L"Ntilde"}, {210, L"Ograve"}, {211, L"Oacute"}, |
| 185 | {212, L"Ocirc"}, {213, L"Otilde"}, {214, L"Ouml"}, |
| 186 | {215, L"times"}, {216, L"Oslash"}, {217, L"Ugrave"}, |
| 187 | {218, L"Uacute"}, {219, L"Ucirc"}, {220, L"Uuml"}, |
| 188 | {221, L"Yacute"}, {222, L"THORN"}, {223, L"szlig"}, |
| 189 | {224, L"agrave"}, {225, L"aacute"}, {226, L"acirc"}, |
| 190 | {227, L"atilde"}, {228, L"auml"}, {229, L"aring"}, |
| 191 | {230, L"aelig"}, {231, L"ccedil"}, {232, L"egrave"}, |
| 192 | {233, L"eacute"}, {234, L"ecirc"}, {235, L"euml"}, |
| 193 | {236, L"igrave"}, {237, L"iacute"}, {238, L"icirc"}, |
| 194 | {239, L"iuml"}, {240, L"eth"}, {241, L"ntilde"}, |
| 195 | {242, L"ograve"}, {243, L"oacute"}, {244, L"ocirc"}, |
| 196 | {245, L"otilde"}, {246, L"ouml"}, {247, L"divide"}, |
| 197 | {248, L"oslash"}, {249, L"ugrave"}, {250, L"uacute"}, |
| 198 | {251, L"ucirc"}, {252, L"uuml"}, {253, L"yacute"}, |
| 199 | {254, L"thorn"}, {255, L"yuml"}, {338, L"OElig"}, |
| 200 | {339, L"oelig"}, {352, L"Scaron"}, {353, L"saron"}, |
| 201 | {376, L"Yuml"}, {402, L"fnof"}, {710, L"circ"}, |
| 202 | {732, L"tilde"}, {913, L"Alpha"}, {914, L"Beta"}, |
| 203 | {915, L"Gamma"}, {916, L"Delta"}, {917, L"Epsilon"}, |
| 204 | {918, L"Zeta"}, {919, L"Eta"}, {920, L"Theta"}, |
| 205 | {921, L"lota"}, {922, L"Kappa"}, {923, L"Lambda"}, |
| 206 | {924, L"Mu"}, {925, L"Nu"}, {926, L"Xi"}, |
| 207 | {927, L"Omicron"}, {928, L"Pi"}, {929, L"Rho"}, |
| 208 | {931, L"Sigma"}, {932, L"Tau"}, {933, L"Upsilon"}, |
| 209 | {934, L"Phi"}, {935, L"Chi"}, {936, L"Psi"}, |
| 210 | {937, L"Omega"}, {945, L"alpha"}, {946, L"beta"}, |
| 211 | {947, L"gamma"}, {948, L"delta"}, {949, L"epsilon"}, |
| 212 | {950, L"zeta"}, {951, L"eta"}, {952, L"theta"}, |
| 213 | {953, L"iota"}, {954, L"kappa"}, {955, L"lambda"}, |
| 214 | {956, L"mu"}, {957, L"nu"}, {958, L"xi"}, |
| 215 | {959, L"omicron"}, {960, L"pi"}, {961, L"rho"}, |
| 216 | {962, L"sigmaf"}, {963, L"sigma"}, {964, L"tau"}, |
| 217 | {965, L"upsilon"}, {966, L"phi"}, {967, L"chi"}, |
| 218 | {968, L"psi"}, {969, L"omega"}, {977, L"thetasym"}, |
| 219 | {978, L"upsih"}, {982, L"piv"}, {8194, L"ensp"}, |
| 220 | {8195, L"emsp"}, {8201, L"thinsp"}, {8204, L"zwnj"}, |
| 221 | {8205, L"zwj"}, {8206, L"lrm"}, {8207, L"rlm"}, |
| 222 | {8211, L"ndash"}, {8212, L"mdash"}, {8216, L"lsquo"}, |
| 223 | {8217, L"rsquo"}, {8218, L"sbquo"}, {8220, L"ldquo"}, |
| 224 | {8221, L"tdquo"}, {8222, L"bdquo"}, {8224, L"dagger"}, |
| 225 | {8225, L"Dagger"}, {8226, L"bull"}, {8230, L"hellip"}, |
| 226 | {8240, L"permil"}, {8242, L"prime"}, {8249, L"lsaquo"}, |
| 227 | {8250, L"rsaquo"}, {8254, L"oline"}, {8260, L"frasl"}, |
| 228 | {8364, L"euro"}, {8465, L"image"}, {8472, L"weierp"}, |
| 229 | {8476, L"real"}, {8482, L"trade"}, {8501, L"alefsym"}, |
| 230 | {8592, L"larr"}, {8593, L"uarr"}, {8594, L"rarr"}, |
| 231 | {8595, L"darr"}, {8596, L"harr"}, {8629, L"crarr"}, |
| 232 | {8656, L"lArr"}, {8657, L"uArr"}, {8658, L"rArr"}, |
| 233 | {8659, L"dArr"}, {8660, L"hArr"}, {8704, L"forall"}, |
| 234 | {8706, L"part"}, {8707, L"exist"}, {8709, L"empty"}, |
| 235 | {8711, L"nabla"}, {8712, L"isin"}, {8713, L"notin"}, |
| 236 | {8715, L"ni"}, {8719, L"prod"}, {8721, L"sum"}, |
| 237 | {8722, L"minus"}, {8727, L"lowast"}, {8730, L"radic"}, |
| 238 | {8733, L"prop"}, {8734, L"infin"}, {8736, L"ang"}, |
| 239 | {8743, L"and"}, {8744, L"or"}, {8745, L"cap"}, |
| 240 | {8746, L"cup"}, {8747, L"int"}, {8756, L"there4"}, |
| 241 | {8764, L"sim"}, {8773, L"cong"}, {8776, L"asymp"}, |
| 242 | {8800, L"ne"}, {8801, L"equiv"}, {8804, L"le"}, |
| 243 | {8805, L"ge"}, {8834, L"sub"}, {8835, L"sup"}, |
| 244 | {8836, L"nsub"}, {8838, L"sube"}, {8839, L"supe"}, |
| 245 | {8853, L"oplus"}, {8855, L"otimes"}, {8869, L"perp"}, |
| 246 | {8901, L"sdot"}, {8968, L"lceil"}, {8969, L"rceil"}, |
| 247 | {8970, L"lfloor"}, {8971, L"rfloor"}, {9001, L"lang"}, |
| 248 | {9002, L"rang"}, {9674, L"loz"}, {9824, L"spades"}, |
| 249 | {9827, L"clubs"}, {9829, L"hearts"}, {9830, L"diams"}, |
| 250 | }; |
| 251 | |
tsepez | 1c9cfe1 | 2016-05-26 13:30:56 -0700 | [diff] [blame] | 252 | const FXJSE_FUNCTION_DESCRIPTOR formcalc_fm2js_functions[] = { |
| 253 | {"Abs", CXFA_FM2JSContext::Abs}, |
| 254 | {"Avg", CXFA_FM2JSContext::Avg}, |
| 255 | {"Ceil", CXFA_FM2JSContext::Ceil}, |
| 256 | {"Count", CXFA_FM2JSContext::Count}, |
| 257 | {"Floor", CXFA_FM2JSContext::Floor}, |
| 258 | {"Max", CXFA_FM2JSContext::Max}, |
| 259 | {"Min", CXFA_FM2JSContext::Min}, |
| 260 | {"Mod", CXFA_FM2JSContext::Mod}, |
| 261 | {"Round", CXFA_FM2JSContext::Round}, |
| 262 | {"Sum", CXFA_FM2JSContext::Sum}, |
| 263 | {"Date", CXFA_FM2JSContext::Date}, |
| 264 | {"Date2Num", CXFA_FM2JSContext::Date2Num}, |
| 265 | {"DateFmt", CXFA_FM2JSContext::DateFmt}, |
| 266 | {"IsoDate2Num", CXFA_FM2JSContext::IsoDate2Num}, |
| 267 | {"IsoTime2Num", CXFA_FM2JSContext::IsoTime2Num}, |
| 268 | {"LocalDateFmt", CXFA_FM2JSContext::LocalDateFmt}, |
| 269 | {"LocalTimeFmt", CXFA_FM2JSContext::LocalTimeFmt}, |
| 270 | {"Num2Date", CXFA_FM2JSContext::Num2Date}, |
| 271 | {"Num2GMTime", CXFA_FM2JSContext::Num2GMTime}, |
| 272 | {"Num2Time", CXFA_FM2JSContext::Num2Time}, |
| 273 | {"Time", CXFA_FM2JSContext::Time}, |
| 274 | {"Time2Num", CXFA_FM2JSContext::Time2Num}, |
| 275 | {"TimeFmt", CXFA_FM2JSContext::TimeFmt}, |
| 276 | {"Apr", CXFA_FM2JSContext::Apr}, |
| 277 | {"Cterm", CXFA_FM2JSContext::CTerm}, |
| 278 | {"FV", CXFA_FM2JSContext::FV}, |
| 279 | {"Ipmt", CXFA_FM2JSContext::IPmt}, |
| 280 | {"NPV", CXFA_FM2JSContext::NPV}, |
| 281 | {"Pmt", CXFA_FM2JSContext::Pmt}, |
| 282 | {"PPmt", CXFA_FM2JSContext::PPmt}, |
| 283 | {"PV", CXFA_FM2JSContext::PV}, |
| 284 | {"Rate", CXFA_FM2JSContext::Rate}, |
| 285 | {"Term", CXFA_FM2JSContext::Term}, |
| 286 | {"Choose", CXFA_FM2JSContext::Choose}, |
| 287 | {"Exists", CXFA_FM2JSContext::Exists}, |
| 288 | {"HasValue", CXFA_FM2JSContext::HasValue}, |
| 289 | {"Oneof", CXFA_FM2JSContext::Oneof}, |
| 290 | {"Within", CXFA_FM2JSContext::Within}, |
| 291 | {"If", CXFA_FM2JSContext::If}, |
| 292 | {"Eval", CXFA_FM2JSContext::Eval}, |
| 293 | {"Translate", CXFA_FM2JSContext::eval_translation}, |
| 294 | {"Ref", CXFA_FM2JSContext::Ref}, |
| 295 | {"UnitType", CXFA_FM2JSContext::UnitType}, |
| 296 | {"UnitValue", CXFA_FM2JSContext::UnitValue}, |
| 297 | {"At", CXFA_FM2JSContext::At}, |
| 298 | {"Concat", CXFA_FM2JSContext::Concat}, |
| 299 | {"Decode", CXFA_FM2JSContext::Decode}, |
| 300 | {"Encode", CXFA_FM2JSContext::Encode}, |
| 301 | {"Format", CXFA_FM2JSContext::Format}, |
| 302 | {"Left", CXFA_FM2JSContext::Left}, |
| 303 | {"Len", CXFA_FM2JSContext::Len}, |
| 304 | {"Lower", CXFA_FM2JSContext::Lower}, |
| 305 | {"Ltrim", CXFA_FM2JSContext::Ltrim}, |
| 306 | {"Parse", CXFA_FM2JSContext::Parse}, |
| 307 | {"Replace", CXFA_FM2JSContext::Replace}, |
| 308 | {"Right", CXFA_FM2JSContext::Right}, |
| 309 | {"Rtrim", CXFA_FM2JSContext::Rtrim}, |
| 310 | {"Space", CXFA_FM2JSContext::Space}, |
| 311 | {"Str", CXFA_FM2JSContext::Str}, |
| 312 | {"Stuff", CXFA_FM2JSContext::Stuff}, |
| 313 | {"Substr", CXFA_FM2JSContext::Substr}, |
| 314 | {"Uuid", CXFA_FM2JSContext::Uuid}, |
| 315 | {"Upper", CXFA_FM2JSContext::Upper}, |
| 316 | {"WordNum", CXFA_FM2JSContext::WordNum}, |
| 317 | {"Get", CXFA_FM2JSContext::Get}, |
| 318 | {"Post", CXFA_FM2JSContext::Post}, |
| 319 | {"Put", CXFA_FM2JSContext::Put}, |
| 320 | {"positive_operator", CXFA_FM2JSContext::positive_operator}, |
| 321 | {"negative_operator", CXFA_FM2JSContext::negative_operator}, |
| 322 | {"logical_or_operator", CXFA_FM2JSContext::logical_or_operator}, |
| 323 | {"logical_and_operator", CXFA_FM2JSContext::logical_and_operator}, |
| 324 | {"logical_not_operator", CXFA_FM2JSContext::logical_not_operator}, |
| 325 | {"equality_operator", CXFA_FM2JSContext::equality_operator}, |
| 326 | {"notequality_operator", CXFA_FM2JSContext::notequality_operator}, |
| 327 | {"less_operator", CXFA_FM2JSContext::less_operator}, |
| 328 | {"lessequal_operator", CXFA_FM2JSContext::lessequal_operator}, |
| 329 | {"greater_operator", CXFA_FM2JSContext::greater_operator}, |
| 330 | {"greaterequal_operator", CXFA_FM2JSContext::greaterequal_operator}, |
| 331 | {"plus_operator", CXFA_FM2JSContext::plus_operator}, |
| 332 | {"minus_operator", CXFA_FM2JSContext::minus_operator}, |
| 333 | {"multiple_operator", CXFA_FM2JSContext::multiple_operator}, |
| 334 | {"divide_operator", CXFA_FM2JSContext::divide_operator}, |
| 335 | {"assign_value_operator", CXFA_FM2JSContext::assign_value_operator}, |
| 336 | {"dot_accessor", CXFA_FM2JSContext::dot_accessor}, |
| 337 | {"dotdot_accessor", CXFA_FM2JSContext::dotdot_accessor}, |
| 338 | {"concat_fm_object", CXFA_FM2JSContext::concat_fm_object}, |
| 339 | {"is_fm_object", CXFA_FM2JSContext::is_fm_object}, |
| 340 | {"is_fm_array", CXFA_FM2JSContext::is_fm_array}, |
| 341 | {"get_fm_value", CXFA_FM2JSContext::get_fm_value}, |
| 342 | {"get_fm_jsobj", CXFA_FM2JSContext::get_fm_jsobj}, |
| 343 | {"fm_var_filter", CXFA_FM2JSContext::fm_var_filter}, |
| 344 | }; |
| 345 | |
| 346 | const FXJSE_CLASS_DESCRIPTOR formcalc_fm2js_descriptor = { |
| 347 | "XFA_FM2JS_FormCalcClass", // name |
| 348 | nullptr, // constructor |
| 349 | nullptr, // properties |
| 350 | formcalc_fm2js_functions, // methods |
| 351 | 0, // number of properties |
| 352 | FX_ArraySize(formcalc_fm2js_functions), // number of methods |
| 353 | nullptr, // dynamic prop type |
| 354 | nullptr, // dynamic prop getter |
| 355 | nullptr, // dynamic prop setter |
| 356 | nullptr, // dynamic prop deleter |
| 357 | nullptr, // dynamic prop method call |
| 358 | }; |
| 359 | |
dsinclair | 0c268e9 | 2016-05-17 14:04:14 -0700 | [diff] [blame] | 360 | const uint8_t g_sAltTable_Date[] = { |
ochang | f6be145 | 2016-06-01 12:20:03 -0700 | [diff] [blame] | 361 | 255, 255, 255, 3, 9, 255, 255, 255, 255, 255, 255, |
| 362 | 255, 2, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 363 | 255, 255, 1, 255, 255, 255, 255, 255, 255, 255, 255, |
dsinclair | 0c268e9 | 2016-05-17 14:04:14 -0700 | [diff] [blame] | 364 | }; |
ochang | f6be145 | 2016-06-01 12:20:03 -0700 | [diff] [blame] | 365 | static_assert(FX_ArraySize(g_sAltTable_Date) == L'a' - L'A' + 1, |
| 366 | "Invalid g_sAltTable_Date size."); |
dsinclair | 0c268e9 | 2016-05-17 14:04:14 -0700 | [diff] [blame] | 367 | |
| 368 | const uint8_t g_sAltTable_Time[] = { |
ochang | f6be145 | 2016-06-01 12:20:03 -0700 | [diff] [blame] | 369 | 14, 255, 255, 3, 9, 255, 255, 15, 255, 255, 255, |
| 370 | 255, 6, 255, 255, 255, 255, 255, 7, 255, 255, 255, |
| 371 | 255, 255, 1, 17, 255, 255, 255, 255, 255, 255, 255, |
dsinclair | 0c268e9 | 2016-05-17 14:04:14 -0700 | [diff] [blame] | 372 | }; |
ochang | f6be145 | 2016-06-01 12:20:03 -0700 | [diff] [blame] | 373 | static_assert(FX_ArraySize(g_sAltTable_Time) == L'a' - L'A' + 1, |
| 374 | "Invalid g_sAltTable_Time size."); |
dsinclair | 0c268e9 | 2016-05-17 14:04:14 -0700 | [diff] [blame] | 375 | |
| 376 | void AlternateDateTimeSymbols(CFX_WideString& wsPattern, |
| 377 | const CFX_WideString& wsAltSymbols, |
| 378 | const uint8_t* pAltTable) { |
| 379 | int32_t nLength = wsPattern.GetLength(); |
| 380 | FX_BOOL bInConstRange = FALSE; |
| 381 | FX_BOOL bEscape = FALSE; |
ochang | f6be145 | 2016-06-01 12:20:03 -0700 | [diff] [blame] | 382 | int32_t i = 0; |
dsinclair | 0c268e9 | 2016-05-17 14:04:14 -0700 | [diff] [blame] | 383 | while (i < nLength) { |
| 384 | FX_WCHAR wc = wsPattern[i]; |
| 385 | if (wc == L'\'') { |
| 386 | bInConstRange = !bInConstRange; |
| 387 | if (bEscape) { |
| 388 | i++; |
| 389 | } else { |
| 390 | wsPattern.Delete(i); |
| 391 | nLength--; |
| 392 | } |
| 393 | bEscape = !bEscape; |
| 394 | continue; |
| 395 | } |
ochang | f6be145 | 2016-06-01 12:20:03 -0700 | [diff] [blame] | 396 | if (!bInConstRange && wc >= L'A' && wc <= L'a') { |
| 397 | uint8_t nAlt = pAltTable[wc - L'A']; |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 398 | if (nAlt != 255) |
dsinclair | 0c268e9 | 2016-05-17 14:04:14 -0700 | [diff] [blame] | 399 | wsPattern.SetAt(i, wsAltSymbols[nAlt]); |
dsinclair | 0c268e9 | 2016-05-17 14:04:14 -0700 | [diff] [blame] | 400 | } |
| 401 | i++; |
| 402 | bEscape = FALSE; |
| 403 | } |
| 404 | } |
| 405 | |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 406 | bool PatternStringType(const CFX_ByteStringC& szPattern, |
| 407 | uint32_t& patternType) { |
| 408 | CFX_WideString wsPattern = CFX_WideString::FromUTF8(szPattern); |
| 409 | if (FX_WSTRC(L"datetime") == wsPattern.Left(8)) { |
| 410 | patternType = XFA_VT_DATETIME; |
| 411 | return true; |
| 412 | } |
| 413 | if (FX_WSTRC(L"date") == wsPattern.Left(4)) { |
| 414 | patternType = wsPattern.Find(L"time") > 0 ? XFA_VT_DATETIME : XFA_VT_DATE; |
| 415 | return true; |
| 416 | } |
| 417 | if (FX_WSTRC(L"time") == wsPattern.Left(4)) { |
| 418 | patternType = XFA_VT_TIME; |
| 419 | return true; |
| 420 | } |
| 421 | if (FX_WSTRC(L"text") == wsPattern.Left(4)) { |
| 422 | patternType = XFA_VT_TEXT; |
| 423 | return true; |
| 424 | } |
| 425 | if (FX_WSTRC(L"num") == wsPattern.Left(3)) { |
| 426 | if (FX_WSTRC(L"integer") == wsPattern.Mid(4, 7)) { |
| 427 | patternType = XFA_VT_INTEGER; |
| 428 | } else if (FX_WSTRC(L"decimal") == wsPattern.Mid(4, 7)) { |
| 429 | patternType = XFA_VT_DECIMAL; |
| 430 | } else if (FX_WSTRC(L"currency") == wsPattern.Mid(4, 8)) { |
| 431 | patternType = XFA_VT_FLOAT; |
| 432 | } else if (FX_WSTRC(L"percent") == wsPattern.Mid(4, 7)) { |
| 433 | patternType = XFA_VT_FLOAT; |
| 434 | } else { |
| 435 | patternType = XFA_VT_FLOAT; |
| 436 | } |
| 437 | return true; |
| 438 | } |
| 439 | |
| 440 | patternType = XFA_VT_NULL; |
| 441 | wsPattern.MakeLower(); |
| 442 | const FX_WCHAR* pData = wsPattern.c_str(); |
| 443 | int32_t iLength = wsPattern.GetLength(); |
| 444 | int32_t iIndex = 0; |
| 445 | FX_BOOL bSingleQuotation = FALSE; |
| 446 | FX_WCHAR patternChar; |
| 447 | while (iIndex < iLength) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 448 | patternChar = pData[iIndex]; |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 449 | if (patternChar == 0x27) { |
| 450 | bSingleQuotation = !bSingleQuotation; |
| 451 | } else if (!bSingleQuotation && |
| 452 | (patternChar == 'y' || patternChar == 'j')) { |
| 453 | patternType = XFA_VT_DATE; |
| 454 | iIndex++; |
| 455 | FX_WCHAR timePatternChar; |
| 456 | while (iIndex < iLength) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 457 | timePatternChar = pData[iIndex]; |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 458 | if (timePatternChar == 0x27) { |
| 459 | bSingleQuotation = !bSingleQuotation; |
| 460 | } else if (!bSingleQuotation && timePatternChar == 't') { |
| 461 | patternType = XFA_VT_DATETIME; |
| 462 | break; |
| 463 | } |
| 464 | iIndex++; |
| 465 | } |
| 466 | break; |
| 467 | } else if (!bSingleQuotation && |
| 468 | (patternChar == 'h' || patternChar == 'k')) { |
| 469 | patternType = XFA_VT_TIME; |
| 470 | break; |
| 471 | } else if (!bSingleQuotation && |
| 472 | (patternChar == 'a' || patternChar == 'x' || |
| 473 | patternChar == 'o' || patternChar == '0')) { |
| 474 | patternType = XFA_VT_TEXT; |
| 475 | if (patternChar == 'x' || patternChar == 'o' || patternChar == '0') { |
| 476 | break; |
| 477 | } |
| 478 | } else if (!bSingleQuotation && |
| 479 | (patternChar == 'z' || patternChar == 's' || |
| 480 | patternChar == 'e' || patternChar == 'v' || |
| 481 | patternChar == '8' || patternChar == ',' || |
| 482 | patternChar == '.' || patternChar == '$')) { |
| 483 | patternType = XFA_VT_FLOAT; |
| 484 | if (patternChar == 'v' || patternChar == '8' || patternChar == '$') { |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | iIndex++; |
| 489 | } |
| 490 | if (patternType == XFA_VT_NULL) { |
| 491 | patternType = XFA_VT_TEXT | XFA_VT_FLOAT; |
| 492 | } |
| 493 | return false; |
| 494 | } |
| 495 | |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 496 | CXFA_FM2JSContext* ToJSContext(CFXJSE_Value* pValue, CFXJSE_Class* pClass) { |
| 497 | return static_cast<CXFA_FM2JSContext*>(pValue->ToHostObject(pClass)); |
| 498 | } |
| 499 | |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 500 | bool IsWhitespace(char c) { |
| 501 | return c == 0x20 || c == 0x09 || c == 0x0B || c == 0x0C || c == 0x0A || |
| 502 | c == 0x0D; |
| 503 | } |
| 504 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 505 | } // namespace |
| 506 | |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 507 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 508 | void CXFA_FM2JSContext::Abs(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 509 | const CFX_ByteStringC& szFuncName, |
| 510 | CFXJSE_Arguments& args) { |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 511 | if (args.GetLength() != 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 512 | ToJSContext(pThis, nullptr) |
| 513 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Abs"); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 514 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 515 | } |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 516 | |
| 517 | std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); |
| 518 | if (ValueIsNull(pThis, argOne.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 519 | args.GetReturnValue()->SetNull(); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 520 | return; |
| 521 | } |
| 522 | |
| 523 | FX_DOUBLE dValue = ValueToDouble(pThis, argOne.get()); |
| 524 | if (dValue < 0) |
| 525 | dValue = -dValue; |
| 526 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 527 | args.GetReturnValue()->SetDouble(dValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 528 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 529 | |
| 530 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 531 | void CXFA_FM2JSContext::Avg(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 532 | const CFX_ByteStringC& szFuncName, |
| 533 | CFXJSE_Arguments& args) { |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 534 | int32_t argc = args.GetLength(); |
| 535 | if (argc < 1) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 536 | args.GetReturnValue()->SetNull(); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 537 | return; |
| 538 | } |
| 539 | |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 540 | v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 541 | uint32_t uCount = 0; |
| 542 | FX_DOUBLE dSum = 0.0; |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 543 | for (int32_t i = 0; i < argc; i++) { |
| 544 | std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 545 | if (argValue->IsNull()) |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 546 | continue; |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 547 | |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 548 | if (!argValue->IsArray()) { |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 549 | dSum += ValueToDouble(pThis, argValue.get()); |
| 550 | uCount++; |
| 551 | continue; |
| 552 | } |
| 553 | |
| 554 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 555 | argValue->GetObjectProperty("length", lengthValue.get()); |
| 556 | int32_t iLength = lengthValue->ToInteger(); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 557 | |
| 558 | if (iLength > 2) { |
| 559 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 560 | argValue->GetObjectPropertyByIdx(1, propertyValue.get()); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 561 | |
| 562 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 563 | if (propertyValue->IsNull()) { |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 564 | for (int32_t j = 2; j < iLength; j++) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 565 | argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 566 | std::unique_ptr<CFXJSE_Value> defaultPropValue( |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 567 | new CFXJSE_Value(pIsolate)); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 568 | GetObjectDefaultValue(jsObjectValue.get(), defaultPropValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 569 | if (defaultPropValue->IsNull()) |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 570 | continue; |
| 571 | |
| 572 | dSum += ValueToDouble(pThis, defaultPropValue.get()); |
| 573 | uCount++; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 574 | } |
| 575 | } else { |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 576 | for (int32_t j = 2; j < iLength; j++) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 577 | argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 578 | std::unique_ptr<CFXJSE_Value> newPropertyValue( |
| 579 | new CFXJSE_Value(pIsolate)); |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 580 | jsObjectValue->GetObjectProperty( |
| 581 | propertyValue->ToString().AsStringC(), newPropertyValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 582 | if (newPropertyValue->IsNull()) |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 583 | continue; |
| 584 | |
| 585 | dSum += ValueToDouble(pThis, newPropertyValue.get()); |
| 586 | uCount++; |
| 587 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 588 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 589 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 590 | } |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 591 | if (uCount == 0) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 592 | args.GetReturnValue()->SetNull(); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 593 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 594 | } |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 595 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 596 | args.GetReturnValue()->SetDouble(dSum / uCount); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 597 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 598 | |
| 599 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 600 | void CXFA_FM2JSContext::Ceil(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 601 | const CFX_ByteStringC& szFuncName, |
| 602 | CFXJSE_Arguments& args) { |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 603 | if (args.GetLength() != 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 604 | ToJSContext(pThis, nullptr) |
| 605 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Ceil"); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 606 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 607 | } |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 608 | |
| 609 | std::unique_ptr<CFXJSE_Value> argValue = GetSimpleValue(pThis, args, 0); |
| 610 | if (ValueIsNull(pThis, argValue.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 611 | args.GetReturnValue()->SetNull(); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 612 | return; |
| 613 | } |
| 614 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 615 | args.GetReturnValue()->SetFloat( |
| 616 | FXSYS_ceil(ValueToFloat(pThis, argValue.get()))); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 617 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 618 | |
| 619 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 620 | void CXFA_FM2JSContext::Count(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 621 | const CFX_ByteStringC& szFuncName, |
| 622 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 623 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | ec3da5b | 2016-05-25 16:42:05 -0700 | [diff] [blame] | 624 | v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 625 | int32_t iCount = 0; |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 626 | for (int32_t i = 0; i < args.GetLength(); i++) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 627 | std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 628 | if (argValue->IsNull()) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 629 | continue; |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 630 | |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 631 | if (argValue->IsArray()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 632 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 633 | argValue->GetObjectProperty("length", lengthValue.get()); |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 634 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 635 | int32_t iLength = lengthValue->ToInteger(); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 636 | if (iLength <= 2) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 637 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 638 | return; |
| 639 | } |
| 640 | |
| 641 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 642 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 643 | std::unique_ptr<CFXJSE_Value> newPropertyValue( |
| 644 | new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 645 | argValue->GetObjectPropertyByIdx(1, propertyValue.get()); |
| 646 | argValue->GetObjectPropertyByIdx(2, jsObjectValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 647 | if (propertyValue->IsNull()) { |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 648 | for (int32_t j = 2; j < iLength; j++) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 649 | argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 650 | GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 651 | if (!newPropertyValue->IsNull()) |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 652 | iCount++; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 653 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 654 | } else { |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 655 | for (int32_t j = 2; j < iLength; j++) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 656 | argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 657 | jsObjectValue->GetObjectProperty( |
| 658 | propertyValue->ToString().AsStringC(), newPropertyValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 659 | iCount += newPropertyValue->IsNull() ? 0 : 1; |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 660 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 661 | } |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 662 | } else if (argValue->IsObject()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 663 | std::unique_ptr<CFXJSE_Value> newPropertyValue( |
| 664 | new CFXJSE_Value(pIsolate)); |
| 665 | GetObjectDefaultValue(argValue.get(), newPropertyValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 666 | if (!newPropertyValue->IsNull()) |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 667 | iCount++; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 668 | } else { |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 669 | iCount++; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 670 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 671 | } |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 672 | args.GetReturnValue()->SetInteger(iCount); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 673 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 674 | |
| 675 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 676 | void CXFA_FM2JSContext::Floor(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 677 | const CFX_ByteStringC& szFuncName, |
| 678 | CFXJSE_Arguments& args) { |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 679 | if (args.GetLength() != 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 680 | ToJSContext(pThis, nullptr) |
| 681 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Floor"); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 682 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 683 | } |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 684 | |
| 685 | std::unique_ptr<CFXJSE_Value> argValue = GetSimpleValue(pThis, args, 0); |
| 686 | if (ValueIsNull(pThis, argValue.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 687 | args.GetReturnValue()->SetNull(); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 688 | return; |
| 689 | } |
| 690 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 691 | args.GetReturnValue()->SetFloat( |
| 692 | FXSYS_floor(ValueToFloat(pThis, argValue.get()))); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 693 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 694 | |
| 695 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 696 | void CXFA_FM2JSContext::Max(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 697 | const CFX_ByteStringC& szFuncName, |
| 698 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 699 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | ec3da5b | 2016-05-25 16:42:05 -0700 | [diff] [blame] | 700 | v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 701 | uint32_t uCount = 0; |
| 702 | FX_DOUBLE dMaxValue = 0.0; |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 703 | for (int32_t i = 0; i < args.GetLength(); i++) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 704 | std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 705 | if (argValue->IsNull()) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 706 | continue; |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 707 | |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 708 | if (argValue->IsArray()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 709 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 710 | argValue->GetObjectProperty("length", lengthValue.get()); |
| 711 | int32_t iLength = lengthValue->ToInteger(); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 712 | if (iLength <= 2) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 713 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 714 | return; |
| 715 | } |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 716 | |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 717 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 718 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 719 | std::unique_ptr<CFXJSE_Value> newPropertyValue( |
| 720 | new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 721 | argValue->GetObjectPropertyByIdx(1, propertyValue.get()); |
| 722 | argValue->GetObjectPropertyByIdx(2, jsObjectValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 723 | if (propertyValue->IsNull()) { |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 724 | for (int32_t j = 2; j < iLength; j++) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 725 | argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 726 | GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 727 | if (newPropertyValue->IsNull()) |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 728 | continue; |
| 729 | |
| 730 | uCount++; |
| 731 | FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get()); |
| 732 | dMaxValue = (uCount == 1) ? dValue : std::max(dMaxValue, dValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 733 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 734 | } else { |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 735 | for (int32_t j = 2; j < iLength; j++) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 736 | argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 737 | jsObjectValue->GetObjectProperty( |
| 738 | propertyValue->ToString().AsStringC(), newPropertyValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 739 | if (newPropertyValue->IsNull()) |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 740 | continue; |
| 741 | |
| 742 | uCount++; |
| 743 | FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get()); |
| 744 | dMaxValue = (uCount == 1) ? dValue : std::max(dMaxValue, dValue); |
| 745 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 746 | } |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 747 | } else if (argValue->IsObject()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 748 | std::unique_ptr<CFXJSE_Value> newPropertyValue( |
| 749 | new CFXJSE_Value(pIsolate)); |
| 750 | GetObjectDefaultValue(argValue.get(), newPropertyValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 751 | if (newPropertyValue->IsNull()) |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 752 | continue; |
| 753 | |
| 754 | uCount++; |
| 755 | FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get()); |
| 756 | dMaxValue = (uCount == 1) ? dValue : std::max(dMaxValue, dValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 757 | } else { |
| 758 | uCount++; |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 759 | FX_DOUBLE dValue = ValueToDouble(pThis, argValue.get()); |
| 760 | dMaxValue = (uCount == 1) ? dValue : std::max(dMaxValue, dValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 761 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 762 | } |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 763 | if (uCount == 0) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 764 | args.GetReturnValue()->SetNull(); |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 765 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 766 | } |
dsinclair | 89ba6de | 2016-05-31 12:11:22 -0700 | [diff] [blame] | 767 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 768 | args.GetReturnValue()->SetDouble(dMaxValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 769 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 770 | |
| 771 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 772 | void CXFA_FM2JSContext::Min(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 773 | const CFX_ByteStringC& szFuncName, |
| 774 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 775 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | ec3da5b | 2016-05-25 16:42:05 -0700 | [diff] [blame] | 776 | v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 777 | uint32_t uCount = 0; |
| 778 | FX_DOUBLE dMinValue = 0.0; |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 779 | for (int32_t i = 0; i < args.GetLength(); i++) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 780 | std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 781 | if (argValue->IsNull()) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 782 | continue; |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 783 | |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 784 | if (argValue->IsArray()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 785 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 786 | argValue->GetObjectProperty("length", lengthValue.get()); |
| 787 | int32_t iLength = lengthValue->ToInteger(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 788 | if (iLength <= 2) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 789 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 790 | return; |
| 791 | } |
| 792 | |
| 793 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 794 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 795 | std::unique_ptr<CFXJSE_Value> newPropertyValue( |
| 796 | new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 797 | argValue->GetObjectPropertyByIdx(1, propertyValue.get()); |
| 798 | argValue->GetObjectPropertyByIdx(2, jsObjectValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 799 | if (propertyValue->IsNull()) { |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 800 | for (int32_t j = 2; j < iLength; j++) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 801 | argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 802 | GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 803 | if (newPropertyValue->IsNull()) |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 804 | continue; |
| 805 | |
| 806 | uCount++; |
| 807 | FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get()); |
| 808 | dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 809 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 810 | } else { |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 811 | for (int32_t j = 2; j < iLength; j++) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 812 | argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 813 | jsObjectValue->GetObjectProperty( |
| 814 | propertyValue->ToString().AsStringC(), newPropertyValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 815 | if (newPropertyValue->IsNull()) |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 816 | continue; |
| 817 | |
| 818 | uCount++; |
| 819 | FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get()); |
| 820 | dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue); |
| 821 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 822 | } |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 823 | } else if (argValue->IsObject()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 824 | std::unique_ptr<CFXJSE_Value> newPropertyValue( |
| 825 | new CFXJSE_Value(pIsolate)); |
| 826 | GetObjectDefaultValue(argValue.get(), newPropertyValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 827 | if (newPropertyValue->IsNull()) |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 828 | continue; |
| 829 | |
| 830 | uCount++; |
| 831 | FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get()); |
| 832 | dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 833 | } else { |
| 834 | uCount++; |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 835 | FX_DOUBLE dValue = ValueToDouble(pThis, argValue.get()); |
| 836 | dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 837 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 838 | } |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 839 | if (uCount == 0) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 840 | args.GetReturnValue()->SetNull(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 841 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 842 | } |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 843 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 844 | args.GetReturnValue()->SetDouble(dMinValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 845 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 846 | |
| 847 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 848 | void CXFA_FM2JSContext::Mod(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 849 | const CFX_ByteStringC& szFuncName, |
| 850 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 851 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 852 | if (args.GetLength() != 2) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 853 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Mod"); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 854 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 855 | } |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 856 | |
| 857 | std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); |
| 858 | std::unique_ptr<CFXJSE_Value> argTwo = args.GetValue(1); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 859 | if (argOne->IsNull() || argTwo->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 860 | args.GetReturnValue()->SetNull(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 861 | return; |
| 862 | } |
| 863 | |
| 864 | bool argOneResult; |
| 865 | FX_DOUBLE dDividend = ExtractDouble(pThis, argOne.get(), &argOneResult); |
| 866 | bool argTwoResult; |
| 867 | FX_DOUBLE dDivisor = ExtractDouble(pThis, argTwo.get(), &argTwoResult); |
| 868 | if (!argOneResult || !argTwoResult) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 869 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 870 | return; |
| 871 | } |
| 872 | |
| 873 | if (dDivisor == 0.0) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 874 | pContext->ThrowException(XFA_IDS_DIVIDE_ZERO); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 875 | return; |
| 876 | } |
| 877 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 878 | args.GetReturnValue()->SetDouble(dDividend - |
| 879 | dDivisor * (int32_t)(dDividend / dDivisor)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 880 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 881 | |
| 882 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 883 | void CXFA_FM2JSContext::Round(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 884 | const CFX_ByteStringC& szFuncName, |
| 885 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 886 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 887 | int32_t argc = args.GetLength(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 888 | if (argc < 1 || argc > 2) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 889 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Round"); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 890 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 891 | } |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 892 | |
| 893 | std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 894 | if (argOne->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 895 | args.GetReturnValue()->SetNull(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 896 | return; |
| 897 | } |
| 898 | |
| 899 | bool dValueRet; |
| 900 | FX_DOUBLE dValue = ExtractDouble(pThis, argOne.get(), &dValueRet); |
| 901 | if (!dValueRet) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 902 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 903 | return; |
| 904 | } |
| 905 | |
| 906 | uint8_t uPrecision = 0; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 907 | if (argc > 1) { |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 908 | std::unique_ptr<CFXJSE_Value> argTwo = args.GetValue(1); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 909 | if (argTwo->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 910 | args.GetReturnValue()->SetNull(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 911 | return; |
| 912 | } |
| 913 | |
| 914 | bool dPrecisionRet; |
| 915 | FX_DOUBLE dPrecision = ExtractDouble(pThis, argTwo.get(), &dPrecisionRet); |
| 916 | if (!dPrecisionRet) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 917 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 918 | return; |
| 919 | } |
| 920 | |
| 921 | uPrecision = |
| 922 | static_cast<uint8_t>(std::min(std::max(dPrecision, 0.0), 12.0)); |
| 923 | } |
| 924 | |
| 925 | CFX_Decimal decimalValue((FX_FLOAT)dValue, uPrecision); |
| 926 | CFX_WideString wsValue = decimalValue; |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 927 | args.GetReturnValue()->SetString(wsValue.UTF8Encode().AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 928 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 929 | |
| 930 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 931 | void CXFA_FM2JSContext::Sum(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 932 | const CFX_ByteStringC& szFuncName, |
| 933 | CFXJSE_Arguments& args) { |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 934 | int32_t argc = args.GetLength(); |
| 935 | if (argc == 0) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 936 | args.GetReturnValue()->SetNull(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 937 | return; |
| 938 | } |
| 939 | |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 940 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | ec3da5b | 2016-05-25 16:42:05 -0700 | [diff] [blame] | 941 | v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 942 | uint32_t uCount = 0; |
| 943 | FX_DOUBLE dSum = 0.0; |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 944 | for (int32_t i = 0; i < argc; i++) { |
| 945 | std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 946 | if (argValue->IsNull()) |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 947 | continue; |
| 948 | |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 949 | if (argValue->IsArray()) { |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 950 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 951 | argValue->GetObjectProperty("length", lengthValue.get()); |
| 952 | int32_t iLength = lengthValue->ToInteger(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 953 | if (iLength <= 2) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 954 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 955 | return; |
| 956 | } |
| 957 | |
| 958 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 959 | argValue->GetObjectPropertyByIdx(1, propertyValue.get()); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 960 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 961 | std::unique_ptr<CFXJSE_Value> newPropertyValue( |
| 962 | new CFXJSE_Value(pIsolate)); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 963 | if (propertyValue->IsNull()) { |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 964 | for (int32_t j = 2; j < iLength; j++) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 965 | argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 966 | GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 967 | if (newPropertyValue->IsNull()) |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 968 | continue; |
| 969 | |
| 970 | dSum += ValueToDouble(pThis, jsObjectValue.get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 971 | uCount++; |
| 972 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 973 | } else { |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 974 | for (int32_t j = 2; j < iLength; j++) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 975 | argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 976 | jsObjectValue->GetObjectProperty( |
| 977 | propertyValue->ToString().AsStringC(), newPropertyValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 978 | if (newPropertyValue->IsNull()) |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 979 | continue; |
| 980 | |
| 981 | dSum += ValueToDouble(pThis, newPropertyValue.get()); |
| 982 | uCount++; |
| 983 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 984 | } |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 985 | } else if (argValue->IsObject()) { |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 986 | std::unique_ptr<CFXJSE_Value> newPropertyValue( |
| 987 | new CFXJSE_Value(pIsolate)); |
| 988 | GetObjectDefaultValue(argValue.get(), newPropertyValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 989 | if (newPropertyValue->IsNull()) |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 990 | continue; |
| 991 | |
| 992 | dSum += ValueToDouble(pThis, argValue.get()); |
| 993 | uCount++; |
| 994 | } else { |
| 995 | dSum += ValueToDouble(pThis, argValue.get()); |
| 996 | uCount++; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 997 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 998 | } |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 999 | if (uCount == 0) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1000 | args.GetReturnValue()->SetNull(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1001 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1002 | } |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1003 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1004 | args.GetReturnValue()->SetDouble(dSum); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1005 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1006 | |
| 1007 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1008 | void CXFA_FM2JSContext::Date(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1009 | const CFX_ByteStringC& szFuncName, |
| 1010 | CFXJSE_Arguments& args) { |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1011 | if (args.GetLength() != 0) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1012 | ToJSContext(pThis, nullptr) |
| 1013 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Date"); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1014 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1015 | } |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1016 | |
| 1017 | time_t currentTime; |
| 1018 | time(¤tTime); |
| 1019 | struct tm* pTmStruct = gmtime(¤tTime); |
| 1020 | |
| 1021 | CFX_ByteString bufferYear; |
| 1022 | CFX_ByteString bufferMon; |
| 1023 | CFX_ByteString bufferDay; |
| 1024 | bufferYear.Format("%d", pTmStruct->tm_year + 1900); |
| 1025 | bufferMon.Format("%02d", pTmStruct->tm_mon + 1); |
| 1026 | bufferDay.Format("%02d", pTmStruct->tm_mday); |
| 1027 | |
| 1028 | CFX_ByteString bufferCurrent = bufferYear + bufferMon + bufferDay; |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1029 | args.GetReturnValue()->SetInteger(DateString2Num(bufferCurrent.AsStringC())); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1030 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1031 | |
| 1032 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1033 | void CXFA_FM2JSContext::Date2Num(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1034 | const CFX_ByteStringC& szFuncName, |
| 1035 | CFXJSE_Arguments& args) { |
| 1036 | int32_t argc = args.GetLength(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1037 | if (argc < 1 || argc > 3) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1038 | ToJSContext(pThis, nullptr) |
| 1039 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Date2Num"); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1040 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1041 | } |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1042 | |
| 1043 | std::unique_ptr<CFXJSE_Value> dateValue = GetSimpleValue(pThis, args, 0); |
| 1044 | if (ValueIsNull(pThis, dateValue.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1045 | args.GetReturnValue()->SetNull(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1046 | return; |
| 1047 | } |
| 1048 | |
| 1049 | CFX_ByteString dateString; |
| 1050 | ValueToUTF8String(dateValue.get(), dateString); |
| 1051 | |
| 1052 | CFX_ByteString formatString; |
| 1053 | if (argc > 1) { |
| 1054 | std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1); |
| 1055 | if (ValueIsNull(pThis, formatValue.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1056 | args.GetReturnValue()->SetNull(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1057 | return; |
| 1058 | } |
| 1059 | ValueToUTF8String(formatValue.get(), formatString); |
| 1060 | } |
| 1061 | |
| 1062 | CFX_ByteString localString; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1063 | if (argc > 2) { |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1064 | std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2); |
| 1065 | if (ValueIsNull(pThis, localValue.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1066 | args.GetReturnValue()->SetNull(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1067 | return; |
| 1068 | } |
| 1069 | ValueToUTF8String(localValue.get(), localString); |
| 1070 | } |
| 1071 | |
| 1072 | CFX_ByteString szIsoDateString; |
| 1073 | if (!Local2IsoDate(pThis, dateString.AsStringC(), formatString.AsStringC(), |
| 1074 | localString.AsStringC(), szIsoDateString)) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1075 | args.GetReturnValue()->SetInteger(0); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1076 | return; |
| 1077 | } |
| 1078 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1079 | args.GetReturnValue()->SetInteger( |
| 1080 | DateString2Num(szIsoDateString.AsStringC())); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1081 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1082 | |
| 1083 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1084 | void CXFA_FM2JSContext::DateFmt(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1085 | const CFX_ByteStringC& szFuncName, |
| 1086 | CFXJSE_Arguments& args) { |
| 1087 | int32_t argc = args.GetLength(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1088 | if (argc > 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1089 | ToJSContext(pThis, nullptr) |
| 1090 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Date2Num"); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1091 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1092 | } |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1093 | |
| 1094 | int32_t iStyle = 0; |
| 1095 | if (argc > 0) { |
| 1096 | std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1097 | if (argStyle->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1098 | args.GetReturnValue()->SetNull(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1099 | return; |
| 1100 | } |
| 1101 | |
| 1102 | iStyle = (int32_t)ValueToFloat(pThis, argStyle.get()); |
| 1103 | if (iStyle < 0 || iStyle > 4) |
| 1104 | iStyle = 0; |
| 1105 | } |
| 1106 | |
| 1107 | CFX_ByteString szLocal; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1108 | if (argc > 1) { |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1109 | std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1110 | if (argLocal->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1111 | args.GetReturnValue()->SetNull(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1112 | return; |
| 1113 | } |
| 1114 | ValueToUTF8String(argLocal.get(), szLocal); |
| 1115 | } |
| 1116 | |
| 1117 | CFX_ByteString formatStr; |
| 1118 | GetStandardDateFormat(pThis, iStyle, szLocal.AsStringC(), formatStr); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1119 | args.GetReturnValue()->SetString(formatStr.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1120 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1121 | |
| 1122 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1123 | void CXFA_FM2JSContext::IsoDate2Num(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1124 | const CFX_ByteStringC& szFuncName, |
| 1125 | CFXJSE_Arguments& args) { |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1126 | if (args.GetLength() != 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1127 | ToJSContext(pThis, nullptr) |
| 1128 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"IsoDate2Num"); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1129 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1130 | } |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1131 | |
| 1132 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1133 | if (argOne->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1134 | args.GetReturnValue()->SetNull(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1135 | return; |
| 1136 | } |
| 1137 | |
| 1138 | CFX_ByteString szArgString; |
| 1139 | ValueToUTF8String(argOne.get(), szArgString); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1140 | args.GetReturnValue()->SetInteger(DateString2Num(szArgString.AsStringC())); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1141 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1142 | |
| 1143 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1144 | void CXFA_FM2JSContext::IsoTime2Num(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1145 | const CFX_ByteStringC& szFuncName, |
| 1146 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1147 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1148 | if (args.GetLength() != 1) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 1149 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, |
| 1150 | L"IsoTime2Num"); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1151 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1152 | } |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1153 | |
| 1154 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 1155 | if (ValueIsNull(pThis, argOne.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1156 | args.GetReturnValue()->SetNull(); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1157 | return; |
| 1158 | } |
| 1159 | |
| 1160 | CXFA_Document* pDoc = pContext->GetDocument(); |
| 1161 | CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); |
| 1162 | |
| 1163 | CFX_ByteString szArgString; |
| 1164 | ValueToUTF8String(argOne.get(), szArgString); |
| 1165 | szArgString = szArgString.Mid(szArgString.Find('T', 0) + 1); |
| 1166 | if (szArgString.IsEmpty()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1167 | args.GetReturnValue()->SetInteger(0); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1168 | return; |
| 1169 | } |
| 1170 | |
| 1171 | CXFA_LocaleValue timeValue( |
| 1172 | XFA_VT_TIME, CFX_WideString::FromUTF8(szArgString.AsStringC()), pMgr); |
| 1173 | if (!timeValue.IsValid()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1174 | args.GetReturnValue()->SetInteger(0); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | CFX_Unitime uniTime = timeValue.GetTime(); |
| 1179 | int32_t hour = uniTime.GetHour(); |
| 1180 | int32_t min = uniTime.GetMinute(); |
| 1181 | int32_t second = uniTime.GetSecond(); |
| 1182 | int32_t milSecond = uniTime.GetMillisecond(); |
| 1183 | |
| 1184 | FX_TIMEZONE tzLocale; |
| 1185 | pMgr->GetDefLocale()->GetTimeZone(tzLocale); |
| 1186 | |
| 1187 | // TODO(dsinclair): See if there is other time conversion code in pdfium and |
| 1188 | // consolidate. |
| 1189 | int32_t mins = hour * 60 + min; |
| 1190 | mins -= (tzLocale.tzHour * 60); |
| 1191 | while (mins > 1440) |
| 1192 | mins -= 1440; |
| 1193 | while (mins < 0) |
| 1194 | mins += 1440; |
| 1195 | hour = mins / 60; |
| 1196 | min = mins % 60; |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1197 | |
| 1198 | args.GetReturnValue()->SetInteger(hour * 3600000 + min * 60000 + |
| 1199 | second * 1000 + milSecond + 1); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1200 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1201 | |
| 1202 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1203 | void CXFA_FM2JSContext::LocalDateFmt(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1204 | const CFX_ByteStringC& szFuncName, |
| 1205 | CFXJSE_Arguments& args) { |
| 1206 | int32_t argc = args.GetLength(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1207 | if (argc > 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1208 | ToJSContext(pThis, nullptr) |
| 1209 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"LocalDateFmt"); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1210 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1211 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1212 | |
| 1213 | int32_t iStyle = 0; |
| 1214 | if (argc > 0) { |
| 1215 | std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1216 | if (argStyle->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1217 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1218 | return; |
| 1219 | } |
| 1220 | iStyle = (int32_t)ValueToFloat(pThis, argStyle.get()); |
| 1221 | if (iStyle > 4 || iStyle < 0) |
| 1222 | iStyle = 0; |
| 1223 | } |
| 1224 | |
| 1225 | CFX_ByteString szLocal; |
| 1226 | if (argc > 1) { |
| 1227 | std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1228 | if (argLocal->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1229 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1230 | return; |
| 1231 | } |
| 1232 | ValueToUTF8String(argLocal.get(), szLocal); |
| 1233 | } |
| 1234 | |
| 1235 | CFX_ByteString formatStr; |
| 1236 | GetLocalDateFormat(pThis, iStyle, szLocal.AsStringC(), formatStr, FALSE); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1237 | args.GetReturnValue()->SetString(formatStr.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1238 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1239 | |
| 1240 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1241 | void CXFA_FM2JSContext::LocalTimeFmt(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1242 | const CFX_ByteStringC& szFuncName, |
| 1243 | CFXJSE_Arguments& args) { |
| 1244 | int32_t argc = args.GetLength(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1245 | if (argc > 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1246 | ToJSContext(pThis, nullptr) |
| 1247 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"LocalTimeFmt"); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1248 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1249 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1250 | |
| 1251 | int32_t iStyle = 0; |
| 1252 | if (argc > 0) { |
| 1253 | std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1254 | if (argStyle->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1255 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1256 | return; |
| 1257 | } |
| 1258 | iStyle = (int32_t)ValueToFloat(pThis, argStyle.get()); |
| 1259 | if (iStyle > 4 || iStyle < 0) |
| 1260 | iStyle = 0; |
| 1261 | } |
| 1262 | |
| 1263 | CFX_ByteString szLocal; |
| 1264 | if (argc > 1) { |
| 1265 | std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1266 | if (argLocal->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1267 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1268 | return; |
| 1269 | } |
| 1270 | ValueToUTF8String(argLocal.get(), szLocal); |
| 1271 | } |
| 1272 | |
| 1273 | CFX_ByteString formatStr; |
| 1274 | GetLocalTimeFormat(pThis, iStyle, szLocal.AsStringC(), formatStr, FALSE); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1275 | args.GetReturnValue()->SetString(formatStr.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1276 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1277 | |
| 1278 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1279 | void CXFA_FM2JSContext::Num2Date(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1280 | const CFX_ByteStringC& szFuncName, |
| 1281 | CFXJSE_Arguments& args) { |
| 1282 | int32_t argc = args.GetLength(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1283 | if (argc < 1 || argc > 3) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1284 | ToJSContext(pThis, nullptr) |
| 1285 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Num2Date"); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1286 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1287 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1288 | |
| 1289 | std::unique_ptr<CFXJSE_Value> dateValue = GetSimpleValue(pThis, args, 0); |
| 1290 | if (ValueIsNull(pThis, dateValue.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1291 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1292 | return; |
| 1293 | } |
| 1294 | int32_t dDate = (int32_t)ValueToFloat(pThis, dateValue.get()); |
| 1295 | if (dDate < 1) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1296 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1297 | return; |
| 1298 | } |
| 1299 | |
| 1300 | CFX_ByteString formatString; |
| 1301 | if (argc > 1) { |
| 1302 | std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1); |
| 1303 | if (ValueIsNull(pThis, formatValue.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1304 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1305 | return; |
| 1306 | } |
| 1307 | ValueToUTF8String(formatValue.get(), formatString); |
| 1308 | } |
| 1309 | |
| 1310 | CFX_ByteString localString; |
| 1311 | if (argc > 2) { |
| 1312 | std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2); |
| 1313 | if (ValueIsNull(pThis, localValue.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1314 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1315 | return; |
| 1316 | } |
| 1317 | ValueToUTF8String(localValue.get(), localString); |
| 1318 | } |
| 1319 | |
| 1320 | int32_t iYear = 1900; |
| 1321 | int32_t iMonth = 1; |
| 1322 | int32_t iDay = 1; |
| 1323 | int32_t i = 0; |
| 1324 | while (dDate > 0) { |
| 1325 | if (iMonth == 2) { |
| 1326 | if ((!((iYear + i) % 4) && ((iYear + i) % 100)) || !((iYear + i) % 400)) { |
| 1327 | if (dDate > 29) { |
| 1328 | ++iMonth; |
| 1329 | if (iMonth > 12) { |
| 1330 | iMonth = 1; |
| 1331 | ++i; |
| 1332 | } |
| 1333 | iDay = 1; |
| 1334 | dDate -= 29; |
| 1335 | } else { |
| 1336 | iDay += static_cast<int32_t>(dDate) - 1; |
| 1337 | dDate = 0; |
| 1338 | } |
| 1339 | } else { |
| 1340 | if (dDate > 28) { |
| 1341 | ++iMonth; |
| 1342 | if (iMonth > 12) { |
| 1343 | iMonth = 1; |
| 1344 | ++i; |
| 1345 | } |
| 1346 | iDay = 1; |
| 1347 | dDate -= 28; |
| 1348 | } else { |
| 1349 | iDay += static_cast<int32_t>(dDate) - 1; |
| 1350 | dDate = 0; |
| 1351 | } |
| 1352 | } |
| 1353 | } else if (iMonth < 8) { |
| 1354 | if ((iMonth % 2 == 0)) { |
| 1355 | if (dDate > 30) { |
| 1356 | ++iMonth; |
| 1357 | if (iMonth > 12) { |
| 1358 | iMonth = 1; |
| 1359 | ++i; |
| 1360 | } |
| 1361 | iDay = 1; |
| 1362 | dDate -= 30; |
| 1363 | } else { |
| 1364 | iDay += static_cast<int32_t>(dDate) - 1; |
| 1365 | dDate = 0; |
| 1366 | } |
| 1367 | } else { |
| 1368 | if (dDate > 31) { |
| 1369 | ++iMonth; |
| 1370 | if (iMonth > 12) { |
| 1371 | iMonth = 1; |
| 1372 | ++i; |
| 1373 | } |
| 1374 | iDay = 1; |
| 1375 | dDate -= 31; |
| 1376 | } else { |
| 1377 | iDay += static_cast<int32_t>(dDate) - 1; |
| 1378 | dDate = 0; |
| 1379 | } |
| 1380 | } |
| 1381 | } else { |
| 1382 | if (iMonth % 2 != 0) { |
| 1383 | if (dDate > 30) { |
| 1384 | ++iMonth; |
| 1385 | if (iMonth > 12) { |
| 1386 | iMonth = 1; |
| 1387 | ++i; |
| 1388 | } |
| 1389 | iDay = 1; |
| 1390 | dDate -= 30; |
| 1391 | } else { |
| 1392 | iDay += static_cast<int32_t>(dDate) - 1; |
| 1393 | dDate = 0; |
| 1394 | } |
| 1395 | } else { |
| 1396 | if (dDate > 31) { |
| 1397 | ++iMonth; |
| 1398 | if (iMonth > 12) { |
| 1399 | iMonth = 1; |
| 1400 | ++i; |
| 1401 | } |
| 1402 | iDay = 1; |
| 1403 | dDate -= 31; |
| 1404 | } else { |
| 1405 | iDay += static_cast<int32_t>(dDate) - 1; |
| 1406 | dDate = 0; |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | CFX_ByteString szIsoDateString; |
| 1413 | szIsoDateString.Format("%d%02d%02d", iYear + i, iMonth, iDay); |
| 1414 | CFX_ByteString szLocalDateString; |
| 1415 | IsoDate2Local(pThis, szIsoDateString.AsStringC(), formatString.AsStringC(), |
| 1416 | localString.AsStringC(), szLocalDateString); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1417 | args.GetReturnValue()->SetString(szLocalDateString.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1418 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1419 | |
| 1420 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1421 | void CXFA_FM2JSContext::Num2GMTime(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1422 | const CFX_ByteStringC& szFuncName, |
| 1423 | CFXJSE_Arguments& args) { |
| 1424 | int32_t argc = args.GetLength(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1425 | if (argc < 1 || argc > 3) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1426 | ToJSContext(pThis, nullptr) |
| 1427 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Num2GMTime"); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1428 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1429 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1430 | |
| 1431 | std::unique_ptr<CFXJSE_Value> timeValue = GetSimpleValue(pThis, args, 0); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1432 | if (timeValue->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1433 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1434 | return; |
| 1435 | } |
| 1436 | int32_t iTime = (int32_t)ValueToFloat(pThis, timeValue.get()); |
| 1437 | if (FXSYS_abs(iTime) < 1.0) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1438 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1439 | return; |
| 1440 | } |
| 1441 | |
| 1442 | CFX_ByteString formatString; |
| 1443 | if (argc > 1) { |
| 1444 | std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1445 | if (formatValue->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1446 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1447 | return; |
| 1448 | } |
| 1449 | ValueToUTF8String(formatValue.get(), formatString); |
| 1450 | } |
| 1451 | |
| 1452 | CFX_ByteString localString; |
| 1453 | if (argc > 2) { |
| 1454 | std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1455 | if (localValue->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1456 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1457 | return; |
| 1458 | } |
| 1459 | ValueToUTF8String(localValue.get(), localString); |
| 1460 | } |
| 1461 | |
| 1462 | CFX_ByteString szGMTTimeString; |
| 1463 | Num2AllTime(pThis, iTime, formatString.AsStringC(), localString.AsStringC(), |
| 1464 | TRUE, szGMTTimeString); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1465 | args.GetReturnValue()->SetString(szGMTTimeString.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1466 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1467 | |
| 1468 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1469 | void CXFA_FM2JSContext::Num2Time(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1470 | const CFX_ByteStringC& szFuncName, |
| 1471 | CFXJSE_Arguments& args) { |
| 1472 | int32_t argc = args.GetLength(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1473 | if (argc < 1 || argc > 3) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1474 | ToJSContext(pThis, nullptr) |
| 1475 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Num2Time"); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1476 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1477 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1478 | |
| 1479 | std::unique_ptr<CFXJSE_Value> timeValue = GetSimpleValue(pThis, args, 0); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1480 | if (timeValue->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1481 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1482 | return; |
| 1483 | } |
| 1484 | FX_FLOAT fTime = ValueToFloat(pThis, timeValue.get()); |
| 1485 | if (FXSYS_fabs(fTime) < 1.0) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1486 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1487 | return; |
| 1488 | } |
| 1489 | |
| 1490 | CFX_ByteString formatString; |
| 1491 | if (argc > 1) { |
| 1492 | std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1493 | if (formatValue->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1494 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1495 | return; |
| 1496 | } |
| 1497 | ValueToUTF8String(formatValue.get(), formatString); |
| 1498 | } |
| 1499 | |
| 1500 | CFX_ByteString localString; |
| 1501 | if (argc > 2) { |
| 1502 | std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1503 | if (localValue->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1504 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1505 | return; |
| 1506 | } |
| 1507 | ValueToUTF8String(localValue.get(), localString); |
| 1508 | } |
| 1509 | |
| 1510 | CFX_ByteString szLocalTimeString; |
| 1511 | Num2AllTime(pThis, (int32_t)fTime, formatString.AsStringC(), |
| 1512 | localString.AsStringC(), FALSE, szLocalTimeString); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1513 | args.GetReturnValue()->SetString(szLocalTimeString.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1514 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1515 | |
| 1516 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1517 | void CXFA_FM2JSContext::Time(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1518 | const CFX_ByteStringC& szFuncName, |
| 1519 | CFXJSE_Arguments& args) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1520 | if (args.GetLength() != 0) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1521 | ToJSContext(pThis, nullptr) |
| 1522 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Time"); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1523 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1524 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1525 | |
| 1526 | time_t now; |
| 1527 | time(&now); |
| 1528 | |
| 1529 | struct tm* pGmt = gmtime(&now); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1530 | args.GetReturnValue()->SetInteger( |
| 1531 | (pGmt->tm_hour * 3600 + pGmt->tm_min * 60 + pGmt->tm_sec) * 1000); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1532 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1533 | |
| 1534 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1535 | void CXFA_FM2JSContext::Time2Num(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1536 | const CFX_ByteStringC& szFuncName, |
| 1537 | CFXJSE_Arguments& args) { |
| 1538 | int32_t argc = args.GetLength(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1539 | if (argc < 1 || argc > 3) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1540 | ToJSContext(pThis, nullptr) |
| 1541 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Time2Num"); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1542 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1543 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1544 | |
| 1545 | CFX_ByteString timeString; |
| 1546 | std::unique_ptr<CFXJSE_Value> timeValue = GetSimpleValue(pThis, args, 0); |
| 1547 | if (ValueIsNull(pThis, timeValue.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1548 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1549 | return; |
| 1550 | } |
| 1551 | ValueToUTF8String(timeValue.get(), timeString); |
| 1552 | |
| 1553 | CFX_ByteString formatString; |
| 1554 | if (argc > 1) { |
| 1555 | std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1); |
| 1556 | if (ValueIsNull(pThis, formatValue.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1557 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1558 | return; |
| 1559 | } |
| 1560 | ValueToUTF8String(formatValue.get(), formatString); |
| 1561 | } |
| 1562 | |
| 1563 | CFX_ByteString localString; |
| 1564 | if (argc > 2) { |
| 1565 | std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2); |
| 1566 | if (ValueIsNull(pThis, localValue.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1567 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1568 | return; |
| 1569 | } |
| 1570 | ValueToUTF8String(localValue.get(), localString); |
| 1571 | } |
| 1572 | |
| 1573 | CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); |
| 1574 | CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); |
| 1575 | IFX_Locale* pLocale = nullptr; |
| 1576 | if (localString.IsEmpty()) { |
| 1577 | CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject()); |
| 1578 | ASSERT(pThisNode); |
| 1579 | |
| 1580 | CXFA_WidgetData widgetData(pThisNode); |
| 1581 | pLocale = widgetData.GetLocal(); |
| 1582 | } else { |
| 1583 | pLocale = pMgr->GetLocaleByName( |
| 1584 | CFX_WideString::FromUTF8(localString.AsStringC())); |
| 1585 | } |
| 1586 | |
| 1587 | CFX_WideString wsFormat; |
| 1588 | if (formatString.IsEmpty()) |
| 1589 | pLocale->GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY_Default, wsFormat); |
| 1590 | else |
| 1591 | wsFormat = CFX_WideString::FromUTF8(formatString.AsStringC()); |
| 1592 | |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 1593 | wsFormat = L"time{" + wsFormat + L"}"; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1594 | CXFA_LocaleValue localeValue(XFA_VT_TIME, |
| 1595 | CFX_WideString::FromUTF8(timeString.AsStringC()), |
| 1596 | wsFormat, pLocale, pMgr); |
| 1597 | if (!localeValue.IsValid()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1598 | args.GetReturnValue()->SetInteger(0); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1599 | return; |
| 1600 | } |
| 1601 | |
| 1602 | CFX_Unitime uniTime = localeValue.GetTime(); |
| 1603 | int32_t hour = uniTime.GetHour(); |
| 1604 | int32_t min = uniTime.GetMinute(); |
| 1605 | int32_t second = uniTime.GetSecond(); |
| 1606 | int32_t milSecond = uniTime.GetMillisecond(); |
| 1607 | int32_t mins = hour * 60 + min; |
| 1608 | CXFA_TimeZoneProvider* pProvider = CXFA_TimeZoneProvider::Get(); |
| 1609 | if (pProvider) { |
| 1610 | FX_TIMEZONE tz; |
| 1611 | pProvider->GetTimeZone(tz); |
| 1612 | mins -= (tz.tzHour * 60); |
| 1613 | while (mins > 1440) |
| 1614 | mins -= 1440; |
| 1615 | while (mins < 0) |
| 1616 | mins += 1440; |
| 1617 | |
| 1618 | hour = mins / 60; |
| 1619 | min = mins % 60; |
| 1620 | } |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1621 | args.GetReturnValue()->SetInteger(hour * 3600000 + min * 60000 + |
| 1622 | second * 1000 + milSecond + 1); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1623 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1624 | |
| 1625 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1626 | void CXFA_FM2JSContext::TimeFmt(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1627 | const CFX_ByteStringC& szFuncName, |
| 1628 | CFXJSE_Arguments& args) { |
| 1629 | int32_t argc = args.GetLength(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1630 | if (argc > 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1631 | ToJSContext(pThis, nullptr) |
| 1632 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"TimeFmt"); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1633 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1634 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1635 | |
| 1636 | int32_t iStyle = 0; |
| 1637 | if (argc > 0) { |
| 1638 | std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1639 | if (argStyle->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1640 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1641 | return; |
| 1642 | } |
| 1643 | iStyle = (int32_t)ValueToFloat(pThis, argStyle.get()); |
| 1644 | if (iStyle > 4 || iStyle < 0) |
| 1645 | iStyle = 0; |
| 1646 | } |
| 1647 | |
| 1648 | CFX_ByteString szLocal; |
| 1649 | if (argc > 1) { |
| 1650 | std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 1651 | if (argLocal->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1652 | args.GetReturnValue()->SetNull(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1653 | return; |
| 1654 | } |
| 1655 | ValueToUTF8String(argLocal.get(), szLocal); |
| 1656 | } |
| 1657 | |
| 1658 | CFX_ByteString formatStr; |
| 1659 | GetStandardTimeFormat(pThis, iStyle, szLocal.AsStringC(), formatStr); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 1660 | args.GetReturnValue()->SetString(formatStr.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1661 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1662 | |
| 1663 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1664 | FX_BOOL CXFA_FM2JSContext::IsIsoDateFormat(const FX_CHAR* pData, |
| 1665 | int32_t iLength, |
| 1666 | int32_t& iStyle, |
| 1667 | int32_t& iYear, |
| 1668 | int32_t& iMonth, |
| 1669 | int32_t& iDay) { |
| 1670 | iYear = 0; |
| 1671 | iMonth = 1; |
| 1672 | iDay = 1; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1673 | |
| 1674 | if (iLength < 4) |
| 1675 | return FALSE; |
| 1676 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1677 | FX_CHAR strYear[5]; |
| 1678 | strYear[4] = '\0'; |
| 1679 | for (int32_t i = 0; i < 4; ++i) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1680 | if (pData[i] > '9' || pData[i] < '0') |
| 1681 | return FALSE; |
| 1682 | |
| 1683 | strYear[i] = pData[i]; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1684 | } |
| 1685 | iYear = FXSYS_atoi(strYear); |
| 1686 | iStyle = 0; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1687 | if (iLength == 4) |
| 1688 | return TRUE; |
| 1689 | |
| 1690 | iStyle = pData[4] == '-' ? 1 : 0; |
| 1691 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1692 | FX_CHAR strTemp[3]; |
| 1693 | strTemp[2] = '\0'; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1694 | int32_t iPosOff = iStyle == 0 ? 4 : 5; |
| 1695 | if ((pData[iPosOff] > '9' || pData[iPosOff] < '0') || |
| 1696 | (pData[iPosOff + 1] > '9' || pData[iPosOff + 1] < '0')) |
| 1697 | return FALSE; |
| 1698 | |
| 1699 | strTemp[0] = pData[iPosOff]; |
| 1700 | strTemp[1] = pData[iPosOff + 1]; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1701 | iMonth = FXSYS_atoi(strTemp); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1702 | if (iMonth > 12 || iMonth < 1) |
| 1703 | return FALSE; |
| 1704 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1705 | if (iStyle == 0) { |
| 1706 | iPosOff += 2; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1707 | if (iLength == 6) |
| 1708 | return TRUE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1709 | } else { |
| 1710 | iPosOff += 3; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1711 | if (iLength == 7) |
| 1712 | return TRUE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1713 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1714 | if ((pData[iPosOff] > '9' || pData[iPosOff] < '0') || |
| 1715 | (pData[iPosOff + 1] > '9' || pData[iPosOff + 1] < '0')) |
| 1716 | return FALSE; |
| 1717 | |
| 1718 | strTemp[0] = pData[iPosOff]; |
| 1719 | strTemp[1] = pData[iPosOff + 1]; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1720 | iDay = FXSYS_atoi(strTemp); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1721 | if (iPosOff + 2 < iLength) |
| 1722 | return FALSE; |
| 1723 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1724 | if ((!(iYear % 4) && (iYear % 100)) || !(iYear % 400)) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1725 | if (iMonth == 2 && iDay > 29) |
| 1726 | return FALSE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1727 | } else { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1728 | if (iMonth == 2 && iDay > 28) |
| 1729 | return FALSE; |
| 1730 | } |
| 1731 | if (iMonth != 2) { |
| 1732 | if (iMonth < 8) { |
| 1733 | if (iDay > (iMonth % 2 == 0 ? 30 : 31)) |
| 1734 | return FALSE; |
| 1735 | } else if (iDay > (iMonth % 2 == 0 ? 31 : 30)) { |
| 1736 | return FALSE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1737 | } |
| 1738 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1739 | return TRUE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1740 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1741 | |
| 1742 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1743 | FX_BOOL CXFA_FM2JSContext::IsIsoTimeFormat(const FX_CHAR* pData, |
| 1744 | int32_t iLength, |
| 1745 | int32_t& iHour, |
| 1746 | int32_t& iMinute, |
| 1747 | int32_t& iSecond, |
| 1748 | int32_t& iMilliSecond, |
| 1749 | int32_t& iZoneHour, |
| 1750 | int32_t& iZoneMinute) { |
| 1751 | iHour = 0; |
| 1752 | iMinute = 0; |
| 1753 | iSecond = 0; |
| 1754 | iMilliSecond = 0; |
| 1755 | iZoneHour = 0; |
| 1756 | iZoneMinute = 0; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1757 | if (!pData) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1758 | return FALSE; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1759 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1760 | FX_CHAR strTemp[3]; |
| 1761 | strTemp[2] = '\0'; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1762 | int32_t iZone = 0; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1763 | int32_t i = 0; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1764 | while (i < iLength) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1765 | if ((pData[i] > '9' || pData[i] < '0') && pData[i] != ':') { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1766 | iZone = i; |
| 1767 | break; |
| 1768 | } |
| 1769 | ++i; |
| 1770 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1771 | if (i == iLength) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1772 | iZone = iLength; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1773 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1774 | int32_t iPos = 0; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1775 | int32_t iIndex = 0; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1776 | while (iIndex < iZone) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1777 | if (iIndex >= iZone) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1778 | break; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1779 | |
| 1780 | if (pData[iIndex] > '9' || pData[iIndex] < '0') |
| 1781 | return FALSE; |
| 1782 | |
| 1783 | strTemp[0] = pData[iIndex]; |
| 1784 | if (pData[iIndex + 1] > '9' || pData[iIndex + 1] < '0') |
| 1785 | return FALSE; |
| 1786 | |
| 1787 | strTemp[1] = pData[iIndex + 1]; |
| 1788 | if (FXSYS_atoi(strTemp) > 60) |
| 1789 | return FALSE; |
| 1790 | |
| 1791 | if (pData[2] == ':') { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1792 | if (iPos == 0) { |
| 1793 | iHour = FXSYS_atoi(strTemp); |
| 1794 | ++iPos; |
| 1795 | } else if (iPos == 1) { |
| 1796 | iMinute = FXSYS_atoi(strTemp); |
| 1797 | ++iPos; |
| 1798 | } else { |
| 1799 | iSecond = FXSYS_atoi(strTemp); |
| 1800 | } |
| 1801 | iIndex += 3; |
| 1802 | } else { |
| 1803 | if (iPos == 0) { |
| 1804 | iHour = FXSYS_atoi(strTemp); |
| 1805 | ++iPos; |
| 1806 | } else if (iPos == 1) { |
| 1807 | iMinute = FXSYS_atoi(strTemp); |
| 1808 | ++iPos; |
| 1809 | } else if (iPos == 2) { |
| 1810 | iSecond = FXSYS_atoi(strTemp); |
| 1811 | ++iPos; |
| 1812 | } |
| 1813 | iIndex += 2; |
| 1814 | } |
| 1815 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1816 | if (pData[iIndex] == '.') { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1817 | ++iIndex; |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 1818 | FX_CHAR strSec[4]; |
| 1819 | strSec[3] = '\0'; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1820 | if (pData[iIndex] > '9' || pData[iIndex] < '0') |
| 1821 | return FALSE; |
| 1822 | |
| 1823 | strSec[0] = pData[iIndex]; |
| 1824 | if (pData[iIndex + 1] > '9' || pData[iIndex + 1] < '0') |
| 1825 | return FALSE; |
| 1826 | |
| 1827 | strSec[1] = pData[iIndex + 1]; |
| 1828 | if (pData[iIndex + 2] > '9' || pData[iIndex + 2] < '0') |
| 1829 | return FALSE; |
| 1830 | |
| 1831 | strSec[2] = pData[iIndex + 2]; |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 1832 | iMilliSecond = FXSYS_atoi(strSec); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1833 | if (iMilliSecond > 100) { |
| 1834 | iMilliSecond = 0; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1835 | return FALSE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1836 | } |
| 1837 | iIndex += 3; |
| 1838 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1839 | if (pData[iIndex] == 'z' || pData[iIndex] == 'Z') |
| 1840 | return TRUE; |
| 1841 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1842 | int32_t iSign = 1; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1843 | if (pData[iIndex] == '+') { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1844 | ++iIndex; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1845 | } else if (pData[iIndex] == '-') { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1846 | iSign = -1; |
| 1847 | ++iIndex; |
| 1848 | } |
| 1849 | iPos = 0; |
| 1850 | while (iIndex < iLength) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1851 | if (iIndex >= iLength) |
| 1852 | return FALSE; |
| 1853 | if (pData[iIndex] > '9' || pData[iIndex] < '0') |
| 1854 | return FALSE; |
| 1855 | |
| 1856 | strTemp[0] = pData[iIndex]; |
| 1857 | if (pData[iIndex + 1] > '9' || pData[iIndex + 1] < '0') |
| 1858 | return FALSE; |
| 1859 | |
| 1860 | strTemp[1] = pData[iIndex + 1]; |
| 1861 | if (FXSYS_atoi(strTemp) > 60) |
| 1862 | return FALSE; |
| 1863 | |
| 1864 | if (pData[2] == ':') { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1865 | if (iPos == 0) { |
| 1866 | iZoneHour = FXSYS_atoi(strTemp); |
| 1867 | } else if (iPos == 1) { |
| 1868 | iZoneMinute = FXSYS_atoi(strTemp); |
| 1869 | } |
| 1870 | iIndex += 3; |
| 1871 | } else { |
| 1872 | if (!iPos) { |
| 1873 | iZoneHour = FXSYS_atoi(strTemp); |
| 1874 | ++iPos; |
| 1875 | } else if (iPos == 1) { |
| 1876 | iZoneMinute = FXSYS_atoi(strTemp); |
| 1877 | ++iPos; |
| 1878 | } |
| 1879 | iIndex += 2; |
| 1880 | } |
| 1881 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1882 | if (iIndex < iLength) |
| 1883 | return FALSE; |
| 1884 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1885 | iZoneHour *= iSign; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1886 | return TRUE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1887 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1888 | |
| 1889 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1890 | FX_BOOL CXFA_FM2JSContext::IsIsoDateTimeFormat(const FX_CHAR* pData, |
| 1891 | int32_t iLength, |
| 1892 | int32_t& iYear, |
| 1893 | int32_t& iMonth, |
| 1894 | int32_t& iDay, |
| 1895 | int32_t& iHour, |
| 1896 | int32_t& iMinute, |
| 1897 | int32_t& iSecond, |
| 1898 | int32_t& iMillionSecond, |
| 1899 | int32_t& iZoneHour, |
| 1900 | int32_t& iZoneMinute) { |
| 1901 | iYear = 0; |
| 1902 | iMonth = 0; |
| 1903 | iDay = 0; |
| 1904 | iHour = 0; |
| 1905 | iMinute = 0; |
| 1906 | iSecond = 0; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1907 | if (!pData) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1908 | return FALSE; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1909 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1910 | int32_t iIndex = 0; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1911 | while (pData[iIndex] != 'T' && pData[iIndex] != 't') { |
| 1912 | if (iIndex >= iLength) |
| 1913 | return FALSE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1914 | ++iIndex; |
| 1915 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1916 | if (iIndex != 8 && iIndex != 10) |
| 1917 | return FALSE; |
| 1918 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1919 | int32_t iStyle = -1; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1920 | if (!IsIsoDateFormat(pData, iIndex, iStyle, iYear, iMonth, iDay)) |
| 1921 | return FALSE; |
| 1922 | if (pData[iIndex] != 'T' && pData[iIndex] != 't') |
| 1923 | return TRUE; |
| 1924 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1925 | ++iIndex; |
| 1926 | if (((iLength - iIndex > 13) && (iLength - iIndex < 6)) && |
| 1927 | (iLength - iIndex != 15)) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1928 | return TRUE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1929 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1930 | if (!IsIsoTimeFormat(pData + iIndex, iLength - iIndex, iHour, iMinute, |
| 1931 | iSecond, iMillionSecond, iZoneHour, iZoneMinute)) { |
| 1932 | return FALSE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1933 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1934 | |
| 1935 | return TRUE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1936 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1937 | |
| 1938 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1939 | FX_BOOL CXFA_FM2JSContext::Local2IsoDate(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1940 | const CFX_ByteStringC& szDate, |
| 1941 | const CFX_ByteStringC& szFormat, |
| 1942 | const CFX_ByteStringC& szLocale, |
| 1943 | CFX_ByteString& strIsoDate) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1944 | CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1945 | if (!pDoc) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1946 | return FALSE; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1947 | |
| 1948 | CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1949 | IFX_Locale* pLocale = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1950 | if (szLocale.IsEmpty()) { |
| 1951 | CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject()); |
dsinclair | 43854a5 | 2016-04-27 12:26:00 -0700 | [diff] [blame] | 1952 | ASSERT(pThisNode); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1953 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1954 | CXFA_WidgetData widgetData(pThisNode); |
| 1955 | pLocale = widgetData.GetLocal(); |
| 1956 | } else { |
tsepez | afe9430 | 2016-05-13 17:21:31 -0700 | [diff] [blame] | 1957 | pLocale = pMgr->GetLocaleByName(CFX_WideString::FromUTF8(szLocale)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1958 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1959 | if (!pLocale) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1960 | return FALSE; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1961 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1962 | CFX_WideString wsFormat; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1963 | if (szFormat.IsEmpty()) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1964 | pLocale->GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY_Default, wsFormat); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1965 | else |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 1966 | wsFormat = CFX_WideString::FromUTF8(szFormat); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1967 | |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 1968 | CXFA_LocaleValue widgetValue(XFA_VT_DATE, CFX_WideString::FromUTF8(szDate), |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1969 | wsFormat, pLocale, pMgr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1970 | CFX_Unitime dt = widgetValue.GetDate(); |
| 1971 | strIsoDate.Format("%4d-%02d-%02d", dt.GetYear(), dt.GetMonth(), dt.GetDay()); |
| 1972 | return TRUE; |
| 1973 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1974 | |
| 1975 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 1976 | FX_BOOL CXFA_FM2JSContext::Local2IsoTime(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1977 | const CFX_ByteStringC& szTime, |
| 1978 | const CFX_ByteStringC& szFormat, |
| 1979 | const CFX_ByteStringC& szLocale, |
| 1980 | CFX_ByteString& strIsoTime) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 1981 | CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1982 | if (!pDoc) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1983 | return FALSE; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1984 | |
| 1985 | CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 1986 | IFX_Locale* pLocale = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1987 | if (szLocale.IsEmpty()) { |
| 1988 | CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject()); |
dsinclair | 43854a5 | 2016-04-27 12:26:00 -0700 | [diff] [blame] | 1989 | ASSERT(pThisNode); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1990 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1991 | CXFA_WidgetData widgetData(pThisNode); |
| 1992 | pLocale = widgetData.GetLocal(); |
| 1993 | } else { |
tsepez | afe9430 | 2016-05-13 17:21:31 -0700 | [diff] [blame] | 1994 | pLocale = pMgr->GetLocaleByName(CFX_WideString::FromUTF8(szLocale)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1995 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1996 | if (!pLocale) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1997 | return FALSE; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 1998 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1999 | CFX_WideString wsFormat; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2000 | if (szFormat.IsEmpty()) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2001 | pLocale->GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY_Default, wsFormat); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2002 | else |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 2003 | wsFormat = CFX_WideString::FromUTF8(szFormat); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2004 | |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 2005 | wsFormat = L"time{" + wsFormat + L"}"; |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 2006 | CXFA_LocaleValue widgetValue(XFA_VT_TIME, CFX_WideString::FromUTF8(szTime), |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2007 | wsFormat, pLocale, pMgr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2008 | CFX_Unitime utime = widgetValue.GetTime(); |
| 2009 | strIsoTime.Format("%02d:%02d:%02d.%03d", utime.GetHour(), utime.GetMinute(), |
| 2010 | utime.GetSecond(), utime.GetMillisecond()); |
| 2011 | return TRUE; |
| 2012 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2013 | |
| 2014 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2015 | FX_BOOL CXFA_FM2JSContext::IsoDate2Local(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2016 | const CFX_ByteStringC& szDate, |
| 2017 | const CFX_ByteStringC& szFormat, |
| 2018 | const CFX_ByteStringC& szLocale, |
| 2019 | CFX_ByteString& strLocalDate) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2020 | CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2021 | if (!pDoc) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2022 | return FALSE; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2023 | |
| 2024 | CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2025 | IFX_Locale* pLocale = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2026 | if (szLocale.IsEmpty()) { |
| 2027 | CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject()); |
dsinclair | 43854a5 | 2016-04-27 12:26:00 -0700 | [diff] [blame] | 2028 | ASSERT(pThisNode); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2029 | CXFA_WidgetData widgetData(pThisNode); |
| 2030 | pLocale = widgetData.GetLocal(); |
| 2031 | } else { |
tsepez | afe9430 | 2016-05-13 17:21:31 -0700 | [diff] [blame] | 2032 | pLocale = pMgr->GetLocaleByName(CFX_WideString::FromUTF8(szLocale)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2033 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2034 | if (!pLocale) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2035 | return FALSE; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2036 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2037 | CFX_WideString wsFormat; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2038 | if (szFormat.IsEmpty()) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2039 | pLocale->GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY_Default, wsFormat); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2040 | else |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 2041 | wsFormat = CFX_WideString::FromUTF8(szFormat); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2042 | |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 2043 | CXFA_LocaleValue widgetValue(XFA_VT_DATE, CFX_WideString::FromUTF8(szDate), |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2044 | pMgr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2045 | CFX_WideString wsRet; |
| 2046 | widgetValue.FormatPatterns(wsRet, wsFormat, pLocale, |
| 2047 | XFA_VALUEPICTURE_Display); |
tsepez | bd9748d | 2016-04-13 21:40:19 -0700 | [diff] [blame] | 2048 | strLocalDate = FX_UTF8Encode(wsRet.c_str(), wsRet.GetLength()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2049 | return TRUE; |
| 2050 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2051 | |
| 2052 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2053 | FX_BOOL CXFA_FM2JSContext::IsoTime2Local(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2054 | const CFX_ByteStringC& szTime, |
| 2055 | const CFX_ByteStringC& szFormat, |
| 2056 | const CFX_ByteStringC& szLocale, |
| 2057 | CFX_ByteString& strLocalTime) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2058 | CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2059 | if (!pDoc) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2060 | return FALSE; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2061 | |
| 2062 | CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2063 | IFX_Locale* pLocale = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2064 | if (szLocale.IsEmpty()) { |
| 2065 | CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject()); |
dsinclair | 43854a5 | 2016-04-27 12:26:00 -0700 | [diff] [blame] | 2066 | ASSERT(pThisNode); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2067 | CXFA_WidgetData widgetData(pThisNode); |
| 2068 | pLocale = widgetData.GetLocal(); |
| 2069 | } else { |
tsepez | afe9430 | 2016-05-13 17:21:31 -0700 | [diff] [blame] | 2070 | pLocale = pMgr->GetLocaleByName(CFX_WideString::FromUTF8(szLocale)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2071 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2072 | if (!pLocale) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2073 | return FALSE; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2074 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2075 | CFX_WideString wsFormat; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2076 | if (szFormat.IsEmpty()) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2077 | pLocale->GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY_Default, wsFormat); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2078 | else |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 2079 | wsFormat = CFX_WideString::FromUTF8(szFormat); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2080 | |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 2081 | wsFormat = L"time{" + wsFormat + L"}"; |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 2082 | CXFA_LocaleValue widgetValue(XFA_VT_TIME, CFX_WideString::FromUTF8(szTime), |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2083 | pMgr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2084 | CFX_WideString wsRet; |
| 2085 | widgetValue.FormatPatterns(wsRet, wsFormat, pLocale, |
| 2086 | XFA_VALUEPICTURE_Display); |
tsepez | bd9748d | 2016-04-13 21:40:19 -0700 | [diff] [blame] | 2087 | strLocalTime = FX_UTF8Encode(wsRet.c_str(), wsRet.GetLength()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2088 | return TRUE; |
| 2089 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2090 | |
| 2091 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2092 | FX_BOOL CXFA_FM2JSContext::GetGMTTime(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2093 | const CFX_ByteStringC& szTime, |
| 2094 | const CFX_ByteStringC& szFormat, |
| 2095 | const CFX_ByteStringC& szLocale, |
| 2096 | CFX_ByteString& strGMTTime) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2097 | CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2098 | if (!pDoc) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2099 | return FALSE; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2100 | |
| 2101 | CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2102 | IFX_Locale* pLocale = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2103 | if (szLocale.IsEmpty()) { |
| 2104 | CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject()); |
dsinclair | 43854a5 | 2016-04-27 12:26:00 -0700 | [diff] [blame] | 2105 | ASSERT(pThisNode); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2106 | CXFA_WidgetData widgetData(pThisNode); |
| 2107 | pLocale = widgetData.GetLocal(); |
| 2108 | } else { |
tsepez | afe9430 | 2016-05-13 17:21:31 -0700 | [diff] [blame] | 2109 | pLocale = pMgr->GetLocaleByName(CFX_WideString::FromUTF8(szLocale)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2110 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2111 | if (!pLocale) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2112 | return FALSE; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2113 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2114 | CFX_WideString wsFormat; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2115 | if (szFormat.IsEmpty()) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2116 | pLocale->GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY_Default, wsFormat); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2117 | else |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 2118 | wsFormat = CFX_WideString::FromUTF8(szFormat); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2119 | |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 2120 | wsFormat = L"time{" + wsFormat + L"}"; |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 2121 | CXFA_LocaleValue widgetValue(XFA_VT_TIME, CFX_WideString::FromUTF8(szTime), |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2122 | pMgr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2123 | CFX_WideString wsRet; |
| 2124 | widgetValue.FormatPatterns(wsRet, wsFormat, pLocale, |
| 2125 | XFA_VALUEPICTURE_Display); |
tsepez | bd9748d | 2016-04-13 21:40:19 -0700 | [diff] [blame] | 2126 | strGMTTime = FX_UTF8Encode(wsRet.c_str(), wsRet.GetLength()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2127 | return TRUE; |
| 2128 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2129 | |
| 2130 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2131 | int32_t CXFA_FM2JSContext::DateString2Num(const CFX_ByteStringC& szDateString) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2132 | int32_t iLength = szDateString.GetLength(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2133 | int32_t iYear = 0; |
| 2134 | int32_t iMonth = 0; |
| 2135 | int32_t iDay = 0; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2136 | if (iLength <= 10) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2137 | int32_t iStyle = -1; |
| 2138 | if (!IsIsoDateFormat(szDateString.c_str(), iLength, iStyle, iYear, iMonth, |
| 2139 | iDay)) { |
| 2140 | return 0; |
| 2141 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2142 | } else { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2143 | int32_t iHour = 0; |
| 2144 | int32_t iMinute = 0; |
| 2145 | int32_t iSecond = 0; |
| 2146 | int32_t iMilliSecond = 0; |
| 2147 | int32_t iZoneHour = 0; |
| 2148 | int32_t iZoneMinute = 0; |
| 2149 | if (!IsIsoDateTimeFormat(szDateString.c_str(), iLength, iYear, iMonth, iDay, |
| 2150 | iHour, iMinute, iSecond, iMilliSecond, iZoneHour, |
| 2151 | iZoneMinute)) { |
| 2152 | return 0; |
| 2153 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2154 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2155 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2156 | FX_FLOAT dDays = 0; |
| 2157 | int32_t i = 1; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2158 | if (iYear < 1900) |
| 2159 | return 0; |
| 2160 | |
| 2161 | while (iYear - i >= 1900) { |
| 2162 | dDays += |
| 2163 | ((!((iYear - i) % 4) && ((iYear - i) % 100)) || !((iYear - i) % 400)) |
| 2164 | ? 366 |
| 2165 | : 365; |
| 2166 | ++i; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2167 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2168 | i = 1; |
| 2169 | while (i < iMonth) { |
| 2170 | if (i == 2) |
| 2171 | dDays += ((!(iYear % 4) && (iYear % 100)) || !(iYear % 400)) ? 29 : 28; |
| 2172 | else if (i <= 7) |
| 2173 | dDays += (i % 2 == 0) ? 30 : 31; |
| 2174 | else |
| 2175 | dDays += (i % 2 == 0) ? 31 : 30; |
| 2176 | |
| 2177 | ++i; |
| 2178 | } |
| 2179 | i = 0; |
| 2180 | while (iDay - i > 0) { |
| 2181 | dDays += 1; |
| 2182 | ++i; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2183 | } |
| 2184 | return (int32_t)dDays; |
| 2185 | } |
dsinclair | 0c268e9 | 2016-05-17 14:04:14 -0700 | [diff] [blame] | 2186 | |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2187 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2188 | void CXFA_FM2JSContext::GetLocalDateFormat(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2189 | int32_t iStyle, |
| 2190 | const CFX_ByteStringC& szLocalStr, |
| 2191 | CFX_ByteString& strFormat, |
| 2192 | FX_BOOL bStandard) { |
| 2193 | FX_LOCALEDATETIMESUBCATEGORY strStyle; |
| 2194 | switch (iStyle) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2195 | case 1: |
| 2196 | strStyle = FX_LOCALEDATETIMESUBCATEGORY_Short; |
| 2197 | break; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2198 | case 3: |
| 2199 | strStyle = FX_LOCALEDATETIMESUBCATEGORY_Long; |
| 2200 | break; |
| 2201 | case 4: |
| 2202 | strStyle = FX_LOCALEDATETIMESUBCATEGORY_Full; |
| 2203 | break; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2204 | case 0: |
| 2205 | case 2: |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2206 | default: |
| 2207 | strStyle = FX_LOCALEDATETIMESUBCATEGORY_Medium; |
| 2208 | break; |
| 2209 | } |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2210 | CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2211 | if (!pDoc) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2212 | return; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2213 | |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2214 | IFX_Locale* pLocale = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2215 | if (szLocalStr.IsEmpty()) { |
| 2216 | CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject()); |
dsinclair | 43854a5 | 2016-04-27 12:26:00 -0700 | [diff] [blame] | 2217 | ASSERT(pThisNode); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2218 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2219 | CXFA_WidgetData widgetData(pThisNode); |
| 2220 | pLocale = widgetData.GetLocal(); |
| 2221 | } else { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2222 | pLocale = pDoc->GetLocalMgr()->GetLocaleByName( |
| 2223 | CFX_WideString::FromUTF8(szLocalStr)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2224 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2225 | if (!pLocale) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2226 | return; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2227 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2228 | CFX_WideString strRet; |
| 2229 | pLocale->GetDatePattern(strStyle, strRet); |
| 2230 | if (!bStandard) { |
| 2231 | CFX_WideString wsSymbols; |
| 2232 | pLocale->GetDateTimeSymbols(wsSymbols); |
dsinclair | 0c268e9 | 2016-05-17 14:04:14 -0700 | [diff] [blame] | 2233 | AlternateDateTimeSymbols(strRet, wsSymbols, g_sAltTable_Date); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2234 | } |
tsepez | bd9748d | 2016-04-13 21:40:19 -0700 | [diff] [blame] | 2235 | strFormat = FX_UTF8Encode(strRet.c_str(), strRet.GetLength()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2236 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2237 | |
| 2238 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2239 | void CXFA_FM2JSContext::GetLocalTimeFormat(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2240 | int32_t iStyle, |
| 2241 | const CFX_ByteStringC& szLocalStr, |
| 2242 | CFX_ByteString& strFormat, |
| 2243 | FX_BOOL bStandard) { |
| 2244 | FX_LOCALEDATETIMESUBCATEGORY strStyle; |
| 2245 | switch (iStyle) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2246 | case 1: |
| 2247 | strStyle = FX_LOCALEDATETIMESUBCATEGORY_Short; |
| 2248 | break; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2249 | case 3: |
| 2250 | strStyle = FX_LOCALEDATETIMESUBCATEGORY_Long; |
| 2251 | break; |
| 2252 | case 4: |
| 2253 | strStyle = FX_LOCALEDATETIMESUBCATEGORY_Full; |
| 2254 | break; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2255 | case 0: |
| 2256 | case 2: |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2257 | default: |
| 2258 | strStyle = FX_LOCALEDATETIMESUBCATEGORY_Medium; |
| 2259 | break; |
| 2260 | } |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2261 | CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2262 | if (!pDoc) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2263 | return; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2264 | |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2265 | IFX_Locale* pLocale = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2266 | if (szLocalStr.IsEmpty()) { |
| 2267 | CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject()); |
dsinclair | 43854a5 | 2016-04-27 12:26:00 -0700 | [diff] [blame] | 2268 | ASSERT(pThisNode); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2269 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2270 | CXFA_WidgetData widgetData(pThisNode); |
| 2271 | pLocale = widgetData.GetLocal(); |
| 2272 | } else { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2273 | pLocale = pDoc->GetLocalMgr()->GetLocaleByName( |
| 2274 | CFX_WideString::FromUTF8(szLocalStr)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2275 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2276 | if (!pLocale) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2277 | return; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2278 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2279 | CFX_WideString strRet; |
| 2280 | pLocale->GetTimePattern(strStyle, strRet); |
| 2281 | if (!bStandard) { |
| 2282 | CFX_WideString wsSymbols; |
| 2283 | pLocale->GetDateTimeSymbols(wsSymbols); |
dsinclair | 0c268e9 | 2016-05-17 14:04:14 -0700 | [diff] [blame] | 2284 | AlternateDateTimeSymbols(strRet, wsSymbols, g_sAltTable_Time); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2285 | } |
tsepez | bd9748d | 2016-04-13 21:40:19 -0700 | [diff] [blame] | 2286 | strFormat = FX_UTF8Encode(strRet.c_str(), strRet.GetLength()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2287 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2288 | |
| 2289 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2290 | void CXFA_FM2JSContext::GetStandardDateFormat(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2291 | int32_t iStyle, |
| 2292 | const CFX_ByteStringC& szLocalStr, |
| 2293 | CFX_ByteString& strFormat) { |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2294 | GetLocalDateFormat(pThis, iStyle, szLocalStr, strFormat, TRUE); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2295 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2296 | |
| 2297 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2298 | void CXFA_FM2JSContext::GetStandardTimeFormat(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2299 | int32_t iStyle, |
| 2300 | const CFX_ByteStringC& szLocalStr, |
| 2301 | CFX_ByteString& strFormat) { |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2302 | GetLocalTimeFormat(pThis, iStyle, szLocalStr, strFormat, TRUE); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2303 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2304 | |
| 2305 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2306 | void CXFA_FM2JSContext::Num2AllTime(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2307 | int32_t iTime, |
| 2308 | const CFX_ByteStringC& szFormat, |
| 2309 | const CFX_ByteStringC& szLocale, |
| 2310 | FX_BOOL bGM, |
| 2311 | CFX_ByteString& strTime) { |
| 2312 | int32_t iHour = 0; |
| 2313 | int32_t iMin = 0; |
| 2314 | int32_t iSec = 0; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2315 | iHour = static_cast<int>(iTime) / 3600000; |
| 2316 | iMin = (static_cast<int>(iTime) - iHour * 3600000) / 60000; |
| 2317 | iSec = (static_cast<int>(iTime) - iHour * 3600000 - iMin * 60000) / 1000; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2318 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2319 | if (!bGM) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2320 | int32_t iZoneHour = 0; |
| 2321 | int32_t iZoneMin = 0; |
| 2322 | int32_t iZoneSec = 0; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2323 | GetLocalTimeZone(iZoneHour, iZoneMin, iZoneSec); |
| 2324 | iHour += iZoneHour; |
| 2325 | iMin += iZoneMin; |
| 2326 | iSec += iZoneSec; |
| 2327 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2328 | |
| 2329 | FX_BOOL iRet = FALSE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2330 | CFX_ByteString strIsoTime; |
| 2331 | strIsoTime.Format("%02d:%02d:%02d", iHour, iMin, iSec); |
| 2332 | if (bGM) { |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 2333 | iRet = |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2334 | GetGMTTime(pThis, strIsoTime.AsStringC(), szFormat, szLocale, strTime); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2335 | } else { |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2336 | iRet = IsoTime2Local(pThis, strIsoTime.AsStringC(), szFormat, szLocale, |
tsepez | 28f97ff | 2016-04-04 16:41:35 -0700 | [diff] [blame] | 2337 | strTime); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2338 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2339 | if (!iRet) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2340 | strTime = ""; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2341 | } |
| 2342 | |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2343 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2344 | void CXFA_FM2JSContext::GetLocalTimeZone(int32_t& iHour, |
| 2345 | int32_t& iMin, |
| 2346 | int32_t& iSec) { |
| 2347 | time_t now; |
| 2348 | time(&now); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2349 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2350 | struct tm* pGmt = gmtime(&now); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2351 | struct tm* pLocal = localtime(&now); |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 2352 | iHour = pLocal->tm_hour - pGmt->tm_hour; |
| 2353 | iMin = pLocal->tm_min - pGmt->tm_min; |
| 2354 | iSec = pLocal->tm_sec - pGmt->tm_sec; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2355 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2356 | |
| 2357 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2358 | void CXFA_FM2JSContext::Apr(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2359 | const CFX_ByteStringC& szFuncName, |
| 2360 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2361 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2362 | if (args.GetLength() != 3) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 2363 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Apr"); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2364 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2365 | } |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2366 | |
| 2367 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 2368 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 2369 | std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); |
| 2370 | if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || |
| 2371 | ValueIsNull(pThis, argThree.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2372 | args.GetReturnValue()->SetNull(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2373 | return; |
| 2374 | } |
| 2375 | |
| 2376 | FX_DOUBLE nPrincipal = ValueToDouble(pThis, argOne.get()); |
| 2377 | FX_DOUBLE nPayment = ValueToDouble(pThis, argTwo.get()); |
| 2378 | FX_DOUBLE nPeriods = ValueToDouble(pThis, argThree.get()); |
| 2379 | if (nPrincipal <= 0 || nPayment <= 0 || nPeriods <= 0) { |
| 2380 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
| 2381 | return; |
| 2382 | } |
| 2383 | |
| 2384 | FX_DOUBLE r = |
| 2385 | 2 * (nPeriods * nPayment - nPrincipal) / (nPeriods * nPrincipal); |
| 2386 | FX_DOUBLE nTemp = 1; |
| 2387 | for (int32_t i = 0; i < nPeriods; ++i) |
| 2388 | nTemp *= (1 + r); |
| 2389 | |
| 2390 | FX_DOUBLE nRet = r * nTemp / (nTemp - 1) - nPayment / nPrincipal; |
| 2391 | while (fabs(nRet) > kFinancialPrecision) { |
| 2392 | FX_DOUBLE nDerivative = |
| 2393 | ((nTemp + r * nPeriods * (nTemp / (1 + r))) * (nTemp - 1) - |
| 2394 | (r * nTemp * nPeriods * (nTemp / (1 + r)))) / |
| 2395 | ((nTemp - 1) * (nTemp - 1)); |
| 2396 | if (nDerivative == 0) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2397 | args.GetReturnValue()->SetNull(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2398 | return; |
| 2399 | } |
| 2400 | |
| 2401 | r = r - nRet / nDerivative; |
| 2402 | nTemp = 1; |
| 2403 | for (int32_t i = 0; i < nPeriods; ++i) { |
| 2404 | nTemp *= (1 + r); |
| 2405 | } |
| 2406 | nRet = r * nTemp / (nTemp - 1) - nPayment / nPrincipal; |
| 2407 | } |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2408 | args.GetReturnValue()->SetDouble(r * 12); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2409 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2410 | |
| 2411 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2412 | void CXFA_FM2JSContext::CTerm(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2413 | const CFX_ByteStringC& szFuncName, |
| 2414 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2415 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2416 | if (args.GetLength() != 3) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 2417 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"CTerm"); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2418 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2419 | } |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2420 | |
| 2421 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 2422 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 2423 | std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); |
| 2424 | if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || |
| 2425 | ValueIsNull(pThis, argThree.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2426 | args.GetReturnValue()->SetNull(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2427 | return; |
| 2428 | } |
| 2429 | |
| 2430 | FX_FLOAT nRate = ValueToFloat(pThis, argOne.get()); |
| 2431 | FX_FLOAT nFutureValue = ValueToFloat(pThis, argTwo.get()); |
| 2432 | FX_FLOAT nInitAmount = ValueToFloat(pThis, argThree.get()); |
| 2433 | if ((nRate <= 0) || (nFutureValue <= 0) || (nInitAmount <= 0)) { |
| 2434 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
| 2435 | return; |
| 2436 | } |
| 2437 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2438 | args.GetReturnValue()->SetFloat( |
| 2439 | FXSYS_log((FX_FLOAT)(nFutureValue / nInitAmount)) / |
| 2440 | FXSYS_log((FX_FLOAT)(1 + nRate))); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2441 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2442 | |
| 2443 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2444 | void CXFA_FM2JSContext::FV(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2445 | const CFX_ByteStringC& szFuncName, |
| 2446 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2447 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2448 | if (args.GetLength() != 3) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 2449 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"FV"); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2450 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2451 | } |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2452 | |
| 2453 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 2454 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 2455 | std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); |
| 2456 | if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || |
| 2457 | ValueIsNull(pThis, argThree.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2458 | args.GetReturnValue()->SetNull(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2459 | return; |
| 2460 | } |
| 2461 | |
| 2462 | FX_DOUBLE nAmount = ValueToDouble(pThis, argOne.get()); |
| 2463 | FX_DOUBLE nRate = ValueToDouble(pThis, argTwo.get()); |
| 2464 | FX_DOUBLE nPeriod = ValueToDouble(pThis, argThree.get()); |
| 2465 | if ((nRate < 0) || (nPeriod <= 0) || (nAmount <= 0)) { |
| 2466 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
| 2467 | return; |
| 2468 | } |
| 2469 | |
| 2470 | FX_DOUBLE dResult = 0; |
| 2471 | if (nRate) { |
| 2472 | FX_DOUBLE nTemp = 1; |
| 2473 | for (int i = 0; i < nPeriod; ++i) { |
| 2474 | nTemp *= 1 + nRate; |
| 2475 | } |
| 2476 | dResult = nAmount * (nTemp - 1) / nRate; |
| 2477 | } else { |
| 2478 | dResult = nAmount * nPeriod; |
| 2479 | } |
| 2480 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2481 | args.GetReturnValue()->SetDouble(dResult); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2482 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2483 | |
| 2484 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2485 | void CXFA_FM2JSContext::IPmt(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2486 | const CFX_ByteStringC& szFuncName, |
| 2487 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2488 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2489 | if (args.GetLength() != 5) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 2490 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"IPmt"); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2491 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2492 | } |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2493 | |
| 2494 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 2495 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 2496 | std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); |
| 2497 | std::unique_ptr<CFXJSE_Value> argFour = GetSimpleValue(pThis, args, 3); |
| 2498 | std::unique_ptr<CFXJSE_Value> argFive = GetSimpleValue(pThis, args, 4); |
| 2499 | if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || |
| 2500 | ValueIsNull(pThis, argThree.get()) || ValueIsNull(pThis, argFour.get()) || |
| 2501 | ValueIsNull(pThis, argFive.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2502 | args.GetReturnValue()->SetNull(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2503 | return; |
| 2504 | } |
| 2505 | |
| 2506 | FX_FLOAT nPrincipalAmount = ValueToFloat(pThis, argOne.get()); |
| 2507 | FX_FLOAT nRate = ValueToFloat(pThis, argTwo.get()); |
| 2508 | FX_FLOAT nPayment = ValueToFloat(pThis, argThree.get()); |
| 2509 | FX_FLOAT nFirstMonth = ValueToFloat(pThis, argFour.get()); |
| 2510 | FX_FLOAT nNumberOfMonths = ValueToFloat(pThis, argFive.get()); |
| 2511 | if ((nPrincipalAmount <= 0) || (nRate <= 0) || (nPayment <= 0) || |
| 2512 | (nFirstMonth < 0) || (nNumberOfMonths < 0)) { |
| 2513 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
| 2514 | return; |
| 2515 | } |
| 2516 | |
| 2517 | FX_FLOAT nRateOfMonth = nRate / 12; |
| 2518 | int32_t iNums = (int32_t)( |
| 2519 | (FXSYS_log10((FX_FLOAT)(nPayment / nPrincipalAmount)) - |
| 2520 | FXSYS_log10((FX_FLOAT)(nPayment / nPrincipalAmount - nRateOfMonth))) / |
| 2521 | FXSYS_log10((FX_FLOAT)(1 + nRateOfMonth))); |
| 2522 | int32_t iEnd = std::min((int32_t)(nFirstMonth + nNumberOfMonths - 1), iNums); |
| 2523 | |
| 2524 | if (nPayment < nPrincipalAmount * nRateOfMonth) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2525 | args.GetReturnValue()->SetFloat(0); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2526 | return; |
| 2527 | } |
| 2528 | |
| 2529 | int32_t i = 0; |
| 2530 | for (i = 0; i < nFirstMonth - 1; ++i) |
| 2531 | nPrincipalAmount -= nPayment - nPrincipalAmount * nRateOfMonth; |
| 2532 | |
| 2533 | FX_FLOAT nSum = 0; |
| 2534 | for (; i < iEnd; ++i) { |
| 2535 | nSum += nPrincipalAmount * nRateOfMonth; |
| 2536 | nPrincipalAmount -= nPayment - nPrincipalAmount * nRateOfMonth; |
| 2537 | } |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2538 | args.GetReturnValue()->SetFloat(nSum); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2539 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2540 | |
| 2541 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2542 | void CXFA_FM2JSContext::NPV(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2543 | const CFX_ByteStringC& szFuncName, |
| 2544 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2545 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2546 | int32_t argc = args.GetLength(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2547 | if (argc < 3) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 2548 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"NPV"); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2549 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2550 | } |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2551 | |
| 2552 | std::vector<std::unique_ptr<CFXJSE_Value>> argValues; |
| 2553 | for (int32_t i = 0; i < argc; i++) { |
| 2554 | argValues.push_back(GetSimpleValue(pThis, args, i)); |
| 2555 | if (ValueIsNull(pThis, argValues[i].get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2556 | args.GetReturnValue()->SetNull(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2557 | return; |
| 2558 | } |
| 2559 | } |
| 2560 | |
| 2561 | FX_DOUBLE nRate = ValueToDouble(pThis, argValues[0].get()); |
| 2562 | if (nRate <= 0) { |
| 2563 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
| 2564 | return; |
| 2565 | } |
| 2566 | |
| 2567 | std::vector<FX_DOUBLE> data(argc - 1); |
| 2568 | for (int32_t i = 1; i < argc; i++) |
| 2569 | data.push_back(ValueToDouble(pThis, argValues[i].get())); |
| 2570 | |
| 2571 | FX_DOUBLE nSum = 0; |
| 2572 | int32_t iIndex = 0; |
| 2573 | for (int32_t i = 0; i < argc - 1; i++) { |
| 2574 | FX_DOUBLE nTemp = 1; |
| 2575 | for (int32_t j = 0; j <= i; j++) |
| 2576 | nTemp *= 1 + nRate; |
| 2577 | |
| 2578 | FX_DOUBLE nNum = data[iIndex++]; |
| 2579 | nSum += nNum / nTemp; |
| 2580 | } |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2581 | args.GetReturnValue()->SetDouble(nSum); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2582 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2583 | |
| 2584 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2585 | void CXFA_FM2JSContext::Pmt(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2586 | const CFX_ByteStringC& szFuncName, |
| 2587 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2588 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2589 | if (args.GetLength() != 3) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 2590 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Pmt"); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2591 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2592 | } |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2593 | |
| 2594 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 2595 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 2596 | std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); |
| 2597 | if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || |
| 2598 | ValueIsNull(pThis, argThree.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2599 | args.GetReturnValue()->SetNull(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2600 | return; |
| 2601 | } |
| 2602 | |
| 2603 | FX_FLOAT nPrincipal = ValueToFloat(pThis, argOne.get()); |
| 2604 | FX_FLOAT nRate = ValueToFloat(pThis, argTwo.get()); |
| 2605 | FX_FLOAT nPeriods = ValueToFloat(pThis, argThree.get()); |
| 2606 | if ((nPrincipal <= 0) || (nRate <= 0) || (nPeriods <= 0)) { |
| 2607 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
| 2608 | return; |
| 2609 | } |
| 2610 | |
| 2611 | FX_FLOAT nTmp = 1 + nRate; |
| 2612 | FX_FLOAT nSum = nTmp; |
| 2613 | for (int32_t i = 0; i < nPeriods - 1; ++i) |
| 2614 | nSum *= nTmp; |
| 2615 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2616 | args.GetReturnValue()->SetFloat((nPrincipal * nRate * nSum) / (nSum - 1)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2617 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2618 | |
| 2619 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2620 | void CXFA_FM2JSContext::PPmt(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2621 | const CFX_ByteStringC& szFuncName, |
| 2622 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2623 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2624 | if (args.GetLength() != 5) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 2625 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"PPmt"); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2626 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2627 | } |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2628 | |
| 2629 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 2630 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 2631 | std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); |
| 2632 | std::unique_ptr<CFXJSE_Value> argFour = GetSimpleValue(pThis, args, 3); |
| 2633 | std::unique_ptr<CFXJSE_Value> argFive = GetSimpleValue(pThis, args, 4); |
| 2634 | if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || |
| 2635 | ValueIsNull(pThis, argThree.get()) || ValueIsNull(pThis, argFour.get()) || |
| 2636 | ValueIsNull(pThis, argFive.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2637 | args.GetReturnValue()->SetNull(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2638 | return; |
| 2639 | } |
| 2640 | |
| 2641 | FX_FLOAT nPrincipalAmount = ValueToFloat(pThis, argOne.get()); |
| 2642 | FX_FLOAT nRate = ValueToFloat(pThis, argTwo.get()); |
| 2643 | FX_FLOAT nPayment = ValueToFloat(pThis, argThree.get()); |
| 2644 | FX_FLOAT nFirstMonth = ValueToFloat(pThis, argFour.get()); |
| 2645 | FX_FLOAT nNumberOfMonths = ValueToFloat(pThis, argFive.get()); |
| 2646 | if ((nPrincipalAmount <= 0) || (nRate <= 0) || (nPayment <= 0) || |
| 2647 | (nFirstMonth < 0) || (nNumberOfMonths < 0)) { |
| 2648 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
| 2649 | return; |
| 2650 | } |
| 2651 | |
| 2652 | FX_FLOAT nRateOfMonth = nRate / 12; |
| 2653 | int32_t iNums = (int32_t)( |
| 2654 | (FXSYS_log10((FX_FLOAT)(nPayment / nPrincipalAmount)) - |
| 2655 | FXSYS_log10((FX_FLOAT)(nPayment / nPrincipalAmount - nRateOfMonth))) / |
| 2656 | FXSYS_log10((FX_FLOAT)(1 + nRateOfMonth))); |
| 2657 | int32_t iEnd = std::min((int32_t)(nFirstMonth + nNumberOfMonths - 1), iNums); |
| 2658 | if (nPayment < nPrincipalAmount * nRateOfMonth) { |
| 2659 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
| 2660 | return; |
| 2661 | } |
| 2662 | |
| 2663 | int32_t i = 0; |
| 2664 | for (i = 0; i < nFirstMonth - 1; ++i) |
| 2665 | nPrincipalAmount -= nPayment - nPrincipalAmount * nRateOfMonth; |
| 2666 | |
| 2667 | FX_FLOAT nTemp = 0; |
| 2668 | FX_FLOAT nSum = 0; |
| 2669 | for (; i < iEnd; ++i) { |
| 2670 | nTemp = nPayment - nPrincipalAmount * nRateOfMonth; |
| 2671 | nSum += nTemp; |
| 2672 | nPrincipalAmount -= nTemp; |
| 2673 | } |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2674 | args.GetReturnValue()->SetFloat(nSum); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2675 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2676 | |
| 2677 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2678 | void CXFA_FM2JSContext::PV(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2679 | const CFX_ByteStringC& szFuncName, |
| 2680 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2681 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2682 | if (args.GetLength() != 3) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 2683 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"PV"); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2684 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2685 | } |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2686 | |
| 2687 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 2688 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 2689 | std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); |
| 2690 | if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || |
| 2691 | ValueIsNull(pThis, argThree.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2692 | args.GetReturnValue()->SetNull(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2693 | return; |
| 2694 | } |
| 2695 | |
| 2696 | FX_DOUBLE nAmount = ValueToDouble(pThis, argOne.get()); |
| 2697 | FX_DOUBLE nRate = ValueToDouble(pThis, argTwo.get()); |
| 2698 | FX_DOUBLE nPeriod = ValueToDouble(pThis, argThree.get()); |
| 2699 | if ((nAmount <= 0) || (nRate < 0) || (nPeriod <= 0)) { |
| 2700 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
| 2701 | return; |
| 2702 | } |
| 2703 | |
| 2704 | FX_DOUBLE nTemp = 1; |
| 2705 | for (int32_t i = 0; i < nPeriod; ++i) |
| 2706 | nTemp *= 1 + nRate; |
| 2707 | |
| 2708 | nTemp = 1 / nTemp; |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2709 | args.GetReturnValue()->SetDouble(nAmount * ((1 - nTemp) / nRate)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2710 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2711 | |
| 2712 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2713 | void CXFA_FM2JSContext::Rate(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2714 | const CFX_ByteStringC& szFuncName, |
| 2715 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2716 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2717 | if (args.GetLength() != 3) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 2718 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Rate"); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2719 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2720 | } |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2721 | |
| 2722 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 2723 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 2724 | std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); |
| 2725 | if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || |
| 2726 | ValueIsNull(pThis, argThree.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2727 | args.GetReturnValue()->SetNull(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2728 | return; |
| 2729 | } |
| 2730 | |
| 2731 | FX_FLOAT nFuture = ValueToFloat(pThis, argOne.get()); |
| 2732 | FX_FLOAT nPresent = ValueToFloat(pThis, argTwo.get()); |
| 2733 | FX_FLOAT nTotalNumber = ValueToFloat(pThis, argThree.get()); |
| 2734 | if ((nFuture <= 0) || (nPresent < 0) || (nTotalNumber <= 0)) { |
| 2735 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
| 2736 | return; |
| 2737 | } |
| 2738 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2739 | args.GetReturnValue()->SetFloat( |
| 2740 | FXSYS_pow((FX_FLOAT)(nFuture / nPresent), (FX_FLOAT)(1 / nTotalNumber)) - |
| 2741 | 1); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2742 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2743 | |
| 2744 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2745 | void CXFA_FM2JSContext::Term(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2746 | const CFX_ByteStringC& szFuncName, |
| 2747 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2748 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2749 | if (args.GetLength() != 3) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 2750 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Term"); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2751 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2752 | } |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2753 | |
| 2754 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 2755 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 2756 | std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); |
| 2757 | if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || |
| 2758 | ValueIsNull(pThis, argThree.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2759 | args.GetReturnValue()->SetNull(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2760 | return; |
| 2761 | } |
| 2762 | |
| 2763 | FX_FLOAT nMount = ValueToFloat(pThis, argOne.get()); |
| 2764 | FX_FLOAT nRate = ValueToFloat(pThis, argTwo.get()); |
| 2765 | FX_FLOAT nFuture = ValueToFloat(pThis, argThree.get()); |
| 2766 | if ((nMount <= 0) || (nRate <= 0) || (nFuture <= 0)) { |
| 2767 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
| 2768 | return; |
| 2769 | } |
| 2770 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2771 | args.GetReturnValue()->SetFloat( |
| 2772 | FXSYS_log((FX_FLOAT)(nFuture / nMount * nRate) + 1) / |
| 2773 | FXSYS_log((FX_FLOAT)(1 + nRate))); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2774 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2775 | |
| 2776 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2777 | void CXFA_FM2JSContext::Choose(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2778 | const CFX_ByteStringC& szFuncName, |
| 2779 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2780 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2781 | int32_t argc = args.GetLength(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2782 | if (argc < 2) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 2783 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Choose"); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2784 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2785 | } |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2786 | |
| 2787 | std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); |
| 2788 | if (ValueIsNull(pThis, argOne.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2789 | args.GetReturnValue()->SetNull(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2790 | return; |
| 2791 | } |
| 2792 | |
| 2793 | int32_t iIndex = (int32_t)ValueToFloat(pThis, argOne.get()); |
| 2794 | if (iIndex < 1) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2795 | args.GetReturnValue()->SetString(""); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2796 | return; |
| 2797 | } |
| 2798 | |
| 2799 | FX_BOOL bFound = FALSE; |
| 2800 | FX_BOOL bStopCounterFlags = FALSE; |
| 2801 | int32_t iArgIndex = 1; |
| 2802 | int32_t iValueIndex = 0; |
| 2803 | v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
| 2804 | while (!bFound && !bStopCounterFlags && (iArgIndex < argc)) { |
| 2805 | std::unique_ptr<CFXJSE_Value> argIndexValue = args.GetValue(iArgIndex); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 2806 | if (argIndexValue->IsArray()) { |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2807 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2808 | argIndexValue->GetObjectProperty("length", lengthValue.get()); |
| 2809 | int32_t iLength = lengthValue->ToInteger(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2810 | if (iLength > 3) |
| 2811 | bStopCounterFlags = TRUE; |
| 2812 | |
| 2813 | iValueIndex += (iLength - 2); |
| 2814 | if (iValueIndex >= iIndex) { |
| 2815 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 2816 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 2817 | std::unique_ptr<CFXJSE_Value> newPropertyValue( |
| 2818 | new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2819 | argIndexValue->GetObjectPropertyByIdx(1, propertyValue.get()); |
| 2820 | argIndexValue->GetObjectPropertyByIdx( |
| 2821 | (iLength - 1) - (iValueIndex - iIndex), jsObjectValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 2822 | if (propertyValue->IsNull()) { |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2823 | GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); |
| 2824 | } else { |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 2825 | jsObjectValue->GetObjectProperty( |
| 2826 | propertyValue->ToString().AsStringC(), newPropertyValue.get()); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2827 | } |
| 2828 | CFX_ByteString bsChoosed; |
| 2829 | ValueToUTF8String(newPropertyValue.get(), bsChoosed); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2830 | args.GetReturnValue()->SetString(bsChoosed.AsStringC()); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2831 | bFound = TRUE; |
| 2832 | } |
| 2833 | } else { |
| 2834 | iValueIndex++; |
| 2835 | if (iValueIndex == iIndex) { |
| 2836 | CFX_ByteString bsChoosed; |
| 2837 | ValueToUTF8String(argIndexValue.get(), bsChoosed); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2838 | args.GetReturnValue()->SetString(bsChoosed.AsStringC()); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2839 | bFound = TRUE; |
| 2840 | } |
| 2841 | } |
| 2842 | iArgIndex++; |
| 2843 | } |
| 2844 | if (!bFound) |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2845 | args.GetReturnValue()->SetString(""); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2846 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2847 | |
| 2848 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2849 | void CXFA_FM2JSContext::Exists(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2850 | const CFX_ByteStringC& szFuncName, |
| 2851 | CFXJSE_Arguments& args) { |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2852 | if (args.GetLength() != 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2853 | ToJSContext(pThis, nullptr) |
| 2854 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Exists"); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2855 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2856 | } |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 2857 | args.GetReturnValue()->SetInteger(args.GetValue(0)->IsObject()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2858 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2859 | |
| 2860 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2861 | void CXFA_FM2JSContext::HasValue(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2862 | const CFX_ByteStringC& szFuncName, |
| 2863 | CFXJSE_Arguments& args) { |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2864 | if (args.GetLength() != 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2865 | ToJSContext(pThis, nullptr) |
| 2866 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"HasValue"); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2867 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2868 | } |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2869 | |
| 2870 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 2871 | if (!argOne->IsString()) { |
| 2872 | args.GetReturnValue()->SetInteger(argOne->IsNumber() || |
| 2873 | argOne->IsBoolean()); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2874 | return; |
| 2875 | } |
| 2876 | |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 2877 | CFX_ByteString valueStr = argOne->ToString(); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2878 | valueStr.TrimLeft(); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2879 | args.GetReturnValue()->SetInteger(!valueStr.IsEmpty()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2880 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2881 | |
| 2882 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2883 | void CXFA_FM2JSContext::Oneof(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2884 | const CFX_ByteStringC& szFuncName, |
| 2885 | CFXJSE_Arguments& args) { |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2886 | if (args.GetLength() < 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2887 | ToJSContext(pThis, nullptr) |
| 2888 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Oneof"); |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2889 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2890 | } |
dsinclair | 96a05f4 | 2016-06-07 06:48:02 -0700 | [diff] [blame] | 2891 | |
| 2892 | FX_BOOL bFlags = FALSE; |
| 2893 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 2894 | CFXJSE_Value** parametersValue = nullptr; |
| 2895 | int32_t iCount = 0; |
| 2896 | unfoldArgs(pThis, args, parametersValue, iCount, 1); |
| 2897 | for (int32_t i = 0; i < iCount; i++) { |
| 2898 | if (simpleValueCompare(pThis, argOne.get(), parametersValue[i])) { |
| 2899 | bFlags = TRUE; |
| 2900 | break; |
| 2901 | } |
| 2902 | } |
| 2903 | for (int32_t i = 0; i < iCount; i++) |
| 2904 | delete parametersValue[i]; |
| 2905 | FX_Free(parametersValue); |
| 2906 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2907 | args.GetReturnValue()->SetInteger(bFlags); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2908 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2909 | |
| 2910 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2911 | void CXFA_FM2JSContext::Within(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2912 | const CFX_ByteStringC& szFuncName, |
| 2913 | CFXJSE_Arguments& args) { |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2914 | if (args.GetLength() != 3) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2915 | ToJSContext(pThis, nullptr) |
| 2916 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Within"); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2917 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2918 | } |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2919 | |
| 2920 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 2921 | if (argOne->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2922 | args.GetReturnValue()->SetUndefined(); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2923 | return; |
| 2924 | } |
| 2925 | |
| 2926 | std::unique_ptr<CFXJSE_Value> argLow = GetSimpleValue(pThis, args, 1); |
| 2927 | std::unique_ptr<CFXJSE_Value> argHigh = GetSimpleValue(pThis, args, 2); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 2928 | if (argOne->IsNumber()) { |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2929 | FX_FLOAT oneNumber = ValueToFloat(pThis, argOne.get()); |
| 2930 | FX_FLOAT lowNumber = ValueToFloat(pThis, argLow.get()); |
| 2931 | FX_FLOAT heightNumber = ValueToFloat(pThis, argHigh.get()); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2932 | args.GetReturnValue()->SetInteger((oneNumber >= lowNumber) && |
| 2933 | (oneNumber <= heightNumber)); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2934 | return; |
| 2935 | } |
| 2936 | |
| 2937 | CFX_ByteString oneString; |
| 2938 | CFX_ByteString lowString; |
| 2939 | CFX_ByteString heightString; |
| 2940 | ValueToUTF8String(argOne.get(), oneString); |
| 2941 | ValueToUTF8String(argLow.get(), lowString); |
| 2942 | ValueToUTF8String(argHigh.get(), heightString); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2943 | args.GetReturnValue()->SetInteger( |
| 2944 | (oneString.Compare(lowString.AsStringC()) >= 0) && |
| 2945 | (oneString.Compare(heightString.AsStringC()) <= 0)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2946 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2947 | |
| 2948 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2949 | void CXFA_FM2JSContext::If(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2950 | const CFX_ByteStringC& szFuncName, |
| 2951 | CFXJSE_Arguments& args) { |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2952 | if (args.GetLength() != 3) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2953 | ToJSContext(pThis, nullptr) |
| 2954 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"If"); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2955 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2956 | } |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2957 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2958 | args.GetReturnValue()->Assign(GetSimpleValue(pThis, args, 0)->ToBoolean() |
| 2959 | ? GetSimpleValue(pThis, args, 1).get() |
| 2960 | : GetSimpleValue(pThis, args, 2).get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2961 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2962 | |
| 2963 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 2964 | void CXFA_FM2JSContext::Eval(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2965 | const CFX_ByteStringC& szFuncName, |
| 2966 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 2967 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2968 | if (args.GetLength() != 1) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 2969 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Eval"); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2970 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2971 | } |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2972 | |
| 2973 | v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
| 2974 | std::unique_ptr<CFXJSE_Value> scriptValue = GetSimpleValue(pThis, args, 0); |
| 2975 | CFX_ByteString utf8ScriptString; |
| 2976 | ValueToUTF8String(scriptValue.get(), utf8ScriptString); |
| 2977 | if (utf8ScriptString.IsEmpty()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2978 | args.GetReturnValue()->SetNull(); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2979 | return; |
| 2980 | } |
| 2981 | |
| 2982 | CFX_WideTextBuf wsJavaScriptBuf; |
| 2983 | CFX_WideString wsError; |
| 2984 | CXFA_FM2JSContext::Translate( |
| 2985 | CFX_WideString::FromUTF8(utf8ScriptString.AsStringC()).AsStringC(), |
| 2986 | wsJavaScriptBuf, wsError); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 2987 | std::unique_ptr<CFXJSE_Context> pNewContext( |
| 2988 | CFXJSE_Context::Create(pIsolate, nullptr, nullptr)); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2989 | |
| 2990 | std::unique_ptr<CFXJSE_Value> returnValue(new CFXJSE_Value(pIsolate)); |
| 2991 | CFX_WideString javaScript(wsJavaScriptBuf.AsStringC()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 2992 | pNewContext->ExecuteScript( |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 2993 | FX_UTF8Encode(javaScript.c_str(), javaScript.GetLength()).c_str(), |
| 2994 | returnValue.get()); |
| 2995 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 2996 | args.GetReturnValue()->Assign(returnValue.get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 2997 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 2998 | |
| 2999 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 3000 | void CXFA_FM2JSContext::Ref(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3001 | const CFX_ByteStringC& szFuncName, |
| 3002 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 3003 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | ec3da5b | 2016-05-25 16:42:05 -0700 | [diff] [blame] | 3004 | v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3005 | if (args.GetLength() != 1) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 3006 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Ref"); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3007 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3008 | } |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3009 | |
| 3010 | std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 3011 | if (!argOne->IsArray() && !argOne->IsObject() && !argOne->IsBoolean() && |
| 3012 | !argOne->IsString() && !argOne->IsNull() && !argOne->IsNumber()) { |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3013 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
| 3014 | return; |
| 3015 | } |
| 3016 | |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 3017 | if (argOne->IsBoolean() || argOne->IsString() || argOne->IsNumber()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3018 | args.GetReturnValue()->Assign(argOne.get()); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3019 | return; |
| 3020 | } |
| 3021 | |
| 3022 | CFXJSE_Value* rgValues[3]; |
| 3023 | for (int32_t i = 0; i < 3; i++) |
| 3024 | rgValues[i] = new CFXJSE_Value(pIsolate); |
| 3025 | |
| 3026 | int intVal = 3; |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 3027 | if (argOne->IsNull()) { |
| 3028 | // TODO(dsinclair): Why is this 4 when the others are all 3? |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3029 | intVal = 4; |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3030 | rgValues[2]->SetNull(); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 3031 | } else if (argOne->IsArray()) { |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3032 | #ifndef NDEBUG |
| 3033 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3034 | argOne->GetObjectProperty("length", lengthValue.get()); |
| 3035 | ASSERT(lengthValue->ToInteger() >= 3); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3036 | #endif |
| 3037 | |
| 3038 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 3039 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3040 | argOne->GetObjectPropertyByIdx(1, propertyValue.get()); |
| 3041 | argOne->GetObjectPropertyByIdx(2, jsObjectValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 3042 | if (!propertyValue->IsNull() || jsObjectValue->IsNull()) { |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3043 | for (int32_t i = 0; i < 3; i++) |
| 3044 | delete rgValues[i]; |
| 3045 | |
| 3046 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
| 3047 | return; |
| 3048 | } |
| 3049 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3050 | rgValues[2]->Assign(jsObjectValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 3051 | } else if (argOne->IsObject()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3052 | rgValues[2]->Assign(argOne.get()); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3053 | } |
| 3054 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3055 | rgValues[0]->SetInteger(intVal); |
| 3056 | rgValues[1]->SetNull(); |
| 3057 | args.GetReturnValue()->SetArray(3, rgValues); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3058 | |
| 3059 | for (int32_t i = 0; i < 3; i++) |
| 3060 | delete rgValues[i]; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3061 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3062 | |
| 3063 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 3064 | void CXFA_FM2JSContext::UnitType(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3065 | const CFX_ByteStringC& szFuncName, |
| 3066 | CFXJSE_Arguments& args) { |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3067 | if (args.GetLength() != 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 3068 | ToJSContext(pThis, nullptr) |
| 3069 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"UnitType"); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3070 | return; |
| 3071 | } |
| 3072 | |
| 3073 | std::unique_ptr<CFXJSE_Value> unitspanValue = GetSimpleValue(pThis, args, 0); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 3074 | if (unitspanValue->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3075 | args.GetReturnValue()->SetNull(); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3076 | return; |
| 3077 | } |
| 3078 | |
| 3079 | CFX_ByteString unitspanString; |
| 3080 | ValueToUTF8String(unitspanValue.get(), unitspanString); |
| 3081 | if (unitspanString.IsEmpty()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3082 | args.GetReturnValue()->SetString("in"); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3083 | return; |
| 3084 | } |
| 3085 | |
| 3086 | enum XFA_FM2JS_VALUETYPE_ParserStatus { |
| 3087 | VALUETYPE_START, |
| 3088 | VALUETYPE_HAVEINVALIDCHAR, |
| 3089 | VALUETYPE_HAVEDIGIT, |
| 3090 | VALUETYPE_HAVEDIGITWHITE, |
| 3091 | VALUETYPE_ISCM, |
| 3092 | VALUETYPE_ISMM, |
| 3093 | VALUETYPE_ISPT, |
| 3094 | VALUETYPE_ISMP, |
| 3095 | VALUETYPE_ISIN, |
| 3096 | }; |
| 3097 | unitspanString.MakeLower(); |
| 3098 | CFX_WideString wsTypeString = |
| 3099 | CFX_WideString::FromUTF8(unitspanString.AsStringC()); |
| 3100 | const FX_WCHAR* pData = wsTypeString.c_str(); |
| 3101 | int32_t u = 0; |
| 3102 | int32_t uLen = wsTypeString.GetLength(); |
| 3103 | while (IsWhitespace(pData[u])) |
| 3104 | u++; |
| 3105 | |
| 3106 | XFA_FM2JS_VALUETYPE_ParserStatus eParserStatus = VALUETYPE_START; |
| 3107 | FX_WCHAR typeChar; |
| 3108 | // TODO(dsinclair): Cleanup this parser, figure out what the various checks |
| 3109 | // are for. |
| 3110 | while (u < uLen) { |
| 3111 | typeChar = pData[u]; |
| 3112 | if (IsWhitespace(typeChar)) { |
| 3113 | if (eParserStatus != VALUETYPE_HAVEDIGIT && |
| 3114 | eParserStatus != VALUETYPE_HAVEDIGITWHITE) { |
| 3115 | eParserStatus = VALUETYPE_ISIN; |
| 3116 | break; |
| 3117 | } |
| 3118 | eParserStatus = VALUETYPE_HAVEDIGITWHITE; |
| 3119 | } else if ((typeChar >= '0' && typeChar <= '9') || typeChar == '-' || |
| 3120 | typeChar == '.') { |
| 3121 | if (eParserStatus == VALUETYPE_HAVEDIGITWHITE) { |
| 3122 | eParserStatus = VALUETYPE_ISIN; |
| 3123 | break; |
| 3124 | } |
| 3125 | eParserStatus = VALUETYPE_HAVEDIGIT; |
| 3126 | } else if ((typeChar == 'c' || typeChar == 'p') && (u + 1 < uLen)) { |
| 3127 | FX_WCHAR nextChar = pData[u + 1]; |
| 3128 | if ((eParserStatus == VALUETYPE_START || |
| 3129 | eParserStatus == VALUETYPE_HAVEDIGIT || |
| 3130 | eParserStatus == VALUETYPE_HAVEDIGITWHITE) && |
| 3131 | (nextChar > '9' || nextChar < '0') && nextChar != '.' && |
| 3132 | nextChar != '-') { |
| 3133 | eParserStatus = (typeChar == 'c') ? VALUETYPE_ISCM : VALUETYPE_ISPT; |
| 3134 | break; |
| 3135 | } |
| 3136 | eParserStatus = VALUETYPE_HAVEINVALIDCHAR; |
| 3137 | } else if (typeChar == 'm' && (u + 1 < uLen)) { |
| 3138 | FX_WCHAR nextChar = pData[u + 1]; |
| 3139 | if ((eParserStatus == VALUETYPE_START || |
| 3140 | eParserStatus == VALUETYPE_HAVEDIGIT || |
| 3141 | eParserStatus == VALUETYPE_HAVEDIGITWHITE) && |
| 3142 | (nextChar > '9' || nextChar < '0') && nextChar != '.' && |
| 3143 | nextChar != '-') { |
| 3144 | eParserStatus = VALUETYPE_ISMM; |
| 3145 | if (nextChar == 'p' || ((u + 5 < uLen) && pData[u + 1] == 'i' && |
| 3146 | pData[u + 2] == 'l' && pData[u + 3] == 'l' && |
| 3147 | pData[u + 4] == 'i' && pData[u + 5] == 'p')) { |
| 3148 | eParserStatus = VALUETYPE_ISMP; |
| 3149 | } |
| 3150 | break; |
| 3151 | } |
| 3152 | } else { |
| 3153 | eParserStatus = VALUETYPE_HAVEINVALIDCHAR; |
| 3154 | } |
| 3155 | u++; |
| 3156 | } |
| 3157 | switch (eParserStatus) { |
| 3158 | case VALUETYPE_ISCM: |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3159 | args.GetReturnValue()->SetString("cm"); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3160 | break; |
| 3161 | case VALUETYPE_ISMM: |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3162 | args.GetReturnValue()->SetString("mm"); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3163 | break; |
| 3164 | case VALUETYPE_ISPT: |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3165 | args.GetReturnValue()->SetString("pt"); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3166 | break; |
| 3167 | case VALUETYPE_ISMP: |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3168 | args.GetReturnValue()->SetString("mp"); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3169 | break; |
| 3170 | default: |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3171 | args.GetReturnValue()->SetString("in"); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3172 | break; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3173 | } |
| 3174 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3175 | |
| 3176 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 3177 | void CXFA_FM2JSContext::UnitValue(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3178 | const CFX_ByteStringC& szFuncName, |
| 3179 | CFXJSE_Arguments& args) { |
| 3180 | int32_t argc = args.GetLength(); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3181 | if (argc < 1 || argc > 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 3182 | ToJSContext(pThis, nullptr) |
| 3183 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"UnitValue"); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3184 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3185 | } |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3186 | |
| 3187 | std::unique_ptr<CFXJSE_Value> unitspanValue = GetSimpleValue(pThis, args, 0); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 3188 | if (unitspanValue->IsNull()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3189 | args.GetReturnValue()->SetNull(); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3190 | return; |
| 3191 | } |
| 3192 | |
| 3193 | CFX_ByteString unitspanString; |
| 3194 | ValueToUTF8String(unitspanValue.get(), unitspanString); |
| 3195 | const FX_CHAR* pData = unitspanString.c_str(); |
| 3196 | if (!pData) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3197 | args.GetReturnValue()->SetInteger(0); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3198 | return; |
| 3199 | } |
| 3200 | |
| 3201 | int32_t u = 0; |
| 3202 | while (IsWhitespace(pData[u])) |
| 3203 | ++u; |
| 3204 | |
| 3205 | while (u < unitspanString.GetLength()) { |
| 3206 | if ((pData[u] > '9' || pData[u] < '0') && pData[u] != '.' && |
| 3207 | pData[u] != '-') { |
| 3208 | break; |
| 3209 | } |
| 3210 | ++u; |
| 3211 | } |
| 3212 | |
| 3213 | FX_CHAR* pTemp = nullptr; |
| 3214 | FX_DOUBLE dFirstNumber = strtod(pData, &pTemp); |
| 3215 | while (IsWhitespace(pData[u])) |
| 3216 | ++u; |
| 3217 | |
| 3218 | int32_t uLen = unitspanString.GetLength(); |
| 3219 | CFX_ByteString strFirstUnit; |
| 3220 | while (u < uLen) { |
| 3221 | if (pData[u] == ' ') |
| 3222 | break; |
| 3223 | |
| 3224 | strFirstUnit += pData[u]; |
| 3225 | ++u; |
| 3226 | } |
| 3227 | strFirstUnit.MakeLower(); |
| 3228 | |
| 3229 | CFX_ByteString strUnit; |
| 3230 | if (argc > 1) { |
| 3231 | std::unique_ptr<CFXJSE_Value> unitValue = GetSimpleValue(pThis, args, 1); |
| 3232 | CFX_ByteString unitTempString; |
| 3233 | ValueToUTF8String(unitValue.get(), unitTempString); |
| 3234 | const FX_CHAR* pChar = unitTempString.c_str(); |
| 3235 | int32_t uVal = 0; |
| 3236 | while (IsWhitespace(pChar[uVal])) |
| 3237 | ++uVal; |
| 3238 | |
| 3239 | while (uVal < unitTempString.GetLength()) { |
| 3240 | if ((pChar[uVal] > '9' || pChar[uVal] < '0') && pChar[uVal] != '.') { |
| 3241 | break; |
| 3242 | } |
| 3243 | ++uVal; |
| 3244 | } |
| 3245 | while (IsWhitespace(pChar[uVal])) |
| 3246 | ++uVal; |
| 3247 | |
| 3248 | int32_t uValLen = unitTempString.GetLength(); |
| 3249 | while (uVal < uValLen) { |
| 3250 | if (pChar[uVal] == ' ') |
| 3251 | break; |
| 3252 | |
| 3253 | strUnit += pChar[uVal]; |
| 3254 | ++uVal; |
| 3255 | } |
| 3256 | strUnit.MakeLower(); |
| 3257 | } else { |
| 3258 | strUnit = strFirstUnit; |
| 3259 | } |
| 3260 | |
| 3261 | FX_DOUBLE dResult = 0; |
| 3262 | if (strFirstUnit == "in" || strFirstUnit == "inches") { |
| 3263 | if (strUnit == "mm" || strUnit == "millimeters") |
| 3264 | dResult = dFirstNumber * 25.4; |
| 3265 | else if (strUnit == "cm" || strUnit == "centimeters") |
| 3266 | dResult = dFirstNumber * 2.54; |
| 3267 | else if (strUnit == "pt" || strUnit == "points") |
| 3268 | dResult = dFirstNumber / 72; |
| 3269 | else if (strUnit == "mp" || strUnit == "millipoints") |
| 3270 | dResult = dFirstNumber / 72000; |
| 3271 | else |
| 3272 | dResult = dFirstNumber; |
| 3273 | } else if (strFirstUnit == "mm" || strFirstUnit == "millimeters") { |
| 3274 | if (strUnit == "mm" || strUnit == "millimeters") |
| 3275 | dResult = dFirstNumber; |
| 3276 | else if (strUnit == "cm" || strUnit == "centimeters") |
| 3277 | dResult = dFirstNumber / 10; |
| 3278 | else if (strUnit == "pt" || strUnit == "points") |
| 3279 | dResult = dFirstNumber / 25.4 / 72; |
| 3280 | else if (strUnit == "mp" || strUnit == "millipoints") |
| 3281 | dResult = dFirstNumber / 25.4 / 72000; |
| 3282 | else |
| 3283 | dResult = dFirstNumber / 25.4; |
| 3284 | } else if (strFirstUnit == "cm" || strFirstUnit == "centimeters") { |
| 3285 | if (strUnit == "mm" || strUnit == "millimeters") |
| 3286 | dResult = dFirstNumber * 10; |
| 3287 | else if (strUnit == "cm" || strUnit == "centimeters") |
| 3288 | dResult = dFirstNumber; |
| 3289 | else if (strUnit == "pt" || strUnit == "points") |
| 3290 | dResult = dFirstNumber / 2.54 / 72; |
| 3291 | else if (strUnit == "mp" || strUnit == "millipoints") |
| 3292 | dResult = dFirstNumber / 2.54 / 72000; |
| 3293 | else |
| 3294 | dResult = dFirstNumber / 2.54; |
| 3295 | } else if (strFirstUnit == "pt" || strFirstUnit == "points") { |
| 3296 | if (strUnit == "mm" || strUnit == "millimeters") |
| 3297 | dResult = dFirstNumber / 72 * 25.4; |
| 3298 | else if (strUnit == "cm" || strUnit == "centimeters") |
| 3299 | dResult = dFirstNumber / 72 * 2.54; |
| 3300 | else if (strUnit == "pt" || strUnit == "points") |
| 3301 | dResult = dFirstNumber; |
| 3302 | else if (strUnit == "mp" || strUnit == "millipoints") |
| 3303 | dResult = dFirstNumber * 1000; |
| 3304 | else |
| 3305 | dResult = dFirstNumber / 72; |
| 3306 | } else if (strFirstUnit == "mp" || strFirstUnit == "millipoints") { |
| 3307 | if (strUnit == "mm" || strUnit == "millimeters") |
| 3308 | dResult = dFirstNumber / 72000 * 25.4; |
| 3309 | else if (strUnit == "cm" || strUnit == "centimeters") |
| 3310 | dResult = dFirstNumber / 72000 * 2.54; |
| 3311 | else if (strUnit == "pt" || strUnit == "points") |
| 3312 | dResult = dFirstNumber / 1000; |
| 3313 | else if (strUnit == "mp" || strUnit == "millipoints") |
| 3314 | dResult = dFirstNumber; |
| 3315 | else |
| 3316 | dResult = dFirstNumber / 72000; |
| 3317 | } |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3318 | args.GetReturnValue()->SetDouble(dResult); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3319 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3320 | |
| 3321 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 3322 | void CXFA_FM2JSContext::At(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3323 | const CFX_ByteStringC& szFuncName, |
| 3324 | CFXJSE_Arguments& args) { |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3325 | if (args.GetLength() != 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 3326 | ToJSContext(pThis, nullptr) |
| 3327 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"At"); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3328 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3329 | } |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3330 | |
| 3331 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 3332 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 3333 | if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3334 | args.GetReturnValue()->SetNull(); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3335 | return; |
| 3336 | } |
| 3337 | |
| 3338 | CFX_ByteString stringTwo; |
| 3339 | ValueToUTF8String(argTwo.get(), stringTwo); |
| 3340 | if (stringTwo.IsEmpty()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3341 | args.GetReturnValue()->SetInteger(1); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3342 | return; |
| 3343 | } |
| 3344 | |
| 3345 | CFX_ByteString stringOne; |
| 3346 | ValueToUTF8String(argOne.get(), stringOne); |
| 3347 | FX_STRSIZE iPosition = stringOne.Find(stringTwo.AsStringC()); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3348 | args.GetReturnValue()->SetInteger(iPosition + 1); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3349 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3350 | |
| 3351 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 3352 | void CXFA_FM2JSContext::Concat(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3353 | const CFX_ByteStringC& szFuncName, |
| 3354 | CFXJSE_Arguments& args) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3355 | int32_t argc = args.GetLength(); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3356 | if (argc < 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 3357 | ToJSContext(pThis, nullptr) |
| 3358 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Concat"); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3359 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3360 | } |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3361 | |
| 3362 | CFX_ByteString resultString; |
| 3363 | FX_BOOL bAllNull = TRUE; |
| 3364 | for (int32_t i = 0; i < argc; i++) { |
| 3365 | std::unique_ptr<CFXJSE_Value> value = GetSimpleValue(pThis, args, i); |
| 3366 | if (ValueIsNull(pThis, value.get())) |
| 3367 | continue; |
| 3368 | |
| 3369 | bAllNull = FALSE; |
| 3370 | |
| 3371 | CFX_ByteString valueStr; |
| 3372 | ValueToUTF8String(value.get(), valueStr); |
| 3373 | resultString += valueStr; |
| 3374 | } |
| 3375 | |
| 3376 | if (bAllNull) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3377 | args.GetReturnValue()->SetNull(); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3378 | return; |
| 3379 | } |
| 3380 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3381 | args.GetReturnValue()->SetString(resultString.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3382 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3383 | |
| 3384 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 3385 | void CXFA_FM2JSContext::Decode(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3386 | const CFX_ByteStringC& szFuncName, |
| 3387 | CFXJSE_Arguments& args) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3388 | int32_t argc = args.GetLength(); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3389 | if (argc < 1 || argc > 2) { |
| 3390 | ToJSContext(pThis, nullptr) |
| 3391 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Decode"); |
| 3392 | return; |
| 3393 | } |
| 3394 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3395 | if (argc == 1) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 3396 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 3397 | if (ValueIsNull(pThis, argOne.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3398 | args.GetReturnValue()->SetNull(); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3399 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3400 | } |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3401 | |
| 3402 | CFX_ByteString toDecodeString; |
| 3403 | ValueToUTF8String(argOne.get(), toDecodeString); |
| 3404 | CFX_ByteTextBuf resultBuf; |
| 3405 | DecodeURL(toDecodeString.AsStringC(), resultBuf); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3406 | args.GetReturnValue()->SetString(resultBuf.AsStringC()); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3407 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3408 | } |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3409 | |
| 3410 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 3411 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 3412 | if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3413 | args.GetReturnValue()->SetNull(); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3414 | return; |
| 3415 | } |
| 3416 | |
| 3417 | CFX_ByteString toDecodeString; |
| 3418 | ValueToUTF8String(argOne.get(), toDecodeString); |
| 3419 | |
| 3420 | CFX_ByteString identifyString; |
| 3421 | ValueToUTF8String(argTwo.get(), identifyString); |
| 3422 | |
| 3423 | CFX_ByteTextBuf resultBuf; |
| 3424 | if (identifyString.EqualNoCase("html")) |
| 3425 | DecodeHTML(toDecodeString.AsStringC(), resultBuf); |
| 3426 | else if (identifyString.EqualNoCase("xml")) |
| 3427 | DecodeXML(toDecodeString.AsStringC(), resultBuf); |
| 3428 | else |
| 3429 | DecodeURL(toDecodeString.AsStringC(), resultBuf); |
| 3430 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3431 | args.GetReturnValue()->SetString(resultBuf.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3432 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3433 | |
| 3434 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3435 | void CXFA_FM2JSContext::DecodeURL(const CFX_ByteStringC& szURLString, |
| 3436 | CFX_ByteTextBuf& szResultString) { |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 3437 | CFX_WideString wsURLString = CFX_WideString::FromUTF8(szURLString); |
tsepez | bd9748d | 2016-04-13 21:40:19 -0700 | [diff] [blame] | 3438 | const FX_WCHAR* pData = wsURLString.c_str(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3439 | int32_t i = 0; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3440 | CFX_WideTextBuf wsResultBuf; |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3441 | while (i < wsURLString.GetLength()) { |
| 3442 | FX_WCHAR ch = pData[i]; |
| 3443 | if ('%' != ch) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3444 | wsResultBuf.AppendChar(ch); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3445 | ++i; |
| 3446 | continue; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3447 | } |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3448 | |
| 3449 | FX_WCHAR chTemp = 0; |
| 3450 | int32_t iCount = 0; |
| 3451 | while (iCount < 2) { |
| 3452 | ++i; |
| 3453 | ch = pData[i]; |
| 3454 | if (ch <= '9' && ch >= '0') { |
| 3455 | // TODO(dsinclair): Premultiply and add rather then scale. |
| 3456 | chTemp += (ch - '0') * (!iCount ? 16 : 1); |
| 3457 | } else if (ch <= 'F' && ch >= 'A') { |
| 3458 | chTemp += (ch - 'A' + 10) * (!iCount ? 16 : 1); |
| 3459 | } else if (ch <= 'f' && ch >= 'a') { |
| 3460 | chTemp += (ch - 'a' + 10) * (!iCount ? 16 : 1); |
| 3461 | } else { |
| 3462 | wsResultBuf.Clear(); |
| 3463 | return; |
| 3464 | } |
| 3465 | ++iCount; |
| 3466 | } |
| 3467 | wsResultBuf.AppendChar(chTemp); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3468 | ++i; |
| 3469 | } |
| 3470 | wsResultBuf.AppendChar(0); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3471 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3472 | szResultString.Clear(); |
| 3473 | szResultString << FX_UTF8Encode(wsResultBuf.GetBuffer(), |
tsepez | 28f97ff | 2016-04-04 16:41:35 -0700 | [diff] [blame] | 3474 | wsResultBuf.GetLength()) |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 3475 | .AsStringC(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3476 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3477 | |
| 3478 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3479 | void CXFA_FM2JSContext::DecodeHTML(const CFX_ByteStringC& szHTMLString, |
| 3480 | CFX_ByteTextBuf& szResultString) { |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 3481 | CFX_WideString wsHTMLString = CFX_WideString::FromUTF8(szHTMLString); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3482 | FX_WCHAR strString[9]; |
| 3483 | int32_t iStrIndex = 0; |
| 3484 | int32_t iLen = wsHTMLString.GetLength(); |
| 3485 | int32_t i = 0; |
| 3486 | int32_t iCode = 0; |
tsepez | bd9748d | 2016-04-13 21:40:19 -0700 | [diff] [blame] | 3487 | const FX_WCHAR* pData = wsHTMLString.c_str(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3488 | CFX_WideTextBuf wsResultBuf; |
| 3489 | while (i < iLen) { |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3490 | FX_WCHAR ch = pData[i]; |
| 3491 | if (ch != '&') { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3492 | wsResultBuf.AppendChar(ch); |
| 3493 | ++i; |
| 3494 | continue; |
| 3495 | } |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3496 | |
| 3497 | ++i; |
| 3498 | ch = pData[i]; |
| 3499 | if (ch == '#') { |
| 3500 | ++i; |
| 3501 | ch = pData[i]; |
| 3502 | if (ch != 'x' && ch != 'X') { |
| 3503 | wsResultBuf.Clear(); |
| 3504 | return; |
| 3505 | } |
| 3506 | |
| 3507 | ++i; |
| 3508 | ch = pData[i]; |
| 3509 | if ((ch >= '0' && ch <= '9') || (ch <= 'f' && ch >= 'a') || |
| 3510 | (ch <= 'F' && ch >= 'A')) { |
| 3511 | while (ch != ';' && i < iLen) { |
| 3512 | if (ch >= '0' && ch <= '9') { |
| 3513 | iCode += ch - '0'; |
| 3514 | } else if (ch <= 'f' && ch >= 'a') { |
| 3515 | iCode += ch - 'a' + 10; |
| 3516 | } else if (ch <= 'F' && ch >= 'A') { |
| 3517 | iCode += ch - 'A' + 10; |
| 3518 | } else { |
| 3519 | wsResultBuf.Clear(); |
| 3520 | return; |
| 3521 | } |
| 3522 | ++i; |
| 3523 | // TODO(dsinclair): Postmultiply seems wrong, start at zero |
| 3524 | // and pre-multiply then can remove the post divide. |
| 3525 | iCode *= 16; |
| 3526 | ch = pData[i]; |
| 3527 | } |
| 3528 | iCode /= 16; |
| 3529 | } |
| 3530 | } else { |
| 3531 | while (ch != ';' && i < iLen) { |
| 3532 | strString[iStrIndex++] = ch; |
| 3533 | ++i; |
| 3534 | ch = pData[i]; |
| 3535 | } |
| 3536 | strString[iStrIndex] = 0; |
| 3537 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3538 | uint32_t iData = 0; |
| 3539 | if (HTMLSTR2Code(strString, iData)) { |
| 3540 | wsResultBuf.AppendChar((FX_WCHAR)iData); |
| 3541 | } else { |
| 3542 | wsResultBuf.AppendChar(iCode); |
| 3543 | } |
| 3544 | iStrIndex = 0; |
| 3545 | strString[iStrIndex] = 0; |
| 3546 | ++i; |
| 3547 | } |
| 3548 | wsResultBuf.AppendChar(0); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3549 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3550 | szResultString.Clear(); |
| 3551 | szResultString << FX_UTF8Encode(wsResultBuf.GetBuffer(), |
tsepez | 28f97ff | 2016-04-04 16:41:35 -0700 | [diff] [blame] | 3552 | wsResultBuf.GetLength()) |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 3553 | .AsStringC(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3554 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3555 | |
| 3556 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3557 | void CXFA_FM2JSContext::DecodeXML(const CFX_ByteStringC& szXMLString, |
| 3558 | CFX_ByteTextBuf& szResultString) { |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 3559 | CFX_WideString wsXMLString = CFX_WideString::FromUTF8(szXMLString); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3560 | FX_WCHAR strString[9]; |
| 3561 | int32_t iStrIndex = 0; |
| 3562 | int32_t iLen = wsXMLString.GetLength(); |
| 3563 | int32_t i = 0; |
| 3564 | int32_t iCode = 0; |
| 3565 | FX_WCHAR ch = 0; |
tsepez | bd9748d | 2016-04-13 21:40:19 -0700 | [diff] [blame] | 3566 | const FX_WCHAR* pData = wsXMLString.c_str(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3567 | CFX_WideTextBuf wsXMLBuf; |
| 3568 | while (i < iLen) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 3569 | ch = pData[i]; |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3570 | if (ch != '&') { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3571 | wsXMLBuf.AppendChar(ch); |
| 3572 | ++i; |
| 3573 | continue; |
| 3574 | } |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3575 | |
| 3576 | // TODO(dsinclair): This is very similar to DecodeHTML, can they be |
| 3577 | // combined? |
| 3578 | ++i; |
| 3579 | ch = pData[i]; |
| 3580 | if (ch == '#') { |
| 3581 | ++i; |
| 3582 | ch = pData[i]; |
| 3583 | if (ch != 'x' && ch != 'X') { |
| 3584 | wsXMLBuf.Clear(); |
| 3585 | return; |
| 3586 | } |
| 3587 | |
| 3588 | ++i; |
| 3589 | ch = pData[i]; |
| 3590 | if ((ch >= '0' && ch <= '9') || (ch <= 'f' && ch >= 'a') || |
| 3591 | (ch <= 'F' && ch >= 'A')) { |
| 3592 | while (ch != ';') { |
| 3593 | if (ch >= '0' && ch <= '9') { |
| 3594 | iCode += ch - '0'; |
| 3595 | } else if (ch <= 'f' && ch >= 'a') { |
| 3596 | iCode += ch - 'a' + 10; |
| 3597 | } else if (ch <= 'F' && ch >= 'A') { |
| 3598 | iCode += ch - 'A' + 10; |
| 3599 | } else { |
| 3600 | wsXMLBuf.Clear(); |
| 3601 | return; |
| 3602 | } |
| 3603 | ++i; |
| 3604 | iCode *= 16; |
| 3605 | ch = pData[i]; |
| 3606 | } |
| 3607 | iCode /= 16; |
| 3608 | } |
| 3609 | } else { |
| 3610 | while (ch != ';' && i < iLen) { |
| 3611 | strString[iStrIndex++] = ch; |
| 3612 | ++i; |
| 3613 | ch = pData[i]; |
| 3614 | } |
| 3615 | strString[iStrIndex] = 0; |
| 3616 | } |
| 3617 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3618 | const FX_WCHAR* const strName[] = {L"quot", L"amp", L"apos", L"lt", L"gt"}; |
| 3619 | int32_t iIndex = 0; |
| 3620 | while (iIndex < 5) { |
| 3621 | if (FXSYS_memcmp(strString, strName[iIndex], |
| 3622 | FXSYS_wcslen(strName[iIndex])) == 0) { |
| 3623 | break; |
| 3624 | } |
| 3625 | ++iIndex; |
| 3626 | } |
| 3627 | switch (iIndex) { |
| 3628 | case 0: |
| 3629 | wsXMLBuf.AppendChar('"'); |
| 3630 | break; |
| 3631 | case 1: |
| 3632 | wsXMLBuf.AppendChar('&'); |
| 3633 | break; |
| 3634 | case 2: |
| 3635 | wsXMLBuf.AppendChar('\''); |
| 3636 | break; |
| 3637 | case 3: |
| 3638 | wsXMLBuf.AppendChar('<'); |
| 3639 | break; |
| 3640 | case 4: |
| 3641 | wsXMLBuf.AppendChar('>'); |
| 3642 | break; |
| 3643 | default: |
| 3644 | wsXMLBuf.AppendChar(iCode); |
| 3645 | break; |
| 3646 | } |
| 3647 | iStrIndex = 0; |
| 3648 | strString[iStrIndex] = 0; |
| 3649 | ++i; |
| 3650 | iCode = 0; |
| 3651 | } |
| 3652 | wsXMLBuf.AppendChar(0); |
dsinclair | 759de38 | 2016-06-07 14:10:16 -0700 | [diff] [blame] | 3653 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3654 | szResultString.Clear(); |
tsepez | 28f97ff | 2016-04-04 16:41:35 -0700 | [diff] [blame] | 3655 | szResultString << FX_UTF8Encode(wsXMLBuf.GetBuffer(), wsXMLBuf.GetLength()) |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 3656 | .AsStringC(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3657 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3658 | |
| 3659 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 3660 | void CXFA_FM2JSContext::Encode(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3661 | const CFX_ByteStringC& szFuncName, |
| 3662 | CFXJSE_Arguments& args) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3663 | int32_t argc = args.GetLength(); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3664 | if (argc < 1 || argc > 2) { |
| 3665 | ToJSContext(pThis, nullptr) |
| 3666 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Encode"); |
| 3667 | return; |
| 3668 | } |
| 3669 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3670 | if (argc == 1) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 3671 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 3672 | if (ValueIsNull(pThis, argOne.get())) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 3673 | args.GetReturnValue()->SetNull(); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3674 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3675 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3676 | |
| 3677 | CFX_ByteString toEncodeString; |
| 3678 | ValueToUTF8String(argOne.get(), toEncodeString); |
| 3679 | CFX_ByteTextBuf resultBuf; |
| 3680 | EncodeURL(toEncodeString.AsStringC(), resultBuf); |
| 3681 | args.GetReturnValue()->SetString(resultBuf.AsStringC()); |
| 3682 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3683 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3684 | |
| 3685 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 3686 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 3687 | if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get())) { |
| 3688 | args.GetReturnValue()->SetNull(); |
| 3689 | return; |
| 3690 | } |
| 3691 | |
| 3692 | CFX_ByteString toEncodeString; |
| 3693 | ValueToUTF8String(argOne.get(), toEncodeString); |
| 3694 | CFX_ByteString identifyString; |
| 3695 | ValueToUTF8String(argTwo.get(), identifyString); |
| 3696 | CFX_ByteTextBuf resultBuf; |
| 3697 | if (identifyString.EqualNoCase("html")) |
| 3698 | EncodeHTML(toEncodeString.AsStringC(), resultBuf); |
| 3699 | else if (identifyString.EqualNoCase("xml")) |
| 3700 | EncodeXML(toEncodeString.AsStringC(), resultBuf); |
| 3701 | else |
| 3702 | EncodeURL(toEncodeString.AsStringC(), resultBuf); |
| 3703 | |
| 3704 | args.GetReturnValue()->SetString(resultBuf.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3705 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3706 | |
| 3707 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3708 | void CXFA_FM2JSContext::EncodeURL(const CFX_ByteStringC& szURLString, |
| 3709 | CFX_ByteTextBuf& szResultBuf) { |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 3710 | CFX_WideString wsURLString = CFX_WideString::FromUTF8(szURLString); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3711 | CFX_WideTextBuf wsResultBuf; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3712 | FX_WCHAR strEncode[4]; |
| 3713 | strEncode[0] = '%'; |
| 3714 | strEncode[3] = 0; |
| 3715 | FX_WCHAR strUnsafe[] = {' ', '<', '>', '"', '#', '%', '{', '}', |
| 3716 | '|', '\\', '^', '~', '[', ']', '`'}; |
| 3717 | FX_WCHAR strReserved[] = {';', '/', '?', ':', '@', '=', '&'}; |
| 3718 | FX_WCHAR strSpecial[] = {'$', '-', '+', '!', '*', '\'', '(', ')', ','}; |
| 3719 | const FX_WCHAR* strCode = L"0123456789abcdef"; |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3720 | for (int32_t u = 0; u < wsURLString.GetLength(); ++u) { |
| 3721 | FX_WCHAR ch = wsURLString.GetAt(u); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3722 | int32_t i = 0; |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3723 | int32_t iCount = FX_ArraySize(strUnsafe); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3724 | while (i < iCount) { |
| 3725 | if (ch == strUnsafe[i]) { |
| 3726 | int32_t iIndex = ch / 16; |
| 3727 | strEncode[1] = strCode[iIndex]; |
| 3728 | strEncode[2] = strCode[ch - iIndex * 16]; |
| 3729 | wsResultBuf << FX_WSTRC(strEncode); |
| 3730 | break; |
| 3731 | } |
| 3732 | ++i; |
| 3733 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3734 | if (i < iCount) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3735 | continue; |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3736 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3737 | i = 0; |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3738 | iCount = FX_ArraySize(strReserved); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3739 | while (i < iCount) { |
| 3740 | if (ch == strReserved[i]) { |
| 3741 | int32_t iIndex = ch / 16; |
| 3742 | strEncode[1] = strCode[iIndex]; |
| 3743 | strEncode[2] = strCode[ch - iIndex * 16]; |
| 3744 | wsResultBuf << FX_WSTRC(strEncode); |
| 3745 | break; |
| 3746 | } |
| 3747 | ++i; |
| 3748 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3749 | if (i < iCount) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3750 | continue; |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3751 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3752 | i = 0; |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3753 | iCount = FX_ArraySize(strSpecial); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3754 | while (i < iCount) { |
| 3755 | if (ch == strSpecial[i]) { |
| 3756 | wsResultBuf.AppendChar(ch); |
| 3757 | break; |
| 3758 | } |
| 3759 | ++i; |
| 3760 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3761 | if (i < iCount) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3762 | continue; |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3763 | |
| 3764 | if ((ch >= 0x80 && ch <= 0xff) || ch <= 0x1f || ch == 0x7f) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3765 | int32_t iIndex = ch / 16; |
| 3766 | strEncode[1] = strCode[iIndex]; |
| 3767 | strEncode[2] = strCode[ch - iIndex * 16]; |
| 3768 | wsResultBuf << FX_WSTRC(strEncode); |
| 3769 | } else if (ch >= 0x20 && ch <= 0x7e) { |
| 3770 | wsResultBuf.AppendChar(ch); |
| 3771 | } else { |
ochang | f6be145 | 2016-06-01 12:20:03 -0700 | [diff] [blame] | 3772 | const FX_WCHAR iRadix = 16; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3773 | CFX_WideString strTmp; |
| 3774 | while (ch >= iRadix) { |
| 3775 | FX_WCHAR tmp = strCode[ch % iRadix]; |
| 3776 | ch /= iRadix; |
| 3777 | strTmp += tmp; |
| 3778 | } |
| 3779 | strTmp += strCode[ch]; |
| 3780 | int32_t iLen = strTmp.GetLength(); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3781 | if (iLen < 2) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3782 | break; |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3783 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3784 | int32_t iIndex = 0; |
| 3785 | if (iLen % 2 != 0) { |
| 3786 | strEncode[1] = '0'; |
| 3787 | strEncode[2] = strTmp.GetAt(iLen - 1); |
| 3788 | iIndex = iLen - 2; |
| 3789 | } else { |
| 3790 | strEncode[1] = strTmp.GetAt(iLen - 1); |
| 3791 | strEncode[2] = strTmp.GetAt(iLen - 2); |
| 3792 | iIndex = iLen - 3; |
| 3793 | } |
| 3794 | wsResultBuf << FX_WSTRC(strEncode); |
| 3795 | while (iIndex > 0) { |
| 3796 | strEncode[1] = strTmp.GetAt(iIndex); |
| 3797 | strEncode[2] = strTmp.GetAt(iIndex - 1); |
| 3798 | iIndex -= 2; |
| 3799 | wsResultBuf << FX_WSTRC(strEncode); |
| 3800 | } |
| 3801 | } |
| 3802 | } |
| 3803 | wsResultBuf.AppendChar(0); |
| 3804 | szResultBuf.Clear(); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3805 | |
tsepez | 28f97ff | 2016-04-04 16:41:35 -0700 | [diff] [blame] | 3806 | szResultBuf << FX_UTF8Encode(wsResultBuf.GetBuffer(), wsResultBuf.GetLength()) |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 3807 | .AsStringC(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3808 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3809 | |
| 3810 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3811 | void CXFA_FM2JSContext::EncodeHTML(const CFX_ByteStringC& szHTMLString, |
| 3812 | CFX_ByteTextBuf& szResultBuf) { |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 3813 | CFX_ByteString str = szHTMLString.c_str(); |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 3814 | CFX_WideString wsHTMLString = CFX_WideString::FromUTF8(str.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3815 | const FX_WCHAR* strCode = L"0123456789abcdef"; |
| 3816 | FX_WCHAR strEncode[9]; |
| 3817 | strEncode[0] = '&'; |
| 3818 | strEncode[1] = '#'; |
| 3819 | strEncode[2] = 'x'; |
| 3820 | strEncode[5] = ';'; |
| 3821 | strEncode[6] = 0; |
| 3822 | strEncode[7] = ';'; |
| 3823 | strEncode[8] = 0; |
| 3824 | CFX_WideTextBuf wsResultBuf; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3825 | int32_t iLen = wsHTMLString.GetLength(); |
| 3826 | int32_t i = 0; |
tsepez | bd9748d | 2016-04-13 21:40:19 -0700 | [diff] [blame] | 3827 | const FX_WCHAR* pData = wsHTMLString.c_str(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3828 | while (i < iLen) { |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3829 | uint32_t ch = pData[i]; |
| 3830 | CFX_WideString htmlReserve; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3831 | if (HTMLCode2STR(ch, htmlReserve)) { |
| 3832 | wsResultBuf.AppendChar(L'&'); |
| 3833 | wsResultBuf << htmlReserve; |
| 3834 | wsResultBuf.AppendChar(L';'); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3835 | } else if (ch >= 32 && ch <= 126) { |
| 3836 | wsResultBuf.AppendChar((FX_WCHAR)ch); |
| 3837 | } else if (ch < 256) { |
| 3838 | int32_t iIndex = ch / 16; |
| 3839 | strEncode[3] = strCode[iIndex]; |
| 3840 | strEncode[4] = strCode[ch - iIndex * 16]; |
| 3841 | strEncode[5] = ';'; |
| 3842 | strEncode[6] = 0; |
| 3843 | wsResultBuf << FX_WSTRC(strEncode); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3844 | } else { |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3845 | int32_t iBigByte = ch / 256; |
| 3846 | int32_t iLittleByte = ch % 256; |
| 3847 | strEncode[3] = strCode[iBigByte / 16]; |
| 3848 | strEncode[4] = strCode[iBigByte % 16]; |
| 3849 | strEncode[5] = strCode[iLittleByte / 16]; |
| 3850 | strEncode[6] = strCode[iLittleByte % 16]; |
| 3851 | wsResultBuf << FX_WSTRC(strEncode); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3852 | } |
| 3853 | ++i; |
| 3854 | } |
| 3855 | wsResultBuf.AppendChar(0); |
| 3856 | szResultBuf.Clear(); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3857 | |
tsepez | 28f97ff | 2016-04-04 16:41:35 -0700 | [diff] [blame] | 3858 | szResultBuf << FX_UTF8Encode(wsResultBuf.GetBuffer(), wsResultBuf.GetLength()) |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 3859 | .AsStringC(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3860 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3861 | |
| 3862 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3863 | void CXFA_FM2JSContext::EncodeXML(const CFX_ByteStringC& szXMLString, |
| 3864 | CFX_ByteTextBuf& szResultBuf) { |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 3865 | CFX_WideString wsXMLString = CFX_WideString::FromUTF8(szXMLString); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3866 | CFX_WideTextBuf wsResultBuf; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3867 | FX_WCHAR strEncode[9]; |
| 3868 | strEncode[0] = '&'; |
| 3869 | strEncode[1] = '#'; |
| 3870 | strEncode[2] = 'x'; |
| 3871 | strEncode[5] = ';'; |
| 3872 | strEncode[6] = 0; |
| 3873 | strEncode[7] = ';'; |
| 3874 | strEncode[8] = 0; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3875 | const FX_WCHAR* strCode = L"0123456789abcdef"; |
tsepez | bd9748d | 2016-04-13 21:40:19 -0700 | [diff] [blame] | 3876 | const FX_WCHAR* pData = wsXMLString.c_str(); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3877 | for (int32_t u = 0; u < wsXMLString.GetLength(); ++u) { |
| 3878 | FX_WCHAR ch = pData[u]; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3879 | switch (ch) { |
| 3880 | case '"': |
| 3881 | wsResultBuf.AppendChar('&'); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3882 | wsResultBuf << CFX_WideStringC(L"quot"); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3883 | wsResultBuf.AppendChar(';'); |
| 3884 | break; |
| 3885 | case '&': |
| 3886 | wsResultBuf.AppendChar('&'); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3887 | wsResultBuf << CFX_WideStringC(L"amp"); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3888 | wsResultBuf.AppendChar(';'); |
| 3889 | break; |
| 3890 | case '\'': |
| 3891 | wsResultBuf.AppendChar('&'); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3892 | wsResultBuf << CFX_WideStringC(L"apos"); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3893 | wsResultBuf.AppendChar(';'); |
| 3894 | break; |
| 3895 | case '<': |
| 3896 | wsResultBuf.AppendChar('&'); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3897 | wsResultBuf << CFX_WideStringC(L"lt"); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3898 | wsResultBuf.AppendChar(';'); |
| 3899 | break; |
| 3900 | case '>': |
| 3901 | wsResultBuf.AppendChar('&'); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3902 | wsResultBuf << CFX_WideStringC(L"gt"); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3903 | wsResultBuf.AppendChar(';'); |
| 3904 | break; |
| 3905 | default: { |
| 3906 | if (ch >= 32 && ch <= 126) { |
| 3907 | wsResultBuf.AppendChar(ch); |
| 3908 | } else if (ch < 256) { |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3909 | int32_t iIndex = ch / 16; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3910 | strEncode[3] = strCode[iIndex]; |
| 3911 | strEncode[4] = strCode[ch - iIndex * 16]; |
| 3912 | strEncode[5] = ';'; |
| 3913 | strEncode[6] = 0; |
| 3914 | wsResultBuf << FX_WSTRC(strEncode); |
| 3915 | } else { |
| 3916 | int32_t iBigByte = ch / 256; |
| 3917 | int32_t iLittleByte = ch % 256; |
| 3918 | strEncode[3] = strCode[iBigByte / 16]; |
| 3919 | strEncode[4] = strCode[iBigByte % 16]; |
| 3920 | strEncode[5] = strCode[iLittleByte / 16]; |
| 3921 | strEncode[6] = strCode[iLittleByte % 16]; |
| 3922 | wsResultBuf << FX_WSTRC(strEncode); |
| 3923 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3924 | break; |
| 3925 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3926 | } |
| 3927 | } |
| 3928 | wsResultBuf.AppendChar(0); |
| 3929 | szResultBuf.Clear(); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3930 | |
tsepez | 28f97ff | 2016-04-04 16:41:35 -0700 | [diff] [blame] | 3931 | szResultBuf << FX_UTF8Encode(wsResultBuf.GetBuffer(), wsResultBuf.GetLength()) |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 3932 | .AsStringC(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3933 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3934 | |
| 3935 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3936 | FX_BOOL CXFA_FM2JSContext::HTMLSTR2Code(const CFX_WideStringC& pData, |
| 3937 | uint32_t& iCode) { |
tsepez | b6853cf | 2016-04-25 11:23:43 -0700 | [diff] [blame] | 3938 | uint32_t uHash = FX_HashCode_GetW(pData, false); |
| 3939 | int32_t iStart = 0; |
| 3940 | int32_t iEnd = FX_ArraySize(reservesForDecode) - 1; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3941 | do { |
tsepez | b6853cf | 2016-04-25 11:23:43 -0700 | [diff] [blame] | 3942 | int32_t iMid = (iStart + iEnd) / 2; |
| 3943 | XFA_FMHtmlHashedReserveCode htmlhashedreservecode = reservesForDecode[iMid]; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3944 | if (uHash == htmlhashedreservecode.m_uHash) { |
| 3945 | iCode = htmlhashedreservecode.m_uCode; |
| 3946 | return TRUE; |
tsepez | b6853cf | 2016-04-25 11:23:43 -0700 | [diff] [blame] | 3947 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3948 | |
| 3949 | if (uHash < htmlhashedreservecode.m_uHash) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3950 | iEnd = iMid - 1; |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3951 | else |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3952 | iStart = iMid + 1; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3953 | } while (iStart <= iEnd); |
| 3954 | return FALSE; |
| 3955 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3956 | |
| 3957 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3958 | FX_BOOL CXFA_FM2JSContext::HTMLCode2STR(uint32_t iCode, |
| 3959 | CFX_WideString& wsHTMLReserve) { |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3960 | int32_t iStart = 0; |
| 3961 | int32_t iEnd = FX_ArraySize(reservesForEncode) - 1; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3962 | do { |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3963 | int32_t iMid = (iStart + iEnd) / 2; |
| 3964 | XFA_FMHtmlReserveCode htmlreservecode = reservesForEncode[iMid]; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3965 | if (iCode == htmlreservecode.m_uCode) { |
| 3966 | wsHTMLReserve = htmlreservecode.m_htmlReserve; |
| 3967 | return TRUE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3968 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3969 | |
| 3970 | if (iCode < htmlreservecode.m_uCode) |
| 3971 | iEnd = iMid - 1; |
| 3972 | else |
| 3973 | iStart = iMid + 1; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3974 | } while (iStart <= iEnd); |
| 3975 | return FALSE; |
| 3976 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 3977 | |
| 3978 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 3979 | void CXFA_FM2JSContext::Format(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3980 | const CFX_ByteStringC& szFuncName, |
| 3981 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 3982 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3983 | if (args.GetLength() < 2) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 3984 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Format"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3985 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 3986 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 3987 | |
| 3988 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 3989 | CFX_ByteString szPattern; |
| 3990 | ValueToUTF8String(argOne.get(), szPattern); |
| 3991 | |
| 3992 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 3993 | CFX_ByteString szValue; |
| 3994 | ValueToUTF8String(argTwo.get(), szValue); |
| 3995 | |
| 3996 | CXFA_Document* pDoc = pContext->GetDocument(); |
| 3997 | CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); |
| 3998 | CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject()); |
| 3999 | ASSERT(pThisNode); |
| 4000 | |
| 4001 | CXFA_WidgetData widgetData(pThisNode); |
| 4002 | IFX_Locale* pLocale = widgetData.GetLocal(); |
| 4003 | uint32_t patternType; |
| 4004 | CFX_WideString wsPattern = CFX_WideString::FromUTF8(szPattern.AsStringC()); |
| 4005 | CFX_WideString wsValue = CFX_WideString::FromUTF8(szValue.AsStringC()); |
| 4006 | if (!PatternStringType(szPattern.AsStringC(), patternType)) { |
| 4007 | switch (patternType) { |
| 4008 | case XFA_VT_DATETIME: { |
| 4009 | FX_STRSIZE iTChar = wsPattern.Find(L'T'); |
| 4010 | CFX_WideString wsDatePattern(L"date{"); |
| 4011 | wsDatePattern += wsPattern.Left(iTChar) + L"} "; |
| 4012 | |
| 4013 | CFX_WideString wsTimePattern(L"time{"); |
| 4014 | wsTimePattern += wsPattern.Mid(iTChar + 1) + L"}"; |
| 4015 | wsPattern = wsDatePattern + wsTimePattern; |
| 4016 | } break; |
| 4017 | case XFA_VT_DATE: { |
| 4018 | wsPattern = L"date{" + wsPattern + L"}"; |
| 4019 | } break; |
| 4020 | case XFA_VT_TIME: { |
| 4021 | wsPattern = L"time{" + wsPattern + L"}"; |
| 4022 | } break; |
| 4023 | case XFA_VT_TEXT: { |
| 4024 | wsPattern = L"text{" + wsPattern + L"}"; |
| 4025 | } break; |
| 4026 | case XFA_VT_FLOAT: { |
| 4027 | wsPattern = L"num{" + wsPattern + L"}"; |
| 4028 | } break; |
| 4029 | default: { |
| 4030 | CFX_WideString wsTestPattern; |
| 4031 | wsTestPattern = L"num{" + wsPattern + L"}"; |
| 4032 | CXFA_LocaleValue tempLocaleValue(XFA_VT_FLOAT, wsValue, wsTestPattern, |
| 4033 | pLocale, pMgr); |
| 4034 | if (tempLocaleValue.IsValid()) { |
| 4035 | wsPattern = wsTestPattern; |
| 4036 | patternType = XFA_VT_FLOAT; |
| 4037 | } else { |
| 4038 | wsTestPattern = L"text{" + wsPattern + L"}"; |
| 4039 | wsPattern = wsTestPattern; |
| 4040 | patternType = XFA_VT_TEXT; |
| 4041 | } |
| 4042 | } break; |
| 4043 | } |
| 4044 | } |
| 4045 | CXFA_LocaleValue localeValue(patternType, wsValue, wsPattern, pLocale, pMgr); |
| 4046 | CFX_WideString wsRet; |
| 4047 | if (!localeValue.FormatPatterns(wsRet, wsPattern, pLocale, |
| 4048 | XFA_VALUEPICTURE_Display)) { |
| 4049 | args.GetReturnValue()->SetString(""); |
| 4050 | return; |
| 4051 | } |
| 4052 | |
| 4053 | args.GetReturnValue()->SetString( |
| 4054 | FX_UTF8Encode(wsRet.c_str(), wsRet.GetLength()).AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4055 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4056 | |
| 4057 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4058 | void CXFA_FM2JSContext::Left(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4059 | const CFX_ByteStringC& szFuncName, |
| 4060 | CFXJSE_Arguments& args) { |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4061 | if (args.GetLength() != 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4062 | ToJSContext(pThis, nullptr) |
| 4063 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Left"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4064 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4065 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4066 | |
| 4067 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 4068 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 4069 | if ((ValueIsNull(pThis, argOne.get())) || |
| 4070 | (ValueIsNull(pThis, argTwo.get()))) { |
| 4071 | args.GetReturnValue()->SetNull(); |
| 4072 | return; |
| 4073 | } |
| 4074 | |
| 4075 | CFX_ByteString sourceString; |
| 4076 | ValueToUTF8String(argOne.get(), sourceString); |
| 4077 | int32_t count = std::max(0, ValueToInteger(pThis, argTwo.get())); |
| 4078 | args.GetReturnValue()->SetString(sourceString.Left(count).AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4079 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4080 | |
| 4081 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4082 | void CXFA_FM2JSContext::Len(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4083 | const CFX_ByteStringC& szFuncName, |
| 4084 | CFXJSE_Arguments& args) { |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4085 | if (args.GetLength() != 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4086 | ToJSContext(pThis, nullptr) |
| 4087 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Len"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4088 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4089 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4090 | |
| 4091 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 4092 | if (ValueIsNull(pThis, argOne.get())) { |
| 4093 | args.GetReturnValue()->SetNull(); |
| 4094 | return; |
| 4095 | } |
| 4096 | |
| 4097 | CFX_ByteString sourceString; |
| 4098 | ValueToUTF8String(argOne.get(), sourceString); |
| 4099 | args.GetReturnValue()->SetInteger(sourceString.GetLength()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4100 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4101 | |
| 4102 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4103 | void CXFA_FM2JSContext::Lower(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4104 | const CFX_ByteStringC& szFuncName, |
| 4105 | CFXJSE_Arguments& args) { |
| 4106 | int32_t argc = args.GetLength(); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4107 | if (argc < 1 || argc > 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4108 | ToJSContext(pThis, nullptr) |
| 4109 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Lower"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4110 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4111 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4112 | |
| 4113 | CFX_ByteString argString; |
| 4114 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 4115 | if (ValueIsNull(pThis, argOne.get())) { |
| 4116 | args.GetReturnValue()->SetNull(); |
| 4117 | return; |
| 4118 | } |
| 4119 | |
| 4120 | ValueToUTF8String(argOne.get(), argString); |
| 4121 | CFX_WideTextBuf lowStringBuf; |
| 4122 | CFX_WideString wsArgString = CFX_WideString::FromUTF8(argString.AsStringC()); |
| 4123 | const FX_WCHAR* pData = wsArgString.c_str(); |
| 4124 | int32_t i = 0; |
| 4125 | while (i < argString.GetLength()) { |
| 4126 | int32_t ch = pData[i]; |
| 4127 | if ((ch >= 0x41 && ch <= 0x5A) || (ch >= 0xC0 && ch <= 0xDE)) |
| 4128 | ch += 32; |
| 4129 | else if (ch == 0x100 || ch == 0x102 || ch == 0x104) |
| 4130 | ch += 1; |
| 4131 | |
| 4132 | lowStringBuf.AppendChar(ch); |
| 4133 | ++i; |
| 4134 | } |
| 4135 | lowStringBuf.AppendChar(0); |
| 4136 | |
| 4137 | args.GetReturnValue()->SetString( |
| 4138 | FX_UTF8Encode(lowStringBuf.GetBuffer(), lowStringBuf.GetLength()) |
| 4139 | .AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4140 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4141 | |
| 4142 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4143 | void CXFA_FM2JSContext::Ltrim(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4144 | const CFX_ByteStringC& szFuncName, |
| 4145 | CFXJSE_Arguments& args) { |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4146 | if (args.GetLength() != 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4147 | ToJSContext(pThis, nullptr) |
| 4148 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Ltrim"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4149 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4150 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4151 | |
| 4152 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 4153 | if (ValueIsNull(pThis, argOne.get())) { |
| 4154 | args.GetReturnValue()->SetNull(); |
| 4155 | return; |
| 4156 | } |
| 4157 | |
| 4158 | CFX_ByteString sourceString; |
| 4159 | ValueToUTF8String(argOne.get(), sourceString); |
| 4160 | sourceString.TrimLeft(); |
| 4161 | args.GetReturnValue()->SetString(sourceString.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4162 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4163 | |
| 4164 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4165 | void CXFA_FM2JSContext::Parse(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4166 | const CFX_ByteStringC& szFuncName, |
| 4167 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4168 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4169 | if (args.GetLength() != 2) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 4170 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Parse"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4171 | return; |
| 4172 | } |
| 4173 | |
| 4174 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 4175 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 4176 | if (ValueIsNull(pThis, argTwo.get())) { |
| 4177 | args.GetReturnValue()->SetNull(); |
| 4178 | return; |
| 4179 | } |
| 4180 | |
| 4181 | CFX_ByteString szPattern; |
| 4182 | ValueToUTF8String(argOne.get(), szPattern); |
| 4183 | CFX_ByteString szValue; |
| 4184 | ValueToUTF8String(argTwo.get(), szValue); |
| 4185 | |
| 4186 | CXFA_Document* pDoc = pContext->GetDocument(); |
| 4187 | CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); |
| 4188 | CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject()); |
| 4189 | ASSERT(pThisNode); |
| 4190 | |
| 4191 | CXFA_WidgetData widgetData(pThisNode); |
| 4192 | IFX_Locale* pLocale = widgetData.GetLocal(); |
| 4193 | CFX_WideString wsPattern = CFX_WideString::FromUTF8(szPattern.AsStringC()); |
| 4194 | CFX_WideString wsValue = CFX_WideString::FromUTF8(szValue.AsStringC()); |
| 4195 | uint32_t patternType; |
| 4196 | if (PatternStringType(szPattern.AsStringC(), patternType)) { |
| 4197 | CXFA_LocaleValue localeValue(patternType, wsValue, wsPattern, pLocale, |
| 4198 | pMgr); |
| 4199 | if (!localeValue.IsValid()) { |
| 4200 | args.GetReturnValue()->SetString(""); |
| 4201 | return; |
| 4202 | } |
| 4203 | args.GetReturnValue()->SetString( |
| 4204 | FX_UTF8Encode(localeValue.GetValue()).AsStringC()); |
| 4205 | return; |
| 4206 | } |
| 4207 | |
| 4208 | switch (patternType) { |
| 4209 | case XFA_VT_DATETIME: { |
| 4210 | FX_STRSIZE iTChar = wsPattern.Find(L'T'); |
| 4211 | CFX_WideString wsDatePattern(L"date{" + wsPattern.Left(iTChar) + L"} "); |
| 4212 | CFX_WideString wsTimePattern(L"time{" + wsPattern.Mid(iTChar + 1) + L"}"); |
| 4213 | wsPattern = wsDatePattern + wsTimePattern; |
| 4214 | CXFA_LocaleValue localeValue(patternType, wsValue, wsPattern, pLocale, |
| 4215 | pMgr); |
| 4216 | if (!localeValue.IsValid()) { |
| 4217 | args.GetReturnValue()->SetString(""); |
| 4218 | return; |
| 4219 | } |
| 4220 | args.GetReturnValue()->SetString( |
| 4221 | FX_UTF8Encode(localeValue.GetValue()).AsStringC()); |
| 4222 | return; |
| 4223 | } |
| 4224 | case XFA_VT_DATE: { |
| 4225 | wsPattern = L"date{" + wsPattern + L"}"; |
| 4226 | CXFA_LocaleValue localeValue(patternType, wsValue, wsPattern, pLocale, |
| 4227 | pMgr); |
| 4228 | if (!localeValue.IsValid()) { |
| 4229 | args.GetReturnValue()->SetString(""); |
| 4230 | return; |
| 4231 | } |
| 4232 | args.GetReturnValue()->SetString( |
| 4233 | FX_UTF8Encode(localeValue.GetValue()).AsStringC()); |
| 4234 | return; |
| 4235 | } |
| 4236 | case XFA_VT_TIME: { |
| 4237 | wsPattern = L"time{" + wsPattern + L"}"; |
| 4238 | CXFA_LocaleValue localeValue(patternType, wsValue, wsPattern, pLocale, |
| 4239 | pMgr); |
| 4240 | if (!localeValue.IsValid()) { |
| 4241 | args.GetReturnValue()->SetString(""); |
| 4242 | return; |
| 4243 | } |
| 4244 | args.GetReturnValue()->SetString( |
| 4245 | FX_UTF8Encode(localeValue.GetValue()).AsStringC()); |
| 4246 | return; |
| 4247 | } |
| 4248 | case XFA_VT_TEXT: { |
| 4249 | wsPattern = L"text{" + wsPattern + L"}"; |
| 4250 | CXFA_LocaleValue localeValue(XFA_VT_TEXT, wsValue, wsPattern, pLocale, |
| 4251 | pMgr); |
| 4252 | if (!localeValue.IsValid()) { |
| 4253 | args.GetReturnValue()->SetString(""); |
| 4254 | return; |
| 4255 | } |
| 4256 | args.GetReturnValue()->SetString( |
| 4257 | FX_UTF8Encode(localeValue.GetValue()).AsStringC()); |
| 4258 | return; |
| 4259 | } |
| 4260 | case XFA_VT_FLOAT: { |
| 4261 | wsPattern = L"num{" + wsPattern + L"}"; |
| 4262 | CXFA_LocaleValue localeValue(XFA_VT_FLOAT, wsValue, wsPattern, pLocale, |
| 4263 | pMgr); |
| 4264 | if (!localeValue.IsValid()) { |
| 4265 | args.GetReturnValue()->SetString(""); |
| 4266 | return; |
| 4267 | } |
| 4268 | args.GetReturnValue()->SetDouble(localeValue.GetDoubleNum()); |
| 4269 | return; |
| 4270 | } |
| 4271 | default: { |
| 4272 | CFX_WideString wsTestPattern; |
| 4273 | wsTestPattern = L"num{" + wsPattern + L"}"; |
| 4274 | CXFA_LocaleValue localeValue(XFA_VT_FLOAT, wsValue, wsTestPattern, |
| 4275 | pLocale, pMgr); |
| 4276 | if (localeValue.IsValid()) { |
| 4277 | args.GetReturnValue()->SetDouble(localeValue.GetDoubleNum()); |
| 4278 | return; |
| 4279 | } |
| 4280 | |
| 4281 | wsTestPattern = L"text{" + wsPattern + L"}"; |
| 4282 | CXFA_LocaleValue localeValue2(XFA_VT_TEXT, wsValue, wsTestPattern, |
| 4283 | pLocale, pMgr); |
| 4284 | if (!localeValue2.IsValid()) { |
| 4285 | args.GetReturnValue()->SetString(""); |
| 4286 | return; |
| 4287 | } |
| 4288 | args.GetReturnValue()->SetString( |
| 4289 | FX_UTF8Encode(localeValue2.GetValue()).AsStringC()); |
| 4290 | return; |
| 4291 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4292 | } |
| 4293 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4294 | |
| 4295 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4296 | void CXFA_FM2JSContext::Replace(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4297 | const CFX_ByteStringC& szFuncName, |
| 4298 | CFXJSE_Arguments& args) { |
| 4299 | int32_t argc = args.GetLength(); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4300 | if (argc < 2 || argc > 3) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4301 | ToJSContext(pThis, nullptr) |
| 4302 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Replace"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4303 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4304 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4305 | |
| 4306 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 4307 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 4308 | CFX_ByteString oneString; |
| 4309 | CFX_ByteString twoString; |
| 4310 | if (!ValueIsNull(pThis, argOne.get()) && !ValueIsNull(pThis, argTwo.get())) { |
| 4311 | ValueToUTF8String(argOne.get(), oneString); |
| 4312 | ValueToUTF8String(argTwo.get(), twoString); |
| 4313 | } |
| 4314 | |
| 4315 | CFX_ByteString threeString; |
| 4316 | if (argc > 2) { |
| 4317 | std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); |
| 4318 | ValueToUTF8String(argThree.get(), threeString); |
| 4319 | } |
| 4320 | |
| 4321 | int32_t iFindLen = twoString.GetLength(); |
| 4322 | CFX_ByteTextBuf resultString; |
| 4323 | int32_t iFindIndex = 0; |
| 4324 | for (int32_t u = 0; u < oneString.GetLength(); ++u) { |
| 4325 | uint8_t ch = oneString.GetAt(u); |
| 4326 | if (ch != twoString.GetAt(iFindIndex)) { |
| 4327 | resultString.AppendChar(ch); |
| 4328 | continue; |
| 4329 | } |
| 4330 | |
| 4331 | int32_t iTemp = u + 1; |
| 4332 | ++iFindIndex; |
| 4333 | while (iFindIndex < iFindLen) { |
| 4334 | uint8_t chTemp = oneString.GetAt(iTemp); |
| 4335 | if (chTemp != twoString.GetAt(iFindIndex)) { |
| 4336 | iFindIndex = 0; |
| 4337 | break; |
| 4338 | } |
| 4339 | |
| 4340 | ++iTemp; |
| 4341 | ++iFindIndex; |
| 4342 | } |
| 4343 | if (iFindIndex == iFindLen) { |
| 4344 | resultString << threeString.AsStringC(); |
| 4345 | u += iFindLen - 1; |
| 4346 | iFindIndex = 0; |
| 4347 | } else { |
| 4348 | resultString.AppendChar(ch); |
| 4349 | } |
| 4350 | } |
| 4351 | resultString.AppendChar(0); |
| 4352 | args.GetReturnValue()->SetString(resultString.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4353 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4354 | |
| 4355 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4356 | void CXFA_FM2JSContext::Right(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4357 | const CFX_ByteStringC& szFuncName, |
| 4358 | CFXJSE_Arguments& args) { |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4359 | if (args.GetLength() != 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4360 | ToJSContext(pThis, nullptr) |
| 4361 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Right"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4362 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4363 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4364 | |
| 4365 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 4366 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 4367 | if ((ValueIsNull(pThis, argOne.get())) || |
| 4368 | (ValueIsNull(pThis, argTwo.get()))) { |
| 4369 | args.GetReturnValue()->SetNull(); |
| 4370 | return; |
| 4371 | } |
| 4372 | |
| 4373 | CFX_ByteString sourceString; |
| 4374 | ValueToUTF8String(argOne.get(), sourceString); |
| 4375 | int32_t count = std::max(0, ValueToInteger(pThis, argTwo.get())); |
| 4376 | args.GetReturnValue()->SetString(sourceString.Right(count).AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4377 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4378 | |
| 4379 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4380 | void CXFA_FM2JSContext::Rtrim(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4381 | const CFX_ByteStringC& szFuncName, |
| 4382 | CFXJSE_Arguments& args) { |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4383 | if (args.GetLength() != 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4384 | ToJSContext(pThis, nullptr) |
| 4385 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Rtrim"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4386 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4387 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4388 | |
| 4389 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 4390 | if (ValueIsNull(pThis, argOne.get())) { |
| 4391 | args.GetReturnValue()->SetNull(); |
| 4392 | return; |
| 4393 | } |
| 4394 | |
| 4395 | CFX_ByteString sourceString; |
| 4396 | ValueToUTF8String(argOne.get(), sourceString); |
| 4397 | sourceString.TrimRight(); |
| 4398 | args.GetReturnValue()->SetString(sourceString.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4399 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4400 | |
| 4401 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4402 | void CXFA_FM2JSContext::Space(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4403 | const CFX_ByteStringC& szFuncName, |
| 4404 | CFXJSE_Arguments& args) { |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4405 | if (args.GetLength() != 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4406 | ToJSContext(pThis, nullptr) |
| 4407 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Space"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4408 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4409 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4410 | |
| 4411 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 4412 | if (argOne->IsNull()) { |
| 4413 | args.GetReturnValue()->SetNull(); |
| 4414 | return; |
| 4415 | } |
| 4416 | |
| 4417 | int32_t count = std::max(0, ValueToInteger(pThis, argOne.get())); |
| 4418 | CFX_ByteTextBuf spaceString; |
| 4419 | int32_t index = 0; |
| 4420 | while (index < count) { |
| 4421 | spaceString.AppendByte(' '); |
| 4422 | index++; |
| 4423 | } |
| 4424 | spaceString.AppendByte(0); |
| 4425 | args.GetReturnValue()->SetString(spaceString.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4426 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4427 | |
| 4428 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4429 | void CXFA_FM2JSContext::Str(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4430 | const CFX_ByteStringC& szFuncName, |
| 4431 | CFXJSE_Arguments& args) { |
| 4432 | int32_t argc = args.GetLength(); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4433 | if (argc < 1 || argc > 3) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4434 | ToJSContext(pThis, nullptr) |
| 4435 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Str"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4436 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4437 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4438 | |
| 4439 | std::unique_ptr<CFXJSE_Value> numberValue = GetSimpleValue(pThis, args, 0); |
| 4440 | if (numberValue->IsNull()) { |
| 4441 | args.GetReturnValue()->SetNull(); |
| 4442 | return; |
| 4443 | } |
| 4444 | FX_FLOAT fNumber = ValueToFloat(pThis, numberValue.get()); |
| 4445 | |
| 4446 | int32_t iWidth = 10; |
| 4447 | if (argc > 1) { |
| 4448 | std::unique_ptr<CFXJSE_Value> widthValue = GetSimpleValue(pThis, args, 1); |
| 4449 | iWidth = static_cast<int32_t>(ValueToFloat(pThis, widthValue.get())); |
| 4450 | } |
| 4451 | |
| 4452 | int32_t iPrecision = 0; |
| 4453 | if (argc > 2) { |
| 4454 | std::unique_ptr<CFXJSE_Value> precisionValue = |
| 4455 | GetSimpleValue(pThis, args, 2); |
| 4456 | iPrecision = std::max( |
| 4457 | 0, static_cast<int32_t>(ValueToFloat(pThis, precisionValue.get()))); |
| 4458 | } |
| 4459 | |
| 4460 | CFX_ByteString numberString; |
| 4461 | CFX_ByteString formatStr = "%"; |
| 4462 | if (iPrecision) { |
| 4463 | formatStr += "."; |
| 4464 | formatStr += CFX_ByteString::FormatInteger(iPrecision); |
| 4465 | } |
| 4466 | formatStr += "f"; |
| 4467 | numberString.Format(formatStr.c_str(), fNumber); |
| 4468 | |
| 4469 | const FX_CHAR* pData = numberString.c_str(); |
| 4470 | int32_t iLength = numberString.GetLength(); |
| 4471 | int32_t u = 0; |
| 4472 | while (u < iLength) { |
| 4473 | if (pData[u] == '.') |
| 4474 | break; |
| 4475 | |
| 4476 | ++u; |
| 4477 | } |
| 4478 | |
| 4479 | CFX_ByteTextBuf resultBuf; |
| 4480 | if (u > iWidth || (iPrecision + u) >= iWidth) { |
| 4481 | int32_t i = 0; |
| 4482 | while (i < iWidth) { |
| 4483 | resultBuf.AppendChar('*'); |
| 4484 | ++i; |
| 4485 | } |
| 4486 | resultBuf.AppendChar(0); |
| 4487 | args.GetReturnValue()->SetString(resultBuf.AsStringC()); |
| 4488 | return; |
| 4489 | } |
| 4490 | |
| 4491 | if (u == iLength) { |
| 4492 | if (iLength > iWidth) { |
| 4493 | int32_t i = 0; |
| 4494 | while (i < iWidth) { |
| 4495 | resultBuf.AppendChar('*'); |
| 4496 | ++i; |
| 4497 | } |
| 4498 | } else { |
| 4499 | int32_t i = 0; |
| 4500 | while (i < iWidth - iLength) { |
| 4501 | resultBuf.AppendChar(' '); |
| 4502 | ++i; |
| 4503 | } |
| 4504 | resultBuf << pData; |
| 4505 | } |
| 4506 | args.GetReturnValue()->SetString(resultBuf.AsStringC()); |
| 4507 | return; |
| 4508 | } |
| 4509 | |
| 4510 | int32_t iLeavingSpace = iWidth - u - iPrecision; |
| 4511 | if (iPrecision != 0) |
| 4512 | iLeavingSpace--; |
| 4513 | |
| 4514 | int32_t i = 0; |
| 4515 | while (i < iLeavingSpace) { |
| 4516 | resultBuf.AppendChar(' '); |
| 4517 | ++i; |
| 4518 | } |
| 4519 | i = 0; |
| 4520 | while (i < u) { |
| 4521 | resultBuf.AppendChar(pData[i]); |
| 4522 | ++i; |
| 4523 | } |
| 4524 | if (iPrecision != 0) |
| 4525 | resultBuf.AppendChar('.'); |
| 4526 | |
| 4527 | u++; |
| 4528 | i = 0; |
| 4529 | while (u < iLength) { |
| 4530 | if (i >= iPrecision) |
| 4531 | break; |
| 4532 | |
| 4533 | resultBuf.AppendChar(pData[u]); |
| 4534 | ++i; |
| 4535 | ++u; |
| 4536 | } |
| 4537 | while (i < iPrecision) { |
| 4538 | resultBuf.AppendChar('0'); |
| 4539 | ++i; |
| 4540 | } |
| 4541 | resultBuf.AppendChar(0); |
| 4542 | args.GetReturnValue()->SetString(resultBuf.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4543 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4544 | |
| 4545 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4546 | void CXFA_FM2JSContext::Stuff(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4547 | const CFX_ByteStringC& szFuncName, |
| 4548 | CFXJSE_Arguments& args) { |
| 4549 | int32_t argc = args.GetLength(); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4550 | if (argc < 3 || argc > 4) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4551 | ToJSContext(pThis, nullptr) |
| 4552 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Stuff"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4553 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4554 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4555 | |
| 4556 | CFX_ByteString sourceString; |
| 4557 | CFX_ByteString insertString; |
| 4558 | int32_t iLength = 0; |
| 4559 | int32_t iStart = 0; |
| 4560 | int32_t iDelete = 0; |
| 4561 | std::unique_ptr<CFXJSE_Value> sourceValue = GetSimpleValue(pThis, args, 0); |
| 4562 | std::unique_ptr<CFXJSE_Value> startValue = GetSimpleValue(pThis, args, 1); |
| 4563 | std::unique_ptr<CFXJSE_Value> deleteValue = GetSimpleValue(pThis, args, 2); |
| 4564 | if (!sourceValue->IsNull() && !startValue->IsNull() && |
| 4565 | !deleteValue->IsNull()) { |
| 4566 | ValueToUTF8String(sourceValue.get(), sourceString); |
| 4567 | iLength = sourceString.GetLength(); |
| 4568 | iStart = std::min(iLength, std::max(1, static_cast<int32_t>(ValueToFloat( |
| 4569 | pThis, startValue.get())))); |
| 4570 | iDelete = std::max( |
| 4571 | 0, static_cast<int32_t>(ValueToFloat(pThis, deleteValue.get()))); |
| 4572 | } |
| 4573 | |
| 4574 | if (argc > 3) { |
| 4575 | std::unique_ptr<CFXJSE_Value> insertValue = GetSimpleValue(pThis, args, 3); |
| 4576 | ValueToUTF8String(insertValue.get(), insertString); |
| 4577 | } |
| 4578 | |
| 4579 | iStart -= 1; |
| 4580 | CFX_ByteTextBuf resultString; |
| 4581 | int32_t i = 0; |
| 4582 | while (i < iStart) { |
| 4583 | resultString.AppendChar(sourceString.GetAt(i)); |
| 4584 | ++i; |
| 4585 | } |
| 4586 | resultString << insertString.AsStringC(); |
| 4587 | i = iStart + iDelete; |
| 4588 | while (i < iLength) { |
| 4589 | resultString.AppendChar(sourceString.GetAt(i)); |
| 4590 | ++i; |
| 4591 | } |
| 4592 | resultString.AppendChar(0); |
| 4593 | args.GetReturnValue()->SetString(resultString.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4594 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4595 | |
| 4596 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4597 | void CXFA_FM2JSContext::Substr(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4598 | const CFX_ByteStringC& szFuncName, |
| 4599 | CFXJSE_Arguments& args) { |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4600 | if (args.GetLength() != 3) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4601 | ToJSContext(pThis, nullptr) |
| 4602 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Substr"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4603 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4604 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4605 | |
| 4606 | std::unique_ptr<CFXJSE_Value> stringValue = GetSimpleValue(pThis, args, 0); |
| 4607 | std::unique_ptr<CFXJSE_Value> startValue = GetSimpleValue(pThis, args, 1); |
| 4608 | std::unique_ptr<CFXJSE_Value> endValue = GetSimpleValue(pThis, args, 2); |
| 4609 | if (ValueIsNull(pThis, stringValue.get()) || |
| 4610 | (ValueIsNull(pThis, startValue.get())) || |
| 4611 | (ValueIsNull(pThis, endValue.get()))) { |
| 4612 | args.GetReturnValue()->SetNull(); |
| 4613 | return; |
| 4614 | } |
| 4615 | |
| 4616 | CFX_ByteString szSourceStr; |
| 4617 | int32_t iStart = 0; |
| 4618 | int32_t iCount = 0; |
| 4619 | ValueToUTF8String(stringValue.get(), szSourceStr); |
| 4620 | int32_t iLength = szSourceStr.GetLength(); |
| 4621 | if (iLength == 0) { |
| 4622 | args.GetReturnValue()->SetString(""); |
| 4623 | return; |
| 4624 | } |
| 4625 | |
| 4626 | iStart = std::min( |
| 4627 | iLength, |
| 4628 | std::max(1, static_cast<int32_t>(ValueToFloat(pThis, startValue.get())))); |
| 4629 | iCount = |
| 4630 | std::max(0, static_cast<int32_t>(ValueToFloat(pThis, endValue.get()))); |
| 4631 | |
| 4632 | iStart -= 1; |
| 4633 | args.GetReturnValue()->SetString(szSourceStr.Mid(iStart, iCount).AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4634 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4635 | |
| 4636 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4637 | void CXFA_FM2JSContext::Uuid(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4638 | const CFX_ByteStringC& szFuncName, |
| 4639 | CFXJSE_Arguments& args) { |
| 4640 | int32_t argc = args.GetLength(); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4641 | if (argc < 0 || argc > 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4642 | ToJSContext(pThis, nullptr) |
| 4643 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Uuid"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4644 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4645 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4646 | |
| 4647 | int32_t iNum = 0; |
| 4648 | if (argc > 0) { |
| 4649 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 4650 | iNum = static_cast<int32_t>(ValueToFloat(pThis, argOne.get())); |
| 4651 | } |
| 4652 | FX_GUID guid; |
| 4653 | FX_GUID_CreateV4(&guid); |
| 4654 | |
| 4655 | CFX_ByteString bsUId; |
tsepez | 478ed62 | 2016-10-27 14:32:33 -0700 | [diff] [blame^] | 4656 | FX_GUID_ToString(&guid, bsUId, !!iNum); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4657 | args.GetReturnValue()->SetString(bsUId.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4658 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4659 | |
| 4660 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4661 | void CXFA_FM2JSContext::Upper(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4662 | const CFX_ByteStringC& szFuncName, |
| 4663 | CFXJSE_Arguments& args) { |
| 4664 | int32_t argc = args.GetLength(); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4665 | if (argc < 1 || argc > 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4666 | ToJSContext(pThis, nullptr) |
| 4667 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Upper"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4668 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4669 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4670 | |
| 4671 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 4672 | if (ValueIsNull(pThis, argOne.get())) { |
| 4673 | args.GetReturnValue()->SetNull(); |
| 4674 | return; |
| 4675 | } |
| 4676 | |
| 4677 | CFX_ByteString argString; |
| 4678 | ValueToUTF8String(argOne.get(), argString); |
| 4679 | |
| 4680 | CFX_WideTextBuf upperStringBuf; |
| 4681 | CFX_WideString wsArgString = CFX_WideString::FromUTF8(argString.AsStringC()); |
| 4682 | const FX_WCHAR* pData = wsArgString.c_str(); |
| 4683 | int32_t i = 0; |
| 4684 | while (i < wsArgString.GetLength()) { |
| 4685 | int32_t ch = pData[i]; |
| 4686 | if ((ch >= 0x61 && ch <= 0x7A) || (ch >= 0xE0 && ch <= 0xFE)) |
| 4687 | ch -= 32; |
| 4688 | else if (ch == 0x101 || ch == 0x103 || ch == 0x105) |
| 4689 | ch -= 1; |
| 4690 | |
| 4691 | upperStringBuf.AppendChar(ch); |
| 4692 | ++i; |
| 4693 | } |
| 4694 | upperStringBuf.AppendChar(0); |
| 4695 | |
| 4696 | args.GetReturnValue()->SetString( |
| 4697 | FX_UTF8Encode(upperStringBuf.GetBuffer(), upperStringBuf.GetLength()) |
| 4698 | .AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4699 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4700 | |
| 4701 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4702 | void CXFA_FM2JSContext::WordNum(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4703 | const CFX_ByteStringC& szFuncName, |
| 4704 | CFXJSE_Arguments& args) { |
| 4705 | int32_t argc = args.GetLength(); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4706 | if (argc < 1 || argc > 3) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4707 | ToJSContext(pThis, nullptr) |
| 4708 | ->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"WordNum"); |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4709 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4710 | } |
dsinclair | cf7f322 | 2016-06-08 20:34:02 -0700 | [diff] [blame] | 4711 | |
| 4712 | std::unique_ptr<CFXJSE_Value> numberValue = GetSimpleValue(pThis, args, 0); |
| 4713 | if (numberValue->IsNull()) { |
| 4714 | args.GetReturnValue()->SetNull(); |
| 4715 | return; |
| 4716 | } |
| 4717 | FX_FLOAT fNumber = ValueToFloat(pThis, numberValue.get()); |
| 4718 | |
| 4719 | int32_t iIdentifier = 0; |
| 4720 | if (argc > 1) { |
| 4721 | std::unique_ptr<CFXJSE_Value> identifierValue = |
| 4722 | GetSimpleValue(pThis, args, 1); |
| 4723 | if (identifierValue->IsNull()) { |
| 4724 | args.GetReturnValue()->SetNull(); |
| 4725 | return; |
| 4726 | } |
| 4727 | iIdentifier = |
| 4728 | static_cast<int32_t>(ValueToFloat(pThis, identifierValue.get())); |
| 4729 | } |
| 4730 | |
| 4731 | CFX_ByteString localeString; |
| 4732 | if (argc > 2) { |
| 4733 | std::unique_ptr<CFXJSE_Value> localeValue = GetSimpleValue(pThis, args, 2); |
| 4734 | if (localeValue->IsNull()) { |
| 4735 | args.GetReturnValue()->SetNull(); |
| 4736 | return; |
| 4737 | } |
| 4738 | ValueToUTF8String(localeValue.get(), localeString); |
| 4739 | } |
| 4740 | |
| 4741 | if (fNumber < 0.0f || fNumber > 922337203685477550.0f) { |
| 4742 | args.GetReturnValue()->SetString("*"); |
| 4743 | return; |
| 4744 | } |
| 4745 | |
| 4746 | CFX_ByteString numberString; |
| 4747 | numberString.Format("%.2f", fNumber); |
| 4748 | |
| 4749 | CFX_ByteTextBuf resultBuf; |
| 4750 | WordUS(numberString.AsStringC(), iIdentifier, resultBuf); |
| 4751 | args.GetReturnValue()->SetString(resultBuf.AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4752 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4753 | |
| 4754 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4755 | void CXFA_FM2JSContext::TrillionUS(const CFX_ByteStringC& szData, |
| 4756 | CFX_ByteTextBuf& strBuf) { |
| 4757 | CFX_ByteStringC pUnits[] = {"zero", "one", "two", "three", "four", |
| 4758 | "five", "six", "seven", "eight", "nine"}; |
| 4759 | CFX_ByteStringC pCapUnits[] = {"Zero", "One", "Two", "Three", "Four", |
| 4760 | "Five", "Six", "Seven", "Eight", "Nine"}; |
| 4761 | CFX_ByteStringC pTens[] = {"Ten", "Eleven", "Twelve", "Thirteen", |
| 4762 | "Fourteen", "Fifteen", "Sixteen", "Seventeen", |
| 4763 | "Eighteen", "Nineteen"}; |
| 4764 | CFX_ByteStringC pLastTens[] = {"Twenty", "Thirty", "Forty", "Fifty", |
| 4765 | "Sixty", "Seventy", "Eighty", "Ninety"}; |
| 4766 | CFX_ByteStringC pComm[] = {" Hundred ", " Thousand ", " Million ", |
| 4767 | " Billion ", "Trillion"}; |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 4768 | const FX_CHAR* pData = szData.c_str(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4769 | int32_t iLength = szData.GetLength(); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4770 | int32_t iComm = 0; |
| 4771 | if (iLength > 12) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4772 | iComm = 4; |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4773 | else if (iLength > 9) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4774 | iComm = 3; |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4775 | else if (iLength > 6) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4776 | iComm = 2; |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4777 | else if (iLength > 3) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4778 | iComm = 1; |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4779 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4780 | int32_t iFirstCount = iLength % 3; |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4781 | if (iFirstCount == 0) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4782 | iFirstCount = 3; |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4783 | |
| 4784 | int32_t iIndex = 0; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4785 | if (iFirstCount == 3) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 4786 | if (pData[iIndex] != '0') { |
| 4787 | strBuf << pCapUnits[pData[iIndex] - '0']; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4788 | strBuf << pComm[0]; |
| 4789 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 4790 | if (pData[iIndex + 1] == '0') { |
| 4791 | strBuf << pCapUnits[pData[iIndex + 2] - '0']; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4792 | } else { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 4793 | if (pData[iIndex + 1] > '1') { |
| 4794 | strBuf << pLastTens[pData[iIndex + 1] - '2']; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4795 | strBuf << "-"; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 4796 | strBuf << pUnits[pData[iIndex + 2] - '0']; |
| 4797 | } else if (pData[iIndex + 1] == '1') { |
| 4798 | strBuf << pTens[pData[iIndex + 2] - '0']; |
| 4799 | } else if (pData[iIndex + 1] == '0') { |
| 4800 | strBuf << pCapUnits[pData[iIndex + 2] - '0']; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4801 | } |
| 4802 | } |
| 4803 | iIndex += 3; |
| 4804 | } else if (iFirstCount == 2) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 4805 | if (pData[iIndex] == '0') { |
| 4806 | strBuf << pCapUnits[pData[iIndex + 1] - '0']; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4807 | } else { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 4808 | if (pData[iIndex] > '1') { |
| 4809 | strBuf << pLastTens[pData[iIndex] - '2']; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4810 | strBuf << "-"; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 4811 | strBuf << pUnits[pData[iIndex + 1] - '0']; |
| 4812 | } else if (pData[iIndex] == '1') { |
| 4813 | strBuf << pTens[pData[iIndex + 1] - '0']; |
| 4814 | } else if (pData[iIndex] == '0') { |
| 4815 | strBuf << pCapUnits[pData[iIndex + 1] - '0']; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4816 | } |
| 4817 | } |
| 4818 | iIndex += 2; |
| 4819 | } else if (iFirstCount == 1) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 4820 | strBuf << pCapUnits[pData[iIndex] - '0']; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4821 | iIndex += 1; |
| 4822 | } |
| 4823 | if (iLength > 3 && iFirstCount > 0) { |
| 4824 | strBuf << pComm[iComm]; |
| 4825 | --iComm; |
| 4826 | } |
| 4827 | while (iIndex < iLength) { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 4828 | if (pData[iIndex] != '0') { |
| 4829 | strBuf << pCapUnits[pData[iIndex] - '0']; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4830 | strBuf << pComm[0]; |
| 4831 | } |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 4832 | if (pData[iIndex + 1] == '0') { |
| 4833 | strBuf << pCapUnits[pData[iIndex + 2] - '0']; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4834 | } else { |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 4835 | if (pData[iIndex + 1] > '1') { |
| 4836 | strBuf << pLastTens[pData[iIndex + 1] - '2']; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4837 | strBuf << "-"; |
dsinclair | 2b6d64e | 2016-06-06 11:39:42 -0700 | [diff] [blame] | 4838 | strBuf << pUnits[pData[iIndex + 2] - '0']; |
| 4839 | } else if (pData[iIndex + 1] == '1') { |
| 4840 | strBuf << pTens[pData[iIndex + 2] - '0']; |
| 4841 | } else if (pData[iIndex + 1] == '0') { |
| 4842 | strBuf << pCapUnits[pData[iIndex + 2] - '0']; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4843 | } |
| 4844 | } |
| 4845 | if (iIndex < iLength - 3) { |
| 4846 | strBuf << pComm[iComm]; |
| 4847 | --iComm; |
| 4848 | } |
| 4849 | iIndex += 3; |
| 4850 | } |
| 4851 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4852 | |
| 4853 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4854 | void CXFA_FM2JSContext::WordUS(const CFX_ByteStringC& szData, |
| 4855 | int32_t iStyle, |
| 4856 | CFX_ByteTextBuf& strBuf) { |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 4857 | const FX_CHAR* pData = szData.c_str(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4858 | int32_t iLength = szData.GetLength(); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4859 | if (iStyle < 0 || iStyle > 2) { |
| 4860 | return; |
| 4861 | } |
| 4862 | |
| 4863 | int32_t iIndex = 0; |
| 4864 | while (iIndex < iLength) { |
| 4865 | if (pData[iIndex] == '.') |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4866 | break; |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4867 | ++iIndex; |
| 4868 | } |
| 4869 | int32_t iInteger = iIndex; |
| 4870 | iIndex = 0; |
| 4871 | while (iIndex < iInteger) { |
| 4872 | int32_t iCount = (iInteger - iIndex) % 12; |
| 4873 | if (!iCount && iInteger - iIndex > 0) |
| 4874 | iCount = 12; |
| 4875 | |
| 4876 | TrillionUS(CFX_ByteStringC(pData + iIndex, iCount), strBuf); |
| 4877 | iIndex += iCount; |
| 4878 | if (iIndex < iInteger) |
| 4879 | strBuf << " Trillion "; |
| 4880 | } |
| 4881 | |
| 4882 | if (iStyle > 0) |
| 4883 | strBuf << " Dollars"; |
| 4884 | |
| 4885 | if (iStyle > 1 && iInteger < iLength) { |
| 4886 | strBuf << " And "; |
| 4887 | iIndex = iInteger + 1; |
| 4888 | while (iIndex < iLength) { |
| 4889 | int32_t iCount = (iLength - iIndex) % 12; |
| 4890 | if (!iCount && iLength - iIndex > 0) |
| 4891 | iCount = 12; |
| 4892 | |
| 4893 | TrillionUS(CFX_ByteStringC(pData + iIndex, iCount), strBuf); |
| 4894 | iIndex += iCount; |
| 4895 | if (iIndex < iLength) |
| 4896 | strBuf << " Trillion "; |
| 4897 | } |
| 4898 | strBuf << " Cents"; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4899 | } |
| 4900 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4901 | |
| 4902 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4903 | void CXFA_FM2JSContext::Get(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4904 | const CFX_ByteStringC& szFuncName, |
| 4905 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4906 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4907 | if (args.GetLength() != 1) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 4908 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Get"); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4909 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4910 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4911 | |
| 4912 | CXFA_Document* pDoc = pContext->GetDocument(); |
| 4913 | if (!pDoc) |
| 4914 | return; |
| 4915 | |
dsinclair | a1b0772 | 2016-07-11 08:20:58 -0700 | [diff] [blame] | 4916 | IXFA_AppProvider* pAppProvider = pDoc->GetNotify()->GetAppProvider(); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4917 | if (!pAppProvider) |
| 4918 | return; |
| 4919 | |
| 4920 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 4921 | CFX_ByteString urlString; |
| 4922 | ValueToUTF8String(argOne.get(), urlString); |
tsepez | ad2441e | 2016-10-24 10:19:11 -0700 | [diff] [blame] | 4923 | IFX_SeekableReadStream* pFile = pAppProvider->DownloadURL( |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4924 | CFX_WideString::FromUTF8(urlString.AsStringC())); |
| 4925 | if (!pFile) |
| 4926 | return; |
| 4927 | |
| 4928 | int32_t size = pFile->GetSize(); |
| 4929 | std::unique_ptr<uint8_t> pData(FX_Alloc(uint8_t, size)); |
| 4930 | pFile->ReadBlock(pData.get(), size); |
| 4931 | args.GetReturnValue()->SetString(CFX_ByteStringC(pData.get(), size)); |
| 4932 | pFile->Release(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4933 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4934 | |
| 4935 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4936 | void CXFA_FM2JSContext::Post(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4937 | const CFX_ByteStringC& szFuncName, |
| 4938 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 4939 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4940 | int32_t argc = args.GetLength(); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4941 | if (argc < 2 || argc > 5) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 4942 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Post"); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4943 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4944 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4945 | |
| 4946 | CXFA_Document* pDoc = pContext->GetDocument(); |
| 4947 | if (!pDoc) |
| 4948 | return; |
| 4949 | |
dsinclair | a1b0772 | 2016-07-11 08:20:58 -0700 | [diff] [blame] | 4950 | IXFA_AppProvider* pAppProvider = pDoc->GetNotify()->GetAppProvider(); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 4951 | if (!pAppProvider) |
| 4952 | return; |
| 4953 | |
| 4954 | CFX_ByteString bsURL; |
| 4955 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 4956 | ValueToUTF8String(argOne.get(), bsURL); |
| 4957 | |
| 4958 | CFX_ByteString bsData; |
| 4959 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 4960 | ValueToUTF8String(argTwo.get(), bsData); |
| 4961 | |
| 4962 | CFX_ByteString bsContentType; |
| 4963 | if (argc > 2) { |
| 4964 | std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); |
| 4965 | ValueToUTF8String(argThree.get(), bsContentType); |
| 4966 | } |
| 4967 | |
| 4968 | CFX_ByteString bsEncode; |
| 4969 | if (argc > 3) { |
| 4970 | std::unique_ptr<CFXJSE_Value> argFour = GetSimpleValue(pThis, args, 3); |
| 4971 | ValueToUTF8String(argFour.get(), bsEncode); |
| 4972 | } |
| 4973 | |
| 4974 | CFX_ByteString bsHeader; |
| 4975 | if (argc > 4) { |
| 4976 | std::unique_ptr<CFXJSE_Value> argFive = GetSimpleValue(pThis, args, 4); |
| 4977 | ValueToUTF8String(argFive.get(), bsHeader); |
| 4978 | } |
| 4979 | |
| 4980 | CFX_WideString decodedResponse; |
| 4981 | if (!pAppProvider->PostRequestURL( |
| 4982 | CFX_WideString::FromUTF8(bsURL.AsStringC()), |
| 4983 | CFX_WideString::FromUTF8(bsData.AsStringC()), |
| 4984 | CFX_WideString::FromUTF8(bsContentType.AsStringC()), |
| 4985 | CFX_WideString::FromUTF8(bsEncode.AsStringC()), |
| 4986 | CFX_WideString::FromUTF8(bsHeader.AsStringC()), decodedResponse)) { |
| 4987 | pContext->ThrowException(XFA_IDS_SERVER_DENY); |
| 4988 | return; |
| 4989 | } |
| 4990 | |
| 4991 | args.GetReturnValue()->SetString( |
| 4992 | FX_UTF8Encode(decodedResponse.c_str(), decodedResponse.GetLength()) |
| 4993 | .AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4994 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 4995 | |
| 4996 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 4997 | void CXFA_FM2JSContext::Put(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 4998 | const CFX_ByteStringC& szFuncName, |
| 4999 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5000 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5001 | int32_t argc = args.GetLength(); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5002 | if (argc < 2 || argc > 3) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 5003 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Put"); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5004 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5005 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5006 | |
| 5007 | CXFA_Document* pDoc = pContext->GetDocument(); |
| 5008 | if (!pDoc) |
| 5009 | return; |
| 5010 | |
dsinclair | a1b0772 | 2016-07-11 08:20:58 -0700 | [diff] [blame] | 5011 | IXFA_AppProvider* pAppProvider = pDoc->GetNotify()->GetAppProvider(); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5012 | if (!pAppProvider) |
| 5013 | return; |
| 5014 | |
| 5015 | CFX_ByteString bsURL; |
| 5016 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 5017 | ValueToUTF8String(argOne.get(), bsURL); |
| 5018 | |
| 5019 | CFX_ByteString bsData; |
| 5020 | std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); |
| 5021 | ValueToUTF8String(argTwo.get(), bsData); |
| 5022 | |
| 5023 | CFX_ByteString bsEncode; |
| 5024 | if (argc > 2) { |
| 5025 | std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); |
| 5026 | ValueToUTF8String(argThree.get(), bsEncode); |
| 5027 | } |
| 5028 | |
| 5029 | if (!pAppProvider->PutRequestURL( |
| 5030 | CFX_WideString::FromUTF8(bsURL.AsStringC()), |
| 5031 | CFX_WideString::FromUTF8(bsData.AsStringC()), |
| 5032 | CFX_WideString::FromUTF8(bsEncode.AsStringC()))) { |
| 5033 | pContext->ThrowException(XFA_IDS_SERVER_DENY); |
| 5034 | return; |
| 5035 | } |
| 5036 | |
| 5037 | args.GetReturnValue()->SetString(""); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5038 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5039 | |
| 5040 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5041 | void CXFA_FM2JSContext::assign_value_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5042 | const CFX_ByteStringC& szFuncName, |
| 5043 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5044 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5045 | if (args.GetLength() != 2) { |
| 5046 | pContext->ThrowException(XFA_IDS_COMPILER_ERROR); |
| 5047 | return; |
| 5048 | } |
| 5049 | |
| 5050 | std::unique_ptr<CFXJSE_Value> lValue = args.GetValue(0); |
| 5051 | std::unique_ptr<CFXJSE_Value> rValue = GetSimpleValue(pThis, args, 1); |
| 5052 | if (lValue->IsArray()) { |
| 5053 | v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
| 5054 | std::unique_ptr<CFXJSE_Value> leftLengthValue(new CFXJSE_Value(pIsolate)); |
| 5055 | lValue->GetObjectProperty("length", leftLengthValue.get()); |
| 5056 | int32_t iLeftLength = leftLengthValue->ToInteger(); |
| 5057 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 5058 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 5059 | lValue->GetObjectPropertyByIdx(1, propertyValue.get()); |
| 5060 | if (propertyValue->IsNull()) { |
| 5061 | for (int32_t i = 2; i < iLeftLength; i++) { |
| 5062 | lValue->GetObjectPropertyByIdx(i, jsObjectValue.get()); |
| 5063 | if (!SetObjectDefaultValue(jsObjectValue.get(), rValue.get())) { |
| 5064 | pContext->ThrowException(XFA_IDS_NOT_DEFAUL_VALUE); |
| 5065 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5066 | } |
| 5067 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5068 | } else { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5069 | for (int32_t i = 2; i < iLeftLength; i++) { |
| 5070 | lValue->GetObjectPropertyByIdx(i, jsObjectValue.get()); |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 5071 | jsObjectValue->SetObjectProperty(propertyValue->ToString().AsStringC(), |
| 5072 | rValue.get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5073 | } |
| 5074 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5075 | } else if (lValue->IsObject()) { |
| 5076 | if (!SetObjectDefaultValue(lValue.get(), rValue.get())) { |
| 5077 | pContext->ThrowException(XFA_IDS_NOT_DEFAUL_VALUE); |
| 5078 | return; |
| 5079 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5080 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5081 | args.GetReturnValue()->Assign(rValue.get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5082 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5083 | |
| 5084 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5085 | void CXFA_FM2JSContext::logical_or_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5086 | const CFX_ByteStringC& szFuncName, |
| 5087 | CFXJSE_Arguments& args) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5088 | if (args.GetLength() != 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5089 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5090 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5091 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5092 | |
| 5093 | std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); |
| 5094 | std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); |
| 5095 | if (argFirst->IsNull() && argSecond->IsNull()) { |
| 5096 | args.GetReturnValue()->SetNull(); |
| 5097 | return; |
| 5098 | } |
| 5099 | |
| 5100 | FX_FLOAT first = ValueToFloat(pThis, argFirst.get()); |
| 5101 | FX_FLOAT second = ValueToFloat(pThis, argSecond.get()); |
| 5102 | args.GetReturnValue()->SetInteger((first || second) ? 1 : 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5103 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5104 | |
| 5105 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5106 | void CXFA_FM2JSContext::logical_and_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5107 | const CFX_ByteStringC& szFuncName, |
| 5108 | CFXJSE_Arguments& args) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5109 | if (args.GetLength() != 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5110 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5111 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5112 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5113 | |
| 5114 | std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); |
| 5115 | std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); |
| 5116 | if (argFirst->IsNull() && argSecond->IsNull()) { |
| 5117 | args.GetReturnValue()->SetNull(); |
| 5118 | return; |
| 5119 | } |
| 5120 | |
| 5121 | FX_FLOAT first = ValueToFloat(pThis, argFirst.get()); |
| 5122 | FX_FLOAT second = ValueToFloat(pThis, argSecond.get()); |
| 5123 | args.GetReturnValue()->SetInteger((first && second) ? 1 : 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5124 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5125 | |
| 5126 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5127 | void CXFA_FM2JSContext::equality_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5128 | const CFX_ByteStringC& szFuncName, |
| 5129 | CFXJSE_Arguments& args) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5130 | if (args.GetLength() != 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5131 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5132 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5133 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5134 | |
| 5135 | if (fm_ref_equal(pThis, args)) { |
| 5136 | args.GetReturnValue()->SetInteger(1); |
| 5137 | return; |
| 5138 | } |
| 5139 | |
| 5140 | std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); |
| 5141 | std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); |
| 5142 | if (argFirst->IsNull() || argSecond->IsNull()) { |
| 5143 | args.GetReturnValue()->SetInteger( |
| 5144 | (argFirst->IsNull() && argSecond->IsNull()) ? 1 : 0); |
| 5145 | return; |
| 5146 | } |
| 5147 | |
| 5148 | if (argFirst->IsString() && argSecond->IsString()) { |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 5149 | args.GetReturnValue()->SetInteger(argFirst->ToString() == |
| 5150 | argSecond->ToString()); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5151 | return; |
| 5152 | } |
| 5153 | |
| 5154 | FX_DOUBLE first = ValueToDouble(pThis, argFirst.get()); |
| 5155 | FX_DOUBLE second = ValueToDouble(pThis, argSecond.get()); |
| 5156 | args.GetReturnValue()->SetInteger((first == second) ? 1 : 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5157 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5158 | |
| 5159 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5160 | void CXFA_FM2JSContext::notequality_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5161 | const CFX_ByteStringC& szFuncName, |
| 5162 | CFXJSE_Arguments& args) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5163 | if (args.GetLength() != 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5164 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5165 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5166 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5167 | |
| 5168 | if (fm_ref_equal(pThis, args)) { |
| 5169 | args.GetReturnValue()->SetInteger(0); |
| 5170 | return; |
| 5171 | } |
| 5172 | |
| 5173 | std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); |
| 5174 | std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); |
| 5175 | if (argFirst->IsNull() || argSecond->IsNull()) { |
| 5176 | args.GetReturnValue()->SetInteger( |
| 5177 | (argFirst->IsNull() && argSecond->IsNull()) ? 0 : 1); |
| 5178 | return; |
| 5179 | } |
| 5180 | |
| 5181 | if (argFirst->IsString() && argSecond->IsString()) { |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 5182 | args.GetReturnValue()->SetInteger(argFirst->ToString() != |
| 5183 | argSecond->ToString()); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5184 | return; |
| 5185 | } |
| 5186 | |
| 5187 | FX_DOUBLE first = ValueToDouble(pThis, argFirst.get()); |
| 5188 | FX_DOUBLE second = ValueToDouble(pThis, argSecond.get()); |
| 5189 | args.GetReturnValue()->SetInteger(first != second); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5190 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5191 | |
| 5192 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5193 | FX_BOOL CXFA_FM2JSContext::fm_ref_equal(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5194 | CFXJSE_Arguments& args) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 5195 | std::unique_ptr<CFXJSE_Value> argFirst = args.GetValue(0); |
| 5196 | std::unique_ptr<CFXJSE_Value> argSecond = args.GetValue(1); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5197 | if (!argFirst->IsArray() || !argSecond->IsArray()) |
| 5198 | return FALSE; |
| 5199 | |
| 5200 | v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); |
| 5201 | std::unique_ptr<CFXJSE_Value> firstFlagValue(new CFXJSE_Value(pIsolate)); |
| 5202 | std::unique_ptr<CFXJSE_Value> secondFlagValue(new CFXJSE_Value(pIsolate)); |
| 5203 | argFirst->GetObjectPropertyByIdx(0, firstFlagValue.get()); |
| 5204 | argSecond->GetObjectPropertyByIdx(0, secondFlagValue.get()); |
| 5205 | if (firstFlagValue->ToInteger() != 3 || secondFlagValue->ToInteger() != 3) |
| 5206 | return FALSE; |
| 5207 | |
| 5208 | std::unique_ptr<CFXJSE_Value> firstJSObject(new CFXJSE_Value(pIsolate)); |
| 5209 | std::unique_ptr<CFXJSE_Value> secondJSObject(new CFXJSE_Value(pIsolate)); |
| 5210 | argFirst->GetObjectPropertyByIdx(2, firstJSObject.get()); |
| 5211 | argSecond->GetObjectPropertyByIdx(2, secondJSObject.get()); |
| 5212 | if (firstJSObject->IsNull() || secondJSObject->IsNull()) |
| 5213 | return FALSE; |
| 5214 | |
| 5215 | return (firstJSObject->ToHostObject(nullptr) == |
| 5216 | secondJSObject->ToHostObject(nullptr)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5217 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5218 | |
| 5219 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5220 | void CXFA_FM2JSContext::less_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5221 | const CFX_ByteStringC& szFuncName, |
| 5222 | CFXJSE_Arguments& args) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5223 | if (args.GetLength() != 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5224 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5225 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5226 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5227 | |
| 5228 | std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); |
| 5229 | std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); |
| 5230 | if (argFirst->IsNull() || argSecond->IsNull()) { |
| 5231 | args.GetReturnValue()->SetInteger(0); |
| 5232 | return; |
| 5233 | } |
| 5234 | |
| 5235 | if (argFirst->IsString() && argSecond->IsString()) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5236 | args.GetReturnValue()->SetInteger( |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 5237 | argFirst->ToString().Compare(argSecond->ToString().AsStringC()) == -1); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5238 | return; |
| 5239 | } |
| 5240 | |
| 5241 | FX_DOUBLE first = ValueToDouble(pThis, argFirst.get()); |
| 5242 | FX_DOUBLE second = ValueToDouble(pThis, argSecond.get()); |
| 5243 | args.GetReturnValue()->SetInteger((first < second) ? 1 : 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5244 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5245 | |
| 5246 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5247 | void CXFA_FM2JSContext::lessequal_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5248 | const CFX_ByteStringC& szFuncName, |
| 5249 | CFXJSE_Arguments& args) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5250 | if (args.GetLength() != 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5251 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5252 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5253 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5254 | |
| 5255 | std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); |
| 5256 | std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); |
| 5257 | if (argFirst->IsNull() || argSecond->IsNull()) { |
| 5258 | args.GetReturnValue()->SetInteger( |
| 5259 | (argFirst->IsNull() && argSecond->IsNull()) ? 1 : 0); |
| 5260 | return; |
| 5261 | } |
| 5262 | |
| 5263 | if (argFirst->IsString() && argSecond->IsString()) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5264 | args.GetReturnValue()->SetInteger( |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 5265 | argFirst->ToString().Compare(argSecond->ToString().AsStringC()) != 1); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5266 | return; |
| 5267 | } |
| 5268 | |
| 5269 | FX_DOUBLE first = ValueToDouble(pThis, argFirst.get()); |
| 5270 | FX_DOUBLE second = ValueToDouble(pThis, argSecond.get()); |
| 5271 | args.GetReturnValue()->SetInteger((first <= second) ? 1 : 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5272 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5273 | |
| 5274 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5275 | void CXFA_FM2JSContext::greater_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5276 | const CFX_ByteStringC& szFuncName, |
| 5277 | CFXJSE_Arguments& args) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5278 | if (args.GetLength() != 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5279 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5280 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5281 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5282 | |
| 5283 | std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); |
| 5284 | std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); |
| 5285 | if (argFirst->IsNull() || argSecond->IsNull()) { |
| 5286 | args.GetReturnValue()->SetInteger(0); |
| 5287 | return; |
| 5288 | } |
| 5289 | |
| 5290 | if (argFirst->IsString() && argSecond->IsString()) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5291 | args.GetReturnValue()->SetInteger( |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 5292 | argFirst->ToString().Compare(argSecond->ToString().AsStringC()) == 1); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5293 | return; |
| 5294 | } |
| 5295 | |
| 5296 | FX_DOUBLE first = ValueToDouble(pThis, argFirst.get()); |
| 5297 | FX_DOUBLE second = ValueToDouble(pThis, argSecond.get()); |
| 5298 | args.GetReturnValue()->SetInteger((first > second) ? 1 : 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5299 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5300 | |
| 5301 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5302 | void CXFA_FM2JSContext::greaterequal_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5303 | const CFX_ByteStringC& szFuncName, |
| 5304 | CFXJSE_Arguments& args) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5305 | if (args.GetLength() != 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5306 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5307 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5308 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5309 | |
| 5310 | std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); |
| 5311 | std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); |
| 5312 | if (argFirst->IsNull() || argSecond->IsNull()) { |
| 5313 | args.GetReturnValue()->SetInteger( |
| 5314 | (argFirst->IsNull() && argSecond->IsNull()) ? 1 : 0); |
| 5315 | return; |
| 5316 | } |
| 5317 | |
| 5318 | if (argFirst->IsString() && argSecond->IsString()) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5319 | args.GetReturnValue()->SetInteger( |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 5320 | argFirst->ToString().Compare(argSecond->ToString().AsStringC()) != -1); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5321 | return; |
| 5322 | } |
| 5323 | |
| 5324 | FX_DOUBLE first = ValueToDouble(pThis, argFirst.get()); |
| 5325 | FX_DOUBLE second = ValueToDouble(pThis, argSecond.get()); |
| 5326 | args.GetReturnValue()->SetInteger((first >= second) ? 1 : 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5327 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5328 | |
| 5329 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5330 | void CXFA_FM2JSContext::plus_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5331 | const CFX_ByteStringC& szFuncName, |
| 5332 | CFXJSE_Arguments& args) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5333 | if (args.GetLength() != 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5334 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5335 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5336 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5337 | |
| 5338 | std::unique_ptr<CFXJSE_Value> argFirst = args.GetValue(0); |
| 5339 | std::unique_ptr<CFXJSE_Value> argSecond = args.GetValue(1); |
| 5340 | if (ValueIsNull(pThis, argFirst.get()) && |
| 5341 | ValueIsNull(pThis, argSecond.get())) { |
| 5342 | args.GetReturnValue()->SetNull(); |
| 5343 | return; |
| 5344 | } |
| 5345 | |
| 5346 | FX_DOUBLE first = ValueToDouble(pThis, argFirst.get()); |
| 5347 | FX_DOUBLE second = ValueToDouble(pThis, argSecond.get()); |
| 5348 | args.GetReturnValue()->SetDouble(first + second); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5349 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5350 | |
| 5351 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5352 | void CXFA_FM2JSContext::minus_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5353 | const CFX_ByteStringC& szFuncName, |
| 5354 | CFXJSE_Arguments& args) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5355 | if (args.GetLength() != 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5356 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5357 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5358 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5359 | |
| 5360 | std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); |
| 5361 | std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); |
| 5362 | if (argFirst->IsNull() && argSecond->IsNull()) { |
| 5363 | args.GetReturnValue()->SetNull(); |
| 5364 | return; |
| 5365 | } |
| 5366 | |
| 5367 | FX_DOUBLE first = ValueToDouble(pThis, argFirst.get()); |
| 5368 | FX_DOUBLE second = ValueToDouble(pThis, argSecond.get()); |
| 5369 | args.GetReturnValue()->SetDouble(first - second); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5370 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5371 | |
| 5372 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5373 | void CXFA_FM2JSContext::multiple_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5374 | const CFX_ByteStringC& szFuncName, |
| 5375 | CFXJSE_Arguments& args) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5376 | if (args.GetLength() != 2) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5377 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5378 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5379 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5380 | |
| 5381 | std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); |
| 5382 | std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); |
| 5383 | if (argFirst->IsNull() && argSecond->IsNull()) { |
| 5384 | args.GetReturnValue()->SetNull(); |
| 5385 | return; |
| 5386 | } |
| 5387 | |
| 5388 | FX_DOUBLE first = ValueToDouble(pThis, argFirst.get()); |
| 5389 | FX_DOUBLE second = ValueToDouble(pThis, argSecond.get()); |
| 5390 | args.GetReturnValue()->SetDouble(first * second); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5391 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5392 | |
| 5393 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5394 | void CXFA_FM2JSContext::divide_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5395 | const CFX_ByteStringC& szFuncName, |
| 5396 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5397 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5398 | if (args.GetLength() != 2) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 5399 | pContext->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5400 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5401 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5402 | |
| 5403 | std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); |
| 5404 | std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); |
| 5405 | if (argFirst->IsNull() && argSecond->IsNull()) { |
| 5406 | args.GetReturnValue()->SetNull(); |
| 5407 | return; |
| 5408 | } |
| 5409 | |
| 5410 | FX_DOUBLE second = ValueToDouble(pThis, argSecond.get()); |
| 5411 | if (second == 0.0) { |
| 5412 | pContext->ThrowException(XFA_IDS_DIVIDE_ZERO); |
| 5413 | return; |
| 5414 | } |
| 5415 | |
| 5416 | FX_DOUBLE first = ValueToDouble(pThis, argFirst.get()); |
| 5417 | args.GetReturnValue()->SetDouble(first / second); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5418 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5419 | |
| 5420 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5421 | void CXFA_FM2JSContext::positive_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5422 | const CFX_ByteStringC& szFuncName, |
| 5423 | CFXJSE_Arguments& args) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5424 | if (args.GetLength() != 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5425 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5426 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5427 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5428 | |
| 5429 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 5430 | if (argOne->IsNull()) { |
| 5431 | args.GetReturnValue()->SetNull(); |
| 5432 | return; |
| 5433 | } |
| 5434 | args.GetReturnValue()->SetDouble(0.0 + ValueToDouble(pThis, argOne.get())); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5435 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5436 | |
| 5437 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5438 | void CXFA_FM2JSContext::negative_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5439 | const CFX_ByteStringC& szFuncName, |
| 5440 | CFXJSE_Arguments& args) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5441 | if (args.GetLength() != 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5442 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5443 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5444 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5445 | |
| 5446 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 5447 | if (argOne->IsNull()) { |
| 5448 | args.GetReturnValue()->SetNull(); |
| 5449 | return; |
| 5450 | } |
| 5451 | args.GetReturnValue()->SetDouble(0.0 - ValueToDouble(pThis, argOne.get())); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5452 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5453 | |
| 5454 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5455 | void CXFA_FM2JSContext::logical_not_operator(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5456 | const CFX_ByteStringC& szFuncName, |
| 5457 | CFXJSE_Arguments& args) { |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5458 | if (args.GetLength() != 1) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5459 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5460 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5461 | } |
dsinclair | 947c873 | 2016-06-08 20:40:30 -0700 | [diff] [blame] | 5462 | |
| 5463 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 5464 | if (argOne->IsNull()) { |
| 5465 | args.GetReturnValue()->SetNull(); |
| 5466 | return; |
| 5467 | } |
| 5468 | |
| 5469 | FX_DOUBLE first = ValueToDouble(pThis, argOne.get()); |
| 5470 | args.GetReturnValue()->SetInteger((first == 0.0) ? 1 : 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5471 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5472 | |
| 5473 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5474 | void CXFA_FM2JSContext::dot_accessor(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5475 | const CFX_ByteStringC& szFuncName, |
| 5476 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5477 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | ec3da5b | 2016-05-25 16:42:05 -0700 | [diff] [blame] | 5478 | v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5479 | int32_t argc = args.GetLength(); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5480 | if (argc < 4 || argc > 5) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 5481 | pContext->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5482 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5483 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5484 | |
| 5485 | bool bIsStar = true; |
| 5486 | int32_t iIndexValue = 0; |
| 5487 | if (argc > 4) { |
| 5488 | bIsStar = false; |
| 5489 | iIndexValue = ValueToInteger(pThis, args.GetValue(4).get()); |
| 5490 | } |
| 5491 | |
| 5492 | CFX_ByteString szName = args.GetUTF8String(2); |
| 5493 | CFX_ByteString szSomExp; |
| 5494 | GenerateSomExpression(szName.AsStringC(), args.GetInt32(3), iIndexValue, |
| 5495 | bIsStar, szSomExp); |
| 5496 | |
| 5497 | std::unique_ptr<CFXJSE_Value> argAccessor = args.GetValue(0); |
| 5498 | if (argAccessor->IsArray()) { |
| 5499 | std::unique_ptr<CFXJSE_Value> pLengthValue(new CFXJSE_Value(pIsolate)); |
| 5500 | argAccessor->GetObjectProperty("length", pLengthValue.get()); |
| 5501 | int32_t iLength = pLengthValue->ToInteger(); |
| 5502 | CFXJSE_Value*** hResolveValues = FX_Alloc(CFXJSE_Value**, iLength - 2); |
| 5503 | int32_t* iSizes = FX_Alloc(int32_t, iLength - 2); |
| 5504 | for (int32_t i = 0; i < (iLength - 2); i++) |
| 5505 | iSizes[i] = 0; |
| 5506 | |
| 5507 | std::unique_ptr<CFXJSE_Value> hJSObjValue(new CFXJSE_Value(pIsolate)); |
| 5508 | FX_BOOL bAttribute = FALSE; |
| 5509 | int32_t iCounter = 0; |
| 5510 | for (int32_t i = 2; i < iLength; i++) { |
| 5511 | argAccessor->GetObjectPropertyByIdx(i, hJSObjValue.get()); |
| 5512 | |
| 5513 | XFA_RESOLVENODE_RS resoveNodeRS; |
| 5514 | if (ResolveObjects(pThis, hJSObjValue.get(), szSomExp.AsStringC(), |
| 5515 | resoveNodeRS, TRUE, szName.IsEmpty()) > 0) { |
| 5516 | ParseResolveResult(pThis, resoveNodeRS, hJSObjValue.get(), |
| 5517 | hResolveValues[i - 2], iSizes[i - 2], bAttribute); |
| 5518 | iCounter += iSizes[i - 2]; |
| 5519 | } |
| 5520 | } |
| 5521 | if (iCounter < 1) { |
| 5522 | CFX_WideString wsPropertyName = |
| 5523 | CFX_WideString::FromUTF8(szName.AsStringC()); |
| 5524 | CFX_WideString wsSomExpression = |
| 5525 | CFX_WideString::FromUTF8(szSomExp.AsStringC()); |
| 5526 | pContext->ThrowException(XFA_IDS_ACCESS_PROPERTY_IN_NOT_OBJECT, |
| 5527 | wsPropertyName.c_str(), wsSomExpression.c_str()); |
| 5528 | return; |
| 5529 | } |
| 5530 | |
| 5531 | CFXJSE_Value** rgValues = FX_Alloc(CFXJSE_Value*, iCounter + 2); |
| 5532 | for (int32_t i = 0; i < (iCounter + 2); i++) |
| 5533 | rgValues[i] = new CFXJSE_Value(pIsolate); |
| 5534 | |
| 5535 | rgValues[0]->SetInteger(1); |
| 5536 | if (bAttribute) |
| 5537 | rgValues[1]->SetString(szName.AsStringC()); |
| 5538 | else |
| 5539 | rgValues[1]->SetNull(); |
| 5540 | |
| 5541 | int32_t iIndex = 2; |
| 5542 | for (int32_t i = 0; i < iLength - 2; i++) { |
| 5543 | for (int32_t j = 0; j < iSizes[i]; j++) { |
| 5544 | rgValues[iIndex]->Assign(hResolveValues[i][j]); |
| 5545 | iIndex++; |
| 5546 | } |
| 5547 | } |
| 5548 | args.GetReturnValue()->SetArray(iCounter + 2, rgValues); |
| 5549 | |
| 5550 | for (int32_t i = 0; i < (iCounter + 2); i++) |
| 5551 | delete rgValues[i]; |
| 5552 | FX_Free(rgValues); |
| 5553 | |
| 5554 | for (int32_t i = 0; i < iLength - 2; i++) { |
| 5555 | for (int32_t j = 0; j < iSizes[i]; j++) |
| 5556 | delete hResolveValues[i][j]; |
| 5557 | |
| 5558 | if (iSizes[i] > 0) |
| 5559 | FX_Free(hResolveValues[i]); |
| 5560 | } |
| 5561 | FX_Free(hResolveValues); |
| 5562 | FX_Free(iSizes); |
| 5563 | |
| 5564 | return; |
| 5565 | } |
| 5566 | |
| 5567 | XFA_RESOLVENODE_RS resoveNodeRS; |
| 5568 | int32_t iRet = 0; |
| 5569 | CFX_ByteString bsAccessorName = args.GetUTF8String(1); |
| 5570 | if (argAccessor->IsObject() || |
| 5571 | (argAccessor->IsNull() && bsAccessorName.IsEmpty())) { |
| 5572 | iRet = ResolveObjects(pThis, argAccessor.get(), szSomExp.AsStringC(), |
| 5573 | resoveNodeRS, TRUE, szName.IsEmpty()); |
| 5574 | } else if (!argAccessor->IsObject() && !bsAccessorName.IsEmpty() && |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 5575 | GetObjectForName(pThis, argAccessor.get(), |
| 5576 | bsAccessorName.AsStringC())) { |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5577 | iRet = ResolveObjects(pThis, argAccessor.get(), szSomExp.AsStringC(), |
| 5578 | resoveNodeRS, TRUE, szName.IsEmpty()); |
| 5579 | } |
| 5580 | if (iRet < 1) { |
| 5581 | CFX_WideString wsPropertyName = |
| 5582 | CFX_WideString::FromUTF8(szName.AsStringC()); |
| 5583 | CFX_WideString wsSomExpression = |
| 5584 | CFX_WideString::FromUTF8(szSomExp.AsStringC()); |
| 5585 | pContext->ThrowException(XFA_IDS_ACCESS_PROPERTY_IN_NOT_OBJECT, |
| 5586 | wsPropertyName.c_str(), wsSomExpression.c_str()); |
| 5587 | return; |
| 5588 | } |
| 5589 | |
| 5590 | CFXJSE_Value** hResolveValues; |
| 5591 | int32_t iSize = 0; |
| 5592 | FX_BOOL bAttribute = FALSE; |
| 5593 | ParseResolveResult(pThis, resoveNodeRS, argAccessor.get(), hResolveValues, |
| 5594 | iSize, bAttribute); |
| 5595 | CFXJSE_Value** rgValues = FX_Alloc(CFXJSE_Value*, iSize + 2); |
| 5596 | for (int32_t i = 0; i < (iSize + 2); i++) |
| 5597 | rgValues[i] = new CFXJSE_Value(pIsolate); |
| 5598 | |
| 5599 | rgValues[0]->SetInteger(1); |
| 5600 | if (bAttribute) |
| 5601 | rgValues[1]->SetString(szName.AsStringC()); |
| 5602 | else |
| 5603 | rgValues[1]->SetNull(); |
| 5604 | |
| 5605 | for (int32_t i = 0; i < iSize; i++) |
| 5606 | rgValues[i + 2]->Assign(hResolveValues[i]); |
| 5607 | |
| 5608 | args.GetReturnValue()->SetArray(iSize + 2, rgValues); |
| 5609 | for (int32_t i = 0; i < (iSize + 2); i++) |
| 5610 | delete rgValues[i]; |
| 5611 | FX_Free(rgValues); |
| 5612 | |
| 5613 | for (int32_t i = 0; i < iSize; i++) |
| 5614 | delete hResolveValues[i]; |
| 5615 | FX_Free(hResolveValues); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5616 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5617 | |
| 5618 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5619 | void CXFA_FM2JSContext::dotdot_accessor(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5620 | const CFX_ByteStringC& szFuncName, |
| 5621 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5622 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | ec3da5b | 2016-05-25 16:42:05 -0700 | [diff] [blame] | 5623 | v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5624 | int32_t argc = args.GetLength(); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5625 | if (argc < 4 || argc > 5) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 5626 | pContext->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5627 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5628 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5629 | |
| 5630 | bool bIsStar = true; |
| 5631 | int32_t iIndexValue = 0; |
| 5632 | if (argc > 4) { |
| 5633 | bIsStar = false; |
| 5634 | iIndexValue = ValueToInteger(pThis, args.GetValue(4).get()); |
| 5635 | } |
| 5636 | |
| 5637 | CFX_ByteString szSomExp; |
| 5638 | CFX_ByteString szName = args.GetUTF8String(2); |
| 5639 | GenerateSomExpression(szName.AsStringC(), args.GetInt32(3), iIndexValue, |
| 5640 | bIsStar, szSomExp); |
| 5641 | |
| 5642 | std::unique_ptr<CFXJSE_Value> argAccessor = args.GetValue(0); |
| 5643 | if (argAccessor->IsArray()) { |
| 5644 | std::unique_ptr<CFXJSE_Value> pLengthValue(new CFXJSE_Value(pIsolate)); |
| 5645 | argAccessor->GetObjectProperty("length", pLengthValue.get()); |
| 5646 | int32_t iLength = pLengthValue->ToInteger(); |
| 5647 | int32_t iCounter = 0; |
| 5648 | |
| 5649 | CFXJSE_Value*** hResolveValues = FX_Alloc(CFXJSE_Value**, iLength - 2); |
| 5650 | int32_t* iSizes = FX_Alloc(int32_t, iLength - 2); |
| 5651 | std::unique_ptr<CFXJSE_Value> hJSObjValue(new CFXJSE_Value(pIsolate)); |
| 5652 | FX_BOOL bAttribute = FALSE; |
| 5653 | for (int32_t i = 2; i < iLength; i++) { |
| 5654 | argAccessor->GetObjectPropertyByIdx(i, hJSObjValue.get()); |
| 5655 | XFA_RESOLVENODE_RS resoveNodeRS; |
| 5656 | if (ResolveObjects(pThis, hJSObjValue.get(), szSomExp.AsStringC(), |
| 5657 | resoveNodeRS, FALSE) > 0) { |
| 5658 | ParseResolveResult(pThis, resoveNodeRS, hJSObjValue.get(), |
| 5659 | hResolveValues[i - 2], iSizes[i - 2], bAttribute); |
| 5660 | iCounter += iSizes[i - 2]; |
| 5661 | } |
| 5662 | } |
| 5663 | if (iCounter < 1) { |
| 5664 | CFX_WideString wsPropertyName = |
| 5665 | CFX_WideString::FromUTF8(szName.AsStringC()); |
| 5666 | CFX_WideString wsSomExpression = |
| 5667 | CFX_WideString::FromUTF8(szSomExp.AsStringC()); |
| 5668 | pContext->ThrowException(XFA_IDS_ACCESS_PROPERTY_IN_NOT_OBJECT, |
| 5669 | wsPropertyName.c_str(), wsSomExpression.c_str()); |
| 5670 | return; |
| 5671 | } |
| 5672 | |
| 5673 | CFXJSE_Value** rgValues = FX_Alloc(CFXJSE_Value*, iCounter + 2); |
| 5674 | for (int32_t i = 0; i < (iCounter + 2); i++) |
| 5675 | rgValues[i] = new CFXJSE_Value(pIsolate); |
| 5676 | |
| 5677 | rgValues[0]->SetInteger(1); |
| 5678 | if (bAttribute) |
| 5679 | rgValues[1]->SetString(szName.AsStringC()); |
| 5680 | else |
| 5681 | rgValues[1]->SetNull(); |
| 5682 | |
| 5683 | int32_t iIndex = 2; |
| 5684 | for (int32_t i = 0; i < iLength - 2; i++) { |
| 5685 | for (int32_t j = 0; j < iSizes[i]; j++) { |
| 5686 | rgValues[iIndex]->Assign(hResolveValues[i][j]); |
| 5687 | iIndex++; |
| 5688 | } |
| 5689 | } |
| 5690 | args.GetReturnValue()->SetArray(iCounter + 2, rgValues); |
| 5691 | for (int32_t i = 0; i < (iCounter + 2); i++) |
| 5692 | delete rgValues[i]; |
| 5693 | FX_Free(rgValues); |
| 5694 | |
| 5695 | for (int32_t i = 0; i < iLength - 2; i++) { |
| 5696 | for (int32_t j = 0; j < iSizes[i]; j++) |
| 5697 | delete hResolveValues[i][j]; |
| 5698 | FX_Free(hResolveValues[i]); |
| 5699 | } |
| 5700 | FX_Free(hResolveValues); |
| 5701 | FX_Free(iSizes); |
| 5702 | return; |
| 5703 | } |
| 5704 | |
| 5705 | XFA_RESOLVENODE_RS resoveNodeRS; |
| 5706 | int32_t iRet = 0; |
| 5707 | CFX_ByteString bsAccessorName = args.GetUTF8String(1); |
| 5708 | if (argAccessor->IsObject() || |
| 5709 | (argAccessor->IsNull() && bsAccessorName.IsEmpty())) { |
| 5710 | iRet = ResolveObjects(pThis, argAccessor.get(), szSomExp.AsStringC(), |
| 5711 | resoveNodeRS, FALSE); |
| 5712 | } else if (!argAccessor->IsObject() && !bsAccessorName.IsEmpty() && |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 5713 | GetObjectForName(pThis, argAccessor.get(), |
| 5714 | bsAccessorName.AsStringC())) { |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5715 | iRet = ResolveObjects(pThis, argAccessor.get(), szSomExp.AsStringC(), |
| 5716 | resoveNodeRS, FALSE); |
| 5717 | } |
| 5718 | if (iRet < 1) { |
| 5719 | CFX_WideString wsPropertyName = |
| 5720 | CFX_WideString::FromUTF8(szName.AsStringC()); |
| 5721 | CFX_WideString wsSomExpression = |
| 5722 | CFX_WideString::FromUTF8(szSomExp.AsStringC()); |
| 5723 | pContext->ThrowException(XFA_IDS_ACCESS_PROPERTY_IN_NOT_OBJECT, |
| 5724 | wsPropertyName.c_str(), wsSomExpression.c_str()); |
| 5725 | return; |
| 5726 | } |
| 5727 | |
| 5728 | CFXJSE_Value** hResolveValues; |
| 5729 | int32_t iSize = 0; |
| 5730 | FX_BOOL bAttribute = FALSE; |
| 5731 | ParseResolveResult(pThis, resoveNodeRS, argAccessor.get(), hResolveValues, |
| 5732 | iSize, bAttribute); |
| 5733 | CFXJSE_Value** rgValues = FX_Alloc(CFXJSE_Value*, iSize + 2); |
| 5734 | for (int32_t i = 0; i < (iSize + 2); i++) |
| 5735 | rgValues[i] = new CFXJSE_Value(pIsolate); |
| 5736 | |
| 5737 | rgValues[0]->SetInteger(1); |
| 5738 | if (bAttribute) |
| 5739 | rgValues[1]->SetString(szName.AsStringC()); |
| 5740 | else |
| 5741 | rgValues[1]->SetNull(); |
| 5742 | |
| 5743 | for (int32_t i = 0; i < iSize; i++) |
| 5744 | rgValues[i + 2]->Assign(hResolveValues[i]); |
| 5745 | |
| 5746 | args.GetReturnValue()->SetArray(iSize + 2, rgValues); |
| 5747 | |
| 5748 | for (int32_t i = 0; i < (iSize + 2); i++) |
| 5749 | delete rgValues[i]; |
| 5750 | FX_Free(rgValues); |
| 5751 | |
| 5752 | for (int32_t i = 0; i < iSize; i++) |
| 5753 | delete hResolveValues[i]; |
| 5754 | FX_Free(hResolveValues); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5755 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5756 | |
| 5757 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5758 | void CXFA_FM2JSContext::eval_translation(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5759 | const CFX_ByteStringC& szFuncName, |
| 5760 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5761 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5762 | if (args.GetLength() != 1) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 5763 | pContext->ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"Eval"); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5764 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5765 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5766 | |
| 5767 | std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 5768 | CFX_ByteString argString; |
| 5769 | ValueToUTF8String(argOne.get(), argString); |
| 5770 | if (argString.IsEmpty()) { |
| 5771 | pContext->ThrowException(XFA_IDS_ARGUMENT_MISMATCH); |
| 5772 | return; |
| 5773 | } |
| 5774 | |
| 5775 | CFX_WideString scriptString = CFX_WideString::FromUTF8(argString.AsStringC()); |
| 5776 | CFX_WideTextBuf wsJavaScriptBuf; |
| 5777 | CFX_WideString wsError; |
| 5778 | CXFA_FM2JSContext::Translate(scriptString.AsStringC(), wsJavaScriptBuf, |
| 5779 | wsError); |
| 5780 | if (!wsError.IsEmpty()) { |
| 5781 | pContext->ThrowException(XFA_IDS_COMPILER_ERROR); |
| 5782 | return; |
| 5783 | } |
| 5784 | |
| 5785 | CFX_WideString javaScript = wsJavaScriptBuf.MakeString(); |
| 5786 | args.GetReturnValue()->SetString( |
| 5787 | FX_UTF8Encode(javaScript.c_str(), javaScript.GetLength()).AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5788 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5789 | |
| 5790 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5791 | void CXFA_FM2JSContext::is_fm_object(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5792 | const CFX_ByteStringC& szFuncName, |
| 5793 | CFXJSE_Arguments& args) { |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5794 | if (args.GetLength() != 1) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 5795 | args.GetReturnValue()->SetBoolean(FALSE); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5796 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5797 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5798 | |
| 5799 | std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); |
| 5800 | args.GetReturnValue()->SetBoolean(argOne->IsObject()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5801 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5802 | |
| 5803 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5804 | void CXFA_FM2JSContext::is_fm_array(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5805 | const CFX_ByteStringC& szFuncName, |
| 5806 | CFXJSE_Arguments& args) { |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5807 | if (args.GetLength() != 1) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 5808 | args.GetReturnValue()->SetBoolean(FALSE); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5809 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5810 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5811 | |
| 5812 | std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); |
| 5813 | args.GetReturnValue()->SetBoolean(argOne->IsArray()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5814 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5815 | |
| 5816 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5817 | void CXFA_FM2JSContext::get_fm_value(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5818 | const CFX_ByteStringC& szFuncName, |
| 5819 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5820 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5821 | if (args.GetLength() != 1) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 5822 | pContext->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5823 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5824 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5825 | |
| 5826 | std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); |
| 5827 | if (argOne->IsArray()) { |
| 5828 | v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
| 5829 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 5830 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 5831 | argOne->GetObjectPropertyByIdx(1, propertyValue.get()); |
| 5832 | argOne->GetObjectPropertyByIdx(2, jsObjectValue.get()); |
| 5833 | if (propertyValue->IsNull()) { |
| 5834 | GetObjectDefaultValue(jsObjectValue.get(), args.GetReturnValue()); |
| 5835 | return; |
| 5836 | } |
| 5837 | |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 5838 | jsObjectValue->GetObjectProperty(propertyValue->ToString().AsStringC(), |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5839 | args.GetReturnValue()); |
| 5840 | return; |
| 5841 | } |
| 5842 | |
| 5843 | if (argOne->IsObject()) { |
| 5844 | GetObjectDefaultValue(argOne.get(), args.GetReturnValue()); |
| 5845 | return; |
| 5846 | } |
| 5847 | |
| 5848 | args.GetReturnValue()->Assign(argOne.get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5849 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5850 | |
| 5851 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5852 | void CXFA_FM2JSContext::get_fm_jsobj(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5853 | const CFX_ByteStringC& szFuncName, |
| 5854 | CFXJSE_Arguments& args) { |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5855 | if (args.GetLength() != 1) { |
| 5856 | ToJSContext(pThis, nullptr)->ThrowException(XFA_IDS_COMPILER_ERROR); |
| 5857 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5858 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5859 | |
| 5860 | std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); |
| 5861 | if (!argOne->IsArray()) { |
| 5862 | args.GetReturnValue()->Assign(argOne.get()); |
| 5863 | return; |
| 5864 | } |
| 5865 | |
| 5866 | #ifndef NDEBUG |
| 5867 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
| 5868 | v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
| 5869 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
| 5870 | argOne->GetObjectProperty("length", lengthValue.get()); |
| 5871 | ASSERT(lengthValue->ToInteger() >= 3); |
| 5872 | #endif |
| 5873 | |
| 5874 | argOne->GetObjectPropertyByIdx(2, args.GetReturnValue()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5875 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5876 | |
| 5877 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5878 | void CXFA_FM2JSContext::fm_var_filter(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5879 | const CFX_ByteStringC& szFuncName, |
| 5880 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5881 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5882 | if (args.GetLength() != 1) { |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 5883 | pContext->ThrowException(XFA_IDS_COMPILER_ERROR); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5884 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5885 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5886 | |
| 5887 | v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
| 5888 | std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); |
| 5889 | if (!argOne->IsArray()) { |
| 5890 | std::unique_ptr<CFXJSE_Value> simpleValue = GetSimpleValue(pThis, args, 0); |
| 5891 | args.GetReturnValue()->Assign(simpleValue.get()); |
| 5892 | return; |
| 5893 | } |
| 5894 | |
| 5895 | #ifndef NDEBUG |
| 5896 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
| 5897 | argOne->GetObjectProperty("length", lengthValue.get()); |
| 5898 | ASSERT(lengthValue->ToInteger() >= 3); |
| 5899 | #endif |
| 5900 | |
| 5901 | std::unique_ptr<CFXJSE_Value> flagsValue(new CFXJSE_Value(pIsolate)); |
| 5902 | argOne->GetObjectPropertyByIdx(0, flagsValue.get()); |
| 5903 | int32_t iFlags = flagsValue->ToInteger(); |
| 5904 | if (iFlags != 3 && iFlags != 4) { |
| 5905 | std::unique_ptr<CFXJSE_Value> simpleValue = GetSimpleValue(pThis, args, 0); |
| 5906 | args.GetReturnValue()->Assign(simpleValue.get()); |
| 5907 | return; |
| 5908 | } |
| 5909 | |
| 5910 | if (iFlags == 4) { |
| 5911 | CFXJSE_Value* rgValues[3]; |
| 5912 | for (int32_t i = 0; i < 3; i++) |
| 5913 | rgValues[i] = new CFXJSE_Value(pIsolate); |
| 5914 | |
| 5915 | rgValues[0]->SetInteger(3); |
| 5916 | rgValues[1]->SetNull(); |
| 5917 | rgValues[2]->SetNull(); |
| 5918 | args.GetReturnValue()->SetArray(3, rgValues); |
| 5919 | for (int32_t i = 0; i < 3; i++) |
| 5920 | delete rgValues[i]; |
| 5921 | |
| 5922 | return; |
| 5923 | } |
| 5924 | |
| 5925 | std::unique_ptr<CFXJSE_Value> objectValue(new CFXJSE_Value(pIsolate)); |
| 5926 | argOne->GetObjectPropertyByIdx(2, objectValue.get()); |
| 5927 | if (objectValue->IsNull()) { |
| 5928 | pContext->ThrowException(XFA_IDS_COMPILER_ERROR); |
| 5929 | return; |
| 5930 | } |
| 5931 | args.GetReturnValue()->Assign(argOne.get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5932 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5933 | |
| 5934 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5935 | void CXFA_FM2JSContext::concat_fm_object(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5936 | const CFX_ByteStringC& szFuncName, |
| 5937 | CFXJSE_Arguments& args) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5938 | v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5939 | uint32_t iLength = 0; |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5940 | int32_t argc = args.GetLength(); |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 5941 | std::vector<std::unique_ptr<CFXJSE_Value>> argValues; |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5942 | for (int32_t i = 0; i < argc; i++) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 5943 | argValues.push_back(args.GetValue(i)); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 5944 | if (argValues[i]->IsArray()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 5945 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 5946 | argValues[i]->GetObjectProperty("length", lengthValue.get()); |
| 5947 | int32_t length = lengthValue->ToInteger(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5948 | iLength = iLength + ((length > 2) ? (length - 2) : 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5949 | } |
| 5950 | iLength += 1; |
| 5951 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5952 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 5953 | CFXJSE_Value** returnValues = FX_Alloc(CFXJSE_Value*, iLength); |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 5954 | for (int32_t i = 0; i < (int32_t)iLength; i++) |
| 5955 | returnValues[i] = new CFXJSE_Value(pIsolate); |
| 5956 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5957 | int32_t index = 0; |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5958 | for (int32_t i = 0; i < argc; i++) { |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 5959 | if (argValues[i]->IsArray()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 5960 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 5961 | argValues[i]->GetObjectProperty("length", lengthValue.get()); |
| 5962 | int32_t length = lengthValue->ToInteger(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5963 | for (int32_t j = 2; j < length; j++) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 5964 | argValues[i]->GetObjectPropertyByIdx(j, returnValues[index]); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5965 | index++; |
| 5966 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5967 | } |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 5968 | returnValues[index]->Assign(argValues[i].get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5969 | index++; |
| 5970 | } |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 5971 | args.GetReturnValue()->SetArray(iLength, returnValues); |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 5972 | for (int32_t i = 0; i < (int32_t)iLength; i++) |
| 5973 | delete returnValues[i]; |
| 5974 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5975 | FX_Free(returnValues); |
| 5976 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 5977 | |
| 5978 | // static |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 5979 | std::unique_ptr<CFXJSE_Value> CXFA_FM2JSContext::GetSimpleValue( |
| 5980 | CFXJSE_Value* pThis, |
| 5981 | CFXJSE_Arguments& args, |
| 5982 | uint32_t index) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 5983 | v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); |
dsinclair | 43854a5 | 2016-04-27 12:26:00 -0700 | [diff] [blame] | 5984 | ASSERT(index < (uint32_t)args.GetLength()); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5985 | |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 5986 | std::unique_ptr<CFXJSE_Value> argIndex = args.GetValue(index); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5987 | if (!argIndex->IsArray() && !argIndex->IsObject()) |
| 5988 | return argIndex; |
| 5989 | |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 5990 | if (argIndex->IsArray()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 5991 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 5992 | argIndex->GetObjectProperty("length", lengthValue.get()); |
| 5993 | int32_t iLength = lengthValue->ToInteger(); |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 5994 | std::unique_ptr<CFXJSE_Value> simpleValue(new CFXJSE_Value(pIsolate)); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5995 | if (iLength < 3) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 5996 | simpleValue.get()->SetUndefined(); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5997 | return simpleValue; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 5998 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 5999 | |
| 6000 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 6001 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 6002 | argIndex->GetObjectPropertyByIdx(1, propertyValue.get()); |
| 6003 | argIndex->GetObjectPropertyByIdx(2, jsObjectValue.get()); |
| 6004 | if (propertyValue->IsNull()) { |
| 6005 | GetObjectDefaultValue(jsObjectValue.get(), simpleValue.get()); |
| 6006 | return simpleValue; |
| 6007 | } |
| 6008 | |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 6009 | jsObjectValue->GetObjectProperty(propertyValue->ToString().AsStringC(), |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6010 | simpleValue.get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6011 | return simpleValue; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6012 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6013 | |
| 6014 | std::unique_ptr<CFXJSE_Value> defaultValue(new CFXJSE_Value(pIsolate)); |
| 6015 | GetObjectDefaultValue(argIndex.get(), defaultValue.get()); |
| 6016 | return defaultValue; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6017 | } |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6018 | |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6019 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6020 | FX_BOOL CXFA_FM2JSContext::ValueIsNull(CFXJSE_Value* pThis, CFXJSE_Value* arg) { |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6021 | if (!arg || arg->IsNull()) |
| 6022 | return TRUE; |
| 6023 | |
| 6024 | if (!arg->IsArray() && !arg->IsObject()) |
| 6025 | return FALSE; |
| 6026 | |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6027 | v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6028 | if (arg->IsArray()) { |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6029 | int32_t iLength = hvalue_get_array_length(pThis, arg); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6030 | if (iLength < 3) |
| 6031 | return TRUE; |
| 6032 | |
| 6033 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 6034 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 6035 | arg->GetObjectPropertyByIdx(1, propertyValue.get()); |
| 6036 | arg->GetObjectPropertyByIdx(2, jsObjectValue.get()); |
| 6037 | if (propertyValue->IsNull()) { |
| 6038 | std::unique_ptr<CFXJSE_Value> defaultValue(new CFXJSE_Value(pIsolate)); |
| 6039 | GetObjectDefaultValue(jsObjectValue.get(), defaultValue.get()); |
| 6040 | return defaultValue->IsNull(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6041 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6042 | |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6043 | std::unique_ptr<CFXJSE_Value> newPropertyValue(new CFXJSE_Value(pIsolate)); |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 6044 | jsObjectValue->GetObjectProperty(propertyValue->ToString().AsStringC(), |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6045 | newPropertyValue.get()); |
| 6046 | return newPropertyValue->IsNull(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6047 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6048 | |
| 6049 | std::unique_ptr<CFXJSE_Value> defaultValue(new CFXJSE_Value(pIsolate)); |
| 6050 | GetObjectDefaultValue(arg, defaultValue.get()); |
| 6051 | return defaultValue->IsNull(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6052 | } |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6053 | |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6054 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6055 | int32_t CXFA_FM2JSContext::hvalue_get_array_length(CFXJSE_Value* pThis, |
| 6056 | CFXJSE_Value* arg) { |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6057 | if (!arg || !arg->IsArray()) |
| 6058 | return 0; |
| 6059 | |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6060 | v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6061 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
| 6062 | arg->GetObjectProperty("length", lengthValue.get()); |
| 6063 | return lengthValue->ToInteger(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6064 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6065 | |
| 6066 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6067 | FX_BOOL CXFA_FM2JSContext::simpleValueCompare(CFXJSE_Value* pThis, |
| 6068 | CFXJSE_Value* firstValue, |
| 6069 | CFXJSE_Value* secondValue) { |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6070 | if (!firstValue) |
| 6071 | return FALSE; |
| 6072 | |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6073 | if (firstValue->IsString()) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6074 | CFX_ByteString firstString, secondString; |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6075 | ValueToUTF8String(firstValue, firstString); |
| 6076 | ValueToUTF8String(secondValue, secondString); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6077 | return firstString == secondString; |
| 6078 | } |
| 6079 | if (firstValue->IsNumber()) { |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6080 | FX_FLOAT first = ValueToFloat(pThis, firstValue); |
| 6081 | FX_FLOAT second = ValueToFloat(pThis, secondValue); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6082 | return (first == second); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6083 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6084 | if (firstValue->IsBoolean()) |
| 6085 | return (firstValue->ToBoolean() == secondValue->ToBoolean()); |
| 6086 | |
| 6087 | return firstValue->IsNull() && secondValue && secondValue->IsNull(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6088 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6089 | |
| 6090 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6091 | void CXFA_FM2JSContext::unfoldArgs(CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6092 | CFXJSE_Arguments& args, |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6093 | CFXJSE_Value**& resultValues, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6094 | int32_t& iCount, |
| 6095 | int32_t iStart) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6096 | iCount = 0; |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6097 | |
| 6098 | v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6099 | int32_t argc = args.GetLength(); |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6100 | std::vector<std::unique_ptr<CFXJSE_Value>> argsValue; |
| 6101 | for (int32_t i = 0; i < argc - iStart; i++) { |
| 6102 | argsValue.push_back(args.GetValue(i + iStart)); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6103 | if (argsValue[i]->IsArray()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6104 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 6105 | argsValue[i]->GetObjectProperty("length", lengthValue.get()); |
| 6106 | int32_t iLength = lengthValue->ToInteger(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6107 | iCount += ((iLength > 2) ? (iLength - 2) : 0); |
| 6108 | } else { |
| 6109 | iCount += 1; |
| 6110 | } |
| 6111 | } |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6112 | resultValues = FX_Alloc(CFXJSE_Value*, iCount); |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6113 | for (int32_t i = 0; i < iCount; i++) |
| 6114 | resultValues[i] = new CFXJSE_Value(pIsolate); |
| 6115 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6116 | int32_t index = 0; |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6117 | for (int32_t i = 0; i < argc - iStart; i++) { |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6118 | if (argsValue[i]->IsArray()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6119 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 6120 | argsValue[i]->GetObjectProperty("length", lengthValue.get()); |
| 6121 | int32_t iLength = lengthValue->ToInteger(); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6122 | if (iLength < 3) |
| 6123 | continue; |
| 6124 | |
| 6125 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 6126 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 6127 | argsValue[i]->GetObjectPropertyByIdx(1, propertyValue.get()); |
| 6128 | if (propertyValue->IsNull()) { |
| 6129 | for (int32_t j = 2; j < iLength; j++) { |
| 6130 | argsValue[i]->GetObjectPropertyByIdx(j, jsObjectValue.get()); |
| 6131 | GetObjectDefaultValue(jsObjectValue.get(), resultValues[index]); |
| 6132 | index++; |
| 6133 | } |
| 6134 | } else { |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6135 | for (int32_t j = 2; j < iLength; j++) { |
| 6136 | argsValue[i]->GetObjectPropertyByIdx(j, jsObjectValue.get()); |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 6137 | jsObjectValue->GetObjectProperty( |
| 6138 | propertyValue->ToString().AsStringC(), resultValues[index]); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6139 | index++; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6140 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6141 | } |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6142 | } else if (argsValue[i]->IsObject()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6143 | GetObjectDefaultValue(argsValue[i].get(), resultValues[index]); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6144 | index++; |
| 6145 | } else { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 6146 | resultValues[index]->Assign(argsValue[i].get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6147 | index++; |
| 6148 | } |
| 6149 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6150 | } |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6151 | |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6152 | // static |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6153 | void CXFA_FM2JSContext::GetObjectDefaultValue(CFXJSE_Value* pValue, |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6154 | CFXJSE_Value* pDefaultValue) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6155 | CXFA_Node* pNode = ToNode(CXFA_ScriptContext::ToObject(pValue, nullptr)); |
| 6156 | if (!pNode) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 6157 | pDefaultValue->SetNull(); |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6158 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6159 | } |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6160 | pNode->Script_Som_DefaultValue(pDefaultValue, FALSE, (XFA_ATTRIBUTE)-1); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6161 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6162 | |
| 6163 | // static |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6164 | FX_BOOL CXFA_FM2JSContext::SetObjectDefaultValue(CFXJSE_Value* pValue, |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6165 | CFXJSE_Value* hNewValue) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6166 | CXFA_Node* pNode = ToNode(CXFA_ScriptContext::ToObject(pValue, nullptr)); |
| 6167 | if (!pNode) |
| 6168 | return FALSE; |
| 6169 | |
| 6170 | pNode->Script_Som_DefaultValue(hNewValue, TRUE, (XFA_ATTRIBUTE)-1); |
| 6171 | return TRUE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6172 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6173 | |
| 6174 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6175 | void CXFA_FM2JSContext::GenerateSomExpression(const CFX_ByteStringC& szName, |
| 6176 | int32_t iIndexFlags, |
| 6177 | int32_t iIndexValue, |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6178 | bool bIsStar, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6179 | CFX_ByteString& szSomExp) { |
| 6180 | if (bIsStar) { |
| 6181 | szSomExp = szName + "[*]"; |
| 6182 | return; |
| 6183 | } |
| 6184 | if (iIndexFlags == 0) { |
| 6185 | szSomExp = szName; |
| 6186 | return; |
| 6187 | } |
| 6188 | if (iIndexFlags == 1 || iIndexValue == 0) { |
| 6189 | szSomExp = szName + "[" + |
| 6190 | CFX_ByteString::FormatInteger(iIndexValue, FXFORMAT_SIGNED) + |
| 6191 | "]"; |
| 6192 | } else if (iIndexFlags == 2) { |
| 6193 | szSomExp = (iIndexValue < 0) ? (szName + "[-") : (szName + "[+"); |
| 6194 | iIndexValue = (iIndexValue < 0) ? (0 - iIndexValue) : iIndexValue; |
| 6195 | szSomExp += CFX_ByteString::FormatInteger(iIndexValue); |
| 6196 | szSomExp += "]"; |
| 6197 | } else { |
| 6198 | szSomExp = (iIndexValue < 0) ? (szName + "[") : (szName + "[-"); |
| 6199 | iIndexValue = (iIndexValue < 0) ? (0 - iIndexValue) : iIndexValue; |
| 6200 | szSomExp += CFX_ByteString::FormatInteger(iIndexValue); |
| 6201 | szSomExp += "]"; |
| 6202 | } |
| 6203 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6204 | |
| 6205 | // static |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 6206 | FX_BOOL CXFA_FM2JSContext::GetObjectForName( |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6207 | CFXJSE_Value* pThis, |
| 6208 | CFXJSE_Value* accessorValue, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6209 | const CFX_ByteStringC& szAccessorName) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6210 | CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6211 | if (!pDoc) |
| 6212 | return FALSE; |
| 6213 | |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 6214 | CXFA_ScriptContext* pScriptContext = pDoc->GetScriptContext(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6215 | XFA_RESOLVENODE_RS resoveNodeRS; |
tsepez | 736f28a | 2016-03-25 14:19:51 -0700 | [diff] [blame] | 6216 | uint32_t dwFlags = XFA_RESOLVENODE_Children | XFA_RESOLVENODE_Properties | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6217 | XFA_RESOLVENODE_Siblings | XFA_RESOLVENODE_Parent; |
| 6218 | int32_t iRet = pScriptContext->ResolveObjects( |
| 6219 | pScriptContext->GetThisObject(), |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 6220 | CFX_WideString::FromUTF8(szAccessorName).AsStringC(), resoveNodeRS, |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 6221 | dwFlags); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6222 | if (iRet >= 1 && resoveNodeRS.dwFlags == XFA_RESOVENODE_RSTYPE_Nodes) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 6223 | accessorValue->Assign( |
| 6224 | pScriptContext->GetJSValueFromMap(resoveNodeRS.nodes.GetAt(0))); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6225 | return TRUE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6226 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6227 | return FALSE; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6228 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6229 | |
| 6230 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6231 | int32_t CXFA_FM2JSContext::ResolveObjects(CFXJSE_Value* pThis, |
| 6232 | CFXJSE_Value* pRefValue, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6233 | const CFX_ByteStringC& bsSomExp, |
| 6234 | XFA_RESOLVENODE_RS& resoveNodeRS, |
| 6235 | FX_BOOL bdotAccessor, |
| 6236 | FX_BOOL bHasNoResolveName) { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6237 | CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6238 | if (!pDoc) |
| 6239 | return -1; |
| 6240 | |
| 6241 | CFX_WideString wsSomExpression = CFX_WideString::FromUTF8(bsSomExp); |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 6242 | CXFA_ScriptContext* pScriptContext = pDoc->GetScriptContext(); |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6243 | CXFA_Object* pNode = nullptr; |
tsepez | 736f28a | 2016-03-25 14:19:51 -0700 | [diff] [blame] | 6244 | uint32_t dFlags = 0UL; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6245 | if (bdotAccessor) { |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6246 | if (pRefValue && pRefValue->IsNull()) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6247 | pNode = pScriptContext->GetThisObject(); |
| 6248 | dFlags = XFA_RESOLVENODE_Siblings | XFA_RESOLVENODE_Parent; |
| 6249 | } else { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6250 | pNode = CXFA_ScriptContext::ToObject(pRefValue, nullptr); |
dsinclair | 43854a5 | 2016-04-27 12:26:00 -0700 | [diff] [blame] | 6251 | ASSERT(pNode); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6252 | if (bHasNoResolveName) { |
| 6253 | CFX_WideString wsName; |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6254 | if (CXFA_Node* pXFANode = pNode->AsNode()) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6255 | pXFANode->GetAttribute(XFA_ATTRIBUTE_Name, wsName, FALSE); |
dsinclair | 017052a | 2016-06-28 07:43:51 -0700 | [diff] [blame] | 6256 | if (wsName.IsEmpty()) |
| 6257 | wsName = FX_WSTRC(L"#") + pNode->GetClassName(); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6258 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6259 | wsSomExpression = wsName + wsSomExpression; |
| 6260 | dFlags = XFA_RESOLVENODE_Siblings; |
| 6261 | } else { |
| 6262 | dFlags = (bsSomExp == "*") |
| 6263 | ? (XFA_RESOLVENODE_Children) |
| 6264 | : (XFA_RESOLVENODE_Children | XFA_RESOLVENODE_Attributes | |
| 6265 | XFA_RESOLVENODE_Properties); |
| 6266 | } |
| 6267 | } |
| 6268 | } else { |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6269 | pNode = CXFA_ScriptContext::ToObject(pRefValue, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6270 | dFlags = XFA_RESOLVENODE_AnyChild; |
| 6271 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6272 | return pScriptContext->ResolveObjects(pNode, wsSomExpression.AsStringC(), |
tsepez | fc58ad1 | 2016-04-05 12:22:15 -0700 | [diff] [blame] | 6273 | resoveNodeRS, dFlags); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6274 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6275 | |
| 6276 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6277 | void CXFA_FM2JSContext::ParseResolveResult( |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6278 | CFXJSE_Value* pThis, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6279 | const XFA_RESOLVENODE_RS& resoveNodeRS, |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6280 | CFXJSE_Value* pParentValue, |
| 6281 | CFXJSE_Value**& resultValues, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6282 | int32_t& iSize, |
| 6283 | FX_BOOL& bAttribute) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6284 | iSize = 0; |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6285 | resultValues = nullptr; |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6286 | |
| 6287 | CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); |
| 6288 | v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6289 | if (resoveNodeRS.dwFlags == XFA_RESOVENODE_RSTYPE_Nodes) { |
| 6290 | bAttribute = FALSE; |
| 6291 | iSize = resoveNodeRS.nodes.GetSize(); |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6292 | resultValues = FX_Alloc(CFXJSE_Value*, iSize); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6293 | for (int32_t i = 0; i < iSize; i++) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6294 | resultValues[i] = new CFXJSE_Value(pIsolate); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 6295 | resultValues[i]->Assign( |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6296 | pContext->GetDocument()->GetScriptContext()->GetJSValueFromMap( |
| 6297 | resoveNodeRS.nodes.GetAt(i))); |
| 6298 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6299 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6300 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6301 | |
| 6302 | CXFA_ValueArray objectProperties(pIsolate); |
| 6303 | int32_t iRet = resoveNodeRS.GetAttributeResult(objectProperties); |
| 6304 | bAttribute = (iRet == 0); |
| 6305 | if (!bAttribute) { |
| 6306 | iSize = iRet; |
| 6307 | resultValues = FX_Alloc(CFXJSE_Value*, iSize); |
| 6308 | for (int32_t i = 0; i < iSize; i++) { |
| 6309 | resultValues[i] = new CFXJSE_Value(pIsolate); |
| 6310 | resultValues[i]->Assign(objectProperties[i]); |
| 6311 | } |
| 6312 | return; |
| 6313 | } |
| 6314 | |
| 6315 | if (!pParentValue || !pParentValue->IsObject()) |
| 6316 | return; |
| 6317 | |
| 6318 | iSize = 1; |
| 6319 | resultValues = FX_Alloc(CFXJSE_Value*, 1); |
| 6320 | resultValues[0] = new CFXJSE_Value(pIsolate); |
| 6321 | resultValues[0]->Assign(pParentValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6322 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6323 | |
| 6324 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6325 | int32_t CXFA_FM2JSContext::ValueToInteger(CFXJSE_Value* pThis, |
| 6326 | CFXJSE_Value* pValue) { |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6327 | if (!pValue) |
| 6328 | return 0; |
| 6329 | |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6330 | v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6331 | if (pValue->IsArray()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6332 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 6333 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 6334 | std::unique_ptr<CFXJSE_Value> newPropertyValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 6335 | pValue->GetObjectPropertyByIdx(1, propertyValue.get()); |
| 6336 | pValue->GetObjectPropertyByIdx(2, jsObjectValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6337 | if (propertyValue->IsNull()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6338 | GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6339 | return ValueToInteger(pThis, newPropertyValue.get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6340 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6341 | |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 6342 | jsObjectValue->GetObjectProperty(propertyValue->ToString().AsStringC(), |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6343 | newPropertyValue.get()); |
| 6344 | return ValueToInteger(pThis, newPropertyValue.get()); |
| 6345 | } |
| 6346 | if (pValue->IsObject()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6347 | std::unique_ptr<CFXJSE_Value> newPropertyValue(new CFXJSE_Value(pIsolate)); |
| 6348 | GetObjectDefaultValue(pValue, newPropertyValue.get()); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6349 | return ValueToInteger(pThis, newPropertyValue.get()); |
| 6350 | } |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 6351 | if (pValue->IsString()) |
| 6352 | return FXSYS_atoi(pValue->ToString().c_str()); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6353 | return pValue->ToInteger(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6354 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6355 | |
| 6356 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6357 | FX_FLOAT CXFA_FM2JSContext::ValueToFloat(CFXJSE_Value* pThis, |
| 6358 | CFXJSE_Value* arg) { |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6359 | if (!arg) |
| 6360 | return 0.0f; |
| 6361 | |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6362 | v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6363 | if (arg->IsArray()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6364 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 6365 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 6366 | std::unique_ptr<CFXJSE_Value> newPropertyValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 6367 | arg->GetObjectPropertyByIdx(1, propertyValue.get()); |
| 6368 | arg->GetObjectPropertyByIdx(2, jsObjectValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6369 | if (propertyValue->IsNull()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6370 | GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6371 | return ValueToFloat(pThis, newPropertyValue.get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6372 | } |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 6373 | jsObjectValue->GetObjectProperty(propertyValue->ToString().AsStringC(), |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6374 | newPropertyValue.get()); |
| 6375 | return ValueToFloat(pThis, newPropertyValue.get()); |
| 6376 | } |
| 6377 | if (arg->IsObject()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6378 | std::unique_ptr<CFXJSE_Value> newPropertyValue(new CFXJSE_Value(pIsolate)); |
| 6379 | GetObjectDefaultValue(arg, newPropertyValue.get()); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6380 | return ValueToFloat(pThis, newPropertyValue.get()); |
| 6381 | } |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 6382 | if (arg->IsString()) |
| 6383 | return (FX_FLOAT)XFA_ByteStringToDouble(arg->ToString().AsStringC()); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6384 | if (arg->IsUndefined()) |
| 6385 | return 0; |
| 6386 | |
| 6387 | return arg->ToFloat(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6388 | } |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6389 | |
| 6390 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6391 | FX_DOUBLE CXFA_FM2JSContext::ValueToDouble(CFXJSE_Value* pThis, |
| 6392 | CFXJSE_Value* arg) { |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6393 | if (!arg) |
| 6394 | return 0; |
| 6395 | |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 6396 | v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6397 | if (arg->IsArray()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6398 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 6399 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 6400 | std::unique_ptr<CFXJSE_Value> newPropertyValue(new CFXJSE_Value(pIsolate)); |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 6401 | arg->GetObjectPropertyByIdx(1, propertyValue.get()); |
| 6402 | arg->GetObjectPropertyByIdx(2, jsObjectValue.get()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6403 | if (propertyValue->IsNull()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6404 | GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6405 | return ValueToDouble(pThis, newPropertyValue.get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6406 | } |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 6407 | jsObjectValue->GetObjectProperty(propertyValue->ToString().AsStringC(), |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6408 | newPropertyValue.get()); |
| 6409 | return ValueToDouble(pThis, newPropertyValue.get()); |
| 6410 | } |
| 6411 | if (arg->IsObject()) { |
dsinclair | 86fad99 | 2016-05-31 11:34:04 -0700 | [diff] [blame] | 6412 | std::unique_ptr<CFXJSE_Value> newPropertyValue(new CFXJSE_Value(pIsolate)); |
| 6413 | GetObjectDefaultValue(arg, newPropertyValue.get()); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6414 | return ValueToDouble(pThis, newPropertyValue.get()); |
| 6415 | } |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 6416 | if (arg->IsString()) |
| 6417 | return XFA_ByteStringToDouble(arg->ToString().AsStringC()); |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6418 | if (arg->IsUndefined()) |
| 6419 | return 0; |
| 6420 | return arg->ToDouble(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6421 | } |
tsepez | 1c9cfe1 | 2016-05-26 13:30:56 -0700 | [diff] [blame] | 6422 | |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 6423 | // static. |
| 6424 | double CXFA_FM2JSContext::ExtractDouble(CFXJSE_Value* pThis, |
| 6425 | CFXJSE_Value* src, |
| 6426 | bool* ret) { |
| 6427 | ASSERT(ret); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 6428 | *ret = true; |
| 6429 | |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6430 | if (!src) |
| 6431 | return 0; |
| 6432 | |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6433 | if (!src->IsArray()) |
| 6434 | return ValueToDouble(pThis, src); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 6435 | |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6436 | v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); |
| 6437 | std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
| 6438 | src->GetObjectProperty("length", lengthValue.get()); |
| 6439 | int32_t iLength = lengthValue->ToInteger(); |
| 6440 | if (iLength <= 2) { |
| 6441 | *ret = false; |
| 6442 | return 0.0; |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 6443 | } |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6444 | |
| 6445 | std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 6446 | std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 6447 | src->GetObjectPropertyByIdx(1, propertyValue.get()); |
| 6448 | src->GetObjectPropertyByIdx(2, jsObjectValue.get()); |
| 6449 | if (propertyValue->IsNull()) |
| 6450 | return ValueToDouble(pThis, jsObjectValue.get()); |
| 6451 | |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6452 | std::unique_ptr<CFXJSE_Value> newPropertyValue(new CFXJSE_Value(pIsolate)); |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 6453 | jsObjectValue->GetObjectProperty(propertyValue->ToString().AsStringC(), |
dsinclair | bd136fb | 2016-06-09 11:47:51 -0700 | [diff] [blame] | 6454 | newPropertyValue.get()); |
| 6455 | return ValueToDouble(pThis, newPropertyValue.get()); |
dsinclair | dbdcb81 | 2016-06-01 20:07:22 -0700 | [diff] [blame] | 6456 | } |
| 6457 | |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6458 | // static |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6459 | void CXFA_FM2JSContext::ValueToUTF8String(CFXJSE_Value* arg, |
| 6460 | CFX_ByteString& szOutputString) { |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6461 | if (!arg) |
| 6462 | return; |
| 6463 | |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 6464 | if (arg->IsNull() || arg->IsUndefined()) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6465 | szOutputString = ""; |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 6466 | else if (arg->IsBoolean()) |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 6467 | szOutputString = arg->ToBoolean() ? "1" : "0"; |
dsinclair | 2f5582f | 2016-06-09 11:48:23 -0700 | [diff] [blame] | 6468 | else |
| 6469 | szOutputString = arg->ToString(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6470 | } |
tsepez | 1c9cfe1 | 2016-05-26 13:30:56 -0700 | [diff] [blame] | 6471 | |
dsinclair | e80e9f8 | 2016-06-01 06:10:04 -0700 | [diff] [blame] | 6472 | // static. |
| 6473 | int32_t CXFA_FM2JSContext::Translate(const CFX_WideStringC& wsFormcalc, |
| 6474 | CFX_WideTextBuf& wsJavascript, |
| 6475 | CFX_WideString& wsError) { |
| 6476 | if (wsFormcalc.IsEmpty()) { |
| 6477 | wsJavascript.Clear(); |
| 6478 | wsError.clear(); |
| 6479 | return 0; |
| 6480 | } |
| 6481 | int32_t status = 0; |
| 6482 | CXFA_FMProgram program; |
| 6483 | status = program.Init(wsFormcalc); |
| 6484 | if (status) { |
| 6485 | wsError = program.GetError().message; |
| 6486 | return status; |
| 6487 | } |
| 6488 | status = program.ParseProgram(); |
| 6489 | if (status) { |
| 6490 | wsError = program.GetError().message; |
| 6491 | return status; |
| 6492 | } |
| 6493 | program.TranslateProgram(wsJavascript); |
| 6494 | return 0; |
| 6495 | } |
| 6496 | |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6497 | CXFA_FM2JSContext::CXFA_FM2JSContext(v8::Isolate* pScriptIsolate, |
| 6498 | CFXJSE_Context* pScriptContext, |
| 6499 | CXFA_Document* pDoc) |
| 6500 | : m_pIsolate(pScriptIsolate), |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6501 | m_pFMClass( |
| 6502 | CFXJSE_Class::Create(pScriptContext, &formcalc_fm2js_descriptor)), |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6503 | m_pValue(new CFXJSE_Value(pScriptIsolate)), |
| 6504 | m_pDocument(pDoc) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 6505 | m_pValue.get()->SetNull(); |
| 6506 | m_pValue.get()->SetObject(this, m_pFMClass); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6507 | } |
tsepez | 1c9cfe1 | 2016-05-26 13:30:56 -0700 | [diff] [blame] | 6508 | |
dsinclair | 48d91dd | 2016-05-31 11:54:01 -0700 | [diff] [blame] | 6509 | CXFA_FM2JSContext::~CXFA_FM2JSContext() {} |
| 6510 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 6511 | void CXFA_FM2JSContext::GlobalPropertyGetter(CFXJSE_Value* pValue) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 6512 | pValue->Assign(m_pValue.get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6513 | } |
tsepez | 5c4dd35 | 2016-05-26 13:57:58 -0700 | [diff] [blame] | 6514 | |
dsinclair | 2235b7b | 2016-06-02 07:42:25 -0700 | [diff] [blame] | 6515 | void CXFA_FM2JSContext::ThrowException(int32_t iStringID, ...) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6516 | IXFA_AppProvider* pAppProvider = m_pDocument->GetNotify()->GetAppProvider(); |
dsinclair | 43854a5 | 2016-04-27 12:26:00 -0700 | [diff] [blame] | 6517 | ASSERT(pAppProvider); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6518 | CFX_WideString wsFormat; |
| 6519 | pAppProvider->LoadString(iStringID, wsFormat); |
| 6520 | CFX_WideString wsMessage; |
| 6521 | va_list arg_ptr; |
| 6522 | va_start(arg_ptr, iStringID); |
tsepez | bd9748d | 2016-04-13 21:40:19 -0700 | [diff] [blame] | 6523 | wsMessage.FormatV(wsFormat.c_str(), arg_ptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6524 | va_end(arg_ptr); |
tsepez | 28f97ff | 2016-04-04 16:41:35 -0700 | [diff] [blame] | 6525 | FXJSE_ThrowMessage( |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 6526 | FX_UTF8Encode(wsMessage.c_str(), wsMessage.GetLength()).AsStringC()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 6527 | } |