Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / CommonsJavaScript / JSUtils.h
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 #ifndef WRTDEVICEAPIS_COMMONSJAVASCRIPT_JS_JSUTILS_H_
17 #define WRTDEVICEAPIS_COMMONSJAVASCRIPT_JS_JSUTILS_H_
18
19 #include <string>
20 #include <dpl/log/log.h>
21 #include <JavaScriptCore/JavaScript.h>
22 #include <Commons/Exception.h>
23 #include <CommonsJavaScript/PrivateObject.h>
24
25 namespace WrtDeviceApis {
26 namespace CommonsJavaScript {
27 class JSUtils
28 {
29   public:
30     static bool hasProperty(JSStaticValue* properties,
31                             JSStringRef name);
32
33     /**
34      * Gets a property from JSObject if exists
35      * @return JSValueRef if property exists, NULL if not
36      */
37     static JSValueRef getJSProperty(JSContextRef context,
38                                     JSValueRef jsValue,
39                                     const std::string &name,
40                                     JSValueRef* exception = NULL);
41
42     static JSValueRef getJSProperty(JSContextRef context,
43                                     JSObjectRef object,
44                                     const std::string& name);
45
46     static JSValueRef getJSPropertyOrUndefined(JSContextRef context,
47                                                JSObjectRef object,
48                                                const std::string& name);
49
50     template<class C>
51     static JSObjectRef makeObject(JSContextRef context,
52                                   JSClassRef classRef,
53                                   C data)
54     {
55         typedef typename PrivateObjectT<C>::Type Private;
56
57         Private* priv = new Private(context, data);
58         JSObjectRef object = JSObjectMake(context, classRef, priv);
59         if (!object) {
60             ThrowMsg(Commons::NullPointerException,
61                      "Could not create JS object.");
62         }
63
64         return object;
65     }
66
67     static JSObjectRef makeObject(JSContextRef context,
68                                   JSClassRef classRef)
69     {
70         typedef PrivateObjectT<void>::Type Private;
71
72         Private* priv = new Private(context);
73         JSObjectRef object = JSObjectMake(context, classRef, priv);
74         if (!object) {
75             ThrowMsg(Commons::NullPointerException,
76                      "Could not create JS object.");
77         }
78
79         return object;
80     }
81 }; // JSUtils
82 } // CommonsJavaScript
83 } // WrtDeviceApis
84
85 #endif /* WRTPLUGINS_COMMONS_JSUTILS_H_ */