2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dpl/scoped_array.h>
19 #include <dpl/scoped_free.h>
20 #include "ScopedJSStringRef.h"
21 #include "Converter.h"
22 #include <dpl/log/log.h>
24 using namespace WrtDeviceApis::Commons;
26 namespace WrtDeviceApis {
27 namespace CommonsJavaScript {
29 Converter::Converter(JSContextRef context) : m_context(context)
33 Converter::~Converter()
37 JSObjectRef Converter::toJSObjectRef(const JSValueRef& arg)
40 Throw(NullPointerException);
42 JSValueRef exception = NULL;
43 JSObjectRef result = JSValueToObject(m_context, arg, &exception);
44 if ((NULL == result) || (NULL != exception)) {
45 ThrowMsg(ConversionException, "Couldn't convert to a JS Object.");
50 bool Converter::toBool(const JSValueRef& arg)
52 return JSValueToBoolean(m_context, arg);
55 unsigned char Converter::toUChar(const JSValueRef& arg)
57 return static_cast<unsigned char>(toChar(arg));
60 char Converter::toChar(const JSValueRef& arg)
62 double tmp = toNumber_(arg);
63 return (isNan(tmp) ? 0 : static_cast<char>(tmp));
66 int Converter::toInt(const JSValueRef& arg)
68 double tmp = toNumber_(arg);
69 return (isNan(tmp) ? 0 : static_cast<int>(tmp));
72 int Converter::toInt(const std::string &arg)
74 return static_cast<int>(toNumber_(arg));
77 long Converter::toLong(const JSValueRef& arg)
79 double tmp = toNumber_(arg);
80 return (isNan(tmp) ? 0 : static_cast<long>(tmp));
83 long long Converter::toLongLong(const JSValueRef& arg)
85 double tmp = toNumber_(arg);
86 return (isNan(tmp) ? 0 : static_cast<long long>(tmp));
89 unsigned long Converter::toULong(const JSValueRef& arg)
91 return static_cast<unsigned long>(toLong(arg));
94 std::size_t Converter::toSizeT(const JSValueRef& arg)
96 double tmp = toNumber_(arg);
97 return (isNan(tmp) ? 0 : static_cast<std::size_t>(tmp));
100 std::size_t Converter::toSizeT(const JSStringRef& arg)
102 return static_cast<std::size_t>(toNumber_(arg));
105 double Converter::toDouble(const JSValueRef& arg)
107 return toNumber_(arg);
110 std::string Converter::toString(int arg)
112 return toString_(arg);
115 std::string Converter::toString(unsigned long arg)
117 return toString_(arg);
120 std::string Converter::toString(long arg)
122 return toString_(arg);
125 std::string Converter::toString(std::size_t arg)
127 return toString_(arg);
130 std::string Converter::toString(const JSValueRef& arg)
132 return toString_(arg);
135 std::string Converter::tryString(const JSValueRef& arg)
137 if (!JSValueIsString(m_context, arg) &&
138 !JSValueIsNumber(m_context, arg) &&
139 !JSValueIsBoolean(m_context, arg)) {
140 ThrowMsg(ConversionException,
141 "Argument is not convertable to JS String.");
144 return toString_(arg);
147 std::string Converter::toDateString(const JSValueRef& arg)
149 if (!JSValueIsObject(m_context, arg)) {
150 ThrowMsg(ConversionException, "Argument is not JS Date.");
153 return toString_(arg);
156 std::string Converter::toString(const JSStringRef& arg)
159 size_t jsSize = JSStringGetMaximumUTF8CStringSize(arg);
162 DPL::ScopedArray<char> buffer(new char[jsSize]);
163 size_t written = JSStringGetUTF8CString(arg, buffer.Get(), jsSize);
164 if (written > jsSize) {
165 ThrowMsg(ConversionException,
166 "Conversion could not be fully performed.");
168 result = buffer.Get();
174 time_t Converter::toDateTimeT(const JSValueRef& arg)
176 struct tm tm = toDateTm(arg);
180 tm Converter::toDateTm(const JSValueRef& arg)
182 std::string stringDate = toDateString(arg);
184 char* currentLocale = setlocale(LC_TIME, NULL);
185 if (currentLocale == NULL) {
186 ThrowMsg(ConversionException, "Couldn't get current locale.");
188 DPL::ScopedFree<char> currentLocaleCopy(strdup(currentLocale));
189 if (setlocale(LC_TIME, "C") == NULL) {
190 ThrowMsg(ConversionException, "Couldn't set POSIX locale.");
192 if (strptime(stringDate.c_str(), "%a %b %d %Y %T", &result) == NULL) {
193 ThrowMsg(ConversionException, "Couldn't convert supplied date.");
195 if (setlocale(LC_TIME, currentLocaleCopy.Get()) == NULL) {
196 ThrowMsg(ConversionException, "Couldn't set previous locale back.");
198 //strptime function doesn't affect tm_isdst flag.
199 //It may cause -1hr conversion error. Setting it to -1 informs that
200 //there is no information about daylight saving time.
201 result.tm_isdst = -1;
205 std::vector<unsigned char> Converter::toVectorOfUChars(const JSValueRef& arg)
207 return toVectorOfT_(arg, &Converter::toUChar);
210 std::vector<char> Converter::toVectorOfChars(const JSValueRef& arg)
212 return toVectorOfT_(arg, &Converter::toChar);
215 std::vector<int> Converter::toVectorOfInts(const JSValueRef& arg)
217 return toVectorOfT_(arg, &Converter::toInt);
220 std::vector<std::string> Converter::toVectorOfStrings(const JSValueRef& arg)
222 return toVectorOfT_(arg, &Converter::toString);
225 std::vector<time_t> Converter::toVectorOfTimeT(const JSValueRef& arg) {
226 return toVectorOfT_(arg, &Converter::toDateTimeT);
229 JSValueRef Converter::toJSValueRef(bool arg)
231 return JSValueMakeBoolean(m_context, arg);
234 JSValueRef Converter::toJSValueRef(int arg)
236 return JSValueMakeNumber(m_context, arg);
239 JSValueRef Converter::toJSValueRef(unsigned int arg)
241 return JSValueMakeNumber(m_context, arg);
244 JSValueRef Converter::toJSValueRef(unsigned long arg)
246 return JSValueMakeNumber(m_context, arg);
249 JSValueRef Converter::toJSValueRefLong(const long arg) {
250 return JSValueMakeNumber(m_context, arg);
253 JSValueRef Converter::toJSValueRef(long long int arg)
255 return JSValueMakeNumber(m_context, arg);
258 JSValueRef Converter::toJSValueRef(double arg)
260 return JSValueMakeNumber(m_context, arg);
263 JSValueRef Converter::toJSValueRef(const std::string& arg)
265 JSValueRef result = NULL;
266 JSStringRef jsString = JSStringCreateWithUTF8CString(arg.c_str());
267 result = JSValueMakeString(m_context, jsString);
268 JSStringRelease(jsString);
272 JSValueRef Converter::toJSValueRef(const tm& arg)
275 args[0] = JSValueMakeNumber(m_context, arg.tm_year + 1900);
276 args[1] = JSValueMakeNumber(m_context, arg.tm_mon);
277 args[2] = JSValueMakeNumber(m_context, arg.tm_mday);
278 args[3] = JSValueMakeNumber(m_context, arg.tm_hour);
279 args[4] = JSValueMakeNumber(m_context, arg.tm_min);
280 args[5] = JSValueMakeNumber(m_context, arg.tm_sec);
282 JSValueRef exception = NULL;
283 JSObjectRef result = JSObjectMakeDate(m_context, 6, args, &exception);
284 if (NULL != exception) {
285 ThrowMsg(ConversionException, "Couldn't convert to a JS Date.");
291 JSValueRef Converter::toJSValueRef(const time_t arg)
293 struct tm *tminfo = localtime(&arg);
294 return toJSValueRef(*tminfo);
297 JSValueRef Converter::toJSValueRef(const std::vector<std::string>& arg)
299 return toJSValueRef_(arg);
302 JSValueRef Converter::toJSValueRef(const char* arg)
304 return toJSValueRef(std::string(arg));
307 JSValueRef Converter::toJSValueRef(const std::vector<int>& arg) {
308 return toJSValueRef_(arg);
311 JSValueRef Converter::toJSValueRef(const std::vector<time_t>& arg) {
312 return toJSValueRef_(arg);
315 JSValueRef Converter::toJSValueRef(JSValueRef arg)
320 JSValueRef Converter::toJSValueRef(const std::vector<JSValueRef>& arg)
322 return toJSValueRef_(arg);
325 JSStringRef Converter::toJSStringRef(const std::string& arg)
327 return JSStringCreateWithUTF8CString(arg.c_str());
330 double Converter::toNumber_(const JSStringRef& arg)
332 return toNumber_(toString(arg));
335 double Converter::toNumber_(const std::string& arg)
338 std::stringstream ss(arg);
339 if (!(ss >> result)) {
340 Throw(ConversionException);
345 double Converter::toNumber_(const JSValueRef& arg)
347 JSValueRef exception = NULL;
348 double result = JSValueToNumber(m_context, arg, &exception);
349 if (NULL != exception) {
350 ThrowMsg(ConversionException, "Couldn't convert to a number.");
355 std::string Converter::toString_(const JSValueRef& arg)
359 JSValueRef exception = NULL;
360 JSStringRef str = JSValueToStringCopy(m_context, arg, &exception);
362 ThrowMsg(ConversionException, "Couldn't cast to a string.");
365 size_t jsSize = JSStringGetMaximumUTF8CStringSize(str);
367 DPL::ScopedArray<char> buffer(new char[jsSize]);
368 JSStringGetUTF8CString(str, buffer.Get(), jsSize);
369 result = buffer.Get();
371 JSStringRelease(str);
375 bool Converter::isNan(double value) const
377 // This should work unless -ffast-math (GCC) option is used.
378 STATIC_CHECK(std::numeric_limits<double>::is_iec559,
379 NOT_IEEE754_REPRESENTATION);
380 return (value != value);
383 } // CommonsJavaScript