1 #ifndef __DALI_V8PLUGIN_V8_UTILS_H__
2 #define __DALI_V8PLUGIN_V8_UTILS_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
35 #include <shared/base-wrapped-object.h>
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
52 #define DALI_SCRIPT_EXCEPTION( isolate, message ) V8Utils::ScriptError( __FUNCTION__ , isolate, message );
57 * Print the list of arguments to std out
58 * @param[in] args v8 function args interpreted as (string, string,...)
60 void Log(const v8::FunctionCallbackInfo< v8::Value >& args) ;
63 * Print out using DALI_LOG_ERROR
64 * @param[in] args v8 function args interpreted as (string, string,...)
66 void LogError(const v8::FunctionCallbackInfo< v8::Value >& args) ;
69 * Read a files contents
71 * @param[in] filename The filename
72 * @param[out] contents string contents of the file
74 void GetFileContents( const std::string &filename, std::string& contents );
77 * Extract the directory path from a full path filename
78 * @param fileName file name
79 * @param directory directory name
81 void GetFileDirectory( const std::string& fileName, std::string& directory );
84 * Extract file name from a full path + file name
85 * @param[in] fullPathName full path + file name
86 * @param[out] fileName file name
88 void GetFileName( const std::string& fullPathName, std::string& fileName);
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
96 void GetModuleName( const std::string& fileName, std::string& moduleName );
99 * Report an exception by writing as a warning to the Dali Log
101 * @param[in] try_catch The v8 TryCatch exception object
103 void ReportException( v8::Isolate* isolate, v8::TryCatch* try_catch) ;
106 * Depending on what coding standard used this can
107 * convert the first character to lower case,
108 * E.g. GetId becomes getId
110 std::string GetJavaScriptFunctionName( const char* functionName );
115 * @param[in] args v8 function args ignored
116 * @return a version string
118 void Version(const v8::FunctionCallbackInfo< v8::Value >& args) ;
121 * Convert v8 string as a std::string
122 * @param[in] value v8 function args interpreted as (v8_string)
125 std::string v8StringToStdString( const v8::Handle<v8::Value>& value) ;
128 * Convert hyphenated to camelCase (Dali property to wrapper property name)
130 * @param[in] hyphenatedName a hyphenated std::string to convert
131 * @return a camelCase'd std::string
133 std::string PropertyNameToJavaScriptName(const std::string& hyphenatedName);
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
141 std::string JavaScriptNameToPropertyName(const std::string& camelCase);
144 * Script error, throws an exception
146 void ScriptError( const char* function, v8::Isolate* isolate, std::string errorString );
149 * @return in the value is a boolean primitive or a boolean object
151 bool IsBooleanPrimitiveOrObject( const v8::Local<v8::Value>& value );
154 * @return the value of boolean primitive or boolean object value
156 bool GetBooleanValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value );
159 * @return true if the value is a number or a number object
161 bool IsNumberPrimitiveOrObject( const v8::Local<v8::Value>& value );
164 * @return the number of a number primitive or number object value
166 float GetNumberValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value );
169 * @return if the value is a string primitve or a string object
171 bool IsStringPrimitiveOrObject( const v8::Local<v8::Value>& value );
174 * @return true if the value is a string or a string object
176 bool IsStringPrimitiveOrObject( const v8::Local<v8::Value>& value );
179 * @return the string from a string value or a string object
181 std::string GetStringValue( v8::Isolate* isolate, const v8::Local<v8::Value>& value );
183 /******************************************
184 * Helper functions for extracting a DALi object from a JavaScript object
185 *******************************************/
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
193 Property::Value GetPropertyValueFromObject( bool& found, v8::Isolate* isolate, const v8::Local<v8::Value >& value );
196 * Given a JavaScript object with
197 * @param [in] object JavaScrript object
198 * @return DALi ProperyMap from the JavaScript object
200 Property::Map GetPropertyMapFromObject( v8::Isolate* isolate, const v8::Local<v8::Object>& object);
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
208 Actor GetActorFromObject( v8::Isolate* isolate, bool& found, v8::Local<v8::Object>& object);
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.
217 * myJavaScriptFunction( 23, 345, myActor );
222 * Actor myActor = v8Utils::GetActorParameter( PARAMETER_2, found, JavaScript Args );
223 ******************************************/
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
229 int GetIntegerParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args, int defaultValue );
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
236 float GetFloatParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args, float defaultValue );
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
243 std::string GetStringParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
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
250 bool GetBooleanParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
253 * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
254 * @param[out] found whether the parameter was found
255 * @return ArrayBufferView from the JavaScript function arguments
257 void* GetArrayBufferViewParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
260 * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
261 * @param[out] found whether the parameter was found
262 * @return DALi Handle value from the JavaScript function arguments
264 Handle GetHandleParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
267 * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
268 * @param[out] found whether the parameter was found
269 * @return Vector2 from the JavaScript function arguments
271 Vector2 GetVector2Parameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
274 * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
275 * @param[out] found whether the parameter was found
276 * @return Vector3 from the JavaScript function arguments
278 Vector3 GetVector3Parameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args);
281 * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
282 * @param[out] found whether the parameter was found
283 * @return Vector4 from the JavaScript function arguments
285 Vector4 GetVector4Parameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args);
288 * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
289 * @param[out] found whether the parameter was found
290 * @return Rect<int> from the JavaScript function arguments
292 Rect<int> GetRectIntParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
295 * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
296 * @param[out] found whether the parameter was found
297 * @return Actor from the JavaScript function arguments
299 Actor GetActorParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
302 * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
303 * @param[out] found whether the parameter was found
304 * @return Layer from the JavaScript function arguments
306 Layer GetLayerParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
309 * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
310 * @param[out] found whether the parameter was found
311 * @return Image from the JavaScript function arguments
313 Image GetImageParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
316 * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
317 * @param[out] found whether the parameter was found
318 * @return RenderTask from the JavaScript function arguments
320 RenderTask GetRenderTaskParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
323 * Extract a DALi wrapped object, from a JavaScript object held in a function argument
324 * @param[in] index argument index the object is held in
325 * @param[in] type type of wrapped object to extract
327 BaseWrappedObject* GetWrappedDaliObjectParameter( unsigned int index, BaseWrappedObject::Type type, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
330 * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
331 * @param[out] found whether the parameter was found
332 * @return ProperyValue from the JavaScript function arguments
334 Property::Value GetPropertyValueParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
337 * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2).
338 * @param[out] found whether the parameter was found
339 * @return ProperyMap from the JavaScript function arguments
341 Property::Map GetPropertyMapParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );
344 * Generate a JavaScript property map, from a DALi property map
345 * @param [in] DALi map property map
346 * @param [in] JavaScript property map
348 void CreatePropertyMap( v8::Isolate* isolate, const Property::Map& map, v8::Local<v8::Object>& object );
352 * Read multiple float arguments from v8 args object
353 * e.g. myJavaFunc( 3.4, 7.6, 8.5 );
355 * @param[out] foundAllArguments flag to say all arguments are found
356 * @param[out] data pointer to a pre-allocated array of floats
357 * @param[in] dataSize the nunber of floats in the data parameter
358 * @param[in] defaultValue default value assigned to floats that don't exist in the arguments
360 void ReadFloatArguments( bool& foundAllArguments, float* data, unsigned int dataSize, const v8::FunctionCallbackInfo< v8::Value >& args, float defaultValue );
363 * Read multiple integer arguments from v8 args object
364 * e.g. myJavaFunc( 3, 7, 8 );
366 * @param[out] foundAllArguments flag to say all arguments are found
367 * @param[out] data pointer to a pre-allocated array of integers
368 * @param[in] dataSize the nunber of integers in the data parameter
369 * @param[in] defaultValue default value assigned to integers that don't exist in the arguments
371 void ReadIntegerArguments( bool& foundAllArguments, int* data, int dataSize, const v8::FunctionCallbackInfo< v8::Value >& args, int defaultValue );
373 } // namespace V8Utils
375 } // namespace V8Plugin
380 #endif // __DALI_V8PLUGIN_V8_UTILS_H__