tizen beta release
[framework/web/wrt-plugins-common.git] / src / 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
23 using namespace WrtDeviceApis::Commons;
24
25
26 bool JSUtils::hasProperty(JSStaticValue* properties,
27         JSStringRef name)
28 {
29     JSStaticValue* property = properties;
30     while (property->name) {
31         if (JSStringIsEqualToUTF8CString(name, property->name)) {
32             return true;
33         }
34         ++property;
35     }
36     return false;
37 }
38
39 JSValueRef JSUtils::getJSProperty(JSContextRef context,
40         JSValueRef jsValue,
41         const std::string &name,
42         JSValueRef *exception)
43 {
44     ScopedJSStringRef jsPropName(JSStringCreateWithUTF8CString(name.c_str()));
45     Converter converter(context);
46     JSObjectRef jsObject = converter.toJSObjectRef(jsValue);
47     if (JSObjectHasProperty(context, jsObject, jsPropName.get())) {
48         return JSObjectGetProperty(context, jsObject,
49                                    jsPropName.get(), exception);
50     }
51     return NULL;
52 }
53
54 JSValueRef JSUtils::getJSProperty(JSContextRef context,
55         JSObjectRef object,
56         const std::string& name)
57 {
58     Converter converter(context);
59     Try {
60         ScopedJSStringRef propName(converter.toJSStringRef(name));
61         if (JSObjectHasProperty(context, object, propName.get())) {
62             JSValueRef result = JSObjectGetProperty(context,
63                                                     object,
64                                                     propName.get(),
65                                                     NULL);
66             if (!JSValueIsUndefined(context, result)) {
67                 return result;
68             }
69         }
70     }
71     Catch(ConversionException) {
72     }
73     return NULL;
74 }
75
76 JSValueRef JSUtils::getJSPropertyOrUndefined(JSContextRef context,
77         JSObjectRef object,
78         const std::string& name)
79 {
80     Converter converter(context);
81     Try {
82         ScopedJSStringRef propName(converter.toJSStringRef(name));
83         if (JSObjectHasProperty(context, object, propName.get())) {
84             return JSObjectGetProperty(context, object, propName.get(), NULL);
85         }
86     }
87     Catch(ConversionException) {
88     }
89     return JSValueMakeUndefined(context);
90 }
91 } // CommonsJavaScript
92 } // WrtDeviceApis