00f36255b1c6373704af62be6df1abad97220fef
[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    * Constructor
68    */
69   ModuleLoader();
70
71   /**
72    * non virtual destructor, not intended as a base class
73    */
74   ~ModuleLoader();
75
76
77   /**
78    * Execute a script from a file
79    * @param[in] fileName file name
80    */
81   bool ExecuteScriptFromFile( v8::Isolate* isolate, const std::string& fileName );
82
83
84   /**
85    * Execute a script
86    * @param[in] sourceCode source code to run
87    * @param[in] sourceFileName source file name
88    */
89   bool ExecuteScript( v8::Isolate* isolate,
90                       const std::string& sourceCode,
91                       const std::string& sourceFileName );
92
93
94   /**
95    * Implements JavaScript Require functionality
96    */
97   void Require(const v8::FunctionCallbackInfo< v8::Value >& args, v8::Persistent<v8::ObjectTemplate>& globalObjectTemplate );
98
99 private:
100
101   /**
102    * Compile and run the JavaScript code
103    * @param[in] sourceCode source code to run
104    * @param[in] sourceFileName source file name
105    */
106   bool CompileAndRun(v8::Isolate* isolate,
107                      const std::string& sourceCode,
108                      const std::string& sourceFileName );
109
110   /**
111    * Store information about the current script
112    * @param[in] sourceFileName source file name
113    */
114   void StoreScriptInfo( const std::string& sourceFileName );
115
116   /**
117    * Store module information
118    * @param[in] sourceFileName source file name
119    * @return module object
120    */
121   Module* StoreModule( const std::string& path,
122                     const std::string& fileName,
123                     const std::string& moduleName,
124                     v8::Isolate* isolate,
125                     v8::Local<v8::Object>& moduleExportsObject );
126
127   /**
128    * Find a module
129    * @param[in] moduleName module name
130    * @return module
131    */
132   const Module* FindModule( const std::string& moduleName );
133
134 private:
135
136   Dali::Vector< Module*> mModules; ///< vector of modules
137   std::string mCurrentScriptPath;  ///< path of the current script being executed (via ExecuteScript).
138
139 };
140
141
142 } // namespace V8Plugin
143
144 } // namespace Dali
145
146 #endif // header MODULE_LOADER