Merge "Changed all property & signal names to lowerCamelCase" into devel/master
[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 void ReportException(  v8::Isolate* isolate, v8::TryCatch* tryCatch)
153 {
154   v8::HandleScope handleScope( isolate );
155
156   v8::String::Utf8Value exception(tryCatch->Exception());
157   v8::Handle<v8::Message> message   = tryCatch->Message();
158
159   if (message.IsEmpty())
160   {
161     // V8 didn't provide any extra information about this error; just
162     // print the exception.
163     DALI_LOG_SCRIPT_ERROR("%s\n", *exception);
164   }
165   else
166   {
167
168     // Print (filename):(line number): (message).
169     v8::String::Utf8Value filename(message->GetScriptResourceName());
170
171     DALI_LOG_SCRIPT_ERROR("\n\n====== Error found in JavaScript: ========= \n");
172
173
174     int linenum = message->GetLineNumber();
175     DALI_LOG_SCRIPT_ERROR("File: %s\n", *filename, linenum, *exception);
176
177      DALI_LOG_SCRIPT_ERROR("Error: :%s\n", *exception );
178      DALI_LOG_SCRIPT_ERROR("Line: :%i\n",  linenum );
179
180     // Print line of source code.
181     v8::String::Utf8Value sourceline(message->GetSourceLine());
182
183     DALI_LOG_SCRIPT_ERROR("Source: %s\n", *sourceline);
184
185     // Print wavy underline (GetUnderline is deprecated).
186
187     std::stringstream msg;
188
189     int start = message->GetStartColumn();
190     for (int i = 0; i < start; i++)
191     {
192       msg << " ";
193     }
194     int end = message->GetEndColumn();
195     for (int i = start; i < end; i++)
196     {
197       msg << "↑";
198     }
199
200     DALI_LOG_SCRIPT_ERROR("        %s\n", msg.str().c_str());
201
202     v8::String::Utf8Value stack_trace(tryCatch->StackTrace());
203     if (stack_trace.length() > 0)
204     {
205       DALI_LOG_SCRIPT_ERROR("%s\n", *stack_trace);
206     }
207     DALI_LOG_SCRIPT_ERROR("\n=========================================== \n");
208
209   }
210 }
211
212 std::string GetJavaScriptFunctionName( const char* functionName  )
213 {
214   // @todo if we are 100% decided on lower case, go through
215   // every api and manually change the function names to lower case first character
216   std::string name( functionName );
217   name[0]=tolower( functionName[0] );
218   return name;
219 }
220
221 void Version(const v8::FunctionCallbackInfo< v8::Value >& args)
222 {
223   v8::HandleScope handleScope( args.GetIsolate());
224
225   v8::Handle<v8::String>  ver = v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion());
226
227   args.GetReturnValue().Set(ver);
228 }
229
230
231 std::string v8StringToStdString( const v8::Handle<v8::Value>& value )
232 {
233   v8::String::Utf8Value utf8(value);
234   return std::string(*utf8);
235 }
236
237
238 std::string PropertyNameToJavaScriptName(const std::string& hyphenatedName)
239 {
240   std::string ret;
241
242   ret.reserve(hyphenatedName.size());
243
244   bool capitlizeNext = false ;
245   for(unsigned int i = 0; i < hyphenatedName.size(); ++i)
246   {
247     char c = hyphenatedName[i];
248     if(c == '-')
249     {
250       capitlizeNext = true;
251     }
252     else
253     {
254       if(capitlizeNext)
255       {
256         ret.push_back(std::toupper(c));
257         capitlizeNext = false;
258       }
259       else
260       {
261         ret.push_back(c);
262       }
263     }
264   }
265
266   return ret;
267 }
268
269
270
271 void ScriptError( const char* function, v8::Isolate* isolate, std::string errorString )
272 {
273   v8::EscapableHandleScope scope( isolate);
274   std::string errorMsg = std::string(function) + std::string("(), ") + errorString;
275
276   // log out to DALI_LOG_ERROR first, so we know something has gone wrong
277   DALI_LOG_ERROR("%s \n", errorMsg.c_str() );
278
279   // throw a V8 exception, DALi will keep running but we will get a print out
280   // of where the error occured in the JavaScript source
281   isolate->ThrowException( v8::String::NewFromUtf8( isolate, errorMsg.c_str()) );
282 }
283
284 bool IsBooleanPrimitiveOrObject( const v8::Local<v8::Value>& value )
285 {
286   return ( value->IsBoolean() || value->IsBooleanObject());
287 }
288
289 bool GetBooleanValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value )
290 {
291   v8::EscapableHandleScope scope( isolate); // may not be required.
292
293   if( value->IsBoolean() )
294   {
295     return value->ToBoolean()->Value();
296   }
297   else if (value->IsBooleanObject() )
298   {
299     const v8::Local<v8::BooleanObject> object = v8::Local<v8::BooleanObject>::Cast(value);
300     return object->BooleanValue();
301   }
302   DALI_SCRIPT_EXCEPTION(isolate, "no bool found");
303   return false;
304 }
305
306 bool IsNumberPrimitiveOrObject( const v8::Local<v8::Value>& value )
307 {
308   return ( value->IsNumber() || value->IsNumberObject());
309 }
310
311 float GetNumberValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value )
312 {
313   v8::EscapableHandleScope scope( isolate); // may not be required.
314
315   if( value->IsNumber() )
316   {
317     return value->ToNumber()->Value();
318   }
319   else if (value->IsNumberObject() )
320   {
321     const v8::Local<v8::NumberObject> object = v8::Local<v8::NumberObject>::Cast(value);
322     return object->ValueOf();
323   }
324
325   DALI_SCRIPT_EXCEPTION(isolate, "no number found?");
326   return 0.f;
327 }
328
329 bool IsStringPrimitiveOrObject( const v8::Local<v8::Value>& value )
330 {
331   return ( value->IsString() || value->IsStringObject());
332 }
333
334 std::string GetStringValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value )
335 {
336   v8::EscapableHandleScope scope( isolate); // may not be required.
337
338   if( value->IsString() )
339   {
340     return V8Utils::v8StringToStdString(value);
341   }
342   else if (value->IsStringObject() )
343   {
344     const v8::Local<v8::StringObject> object = v8::Local<v8::StringObject>::Cast(value);
345     return V8Utils::v8StringToStdString( object->ValueOf() );
346   }
347
348   DALI_SCRIPT_EXCEPTION(isolate, "no string found?");
349   return "";
350 }
351
352
353 Property::Value GetPropertyValueFromObject( bool& found, v8::Isolate* isolate, const   v8::Local<v8::Value >& value  )
354 {
355   v8::HandleScope handleScope( isolate);
356
357   Property::Value  daliPropertyValue;// creates a property with Property::NONE
358
359   found = false;
360
361   if( value->IsObject() )
362   {
363     v8::Local<v8::Object> object = v8::Handle<v8::Object>::Cast( value );
364
365     if( BaseWrappedObject::IsWrappedTypeAPropertyValue( object ) )
366     {
367       found = true;
368       PropertyValueWrapper* propertyWrapper = PropertyValueWrapper::Unwrap( isolate, object );
369       return propertyWrapper->GetValue();
370     }
371     else if( value->IsArray() )
372     {
373       found = true;
374       return PropertyValueWrapper::VectorOrMatrixFromV8Array( isolate, object);//todo check for V8 array / map?
375     }
376   }
377   else if( value->IsBoolean() )
378   {
379     found = true;
380     v8::Local<v8::Boolean> v = value->ToBoolean();
381     return Dali::Property::Value(v->Value());
382   }
383   else if( value->IsNumber() )
384   {
385     found = true;
386     v8::Local<v8::Number> v = value->ToNumber();
387     return Dali::Property::Value(static_cast<float>(v->Value()));
388   }
389   else if( value->IsInt32() || value->IsUint32() )
390   {
391     found = true;
392     v8::Local<v8::Int32> v = value->ToInt32();
393     return Dali::Property::Value(static_cast<int>(v->Value()));
394   }
395   return daliPropertyValue;
396
397 }
398
399 Property::Map GetPropertyMapFromObject( v8::Isolate* isolate, const v8::Local<v8::Object>& object)
400 {
401   v8::Local<v8::Array> properties = object->GetPropertyNames();
402   Property::Map propertyMap;    // empty map
403
404   for(  unsigned int i = 0; i <  properties->Length(); ++i)
405   {
406     // Get the key
407     v8::Local<v8::Value> key = properties->Get( i );
408     std::string keyString = v8StringToStdString( key );
409
410     // Get the value
411     v8::Local<v8::Value> value = object->Get( key );
412
413     if( value->IsBoolean() )
414     {
415       v8::Local<v8::Boolean> v = value->ToBoolean();
416       propertyMap[ keyString ] = v->Value();
417     }
418     else if( value->IsNumber() )
419     {
420       v8::Local<v8::Number> v = value->ToNumber();
421       propertyMap[ keyString ] = static_cast<float>(v->Value());
422     }
423     else if( value->IsInt32() || value->IsUint32() )
424     {
425       v8::Local<v8::Int32> v = value->ToInt32();
426       propertyMap[ keyString ] = static_cast<int>(v->Value());
427     }
428     else if( value->IsString() )
429     {
430       std::string valueString = V8Utils::v8StringToStdString( value );
431       propertyMap[ keyString ] = valueString.c_str();
432     }
433     else if( value->IsArray() )
434     {
435       propertyMap[ keyString ] = PropertyValueWrapper::VectorOrMatrixFromV8Array( isolate, value);
436     }
437   }
438
439   return propertyMap;
440 }
441
442 Actor GetActorFromObject( v8::Isolate* isolate, bool& found, v8::Local<v8::Object>& object)
443 {
444   v8::HandleScope handleScope( isolate);
445   found = false;
446
447   if( BaseWrappedObject::IsWrappedType ( isolate, object, BaseWrappedObject::ACTOR ))
448   {
449     HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, object );
450     return Actor::DownCast( handleWrapper->mHandle );
451   }
452   return Actor();
453 }
454
455
456 int GetIntegerParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args, int defaultValue  )
457 {
458   found = false;
459   unsigned int length = args.Length();
460   if( index >= length )
461   {
462     return defaultValue;
463   }
464   if( args[ index ]->IsInt32() )
465   {
466     found = true;
467     return args[ index ]->Int32Value();
468   }
469   else
470   {
471     return defaultValue;
472   }
473 }
474
475 float GetFloatParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args, float defaultValue  )
476 {
477   found = false;
478   unsigned int length = args.Length();
479   if( index >= length )
480   {
481     return defaultValue;
482   }
483   if( args[ index ]->IsNumber() )
484   {
485     found = true;
486     return args[ index ]->NumberValue();
487   }
488   else
489   {
490     return defaultValue;
491   }
492 }
493
494 std::string GetStringParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
495 {
496   found = false;
497   unsigned int length = args.Length();
498
499   if( index >= length )
500   {
501     return std::string();
502   }
503   if( args[ index ]->IsString() )
504   {
505     found = true;
506     return v8StringToStdString( args[ index ]);
507   }
508   else
509   {
510     return std::string();
511   }
512 }
513
514 bool GetBooleanParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
515 {
516   v8::HandleScope handleScope( isolate);
517
518   found = false;
519   unsigned int length = args.Length();
520   if( index >= length )
521   {
522     return false;
523   }
524   if( args[ index ]->IsBoolean() )
525   {
526     found = true;
527     v8::Local<v8::Boolean> v = args[ index ]->ToBoolean();
528     return v->Value();
529   }
530   else
531   {
532     return false;
533   }
534 }
535
536 void* GetArrayBufferViewParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args  )
537 {
538   found = false;
539   unsigned int length = args.Length();
540   if( index < length && args[index]->IsArrayBufferView() )
541   {
542     found = true;
543     v8::ArrayBufferView* bufferView = v8::ArrayBufferView::Cast(*(args[index]));
544     v8::Handle<v8::ArrayBuffer> buffer = bufferView->Buffer();
545     v8::ArrayBuffer::Contents contents = buffer->Externalize();
546     return contents.Data();
547   }
548   else
549   {
550     return NULL;
551   }
552 }
553
554 Handle GetHandleParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
555 {
556   v8::HandleScope handleScope( isolate);
557
558   found = false;
559   unsigned int length = args.Length();
560   if( index >= length )
561   {
562     return Handle();
563   }
564
565   if( args[ index ]->IsObject() )
566   {
567     v8::Local<v8::Object> object = args[ index ]->ToObject();
568     v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
569     void* ptr = field->Value();
570     if( ptr )
571     {
572         found = true;
573         HandleWrapper* wrapper = static_cast< HandleWrapper *>(ptr);
574         return wrapper->GetHandle();
575     }
576   }
577   return Handle();
578 }
579
580 Vector2 GetVector2Parameter( unsigned int index,  bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
581 {
582   v8::HandleScope handleScope( isolate);
583   unsigned int length =  args.Length();
584   Vector2 ret;
585   found = false;
586
587   if( index < length )
588   {
589     if( args[ index ]->IsObject() )
590     {
591       Dali::Property::Value value;
592       value = PropertyValueWrapper::ExtractPropertyValue( isolate, args[index], Dali::Property::VECTOR2 );
593       if( value.GetType() == Dali::Property::VECTOR2)
594       {
595         found = true;
596         value.Get(ret);
597       }
598       else
599       {
600         DALI_SCRIPT_EXCEPTION(isolate, "Missing Vector2 parameter");
601       }
602     }
603   }
604   else
605   {
606     DALI_SCRIPT_EXCEPTION(isolate, "Missing Vector2 parameter");
607   }
608
609   return ret;
610 }
611
612 Vector2 GetVector2ParameterFrom2Float( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
613 {
614   Vector2 ret(0.0f,0.0f);
615   bool bFound(false);
616   unsigned int argCount( args.Length() );
617
618   if( index+2 >= argCount )
619   {
620     DALI_SCRIPT_EXCEPTION(isolate, "Missing parameter");
621   }
622
623   found = true;
624   ret.x = V8Utils::GetFloatParameter( index, bFound, isolate, args, 0.0f );
625   found = found && bFound;
626   ret.y = V8Utils::GetFloatParameter( index+1, bFound, isolate, args, 0.0f );
627   found = found && bFound;
628
629   return ret;
630 }
631
632 Vector3 GetVector3Parameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args)
633 {
634   v8::HandleScope handleScope( isolate);
635   unsigned int argCount( args.Length() );
636   Vector3 ret;
637   found = false;
638   if( index < argCount )
639   {
640     if( args[ index ]->IsObject() )
641     {
642       Dali::Property::Value value;
643       value = PropertyValueWrapper::ExtractPropertyValue( isolate, args[index], Dali::Property::VECTOR3 );
644       if( value.GetType() == Dali::Property::VECTOR3)
645       {
646         found = true;
647         value.Get(ret);
648       }
649       else
650       {
651         DALI_SCRIPT_EXCEPTION(isolate, "Missing Vector3 parameter");
652       }
653     }
654   }
655   else
656   {
657     DALI_SCRIPT_EXCEPTION(isolate, "Missing Vector3 parameter");
658
659   }
660
661   return ret;
662 }
663
664 Vector4 GetVector4Parameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args)
665 {
666   v8::HandleScope handleScope( isolate);
667   unsigned int argCount( args.Length() );
668   Vector4 ret;
669   found = false;
670
671   if( index < argCount )
672   {
673     if( args[ index ]->IsObject() )
674     {
675       Dali::Property::Value value;
676       value = PropertyValueWrapper::ExtractPropertyValue( isolate, args[index], Dali::Property::VECTOR4 );
677       if( value.GetType() == Dali::Property::VECTOR4)
678       {
679         found = true;
680         value.Get(ret);
681       }
682       else
683       {
684         DALI_SCRIPT_EXCEPTION(isolate, "Missing Vector4 parameter");
685       }
686     }
687   }
688   else
689   {
690     DALI_SCRIPT_EXCEPTION(isolate, "Missing Vector4 parameter");
691   }
692
693   return ret;
694 }
695
696
697 Rect<int> GetRectIntParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
698 {
699   v8::HandleScope handleScope( isolate);
700
701    found = false;
702    int length = args.Length() - index;
703
704    // if it's an array read the 2 numbers into a vector2
705    if( length == 4 )
706    {
707      if( args[ 0 + index ]->IsInt32() &&
708          args[ 1 + index ]->IsInt32() &&
709          args[ 2 + index ]->IsInt32() &&
710          args[ 3 + index ]->IsInt32() )
711      {
712        found = true;
713        Rect<int> rect( args[ 0 + index ]->Int32Value(),
714                        args[ 1 + index ]->Int32Value(),
715                        args[ 2 + index ]->Int32Value(),
716                        args[ 3 + index ]->Int32Value() );
717        return rect;
718      }
719    }
720    // this will extract a Vector4, if it is a Vector4 or a Javascript array object
721    if( args[ index ]->IsObject() )
722    {
723      Dali::Property::Value value;
724      value = PropertyValueWrapper::ExtractPropertyValue( isolate, args[index], Dali::Property::RECTANGLE );
725      if( value.GetType() == Dali::Property::RECTANGLE)
726      {
727        found = true;
728        Rect<int> rect;
729        value.Get(rect);
730        return rect;
731      }
732
733      // @todo support vector4 as well?
734    }
735    return Rect<int>();
736 }
737
738 Actor GetActorParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
739 {
740   BaseWrappedObject* wrapper = GetWrappedDaliObjectParameter( index, BaseWrappedObject::ACTOR, isolate, args);
741   ActorWrapper* actorWrapper = static_cast< ActorWrapper*>( wrapper );
742   if( actorWrapper )
743   {
744     found = true;
745     return actorWrapper->GetActor();
746   }
747   else
748   {
749     return Actor();
750   }
751 }
752
753 Layer GetLayerParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
754 {
755   Actor actor = GetActorParameter( index, found, isolate, args );
756   return Layer::DownCast( actor );
757 }
758
759 Image GetImageParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
760 {
761   BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( index, BaseWrappedObject::IMAGE, isolate, args );
762   if( wrappedObject )
763   {
764     found = true;
765     ImageWrapper* wrapper = static_cast< ImageWrapper *>(wrappedObject);
766     return wrapper->GetImage();
767   }
768   else
769   {
770     return Image();
771   }
772
773 }
774
775 RenderTask GetRenderTaskParameter( unsigned int paramIndex, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
776 {
777   found = false;
778   BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::RENDER_TASK, isolate, args );
779   if( wrappedObject )
780   {
781     found = true;
782     RenderTaskWrapper* wrapper = static_cast< RenderTaskWrapper *>(wrappedObject);
783     return wrapper->GetRenderTask();
784   }
785   else
786   {
787     return RenderTask(); // empty handle
788   }
789 }
790
791 BaseWrappedObject* GetWrappedDaliObjectParameter( unsigned int index,  BaseWrappedObject::Type type,  v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
792 {
793   v8::HandleScope handleScope( isolate);
794   unsigned int length = args.Length();
795
796   if( index >= length )
797   {
798     return NULL;
799   }
800
801   if( !args[ index ]->IsObject() )
802   {
803     return NULL;
804   }
805
806   v8::Local<v8::Object> object = args[ index ]->ToObject();
807
808   if( BaseWrappedObject::IsWrappedType ( isolate, object, type ))
809   {
810     v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
811     void* ptr = field->Value();
812     BaseWrappedObject* wrapper = static_cast< BaseWrappedObject *>(ptr);
813     return wrapper;
814   }
815   return NULL;
816 }
817
818
819 Property::Value GetPropertyValueParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
820 {
821   v8::HandleScope handleScope( isolate);
822
823   Property::Value  daliPropertyValue;// creates a property with Property::INVALID
824
825   found = false;
826   unsigned int length = args.Length();
827
828   if( index >= length )
829   {
830     return daliPropertyValue;
831   }
832   v8::Local<v8::Value > value = args[ index ];
833
834   return GetPropertyValueFromObject( found, isolate, value);
835 }
836
837 Property::Map GetPropertyMapParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
838 {
839   v8::HandleScope handleScope( isolate);
840
841   Property::Map propertyMap;    // empty map
842
843   found = false;
844   unsigned int length = args.Length();
845
846   if( index >= length )
847   {
848     return propertyMap;
849   }
850
851   if( !args[ index ]->IsObject() )
852   {
853     return propertyMap;
854   }
855   found = true;
856
857   // go through each key value pair
858   v8::Local<v8::Object> obj = args[ index ]->ToObject();
859
860   return GetPropertyMapFromObject( isolate, obj );
861
862 }
863
864 void CreatePropertyMap( v8::Isolate* isolate, const Property::Map& map, v8::Local<v8::Object>& object )
865 {
866   v8::HandleScope handleScope( isolate);
867
868   // we're converting a dali property map in to a JavaScript property map
869   if( map.Count() == 0 )
870   {
871     return;
872   }
873
874   for( unsigned int index = 0; index < map.Count() - 1; ++index )
875   {
876     const std::string& key = map.GetKey( index );
877     Property::Value& value = map.GetValue( index );
878     v8::Local<v8::Value> v8Value;
879
880     switch( value.GetType() )
881     {
882       case Dali::Property::FLOAT:
883       {
884         v8Value = v8::Number::New( isolate, value.Get<float>()  );
885         break;
886       }
887       case Dali::Property::BOOLEAN:
888       {
889         v8Value = v8::Boolean::New(  isolate, value.Get<bool>());
890         break;
891       }
892       case Dali::Property::INTEGER:
893       {
894         v8Value = v8::Integer::New( isolate, value.Get<int>());
895         break;
896       }
897       case Dali::Property::STRING:
898       {
899         std::string string = value.Get< std::string >();
900         v8Value = v8::String::NewFromUtf8( isolate,  string.c_str());
901         break;
902       }
903       case Dali::Property::VECTOR2:
904       {
905         // create a vector2
906         Vector2 vec = value.Get<Vector2>();
907         v8::Local<v8::Array> array= v8::Array::New( isolate, 2 );
908         array->Set( 0 , v8::Number::New(isolate, vec.x));
909         array->Set( 1 , v8::Number::New(isolate, vec.y));
910         v8Value = array;
911         break;
912       }
913       case Dali::Property::VECTOR3:
914       {
915         // create a vector 3
916         Vector3 vec = value.Get<Vector3>();
917         v8::Local<v8::Array> array= v8::Array::New( isolate, 3 );
918         array->Set( 0 , v8::Number::New(isolate, vec.x));
919         array->Set( 1 , v8::Number::New(isolate, vec.y));
920         array->Set( 2 , v8::Number::New(isolate, vec.z));
921         v8Value = array;
922         break;
923       }
924       case Dali::Property::VECTOR4:
925       {
926         // create a vector 4
927         Vector4 vec = value.Get<Vector4>();
928         v8::Local<v8::Array> array= v8::Array::New( isolate, 4 );
929         array->Set( 0 , v8::Number::New(isolate, vec.x));
930         array->Set( 1 , v8::Number::New(isolate, vec.y));
931         array->Set( 2 , v8::Number::New(isolate, vec.z));
932         array->Set( 3 , v8::Number::New(isolate, vec.w));
933         v8Value = array;
934         break;
935       }
936
937       default:
938       {
939         DALI_SCRIPT_EXCEPTION( isolate, "Primitive mismatch \n");
940         return;
941       }
942     }
943     object->Set( v8::String::NewFromUtf8( isolate, key.c_str() ), v8Value );
944   }
945 }
946
947 void ReadFloatArguments( bool& foundAllArguments, float* data, unsigned int dataSize, const v8::FunctionCallbackInfo< v8::Value >& args, float defaultValue )
948 {
949   foundAllArguments = true;
950   unsigned int length = args.Length();
951
952   if( length < dataSize )
953   {
954     foundAllArguments = false;
955   }
956
957   for( unsigned int i = 0; i< dataSize ;i++ )
958   {
959     if( i < length )
960     {
961       if( args[ i ]->IsNumber()  )
962       {
963         data[i] = args[i]->NumberValue();
964       }
965       else
966       {
967         data[i] = defaultValue;
968         foundAllArguments = false;   // bad argument
969       }
970     }
971     else
972     {
973       data[i] = defaultValue; // not enough arguments
974     }
975   }
976
977 }
978
979 void ReadIntegerArguments( bool& foundAllArguments, int* data, int dataSize, const v8::FunctionCallbackInfo< v8::Value >& args, int defaultValue )
980 {
981   foundAllArguments = true;
982   int length = args.Length();
983   if( length < dataSize )
984   {
985     foundAllArguments = false;
986   }
987
988   for( int i = 0; i< dataSize ;i++ )
989   {
990     if( i < length )
991     {
992       if( args[ i ]->IsInt32()  )
993       {
994         data[i] = args[i]->Int32Value();
995       }
996       else
997       {
998         data[i] = defaultValue;
999         foundAllArguments = false;   // bad argument
1000       }
1001     }
1002     else
1003     {
1004       data[i] = defaultValue; // not enough arguments
1005     }
1006   }
1007
1008 }
1009 } // namespace V8Utils
1010
1011 } // namespace V8Plugin
1012
1013 } // namespace Dali