blob: adaa9c794b600108b03d7be6569febffca13d03c [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#include "fpdfsdk/javascript/util.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Lei Zhang6b37a5f2015-12-25 00:20:59 -08009#include <time.h>
10
tsepez86a61dc2016-03-25 10:00:11 -070011#include <algorithm>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050012#include <string>
13#include <vector>
14
Dan Sinclaira8a28e02016-03-23 15:41:39 -040015#include "core/fxcrt/include/fx_ext.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040016#include "fpdfsdk/javascript/JS_Define.h"
17#include "fpdfsdk/javascript/JS_EventHandler.h"
18#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040019#include "fpdfsdk/javascript/JS_Value.h"
20#include "fpdfsdk/javascript/PublicMethods.h"
dsinclair64376be2016-03-31 20:03:24 -070021#include "fpdfsdk/javascript/cjs_context.h"
22#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040023#include "fpdfsdk/javascript/resource.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025#if _FX_OS_ == _FX_ANDROID_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026#include <ctype.h>
27#endif
28
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029BEGIN_JS_STATIC_CONST(CJS_Util)
30END_JS_STATIC_CONST()
31
32BEGIN_JS_STATIC_PROP(CJS_Util)
33END_JS_STATIC_PROP()
34
35BEGIN_JS_STATIC_METHOD(CJS_Util)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036JS_STATIC_METHOD_ENTRY(printd)
37JS_STATIC_METHOD_ENTRY(printf)
38JS_STATIC_METHOD_ENTRY(printx)
39JS_STATIC_METHOD_ENTRY(scand)
40JS_STATIC_METHOD_ENTRY(byteToChar)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041END_JS_STATIC_METHOD()
42
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043IMPLEMENT_JS_CLASS(CJS_Util, util)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045#define UTIL_INT 0
46#define UTIL_DOUBLE 1
47#define UTIL_STRING 2
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048
tsepez86a61dc2016-03-25 10:00:11 -070049namespace {
50
51// Map PDF-style directives to equivalent wcsftime directives. Not
52// all have direct equivalents, though.
53struct TbConvert {
54 const FX_WCHAR* lpszJSMark;
55 const FX_WCHAR* lpszCppMark;
56};
57
58// Map PDF-style directives lacking direct wcsftime directives to
59// the value with which they will be replaced.
60struct TbConvertAdditional {
61 const FX_WCHAR* lpszJSMark;
62 int iValue;
63};
64
65const TbConvert TbConvertTable[] = {
66 {L"mmmm", L"%B"}, {L"mmm", L"%b"}, {L"mm", L"%m"}, {L"dddd", L"%A"},
67 {L"ddd", L"%a"}, {L"dd", L"%d"}, {L"yyyy", L"%Y"}, {L"yy", L"%y"},
68 {L"HH", L"%H"}, {L"hh", L"%I"}, {L"MM", L"%M"}, {L"ss", L"%S"},
69 {L"TT", L"%p"},
70#if defined(_WIN32)
71 {L"tt", L"%p"}, {L"h", L"%#I"},
72#else
73 {L"tt", L"%P"}, {L"h", L"%l"},
74#endif
75};
76
77int ParseDataType(std::wstring* sFormat) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 bool bPercent = FALSE;
79 for (size_t i = 0; i < sFormat->length(); ++i) {
80 wchar_t c = (*sFormat)[i];
81 if (c == L'%') {
82 bPercent = true;
83 continue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070084 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 if (bPercent) {
87 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' ||
88 c == L'u' || c == L'x' || c == L'X') {
89 return UTIL_INT;
90 }
91 if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G') {
92 return UTIL_DOUBLE;
93 }
94 if (c == L's' || c == L'S') {
95 // Map s to S since we always deal internally
96 // with wchar_t strings.
97 (*sFormat)[i] = L'S';
98 return UTIL_STRING;
99 }
100 if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' ||
Lei Zhang9559b7a2015-12-21 11:12:20 -0800101 FXSYS_iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 continue;
103 }
104 break;
105 }
106 }
107
108 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700109}
110
tsepez86a61dc2016-03-25 10:00:11 -0700111} // namespace
112
113util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {}
114
115util::~util() {}
116
Tom Sepezba038bc2015-10-08 12:03:00 -0700117FX_BOOL util::printf(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800118 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 CJS_Value& vRet,
120 CFX_WideString& sError) {
121 int iSize = params.size();
122 if (iSize < 1)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700123 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 std::wstring c_ConvChar(params[0].ToCFXWideString().c_str());
125 std::vector<std::wstring> c_strConvers;
126 int iOffset = 0;
127 int iOffend = 0;
128 c_ConvChar.insert(c_ConvChar.begin(), L'S');
129 while (iOffset != -1) {
130 iOffend = c_ConvChar.find(L"%", iOffset + 1);
131 std::wstring strSub;
132 if (iOffend == -1)
133 strSub = c_ConvChar.substr(iOffset);
134 else
135 strSub = c_ConvChar.substr(iOffset, iOffend - iOffset);
136 c_strConvers.push_back(strSub);
137 iOffset = iOffend;
138 }
139
140 std::wstring c_strResult;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141 std::wstring c_strFormat;
142 for (int iIndex = 0; iIndex < (int)c_strConvers.size(); iIndex++) {
143 c_strFormat = c_strConvers[iIndex];
144 if (iIndex == 0) {
145 c_strResult = c_strFormat;
146 continue;
147 }
148
149 CFX_WideString strSegment;
150 if (iIndex >= iSize) {
151 c_strResult += c_strFormat;
152 continue;
153 }
154
tsepez86a61dc2016-03-25 10:00:11 -0700155 switch (ParseDataType(&c_strFormat)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 case UTIL_INT:
157 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt());
158 break;
159 case UTIL_DOUBLE:
160 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToDouble());
161 break;
162 case UTIL_STRING:
163 strSegment.Format(c_strFormat.c_str(),
164 params[iIndex].ToCFXWideString().c_str());
165 break;
166 default:
167 strSegment.Format(L"%S", c_strFormat.c_str());
168 break;
169 }
170 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1);
171 }
172
173 c_strResult.erase(c_strResult.begin());
174 vRet = c_strResult.c_str();
175 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700176}
177
Tom Sepezba038bc2015-10-08 12:03:00 -0700178FX_BOOL util::printd(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800179 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 CJS_Value& vRet,
181 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 int iSize = params.size();
183 if (iSize < 2)
184 return FALSE;
185
Tom Sepez67fd5df2015-10-08 12:24:19 -0700186 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepez86a61dc2016-03-25 10:00:11 -0700187 CJS_Value p1 = params[0];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 CJS_Value p2 = params[1];
Tom Sepez67fd5df2015-10-08 12:24:19 -0700189 CJS_Date jsDate(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 if (!p2.ConvertToDate(jsDate)) {
191 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1);
192 return FALSE;
193 }
194
195 if (!jsDate.IsValidDate()) {
196 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2);
197 return FALSE;
198 }
199
Tom Sepez39bfe122015-09-17 15:25:23 -0700200 if (p1.GetType() == CJS_Value::VT_number) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 CFX_WideString swResult;
tsepez86a61dc2016-03-25 10:00:11 -0700202 switch (p1.ToInt()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 case 0:
204 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(),
205 jsDate.GetMonth() + 1, jsDate.GetDay(),
206 jsDate.GetHours(), jsDate.GetMinutes(),
207 jsDate.GetSeconds());
208 break;
209 case 1:
210 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", jsDate.GetYear(),
211 jsDate.GetMonth() + 1, jsDate.GetDay(),
212 jsDate.GetHours(), jsDate.GetMinutes(),
213 jsDate.GetSeconds());
214 break;
215 case 2:
216 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", jsDate.GetYear(),
217 jsDate.GetMonth() + 1, jsDate.GetDay(),
218 jsDate.GetHours(), jsDate.GetMinutes(),
219 jsDate.GetSeconds());
220 break;
221 default:
tsepez86a61dc2016-03-25 10:00:11 -0700222 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSVALUEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 return FALSE;
224 }
225
226 vRet = swResult.c_str();
227 return TRUE;
228 }
tsepez86a61dc2016-03-25 10:00:11 -0700229
Tom Sepez39bfe122015-09-17 15:25:23 -0700230 if (p1.GetType() == CJS_Value::VT_string) {
tsepez86a61dc2016-03-25 10:00:11 -0700231 if (iSize > 2 && params[2].ToBool()) {
232 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_NOTSUPPORT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 return FALSE; // currently, it doesn't support XFAPicture.
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700234 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700235
tsepez86a61dc2016-03-25 10:00:11 -0700236 // Convert PDF-style format specifiers to wcsftime specifiers. Remove any
237 // pre-existing %-directives before inserting our own.
238 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString().c_str();
239 cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'),
240 cFormat.end());
241
242 for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 int iStart = 0;
244 int iEnd;
tsepez86a61dc2016-03-25 10:00:11 -0700245 while ((iEnd = cFormat.find(TbConvertTable[i].lpszJSMark, iStart)) !=
246 -1) {
247 cFormat.replace(iEnd, FXSYS_wcslen(TbConvertTable[i].lpszJSMark),
248 TbConvertTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 iStart = iEnd;
250 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700251 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252
tsepez86a61dc2016-03-25 10:00:11 -0700253 int iYear = jsDate.GetYear();
254 int iMonth = jsDate.GetMonth();
255 int iDay = jsDate.GetDay();
256 int iHour = jsDate.GetHours();
257 int iMin = jsDate.GetMinutes();
258 int iSec = jsDate.GetSeconds();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259
tsepez86a61dc2016-03-25 10:00:11 -0700260 TbConvertAdditional cTableAd[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 {L"m", iMonth + 1}, {L"d", iDay},
262 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
263 {L"M", iMin}, {L"s", iSec},
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700264 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265
tsepez86a61dc2016-03-25 10:00:11 -0700266 for (size_t i = 0; i < FX_ArraySize(cTableAd); ++i) {
267 wchar_t tszValue[16];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700269 sValue.Format(L"%d", cTableAd[i].iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1),
271 (sValue.GetLength() + 1) * sizeof(wchar_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 int iStart = 0;
274 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700275 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 if (iEnd > 0) {
277 if (cFormat[iEnd - 1] == L'%') {
278 iStart = iEnd + 1;
279 continue;
280 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700281 }
Lei Zhangb9c31972015-08-11 14:09:35 -0700282 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 iStart = iEnd;
284 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700285 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700286
tsepez86a61dc2016-03-25 10:00:11 -0700287 struct tm time = {};
288 time.tm_year = iYear - 1900;
289 time.tm_mon = iMonth;
290 time.tm_mday = iDay;
291 time.tm_hour = iHour;
292 time.tm_min = iMin;
293 time.tm_sec = iSec;
294
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700295 wchar_t buf[64] = {};
tsepez86a61dc2016-03-25 10:00:11 -0700296 wcsftime(buf, 64, cFormat.c_str(), &time);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700297 cFormat = buf;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 vRet = cFormat.c_str();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700299 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300 }
tsepez86a61dc2016-03-25 10:00:11 -0700301
302 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSTYPEERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700304}
305
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700306
Tom Sepezba038bc2015-10-08 12:03:00 -0700307FX_BOOL util::printx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800308 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309 CJS_Value& vRet,
310 CFX_WideString& sError) {
tsepez4f1f41f2016-03-28 14:13:16 -0700311 if (params.size() < 2) {
312 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 return FALSE;
tsepez4f1f41f2016-03-28 14:13:16 -0700314 }
315 vRet = printx(params[0].ToCFXWideString(), params[1].ToCFXWideString());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316 return TRUE;
317}
318
tsepez4f1f41f2016-03-28 14:13:16 -0700319enum CaseMode { kPreserveCase, kUpperCase, kLowerCase };
320
321static FX_WCHAR TranslateCase(FX_WCHAR input, CaseMode eMode) {
322 if (eMode == kLowerCase && input >= 'A' && input <= 'Z')
323 return input | 0x20;
324 if (eMode == kUpperCase && input >= 'a' && input <= 'z')
325 return input & ~0x20;
326 return input;
327}
328
329CFX_WideString util::printx(const CFX_WideString& wsFormat,
330 const CFX_WideString& wsSource) {
331 CFX_WideString wsResult;
332 FX_STRSIZE iSourceIdx = 0;
333 FX_STRSIZE iFormatIdx = 0;
334 CaseMode eCaseMode = kPreserveCase;
335 bool bEscaped = false;
336 while (iFormatIdx < wsFormat.GetLength()) {
337 if (bEscaped) {
338 bEscaped = false;
339 wsResult += wsFormat[iFormatIdx];
340 ++iFormatIdx;
341 continue;
342 }
343 switch (wsFormat[iFormatIdx]) {
344 case '\\': {
345 bEscaped = true;
346 ++iFormatIdx;
347 } break;
348 case '<': {
349 eCaseMode = kLowerCase;
350 ++iFormatIdx;
351 } break;
352 case '>': {
353 eCaseMode = kUpperCase;
354 ++iFormatIdx;
355 } break;
356 case '=': {
357 eCaseMode = kPreserveCase;
358 ++iFormatIdx;
359 } break;
360 case '?': {
361 if (iSourceIdx < wsSource.GetLength()) {
362 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
363 ++iSourceIdx;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700364 }
tsepez4f1f41f2016-03-28 14:13:16 -0700365 ++iFormatIdx;
366 } break;
367 case 'X': {
368 if (iSourceIdx < wsSource.GetLength()) {
369 if ((wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') ||
370 (wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') ||
371 (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) {
372 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
373 ++iFormatIdx;
374 }
375 ++iSourceIdx;
376 } else {
377 ++iFormatIdx;
378 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 } break;
380 case 'A': {
tsepez4f1f41f2016-03-28 14:13:16 -0700381 if (iSourceIdx < wsSource.GetLength()) {
382 if ((wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') ||
383 (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) {
384 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
385 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386 }
tsepez4f1f41f2016-03-28 14:13:16 -0700387 ++iSourceIdx;
388 } else {
389 ++iFormatIdx;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700390 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391 } break;
392 case '9': {
tsepez4f1f41f2016-03-28 14:13:16 -0700393 if (iSourceIdx < wsSource.GetLength()) {
394 if (wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') {
395 wsResult += wsSource[iSourceIdx];
396 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 }
tsepez4f1f41f2016-03-28 14:13:16 -0700398 ++iSourceIdx;
399 } else {
400 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401 }
tsepez4f1f41f2016-03-28 14:13:16 -0700402 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 case '*': {
tsepez4f1f41f2016-03-28 14:13:16 -0700404 if (iSourceIdx < wsSource.GetLength()) {
405 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
406 ++iSourceIdx;
407 } else {
408 ++iFormatIdx;
409 }
410 } break;
411 default: {
412 wsResult += wsFormat[iFormatIdx];
413 ++iFormatIdx;
414 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415 }
416 }
tsepez4f1f41f2016-03-28 14:13:16 -0700417 return wsResult;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700418}
419
Tom Sepezba038bc2015-10-08 12:03:00 -0700420FX_BOOL util::scand(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800421 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700422 CJS_Value& vRet,
423 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424 int iSize = params.size();
425 if (iSize < 2)
426 return FALSE;
427
428 CFX_WideString sFormat = params[0].ToCFXWideString();
429 CFX_WideString sDate = params[1].ToCFXWideString();
430 double dDate = JS_GetDateTime();
431 if (sDate.GetLength() > 0) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800432 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 }
434
435 if (!JS_PortIsNan(dDate)) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700436 vRet = CJS_Date(CJS_Runtime::FromContext(cc), dDate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437 } else {
438 vRet.SetNull();
439 }
440
441 return TRUE;
442}
443
Tom Sepezba038bc2015-10-08 12:03:00 -0700444FX_BOOL util::byteToChar(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800445 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 CJS_Value& vRet,
447 CFX_WideString& sError) {
tsepez90d87792016-03-29 09:21:54 -0700448 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
449 if (params.size() < 1) {
450 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700451 return FALSE;
tsepez90d87792016-03-29 09:21:54 -0700452 }
453 int arg = params[0].ToInt();
454 if (arg < 0 || arg > 255) {
455 sError = JSGetStringFromID(pContext, IDS_STRING_JSVALUEERROR);
456 return FALSE;
457 }
458 CFX_WideString wStr(static_cast<FX_WCHAR>(arg));
459 vRet = wStr.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700461}