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 boolean and returns the resulting bollean
154 * @param[in] ctx The execution context to use.
155 * @param[in] value The JSValue to convert.
157 * @return The result of conversion
159 static bool JSValueToBoolean(JSContextRef ctx, JSValueRef value);
162 * @brief Converts a JavaScript value to time_t and returns the resulting time_t.
164 * @remarks TypeMismatchException is thrown when the value was not Date type.
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 time_t JSValueToTimeT(JSContextRef ctx, JSValueRef value);
176 * @brief Converts a JavaScript value to object and returns the resulting object.
178 * @remarks TypeMismatchException is thrown when the value was not Object type.
180 * @param[in] ctx The execution context to use.
181 * @param[in] value The JSValue to convert.
183 * @return The JSObject result of conversion
185 * @exception TypeMismatchException
187 static JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value);
190 * @brief Converts a JavaScript value to STL string vector and returns the resulting STL string vector
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
196 * @param[in] ctx The execution context to use.
197 * @param[in] value The JSValue to convert.
199 * @return A STL string vector
201 * @exception TypeMismatchException
203 static std::vector<std::string> JSArrayToStringVector(JSContextRef ctx, JSValueRef value);
206 * @brief Converts a JavaScript value to double number vector and returns the resulting double number vector
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
212 * @param[in] ctx The execution context to use.
213 * @param[in] value The JSValue to convert.
215 * @return A double number vector
217 * @exception TypeMismatchException
219 static std::vector<double> JSArrayToDoubleVector(JSContextRef ctx, JSValueRef value);
222 * @brief Converts a JavaScript value to long number vector and returns the resulting long number vector
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
228 * @param[in] ctx The execution context to use.
229 * @param[in] value The JSValue to convert.
231 * @return A long number vector
233 * @exception TypeMismatchException
235 static std::vector<long> JSArrayToLongVector(JSContextRef ctx, JSValueRef value);
238 * @brief Converts a JavaScript value to time_t vector and returns the resulting time_t vector
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
244 * @param[in] ctx The execution context to use.
245 * @param[in] value The JSValue to convert.
247 * @return A time_t vector
249 * @exception TypeMismatchException
251 static std::vector<time_t> JSArrayToTimeTVector(JSContextRef ctx, JSValueRef value);
254 * @brief Converts a JavaScript value to boolean vector and returns the resulting boolean vector
256 * @remarks If the value is not Array object, Will return empty vector
259 * @param[in] ctx The execution context to use.
260 * @param[in] value The JSValue to convert.
262 * @return A boolean vector
264 static std::vector<bool> JSArrayToBoolVector(JSContextRef ctx, JSValueRef value);
267 * @brief Converts a JavaScript value to map<string,string> and returns the resulting object.
269 * @remarks TypeMismatchException is thrown when the value was not Object type.
271 * @param[in] ctx The execution context to use.
272 * @param[in] value The JSValue to convert.
274 * @return The JSObject result of conversion
276 * @exception TypeMismatchException
278 static std::map<std::string, std::string> JSValueToStringMap(JSContextRef ctx, JSValueRef value);
281 * @brief Creates a JavaScript value of the string type.
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.
286 * @return A JSValue of the string type, representing the value of string.
288 static JSValueRef toJSValueRef(JSContextRef ctx, const std::string& str);
291 * @brief Creates a JavaScript value of the number type.
293 * @param[in] ctx The execution context to use.
294 * @param[in] value The long to assign to the newly created JSValue.
296 * @return A JSValue of the number type, representing the value of number.
298 static JSValueRef toJSValueRef(JSContextRef ctx, const long value);
301 * @brief Creates a JavaScript value of the number type.
303 * @param[in] ctx The execution context to use.
304 * @param[in] value The unsigned long to assign to the newly created JSValue.
306 * @return A JSValue of the number type, representing the value of number.
308 static JSValueRef toJSValueRef(JSContextRef ctx, const unsigned long value);
311 * @brief Creates a JavaScript value of the number type.
313 * @param[in] ctx The execution context to use.
314 * @param[in] value The long long to assign to the newly created JSValue.
316 * @return A JSValue of the number type, representing the value of number.
318 static JSValueRef toJSValueRef(JSContextRef ctx, const long long value);
321 * @brief Creates a JavaScript value of the number type.
323 * @param[in] ctx The execution context to use.
324 * @param[in] value The unsigned long long to assign to the newly created JSValue.
326 * @return A JSValue of the number type, representing the value of number.
328 static JSValueRef toJSValueRef(JSContextRef ctx, const unsigned long long value);
330 * @brief Creates a JavaScript value of the number type.
332 * @param[in] ctx The execution context to use.
333 * @param[in] value The double to assign to the newly created JSValue.
335 * @return A JSValue of the number type, representing the value of number.
337 static JSValueRef toJSValueRef(JSContextRef ctx, const double value);
340 * @brief Creates a JavaScript value of the boolean type.
342 * @param[in] ctx The execution context to use.
343 * @param[in] value The bool to assign to the newly created JSValue.
345 * @return A JSValue of the boolean type, representing the value of boolean.
347 static JSValueRef toJSValueRef(JSContextRef ctx, const bool value);
352 * @brief Creates a JavaScript value of the string Array type.
354 * @remarks UnknownException is thrown when could not create Array object.\n
356 * @param[in] ctx The execution context to use.
357 * @param[in] value The STL string vector to assign to the newly created JSArray
359 * @return A JSArray of the string type
361 * @exception UnknownException
363 static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<std::string>& value);
367 * @brief Creates a JavaScript value of the number Array type.
369 * @remarks UnknownException is thrown when could not create Array object.\n
371 * @param[in] ctx The execution context to use.
372 * @param[in] value The long vector to assign to the newly created JSArray
374 * @return A JSArray of the number type
376 * @exception UnknownException
378 static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<long>& value);
381 * @brief Creates a JavaScript value of the number Array type.
383 * @remarks UnknownException is thrown when could not create Array object.\n
385 * @param[in] ctx The execution context to use.
386 * @param[in] value The double vector to assign to the newly created JSArray
388 * @return A JSArray of the number type
390 * @exception UnknownException
392 static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<double>& value);
396 * @brief Creates a JavaScript value of the boolean Array type.
398 * @remarks UnknownException is thrown when could not create Array object.\n
400 * @param[in] ctx The execution context to use.
401 * @param[in] value The boolean vector to assign to the newly created JSArray
403 * @return A JSArray of the boolean type
405 * @exception UnknownException
407 static JSValueRef toJSValueRef(JSContextRef ctx, const std::vector<bool>& value);
410 * @brief Creates a JavaScript value of the string map type.
412 * @remarks UnknownException is thrown when could not create Array object.\n
414 * @param[in] ctx The execution context to use.
415 * @param[in] value The string map to assign to the newly created JSValue
417 * @return A JSValue of the string map type
419 * @exception UnknownException
421 static JSValueRef toJSValueRef(JSContextRef ctx, const std::map<std::string, std::string>& value);
424 * @brief Creates a JavaScript Date object with time_t value
426 * @remarks TypeMismatchException is thrown when could not create Date object.\n
428 * @param[in] ctx The execution context to use.
429 * @param[in] value The time_t value to create
431 * @return A JSObject that is a Date.
433 * @exception TypeMismatchException
435 static JSObjectRef makeDateObject(JSContextRef ctx, const time_t value);
439 static std::vector<T> JSArrayToType_(JSContextRef ctx, JSValueRef value, T (*convert)(JSContextRef, JSValueRef)){
440 std::vector<T> result;
441 if( !JSIsArrayValue(ctx, value)){
444 JSObjectRef arrayobj = JSUtil::JSValueToObject(ctx, value);
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);
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]);
460 JSValueRef exception = NULL;
461 JSObjectRef jsResult = JSObjectMakeArray(ctx, value.size(), valueArray, &exception);
462 if (exception != NULL) {
463 throw UnknownException(ctx, exception);