Update change log and spec for wrt-plugins-tizen_0.4.49
[framework/web/wrt-plugins-tizen.git] / src / Common / JSUtil.h
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #ifndef _JSUTIL_H_
19 #define _JSUTIL_H_
20 #include <JavaScriptCore/JavaScript.h>
21 #include <string>
22 #include <map>
23 #include <vector>
24 #include "PlatformException.h"
25 #include <ctime>
26
27
28 namespace DeviceAPI {
29 namespace Common{
30
31 class JSUtil{
32 public:
33     /**
34      * @brief Gets a property from an object.
35      *
36      * @remarks
37      * if pass NULL in exception, when occurred error, it throw C++ exception(TypeMismatchException).
38      *
39      * @param[in] ctx The execution context to use.
40      * @param[in] object The JSObject whose property you want to get.
41      * @param[in] name The name of property
42      * @param[out] exception A pointer to a JSValueRef in which to store an exception, if any.
43      *
44      * @exception TypeMismatchException
45      */
46     static JSValueRef getProperty(JSContextRef ctx , JSObjectRef object, const char *name, JSValueRef *exception=NULL);
47
48     /**
49      * @brief Sets a property on an object.
50      *
51      * @remarks
52      * if pass NULL in exception, when occurred error, it throw C++ exception(TypeMismatchException).
53      *
54      * @param[in] ctx The execution context to use.
55      * @param[in] object The JSObject whose property you want to set.
56      * @param[in] name The name of property
57      * @param[in] attributes A logically ORed set of JSPropertyAttributes to give to the property.
58      * @param[out] exception A pointer to a JSValueRef in which to store an exception, if any.
59      *
60      * @exception TypeMismatchException
61      */
62     static void setProperty(JSContextRef ctx , JSObjectRef object, const char *name, JSValueRef value, JSPropertyAttributes attributes, JSValueRef *exception=NULL);
63
64     /**
65      * @brief Converts a JavaScript string to STL string
66      *
67      * @param[in] ctx The execution context to use.
68      * @param[in] value The JSString to convert.
69      *
70      * @return A STL string with the result of conversion
71      */
72     static std::string JSStringToString(JSContextRef ctx, JSStringRef str);
73
74     /**
75      * @brief Converts a JavaScript value to STL string
76      *
77      * @param[in] ctx The execution context to use.
78      * @param[in] value The JSValue to convert.
79      *
80      * @return A STL string with the result of conversion
81      *
82      * @exception TypeMismatchException
83      */
84     static std::string JSValueToString(JSContextRef ctx, JSValueRef value);
85
86     /**
87      * @brief Converts a JavaScript value to long number and returns the resulting long number.
88      *
89      * @param[in] ctx The execution context to use.
90      * @param[in] value The JSValue to convert.
91      *
92      * @return The result of conversion
93      *
94      * @exception TypeMismatchException
95      */
96     static long JSValueToLong(JSContextRef ctx, JSValueRef value);
97
98     /**
99      * @brief Converts a JavaScript value to unsigned long number and returns the resulting unsigned long number.
100      *
101      * @param[in] ctx The execution context to use.
102      * @param[in] value The JSValue to convert.
103      *
104      * @return The result of conversion
105      *
106      * @exception TypeMismatchException
107      */
108     static unsigned long JSValueToULong(JSContextRef ctx, JSValueRef value);
109
110
111     /**
112      * @brief Converts a JavaScript value to long long number and returns the resulting long long number.
113      *
114      * @param[in] ctx The execution context to use.
115      * @param[in] value The JSValue to convert.
116      *
117      * @return The result of conversion
118      *
119      * @exception TypeMismatchException
120      */
121     static long long JSValueToLongLong(JSContextRef ctx, JSValueRef value);
122
123     /**
124      * @brief Converts a JavaScript value to unsigned long long number and returns the resulting unsigned long long number.
125      *
126      * @param[in] ctx The execution context to use.
127      * @param[in] value The JSValue to convert.
128      *
129      * @return The result of conversion
130      *
131      * @exception TypeMismatchException
132      */
133     static unsigned long long JSValueToULongLong(JSContextRef ctx, JSValueRef value);
134
135
136     /**
137      * @brief Converts a JavaScript value to double number and returns the resulting double number.
138      *
139      * @remarks TypeMismatchException is thrown when the result of conversion was NaN(Not a Number).
140      *
141      * @param[in] ctx The execution context to use.
142      * @param[in] value The JSValue to convert.
143      *
144      * @return The result of conversion
145      *
146      * @exception TypeMismatchException
147      */
148     static double JSValueToDouble(JSContextRef ctx, JSValueRef value);
149
150     /**
151      * @brief Converts a JavaScript value to number and returns the resulting number.
152      *
153      * @param[in] ctx The execution context to use.
154      * @param[in] value The JSValue to convert.
155      *
156      * @return The result of conversion
157      *
158      * @exception TypeMismatchException
159      */
160     static double JSValueToNumber(JSContextRef ctx, JSValueRef value);
161
162
163     /**
164      * @brief Converts a JavaScript value to byte(signed) number and returns the resulting byte(signed) number.
165      *
166      * @param[in] ctx The execution context to use.
167      * @param[in] value The JSValue to convert.
168      *
169      * @return The result of conversion
170      *
171      * @exception TypeMismatchException
172      */
173     static signed char JSValueToByte(JSContextRef ctx, JSValueRef value);
174
175     /**
176      * @brief Converts a JavaScript value to Octet(unsigned) number and returns the resulting Octet(unsigned) number.
177      *
178      * @param[in] ctx The execution context to use.
179      * @param[in] value The JSValue to convert.
180      *
181      * @return The result of conversion
182      *
183      * @exception TypeMismatchException
184      */
185     static unsigned char JSValueToOctet(JSContextRef ctx, JSValueRef value);
186
187
188     /**
189      * @brief Converts a JavaScript value to boolean and returns the resulting bollean
190      *
191      *
192      * @param[in] ctx The execution context to use.
193      * @param[in] value The JSValue to convert.
194      *
195      * @return The result of conversion
196      */
197     static bool JSValueToBoolean(JSContextRef ctx, JSValueRef value);
198
199     /**
200      * @brief Converts a JavaScript value to time_t and returns the resulting time_t.
201      *
202      * @remarks TypeMismatchException is thrown when the value was not Date type.
203      *
204      * @param[in] ctx The execution context to use.
205      * @param[in] value The JSValue to convert.
206      *
207      * @return The result of conversion
208      *
209      * @exception TypeMismatchException
210      */
211     static time_t JSValueToTimeT(JSContextRef ctx, JSValueRef value);
212
213     /**
214      * @brief Converts a JavaScript value to tm and returns the resulting tm.
215      *
216      * @remarks TypeMismatchException is thrown when the value was not Date type.
217      *
218      * @param[in] ctx The execution context to use.
219      * @param[in] value The JSValue to convert.
220      *
221      * @return The result of conversion
222      *
223      * @exception TypeMismatchException
224      */
225     static std::tm JSValueToDateTm(JSContextRef ctx, JSValueRef value);
226
227     /**
228      * @brief Converts a JavaScript value to tm as UTC time and returns the resulting tm.
229      *
230      * @remarks TypeMismatchException is thrown when the value was not Date type.
231      *
232      * @param[in] ctx The execution context to use.
233      * @param[in] value The JSValue to convert.
234      *
235      * @return The result of conversion
236      *
237      * @exception TypeMismatchException
238      */
239     static std::tm JSValueToDateTmUTC(JSContextRef ctx, JSValueRef value);
240
241     /**
242      * @brief Converts a JavaScript value to object and returns the resulting object.
243      *
244      * @remarks TypeMismatchException is thrown when the value was not Object type.
245      *
246      * @param[in] ctx The execution context to use.
247      * @param[in] value The JSValue to convert.
248      *
249      * @return The JSObject result of conversion
250      *
251      * @exception TypeMismatchException
252      */
253     static JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value);
254
255     /**
256      * @brief Converts a JavaScript value to STL string vector and returns the resulting STL string vector
257      *
258      * @remarks TypeMismatchException is thrown when the array element could not converts to string.\n
259      * If the value is not Array object, Will return empty vector
260      *
261      *
262      * @param[in] ctx The execution context to use.
263      * @param[in] value The JSValue to convert.
264      *
265      * @return A STL string vector
266      *
267      * @exception TypeMismatchException
268      */
269     static std::vector<std::string> JSArrayToStringVector(JSContextRef ctx, JSValueRef value);
270
271     /**
272      * @brief Converts a JavaScript value to double number vector and returns the resulting double number vector
273      *
274      * @remarks TypeMismatchException is thrown when the array element could not converts to double.\n
275      * If the value is not Array object, Will return empty vector
276      *
277      *
278      * @param[in] ctx The execution context to use.
279      * @param[in] value The JSValue to convert.
280      *
281      * @return A double number vector
282      *
283      * @exception TypeMismatchException
284      */
285     static std::vector<double> JSArrayToDoubleVector(JSContextRef ctx, JSValueRef value);
286
287     /**
288      * @brief Converts a JavaScript value to long number vector and returns the resulting long number vector
289      *
290      * @remarks TypeMismatchException is thrown when the array element could not converts to long.\n
291      * If the value is not Array object, Will return empty vector
292      *
293      *
294      * @param[in] ctx The execution context to use.
295      * @param[in] value The JSValue to convert.
296      *
297      * @return A long number vector
298      *
299      * @exception TypeMismatchException
300      */
301     static std::vector<long> JSArrayToLongVector(JSContextRef ctx, JSValueRef value);
302
303     /**
304      * @brief Converts a JavaScript value to time_t vector and returns the resulting time_t vector
305      *
306      * @remarks TypeMismatchException is thrown when the array element could not converts to time_t.\n
307      * If the value is not Array object, Will return empty vector
308      *
309      *
310      * @param[in] ctx The execution context to use.
311      * @param[in] value The JSValue to convert.
312      *
313      * @return A time_t vector
314      *
315      * @exception TypeMismatchException
316      */
317     static std::vector<time_t> JSArrayToTimeTVector(JSContextRef ctx, JSValueRef value);
318
319     /**
320      * @brief Converts a JavaScript value to boolean vector and returns the resulting boolean vector
321      *
322      * @remarks If the value is not Array object, Will return empty vector
323      *
324      *
325      * @param[in] ctx The execution context to use.
326      * @param[in] value The JSValue to convert.
327      *
328      * @return A boolean vector
329      */
330     static std::vector<bool> JSArrayToBoolVector(JSContextRef ctx, JSValueRef value);
331
332     /**
333      * @brief Converts a JavaScript value to map<string,string> and returns the resulting object.
334      *
335      * @remarks TypeMismatchException is thrown when the value was not Object type.
336      *
337      * @param[in] ctx The execution context to use.
338      * @param[in] value The JSValue to convert.
339      *
340      * @return The JSObject result of conversion
341      *
342      * @exception TypeMismatchException
343      */
344     static std::map<std::string, std::string> JSValueToStringMap(JSContextRef ctx, JSValueRef value);
345
346     /**
347      * @brief Creates a JavaScript value of the string type.
348      *
349      * @param[in] ctx The execution context to use.
350      * @param[in] str The STL string to assign to the newly created JSValue. The newly created JSValue retains string, and releases it upon garbage collection.
351      *
352      * @return A JSValue of the string type, representing the value of string.
353      */
354     static JSValueRef toJSValueRef(JSContextRef ctx, const std::string& str);
355
356     /**
357      * @brief Creates a JavaScript value of the number type.
358      *
359      * @param[in] ctx The execution context to use.
360      * @param[in] value The long to assign to the newly created JSValue.
361      *
362      * @return A JSValue of the number type, representing the value of number.
363      */
364     static JSValueRef toJSValueRef(JSContextRef ctx, const long value);
365
366     /**
367      * @brief Creates a JavaScript value of the number type.
368      *
369      * @param[in] ctx The execution context to use.
370      * @param[in] value The unsigned long to assign to the newly created JSValue.
371      *
372      * @return A JSValue of the number type, representing the value of number.
373      */
374     static JSValueRef toJSValueRef(JSContextRef ctx, const unsigned long value);
375
376     /**
377      * @brief Creates a JavaScript value of the number type.
378      *
379      * @param[in] ctx The execution context to use.
380      * @param[in] value The long long to assign to the newly created JSValue.
381      *
382      * @return A JSValue of the number type, representing the value of number.
383      */
384     static JSValueRef toJSValueRef(JSContextRef ctx, const long long value);
385
386     /**
387      * @brief Creates a JavaScript value of the number type.
388      *
389      * @param[in] ctx The execution context to use.
390      * @param[in] value The unsigned long long to assign to the newly created JSValue.
391      *
392      * @return A JSValue of the number type, representing the value of number.
393      */
394     static JSValueRef toJSValueRef(JSContextRef ctx, const unsigned long long value);
395     /**
396      * @brief Creates a JavaScript value of the number type.
397      *
398      * @param[in] ctx The execution context to use.
399      * @param[in] value The double to assign to the newly created JSValue.
400      *
401      * @return A JSValue of the number type, representing the value of number.
402      */
403     static JSValueRef toJSValueRef(JSContextRef ctx, const double value);
404
405     /**
406      * @brief Creates a JavaScript value of the boolean type.
407      *
408      * @param[in] ctx The execution context to use.
409      * @param[in] value The bool to assign to the newly created JSValue.
410      *
411      * @return A JSValue of the boolean type, representing the value of boolean.
412      */
413     static JSValueRef toJSValueRef(JSContextRef ctx, const bool value);
414
415     /**
416      * @brief Creates a JavaScript value of the number type.
417      *
418      * @param[in] ctx The execution context to use.
419      * @param[in] value The signed char to assign to the newly created JSValue.
420      *
421      * @return A JSValue of the number type, representing the value of number.
422      */
423     static JSValueRef toJSValueRef(JSContextRef ctx, const signed char value);
424
425     /**
426      * @brief Creates a JavaScript value of the number type.
427      *
428      * @param[in] ctx The execution context to use.
429      * @param[in] value The signed char to assign to the newly created JSValue.
430      *
431      * @return A JSValue of the number type, representing the value of number.
432      */
433     static JSValueRef toJSValueRef(JSContextRef ctx, const unsigned char value);
434
435
436
437     /**
438      * @brief Creates a JavaScript value of the string Array type.
439      *
440      * @remarks UnknownException is thrown when could not create Array object.\n
441      *
442      * @param[in] ctx The execution context to use.
443      * @param[in] value The STL string vector to assign to the newly created JSArray
444      *
445      * @return A JSArray of the string type
446      *
447      * @exception UnknownException
448      */
449     static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<std::string>& value);
450
451
452     /**
453      * @brief Creates a JavaScript value of the number Array type.
454      *
455      * @remarks UnknownException is thrown when could not create Array object.\n
456      *
457      * @param[in] ctx The execution context to use.
458      * @param[in] value The long vector to assign to the newly created JSArray
459      *
460      * @return A JSArray of the number type
461      *
462      * @exception UnknownException
463      */
464     static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<long>& value);
465
466     /**
467      * @brief Creates a JavaScript value of the number Array type.
468      *
469      * @remarks UnknownException is thrown when could not create Array object.\n
470      *
471      * @param[in] ctx The execution context to use.
472      * @param[in] value The double vector to assign to the newly created JSArray
473      *
474      * @return A JSArray of the number type
475      *
476      * @exception UnknownException
477      */
478     static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<double>& value);
479
480
481     /**
482      * @brief Creates a JavaScript value of the boolean Array type.
483      *
484      * @remarks UnknownException is thrown when could not create Array object.\n
485      *
486      * @param[in] ctx The execution context to use.
487      * @param[in] value The boolean vector to assign to the newly created JSArray
488      *
489      * @return A JSArray of the boolean type
490      *
491      * @exception UnknownException
492      */
493     static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<bool>& value);
494
495     /**
496      * @brief Creates a JavaScript value of the string map type.
497      *
498      * @remarks UnknownException is thrown when could not create Array object.\n
499      *
500      * @param[in] ctx The execution context to use.
501      * @param[in] value The string map to assign to the newly created JSValue
502      *
503      * @return A JSValue of the string map type
504      *
505      * @exception UnknownException
506      */
507     static JSValueRef toJSValueRef(JSContextRef ctx, const std::map<std::string, std::string>& value);
508
509     /**
510      * @brief Creates a JavaScript Date object with time_t value
511      *
512      * @remarks TypeMismatchException is thrown when could not create Date object.\n
513      *
514      * @param[in] ctx The execution context to use.
515      * @param[in] value The time_t value to create
516      *
517      * @return A JSObject that is a Date.
518      *
519      * @exception TypeMismatchException
520      */
521     static JSObjectRef makeDateObject(JSContextRef ctx, const time_t value);
522
523
524     template<class T>
525     static std::vector<T> JSArrayToType_(JSContextRef ctx, JSValueRef value, T (*convert)(JSContextRef, JSValueRef)){
526         std::vector<T> result;
527         if( !JSIsArrayValue(ctx, value)){
528             return result;
529         }
530         JSObjectRef arrayobj = JSUtil::JSValueToObject(ctx, value);
531
532         for (std::size_t i = 0; i < JSGetArrayLength(ctx, arrayobj); ++i) {
533             JSValueRef element = JSGetArrayElement(ctx, arrayobj, i);
534             T v = convert(ctx, element);
535             result.push_back(v);
536         }
537         return result;
538     }
539
540     template<class T>
541     static JSValueRef toJSValueRef_(JSContextRef ctx, const std::vector<T>& value){
542         JSValueRef valueArray[value.size()];
543         for( unsigned int i = 0 ; i < value.size(); i++){
544             valueArray[i] = toJSValueRef(ctx,value[i]);
545         }
546         JSValueRef exception = NULL;
547         JSObjectRef jsResult = JSObjectMakeArray(ctx, value.size(), valueArray, &exception);
548         if (exception != NULL) {
549             throw UnknownException(ctx, exception);
550         }
551         return jsResult;
552     };
553
554 private:
555     static bool JSValueIsDateObject(JSContextRef ctx, JSValueRef jsValue);
556 };
557
558 }}
559
560 #endif //_JSUTIL_H_
561
562