Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / CommonsJavaScript / JSUtils.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include "Converter.h"
17 #include "ScopedJSStringRef.h"
18 #include "JSUtils.h"
19
20 namespace WrtDeviceApis {
21 namespace CommonsJavaScript {
22 using namespace WrtDeviceApis::Commons;
23
24 bool JSUtils::hasProperty(JSStaticValue* properties,
25                           JSStringRef name)
26 {
27     JSStaticValue* property = properties;
28     while (property->name) {
29         if (JSStringIsEqualToUTF8CString(name, property->name)) {
30             return true;
31         }
32         ++property;
33     }
34     return false;
35 }
36
37 JSValueRef JSUtils::getJSProperty(JSContextRef context,
38                                   JSValueRef jsValue,
39                                   const std::string &name,
40                                   JSValueRef *exception)
41 {
42     ScopedJSStringRef jsPropName(JSStringCreateWithUTF8CString(name.c_str()));
43     Converter converter(context);
44     JSObjectRef jsObject = converter.toJSObjectRef(jsValue);
45     if (JSObjectHasProperty(context, jsObject, jsPropName.get())) {
46         return JSObjectGetProperty(context, jsObject,
47                                    jsPropName.get(), exception);
48     }
49     return NULL;
50 }
51
52 JSValueRef JSUtils::getJSProperty(JSContextRef context,
53                                   JSObjectRef object,
54                                   const std::string& name)
55 {
56     Converter converter(context);
57     Try {
58         ScopedJSStringRef propName(converter.toJSStringRef(name));
59         if (JSObjectHasProperty(context, object, propName.get())) {
60             JSValueRef result = JSObjectGetProperty(context,
61                                                     object,
62                                                     propName.get(),
63                                                     NULL);
64             if (!JSValueIsUndefined(context, result)) {
65                 return result;
66             }
67         }
68     }
69     Catch(ConversionException) {}
70     return NULL;
71 }
72
73 JSValueRef JSUtils::getJSPropertyOrUndefined(JSContextRef context,
74                                              JSObjectRef object,
75                                              const std::string& name)
76 {
77     Converter converter(context);
78     Try {
79         ScopedJSStringRef propName(converter.toJSStringRef(name));
80         if (JSObjectHasProperty(context, object, propName.get())) {
81             return JSObjectGetProperty(context, object, propName.get(), NULL);
82         }
83     }
84     Catch(ConversionException) {}
85     return JSValueMakeUndefined(context);
86 }
87 } // CommonsJavaScript
88 } // WrtDeviceApis