2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
23 #include <dpl/log/log.h>
24 #include <dpl/assert.h>
25 #include <CommonsJavaScript/Converter.h>
26 #include <Commons/Exception.h>
30 using namespace WrtDeviceApis;
31 using namespace WrtDeviceApis::Commons;
32 using namespace WrtDeviceApis::CommonsJavaScript;
34 #define CLASS_NAME "Test"
36 namespace WrtPlugins {
39 JSClassDefinition JSTest::m_classInfo =
42 kJSClassAttributeNone,
60 JSStaticFunction JSTest::m_functions[] = {
61 { "setEvaluator", JSTest::setEvaluator, kJSPropertyAttributeNone },
62 { "run", JSTest::run, kJSPropertyAttributeNone },
66 const JSClassRef JSTest::getClassRef() {
68 m_jsClassRef = JSClassCreate(&m_classInfo);
73 const JSClassDefinition* JSTest::getClassInfo() {
77 JSClassRef JSTest::m_jsClassRef = JSClassCreate(JSTest::getClassInfo());
79 void JSTest::initialize(JSContextRef context, JSObjectRef object) {
82 Test* priv = static_cast<Test*>(JSObjectGetPrivate(object));
85 JSObjectSetPrivate(object, static_cast<void*>(priv));
87 priv->setContext(context);
90 void JSTest::finalize(JSObjectRef object) {
92 delete static_cast<Test*>(JSObjectGetPrivate(object));
95 JSValueRef JSTest::setEvaluator(JSContextRef context,
97 JSObjectRef thisObject,
99 const JSValueRef arguments[],
100 JSValueRef* exception)
104 Assert(argumentCount >= 1 && "Not enough arguments.");
107 JSObjectRef evaluator = CommonsJavaScript::Converter(context).toJSObjectRef(arguments[0]);
108 getPrivateObject(thisObject)->setEvaluator(evaluator);
110 catch (const Commons::Exception& ex) {
111 LogError("Exception: " << ex.DumpToString());
114 return JSValueMakeUndefined(context);
117 JSValueRef JSTest::run(JSContextRef context,
119 JSObjectRef thisObject,
120 size_t argumentCount,
121 const JSValueRef arguments[],
122 JSValueRef* exception)
127 double timeout = 0.0;
128 if (argumentCount >= 1) {
129 timeout = CommonsJavaScript::Converter(context).toDouble(arguments[0]);
132 getPrivateObject(thisObject)->run(timeout);
134 catch (const Commons::Exception& ex) {
135 LogError("Exception: " << ex.DumpToString());
138 return JSValueMakeUndefined(context);
141 Test* JSTest::getPrivateObject(JSObjectRef thisObject) {
142 Test* priv = static_cast<Test*>(JSObjectGetPrivate(thisObject));
143 Assert(priv && "Private object not available.");