Merge "Fix for TextField clipping." into devel/master
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / module-loader / module-loader.h
1 #ifndef __DALI_V8PLUGIN_MODULE_LOADER_H__
2 #define __DALI_V8PLUGIN_MODULE_LOADER_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 <v8.h>
23 #include <string.h>
24 #include <dali/public-api/common/dali-vector.h>
25
26 // INTERNAL INCLUDES
27 #include <module-loader/module.h>
28
29 namespace Dali
30 {
31
32 namespace V8Plugin
33 {
34
35 /**
36  *
37  * Responsible for executing JavaScript source code.
38  * This includes loading other JavaScript files that may be referenced
39  * inside JavaScript using the 'require' keyword.
40  *
41  * Notes:
42  *
43  * A module named with a forward slash is loaded as an absolute path:
44  * E.g. require "/usr/apps/scripts/my_module.js"
45  *
46  * A module pre-fixed with a dot slash ( current directory) then the path is relative
47  * to the calling scripts path. E.g.
48  *
49  * -- my_first.js --
50  * require "./circle.js";   // load circle.js from same directory as my_first.js
51  *
52  * @TODO
53  * ----------
54  * Without a leading '/' or './' the module is loaded from the DALi modules directory or
55  * the applications user defined module directory.
56  * E.g. require "dali-spline.js"    // search dali-module directory first, then any user
57  * defined module locations.
58  * ------------
59  *
60  *
61  */
62 class ModuleLoader
63 {
64 public:
65
66   /**
67    * @brief Constructor
68    * @param[in] isolate v8 isolate
69    * @param[in] daliObject dali exports object, used when developer does require('dali');
70    */
71   ModuleLoader();
72
73   /**
74    * @brief non virtual destructor, not intended as a base class
75    */
76   ~ModuleLoader();
77
78
79   /**
80    * @brief Execute a script from a file
81    * @param[in] isolate v8 isolate
82    * @param[in] fileName file name
83    * @return true on success, false on failure
84    *
85    */
86   bool ExecuteScriptFromFile( v8::Isolate* isolate, const std::string& fileName );
87
88
89   /**
90    * @brief Execute a script
91    * @param[in] isolate v8 isolate
92    * @param[in] sourceCode source code to run
93    * @param[in] sourceFileName source file name
94    * @return true on success, false on failure
95    */
96   bool ExecuteScript( v8::Isolate* isolate,
97                       const std::string& sourceCode,
98                       const std::string& sourceFileName );
99
100
101   /**
102    * @brief Implements JavaScript Require functionality
103    * @param[in] args arguments passed to require. The return value is set using   args.GetReturnValue().Set(
104    */
105   void Require( const v8::FunctionCallbackInfo< v8::Value >& args );
106
107   /**
108    * @brief
109    * Stores a pre compiled object as a module.
110    * Currently used for storing the Dali object, so the developer can
111    * perform  var dali = require('dali');
112    * @param[in] isolate v8 isolate
113    * @param[in] exportObject export object
114    * @param[in] name module name, used for the require('name') lookup
115    */
116   void StorePreBuiltModule( v8::Isolate* isolate, v8::Local<v8::Object>& exportObject, const std::string& name );
117
118 private:
119
120   /**
121    * Compile and run the JavaScript code
122    * @param[in] sourceCode source code to run
123    * @param[in] sourceFileName source file name
124    */
125   bool CompileAndRun(v8::Isolate* isolate,
126                      const std::string& sourceCode,
127                      const std::string& sourceFileName );
128
129   /**
130    * @brief Store information about the current script
131    * @param[in] sourceFileName source file name
132    */
133   void StoreScriptInfo( const std::string& sourceFileName );
134
135   /**
136    * @brief Store module information
137    * @param[in] sourceFileName source file name
138    * @return module object
139    */
140   Module* StoreModule( const std::string& path,
141                     const std::string& fileName,
142                     const std::string& moduleName,
143                     v8::Isolate* isolate,
144                     v8::Local<v8::Object>& moduleExportsObject );
145
146   /**
147    * @brief Find a module
148    * @param[in] moduleName module name
149    * @return module
150    */
151   const Module* FindModule( const std::string& moduleName );
152
153 private:
154
155   Dali::Vector< Module*> mModules; ///< vector of modules
156   std::string mCurrentScriptPath;  ///< path of the current script being executed (via ExecuteScript).
157
158 };
159
160
161 } // namespace V8Plugin
162
163 } // namespace Dali
164
165 #endif // header MODULE_LOADER