From 697145b86697d27ad5d66ba0fb7a49a018b1f29c Mon Sep 17 00:00:00 2001 From: Richard Huang Date: Wed, 10 Feb 2016 15:22:00 +0000 Subject: [PATCH] Allow automatic registration of custom property in JavaScript Change-Id: I0e857fea58f85fd55a2e0b729bc01a3b67549d37 --- .../src/object/handle-wrapper.cpp | 10 +++- .../src/object/property-value-wrapper.cpp | 56 +++++++++++++++++++ .../src/object/property-value-wrapper.h | 6 ++ plugins/dali-script-v8/src/utils/v8-utils.cpp | 15 ++++- plugins/dali-script-v8/src/utils/v8-utils.h | 6 ++ 5 files changed, 89 insertions(+), 4 deletions(-) diff --git a/plugins/dali-script-v8/src/object/handle-wrapper.cpp b/plugins/dali-script-v8/src/object/handle-wrapper.cpp index be1739d503..fed77ce2e8 100644 --- a/plugins/dali-script-v8/src/object/handle-wrapper.cpp +++ b/plugins/dali-script-v8/src/object/handle-wrapper.cpp @@ -179,8 +179,14 @@ void HandleWrapper::PropertySet( v8::Local propertyName, } else { - std::string error="Invalid property Set for "+name + "\n"; - DALI_SCRIPT_EXCEPTION( isolate, error ); + // Trying to set the value for a property that is not registered yet. + std::stringstream msg; + msg << "Trying to set the value of an unregistered property: "; + msg << name; + DALI_SCRIPT_WARNING( msg.str().c_str() ); + + // Register the custom property automatically. + handle.RegisterProperty( name, PropertyValueWrapper::ExtractPropertyValue( isolate, javaScriptValue), Property::READ_WRITE ); } } diff --git a/plugins/dali-script-v8/src/object/property-value-wrapper.cpp b/plugins/dali-script-v8/src/object/property-value-wrapper.cpp index 8469d7f1e1..b1606423d9 100644 --- a/plugins/dali-script-v8/src/object/property-value-wrapper.cpp +++ b/plugins/dali-script-v8/src/object/property-value-wrapper.cpp @@ -785,6 +785,62 @@ Dali::Property::Value PropertyValueWrapper::ExtractPropertyValue( v8::Isolate* i return daliPropertyValue; } +Dali::Property::Value PropertyValueWrapper::ExtractPropertyValue( v8::Isolate* isolate, v8::Local< v8::Value> v8Value) +{ + v8::HandleScope handleScope( isolate); + + Dali::Property::Value daliPropertyValue; + + // Check if it's a javascript Array + Dali::Property::Value array = VectorOrMatrixFromV8Array( isolate, v8Value ); + + if( V8Utils::IsBooleanPrimitiveOrObject( v8Value ) ) + { + daliPropertyValue = Dali::Property::Value( V8Utils::GetBooleanValue( isolate, v8Value)); + } + else if( V8Utils::IsNumberPrimitiveOrObject( v8Value ) ) + { + daliPropertyValue = Dali::Property::Value( V8Utils::GetNumberValue( isolate, v8Value) ); + } + else if( v8Value->IsInt32() ) + { + daliPropertyValue = Dali::Property::Value( v8Value->Int32Value() ) ; + } + else if( V8Utils::IsStringPrimitiveOrObject( v8Value) ) + { + daliPropertyValue = Dali::Property::Value( V8Utils::GetStringValue( isolate, v8Value) ); + } + else if( array.GetType() == Dali::Property::VECTOR2 + || array.GetType() == Dali::Property::VECTOR3 + || array.GetType() == Dali::Property::VECTOR4 ) + { + daliPropertyValue = array; + } + else if( array.GetType() == Dali::Property::MATRIX ) + { + Dali::Matrix mat = array.Get(); + daliPropertyValue = mat; + } + else if( array.GetType() == Dali::Property::MATRIX3 ) + { + Dali::Matrix3 mat = array.Get(); + daliPropertyValue = mat; + } + else if( array.GetType() == Dali::Property::ARRAY ) + { + daliPropertyValue = ArrayFromV8Array( isolate, v8Value ); + } + else if( v8Value->IsObject() ) + { + // Assume this is a property map + v8::Local object = v8::Handle::Cast(v8Value); + Dali::Property::Map propertyMap = V8Utils::GetPropertyMapFromObject(isolate, object); + daliPropertyValue = Dali::Property::Value( propertyMap ); + } + + return daliPropertyValue; +} + void PropertyValueWrapper::NewRotation( const v8::FunctionCallbackInfo< v8::Value >& args) { v8::Isolate* isolate = args.GetIsolate(); diff --git a/plugins/dali-script-v8/src/object/property-value-wrapper.h b/plugins/dali-script-v8/src/object/property-value-wrapper.h index 8cb2dacd00..05a3e3853d 100644 --- a/plugins/dali-script-v8/src/object/property-value-wrapper.h +++ b/plugins/dali-script-v8/src/object/property-value-wrapper.h @@ -89,6 +89,12 @@ public: */ static Dali::Property::Value ExtractPropertyValue( v8::Isolate* isolate, v8::Local< v8::Value> v8Value, Dali::Property::Type type); + /** + * Extract a property value from a javascript object + * @return property value + */ + static Dali::Property::Value ExtractPropertyValue( v8::Isolate* isolate, v8::Local< v8::Value> v8Value); + /** * @brief Extract a vector or a matrix from a JavaScript array * @return property value ( vector or matrix) diff --git a/plugins/dali-script-v8/src/utils/v8-utils.cpp b/plugins/dali-script-v8/src/utils/v8-utils.cpp index b77d458497..193836df42 100644 --- a/plugins/dali-script-v8/src/utils/v8-utils.cpp +++ b/plugins/dali-script-v8/src/utils/v8-utils.cpp @@ -378,8 +378,6 @@ std::string PropertyNameToJavaScriptName(const std::string& hyphenatedName) return ret; } - - void ScriptError( const char* function, v8::Isolate* isolate, std::string errorString ) { v8::EscapableHandleScope scope( isolate); @@ -393,6 +391,12 @@ void ScriptError( const char* function, v8::Isolate* isolate, std::string errorS isolate->ThrowException( v8::String::NewFromUtf8( isolate, errorMsg.c_str()) ); } +void ScriptWarning( const char* function, std::string warningString ) +{ + std::string warningMsg = std::string(function) + std::string("(), ") + warningString; + DALI_LOG_WARNING("%s \n", warningMsg.c_str() ); +} + bool IsBooleanPrimitiveOrObject( const v8::Local& value ) { return ( value->IsBoolean() || value->IsBooleanObject()); @@ -504,6 +508,13 @@ Property::Value GetPropertyValueFromObject( bool& found, v8::Isolate* isolate, c v8::Local v = value->ToInt32(); return Dali::Property::Value(static_cast(v->Value())); } + else if( value->IsString() ) + { + found = true; + std::string valueString = V8Utils::v8StringToStdString( value ); + return Dali::Property::Value(valueString); + } + return daliPropertyValue; } diff --git a/plugins/dali-script-v8/src/utils/v8-utils.h b/plugins/dali-script-v8/src/utils/v8-utils.h index 5a86f39c62..63ff2c6289 100644 --- a/plugins/dali-script-v8/src/utils/v8-utils.h +++ b/plugins/dali-script-v8/src/utils/v8-utils.h @@ -50,6 +50,7 @@ enum }; #define DALI_SCRIPT_EXCEPTION( isolate, message ) V8Utils::ScriptError( __FUNCTION__ , isolate, message ); +#define DALI_SCRIPT_WARNING( message ) V8Utils::ScriptWarning( __FUNCTION__ , message ); namespace V8Utils { @@ -145,6 +146,11 @@ std::string GetJavaScriptFunctionName( const char* functionName ); */ void ScriptError( const char* function, v8::Isolate* isolate, std::string errorString ); +/** + * Script warning + */ +void ScriptWarning( const char* function, std::string warningString ); + /** * @return in the value is a boolean primitive or a boolean object */ -- 2.34.1