2 // Tizen Web Device API
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <JavaScriptCore/JavaScript.h>
24 #include "PlatformException.h"
34 * @brief Gets a property from an object.
37 * if pass NULL in exception, when occurred error, it throw C++ exception(TypeMismatchException).
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.
44 * @exception TypeMismatchException
46 static JSValueRef getProperty(JSContextRef ctx , JSObjectRef object, const char *name, JSValueRef *exception=NULL);
49 * @brief Sets a property on an object.
52 * if pass NULL in exception, when occurred error, it throw C++ exception(TypeMismatchException).
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.
60 * @exception TypeMismatchException
62 static void setProperty(JSContextRef ctx , JSObjectRef object, const char *name, JSValueRef value, JSPropertyAttributes attributes, JSValueRef *exception=NULL);
65 * @brief Converts a JavaScript string to STL string
67 * @param[in] ctx The execution context to use.
68 * @param[in] value The JSString to convert.
70 * @return A STL string with the result of conversion
72 static std::string JSStringToString(JSContextRef ctx, JSStringRef str);
75 * @brief Converts a JavaScript value to STL string
77 * @param[in] ctx The execution context to use.
78 * @param[in] value The JSValue to convert.
80 * @return A STL string with the result of conversion
82 * @exception TypeMismatchException
84 static std::string JSValueToString(JSContextRef ctx, JSValueRef value);
87 * @brief Converts a JavaScript value to long number and returns the resulting long number.
89 * @param[in] ctx The execution context to use.
90 * @param[in] value The JSValue to convert.
92 * @return The result of conversion
94 * @exception TypeMismatchException
96 static long JSValueToLong(JSContextRef ctx, JSValueRef value);
99 * @brief Converts a JavaScript value to unsigned long number and returns the resulting unsigned long number.
101 * @param[in] ctx The execution context to use.
102 * @param[in] value The JSValue to convert.
104 * @return The result of conversion
106 * @exception TypeMismatchException
108 static unsigned long JSValueToULong(JSContextRef ctx, JSValueRef value);
112 * @brief Converts a JavaScript value to long long number and returns the resulting long long number.
114 * @param[in] ctx The execution context to use.
115 * @param[in] value The JSValue to convert.
117 * @return The result of conversion
119 * @exception TypeMismatchException
121 static long long JSValueToLongLong(JSContextRef ctx, JSValueRef value);
124 * @brief Converts a JavaScript value to unsigned long long number and returns the resulting unsigned long long number.
126 * @param[in] ctx The execution context to use.
127 * @param[in] value The JSValue to convert.
129 * @return The result of conversion
131 * @exception TypeMismatchException
133 static unsigned long long JSValueToULongLong(JSContextRef ctx, JSValueRef value);
137 * @brief Converts a JavaScript value to double number and returns the resulting double number.
139 * @remarks TypeMismatchException is thrown when the result of conversion was NaN(Not a Number).
141 * @param[in] ctx The execution context to use.
142 * @param[in] value The JSValue to convert.
144 * @return The result of conversion
146 * @exception TypeMismatchException
148 static double JSValueToDouble(JSContextRef ctx, JSValueRef value);
151 * @brief Converts a JavaScript value to number and returns the resulting number.
153 * @param[in] ctx The execution context to use.
154 * @param[in] value The JSValue to convert.
156 * @return The result of conversion
158 * @exception TypeMismatchException
160 static double JSValueToNumber(JSContextRef ctx, JSValueRef value);
164 * @brief Converts a JavaScript value to byte(signed) number and returns the resulting byte(signed) number.
166 * @param[in] ctx The execution context to use.
167 * @param[in] value The JSValue to convert.
169 * @return The result of conversion
171 * @exception TypeMismatchException
173 static signed char JSValueToByte(JSContextRef ctx, JSValueRef value);
176 * @brief Converts a JavaScript value to Octet(unsigned) number and returns the resulting Octet(unsigned) number.
178 * @param[in] ctx The execution context to use.
179 * @param[in] value The JSValue to convert.
181 * @return The result of conversion
183 * @exception TypeMismatchException
185 static unsigned char JSValueToOctet(JSContextRef ctx, JSValueRef value);
189 * @brief Converts a JavaScript value to boolean and returns the resulting bollean
192 * @param[in] ctx The execution context to use.
193 * @param[in] value The JSValue to convert.
195 * @return The result of conversion
197 static bool JSValueToBoolean(JSContextRef ctx, JSValueRef value);
200 * @brief Converts a JavaScript value to time_t and returns the resulting time_t.
202 * @remarks TypeMismatchException is thrown when the value was not Date type.
204 * @param[in] ctx The execution context to use.
205 * @param[in] value The JSValue to convert.
207 * @return The result of conversion
209 * @exception TypeMismatchException
211 static time_t JSValueToTimeT(JSContextRef ctx, JSValueRef value);
214 * @brief Converts a JavaScript value to tm and returns the resulting tm.
216 * @remarks TypeMismatchException is thrown when the value was not Date type.
218 * @param[in] ctx The execution context to use.
219 * @param[in] value The JSValue to convert.
221 * @return The result of conversion
223 * @exception TypeMismatchException
225 static std::tm JSValueToDateTm(JSContextRef ctx, JSValueRef value);
228 * @brief Converts a JavaScript value to tm as UTC time and returns the resulting tm.
230 * @remarks TypeMismatchException is thrown when the value was not Date type.
232 * @param[in] ctx The execution context to use.
233 * @param[in] value The JSValue to convert.
235 * @return The result of conversion
237 * @exception TypeMismatchException
239 static std::tm JSValueToDateTmUTC(JSContextRef ctx, JSValueRef value);
242 * @brief Converts a JavaScript value to object and returns the resulting object.
244 * @remarks TypeMismatchException is thrown when the value was not Object type.
246 * @param[in] ctx The execution context to use.
247 * @param[in] value The JSValue to convert.
249 * @return The JSObject result of conversion
251 * @exception TypeMismatchException
253 static JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value);
256 * @brief Converts a JavaScript value to STL string vector and returns the resulting STL string vector
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
262 * @param[in] ctx The execution context to use.
263 * @param[in] value The JSValue to convert.
265 * @return A STL string vector
267 * @exception TypeMismatchException
269 static std::vector<std::string> JSArrayToStringVector(JSContextRef ctx, JSValueRef value);
272 * @brief Converts a JavaScript value to double number vector and returns the resulting double number vector
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
278 * @param[in] ctx The execution context to use.
279 * @param[in] value The JSValue to convert.
281 * @return A double number vector
283 * @exception TypeMismatchException
285 static std::vector<double> JSArrayToDoubleVector(JSContextRef ctx, JSValueRef value);
288 * @brief Converts a JavaScript value to long number vector and returns the resulting long number vector
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
294 * @param[in] ctx The execution context to use.
295 * @param[in] value The JSValue to convert.
297 * @return A long number vector
299 * @exception TypeMismatchException
301 static std::vector<long> JSArrayToLongVector(JSContextRef ctx, JSValueRef value);
304 * @brief Converts a JavaScript value to time_t vector and returns the resulting time_t vector
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
310 * @param[in] ctx The execution context to use.
311 * @param[in] value The JSValue to convert.
313 * @return A time_t vector
315 * @exception TypeMismatchException
317 static std::vector<time_t> JSArrayToTimeTVector(JSContextRef ctx, JSValueRef value);
320 * @brief Converts a JavaScript value to boolean vector and returns the resulting boolean vector
322 * @remarks If the value is not Array object, Will return empty vector
325 * @param[in] ctx The execution context to use.
326 * @param[in] value The JSValue to convert.
328 * @return A boolean vector
330 static std::vector<bool> JSArrayToBoolVector(JSContextRef ctx, JSValueRef value);
333 * @brief Converts a JavaScript value to map<string,string> and returns the resulting object.
335 * @remarks TypeMismatchException is thrown when the value was not Object type.
337 * @param[in] ctx The execution context to use.
338 * @param[in] value The JSValue to convert.
340 * @return The JSObject result of conversion
342 * @exception TypeMismatchException
344 static std::map<std::string, std::string> JSValueToStringMap(JSContextRef ctx, JSValueRef value);
347 * @brief Creates a JavaScript value of the string type.
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.
352 * @return A JSValue of the string type, representing the value of string.
354 static JSValueRef toJSValueRef(JSContextRef ctx, const std::string& str);
357 * @brief Creates a JavaScript value of the number type.
359 * @param[in] ctx The execution context to use.
360 * @param[in] value The long to assign to the newly created JSValue.
362 * @return A JSValue of the number type, representing the value of number.
364 static JSValueRef toJSValueRef(JSContextRef ctx, const long value);
367 * @brief Creates a JavaScript value of the number type.
369 * @param[in] ctx The execution context to use.
370 * @param[in] value The unsigned long to assign to the newly created JSValue.
372 * @return A JSValue of the number type, representing the value of number.
374 static JSValueRef toJSValueRef(JSContextRef ctx, const unsigned long value);
377 * @brief Creates a JavaScript value of the number type.
379 * @param[in] ctx The execution context to use.
380 * @param[in] value The long long to assign to the newly created JSValue.
382 * @return A JSValue of the number type, representing the value of number.
384 static JSValueRef toJSValueRef(JSContextRef ctx, const long long value);
387 * @brief Creates a JavaScript value of the number type.
389 * @param[in] ctx The execution context to use.
390 * @param[in] value The unsigned long long to assign to the newly created JSValue.
392 * @return A JSValue of the number type, representing the value of number.
394 static JSValueRef toJSValueRef(JSContextRef ctx, const unsigned long long value);
396 * @brief Creates a JavaScript value of the number type.
398 * @param[in] ctx The execution context to use.
399 * @param[in] value The double to assign to the newly created JSValue.
401 * @return A JSValue of the number type, representing the value of number.
403 static JSValueRef toJSValueRef(JSContextRef ctx, const double value);
406 * @brief Creates a JavaScript value of the boolean type.
408 * @param[in] ctx The execution context to use.
409 * @param[in] value The bool to assign to the newly created JSValue.
411 * @return A JSValue of the boolean type, representing the value of boolean.
413 static JSValueRef toJSValueRef(JSContextRef ctx, const bool value);
416 * @brief Creates a JavaScript value of the number type.
418 * @param[in] ctx The execution context to use.
419 * @param[in] value The signed char to assign to the newly created JSValue.
421 * @return A JSValue of the number type, representing the value of number.
423 static JSValueRef toJSValueRef(JSContextRef ctx, const signed char value);
426 * @brief Creates a JavaScript value of the number type.
428 * @param[in] ctx The execution context to use.
429 * @param[in] value The signed char to assign to the newly created JSValue.
431 * @return A JSValue of the number type, representing the value of number.
433 static JSValueRef toJSValueRef(JSContextRef ctx, const unsigned char value);
438 * @brief Creates a JavaScript value of the string Array type.
440 * @remarks UnknownException is thrown when could not create Array object.\n
442 * @param[in] ctx The execution context to use.
443 * @param[in] value The STL string vector to assign to the newly created JSArray
445 * @return A JSArray of the string type
447 * @exception UnknownException
449 static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<std::string>& value);
453 * @brief Creates a JavaScript value of the number Array type.
455 * @remarks UnknownException is thrown when could not create Array object.\n
457 * @param[in] ctx The execution context to use.
458 * @param[in] value The long vector to assign to the newly created JSArray
460 * @return A JSArray of the number type
462 * @exception UnknownException
464 static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<long>& value);
467 * @brief Creates a JavaScript value of the number Array type.
469 * @remarks UnknownException is thrown when could not create Array object.\n
471 * @param[in] ctx The execution context to use.
472 * @param[in] value The double vector to assign to the newly created JSArray
474 * @return A JSArray of the number type
476 * @exception UnknownException
478 static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<double>& value);
482 * @brief Creates a JavaScript value of the boolean Array type.
484 * @remarks UnknownException is thrown when could not create Array object.\n
486 * @param[in] ctx The execution context to use.
487 * @param[in] value The boolean vector to assign to the newly created JSArray
489 * @return A JSArray of the boolean type
491 * @exception UnknownException
493 static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<bool>& value);
496 * @brief Creates a JavaScript value of the string map type.
498 * @remarks UnknownException is thrown when could not create Array object.\n
500 * @param[in] ctx The execution context to use.
501 * @param[in] value The string map to assign to the newly created JSValue
503 * @return A JSValue of the string map type
505 * @exception UnknownException
507 static JSValueRef toJSValueRef(JSContextRef ctx, const std::map<std::string, std::string>& value);
510 * @brief Creates a JavaScript Date object with time_t value
512 * @remarks TypeMismatchException is thrown when could not create Date object.\n
514 * @param[in] ctx The execution context to use.
515 * @param[in] value The time_t value to create
517 * @return A JSObject that is a Date.
519 * @exception TypeMismatchException
521 static JSObjectRef makeDateObject(JSContextRef ctx, const time_t value);
525 static std::vector<T> JSArrayToType_(JSContextRef ctx, JSValueRef value, T (*convert)(JSContextRef, JSValueRef)){
526 std::vector<T> result;
527 if( !JSIsArrayValue(ctx, value)){
530 JSObjectRef arrayobj = JSUtil::JSValueToObject(ctx, value);
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);
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]);
546 JSValueRef exception = NULL;
547 JSObjectRef jsResult = JSObjectMakeArray(ctx, value.size(), valueArray, &exception);
548 if (exception != NULL) {
549 throw UnknownException(ctx, exception);
555 static bool JSValueIsDateObject(JSContextRef ctx, JSValueRef jsValue);