Purge underscored header file barriers
[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) 2019 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 #define DALI_SCRIPT_WARNING( message ) V8Utils::ScriptWarning( __FUNCTION__ , message );
54
55 namespace V8Utils
56 {
57 /**
58  * Print the list of arguments to std out
59  * @param[in] args v8 function args interpreted as (string, string,...)
60  */
61 void Log(const v8::FunctionCallbackInfo< v8::Value >& args) ;
62
63 /**
64  * Print out using DALI_LOG_ERROR
65  * @param[in] args v8 function args interpreted as (string, string,...)
66  */
67 void LogError(const v8::FunctionCallbackInfo< v8::Value >& args) ;
68
69 /**
70  * Read a files contents
71  *
72  * @param[in] filename The filename
73  * @param[out] contents string contents of the file
74  */
75 void GetFileContents( const std::string &filename, std::string& contents );
76
77 /**
78  * Extract the directory path from a full path filename
79  * @param fileName file name
80  * @param directory directory name
81  */
82 void GetFileDirectory( const std::string& fileName, std::string& directory );
83
84 /**
85  * Extract file name from a full path + file name
86  * @param[in] fullPathName full path + file name
87  * @param[out] fileName file name
88  */
89 void GetFileName( const std::string& fullPathName, std::string& fileName);
90
91 /**
92  * Return the module name, e.g. if the module is
93  * my_module.js it will get 'my_module'
94  * @param[in] fileName full path module name
95  * @param[out] moduleName module name
96  */
97 void GetModuleName( const std::string& fileName, std::string& moduleName );
98
99 /**
100  * Compare whether two DALi property maps are identical
101  * @param[in] map1 The first property map to be compared
102  * @param[in] map2 The second property map to be compared
103  * @return true if the two specified property maps are identical or false if not.
104  */
105 bool IsPropertyMapIdentical(Property::Map map1, Property::Map map2);
106
107 /**
108  * Report an exception by writing as a warning to the Dali Log
109  *
110  * @param[in] try_catch The v8 TryCatch exception object
111  */
112 void ReportException(  v8::Isolate* isolate, v8::TryCatch* try_catch) ;
113
114 /**
115  * Depending on what coding standard used this can
116  * convert the first character to lower case,
117  * E.g.  GetId becomes getId
118  */
119 std::string GetJavaScriptFunctionName(  const char* functionName );
120
121  /**
122   * Get V8 Version
123   *
124   * @param[in] args v8 function args ignored
125   * @return a version string
126   */
127  void Version(const v8::FunctionCallbackInfo< v8::Value >& args) ;
128
129 /**
130  * Convert v8 string as a std::string
131  * @param[in] value v8 function args interpreted as (v8_string)
132  * @return std string
133  */
134  std::string v8StringToStdString( const v8::Handle<v8::Value>& value) ;
135
136 /**
137  * Convert hyphenated to camelCase (Dali property to wrapper property name)
138  *
139  * @param[in] hyphenatedName a hyphenated std::string to convert
140  * @return a camelCase'd std::string
141  */
142  std::string PropertyNameToJavaScriptName(const std::string& hyphenatedName);
143
144 /**
145  * Script error, throws an exception
146  */
147 void ScriptError( const char* function, v8::Isolate* isolate, std::string errorString );
148
149 /**
150  * Script warning
151  */
152 void ScriptWarning( const char* function, std::string warningString );
153
154 /**
155  * @return in the value is a boolean primitive or a boolean object
156  */
157 bool IsBooleanPrimitiveOrObject( const v8::Local<v8::Value>& value );
158
159 /**
160  * @return the value of boolean primitive or boolean object value
161  */
162 bool GetBooleanValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value );
163
164 /**
165  * @return true if the value is a number or a number object
166  */
167 bool IsNumberPrimitiveOrObject( const v8::Local<v8::Value>& value );
168
169 /**
170  * @return the number of a number primitive or number object value
171  */
172 float GetNumberValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value );
173
174 /**
175  * @return if the value is a string primitve or a string object
176  */
177 bool IsStringPrimitiveOrObject( const v8::Local<v8::Value>& value );
178
179 /**
180  * @return true if the value is a string or a string object
181  */
182 bool IsStringPrimitiveOrObject( const v8::Local<v8::Value>& value );
183
184 /**
185  * @return the string from a string value or a string object
186  */
187 std::string GetStringValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value );
188
189 /******************************************
190  *  Helper functions for extracting a DALi object from a JavaScript object
191  *******************************************/
192
193 /**
194  * Given a JavaScript object, either extract the embedded DALi property value or
195  * convert it to a property value
196  * @param[out] found whether the property was found
197  * @return property value
198  */
199 Property::Value GetPropertyValueFromObject( bool& found, v8::Isolate* isolate, const v8::Local<v8::Value >& value );
200
201 /**
202  * Given a JavaScript object with
203  * @param [in] object JavaScrript object
204  * @return DALi ProperyMap from the JavaScript object
205  */
206 Property::Map GetPropertyMapFromObject( v8::Isolate* isolate, const v8::Local<v8::Object>& object);
207
208 /**
209  * Extract a DALi wrapped object, from a JavaScript object held in a function argument
210  * @param[in] index argument index the object is held in
211  * @param[in] type type of wrapped object to extract
212  */
213
214 Actor GetActorFromObject( v8::Isolate* isolate, bool& found, v8::Local<v8::Object>& object);
215
216
217 /******************************************
218  *  Helper functions to extract a C++ native type (int,float,string) or a DALi object
219  *  ( handle, vector, actor, layer, property value, property map ) from a
220  *  JavaScript function argument.
221  *  E.g.
222  *  // JavaScript code
223  *     myJavaScriptFunction( 23, 345, myActor );
224  *
225  *  // C++ code
226  * bool found;
227  *
228  * Actor myActor = v8Utils::GetActorParameter( PARAMETER_2, found, JavaScript Args );
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 integer value from the JavaScript function arguments
234  */
235 int GetIntegerParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args, int defaultValue  );
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 float value from the JavaScript function arguments
241  */
242 float GetFloatParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args, float defaultValue  );
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 string value from the JavaScript function arguments
248  */
249 std::string GetStringParameter( 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 boolean value from the JavaScript function arguments
255  */
256 bool GetBooleanParameter( 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 ArrayBufferView from the JavaScript function arguments
262  */
263 void* GetArrayBufferViewParameter( 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 DALi Handle value from the JavaScript function arguments
269  */
270 Handle GetHandleParameter( 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 Vector2 from the JavaScript function arguments
276  */
277 Vector2 GetVector2Parameter( 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 Vector3 from the JavaScript function arguments
283  */
284 Vector3 GetVector3Parameter( 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 Vector4 from the JavaScript function arguments
290  */
291 Vector4 GetVector4Parameter( 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 Rect<int> from the JavaScript function arguments
297  */
298 Rect<int> GetRectIntParameter( 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 Actor from the JavaScript function arguments
304  */
305 Actor GetActorParameter( 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 Layer from the JavaScript function arguments
311  */
312 Layer GetLayerParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
313
314 /**
315  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
316  * @param[out] found whether the parameter was found
317  * @return Image from the JavaScript function arguments
318  */
319 Image GetImageParameter( unsigned int index, bool& found, 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 RenderTask from the JavaScript function arguments
325  */
326 RenderTask GetRenderTaskParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
327
328 /**
329  * Extract a DALi wrapped object, from a JavaScript object held in a function argument
330  * @param[in] index argument index the object is held in
331  * @param[in] type type of wrapped object to extract
332  */
333 BaseWrappedObject* GetWrappedDaliObjectParameter( unsigned int index, BaseWrappedObject::Type type, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
334
335 /**
336  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
337  * @param[out] found whether the parameter was found
338  * @return ProperyValue from the  JavaScript function arguments
339  */
340 Property::Value GetPropertyValueParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
341
342 /**
343  * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
344  * @param[out] found whether the parameter was found
345  * @return ProperyMap from the  JavaScript function arguments
346  */
347 Property::Map GetPropertyMapParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
348
349 /**
350  * Generate a JavaScript property map, from a DALi property map
351  * @param [in] DALi map property map
352  * @param [in] JavaScript property map
353  */
354 void CreatePropertyMap( v8::Isolate* isolate, const Property::Map& map, v8::Local<v8::Object>& object );
355
356
357 /**
358  * Read multiple float arguments from v8 args object
359  * e.g. myJavaFunc( 3.4, 7.6, 8.5 );
360  *
361  * @param[out] foundAllArguments flag to say all arguments are found
362  * @param[out] data pointer to a pre-allocated array of floats
363  * @param[in] dataSize the nunber of floats in the data parameter
364  * @param[in] defaultValue default value assigned to floats that don't exist in the arguments
365  */
366 void ReadFloatArguments( bool& foundAllArguments, float* data, unsigned int dataSize, const v8::FunctionCallbackInfo< v8::Value >& args, float defaultValue );
367
368 /**
369  * Read multiple integer arguments from v8 args object
370  * e.g. myJavaFunc( 3, 7, 8 );
371  *
372  * @param[out] foundAllArguments flag to say all arguments are found
373  * @param[out] data pointer to a pre-allocated array of integers
374  * @param[in] dataSize the nunber of integers in the data parameter
375  * @param[in] defaultValue default value assigned to integers that don't exist in the arguments
376  */
377 void ReadIntegerArguments( bool& foundAllArguments, int* data, int dataSize, const v8::FunctionCallbackInfo< v8::Value >& args, int defaultValue );
378
379 } // namespace V8Utils
380
381 } // namespace V8Plugin
382
383 } // namespace Dali
384
385
386 #endif // DALI_V8PLUGIN_V8_UTILS_H
387