tizen 2.3 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / RefImpl / JSNestedValue.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "JSNestedValue.h"
19 #include "NestedValue.h"
20
21 #include <ArgumentValidator.h>
22 #include <PlatformException.h>
23 #include <JSUtil.h>
24
25 #include <string>
26
27 using namespace DeviceAPI::Common;
28 using namespace std;
29
30 namespace DeviceAPI {
31 namespace Reference {
32
33 JSClassRef JSNestedValue::m_jsClassRef = NULL;
34
35 JSClassDefinition JSNestedValue::m_jsClassInfo = {
36     0, // current (and only) version is 0
37     kJSClassAttributeNone, //attributes
38     "NestedValue", //class name
39     NULL, // parent class
40     JSNestedValue::m_property, //static values
41     JSNestedValue::m_function, // static functions
42     JSNestedValue::initialize, // initialize
43     JSNestedValue::finalize, //finalize
44     NULL, //hasProperty
45     NULL, //getProperty
46     NULL, //setProperty
47     NULL, //deleteProperty
48     NULL, //getPropertyNames
49     NULL, // callAsFunction
50     NULL, // constructor
51     NULL,
52     NULL // convertToType
53 };
54
55
56 JSStaticValue JSNestedValue::m_property[] = {
57         { "Number", JSNestedValue::getProperty, JSNestedValue::setProperty, kJSPropertyAttributeNone},
58         { "Message", JSNestedValue::getProperty, JSNestedValue::setProperty, kJSPropertyAttributeNone},
59         { 0, 0, 0, 0 }
60 };
61
62
63 JSStaticFunction JSNestedValue::m_function[] = {
64     { "print", JSNestedValue::print, kJSPropertyAttributeNone },
65     { 0, 0, 0 }
66 };
67
68
69 const JSClassRef JSNestedValue::getClassRef()
70 {
71     if (!m_jsClassRef) {
72         m_jsClassRef = JSClassCreate(&m_jsClassInfo);
73     }
74     return m_jsClassRef;
75 }
76 const JSClassDefinition* JSNestedValue::getClassInfo()
77 {
78     return &m_jsClassInfo;
79 }
80
81
82 boost::shared_ptr<NestedValue> JSNestedValue::getNative(JSContextRef ctx, JSValueRef value){
83     JSObjectRef object = JSUtil::JSValueToObject(ctx, value);    
84     NestedValueHolder* priv = static_cast<NestedValueHolder*>(JSObjectGetPrivate(object));
85     if( priv == NULL || priv->obj == NULL )
86         throw TypeMismatchException("Not NestedValue Type");
87     return priv->obj;
88 }
89
90 JSObjectRef JSNestedValue::makeJSObject(JSContextRef ctx,  boost::shared_ptr < NestedValue > native){
91     NestedValueHolder *priv = new NestedValueHolder;
92     priv->obj = native;
93     JSObjectRef obj = JSObjectMake(ctx, getClassRef(), priv);
94     return obj;
95 }
96
97 JSObjectRef JSNestedValue::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
98     try{
99         ArgumentValidator validator(ctx, argumentCount, arguments);
100         
101         boost::shared_ptr<NestedValue> native( new NestedValue(validator.toLong(0), validator.toString(1).c_str()) );
102         JSObjectRef obj = makeJSObject(ctx, native);
103         JSUtil::setProperty(ctx, obj, "constructor", constructor, kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete|kJSPropertyAttributeDontEnum);
104         return obj;
105     }catch( const BasePlatformException &err){
106         JSObjectRef error = JSWebAPIErrorFactory::makeErrorObject(ctx, err);
107         *exception = error;
108         return error;
109     }
110 }
111
112 void JSNestedValue::initialize(JSContextRef ctx, JSObjectRef object)
113 {
114     printf("JSNestedValue::initialize()\n");
115 }
116
117 void JSNestedValue::finalize(JSObjectRef object)
118 {
119     printf("JSNestedValue::finalize()\n");
120     NestedValueHolder * priv = static_cast<NestedValueHolder*>(JSObjectGetPrivate(object));
121     if( priv ){
122         JSObjectSetPrivate(object, NULL);
123         delete priv;
124     }
125 }
126
127 JSValueRef JSNestedValue::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception){
128     printf("JSNestedValue::getProperty()\n");
129     NestedValueHolder* priv = static_cast<NestedValueHolder*>(JSObjectGetPrivate(object));
130     
131     string name = JSUtil::JSStringToString(context, propertyName);
132     if( name == "Number" ){
133         return JSUtil::toJSValueRef(context, priv->obj->getNumber());
134     }else if( name == "Message"){
135         return JSUtil::toJSValueRef(context, priv->obj->getMessage());
136     }
137     return NULL;
138 }
139
140 bool JSNestedValue::setProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception){
141     printf("JSNestedValue::setProperty()\n");
142     NestedValueHolder* priv = static_cast<NestedValueHolder*>(JSObjectGetPrivate(object));
143     string name = JSUtil::JSStringToString(context, propertyName);
144     try{
145         JSValueRef values[1] = { value };
146         ArgumentValidator validator(context, 1, values);
147         if( name == "Number" ){
148             priv->obj->setNumber( validator.toLong(0));
149             return true;
150         }else if( name == "Message"){
151             priv->obj->setMessage( validator.toString(0));
152             return true;
153         }
154     }catch( const BasePlatformException& err){
155         printf("Silently error\n"); 
156         return true;
157     }
158     return false;
159 }
160
161
162 JSValueRef JSNestedValue::print(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
163     printf("JSNestedValue::print()\n");
164     NestedValueHolder* priv = static_cast<NestedValueHolder*>(JSObjectGetPrivate(thisObject));
165     if( priv && priv->obj){
166         printf("Number : %d , Message: %s\n" , priv->obj->getNumber(), priv->obj->getMessage().c_str());
167     }
168     return JSValueMakeUndefined(ctx);
169 }
170
171
172 } // Reference
173 } // TizenApis
174