2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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 #include "dali-any-javascript-converter.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>
36 v8::Local<v8::Value> DaliAnyConverter::ConvertToJavaScriptObject(v8::Isolate* isolate, Dali::Any& value )
38 v8::EscapableHandleScope handleScope( isolate );
39 v8::Local<v8::Value> returnValue;
41 const std::type_info& typeInfo( value.GetType());
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.
46 if( typeInfo == typeid( Dali::Actor ))
48 Actor actor = value.Get<Actor>();
51 returnValue = ActorWrapper::WrapActor( isolate, actor );
55 returnValue = v8::Undefined( isolate );
58 else if( typeInfo == typeid( std::string ) )
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());
65 else if( typeInfo == typeid( Dali::Vector3 ) )
67 returnValue = PropertyValueWrapper::WrapDaliProperty( isolate, value.Get<Vector3>() );
69 else if( typeInfo == typeid( Dali::TouchEvent ) )
71 returnValue = EventObjectGenerator::CreateTouchEvent( isolate, value.Get<TouchEvent>() );
73 else if( typeInfo == typeid( Dali::HoverEvent ) )
75 returnValue = EventObjectGenerator::CreateHoverEvent( isolate, value.Get<HoverEvent>() );
77 else if( typeInfo == typeid( Dali::MouseWheelEvent ) )
79 returnValue = EventObjectGenerator::CreateMouseWheelEvent( isolate, value.Get<MouseWheelEvent>() );
81 else if( typeInfo == typeid( Dali::KeyEvent ) )
83 returnValue = EventObjectGenerator::CreateKeyEvent( isolate, value.Get<KeyEvent>() );
85 else if( typeInfo == typeid( Dali::PanGesture ) )
87 returnValue = EventObjectGenerator::CreatePanGesture( isolate, value.Get<PanGesture>() );
89 else if( typeInfo == typeid( Dali::Animation ) )
91 returnValue = AnimationWrapper::WrapAnimation( isolate, value.Get<Animation>() );
93 else if( typeInfo == typeid( Dali::Image ) )
95 returnValue = ImageWrapper::WrapImage( isolate, value.Get<Image>() );
99 DALI_SCRIPT_EXCEPTION( isolate, " Failed to find Dali::Any to JavaScript converter \n" );
102 return handleScope.Escape( returnValue );
105 Dali::Any DaliAnyConverter::ConvertToDaliAny( v8::Isolate* isolate, v8::Local<v8::Value> value, const Dali::Any& requestedType)
107 v8::HandleScope handleScope( isolate );
109 if( value.IsEmpty() )
111 DALI_SCRIPT_EXCEPTION( isolate, "Callback missing a return value \n");
114 // try to match the return value types
115 if( requestedType.GetType() == typeid( bool ))
117 if( value->IsBoolean() )
119 return value->ToBoolean()->Value();
123 DALI_SCRIPT_EXCEPTION( isolate, "Invalid return type from callback, wanted a bool \n");
126 else if( requestedType.GetType() == typeid( unsigned int ) )
128 if( value->IsUint32() )
130 return value->ToUint32()->Value();
134 DALI_SCRIPT_EXCEPTION( isolate, "Invalid return type from callback, wanted an unsigned int\n");
137 // allow positive and negative numbers for int,@todo test how v8 behaves with numbers returned
138 else if( requestedType.GetType() == typeid( int ))
140 if( value->IsInt32() )
142 return value->ToInt32()->Value();
144 else if( value->IsUint32() )
146 return static_cast<int>( value->ToUint32()->Value());
150 DALI_SCRIPT_EXCEPTION(isolate, "Invalid return type from callback, wanted an int\n");
153 else if( requestedType.GetType() == typeid( float ))
155 if( value->IsNumber() )
157 return value->ToNumber()->Value();
159 else if( value->IsInt32() )
161 return static_cast<float>(value->ToInt32()->Value());
163 else if( value->IsUint32() )
165 return static_cast<float>( value->ToUint32()->Value() );
169 DALI_SCRIPT_EXCEPTION(isolate, "Invalid return type from callback, wanted a float\n");
172 else if( requestedType.GetType() == typeid( Actor ))
174 if( value->IsObject() )
176 HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, value->ToObject() );
179 return Actor::DownCast( handleWrapper->mHandle );
183 DALI_SCRIPT_EXCEPTION(isolate, "Invalid return type from callback, wanted an Actor \n");
188 // undefined is used to describe an empty actor handle
189 if (value->IsUndefined())
195 DALI_SCRIPT_EXCEPTION(isolate, "missing return type, wanted an Actor \n");
200 return Dali::Any(); // empty any
203 } // namespace V8Plugin