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.
16 #ifndef WRTDEVICEAPIS_COMMONSJAVASCRIPT_CONVERTER_H_
17 #define WRTDEVICEAPIS_COMMONSJAVASCRIPT_CONVERTER_H_
24 #include <JavaScriptCore/JavaScript.h>
25 #include <dpl/noncopyable.h>
26 #include <dpl/shared_ptr.h>
27 #include <Commons/Exception.h>
28 #include <Commons/StaticAssert.h>
30 namespace WrtDeviceApis {
31 namespace CommonsJavaScript {
32 class Converter : private DPL::Noncopyable
35 explicit Converter(JSContextRef context);
39 * Converts JSValueRef to JSObjectRef.
40 * @param arg JSValueRef object to convert.
41 * @return JSObjectRef object.
42 * @throw ConversionException Thrown when conversion fails.
44 JSObjectRef toJSObjectRef(const JSValueRef& arg);
47 * Converts JSValueRef object to boolean value.
48 * @param arg JSValueRef object to convert.
49 * @return Boolean value.
50 * @throw ConversionException Thrown when conversion fails.
52 bool toBool(const JSValueRef& arg);
55 * Converts JSValueRef object to unsigned char value.
56 * @param arg JSValueRef object to convert.
57 * @return Unsigned char value.
58 * @throw ConversionException Thrown when conversion fails.
60 unsigned char toUChar(const JSValueRef& arg);
63 * Converts JSValueRef object to char value.
64 * @param arg JSValueRef object to convert.
66 * @throw ConversionException Thrown when conversion fails.
68 char toChar(const JSValueRef& arg);
71 * Converts JSValueRef to integer value.
72 * @param arg JSValueRef object to convert.
73 * @return Integer value.
74 * @throw ConversionException Thrown when conversion fails.
76 int toInt(const JSValueRef& arg);
79 * Converts string to integer value.
80 * @param arg string to convert.
81 * @return Integer value.
82 * @throw ConversionException Thrown when conversion fails.
84 int toInt(const std::string& arg);
87 * Converts JSvalueRef to long integer value.
88 * @param arg JSValueRef object to convert.
89 * @return Long integer value.
90 * @throw ConversionException Thrown when conversion fails.
92 long toLong(const JSValueRef& arg);
95 * Converts JSvalueRef to long long integer value.
96 * @param arg JSValueRef object to convert.
97 * @return Long long integer value.
98 * @throw ConversionException Thrown when conversion fails.
100 long long toLongLong(const JSValueRef& arg);
102 * Converts JSValueRef to unsigned long value.
103 * @param arg JSValueRef object to convert.
104 * @return Unsigned long value.
105 * @throw ConversionException Thrown when conversion fails.
107 unsigned long toULong(const JSValueRef& arg);
110 * Converts JSValueRef to double value.
111 * @param arg JSValueRef object to convert.
112 * @return Double value.
113 * @throw ConversionException Thrown when conversion fails.
115 double toDouble(const JSValueRef& arg);
118 * Converts JSValueRef to size_t value.
119 * @param arg JSValueRef object to convert.
120 * @return Size_t value.
121 * @throw ConversionException Thrown when conversion fails.
123 std::size_t toSizeT(const JSValueRef& arg);
126 * Converts JSStringRef to size_t value.
127 * @param arg JSStringRef object to convert.
128 * @return Size_t value.
129 * @throw ConversionException Thrown when conversion fails.
131 std::size_t toSizeT(const JSStringRef& arg);
134 * Converts int to STL string.
135 * @param arg int to convert.
136 * @return STL string.
137 * @throw ConversionException Thrown when conversion fails.
139 std::string toString(int arg);
141 std::string toString(unsigned long arg);
143 std::string toString(long arg);
146 std::string toString(std::size_t arg);
150 * Converts JSValueRef to STL string.
151 * @param arg JSValueRef to convert.
152 * @return STL string.
153 * @throw ConversionException Thrown when conversion fails.
154 * @remarks If one wants to convert JS Date object to a string then
155 * toDateString() should be used.
157 std::string toString(const JSValueRef& arg);
160 * Converts JSStringRef to STL string.
161 * @param arg JSStringRef to convert.
162 * @return STL string.
163 * @throw ConversionException Thrown when conversion fails.
165 std::string toString(const JSStringRef& arg);
168 * Converts JSSValueRef to STL string if
169 * arg is one of: String, Number, Bool
170 * @param arg JSValueReg to convert.
171 * @return STL string.
172 * @throw ConversionException Thrown when conversion fails.
174 std::string tryString(const JSValueRef& arg);
177 * Converts JS Date object to STL string.
178 * @param arg JS Date object to convert.
179 * @return STL string.
180 * @throw ConversionException Thrown when conversion fails.
182 std::string toDateString(const JSValueRef& arg);
185 * Converts JSValueRef to time_t value.
186 * @param arg JSValueRef to convert.
187 * @return time_t value.
188 * @throw ConversionException Thrown when conversion fails.
190 time_t toDateTimeT(const JSValueRef& arg);
193 * Converts JSValueRef to tm struct.
194 * @param arg JSValueRef to convert.
196 * @throw ConversionException Thrown when conversion fails.
198 tm toDateTm(const JSValueRef& arg);
201 * Converts JSValueRef to vector<unsigned char>.
202 * @param arg JSValueRef to convert.
203 * @return vector<unsigned char>.
204 * @throw ConversionException Thrown when conversion fails.
206 std::vector<unsigned char> toVectorOfUChars(const JSValueRef& arg);
209 * Converts JSValueRef to vector<char>.
210 * @param arg JSValueRef to convert.
211 * @return vector<char>.
212 * @throw ConversionException Thrown when conversion fails.
214 std::vector<char> toVectorOfChars(const JSValueRef& arg);
217 * Converts JSValueRef to vector<int>.
218 * @param arg JSValueRef to convert.
219 * @return vector<int>.
220 * @throw ConversionException Thrown when conversion fails.
222 std::vector<int> toVectorOfInts(const JSValueRef& arg);
225 * Converts JSValueRef to vector of string.
226 * @param arg JSValueRef to convert.
227 * @return std::vector<std::string>
228 * @throw ConversionException Thrown when conversion fails.
230 std::vector<std::string> toVectorOfStrings(const JSValueRef& arg);
233 * Converts JSValueRef to vector of time_t.
234 * @param arg JSValueRef to convert.
235 * @return std::vector<std::time_t>
236 * @throw ConversionException Thrown when conversion fails.
238 std::vector<std::time_t> toVectorOfTimeT(const JSValueRef& arg);
241 * Converts boolean value to JSValueRef.
242 * @param arg Boolean value to convert.
243 * @return JSValueRef object.
244 * @throw ConversionException Thrown when conversion fails.
246 JSValueRef toJSValueRef(bool arg);
249 * Converts integer value to JSValueRef.
250 * @param arg Integer value to convert.
251 * @return JSValueRef object.
252 * @throw ConversionException Thrown when conversion fails.
254 JSValueRef toJSValueRef(int arg);
257 * Converts unsigned integer value to JSValueRef.
258 * @param arg Unsigned integer value to convert.
259 * @return JSValueRef object.
260 * @throw ConversionException Thrown when conversion fails.
262 JSValueRef toJSValueRef(unsigned int arg);
265 * Converts double value to JSValueRef.
266 * @param arg Double value to convert.
267 * @return JSValueRef object.
268 * @throw ConversionException Thrown when conversion fails.
270 JSValueRef toJSValueRef(double arg);
273 * Converts unsigned long value to JSValueRef.
274 * @param arg unsigned long value to convert.
275 * @return JSValueRef object.
276 * @throw ConversionException Thrown when conversion fails.
278 JSValueRef toJSValueRef(unsigned long arg);
281 * Converts long value to JSValueRef.
282 * @param arg long value to convert.
283 * @return JSValueRef object.
284 * @throw ConversionException Thrown when conversion fails.
286 JSValueRef toJSValueRefLong(const long arg);
289 * Converts unsigned long value to JSValueRef.
290 * @param arg long long int value to convert.
291 * @return JSValueRef object.
292 * @throw ConversionException Thrown when conversion fails.
294 JSValueRef toJSValueRef(long long int arg);
297 * Converts STL string to JSValueRef.
298 * @param arg STL string to convert.
299 * @return JSValueRef object.
300 * @throw ConversionException Thrown when conversion fails.
302 JSValueRef toJSValueRef(const std::string& arg);
305 * Converts char sequence to JSValueRef.
306 * @param arg char sequence to convert.
307 * @return JSValueRef object.
308 * @throw ConversionException Thrown when conversion fails.
310 JSValueRef toJSValueRef(const char* arg);
313 * Converts time_t value to JSValueRef.
314 * @param arg time_t value to convert.
315 * @return JSValueRef object.
316 * @throw ConversionException Thrown when conversion fails.
318 JSValueRef toJSValueRef(const time_t arg);
321 * Converts tm struct to JSValueRef.
322 * @param arg tm struct to convert.
323 * @return JSValueRef object.
324 * @throw ConversionException Thrown when conversion fails.
326 JSValueRef toJSValueRef(const tm& arg);
329 * Converts STL vector of integer to JSValueRef.
330 * @param arg STL vector of integer to convert.
331 * @return JSValueRef object.
332 * @throw ConversionException Thrown when conversion fails.
334 JSValueRef toJSValueRef(const std::vector<int>& arg);
337 * Converts STL vector of time_t to JSValueRef.
338 * @param arg STL vector of time_t to convert.
339 * @return JSValueRef object.
340 * @throw ConversionException Thrown when conversion fails.
342 JSValueRef toJSValueRef(const std::vector<time_t>& arg);
345 * Converts STL vector of string to JSStringRef.
346 * @param arg STL string to convert.
347 * @return JSValueRef object.
348 * @throw ConversionException Thrown when conversion fails.
350 JSValueRef toJSValueRef(const std::vector<std::string>& arg);
353 * Converts JSValueRef to JSValueRef
354 * @param JSValueRef js value which is returned
355 * @return JSValueRef - parameter
356 * @throw ConversionException Thrown when conversion fails.
358 JSValueRef toJSValueRef(JSValueRef arg);
361 * Converts std::vector to JSValueRef
362 * @param arg vecotr of JSValueRefs
363 * @return JSValueRef - js array
364 * @throw ConversionException Thrown when conversion fails.
366 JSValueRef toJSValueRef(const std::vector<JSValueRef>& arg);
369 * Template specialization to catch errors caused
370 * by wrong usage of general template or
371 * implicit conversions from SharedPtr to
374 template<class Class>
375 JSValueRef toJSValueRef(const DPL::SharedPtr<Class>& c)
377 STATIC_ERROR(ERROR_INVALID_IMPLICIT_CONVERSION, c)
378 //above macro causes compilation error
379 return static_cast<JSValueRef>(NULL);
383 * Converts STL string to JSStringRef.
384 * @param arg STL string to convert.
385 * @return JSStringRef object.
386 * @throw ConversionException Thrown when conversion fails.
388 JSStringRef toJSStringRef(const std::string& arg);
391 bool isNan(double value) const;
393 double toNumber_(const JSValueRef& arg);
395 double toNumber_(const JSStringRef& arg);
397 double toNumber_(const std::string& arg);
399 template<typename T, class Derived>
400 std::vector<T> toVectorOfT_(const JSValueRef& arg,
401 T (Derived::*f)(const JSValueRef &),
404 if (JSValueIsNull(m_context,
405 arg) || JSValueIsUndefined(m_context, arg))
407 return std::vector<T>();
410 if (!JSIsArrayValue(m_context, arg)) {
411 ThrowMsg(Commons::ConversionException,
412 "Argument is not an JS array.");
415 std::vector<T> result;
416 JSObjectRef objArg = toJSObjectRef(arg);
417 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
418 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
419 result.push_back((object->*f)(element));
425 std::vector<T> toVectorOfT_(const JSValueRef& arg,
426 T (Converter::*f)(const JSValueRef &))
428 if (JSValueIsNull(m_context,
429 arg) || JSValueIsUndefined(m_context, arg))
431 return std::vector<T>();
434 if (!JSIsArrayValue(m_context, arg)) {
435 ThrowMsg(Commons::ConversionException,
436 "Argument is not an JS array.");
439 std::vector<T> result;
440 JSObjectRef objArg = toJSObjectRef(arg);
441 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
442 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
443 result.push_back((this->*f)(element));
448 template<class T, class Derived>
449 JSValueRef toJSValueRef_(const std::vector<T>& arg,
450 JSValueRef (Derived::*f)(const T &),
453 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
455 if (NULL == jsResult) {
456 ThrowMsg(WrtDeviceApis::Commons::NullPointerException,
457 "Could not create js array object");
460 for (std::size_t i = 0; i < arg.size(); ++i) {
461 JSValueRef tmpVal = (object->*f)(arg[i]);
462 if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
463 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
464 "Could not insert value into js array");
472 JSValueRef toJSValueRef_(const std::vector<T>& arg)
474 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
476 if (NULL == jsResult) {
477 ThrowMsg(WrtDeviceApis::Commons::NullPointerException,
478 "Could not create js array object");
481 for (std::size_t i = 0; i < arg.size(); ++i) {
482 JSValueRef tmpVal = toJSValueRef(arg[i]);
483 if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
484 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
485 "Could not insert value into js array");
493 JSValueRef toJSValueRef_(const std::list<T>& arg)
495 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
497 if (NULL == jsResult) {
498 ThrowMsg(WrtDeviceApis::Commons::NullPointerException,
499 "Could not create js array object");
502 typename std::list<T>::const_iterator it = arg.begin();
503 for (std::size_t i = 0; it != arg.end(); ++i, ++it) {
504 JSValueRef tmpVal = toJSValueRef(*it);
505 if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
506 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
507 "Could not insert value into js array");
514 template<class T, class Derived>
515 JSValueRef toJSValueRef_(const std::list<T>& arg,
516 JSValueRef (Derived::*f)(const T &),
519 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
521 if (NULL == jsResult) {
522 ThrowMsg(WrtDeviceApis::Commons::NullPointerException,
523 "Could not create js array object");
526 typename std::list<T>::const_iterator it = arg.begin();
527 for (std::size_t i = 0; it != arg.end(); ++i, ++it) {
528 JSValueRef tmpVal = (object->*f)(*it);
529 if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
530 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
531 "Could not insert value into js array");
538 std::string toString_(const JSValueRef& arg);
541 std::string toString_(const T& arg)
543 std::stringstream ss;
545 ThrowMsg(WrtDeviceApis::Commons::ConversionException,
546 "Could not convert to string.");
552 JSContextRef m_context;
556 class ConverterFactory : private DPL::Noncopyable
560 * Converter type which deletes itself when gets out of scope.
562 typedef DPL::SharedPtr<C> ConverterType;
566 * Gets converter object.
567 * @param context JavaScript context the conversion will be performed in.
568 * @param[out] exception JavaScript value for storing exception.
569 * @return Converter object.
571 static ConverterType getConverter(JSContextRef context)
573 C* convert = new C(context);
574 return ConverterType(convert);
581 typedef ConverterFactory<Converter> BasicConverterFactory;
583 typedef BasicConverterFactory::ConverterType BasicConverter;
584 } // CommonsJavaScript
587 #endif /* WRTDEVICEAPIS_COMMONSJAVASCRIPT_CONVERTER_H_ */