2 // Tizen Web Device API
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "JSNestedValue.h"
19 #include "NestedValue.h"
21 #include <ArgumentValidator.h>
22 #include <PlatformException.h>
27 using namespace DeviceAPI::Common;
33 JSClassRef JSNestedValue::m_jsClassRef = NULL;
35 JSClassDefinition JSNestedValue::m_jsClassInfo = {
36 0, // current (and only) version is 0
37 kJSClassAttributeNone, //attributes
38 "NestedValue", //class name
40 JSNestedValue::m_property, //static values
41 JSNestedValue::m_function, // static functions
42 JSNestedValue::initialize, // initialize
43 JSNestedValue::finalize, //finalize
47 NULL, //deleteProperty
48 NULL, //getPropertyNames
49 NULL, // callAsFunction
56 JSStaticValue JSNestedValue::m_property[] = {
57 { "Number", JSNestedValue::getProperty, JSNestedValue::setProperty, kJSPropertyAttributeNone},
58 { "Message", JSNestedValue::getProperty, JSNestedValue::setProperty, kJSPropertyAttributeNone},
63 JSStaticFunction JSNestedValue::m_function[] = {
64 { "print", JSNestedValue::print, kJSPropertyAttributeNone },
69 const JSClassRef JSNestedValue::getClassRef()
72 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
76 const JSClassDefinition* JSNestedValue::getClassInfo()
78 return &m_jsClassInfo;
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");
90 JSObjectRef JSNestedValue::makeJSObject(JSContextRef ctx, boost::shared_ptr < NestedValue > native){
91 NestedValueHolder *priv = new NestedValueHolder;
93 JSObjectRef obj = JSObjectMake(ctx, getClassRef(), priv);
97 JSObjectRef JSNestedValue::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
99 ArgumentValidator validator(ctx, argumentCount, arguments);
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);
105 }catch( const BasePlatformException &err){
106 JSObjectRef error = JSWebAPIErrorFactory::makeErrorObject(ctx, err);
112 void JSNestedValue::initialize(JSContextRef ctx, JSObjectRef object)
114 printf("JSNestedValue::initialize()\n");
117 void JSNestedValue::finalize(JSObjectRef object)
119 printf("JSNestedValue::finalize()\n");
120 NestedValueHolder * priv = static_cast<NestedValueHolder*>(JSObjectGetPrivate(object));
122 JSObjectSetPrivate(object, NULL);
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));
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());
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);
145 JSValueRef values[1] = { value };
146 ArgumentValidator validator(context, 1, values);
147 if( name == "Number" ){
148 priv->obj->setNumber( validator.toLong(0));
150 }else if( name == "Message"){
151 priv->obj->setMessage( validator.toString(0));
154 }catch( const BasePlatformException& err){
155 printf("Silently error\n");
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());
168 return JSValueMakeUndefined(ctx);