Merge "Fix for TextField clipping." into devel/master
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / dali-wrapper.h
1 #ifndef __DALI_V8PLUGIN_DALI_WRAP_H__
2 #define __DALI_V8PLUGIN_DALI_WRAP_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 <dali/public-api/common/dali-common.h>
24
25 // INTERNAL INCLUDES
26 #include <garbage-collector/garbage-collector.h>
27
28 #include <module-loader/module-loader.h>
29
30 namespace Dali
31 {
32
33 namespace V8Plugin
34 {
35
36 /**
37  * Sets up JavaScript context and environment and allows execution of JavaScript in the context
38  *
39  *
40  * Exceptions
41  * ----------
42  *
43  * Execptions can be raised by calling DALI_SCRIPT_EXCEPTION()
44  *
45  * Expections break JavaScript execution with a printout but do not cause a c++ exception.
46  * So always return from the calling function before attempting to run any further V8 code.
47  *
48  * Coding Style
49  * ------------
50  *
51  *  Uses Tizen Web API coding style, which appears to be the same as this:
52  *
53  *  https://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
54  *
55  * "In general, use
56  *
57  * functionNamesLikeThis,
58  * variableNamesLikeThis,
59  * ClassNamesLikeThis,
60  * ConstructorsLikeThis,
61  * EnumNamesLikeThis,
62  * methodNamesLikeThis,
63  * CONSTANT_VALUES_LIKE_THIS,
64  * foo.namespaceNamesLikeThis.bar,
65  * filenameslikethis.js. "
66  */
67 class DALI_INTERNAL DaliWrapper
68 {
69
70 public:
71
72   /**
73    * @brief whether the wrapper is running in standalone using V8,
74    * or inside Node.JS using V8
75    */
76   enum RunMode
77   {
78     RUNNING_STANDALONE,
79     RUNNING_IN_NODE_JS
80   };
81
82   /**
83    * @brief Constructor
84    * @param[in] runMode whether the wrapper is running standalone or inside Node.JS
85    * @param[in] isolate v8 isolate ( can be null if running standalone )
86    */
87   DaliWrapper( RunMode runMode, v8::Isolate* isolate );
88
89   /**
90    * non virtual destructor, not intended as a base class
91    */
92   ~DaliWrapper();
93
94   /**
95    * Gets the DaliWrapper singleton.
96    *
97    * @return the wrapper
98    */
99   static DaliWrapper& Get();
100
101   /**
102    * Intialize DaliWrapper for running inside NodeJS
103    */
104   static v8::Local<v8::Object> CreateWrapperForNodeJS( v8::Isolate* isolate);
105
106
107   /**
108    * Set V8 engine configuration flags
109    *
110    * @param[in] flags Configruation flags (See v8 documentation)
111    */
112   void SetFlagsFromString(const std::string& flags);
113
114   /**
115    * Delete the current V8 context
116    */
117   void Shutdown();
118
119   /**
120    * Excute the buffer in the v8 context
121    *
122    * @param[in] sourceCode The buffer containing javascript to execute
123    * @param[in] sourceFileName Filename associated with the buffer (for error tracing)
124    * @return true on success, false on failure
125    */
126   bool ExecuteBuffer(const std::string &sourceCode, const std::string &sourceFileName);
127
128
129   /**
130    * Excute the buffer in the v8 context
131    *
132    * @param[in] sourceFileName Filename associated with the buffer (for error tracing)
133    * @return true on success, false on failure
134    */
135   bool ExecuteFile( const std::string& sourceFileName);
136
137
138   /**
139    * Get DALi's own garbage collector
140    * @return garbage collector interface
141    */
142   GarbageCollectorInterface& GetDaliGarbageCollector();
143
144 private:
145
146   /**
147    * @brief Apply global objects like console.log and require() to the context
148    */
149   void ApplyGlobalObjectsToContext( v8::Local<v8::Context> context );
150
151   /**
152    * @brief Initialize DaliWrapper for running standalone
153    * Creates a new isolate
154    *
155    */
156   void InitializeStandAlone();
157
158   /**
159    * @brief create dali namespace/object
160    */
161   v8::Local<v8::Object> CreateDaliObject();
162
163   /**
164    * Create Dali ObjectTemplate
165    * @return the Dali ObjectTemplate
166    */
167   static v8::Handle<v8::ObjectTemplate> NewDaliObjectTemplate( v8::Isolate* isolate );
168
169   /**
170    * Called when require keyword is found in a script
171    */
172   static void Require(const v8::FunctionCallbackInfo< v8::Value >& args);
173
174   static bool mInstanceCreated;                                 ///< whether an instance has been created
175   static DaliWrapper* mWrapper;                                 ///< static pointer to the wrapper
176
177   GarbageCollector mGarbageCollector;                           ///< DALi garbage collector
178   ModuleLoader mModuleLoader;                                   ///< Module loader
179   v8::Persistent<v8::Context> mContext;                         ///< A sandboxed execution context with its own set of built-in objects and functions.
180   v8::Isolate* mIsolate;                                        ///< represents an isolated instance of the V8 engine.
181   RunMode mRunMode;
182 };
183
184
185 } // namespace V8Plugin
186
187 } // namespace Dali
188
189 #endif // header