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 "handle-wrapper.h"
26 #include <shared/base-wrapped-object.h>
27 #include <shared/object-template-helper.h>
28 #include <object/property-value-wrapper.h>
36 namespace // un-named name space
40 * Contains a list of all functions that can be called
42 const ApiFunction HandleFunctionTable[]=
44 { "RegisterAnimatableProperty", HandleWrapper::RegisterAnimatableProperty },
47 const unsigned int HandleFunctionTableCount = sizeof(HandleFunctionTable)/sizeof(HandleFunctionTable[0]);
54 HandleWrapper::HandleWrapper( BaseWrappedObject::Type type,
56 GarbageCollectorInterface& gc ) :
57 BaseWrappedObject( type, gc ),
62 HandleWrapper::~HandleWrapper()
66 HandleWrapper* HandleWrapper::Unwrap( v8::Isolate* isolate, v8::Handle< v8::Object> obj)
68 v8::HandleScope handleScope( isolate );
70 v8::Local<v8::External> field = v8::Local<v8::External>::Cast( obj->GetInternalField(0) );
71 void* ptr = field->Value();
72 return static_cast< HandleWrapper *>(ptr);
75 // may have to do this IsUpper to intercept function calls or as function?
76 void HandleWrapper::PropertyGet( v8::Local<v8::String> propertyName,
77 const v8::PropertyCallbackInfo<v8::Value>& info)
79 v8::Isolate* isolate = info.GetIsolate();
80 v8::HandleScope handleScope( isolate );
82 // get the property name
83 std::string name = V8Utils::v8StringToStdString( propertyName );
85 if( std::isupper( name[0] ))
91 HandleWrapper* handleWrapper = Unwrap( isolate, info.This() );
92 Handle handle = handleWrapper->mHandle;
94 // get the property index
95 Dali::Property::Index index = handle.GetPropertyIndex( name );
97 if(index != Dali::Property::INVALID_INDEX)
99 Dali::Property::Value value = handle.GetProperty(index);
101 // Simple Dali properties (ints, strings, bools etc) are stored as JavaScript primitives (v8::Boolean ...)
102 // more complex properties (Vectors, Rectangles...) are wrapped by a JavaScript object
103 v8::Local<v8::Object> ret = PropertyValueWrapper::WrapDaliProperty( isolate, value );
105 info.GetReturnValue().Set( ret );
109 // std::string error="Invalid property Get for "+name + "\n";
110 // DALI_SCRIPT_EXCEPTION( isolate, error );
114 void HandleWrapper::PropertySet( v8::Local<v8::String> propertyName,
115 v8::Local<v8::Value> javaScriptValue,
116 const v8::PropertyCallbackInfo<v8::Value>& info)
119 v8::Isolate* isolate = info.GetIsolate();
120 v8::HandleScope handleScope( isolate );
122 // get the property name
123 std::string name = V8Utils::v8StringToStdString( propertyName );
125 // try to filter out function calls before going to the property system
126 // @todo use installed functions to generate a map
127 if( ( name.compare(0,2,"is") == 0 ) ||
128 ( name.compare(0,3,"get") == 0 ) ||
129 ( name.compare(0,3,"add") == 0 ) ||
130 ( name.compare(0,3,"set") == 0 ) ||
131 ( name.compare(0,3,"get") == 0 ) ||
132 ( name.compare(0,4,"find") == 0 ) ||
133 ( name.compare(0,6,"remove") == 0 )
140 HandleWrapper* handleWrapper = Unwrap( isolate, info.This() );
143 // printf("setting property name %s \n", name.c_str());
147 // DALI_ASSERT_DEBUG( handleWrapper && "not a dali object");
148 Handle handle = handleWrapper->mHandle;
150 Dali::Property::Index index = handle.GetPropertyIndex( name );
152 if(index != Dali::Property::INVALID_INDEX)
154 Dali::Property::Type type = handle.GetPropertyType(index);
156 // we know the type we want to set ( int, vector, etc..)
157 // try and convert the javascript value in to the type we want.
158 Dali::Property::Value value = PropertyValueWrapper::ExtractPropertyValue( isolate, javaScriptValue, type);
160 if( Dali::Property::NONE == value.GetType() )
162 std::stringstream msg;
163 msg << "Invalid property Set: '";
168 msg << "' Cannot convert value to correct type: (";
171 msg << Dali::PropertyTypes::GetName(type);
172 DALI_SCRIPT_EXCEPTION( isolate, msg.str().c_str());
176 handle.SetProperty( index, value );
181 std::string error="Invalid property Set for "+name + "\n";
182 DALI_SCRIPT_EXCEPTION( isolate, error );
186 void HandleWrapper::AddInterceptsToTemplate( v8::Isolate* isolate, v8::Local<v8::ObjectTemplate>& objTemplate )
188 v8::HandleScope handleScope( isolate );
190 objTemplate->SetNamedPropertyHandler( HandleWrapper::PropertyGet, HandleWrapper::PropertySet);
192 // add function properties
193 ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, HandleFunctionTable, HandleFunctionTableCount );
195 ObjectTemplateHelper::AddSignalConnectAndDisconnect( isolate, objTemplate );
200 * Register a new animatable property.
202 * The object should support dynamic properties.
203 * Property names are expected to be unique, but this is not enforced.
204 * Property indices are unique to each registered custom property in a given object.
205 * returns dali.PROPERTY_INVALID_INDEX if registration failed. This can happen if you try
206 * to register animatable property on an object that does not have scene graph object.
208 * @method registerAnimatableProperty
210 * @param {string} name The name of the property.
211 * @param {Object} propertyValue The new value of the property.
212 * @return {integer} The index of the property or dali.PROPERTY_INVALID_INDEX if registration failed
215 * var morphPropertyIdex = actor.registerAnimatableProperty("uMorphAmount", 0.0f);
216 * var fadeColorPropertyIdex = handle.registerAnimatableProperty("uFadeColor", [1.0, 0.0, 0.0, 1.0]);
219 void HandleWrapper::RegisterAnimatableProperty( const v8::FunctionCallbackInfo< v8::Value >& args )
221 v8::Isolate* isolate = args.GetIsolate();
222 v8::HandleScope handleScope( isolate );
225 HandleWrapper* handleWrapper = Unwrap( isolate, args.This() );
231 Handle handle = handleWrapper->mHandle;
234 std::string propertyName = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
237 DALI_SCRIPT_EXCEPTION( isolate, "bad property name parameter" );
242 Dali::Property::Value daliPropertyValue = V8Utils::GetPropertyValueParameter(PARAMETER_1, found, isolate, args );
243 if( !found || Dali::Property::NONE == daliPropertyValue.GetType() )
245 DALI_SCRIPT_EXCEPTION( isolate, "bad property value parameter" );
250 args.GetReturnValue().Set( v8::Integer::New( isolate, handle.RegisterProperty(propertyName, daliPropertyValue) ) );
254 } // namespace V8Plugin