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.
18 #include "property-value-wrapper.h"
21 #include <dali/integration-api/debug.h>
25 #include <dali-wrapper.h>
38 Dali::Property::Type propertyType;
39 BaseWrappedObject::Type wrappedType;
42 * lookup table to convert between dali property types and wrapped types
43 * Wrapped types includes actors / timers / render tasks, hence we can't just
44 * use the dali property types to encode what a wrapped pointer contains
46 const WrapTypeInfo WrapTypeInfoLookup[]=
48 { Dali::Property::VECTOR2, BaseWrappedObject::VECTOR2 },
49 { Dali::Property::VECTOR3, BaseWrappedObject::VECTOR3 },
50 { Dali::Property::VECTOR4, BaseWrappedObject::VECTOR4 },
51 { Dali::Property::RECTANGLE, BaseWrappedObject::RECTANGLE },
52 { Dali::Property::ROTATION, BaseWrappedObject::ROTATION },
53 { Dali::Property::MATRIX, BaseWrappedObject::MATRIX },
54 { Dali::Property::MATRIX3, BaseWrappedObject::MATRIX3 },
58 const unsigned int WrapTypeLookupCount = sizeof( WrapTypeInfoLookup ) / sizeof( WrapTypeInfo );
60 BaseWrappedObject::Type GetWrappedType( Dali::Property::Type type )
62 for( unsigned int i = 0; i < WrapTypeLookupCount; ++i)
64 if( WrapTypeInfoLookup[ i ].propertyType == type )
66 return WrapTypeInfoLookup[ i ].wrappedType;
69 DALI_LOG_ERROR("GetWrappedType failed \n");
70 return BaseWrappedObject::UNKNOWN;
73 // Templates for setting members on propertys, like, x,y,z on a vector
74 template <typename T, typename S>
75 void SetX( Dali::Property::Value &value, S& v)
77 // to set a property value, we need to read into a temporary
78 // then assign it back.
79 // E.g. Vector2 temp = value.Get<Vector2>();
82 T tmp( value.Get<T>() );
87 template <typename T, typename S>
88 void SetY( Dali::Property::Value &value, S& v)
90 T tmp = value.Get<T>();
95 template <typename T, typename S>
96 void SetZ( Dali::Property::Value &value, S& v)
98 T tmp = value.Get<T>();
103 template <typename T, typename S>
104 void SetW( Dali::Property::Value &value, S& v)
106 T tmp = value.Get<T>();
110 template <typename T, typename S>
111 void SetWidth( Dali::Property::Value &value, S& v)
113 T tmp = value.Get<T>();
118 template <typename T, typename S>
119 void SetHeight( Dali::Property::Value &value, S& v)
121 T tmp = value.Get<T>();
126 v8::Handle< v8::Value > GetV8ValueFrom( v8::Isolate* isolate,
127 const Dali::Property::Value& value,
128 const std::string& field)
130 v8::EscapableHandleScope handleScope( isolate );
131 v8::Local<v8::Value> ret;
133 Dali::Property::Type type = value.GetType();
134 std::string objectType="Unknown type";
136 // simple property values are handled by the actor wrapper so we just handle property values with sub fields
139 case Dali::Property::VECTOR2:
141 objectType = "Vector2";
143 if( field == "x" || field == "width" )
145 Dali::Vector2 v2 = value.Get<Dali::Vector2>() ;
146 ret = v8::Number::New( isolate, v2.x) ;
148 else if(field == "y" || field == "height")
150 Dali::Vector2 v2 = value.Get<Dali::Vector2>() ;
151 ret = v8::Number::New( isolate, v2.y) ;
156 case Dali::Property::VECTOR3:
158 objectType = "Vector3";
160 if(field == "x" || field == "width" || field == "r")
162 Dali::Vector3 v3 = value.Get<Dali::Vector3>() ;
163 ret = v8::Number::New( isolate, v3.x) ;
165 else if(field == "y" || field == "height" || field == "g")
167 Dali::Vector3 v3 = value.Get<Dali::Vector3>() ;
168 ret = v8::Number::New( isolate, v3.y) ;
170 else if(field == "z" || field == "depth" || field == "b")
172 Dali::Vector3 v3 = value.Get<Dali::Vector3>() ;
173 ret = v8::Number::New( isolate, v3.z) ;
177 case Dali::Property::VECTOR4:
179 objectType = "vector4";
181 if(field == "x" || field == "s" || field == "r")
183 Dali::Vector4 v4 = value.Get<Dali::Vector4>() ;
184 ret = v8::Number::New( isolate, v4.x) ;
186 else if(field == "y" || field == "g" || field == "t")
188 Dali::Vector4 v4 = value.Get<Dali::Vector4>() ;
189 ret = v8::Number::New( isolate, v4.y) ;
191 else if(field == "z" || field == "r" || field == "b")
193 Dali::Vector4 v4 = value.Get<Dali::Vector4>() ;
194 ret = v8::Number::New( isolate, v4.z) ;
196 else if(field == "w" || field == "a" || field == "q")
198 Dali::Vector4 v4 = value.Get<Dali::Vector4>() ;
199 ret = v8::Number::New( isolate, v4.w) ;
204 case Dali::Property::ROTATION:
206 objectType = "rotation";
210 Dali::Vector4 v4 = value.Get<Dali::Quaternion>().EulerAngles();
211 ret = v8::Number::New( isolate, v4.z) ;
213 else if(field == "pitch")
215 Dali::Vector4 v4 = value.Get<Dali::Quaternion>().EulerAngles();
216 ret = v8::Number::New( isolate, v4.x) ;
218 else if(field == "yaw")
220 Dali::Vector4 v4 = value.Get<Dali::Quaternion>().EulerAngles();
221 ret = v8::Number::New( isolate, v4.y) ;
224 else if(field == "axis")
228 value.Get<Dali::Quaternion>().ToAxisAngle(axis, angle);
229 ret = PropertyValueWrapper::WrapDaliProperty( isolate , Dali::Property::Value( axis ) );
231 else if(field == "angle")
235 value.Get<Dali::Quaternion>().ToAxisAngle(axis, angle);
236 ret = PropertyValueWrapper::WrapDaliProperty( isolate , Dali::Property::Value( angle ) );
240 case Dali::Property::RECTANGLE:
242 objectType = "rectangle";
246 Dali::Rect<int> r = value.Get<Dali::Rect<int> >() ;
247 ret = v8::Number::New( isolate, r.x) ;
249 else if(field == "y")
251 Dali::Rect<int> r = value.Get<Dali::Rect<int> >() ;
252 ret = v8::Number::New( isolate, r.y) ;
254 else if(field == "width")
256 Dali::Rect<int> r = value.Get<Dali::Rect<int> >() ;
257 ret = v8::Number::New( isolate, r.width) ;
259 else if(field == "height")
261 Dali::Rect<int> r = value.Get<Dali::Rect<int> >() ;
262 ret = v8::Number::New( isolate, r.height) ;
269 // will trigger a type error as ret.IsEmpty() == true
277 DALI_SCRIPT_EXCEPTION( isolate, "Cannot get property with field " + objectType+ ":" + field );
279 return handleScope.Escape(ret);
284 * Setting a field on a property, e.g. Position.x = 5 or Rect.Width = 100
285 * The only property field that supports a struct is rotation.axis
287 void SetFromV8Value(v8::Isolate* isolate,
288 Dali::Property::Value& value,
289 const std::string& field,
290 v8::Local<v8::Value> v8Value )
292 v8::EscapableHandleScope handleScope( isolate );
294 Dali::Property::Type type = value.GetType();
299 // all are numbers at the moment
300 if( v8Value->IsNumber() )
302 asFloat = static_cast<float>(v8Value->NumberValue());
303 asInt = static_cast<int>(asFloat);
307 // if the v8 value isn't a number, then then only Dali object we support is a Vector3 part of a rotation
308 if( ! (type == Dali::Property::ROTATION && field == "axis") )
310 DALI_SCRIPT_EXCEPTION(isolate,std::string("Cannot set property with field (value is not a number):") + field);
315 // simple property values are handled by the actor wrapper so we just handle property values with sub fields
318 case Dali::Property::VECTOR2:
320 if(field == "x" || field == "width")
322 SetX<Dali::Vector2>(value, asFloat) ;
324 else if(field == "y" || field == "height")
326 SetY<Dali::Vector2>(value, asFloat) ;
330 DALI_SCRIPT_EXCEPTION(isolate, std::string("Cannot set property with field (unknown type):") + field);
335 case Dali::Property::VECTOR3:
337 if(field == "x" || field == "width" || field == "r")
339 SetX<Dali::Vector3>(value, asFloat) ;
341 else if(field == "y" || field == "height" || field == "g")
343 SetY<Dali::Vector3>(value, asFloat) ;
345 else if(field == "z" || field == "depth" || field == "b")
347 SetZ<Dali::Vector3>(value, asFloat) ;
351 DALI_SCRIPT_EXCEPTION(isolate, std::string("Cannot set property with field (unknown type):") + field);
356 case Dali::Property::VECTOR4:
358 if(field == "x" || field == "s" || field == "r")
360 SetX<Dali::Vector4>(value, asFloat) ;
362 else if(field == "y" || field == "g" || field == "t")
364 SetY<Dali::Vector4>(value, asFloat) ;
366 else if(field == "z" || field == "r" || field == "b")
368 SetZ<Dali::Vector4>(value, asFloat) ;
370 else if(field == "w" || field == "a" || field == "q")
372 SetW<Dali::Vector4>(value, asFloat) ;
376 DALI_SCRIPT_EXCEPTION( isolate, std::string("Cannot set property with field (unknown type):") + field);
381 case Dali::Property::ROTATION:
385 Dali::Vector4 v4 = value.Get<Dali::Quaternion>().EulerAngles();
387 value = Dali::Quaternion( Radian(Degree(v4.x)), Radian(Degree(v4.y)), Radian(Degree(v4.z)) );
389 else if(field == "pitch")
391 Dali::Vector4 v4 = value.Get<Dali::Quaternion>().EulerAngles();
393 value = Dali::Quaternion( Radian(Degree(v4.x)), Radian(Degree(v4.y)), Radian(Degree(v4.z)) );
395 else if(field == "yaw")
397 Dali::Vector4 v4 = value.Get<Dali::Quaternion>().EulerAngles();
399 value = Dali::Quaternion( Radian(Degree(v4.x)), Radian(Degree(v4.y)), Radian(Degree(v4.z)) );
401 else if(field == "axis")
405 value.Get<Dali::Quaternion>().ToAxisAngle(axis, angle);
407 if( v8Value->IsObject() )
409 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast( v8Value );
411 PropertyValueWrapper* wrapper = PropertyValueWrapper::Unwrap( isolate, object );
412 Dali::Property::Value v3 = wrapper->GetValue();
414 if(v3.GetType() == Dali::Property::VECTOR3)
416 value = Dali::Quaternion(angle, v3.Get<Dali::Vector3>());
420 DALI_SCRIPT_EXCEPTION( isolate, "Axis expects a Vector3:");
426 else if(field == "angle")
430 value.Get<Dali::Quaternion>().ToAxisAngle( axis, angle );
431 value = Dali::Quaternion( Degree( asFloat ), axis );
435 DALI_SCRIPT_EXCEPTION( isolate, std::string("Cannot set property with field (unknown type):") + field);
440 case Dali::Property::RECTANGLE:
444 SetX<Dali::Rect<int> >(value, asInt) ;
446 else if(field == "y")
448 SetY<Dali::Rect<int> >(value, asInt) ;
450 else if(field == "width")
452 SetWidth<Dali::Rect<int> >(value, asInt) ;
454 else if(field == "height")
456 SetHeight<Dali::Rect<int> >(value, asInt);
460 DALI_SCRIPT_EXCEPTION( isolate, std::string("Cannot set property with field (unknown type):") + field);
467 DALI_SCRIPT_EXCEPTION( isolate, std::string("Cannot set property with field (unknown type):") + field);
477 bool IsPrimitive( const Dali::Property::Value &value )
479 switch( value.GetType() )
481 case Dali::Property::BOOLEAN:
482 case Dali::Property::INTEGER:
483 case Dali::Property::UNSIGNED_INTEGER:
484 case Dali::Property::STRING:
485 case Dali::Property::FLOAT:
495 v8::Local<v8::Object> CreateJavaScriptPrimitive( v8::Isolate* isolate, const Dali::Property::Value &value )
497 v8::EscapableHandleScope handleScope( isolate );
499 v8::Local<v8::Value> v8Value ;
501 switch( value.GetType() )
503 case Dali::Property::FLOAT:
505 v8Value = v8::Number::New( isolate, value.Get<float>() );
508 case Dali::Property::BOOLEAN:
510 v8Value = v8::Boolean::New( isolate, value.Get<bool>());
513 case Dali::Property::INTEGER:
515 v8Value = v8::Integer::New( isolate, value.Get<int>());
518 case Dali::Property::UNSIGNED_INTEGER:
520 v8Value = v8::Integer::New( isolate, value.Get<unsigned int>());
523 case Dali::Property::STRING:
525 std::string string = value.Get< std::string >();
526 v8Value = v8::String::NewFromUtf8( isolate, string.c_str());
531 DALI_LOG_ERROR("Primitive mismatch \n");
535 v8::Local<v8::Object> ret = v8Value->ToObject();
537 return handleScope.Escape( ret );
541 } // un-named namespace
545 Dali::Property::Value PropertyValueWrapper::VectorOrMatrixFromV8Array( v8::Isolate* isolate, const v8::Local<v8::Value>& v8Value)
547 v8::EscapableHandleScope handleScope( isolate );
549 Dali::Property::Value ret;
551 if( !v8Value->IsArray() )
555 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast( v8Value );//v8::Array::Cast( *v8Value);
556 uint32_t len = array->Length();
557 float out[16] = {0.f, 0.f, 0.f, 0.f,0.f, 0.f, 0.f, 0.f,0.f, 0.f, 0.f, 0.f,0.f, 0.f, 0.f, 0.f};
558 const unsigned int size = sizeof(out) / sizeof(float);
559 for( uint32_t i=0; i < (len > size? size:len); ++i)
561 v8::Handle<v8::Value> entry = array->Get( i);// v8::Integer::New( isolate, i));
562 if( V8Utils::IsNumberPrimitiveOrObject( entry ) )
564 out[i] = V8Utils::GetNumberValue( isolate, entry );
565 //printf("entry is a number %d , entry is a int %f \n",(int)i , out[i] );
570 ret = Dali::Matrix( out[0] );
574 ret = Dali::Matrix3( out[0],out[1],out[2],out[3],out[4],out[5],out[6],out[7],out[8] );
578 ret = Dali::Vector4(out[0], out[1], out[2], out[3]);
582 ret = Dali::Vector3(out[0], out[1], out[2]);
586 ret = Dali::Vector2(out[0], out[1]);
596 Dali::Property::Value PropertyValueWrapper::ArrayFromV8Array( v8::Isolate* isolate, const v8::Local<v8::Value>& v8Value)
598 v8::EscapableHandleScope handleScope( isolate );
600 if( !v8Value->IsArray() )
602 return Dali::Property::Value();
605 //Cast v8::Value to v8::Array
606 v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast( v8Value );
607 uint32_t elementCount = v8Array->Length();
608 Dali::Property::Array data( elementCount );
610 for( uint32_t i(0); i<elementCount; ++i )
612 //Get Property::Value for each element in the array
613 data[i] = V8Utils::GetPropertyValueFromObject( bFound, isolate, v8Array->Get(i) );
616 //return the Property::Value
617 return Dali::Property::Value( data );
620 v8::Persistent<v8::ObjectTemplate> PropertyValueWrapper::mTemplatePrimitive;
623 PropertyValueWrapper::~PropertyValueWrapper()
628 PropertyValueWrapper::PropertyValueWrapper( const Dali::Property::Value& value, GarbageCollectorInterface& gc )
629 : BaseWrappedObject( GetWrappedType( value.GetType() ), gc ),
634 Dali::Property::Value PropertyValueWrapper::ExtractPropertyValue( v8::Isolate* isolate, v8::Local< v8::Value> v8Value, Dali::Property::Type type)
636 v8::HandleScope handleScope( isolate);
638 Dali::Property::Value daliPropertyValue;
640 // check if it's a dali object (vector, rect etc)
641 if( v8Value->IsObject() )
643 v8::Local<v8::Object> object = v8::Handle<v8::Object>::Cast(v8Value);
644 if( object->InternalFieldCount() == BaseWrappedObject::FIELD_COUNT )
646 if( BaseWrappedObject::IsWrappedType( isolate, object, GetWrappedType(type) ) )
648 PropertyValueWrapper* propertyWrapper = Unwrap( isolate, object );
649 return propertyWrapper->GetValue();
654 // check if it's a javascript Array
655 Dali::Property::Value array = VectorOrMatrixFromV8Array( isolate, v8Value );
659 case Dali::Property::BOOLEAN:
661 if( V8Utils::IsBooleanPrimitiveOrObject( v8Value ) )
663 daliPropertyValue = Dali::Property::Value( V8Utils::GetBooleanValue( isolate, v8Value));
667 case Dali::Property::FLOAT:
669 if( V8Utils::IsNumberPrimitiveOrObject( v8Value ) )
671 daliPropertyValue = Dali::Property::Value( V8Utils::GetNumberValue( isolate, v8Value) );
676 case Dali::Property::INTEGER: // todo is it wise to distinguish int from float in javascript?!
678 if( v8Value->IsInt32() )
680 daliPropertyValue = Dali::Property::Value( v8Value->Int32Value() ) ;//static_cast<int>( V8Utils::GetNumberValue( isolate, v8Value) ));
684 case Dali::Property::UNSIGNED_INTEGER:
686 if( v8Value->IsUint32() )
688 daliPropertyValue = Dali::Property::Value( v8Value->Uint32Value() );//static_cast<unsigned int>( V8Utils::GetNumberValue( isolate, v8Value) ));
692 case Dali::Property::STRING:
694 if( V8Utils::IsStringPrimitiveOrObject( v8Value) )
696 daliPropertyValue = Dali::Property::Value( V8Utils::GetStringValue( isolate, v8Value) );
702 case Dali::Property::VECTOR2:
704 if( array.GetType() == Dali::Property::VECTOR2 )
706 daliPropertyValue = array;
710 case Dali::Property::VECTOR3:
712 if( array.GetType() == Dali::Property::VECTOR3)
714 daliPropertyValue = array;
718 case Dali::Property::VECTOR4:
721 if( array.GetType() == Dali::Property::VECTOR4)
723 daliPropertyValue = array;
727 case Dali::Property::RECTANGLE:
729 if( array.GetType() == Dali::Property::VECTOR4)
731 Vector4 vec4( array.Get<Vector4>());
732 daliPropertyValue = Property::Value(Rect<int>( vec4.x,vec4.y, vec4.z, vec4.w ));
736 case Dali::Property::ROTATION:
738 if( array.GetType() == Dali::Property::VECTOR4)
740 // v4 treated as axis angle
741 Dali::Vector4 v = array.Get<Dali::Vector4>();
742 daliPropertyValue = Dali::AngleAxis(Dali::Degree(v[0]), Dali::Vector3(v[1], v[2], v[3]));
744 else if(array.GetType() == Dali::Property::VECTOR3)
746 // v3 treated as euler in degrees
747 Dali::Vector3 v = array.Get<Dali::Vector3>();
748 daliPropertyValue = Dali::Quaternion( Radian(Degree(v[0])), Radian(Degree(v[1])), Radian(Degree(v[2])) );
752 case Dali::Property::MATRIX:
754 Dali::Matrix mat = array.Get<Dali::Matrix>();
755 daliPropertyValue = mat;
758 case Dali::Property::MATRIX3:
760 Dali::Matrix3 mat = array.Get<Dali::Matrix3>();
761 daliPropertyValue = mat;
764 case Dali::Property::ARRAY:
766 daliPropertyValue = ArrayFromV8Array( isolate, v8Value );
769 case Dali::Property::NONE:
770 case Dali::Property::TYPE_COUNT:
777 return daliPropertyValue;
780 void PropertyValueWrapper::NewRotation( const v8::FunctionCallbackInfo< v8::Value >& args)
782 v8::Isolate* isolate = args.GetIsolate();
783 v8::HandleScope handleScope( isolate);
785 if(!args.IsConstructCall())
787 DALI_SCRIPT_EXCEPTION( isolate, "Rotation constructor called without 'new'");
790 v8::Local<v8::Object > object;
792 float v[4] = {0.f,0.f,0.f,0.f} ;
794 bool foundAllArguments(false);
795 V8Utils::ReadFloatArguments( foundAllArguments, v, 4, args, 0.f );
797 int length = args.Length();
799 // if length = 4 create AngleAxis, else create Quaternion
803 const Dali::AngleAxis axis( Degree( v[0] ), Vector3(v[1], v[2], v[3]) );
804 object = WrapDaliProperty( isolate, Dali::Property::Value( axis ) );
806 else if( length > 2 )
808 object = WrapDaliProperty( isolate, Dali::Quaternion( Degree(v[0]), Degree(v[1]), Degree(v[2]) ) );
812 const Dali::Quaternion quaternion( Dali::Quaternion( Dali::ANGLE_0, Dali::Vector3::YAXIS));
813 object = WrapDaliProperty( isolate, quaternion );
816 args.GetReturnValue().Set( object );
820 void PropertyValueWrapper::NewMatrix( const v8::FunctionCallbackInfo< v8::Value >& args )
822 v8::Isolate* isolate = args.GetIsolate();
823 v8::HandleScope handleScope( isolate);
825 if(!args.IsConstructCall())
827 DALI_SCRIPT_EXCEPTION( isolate, "Rotation constructor called without 'new'");
830 v8::Local<v8::Object > object;
832 // we're constructing either a 4x4 Dali::Matrix or a 3x3 Dali::Matrix
833 // we're expecting an array of floats
834 // 4x4 = m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33
835 // 3x3 or m00, m01, m02, m10, m11, m12,m20, m21, m22, m30, m31, m32,
836 bool foundAllArguments( false );
838 if( args.Length() == 16 )
841 V8Utils::ReadFloatArguments( foundAllArguments, v, 16, args, 0.f );
842 if(! foundAllArguments )
844 DALI_SCRIPT_EXCEPTION( isolate, "invalid Matrix parameters");
847 Dali::Matrix matrix( v[0] );
848 object = WrapDaliProperty( isolate , Dali::Property::Value( matrix ) );
850 else if ( args.Length() == 9 )
853 V8Utils::ReadFloatArguments( foundAllArguments, v, 9, args, 0.f );
854 if(! foundAllArguments )
856 DALI_SCRIPT_EXCEPTION( isolate, "invalid Matrix parameters");
859 Dali::Matrix3 matrix( v[0], v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8] );
860 object = WrapDaliProperty( isolate , Dali::Property::Value( matrix ) );
862 args.GetReturnValue().Set( object );
867 v8::Handle<v8::ObjectTemplate> PropertyValueWrapper::MakeDaliPropertyTemplate( v8::Isolate* isolate )
869 v8::EscapableHandleScope handleScope( isolate );
871 v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
873 objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
875 // property handle intercepts property getters and setters
876 objTemplate->SetNamedPropertyHandler( PropertyGet, PropertySet);
878 return handleScope.Escape( objTemplate );
882 v8::Handle<v8::Object> PropertyValueWrapper::WrapDaliProperty(v8::Isolate* isolate, const Dali::Property::Value &value)
884 v8::EscapableHandleScope handleScope( isolate );
886 // Simple Dali properties (ints, strings, bools etc) are stored as JavaScript primitives (v8::Boolean ...)
887 // more complex properties (Vectors, Rectangles...) are wrapped by a JavaScript object
888 if( IsPrimitive( value ))
890 return handleScope.Escape( CreateJavaScriptPrimitive( isolate, value) );
893 // This will wrap a Dali structure (Vector, Rect, Matrix etc) inside a JavaScript object.
895 v8::Local<v8::ObjectTemplate> objectTemplate;
897 if(PropertyValueWrapper::mTemplatePrimitive.IsEmpty() )
899 objectTemplate = MakeDaliPropertyTemplate( isolate );
900 PropertyValueWrapper::mTemplatePrimitive.Reset( isolate, objectTemplate);
904 // get the object template
905 objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, PropertyValueWrapper::mTemplatePrimitive );
908 v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
910 PropertyValueWrapper* pointer = new PropertyValueWrapper( value, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
912 // assign the JavaScript object to the wrapper.
913 // This also stores Dali object, in an internal field inside the JavaScript object.
914 pointer->SetJavascriptObject( isolate, localObject );
916 return handleScope.Escape( localObject );
920 // Utility function that extracts the C++ map pointer from a wrapper
923 PropertyValueWrapper* PropertyValueWrapper::Unwrap( v8::Isolate* isolate, v8::Handle< v8::Object> obj)
925 v8::HandleScope handleScope( isolate );
927 v8::Local<v8::External> field = v8::Local<v8::External>::Cast( obj->GetInternalField(0) );
928 void* ptr = field->Value();
929 return static_cast< PropertyValueWrapper *>(ptr);
932 void PropertyValueWrapper::PropertyGet( v8::Local<v8::String> propertyName,
933 const v8::PropertyCallbackInfo<v8::Value>& info)
935 v8::Isolate* isolate = info.GetIsolate();
936 v8::HandleScope handleScope( isolate );
938 std::string name = V8Utils::v8StringToStdString( propertyName );
941 PropertyValueWrapper* propWrapper = Unwrap( isolate, info.This() );
943 v8::Local<v8::Value> value = GetV8ValueFrom( isolate, propWrapper->mValue, name );
945 info.GetReturnValue().Set( value );
948 void PropertyValueWrapper::PropertySet( v8::Local<v8::String> propertyName,
949 v8::Local<v8::Value> javaScriptValue,
950 const v8::PropertyCallbackInfo<v8::Value>& info)
952 v8::Isolate* isolate = info.GetIsolate();
953 v8::HandleScope handleScope( isolate );
955 std::string name = V8Utils::v8StringToStdString( propertyName );
957 PropertyValueWrapper* propWrapper = Unwrap( isolate, info.This() );
959 SetFromV8Value( isolate, propWrapper->mValue, name, javaScriptValue );
964 } // namespace V8Plugin