DALi Version 1.4.26
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / signals / dali-any-javascript-converter.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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 // CLASS HEADER
19 #include "dali-any-javascript-converter.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <object/property-value-wrapper.h>
24 #include <actors/actor-wrapper.h>
25 #include <events/event-object-generator.h>
26 #include <animation/animation-wrapper.h>
27 #include <image/image-wrapper.h>
28
29
30 namespace Dali
31 {
32
33 namespace V8Plugin
34 {
35
36 v8::Local<v8::Value> DaliAnyConverter::ConvertToJavaScriptObject(v8::Isolate* isolate, Dali::Any& value )
37 {
38   v8::EscapableHandleScope handleScope( isolate );
39   v8::Local<v8::Value> returnValue;
40
41   const std::type_info& typeInfo( value.GetType());
42
43   // check the type of the Any and convert it to a JavaScript object
44   // if we wanted to speed this up, it would be possible to hash the typeInfo.name() field.
45
46   if( typeInfo == typeid( Dali::Actor ))
47   {
48     Actor actor =  value.Get<Actor>();
49     if( actor )
50     {
51       returnValue = ActorWrapper::WrapActor( isolate, actor );
52     }
53     else
54     {
55       returnValue = v8::Undefined( isolate );
56     }
57   }
58   else if(  typeInfo == typeid( std::string ) )
59   {
60
61     std::string string = value.Get< std::string >();
62     //printf(" converting %s to a javascript object \n", string.c_str());
63     returnValue =  v8::String::NewFromUtf8( isolate, string.c_str());
64   }
65   else if(  typeInfo == typeid( Dali::Vector3 ) )
66   {
67     returnValue = PropertyValueWrapper::WrapDaliProperty( isolate, value.Get<Vector3>() );
68   }
69   else if(  typeInfo == typeid( Dali::TouchData ) )
70   {
71     returnValue = EventObjectGenerator::CreateTouchData( isolate, value.Get<TouchData>() );
72   }
73   else if(  typeInfo == typeid( Dali::HoverEvent ) )
74   {
75     returnValue = EventObjectGenerator::CreateHoverEvent( isolate, value.Get<HoverEvent>() );
76   }
77   else if(  typeInfo == typeid( Dali::WheelEvent ) )
78   {
79     returnValue = EventObjectGenerator::CreateWheelEvent( isolate, value.Get<WheelEvent>() );
80   }
81   else if(  typeInfo == typeid( Dali::KeyEvent ) )
82   {
83     returnValue = EventObjectGenerator::CreateKeyEvent( isolate, value.Get<KeyEvent>() );
84   }
85   else if(  typeInfo == typeid( Dali::PanGesture ) )
86   {
87     returnValue = EventObjectGenerator::CreatePanGesture( isolate, value.Get<PanGesture>() );
88   }
89   else if(  typeInfo == typeid( Dali::Animation ) )
90   {
91     returnValue = AnimationWrapper::WrapAnimation( isolate, value.Get<Animation>() );
92   }
93   else if(  typeInfo == typeid( Dali::Image ) )
94   {
95     returnValue = ImageWrapper::WrapImage( isolate, value.Get<Image>() );
96   }
97   else
98   {
99     DALI_SCRIPT_EXCEPTION( isolate, " Failed to find Dali::Any to JavaScript converter \n" );
100   }
101
102   return handleScope.Escape( returnValue );
103 }
104
105 Dali::Any DaliAnyConverter::ConvertToDaliAny( v8::Isolate* isolate, v8::Local<v8::Value> value, const Dali::Any& requestedType)
106 {
107   v8::HandleScope handleScope( isolate );
108
109   if( value.IsEmpty() )
110   {
111     DALI_SCRIPT_EXCEPTION( isolate, "Callback missing a return value \n");
112     return Dali::Any();
113   }
114   // try to match the return value types
115   if( requestedType.GetType() == typeid( bool ))
116   {
117     if( value->IsBoolean() )
118     {
119       return value->ToBoolean()->Value();
120     }
121     else
122     {
123       DALI_SCRIPT_EXCEPTION( isolate, "Invalid return type from callback, wanted a bool \n");
124     }
125   }
126   else if( requestedType.GetType() == typeid( unsigned int ) )
127   {
128     if( value->IsUint32() )
129     {
130       return value->ToUint32()->Value();
131     }
132     else
133     {
134       DALI_SCRIPT_EXCEPTION( isolate, "Invalid return type from callback, wanted an unsigned int\n");
135     }
136   }
137   // allow positive and negative numbers for int,@todo test how v8 behaves with numbers returned
138   else if( requestedType.GetType() == typeid( int ))
139   {
140     if( value->IsInt32() )
141     {
142       return value->ToInt32()->Value();
143     }
144     else if( value->IsUint32() )
145     {
146       return static_cast<int>( value->ToUint32()->Value());
147     }
148     else
149     {
150       DALI_SCRIPT_EXCEPTION(isolate, "Invalid return type from callback, wanted an int\n");
151     }
152   }
153   else if( requestedType.GetType() == typeid( float ))
154   {
155     if( value->IsNumber() )
156     {
157       return value->ToNumber()->Value();
158     }
159     else if( value->IsInt32() )
160     {
161       return static_cast<float>(value->ToInt32()->Value());
162     }
163     else if( value->IsUint32() )
164     {
165       return static_cast<float>( value->ToUint32()->Value() );
166     }
167     else
168     {
169       DALI_SCRIPT_EXCEPTION(isolate, "Invalid return type from callback, wanted a float\n");
170     }
171   }
172   else if( requestedType.GetType() == typeid( Actor ))
173   {
174     if( value->IsObject()  )
175     {
176       HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, value->ToObject() );
177       if( handleWrapper )
178       {
179         return Actor::DownCast( handleWrapper->mHandle );
180       }
181       else
182       {
183         DALI_SCRIPT_EXCEPTION(isolate, "Invalid return type from callback, wanted an Actor \n");
184       }
185     }
186     else
187     {
188       // undefined is used to describe an empty actor handle
189       if (value->IsUndefined())
190       {
191         return Actor();
192       }
193       else
194       {
195         DALI_SCRIPT_EXCEPTION(isolate, "missing return type, wanted an Actor \n");
196       }
197     }
198
199   }
200   return Dali::Any(); // empty any
201 }
202
203 } // namespace V8Plugin
204
205 } // namespace Dali