upload tizen1.0 source
[framework/web/wrt-plugins-common.git] / src / standards / W3C / Widget / JSTest.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 /**
17  *
18  * @file        JSTest.cpp
19  * @author      Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
20  * @version     0.1
21  */
22
23 #include <dpl/log/log.h>
24 #include <dpl/assert.h>
25 #include <CommonsJavaScript/Converter.h>
26 #include <Commons/Exception.h>
27 #include "JSTest.h"
28 #include "Test.h"
29
30 using namespace WrtDeviceApis;
31 using namespace WrtDeviceApis::Commons;
32 using namespace WrtDeviceApis::CommonsJavaScript;
33
34 #define CLASS_NAME "Test"
35
36 namespace WrtPlugins {
37 namespace W3C {
38
39 JSClassDefinition JSTest::m_classInfo =
40 {
41     0,
42     kJSClassAttributeNone,
43     CLASS_NAME,
44     0,
45     NULL,
46     m_functions,
47     initialize,
48     finalize,
49     NULL,
50     NULL,
51     NULL,
52     NULL,
53     NULL,
54     NULL,
55     NULL,
56     NULL,
57     NULL
58 };
59
60 JSStaticFunction JSTest::m_functions[] = {
61     { "setEvaluator", JSTest::setEvaluator, kJSPropertyAttributeNone },
62     { "run",          JSTest::run,          kJSPropertyAttributeNone },
63     { 0, 0, 0 }
64 };
65
66 const JSClassRef JSTest::getClassRef() {
67     if (!m_jsClassRef) {
68         m_jsClassRef = JSClassCreate(&m_classInfo);
69     }
70     return m_jsClassRef;
71 }
72
73 const JSClassDefinition* JSTest::getClassInfo() {
74     return &m_classInfo;
75 }
76
77 JSClassRef JSTest::m_jsClassRef = JSClassCreate(JSTest::getClassInfo());
78
79 void JSTest::initialize(JSContextRef context, JSObjectRef object) {
80     LogDebug("ENTER");
81
82     Test* priv = static_cast<Test*>(JSObjectGetPrivate(object));
83     if (!priv) {
84         priv = new Test();
85         JSObjectSetPrivate(object, static_cast<void*>(priv));
86     }
87     priv->setContext(context);
88 }
89
90 void JSTest::finalize(JSObjectRef object) {
91     LogDebug("ENTER");
92     delete static_cast<Test*>(JSObjectGetPrivate(object));
93 }
94
95 JSValueRef JSTest::setEvaluator(JSContextRef context,
96                                 JSObjectRef object,
97                                 JSObjectRef thisObject,
98                                 size_t argumentCount,
99                                 const JSValueRef arguments[],
100                                 JSValueRef* exception)
101 {
102     LogDebug("ENTER");
103
104     Assert(argumentCount >= 1 && "Not enough arguments.");
105
106     try {
107         JSObjectRef evaluator = CommonsJavaScript::Converter(context).toJSObjectRef(arguments[0]);
108         getPrivateObject(thisObject)->setEvaluator(evaluator);
109     }
110     catch (const Commons::Exception& ex) {
111         LogError("Exception: " << ex.DumpToString());
112     }
113
114     return JSValueMakeUndefined(context);
115 }
116
117 JSValueRef JSTest::run(JSContextRef context,
118                        JSObjectRef object,
119                        JSObjectRef thisObject,
120                        size_t argumentCount,
121                        const JSValueRef arguments[],
122                        JSValueRef* exception)
123 {
124     LogDebug("ENTER");
125
126     try {
127         double timeout = 0.0;
128         if (argumentCount >= 1) {
129             timeout = CommonsJavaScript::Converter(context).toDouble(arguments[0]);
130         }
131
132         getPrivateObject(thisObject)->run(timeout);
133     }
134     catch (const Commons::Exception& ex) {
135         LogError("Exception: " << ex.DumpToString());
136     }
137
138     return JSValueMakeUndefined(context);
139 }
140
141 Test* JSTest::getPrivateObject(JSObjectRef thisObject) {
142     Test* priv = static_cast<Test*>(JSObjectGetPrivate(thisObject));
143     Assert(priv && "Private object not available.");
144
145     return priv;
146 }
147
148 }
149 }