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.h
1 #ifndef __DALI_V8PLUGIN_V8_UTILS_H__
2 #define __DALI_V8PLUGIN_V8_UTILS_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <string>
23 #include <v8.h>
24 #include <dali/public-api/math/vector2.h>
25 #include <dali/public-api/math/vector3.h>
26 #include <dali/public-api/math/vector4.h>
27 #include <dali/public-api/math/rect.h>
28 #include <dali/public-api/actors/actor.h>
29 #include <dali/public-api/actors/layer.h>
30 #include <dali/public-api/render-tasks/render-task.h>
31 #include <dali/public-api/object/property-value.h>
32 #include <dali/public-api/object/property-map.h>
33
34 // INTERNAL INCLUDES
35 #include <shared/base-wrapped-object.h>
36
37 namespace Dali
38 {
39
40 namespace V8Plugin
41 {
42
43 enum
44 {
45   PARAMETER_0 = 0,   ///< first parameter of a function call
46   PARAMETER_1 = 1,   ///< second parameter of a function call
47   PARAMETER_2 = 2,   ///< third parameter of a function call
48   PARAMETER_3 = 3,   ///< forth parameter of a function call
49   PARAMETER_4 = 4,   ///< fifth parameter of a function call
50 };
51
52 #define DALI_SCRIPT_EXCEPTION( isolate, message ) V8Utils::ScriptError( __FUNCTION__ , isolate, message );
53
54 namespace V8Utils
55 {
56 /**
57  * Print the list of arguments to std out
58  * @param[in] args v8 function args interpreted as (string, string,...)
59  */
60 void Log(const v8::FunctionCallbackInfo< v8::Value >& args) ;
61
62 /**
63  * Print out using DALI_LOG_ERROR
64  * @param[in] args v8 function args interpreted as (string, string,...)
65  */
66 void LogError(const v8::FunctionCallbackInfo< v8::Value >& args) ;
67
68 /**
69  * Read a files contents
70  *
71  * @param[in] filename The filename
72  * @param[out] contents string contents of the file
73  */
74 void GetFileContents( const std::string &filename, std::string& contents );
75
76 /**
77  * Extract the directory path from a full path filename
78  * @param fileName file name
79  * @param directory directory name
80  */
81 void GetFileDirectory( const std::string& fileName, std::string& directory );
82
83 /**
84  * Extract file name from a full path + file name
85  * @param[in] fullPathName full path + file name
86  * @param[out] fileName file name
87  */
88 void GetFileName( const std::string& fullPathName, std::string& fileName);
89
90 /**
91  * Return the module name, e.g. if the module is
92  * my_module.js it will get 'my_module'
93  * @param[in] fileName full path module name
94  * @param[out] moduleName module name
95  */
96 void GetModuleName( const std::string& fileName, std::string& moduleName );
97
98 /**
99  * Report an exception by writing as a warning to the Dali Log
100  *
101  * @param[in] try_catch The v8 TryCatch exception object
102  */
103 void ReportException(  v8::Isolate* isolate, v8::TryCatch* try_catch) ;
104
105 /**
106  * Depending on what coding standard used this can
107  * convert the first character to lower case,
108  * E.g.  GetId becomes getId
109  */
110 std::string GetJavaScriptFunctionName(  const char* functionName );
111
112  /**
113   * Get V8 Version
114   *
115   * @param[in] args v8 function args ignored
116   * @return a version string
117   */
118  void Version(const v8::FunctionCallbackInfo< v8::Value >& args) ;
119
120 /**
121  * Convert v8 string as a std::string
122  * @param[in] value v8 function args interpreted as (v8_string)
123  * @return std string
124  */
125  std::string v8StringToStdString( const v8::Handle<v8::Value>& value) ;
126
127 /**
128  * Convert hyphenated to camelCase (Dali property to wrapper property name)
129  *
130  * @param[in] hyphenatedName a hyphenated std::string to convert
131  * @return a camelCase'd std::string
132  */
133  std::string PropertyNameToJavaScriptName(const std::string& hyphenatedName);
134
135 /**
136  * Script error, throws an exception
137  */
138 void ScriptError( const char* function, v8::Isolate* isolate, std::string errorString );
139
140 /**
141  * @return in the value is a boolean primitive or a boolean object
142  */
143 bool IsBooleanPrimitiveOrObject( const v8::Local<v8::Value>& value );
144
145 /**
146  * @return the value of boolean primitive or boolean object value
147  */
148 bool GetBooleanValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value );
149
150 /**
151  * @return true if the value is a number or a number object
152  */
153 bool IsNumberPrimitiveOrObject( const v8::Local<v8::Value>& value );
154
155 /**
156  * @return the number of a number primitive or number object value
157  */
158 float GetNumberValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value );
159
160 /**
161  * @return if the value is a string primitve or a string object
162  */
163 bool IsStringPrimitiveOrObject( const v8::Local<v8::Value>& value );
164
165 /**
166  * @return true if the value is a string or a string object
167  */
168 bool IsStringPrimitiveOrObject( const v8::Local<v8::Value>& value );
169
170 /**
171  * @return the string from a string value or a string object
172  */
173 std::string GetStringValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value );
174
175 /******************************************
176  *  Helper functions for extracting a DALi object from a JavaScript object
177  *******************************************/
178
179 /**
180  * Given a JavaScript object, either extract the embedded DALi property value or
181  * convert it to a property value
182  * @param[out] found whether the property was found
183  * @return property value
184  */
185 Property::Value GetPropertyValueFromObject( bool& found, v8::Isolate* isolate, const v8::Local<v8::Value >& value );
186
187 /**
188  * Given a JavaScript object with
189  * @param [in] object JavaScrript object
190  * @return DALi ProperyMap from the JavaScript object
191  */
192 Property::Map GetPropertyMapFromObject( v8::Isolate* isolate, const v8::Local<v8::Object>& object);
193
194 /**
195  * Extract a DALi wrapped object, from a JavaScript object held in a function argument
196  * @param[in] index argument index the object is held in
197  * @param[in] type type of wrapped object to extract
198  */
199
200 Actor GetActorFromObject( v8::Isolate* isolate, bool& found, v8::Local<v8::Object>& object);
201
202
203 /******************************************
204  *  Helper functions to extract a C++ native type (int,float,string) or a DALi object
205  *  ( handle, vector, actor, layer, property value, property map ) from a
206  *  JavaScript function argument.
207  *  E.g.
208  *  // JavaScript code
209  *     myJavaScriptFunction( 23, 345, myActor );
210  *
211  *  // C++ code
212  * bool found;
213  *
214  * Actor myActor = v8Utils::GetActorParameter( PARAMETER_2, found, JavaScript Args );
215  ******************************************/
216 /**
217  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
218  * @param[out] found whether the parameter was found
219  * @return integer value from the JavaScript function arguments
220  */
221 int GetIntegerParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args, int defaultValue  );
222
223 /**
224  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
225  * @param[out] found whether the parameter was found
226  * @return float value from the JavaScript function arguments
227  */
228 float GetFloatParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args, float defaultValue  );
229
230 /**
231  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
232  * @param[out] found whether the parameter was found
233  * @return string value from the JavaScript function arguments
234  */
235 std::string GetStringParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
236
237 /**
238  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
239  * @param[out] found whether the parameter was found
240  * @return boolean value from the JavaScript function arguments
241  */
242 bool GetBooleanParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
243
244 /**
245  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
246  * @param[out] found whether the parameter was found
247  * @return ArrayBufferView from the JavaScript function arguments
248  */
249 void* GetArrayBufferViewParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args  );
250
251 /**
252  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
253  * @param[out] found whether the parameter was found
254  * @return DALi Handle value from the JavaScript function arguments
255  */
256 Handle GetHandleParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args  );
257
258 /**
259  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
260  * @param[out] found whether the parameter was found
261  * @return Vector2 from the JavaScript function arguments
262  */
263 Vector2 GetVector2Parameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
264
265 /**
266  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
267  * @param[out] found whether the parameter was found
268  * @return Vector3 from the JavaScript function arguments
269  */
270 Vector3 GetVector3Parameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args);
271
272 /**
273  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
274  * @param[out] found whether the parameter was found
275  * @return Vector4 from the JavaScript function arguments
276  */
277 Vector4 GetVector4Parameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args);
278
279 /**
280  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
281  * @param[out] found whether the parameter was found
282  * @return Rect<int> from the JavaScript function arguments
283  */
284 Rect<int> GetRectIntParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
285
286 /**
287  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
288  * @param[out] found whether the parameter was found
289  * @return Actor from the JavaScript function arguments
290  */
291 Actor GetActorParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
292
293 /**
294  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
295  * @param[out] found whether the parameter was found
296  * @return Layer from the JavaScript function arguments
297  */
298 Layer GetLayerParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
299
300 /**
301  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
302  * @param[out] found whether the parameter was found
303  * @return Image from the JavaScript function arguments
304  */
305 Image GetImageParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
306
307 /**
308  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
309  * @param[out] found whether the parameter was found
310  * @return RenderTask from the JavaScript function arguments
311  */
312 RenderTask GetRenderTaskParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
313
314 /**
315  * Extract a DALi wrapped object, from a JavaScript object held in a function argument
316  * @param[in] index argument index the object is held in
317  * @param[in] type type of wrapped object to extract
318  */
319 BaseWrappedObject* GetWrappedDaliObjectParameter( unsigned int index, BaseWrappedObject::Type type, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
320
321 /**
322  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
323  * @param[out] found whether the parameter was found
324  * @return ProperyValue from the  JavaScript function arguments
325  */
326 Property::Value GetPropertyValueParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
327
328 /**
329  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
330  * @param[out] found whether the parameter was found
331  * @return ProperyMap from the  JavaScript function arguments
332  */
333 Property::Map GetPropertyMapParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
334
335 /**
336  * Generate a JavaScript property map, from a DALi property map
337  * @param [in] DALi map property map
338  * @param [in] JavaScript property map
339  */
340 void CreatePropertyMap( v8::Isolate* isolate, const Property::Map& map, v8::Local<v8::Object>& object );
341
342
343 /**
344  * Read multiple float arguments from v8 args object
345  * e.g. myJavaFunc( 3.4, 7.6, 8.5 );
346  *
347  * @param[out] foundAllArguments flag to say all arguments are found
348  * @param[out] data pointer to a pre-allocated array of floats
349  * @param[in] dataSize the nunber of floats in the data parameter
350  * @param[in] defaultValue default value assigned to floats that don't exist in the arguments
351  */
352 void ReadFloatArguments( bool& foundAllArguments, float* data, unsigned int dataSize, const v8::FunctionCallbackInfo< v8::Value >& args, float defaultValue );
353
354 /**
355  * Read multiple integer arguments from v8 args object
356  * e.g. myJavaFunc( 3, 7, 8 );
357  *
358  * @param[out] foundAllArguments flag to say all arguments are found
359  * @param[out] data pointer to a pre-allocated array of integers
360  * @param[in] dataSize the nunber of integers in the data parameter
361  * @param[in] defaultValue default value assigned to integers that don't exist in the arguments
362  */
363 void ReadIntegerArguments( bool& foundAllArguments, int* data, int dataSize, const v8::FunctionCallbackInfo< v8::Value >& args, int defaultValue );
364
365 } // namespace V8Utils
366
367 } // namespace V8Plugin
368
369 } // namespace Dali
370
371
372 #endif // __DALI_V8PLUGIN_V8_UTILS_H__
373