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