Fix JavaScript error after Rendering API clean-up
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / utils / v8-utils.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // HEADER
19 #include "v8-utils.h"
20
21 // EXTERNAL INCLUDES
22 #include <iostream>
23 #include <fstream>
24 #include <sstream>
25 #include <dali/integration-api/debug.h>
26
27 // INTERNAL INCLUDES
28 #include <object/property-value-wrapper.h>
29 #include <actors/actor-wrapper.h>
30 #include <object/handle-wrapper.h>
31 #include <image/image-wrapper.h>
32 #include <render-tasks/render-task-wrapper.h>
33 #include <object/property-value-wrapper.h>
34
35
36 /**
37  * Similar to DALI_LOG_ERROR except the PRETTY_FUNCTION
38  * is removed because it makes no sense for scripting errors.
39  */
40 #define DALI_LOG_SCRIPT_ERROR(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugError, format, ## args)
41
42 namespace Dali
43 {
44
45 namespace V8Plugin
46 {
47
48 namespace V8Utils
49 {
50
51 void Log(const v8::FunctionCallbackInfo< v8::Value >& args)
52 {
53   v8::HandleScope handleScope( args.GetIsolate());
54
55   bool first = true;
56   for (int i = 0; i < args.Length(); i++)
57   {
58     if (first)
59     {
60       first = false;
61     }
62     else
63     {
64       std::cout << " ";
65     }
66     v8::String::Utf8Value utf8_value( args[i] );
67     std::cout << *utf8_value << "\n";
68   }
69 }
70
71 void LogError(const v8::FunctionCallbackInfo< v8::Value >& args)
72 {
73   v8::HandleScope handleScope( args.GetIsolate());
74   std::string output;
75   bool first = true;
76   for (int i = 0; i < args.Length(); i++)
77   {
78     if (first)
79     {
80       first = false;
81     }
82     else
83     {
84       output +=" ";
85     }
86     v8::String::Utf8Value utf8_value( args[i] );
87     output += *utf8_value;
88     output +="\n";
89   }
90   DALI_LOG_ERROR_NOFN( "JavaScript: %s",output.c_str() );
91 }
92
93 void GetFileContents(const std::string &fileName, std::string& contents)
94 {
95    std::ifstream t(fileName.c_str());
96    contents = std::string((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
97 };
98
99 void GetFileDirectory( const std::string& fileName, std::string& directory )
100 {
101   directory = "";
102
103   // get the position of the last slash
104   size_t pos = fileName.find_last_of("\\/");
105
106   // if it doesn't exist, return nothing
107   if( (std::string::npos == pos ) )
108   {
109     return;
110   }
111   else
112   {
113     // check an edge case where the string ends in a forward slash "mydir/"
114     if( (pos+1) < fileName.length() )
115     {
116       directory = fileName.substr(0, pos+1);
117       return;
118     }
119   }
120 }
121
122 void GetFileName( const std::string& fullPathName, std::string& fileName )
123 {
124   // look for last slash
125   size_t pos = fullPathName.find_last_of("\\/");
126
127   if( std::string::npos == pos )
128   {
129     fileName = fullPathName;
130   }
131   else
132   {
133     fileName = fullPathName.substr(pos,fileName.length());
134   }
135 }
136
137 void GetModuleName( const std::string& fileName, std::string& moduleName )
138 {
139   std::string fileNameNoPath;
140    GetFileName( fileName , fileNameNoPath );
141   size_t pos = fileNameNoPath.find_last_of(".");
142   if( std::string::npos == pos )
143   {
144     moduleName = fileNameNoPath;
145   }
146   else
147   {
148     moduleName = fileName.substr(0, pos );
149   }
150 }
151
152 bool IsPropertyMapIdentical(Property::Map map1, Property::Map map2)
153 {
154   bool dirty = false;
155
156   // Compare number of properties
157   if ( map1.Count() != map2.Count() )
158   {
159     dirty = true;
160   }
161   else
162   {
163     for ( unsigned int i = 0, count = map1.Count(); i < count; ++i )
164     {
165       // Compare the key first
166       if(map1.GetKey(i) != map2.GetKey(i))
167       {
168         dirty = true;
169       }
170       else
171       {
172         Property::Value& value = map1.GetValue(i);
173         Property::Value& newValue = map2.GetValue(i);
174
175         // Compare the value type
176         if(value.GetType() != newValue.GetType())
177         {
178           dirty = true;
179         }
180         else
181         {
182           // Compare the value
183           switch( value.GetType() )
184           {
185             case Property::BOOLEAN:
186             {
187               dirty = ( value.Get<bool>() != newValue.Get<bool>() );
188               break;
189             }
190             case Property::FLOAT:
191             {
192               dirty = ( value.Get<float>() != newValue.Get<float>() );
193               break;
194             }
195             case Property::INTEGER:
196             {
197               dirty = ( value.Get<int>() != newValue.Get<int>() );
198               break;
199             }
200             case Property::RECTANGLE:
201             {
202               dirty = ( value.Get< Rect<int> >() != newValue.Get< Rect<int> >() );
203               break;
204             }
205             case Property::VECTOR2:
206             {
207               dirty = ( value.Get<Vector2>() != newValue.Get<Vector2>() );
208               break;
209             }
210             case Property::VECTOR3:
211             {
212               dirty = ( value.Get<Vector3>() != newValue.Get<Vector3>() );
213               break;
214             }
215             case Property::VECTOR4:
216             {
217               dirty = ( value.Get<Vector4>() != newValue.Get<Vector4>() );
218               break;
219             }
220             case Property::MATRIX3:
221             {
222               dirty = ( value.Get<Matrix3>() != newValue.Get<Matrix3>() );
223               break;
224             }
225             case Property::MATRIX:
226             {
227               dirty = ( value.Get<Matrix>() != newValue.Get<Matrix>() );
228               break;
229             }
230             case Property::ROTATION:
231             {
232               dirty = ( value.Get<Quaternion>() != newValue.Get<Quaternion>() );
233               break;
234             }
235             case Property::STRING:
236             {
237               dirty = ( value.Get<std::string>() != newValue.Get<std::string>() );
238               break;
239             }
240             case Property::MAP:
241             {
242               dirty = ( !IsPropertyMapIdentical( value.Get<Property::Map>(), newValue.Get<Property::Map>() ) );
243               break;
244             }
245             default:
246             {
247               break;
248             }
249           }
250         }
251       }
252
253       if(dirty)
254       {
255         // Different already, no need any further comparison
256         break;
257       }
258     }
259   }
260
261   return !dirty;
262 }
263
264 void ReportException(  v8::Isolate* isolate, v8::TryCatch* tryCatch)
265 {
266   v8::HandleScope handleScope( isolate );
267
268   v8::String::Utf8Value exception(tryCatch->Exception());
269   v8::Handle<v8::Message> message   = tryCatch->Message();
270
271   if (message.IsEmpty())
272   {
273     // V8 didn't provide any extra information about this error; just
274     // print the exception.
275     DALI_LOG_SCRIPT_ERROR("%s\n", *exception);
276   }
277   else
278   {
279
280     // Print (filename):(line number): (message).
281     v8::String::Utf8Value filename(message->GetScriptResourceName());
282
283     DALI_LOG_SCRIPT_ERROR("\n\n====== Error found in JavaScript: ========= \n");
284
285
286     int linenum = message->GetLineNumber();
287     DALI_LOG_SCRIPT_ERROR("File: %s\n", *filename, linenum, *exception);
288
289      DALI_LOG_SCRIPT_ERROR("Error: :%s\n", *exception );
290      DALI_LOG_SCRIPT_ERROR("Line: :%i\n",  linenum );
291
292     // Print line of source code.
293     v8::String::Utf8Value sourceline(message->GetSourceLine());
294
295     DALI_LOG_SCRIPT_ERROR("Source: %s\n", *sourceline);
296
297     // Print wavy underline (GetUnderline is deprecated).
298
299     std::stringstream msg;
300
301     int start = message->GetStartColumn();
302     for (int i = 0; i < start; i++)
303     {
304       msg << " ";
305     }
306     int end = message->GetEndColumn();
307     for (int i = start; i < end; i++)
308     {
309       msg << "↑";
310     }
311
312     DALI_LOG_SCRIPT_ERROR("        %s\n", msg.str().c_str());
313
314     v8::String::Utf8Value stack_trace(tryCatch->StackTrace());
315     if (stack_trace.length() > 0)
316     {
317       DALI_LOG_SCRIPT_ERROR("%s\n", *stack_trace);
318     }
319     DALI_LOG_SCRIPT_ERROR("\n=========================================== \n");
320
321   }
322 }
323
324 std::string GetJavaScriptFunctionName( const char* functionName  )
325 {
326   // @todo if we are 100% decided on lower case, go through
327   // every api and manually change the function names to lower case first character
328   std::string name( functionName );
329   name[0]=tolower( functionName[0] );
330   return name;
331 }
332
333 void Version(const v8::FunctionCallbackInfo< v8::Value >& args)
334 {
335   v8::HandleScope handleScope( args.GetIsolate());
336
337   v8::Handle<v8::String>  ver = v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion());
338
339   args.GetReturnValue().Set(ver);
340 }
341
342
343 std::string v8StringToStdString( const v8::Handle<v8::Value>& value )
344 {
345   v8::String::Utf8Value utf8(value);
346   return std::string(*utf8);
347 }
348
349
350 std::string PropertyNameToJavaScriptName(const std::string& hyphenatedName)
351 {
352   std::string ret;
353
354   ret.reserve(hyphenatedName.size());
355
356   bool capitlizeNext = false ;
357   for(unsigned int i = 0; i < hyphenatedName.size(); ++i)
358   {
359     char c = hyphenatedName[i];
360     if(c == '-')
361     {
362       capitlizeNext = true;
363     }
364     else
365     {
366       if(capitlizeNext)
367       {
368         ret.push_back(std::toupper(c));
369         capitlizeNext = false;
370       }
371       else
372       {
373         ret.push_back(c);
374       }
375     }
376   }
377
378   return ret;
379 }
380
381
382
383 void ScriptError( const char* function, v8::Isolate* isolate, std::string errorString )
384 {
385   v8::EscapableHandleScope scope( isolate);
386   std::string errorMsg = std::string(function) + std::string("(), ") + errorString;
387
388   // log out to DALI_LOG_ERROR first, so we know something has gone wrong
389   DALI_LOG_ERROR("%s \n", errorMsg.c_str() );
390
391   // throw a V8 exception, DALi will keep running but we will get a print out
392   // of where the error occured in the JavaScript source
393   isolate->ThrowException( v8::String::NewFromUtf8( isolate, errorMsg.c_str()) );
394 }
395
396 bool IsBooleanPrimitiveOrObject( const v8::Local<v8::Value>& value )
397 {
398   return ( value->IsBoolean() || value->IsBooleanObject());
399 }
400
401 bool GetBooleanValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value )
402 {
403   v8::EscapableHandleScope scope( isolate); // may not be required.
404
405   if( value->IsBoolean() )
406   {
407     return value->ToBoolean()->Value();
408   }
409   else if (value->IsBooleanObject() )
410   {
411     const v8::Local<v8::BooleanObject> object = v8::Local<v8::BooleanObject>::Cast(value);
412     return object->BooleanValue();
413   }
414   DALI_SCRIPT_EXCEPTION(isolate, "no bool found");
415   return false;
416 }
417
418 bool IsNumberPrimitiveOrObject( const v8::Local<v8::Value>& value )
419 {
420   return ( value->IsNumber() || value->IsNumberObject());
421 }
422
423 float GetNumberValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value )
424 {
425   v8::EscapableHandleScope scope( isolate); // may not be required.
426
427   if( value->IsNumber() )
428   {
429     return value->ToNumber()->Value();
430   }
431   else if (value->IsNumberObject() )
432   {
433     const v8::Local<v8::NumberObject> object = v8::Local<v8::NumberObject>::Cast(value);
434     return object->ValueOf();
435   }
436
437   DALI_SCRIPT_EXCEPTION(isolate, "no number found?");
438   return 0.f;
439 }
440
441 bool IsStringPrimitiveOrObject( const v8::Local<v8::Value>& value )
442 {
443   return ( value->IsString() || value->IsStringObject());
444 }
445
446 std::string GetStringValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value )
447 {
448   v8::EscapableHandleScope scope( isolate); // may not be required.
449
450   if( value->IsString() )
451   {
452     return V8Utils::v8StringToStdString(value);
453   }
454   else if (value->IsStringObject() )
455   {
456     const v8::Local<v8::StringObject> object = v8::Local<v8::StringObject>::Cast(value);
457     return V8Utils::v8StringToStdString( object->ValueOf() );
458   }
459
460   DALI_SCRIPT_EXCEPTION(isolate, "no string found?");
461   return "";
462 }
463
464
465 Property::Value GetPropertyValueFromObject( bool& found, v8::Isolate* isolate, const   v8::Local<v8::Value >& value  )
466 {
467   v8::HandleScope handleScope( isolate);
468
469   Property::Value  daliPropertyValue;// creates a property with Property::NONE
470
471   found = false;
472
473   if( value->IsObject() )
474   {
475     v8::Local<v8::Object> object = v8::Handle<v8::Object>::Cast( value );
476
477     if( BaseWrappedObject::IsWrappedTypeAPropertyValue( object ) )
478     {
479       found = true;
480       PropertyValueWrapper* propertyWrapper = PropertyValueWrapper::Unwrap( isolate, object );
481       return propertyWrapper->GetValue();
482     }
483     else if( value->IsArray() )
484     {
485       found = true;
486       return PropertyValueWrapper::VectorOrMatrixFromV8Array( isolate, object);//todo check for V8 array / map?
487     }
488   }
489   else if( value->IsBoolean() )
490   {
491     found = true;
492     v8::Local<v8::Boolean> v = value->ToBoolean();
493     return Dali::Property::Value(v->Value());
494   }
495   else if( value->IsNumber() )
496   {
497     found = true;
498     v8::Local<v8::Number> v = value->ToNumber();
499     return Dali::Property::Value(static_cast<float>(v->Value()));
500   }
501   else if( value->IsInt32() || value->IsUint32() )
502   {
503     found = true;
504     v8::Local<v8::Int32> v = value->ToInt32();
505     return Dali::Property::Value(static_cast<int>(v->Value()));
506   }
507   return daliPropertyValue;
508
509 }
510
511 Property::Map GetPropertyMapFromObject( v8::Isolate* isolate, const v8::Local<v8::Object>& object)
512 {
513   v8::Local<v8::Array> properties = object->GetPropertyNames();
514   Property::Map propertyMap;    // empty map
515
516   for(  unsigned int i = 0; i <  properties->Length(); ++i)
517   {
518     // Get the key
519     v8::Local<v8::Value> key = properties->Get( i );
520     std::string keyString = v8StringToStdString( key );
521
522     // Get the value
523     v8::Local<v8::Value> value = object->Get( key );
524
525     if( value->IsBoolean() )
526     {
527       v8::Local<v8::Boolean> v = value->ToBoolean();
528       propertyMap[ keyString ] = v->Value();
529     }
530     else if( value->IsNumber() )
531     {
532       v8::Local<v8::Number> v = value->ToNumber();
533       propertyMap[ keyString ] = static_cast<float>(v->Value());
534     }
535     else if( value->IsInt32() || value->IsUint32() )
536     {
537       v8::Local<v8::Int32> v = value->ToInt32();
538       propertyMap[ keyString ] = static_cast<int>(v->Value());
539     }
540     else if( value->IsString() )
541     {
542       std::string valueString = V8Utils::v8StringToStdString( value );
543       propertyMap[ keyString ] = valueString.c_str();
544     }
545     else if( value->IsArray() )
546     {
547       propertyMap[ keyString ] = PropertyValueWrapper::VectorOrMatrixFromV8Array( isolate, value);
548     }
549     else if( value->IsObject() )
550     {
551       Dali::Property::Map map = V8Utils::GetPropertyMapFromObject(isolate, value->ToObject());
552       if( !map.Empty() )
553       {
554         propertyMap[ keyString ] = Dali::Property::Value( map );
555       }
556     }
557   }
558
559   return propertyMap;
560 }
561
562 Actor GetActorFromObject( v8::Isolate* isolate, bool& found, v8::Local<v8::Object>& object)
563 {
564   v8::HandleScope handleScope( isolate);
565   found = false;
566
567   if( BaseWrappedObject::IsWrappedType ( isolate, object, BaseWrappedObject::ACTOR ))
568   {
569     HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, object );
570     return Actor::DownCast( handleWrapper->mHandle );
571   }
572   return Actor();
573 }
574
575
576 int GetIntegerParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args, int defaultValue  )
577 {
578   found = false;
579   unsigned int length = args.Length();
580   if( index >= length )
581   {
582     return defaultValue;
583   }
584   if( args[ index ]->IsInt32() )
585   {
586     found = true;
587     return args[ index ]->Int32Value();
588   }
589   else
590   {
591     return defaultValue;
592   }
593 }
594
595 float GetFloatParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args, float defaultValue  )
596 {
597   found = false;
598   unsigned int length = args.Length();
599   if( index >= length )
600   {
601     return defaultValue;
602   }
603   if( args[ index ]->IsNumber() )
604   {
605     found = true;
606     return args[ index ]->NumberValue();
607   }
608   else
609   {
610     return defaultValue;
611   }
612 }
613
614 std::string GetStringParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
615 {
616   found = false;
617   unsigned int length = args.Length();
618
619   if( index >= length )
620   {
621     return std::string();
622   }
623   if( args[ index ]->IsString() )
624   {
625     found = true;
626     return v8StringToStdString( args[ index ]);
627   }
628   else
629   {
630     return std::string();
631   }
632 }
633
634 bool GetBooleanParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
635 {
636   v8::HandleScope handleScope( isolate);
637
638   found = false;
639   unsigned int length = args.Length();
640   if( index >= length )
641   {
642     return false;
643   }
644   if( args[ index ]->IsBoolean() )
645   {
646     found = true;
647     v8::Local<v8::Boolean> v = args[ index ]->ToBoolean();
648     return v->Value();
649   }
650   else
651   {
652     return false;
653   }
654 }
655
656 void* GetArrayBufferViewParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args  )
657 {
658   found = false;
659   unsigned int length = args.Length();
660   if( index < length && args[index]->IsArrayBufferView() )
661   {
662     found = true;
663     v8::ArrayBufferView* bufferView = v8::ArrayBufferView::Cast(*(args[index]));
664     v8::Handle<v8::ArrayBuffer> buffer = bufferView->Buffer();
665     v8::ArrayBuffer::Contents contents = buffer->Externalize();
666     return contents.Data();
667   }
668   else
669   {
670     return NULL;
671   }
672 }
673
674 Handle GetHandleParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
675 {
676   v8::HandleScope handleScope( isolate);
677
678   found = false;
679   unsigned int length = args.Length();
680   if( index >= length )
681   {
682     return Handle();
683   }
684
685   if( args[ index ]->IsObject() )
686   {
687     v8::Local<v8::Object> object = args[ index ]->ToObject();
688     v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
689     void* ptr = field->Value();
690     if( ptr )
691     {
692         found = true;
693         HandleWrapper* wrapper = static_cast< HandleWrapper *>(ptr);
694         return wrapper->GetHandle();
695     }
696   }
697   return Handle();
698 }
699
700 Vector2 GetVector2Parameter( unsigned int index,  bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
701 {
702   v8::HandleScope handleScope( isolate);
703   unsigned int length =  args.Length();
704   Vector2 ret;
705   found = false;
706
707   if( index < length )
708   {
709     if( args[ index ]->IsObject() )
710     {
711       Dali::Property::Value value;
712       value = PropertyValueWrapper::ExtractPropertyValue( isolate, args[index], Dali::Property::VECTOR2 );
713       if( value.GetType() == Dali::Property::VECTOR2)
714       {
715         found = true;
716         value.Get(ret);
717       }
718       else
719       {
720         DALI_SCRIPT_EXCEPTION(isolate, "Missing Vector2 parameter");
721       }
722     }
723   }
724   else
725   {
726     DALI_SCRIPT_EXCEPTION(isolate, "Missing Vector2 parameter");
727   }
728
729   return ret;
730 }
731
732 Vector2 GetVector2ParameterFrom2Float( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
733 {
734   Vector2 ret(0.0f,0.0f);
735   bool bFound(false);
736   unsigned int argCount( args.Length() );
737
738   if( index+2 >= argCount )
739   {
740     DALI_SCRIPT_EXCEPTION(isolate, "Missing parameter");
741   }
742
743   found = true;
744   ret.x = V8Utils::GetFloatParameter( index, bFound, isolate, args, 0.0f );
745   found = found && bFound;
746   ret.y = V8Utils::GetFloatParameter( index+1, bFound, isolate, args, 0.0f );
747   found = found && bFound;
748
749   return ret;
750 }
751
752 Vector3 GetVector3Parameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args)
753 {
754   v8::HandleScope handleScope( isolate);
755   unsigned int argCount( args.Length() );
756   Vector3 ret;
757   found = false;
758   if( index < argCount )
759   {
760     if( args[ index ]->IsObject() )
761     {
762       Dali::Property::Value value;
763       value = PropertyValueWrapper::ExtractPropertyValue( isolate, args[index], Dali::Property::VECTOR3 );
764       if( value.GetType() == Dali::Property::VECTOR3)
765       {
766         found = true;
767         value.Get(ret);
768       }
769       else
770       {
771         DALI_SCRIPT_EXCEPTION(isolate, "Missing Vector3 parameter");
772       }
773     }
774   }
775   else
776   {
777     DALI_SCRIPT_EXCEPTION(isolate, "Missing Vector3 parameter");
778
779   }
780
781   return ret;
782 }
783
784 Vector4 GetVector4Parameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args)
785 {
786   v8::HandleScope handleScope( isolate);
787   unsigned int argCount( args.Length() );
788   Vector4 ret;
789   found = false;
790
791   if( index < argCount )
792   {
793     if( args[ index ]->IsObject() )
794     {
795       Dali::Property::Value value;
796       value = PropertyValueWrapper::ExtractPropertyValue( isolate, args[index], Dali::Property::VECTOR4 );
797       if( value.GetType() == Dali::Property::VECTOR4)
798       {
799         found = true;
800         value.Get(ret);
801       }
802       else
803       {
804         DALI_SCRIPT_EXCEPTION(isolate, "Missing Vector4 parameter");
805       }
806     }
807   }
808   else
809   {
810     DALI_SCRIPT_EXCEPTION(isolate, "Missing Vector4 parameter");
811   }
812
813   return ret;
814 }
815
816
817 Rect<int> GetRectIntParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
818 {
819   v8::HandleScope handleScope( isolate);
820
821    found = false;
822    int length = args.Length() - index;
823
824    // if it's an array read the 2 numbers into a vector2
825    if( length == 4 )
826    {
827      if( args[ 0 + index ]->IsInt32() &&
828          args[ 1 + index ]->IsInt32() &&
829          args[ 2 + index ]->IsInt32() &&
830          args[ 3 + index ]->IsInt32() )
831      {
832        found = true;
833        Rect<int> rect( args[ 0 + index ]->Int32Value(),
834                        args[ 1 + index ]->Int32Value(),
835                        args[ 2 + index ]->Int32Value(),
836                        args[ 3 + index ]->Int32Value() );
837        return rect;
838      }
839    }
840    // this will extract a Vector4, if it is a Vector4 or a Javascript array object
841    if( args[ index ]->IsObject() )
842    {
843      Dali::Property::Value value;
844      value = PropertyValueWrapper::ExtractPropertyValue( isolate, args[index], Dali::Property::RECTANGLE );
845      if( value.GetType() == Dali::Property::RECTANGLE)
846      {
847        found = true;
848        Rect<int> rect;
849        value.Get(rect);
850        return rect;
851      }
852
853      // @todo support vector4 as well?
854    }
855    return Rect<int>();
856 }
857
858 Actor GetActorParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
859 {
860   BaseWrappedObject* wrapper = GetWrappedDaliObjectParameter( index, BaseWrappedObject::ACTOR, isolate, args);
861   ActorWrapper* actorWrapper = static_cast< ActorWrapper*>( wrapper );
862   if( actorWrapper )
863   {
864     found = true;
865     return actorWrapper->GetActor();
866   }
867   else
868   {
869     return Actor();
870   }
871 }
872
873 Layer GetLayerParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
874 {
875   Actor actor = GetActorParameter( index, found, isolate, args );
876   return Layer::DownCast( actor );
877 }
878
879 Image GetImageParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
880 {
881   BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( index, BaseWrappedObject::IMAGE, isolate, args );
882   if( wrappedObject )
883   {
884     found = true;
885     ImageWrapper* wrapper = static_cast< ImageWrapper *>(wrappedObject);
886     return wrapper->GetImage();
887   }
888   else
889   {
890     return Image();
891   }
892
893 }
894
895 RenderTask GetRenderTaskParameter( unsigned int paramIndex, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
896 {
897   found = false;
898   BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::RENDER_TASK, isolate, args );
899   if( wrappedObject )
900   {
901     found = true;
902     RenderTaskWrapper* wrapper = static_cast< RenderTaskWrapper *>(wrappedObject);
903     return wrapper->GetRenderTask();
904   }
905   else
906   {
907     return RenderTask(); // empty handle
908   }
909 }
910
911 BaseWrappedObject* GetWrappedDaliObjectParameter( unsigned int index,  BaseWrappedObject::Type type,  v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
912 {
913   v8::HandleScope handleScope( isolate);
914   unsigned int length = args.Length();
915
916   if( index >= length )
917   {
918     return NULL;
919   }
920
921   if( !args[ index ]->IsObject() )
922   {
923     return NULL;
924   }
925
926   v8::Local<v8::Object> object = args[ index ]->ToObject();
927
928   if( BaseWrappedObject::IsWrappedType ( isolate, object, type ))
929   {
930     v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
931     void* ptr = field->Value();
932     BaseWrappedObject* wrapper = static_cast< BaseWrappedObject *>(ptr);
933     return wrapper;
934   }
935   return NULL;
936 }
937
938
939 Property::Value GetPropertyValueParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
940 {
941   v8::HandleScope handleScope( isolate);
942
943   Property::Value  daliPropertyValue;// creates a property with Property::INVALID
944
945   found = false;
946   unsigned int length = args.Length();
947
948   if( index >= length )
949   {
950     return daliPropertyValue;
951   }
952   v8::Local<v8::Value > value = args[ index ];
953
954   return GetPropertyValueFromObject( found, isolate, value);
955 }
956
957 Property::Map GetPropertyMapParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
958 {
959   v8::HandleScope handleScope( isolate);
960
961   Property::Map propertyMap;    // empty map
962
963   found = false;
964   unsigned int length = args.Length();
965
966   if( index >= length )
967   {
968     return propertyMap;
969   }
970
971   if( !args[ index ]->IsObject() )
972   {
973     return propertyMap;
974   }
975   found = true;
976
977   // go through each key value pair
978   v8::Local<v8::Object> obj = args[ index ]->ToObject();
979
980   return GetPropertyMapFromObject( isolate, obj );
981
982 }
983
984 void CreatePropertyMap( v8::Isolate* isolate, const Property::Map& map, v8::Local<v8::Object>& object )
985 {
986   v8::HandleScope handleScope( isolate);
987
988   // we're converting a dali property map in to a JavaScript property map
989   if( map.Count() == 0 )
990   {
991     return;
992   }
993
994   for( unsigned int index = 0; index < map.Count(); ++index )
995   {
996     const std::string& key = map.GetKey( index );
997     Property::Value& value = map.GetValue( index );
998     v8::Local<v8::Value> v8Value;
999
1000     switch( value.GetType() )
1001     {
1002       case Dali::Property::FLOAT:
1003       {
1004         v8Value = v8::Number::New( isolate, value.Get<float>()  );
1005         break;
1006       }
1007       case Dali::Property::BOOLEAN:
1008       {
1009         v8Value = v8::Boolean::New(  isolate, value.Get<bool>());
1010         break;
1011       }
1012       case Dali::Property::INTEGER:
1013       {
1014         v8Value = v8::Integer::New( isolate, value.Get<int>());
1015         break;
1016       }
1017       case Dali::Property::STRING:
1018       {
1019         std::string string = value.Get< std::string >();
1020         v8Value = v8::String::NewFromUtf8( isolate,  string.c_str());
1021         break;
1022       }
1023       case Dali::Property::VECTOR2:
1024       {
1025         // create a vector2
1026         Vector2 vec = value.Get<Vector2>();
1027         v8::Local<v8::Array> array= v8::Array::New( isolate, 2 );
1028         array->Set( 0 , v8::Number::New(isolate, vec.x));
1029         array->Set( 1 , v8::Number::New(isolate, vec.y));
1030         v8Value = array;
1031         break;
1032       }
1033       case Dali::Property::VECTOR3:
1034       {
1035         // create a vector 3
1036         Vector3 vec = value.Get<Vector3>();
1037         v8::Local<v8::Array> array= v8::Array::New( isolate, 3 );
1038         array->Set( 0 , v8::Number::New(isolate, vec.x));
1039         array->Set( 1 , v8::Number::New(isolate, vec.y));
1040         array->Set( 2 , v8::Number::New(isolate, vec.z));
1041         v8Value = array;
1042         break;
1043       }
1044       case Dali::Property::VECTOR4:
1045       {
1046         // create a vector 4
1047         Vector4 vec = value.Get<Vector4>();
1048         v8::Local<v8::Array> array= v8::Array::New( isolate, 4 );
1049         array->Set( 0 , v8::Number::New(isolate, vec.x));
1050         array->Set( 1 , v8::Number::New(isolate, vec.y));
1051         array->Set( 2 , v8::Number::New(isolate, vec.z));
1052         array->Set( 3 , v8::Number::New(isolate, vec.w));
1053         v8Value = array;
1054         break;
1055       }
1056
1057       default:
1058       {
1059         DALI_SCRIPT_EXCEPTION( isolate, "Primitive mismatch \n");
1060         return;
1061       }
1062     }
1063     object->Set( v8::String::NewFromUtf8( isolate, key.c_str() ), v8Value );
1064   }
1065 }
1066
1067 void ReadFloatArguments( bool& foundAllArguments, float* data, unsigned int dataSize, const v8::FunctionCallbackInfo< v8::Value >& args, float defaultValue )
1068 {
1069   foundAllArguments = true;
1070   unsigned int length = args.Length();
1071
1072   if( length < dataSize )
1073   {
1074     foundAllArguments = false;
1075   }
1076
1077   for( unsigned int i = 0; i< dataSize ;i++ )
1078   {
1079     if( i < length )
1080     {
1081       if( args[ i ]->IsNumber()  )
1082       {
1083         data[i] = args[i]->NumberValue();
1084       }
1085       else
1086       {
1087         data[i] = defaultValue;
1088         foundAllArguments = false;   // bad argument
1089       }
1090     }
1091     else
1092     {
1093       data[i] = defaultValue; // not enough arguments
1094     }
1095   }
1096
1097 }
1098
1099 void ReadIntegerArguments( bool& foundAllArguments, int* data, int dataSize, const v8::FunctionCallbackInfo< v8::Value >& args, int defaultValue )
1100 {
1101   foundAllArguments = true;
1102   int length = args.Length();
1103   if( length < dataSize )
1104   {
1105     foundAllArguments = false;
1106   }
1107
1108   for( int i = 0; i< dataSize ;i++ )
1109   {
1110     if( i < length )
1111     {
1112       if( args[ i ]->IsInt32()  )
1113       {
1114         data[i] = args[i]->Int32Value();
1115       }
1116       else
1117       {
1118         data[i] = defaultValue;
1119         foundAllArguments = false;   // bad argument
1120       }
1121     }
1122     else
1123     {
1124       data[i] = defaultValue; // not enough arguments
1125     }
1126   }
1127
1128 }
1129 } // namespace V8Utils
1130
1131 } // namespace V8Plugin
1132
1133 } // namespace Dali