Update change log and spec for wrt-plugins-tizen_0.4.13
[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 <time.h>
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 boolean and returns the resulting bollean
152      *
153      *
154      * @param[in] ctx The execution context to use.
155      * @param[in] value The JSValue to convert.
156      *
157      * @return The result of conversion
158      */
159     static bool JSValueToBoolean(JSContextRef ctx, JSValueRef value);
160
161     /**
162      * @brief Converts a JavaScript value to time_t and returns the resulting time_t.
163      *
164      * @remarks TypeMismatchException is thrown when the value was not Date type.
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 time_t JSValueToTimeT(JSContextRef ctx, JSValueRef value);
174
175     /**
176      * @brief Converts a JavaScript value to object and returns the resulting object.
177      *
178      * @remarks TypeMismatchException is thrown when the value was not Object type.
179      *
180      * @param[in] ctx The execution context to use.
181      * @param[in] value The JSValue to convert.
182      *
183      * @return The JSObject result of conversion
184      *
185      * @exception TypeMismatchException
186      */
187     static JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value);
188
189     /**
190      * @brief Converts a JavaScript value to STL string vector and returns the resulting STL string vector
191      *
192      * @remarks TypeMismatchException is thrown when the array element could not converts to string.\n
193      * If the value is not Array object, Will return empty vector
194      *
195      *
196      * @param[in] ctx The execution context to use.
197      * @param[in] value The JSValue to convert.
198      *
199      * @return A STL string vector
200      *
201      * @exception TypeMismatchException
202      */
203     static std::vector<std::string> JSArrayToStringVector(JSContextRef ctx, JSValueRef value);
204
205     /**
206      * @brief Converts a JavaScript value to double number vector and returns the resulting double number vector
207      *
208      * @remarks TypeMismatchException is thrown when the array element could not converts to double.\n
209      * If the value is not Array object, Will return empty vector
210      *
211      *
212      * @param[in] ctx The execution context to use.
213      * @param[in] value The JSValue to convert.
214      *
215      * @return A double number vector
216      *
217      * @exception TypeMismatchException
218      */
219     static std::vector<double> JSArrayToDoubleVector(JSContextRef ctx, JSValueRef value);
220
221     /**
222      * @brief Converts a JavaScript value to long number vector and returns the resulting long number vector
223      *
224      * @remarks TypeMismatchException is thrown when the array element could not converts to long.\n
225      * If the value is not Array object, Will return empty vector
226      *
227      *
228      * @param[in] ctx The execution context to use.
229      * @param[in] value The JSValue to convert.
230      *
231      * @return A long number vector
232      *
233      * @exception TypeMismatchException
234      */
235     static std::vector<long> JSArrayToLongVector(JSContextRef ctx, JSValueRef value);
236
237     /**
238      * @brief Converts a JavaScript value to time_t vector and returns the resulting time_t vector
239      *
240      * @remarks TypeMismatchException is thrown when the array element could not converts to time_t.\n
241      * If the value is not Array object, Will return empty vector
242      *
243      *
244      * @param[in] ctx The execution context to use.
245      * @param[in] value The JSValue to convert.
246      *
247      * @return A time_t vector
248      *
249      * @exception TypeMismatchException
250      */
251     static std::vector<time_t> JSArrayToTimeTVector(JSContextRef ctx, JSValueRef value);
252
253     /**
254      * @brief Converts a JavaScript value to boolean vector and returns the resulting boolean vector
255      *
256      * @remarks If the value is not Array object, Will return empty vector
257      *
258      *
259      * @param[in] ctx The execution context to use.
260      * @param[in] value The JSValue to convert.
261      *
262      * @return A boolean vector
263      */
264     static std::vector<bool> JSArrayToBoolVector(JSContextRef ctx, JSValueRef value);
265
266     /**
267      * @brief Converts a JavaScript value to map<string,string> and returns the resulting object.
268      *
269      * @remarks TypeMismatchException is thrown when the value was not Object type.
270      *
271      * @param[in] ctx The execution context to use.
272      * @param[in] value The JSValue to convert.
273      *
274      * @return The JSObject result of conversion
275      *
276      * @exception TypeMismatchException
277      */
278     static std::map<std::string, std::string> JSValueToStringMap(JSContextRef ctx, JSValueRef value);
279
280     /**
281      * @brief Creates a JavaScript value of the string type.
282      *
283      * @param[in] ctx The execution context to use.
284      * @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.
285      *
286      * @return A JSValue of the string type, representing the value of string.
287      */
288     static JSValueRef toJSValueRef(JSContextRef ctx, const std::string& str);
289
290     /**
291      * @brief Creates a JavaScript value of the number type.
292      *
293      * @param[in] ctx The execution context to use.
294      * @param[in] value The long to assign to the newly created JSValue.
295      *
296      * @return A JSValue of the number type, representing the value of number.
297      */
298     static JSValueRef toJSValueRef(JSContextRef ctx, const long value);
299
300     /**
301      * @brief Creates a JavaScript value of the number type.
302      *
303      * @param[in] ctx The execution context to use.
304      * @param[in] value The unsigned long to assign to the newly created JSValue.
305      *
306      * @return A JSValue of the number type, representing the value of number.
307      */
308     static JSValueRef toJSValueRef(JSContextRef ctx, const unsigned long value);
309
310     /**
311      * @brief Creates a JavaScript value of the number type.
312      *
313      * @param[in] ctx The execution context to use.
314      * @param[in] value The long long to assign to the newly created JSValue.
315      *
316      * @return A JSValue of the number type, representing the value of number.
317      */
318     static JSValueRef toJSValueRef(JSContextRef ctx, const long long value);
319
320     /**
321      * @brief Creates a JavaScript value of the number type.
322      *
323      * @param[in] ctx The execution context to use.
324      * @param[in] value The unsigned long long to assign to the newly created JSValue.
325      *
326      * @return A JSValue of the number type, representing the value of number.
327      */
328     static JSValueRef toJSValueRef(JSContextRef ctx, const unsigned long long value);
329     /**
330      * @brief Creates a JavaScript value of the number type.
331      *
332      * @param[in] ctx The execution context to use.
333      * @param[in] value The double to assign to the newly created JSValue.
334      *
335      * @return A JSValue of the number type, representing the value of number.
336      */
337     static JSValueRef toJSValueRef(JSContextRef ctx, const double value);
338
339     /**
340      * @brief Creates a JavaScript value of the boolean type.
341      *
342      * @param[in] ctx The execution context to use.
343      * @param[in] value The bool to assign to the newly created JSValue.
344      *
345      * @return A JSValue of the boolean type, representing the value of boolean.
346      */
347     static JSValueRef toJSValueRef(JSContextRef ctx, const bool value);
348
349
350
351     /**
352      * @brief Creates a JavaScript value of the string Array type.
353      *
354      * @remarks UnknownException is thrown when could not create Array object.\n
355      *
356      * @param[in] ctx The execution context to use.
357      * @param[in] value The STL string vector to assign to the newly created JSArray
358      *
359      * @return A JSArray of the string type
360      *
361      * @exception UnknownException
362      */
363     static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<std::string>& value);
364
365
366     /**
367      * @brief Creates a JavaScript value of the number Array type.
368      *
369      * @remarks UnknownException is thrown when could not create Array object.\n
370      *
371      * @param[in] ctx The execution context to use.
372      * @param[in] value The long vector to assign to the newly created JSArray
373      *
374      * @return A JSArray of the number type
375      *
376      * @exception UnknownException
377      */
378     static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<long>& value);
379
380     /**
381      * @brief Creates a JavaScript value of the number Array type.
382      *
383      * @remarks UnknownException is thrown when could not create Array object.\n
384      *
385      * @param[in] ctx The execution context to use.
386      * @param[in] value The double vector to assign to the newly created JSArray
387      *
388      * @return A JSArray of the number type
389      *
390      * @exception UnknownException
391      */
392     static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<double>& value);
393
394
395     /**
396      * @brief Creates a JavaScript value of the boolean Array type.
397      *
398      * @remarks UnknownException is thrown when could not create Array object.\n
399      *
400      * @param[in] ctx The execution context to use.
401      * @param[in] value The boolean vector to assign to the newly created JSArray
402      *
403      * @return A JSArray of the boolean type
404      *
405      * @exception UnknownException
406      */
407     static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<bool>& value);
408
409     /**
410      * @brief Creates a JavaScript value of the string map type.
411      *
412      * @remarks UnknownException is thrown when could not create Array object.\n
413      *
414      * @param[in] ctx The execution context to use.
415      * @param[in] value The string map to assign to the newly created JSValue
416      *
417      * @return A JSValue of the string map type
418      *
419      * @exception UnknownException
420      */
421     static JSValueRef toJSValueRef(JSContextRef ctx, const std::map<std::string, std::string>& value);
422
423     /**
424      * @brief Creates a JavaScript Date object with time_t value
425      *
426      * @remarks TypeMismatchException is thrown when could not create Date object.\n
427      *
428      * @param[in] ctx The execution context to use.
429      * @param[in] value The time_t value to create
430      *
431      * @return A JSObject that is a Date.
432      *
433      * @exception TypeMismatchException
434      */
435     static JSObjectRef makeDateObject(JSContextRef ctx, const time_t value);
436
437
438     template<class T>
439     static std::vector<T> JSArrayToType_(JSContextRef ctx, JSValueRef value, T (*convert)(JSContextRef, JSValueRef)){
440         std::vector<T> result;
441         if( !JSIsArrayValue(ctx, value)){
442             return result;
443         }
444         JSObjectRef arrayobj = JSUtil::JSValueToObject(ctx, value);
445
446         for (std::size_t i = 0; i < JSGetArrayLength(ctx, arrayobj); ++i) {
447             JSValueRef element = JSGetArrayElement(ctx, arrayobj, i);
448             T v = convert(ctx, element);
449             result.push_back(v);
450         }
451         return result;
452     }
453
454     template<class T>
455     static JSValueRef toJSValueRef_(JSContextRef ctx, const std::vector<T>& value){
456         JSValueRef valueArray[value.size()];
457         for( unsigned int i = 0 ; i < value.size(); i++){
458             valueArray[i] = toJSValueRef(ctx,value[i]);
459         }
460         JSValueRef exception = NULL;
461         JSObjectRef jsResult = JSObjectMakeArray(ctx, value.size(), valueArray, &exception);
462         if (exception != NULL) {
463             throw UnknownException(ctx, exception);
464         }
465         return jsResult;
466     };
467
468 };
469
470 }}
471
472 #endif //_JSUTIL_H_
473
474