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 // convert from camel case to dali property style with hyphens
96 std::string daliPropertyName = V8Utils::JavaScriptNameToPropertyName(name);
97 Dali::Property::Index index = handle.GetPropertyIndex( daliPropertyName );
99 if(index != Dali::Property::INVALID_INDEX)
101 Dali::Property::Value value = handle.GetProperty(index);
103 // Simple Dali properties (ints, strings, bools etc) are stored as JavaScript primitives (v8::Boolean ...)
104 // more complex properties (Vectors, Rectangles...) are wrapped by a JavaScript object
105 v8::Local<v8::Object> ret = PropertyValueWrapper::WrapDaliProperty( isolate, value );
107 info.GetReturnValue().Set( ret );
111 // std::string error="Invalid property Get for "+name + "\n";
112 // DALI_SCRIPT_EXCEPTION( isolate, error );
116 void HandleWrapper::PropertySet( v8::Local<v8::String> propertyName,
117 v8::Local<v8::Value> javaScriptValue,
118 const v8::PropertyCallbackInfo<v8::Value>& info)
121 v8::Isolate* isolate = info.GetIsolate();
122 v8::HandleScope handleScope( isolate );
124 // get the property name
125 std::string name = V8Utils::v8StringToStdString( propertyName );
127 // try to filter out function calls before going to the property system
128 // @todo use installed functions to generate a map
129 if( ( name.compare(0,2,"is") == 0 ) ||
130 ( name.compare(0,3,"get") == 0 ) ||
131 ( name.compare(0,3,"add") == 0 ) ||
132 ( name.compare(0,3,"set") == 0 ) ||
133 ( name.compare(0,3,"get") == 0 ) ||
134 ( name.compare(0,4,"find") == 0 ) ||
135 ( name.compare(0,6,"remove") == 0 )
142 HandleWrapper* handleWrapper = Unwrap( isolate, info.This() );
145 // printf("setting property name %s \n", name.c_str());
149 // DALI_ASSERT_DEBUG( handleWrapper && "not a dali object");
150 Handle handle = handleWrapper->mHandle;
152 // convert from camel case to dali property style with hyphens
153 std::string daliPropertyName = V8Utils::JavaScriptNameToPropertyName(name);
154 Dali::Property::Index index = handle.GetPropertyIndex( daliPropertyName );
156 if(index != Dali::Property::INVALID_INDEX)
158 Dali::Property::Type type = handle.GetPropertyType(index);
160 // we know the type we want to set ( int, vector, etc..)
161 // try and convert the javascript value in to the type we want.
162 Dali::Property::Value value = PropertyValueWrapper::ExtractPropertyValue( isolate, javaScriptValue, type);
164 if( Dali::Property::NONE == value.GetType() )
166 std::stringstream msg;
167 msg << "Invalid property Set: '";
172 msg << "' Cannot convert value to correct type: (";
175 msg << Dali::PropertyTypes::GetName(type);
176 DALI_SCRIPT_EXCEPTION( isolate, msg.str().c_str());
180 handle.SetProperty( index, value );
185 std::string error="Invalid property Set for "+name + "\n";
186 DALI_SCRIPT_EXCEPTION( isolate, error );
190 void HandleWrapper::AddInterceptsToTemplate( v8::Isolate* isolate, v8::Local<v8::ObjectTemplate>& objTemplate )
192 v8::HandleScope handleScope( isolate );
194 objTemplate->SetNamedPropertyHandler( HandleWrapper::PropertyGet, HandleWrapper::PropertySet);
196 // add function properties
197 ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, HandleFunctionTable, HandleFunctionTableCount );
199 ObjectTemplateHelper::AddSignalConnectAndDisconnect( isolate, objTemplate );
204 * Register a new animatable property.
206 * The object should support dynamic properties.
207 * Property names are expected to be unique, but this is not enforced.
208 * Property indices are unique to each registered custom property in a given object.
209 * returns dali.PROPERTY_INVALID_INDEX if registration failed. This can happen if you try
210 * to register animatable property on an object that does not have scene graph object.
212 * @method registerAnimatableProperty
214 * @param {string} name The name of the property.
215 * @param {Object} propertyValue The new value of the property.
216 * @return {integer} The index of the property or dali.PROPERTY_INVALID_INDEX if registration failed
219 * var morphPropertyIdex = actor.registerAnimatableProperty("uMorphAmount", 0.0f);
220 * var fadeColorPropertyIdex = handle.registerAnimatableProperty("uFadeColor", [1.0, 0.0, 0.0, 1.0]);
223 void HandleWrapper::RegisterAnimatableProperty( const v8::FunctionCallbackInfo< v8::Value >& args )
225 v8::Isolate* isolate = args.GetIsolate();
226 v8::HandleScope handleScope( isolate );
229 HandleWrapper* handleWrapper = Unwrap( isolate, args.This() );
235 Handle handle = handleWrapper->mHandle;
238 std::string propertyName = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
241 DALI_SCRIPT_EXCEPTION( isolate, "bad property name parameter" );
246 Dali::Property::Value daliPropertyValue = V8Utils::GetPropertyValueParameter(PARAMETER_1, found, isolate, args );
247 if( !found || Dali::Property::NONE == daliPropertyValue.GetType() )
249 DALI_SCRIPT_EXCEPTION( isolate, "bad property value parameter" );
254 args.GetReturnValue().Set( v8::Integer::New( isolate, handle.RegisterProperty(propertyName, daliPropertyValue) ) );
258 } // namespace V8Plugin