Merge remote-tracking branch 'origin/tizen' into devel/new_mesh
[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 #include <module-loader/module-loader.h>
28
29 namespace Dali
30 {
31
32 namespace V8Plugin
33 {
34
35 /**
36  * Sets up JavaScript context and environment and allows execution of JavaScript in the context
37  *
38  *
39  * Exceptions
40  * ----------
41  *
42  * Execptions can be raised by calling DALI_SCRIPT_EXCEPTION()
43  *
44  * Expections break JavaScript execution with a printout but do not cause a c++ exception.
45  * So always return from the calling function before attempting to run any further V8 code.
46  *
47  * Coding Style
48  * ------------
49  *
50  *  Uses Tizen Web API coding style, which appears to be the same as this:
51  *
52  *  https://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
53  *
54  * "In general, use
55  *
56  * functionNamesLikeThis,
57  * variableNamesLikeThis,
58  * ClassNamesLikeThis,
59  * ConstructorsLikeThis,
60  * EnumNamesLikeThis,
61  * methodNamesLikeThis,
62  * CONSTANT_VALUES_LIKE_THIS,
63  * foo.namespaceNamesLikeThis.bar,
64  * filenameslikethis.js. "
65  */
66 class DALI_INTERNAL DaliWrapper
67 {
68 public:
69
70   /**
71    * Constructor
72    */
73   DaliWrapper();
74
75   /**
76    * non virtual destructor, not intended as a base class
77    */
78   ~DaliWrapper();
79
80   /**
81    * Gets the DaliWrapper singleton.
82    *
83    * @return the wrapper
84    */
85   static DaliWrapper& Get();
86
87   /**
88    * Set V8 engine configuration flags
89    *
90    * @param[in] flags Configruation flags (See v8 documentation)
91    */
92   void SetFlagsFromString(const std::string& flags);
93
94   /**
95    * Delete the current V8 context
96    */
97   void Shutdown();
98
99   /**
100    * Excute the buffer in the v8 context
101    *
102    * @param[in] sourceCode The buffer containing javascript to execute
103    * @param[in] sourceFileName Filename associated with the buffer (for error tracing)
104    */
105   void ExecuteBuffer(const std::string &sourceCode, const std::string &sourceFileName);
106
107
108   /**
109    * Excute the buffer in the v8 context
110    *
111    * @param[in] sourceFileName Filename associated with the buffer (for error tracing)
112    */
113   void ExecuteFile( const std::string& sourceFileName);
114
115
116   /**
117    * Get DALi's own garbage collector
118    * @return garbage collector interface
119    */
120   GarbageCollectorInterface& GetDaliGarbageCollector();
121
122 private:
123
124   /**
125    * Create V8 context
126    */
127   void CreateContext();
128
129   /**
130    * Initialize DaliWrapper
131    */
132   void Initialize();
133
134   /**
135    * Create Dali ObjectTemplate
136    * @return the Dali ObjectTemplate
137    */
138   static v8::Handle<v8::ObjectTemplate> NewDaliObjectTemplate( v8::Isolate* isolate );
139
140   /**
141    * Called when require keyword is found in a script
142    */
143   static void Require(const v8::FunctionCallbackInfo< v8::Value >& args);
144
145   static bool mInstanceCreated;                                 ///< whether an instance has been created
146   static DaliWrapper* mWrapper;                                 ///< static pointer to the wrapper
147
148   GarbageCollector mGarbageCollector;                           ///< DALi garbage collector
149   ModuleLoader mModuleLoader;                                   ///< Module loader
150   v8::Persistent<v8::Context> mContext;                         ///< A sandboxed execution context with its own set of built-in objects and functions.
151   v8::Persistent<v8::ObjectTemplate> mGlobalObjectTemplate;     ///< Global object template for storing things like dali global object
152   v8::Isolate* mIsolate;                                        ///< represents an isolated instance of the V8 engine.
153
154 };
155
156
157 } // namespace V8Plugin
158
159 } // namespace Dali
160
161 #endif // header