Allow automatic registration of custom property in JavaScript 07/59107/4
authorRichard Huang <r.huang@samsung.com>
Wed, 10 Feb 2016 15:22:00 +0000 (15:22 +0000)
committerRichard Huang <r.huang@samsung.com>
Thu, 19 May 2016 14:14:12 +0000 (07:14 -0700)
Change-Id: I0e857fea58f85fd55a2e0b729bc01a3b67549d37

plugins/dali-script-v8/src/object/handle-wrapper.cpp
plugins/dali-script-v8/src/object/property-value-wrapper.cpp
plugins/dali-script-v8/src/object/property-value-wrapper.h
plugins/dali-script-v8/src/utils/v8-utils.cpp
plugins/dali-script-v8/src/utils/v8-utils.h

index be1739d..fed77ce 100644 (file)
@@ -179,8 +179,14 @@ void HandleWrapper::PropertySet( v8::Local<v8::String> 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 );
   }
 }
 
index 8469d7f..b160642 100644 (file)
@@ -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<Dali::Matrix>();
+    daliPropertyValue = mat;
+  }
+  else if( array.GetType() == Dali::Property::MATRIX3 )
+  {
+    Dali::Matrix3 mat = array.Get<Dali::Matrix3>();
+    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<v8::Object> object = v8::Handle<v8::Object>::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();
index 8cb2dac..05a3e38 100644 (file)
@@ -90,6 +90,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)
    */
index b77d458..193836d 100644 (file)
@@ -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<v8::Value>& value )
 {
   return ( value->IsBoolean() || value->IsBooleanObject());
@@ -504,6 +508,13 @@ Property::Value GetPropertyValueFromObject( bool& found, v8::Isolate* isolate, c
     v8::Local<v8::Int32> v = value->ToInt32();
     return Dali::Property::Value(static_cast<int>(v->Value()));
   }
+  else if( value->IsString() )
+  {
+    found = true;
+    std::string valueString = V8Utils::v8StringToStdString( value );
+    return Dali::Property::Value(valueString);
+  }
+
   return daliPropertyValue;
 
 }
index 5a86f39..63ff2c6 100644 (file)
@@ -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
 {
@@ -146,6 +147,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
  */
 bool IsBooleanPrimitiveOrObject( const v8::Local<v8::Value>& value );