534c5472f14270936b8e1ebfd6cf34b95c4baf5d
[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  * Convert camelCase to hyphenated to (Wrapper property name to Dali property)
137  * E.g. anchorPoint to "anchor-point"
138  * @param[in] camelCase a std::string camelCase
139  * @return a hyphenated std::string conversion
140  */
141  std::string JavaScriptNameToPropertyName(const std::string& camelCase);
142
143 /**
144  * Script error, throws an exception
145  */
146 void ScriptError( const char* function, v8::Isolate* isolate, std::string errorString );
147
148 /**
149  * @return in the value is a boolean primitive or a boolean object
150  */
151 bool IsBooleanPrimitiveOrObject( const v8::Local<v8::Value>& value );
152
153 /**
154  * @return the value of boolean primitive or boolean object value
155  */
156 bool GetBooleanValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value );
157
158 /**
159  * @return true if the value is a number or a number object
160  */
161 bool IsNumberPrimitiveOrObject( const v8::Local<v8::Value>& value );
162
163 /**
164  * @return the number of a number primitive or number object value
165  */
166 float GetNumberValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value );
167
168 /**
169  * @return if the value is a string primitve or a string object
170  */
171 bool IsStringPrimitiveOrObject( const v8::Local<v8::Value>& value );
172
173 /**
174  * @return true if the value is a string or a string object
175  */
176 bool IsStringPrimitiveOrObject( const v8::Local<v8::Value>& value );
177
178 /**
179  * @return the string from a string value or a string object
180  */
181 std::string GetStringValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value );
182
183 /******************************************
184  *  Helper functions for extracting a DALi object from a JavaScript object
185  *******************************************/
186
187 /**
188  * Given a JavaScript object, either extract the embedded DALi property value or
189  * convert it to a property value
190  * @param[out] found whether the property was found
191  * @return property value
192  */
193 Property::Value GetPropertyValueFromObject( bool& found, v8::Isolate* isolate, const v8::Local<v8::Value >& value );
194
195 /**
196  * Given a JavaScript object with
197  * @param [in] object JavaScrript object
198  * @return DALi ProperyMap from the  JavaScript object
199  */
200 Property::Map GetPropertyMapFromObject( v8::Isolate* isolate, const v8::Local<v8::Object>& object);
201
202 /**
203  * Extract a DALi wrapped object, from a JavaScript object held in a function argument
204  * @param[in] index argument index the object is held in
205  * @param[in] type type of wrapped object to extract
206  */
207
208 Actor GetActorFromObject( v8::Isolate* isolate, bool& found, v8::Local<v8::Object>& object);
209
210
211 /******************************************
212  *  Helper functions to extract a C++ native type (int,float,string) or a DALi object
213  *  ( handle, vector, actor, layer, property value, property map ) from a
214  *  JavaScript function argument.
215  *  E.g.
216  *  // JavaScript code
217  *     myJavaScriptFunction( 23, 345, myActor );
218  *
219  *  // C++ code
220  * bool found;
221  *
222  * Actor myActor = v8Utils::GetActorParameter( PARAMETER_2, found, JavaScript Args );
223  ******************************************/
224 /**
225  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
226  * @param[out] found whether the parameter was found
227  * @return integer value from the JavaScript function arguments
228  */
229 int GetIntegerParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args, int defaultValue  );
230
231 /**
232  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
233  * @param[out] found whether the parameter was found
234  * @return float value from the JavaScript function arguments
235  */
236 float GetFloatParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args, float defaultValue  );
237
238 /**
239  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
240  * @param[out] found whether the parameter was found
241  * @return string value from the JavaScript function arguments
242  */
243 std::string GetStringParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
244
245 /**
246  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
247  * @param[out] found whether the parameter was found
248  * @return boolean value from the JavaScript function arguments
249  */
250 bool GetBooleanParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
251
252 /**
253  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
254  * @param[out] found whether the parameter was found
255  * @return DALi Handle value from the JavaScript function arguments
256  */
257 Handle GetHandleParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args  );
258
259 /**
260  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
261  * @param[out] found whether the parameter was found
262  * @return Vector2 from the JavaScript function arguments
263  */
264 Vector2 GetVector2Parameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
265
266 /**
267  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
268  * @param[out] found whether the parameter was found
269  * @return Vector3 from the JavaScript function arguments
270  */
271 Vector3 GetVector3Parameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args);
272
273 /**
274  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
275  * @param[out] found whether the parameter was found
276  * @return Vector4 from the JavaScript function arguments
277  */
278 Vector4 GetVector4Parameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args);
279
280 /**
281  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
282  * @param[out] found whether the parameter was found
283  * @return Rect<int> from the JavaScript function arguments
284  */
285 Rect<int> GetRectIntParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
286
287 /**
288  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
289  * @param[out] found whether the parameter was found
290  * @return Actor from the JavaScript function arguments
291  */
292 Actor GetActorParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
293
294 /**
295  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
296  * @param[out] found whether the parameter was found
297  * @return Layer from the JavaScript function arguments
298  */
299 Layer GetLayerParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
300
301 /**
302  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
303  * @param[out] found whether the parameter was found
304  * @return Image from the JavaScript function arguments
305  */
306 Image GetImageParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
307
308 /**
309  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
310  * @param[out] found whether the parameter was found
311  * @return RenderTask from the JavaScript function arguments
312  */
313 RenderTask GetRenderTaskParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
314
315 /**
316  * Extract a DALi wrapped object, from a JavaScript object held in a function argument
317  * @param[in] index argument index the object is held in
318  * @param[in] type type of wrapped object to extract
319  */
320 BaseWrappedObject* GetWrappedDaliObjectParameter( unsigned int index, BaseWrappedObject::Type type, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
321
322 /**
323  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
324  * @param[out] found whether the parameter was found
325  * @return ProperyValue from the  JavaScript function arguments
326  */
327 Property::Value GetPropertyValueParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
328
329 /**
330  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
331  * @param[out] found whether the parameter was found
332  * @return ProperyMap from the  JavaScript function arguments
333  */
334 Property::Map GetPropertyMapParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
335
336 /**
337  * Generate a JavaScript property map, from a DALi property map
338  * @param [in] DALi map property map
339  * @param [in] JavaScript property map
340  */
341 void CreatePropertyMap( v8::Isolate* isolate, const Property::Map& map, v8::Local<v8::Object>& object );
342
343
344 /**
345  * Read multiple float arguments from v8 args object
346  * e.g. myJavaFunc( 3.4, 7.6, 8.5 );
347  *
348  * @param[out] foundAllArguments flag to say all arguments are found
349  * @param[out] data pointer to a pre-allocated array of floats
350  * @param[in] dataSize the nunber of floats in the data parameter
351  * @param[in] defaultValue default value assigned to floats that don't exist in the arguments
352  */
353 void ReadFloatArguments( bool& foundAllArguments, float* data, unsigned int dataSize, const v8::FunctionCallbackInfo< v8::Value >& args, float defaultValue );
354
355 /**
356  * Read multiple integer arguments from v8 args object
357  * e.g. myJavaFunc( 3, 7, 8 );
358  *
359  * @param[out] foundAllArguments flag to say all arguments are found
360  * @param[out] data pointer to a pre-allocated array of integers
361  * @param[in] dataSize the nunber of integers in the data parameter
362  * @param[in] defaultValue default value assigned to integers that don't exist in the arguments
363  */
364 void ReadIntegerArguments( bool& foundAllArguments, int* data, int dataSize, const v8::FunctionCallbackInfo< v8::Value >& args, int defaultValue );
365
366 } // namespace V8Utils
367
368 } // namespace V8Plugin
369
370 } // namespace Dali
371
372
373 #endif // __DALI_V8PLUGIN_V8_UTILS_H__
374