Script UTC test cases
[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    * @return true on success, false on failure
105    */
106   bool ExecuteBuffer(const std::string &sourceCode, const std::string &sourceFileName);
107
108
109   /**
110    * Excute the buffer in the v8 context
111    *
112    * @param[in] sourceFileName Filename associated with the buffer (for error tracing)
113    * @return true on success, false on failure
114    */
115   bool ExecuteFile( const std::string& sourceFileName);
116
117
118   /**
119    * Get DALi's own garbage collector
120    * @return garbage collector interface
121    */
122   GarbageCollectorInterface& GetDaliGarbageCollector();
123
124 private:
125
126   /**
127    * Create V8 context
128    */
129   void CreateContext();
130
131   /**
132    * Initialize DaliWrapper
133    */
134   void Initialize();
135
136   /**
137    * Create Dali ObjectTemplate
138    * @return the Dali ObjectTemplate
139    */
140   static v8::Handle<v8::ObjectTemplate> NewDaliObjectTemplate( v8::Isolate* isolate );
141
142   /**
143    * Called when require keyword is found in a script
144    */
145   static void Require(const v8::FunctionCallbackInfo< v8::Value >& args);
146
147   static bool mInstanceCreated;                                 ///< whether an instance has been created
148   static DaliWrapper* mWrapper;                                 ///< static pointer to the wrapper
149
150   GarbageCollector mGarbageCollector;                           ///< DALi garbage collector
151   ModuleLoader mModuleLoader;                                   ///< Module loader
152   v8::Persistent<v8::Context> mContext;                         ///< A sandboxed execution context with its own set of built-in objects and functions.
153   v8::Persistent<v8::ObjectTemplate> mGlobalObjectTemplate;     ///< Global object template for storing things like dali global object
154   v8::Isolate* mIsolate;                                        ///< represents an isolated instance of the V8 engine.
155
156 };
157
158
159 } // namespace V8Plugin
160
161 } // namespace Dali
162
163 #endif // header