Merge "replaced toolkit pushbutton images with shorter images." into devel/master
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / dali-wrapper.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "dali-wrapper.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <object/property-value-wrapper.h>
24 #include <dali/integration-api/debug.h>
25 #include <actors/actor-wrapper.h>
26 #include <stage/stage-wrapper.h>
27 #include <image/image-wrapper.h>
28 #include <animation/linear-constrainer-wrapper.h>
29 #include <animation/path-constrainer-wrapper.h>
30 #include <animation/path-wrapper.h>
31 #include <animation/animation-wrapper.h>
32 #include <events/pan-gesture-detector-wrapper.h>
33 #include <shader-effects/shader-effect-wrapper.h>
34 #include <shared/object-template-helper.h>
35 #include <constants/constants-wrapper.h>
36 #include <toolkit/builder/builder-wrapper.h>
37 #include <toolkit/focus-manager/keyboard-focus-manager-wrapper.h>
38
39
40 namespace Dali
41 {
42
43 namespace V8Plugin
44 {
45
46 namespace
47 {
48 /**
49  * This string defines how the global DALi object/namespace is used from JavaScript
50  * E.g. new dali.Image or dali.stage.add( )
51  */
52 const char* const DALI_API_NAME = "dali";
53
54 /**
55  * lookup table for setting up function calls for creating Dali objects
56  * e.g.  new dali.TextActor()
57  */
58 const ApiFunction ConstructorFunctionTable[]=
59 {
60     { "Rotation",           PropertyValueWrapper::NewRotation},
61     { "Matrix",             PropertyValueWrapper::NewMatrix},
62     { "Path",               PathWrapper::NewPath },
63     { "PathConstrainer",    PathConstrainerWrapper::NewPathConstrainer},
64     { "LinearConstrainer",  LinearConstrainerWrapper::NewLinearConstrainer},
65     { "Actor",              ActorWrapper::NewActor },
66     { "TextActor",          ActorWrapper::NewActor },
67     { "ImageActor",         ActorWrapper::NewActor },
68     { "MeshActor",          ActorWrapper::NewActor },
69     { "CameraActor",        ActorWrapper::NewActor },
70     { "Layer",              ActorWrapper::NewActor },
71     { "Control",            ActorWrapper::NewControl },
72     { "ResourceImage",      ImageWrapper::NewImage },
73     { "BufferImage",        ImageWrapper::NewImage },
74     { "NinePatchImage",     ImageWrapper::NewImage },
75     { "FrameBufferImage",   ImageWrapper::NewImage },
76     { "Animation",          AnimationWrapper::NewAnimation},
77     { "ShaderEffect",       ShaderEffectWrapper::NewShaderEffect},
78     { "Builder",            BuilderWrapper::NewBuilder},
79     { "PanGestureDetector", PanGestureDetectorWrapper::NewPanGestureDetector},
80
81 };
82
83 const unsigned int PropertyFunctionTableCount = sizeof(ConstructorFunctionTable)/sizeof(ConstructorFunctionTable[0]);
84
85 void FatalErrorCallback(const char* location, const char* message)
86 {
87   DALI_LOG_ERROR("%s, %s \n",location,message);
88   DALI_ASSERT_ALWAYS( 0 && "V8 fatal error");
89 }
90
91 #if defined(DEBUG_ENABLED)
92 // default to verbose logging
93 Integration::Log::Filter* gLogExecuteFilter( Integration::Log::Filter::New(Debug::Verbose, false, "EXECUTE_JAVASCRIPT") );
94 #endif
95 } // un-named name space
96
97
98
99 bool DaliWrapper::mInstanceCreated = false;
100 DaliWrapper* DaliWrapper::mWrapper = NULL;
101
102 DaliWrapper::DaliWrapper( RunMode runMode, v8::Isolate* isolate )
103 :mIsolate( isolate ),
104  mRunMode(runMode)
105 {
106 }
107
108 DaliWrapper::~DaliWrapper()
109 {
110   mInstanceCreated = false;
111 }
112
113 DaliWrapper& DaliWrapper::Get()
114 {
115   if( !mInstanceCreated )
116   {
117     mWrapper = new DaliWrapper( RUNNING_STANDALONE, NULL );
118
119     mInstanceCreated = true;
120
121     mWrapper->InitializeStandAlone();
122
123   }
124   return *mWrapper;
125 }
126
127 v8::Local<v8::Object> DaliWrapper::CreateWrapperForNodeJS( v8::Isolate* isolate )
128 {
129   v8::EscapableHandleScope handleScope( isolate);
130
131   mInstanceCreated = true;
132
133   mWrapper = new DaliWrapper( RUNNING_IN_NODE_JS, isolate );
134
135   v8::Local<v8::Object> dali = mWrapper->CreateDaliObject();
136
137   // As we running inside node, we already have an isolate and context
138   return handleScope.Escape( dali );
139 }
140
141 v8::Local<v8::Object>  DaliWrapper::CreateDaliObject()
142 {
143   v8::EscapableHandleScope handleScope( mIsolate  );
144
145   // Create dali object used for creating objects, and accessing constant values
146   // e.g. var x =  new dali.Actor(), or var col = dali.COLOR_RED;
147
148   v8::Local<v8::ObjectTemplate> daliObjectTemplate = NewDaliObjectTemplate( mIsolate );
149
150   // add dali.staqe
151   v8::Local<v8::Object> stageObject = StageWrapper::WrapStage( mIsolate, Stage::GetCurrent() );
152   daliObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "stage") , stageObject );
153
154   v8::Local<v8::Object> keyboardObject = KeyboardFocusManagerWrapper::WrapKeyboardFocusManager( mIsolate,Toolkit::KeyboardFocusManager::Get() );
155   daliObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "keyboardFocusManager") , keyboardObject );
156
157
158   //create an instance of the template
159   v8::Local<v8::Object> daliObject = daliObjectTemplate->NewInstance();
160
161   ConstantsWrapper::AddDaliConstants( mIsolate, daliObject);
162
163   daliObject->Set( v8::String::NewFromUtf8( mIsolate,  "V8_VERSION") ,v8::String::NewFromUtf8( mIsolate, v8::V8::GetVersion() ));
164
165   return handleScope.Escape( daliObject  );
166 }
167
168
169 void DaliWrapper::SetFlagsFromString(const std::string &flags)
170 {
171   v8::V8::SetFlagsFromString(flags.c_str(), flags.size());
172 }
173
174 void DaliWrapper::Shutdown()
175 {
176   // if we're running inside node then we don't have ownership of the context
177   if( mRunMode == RUNNING_IN_NODE_JS )
178   {
179     return;
180   }
181
182   DALI_LOG_WARNING("Destroying V8 DALi context\n");
183
184   if( !mContext.IsEmpty())
185   {
186     v8::HandleScope handleScope( mIsolate );
187     v8::Local<v8::Context> context = v8::Local<v8::Context>::New(mIsolate, mContext);
188     context->Exit();   // exit the context
189     mContext.Reset();  // destroys the context
190   }
191 }
192
193 bool DaliWrapper::ExecuteBuffer(const std::string &sourceCode, const std::string &sourceFileName)
194 {
195   return mModuleLoader.ExecuteScript( mIsolate,  sourceCode, sourceFileName );
196 }
197
198 bool DaliWrapper::ExecuteFile( const std::string& sourceFileName )
199 {
200   DALI_LOG_INFO( gLogExecuteFilter, Debug::Verbose, "Executing source file %s \n",sourceFileName.c_str() );
201
202   return mModuleLoader.ExecuteScriptFromFile( mIsolate,  sourceFileName );
203 }
204
205 GarbageCollectorInterface& DaliWrapper::GetDaliGarbageCollector()
206 {
207   return mGarbageCollector;
208 }
209
210 void DaliWrapper::ApplyGlobalObjectsToContext( v8::Local<v8::Context> context )
211 {
212   v8::HandleScope handleScope( mIsolate );
213
214   // Add global objects ( functions/ values ) e.g. log function
215   // create a console.log and console.error functions
216   v8::Local<v8::ObjectTemplate> consoleObjectTemplate = v8::ObjectTemplate::New( mIsolate );
217   consoleObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "log"),   v8::FunctionTemplate::New( mIsolate, V8Utils::Log));
218   consoleObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "error"), v8::FunctionTemplate::New( mIsolate, V8Utils::LogError));
219
220   context->Global()->Set( v8::String::NewFromUtf8( mIsolate, "console"), consoleObjectTemplate->NewInstance() );
221
222   // add require functionality
223   context->Global()->Set( v8::String::NewFromUtf8( mIsolate, "require"), v8::FunctionTemplate::New( mIsolate, DaliWrapper::Require)->GetFunction());
224
225   // Create the Dali object
226   // @todo consider forcing developers to perform require('dali') if we want to avoid polluting the global namespace
227   v8::Local<v8::Object> daliObject = CreateDaliObject();
228
229   // allow developers to require('dali'); // this is to maintain compatibility with node.js where dali is not part of the global namespace
230   mModuleLoader.StorePreBuiltModule( mIsolate, daliObject, DALI_API_NAME );
231
232   context->Global()->Set( v8::String::NewFromUtf8( mIsolate, DALI_API_NAME),daliObject );
233
234 }
235
236 void DaliWrapper::InitializeStandAlone()
237 {
238   if( !mIsolate )
239   {
240     v8::V8::InitializeICU();
241
242     v8::V8::Initialize();
243
244     // default isolate removed from V8 version 3.27.1 and beyond.
245     mIsolate = v8::Isolate::New();
246
247     mIsolate->Enter();
248
249     v8::V8::SetFatalErrorHandler( FatalErrorCallback );
250   }
251
252   // if context is null, create it and add dali object to the global object.
253   if( mContext.IsEmpty())
254   {
255      v8::HandleScope handleScope( mIsolate );
256
257      // create a new context.
258      // Isolate = isolated copy of the V8 including a heap manager, a garbage collector
259      // Only 1 thread can access a single Isolate at a given time. However, multiple Isolates can be run in parallel.
260      // Context = multiple contexts can exist in a given Isolate, and share data between contexts
261      v8::Local<v8::Context> context  = v8::Context::New( mIsolate );
262
263      context->Enter();
264
265      // Apply global objects like dali and console to the context
266      ApplyGlobalObjectsToContext(context);
267
268      mContext.Reset( mIsolate, context);
269   }
270
271   DALI_LOG_INFO( gLogExecuteFilter, Debug::Verbose, "V8 Library %s loaded \n", v8::V8::GetVersion() );
272 }
273
274
275 v8::Handle<v8::ObjectTemplate> DaliWrapper::NewDaliObjectTemplate( v8::Isolate* isolate )
276 {
277   v8::EscapableHandleScope handleScope( isolate );
278
279   // create the template
280   v8::Local< v8::ObjectTemplate > objTemplate = v8::ObjectTemplate::New( isolate );
281
282   // Add some value properties ( a property can be a primitive value, an object or a function).
283   objTemplate->Set( v8::String::NewFromUtf8( isolate, "BUILD"),
284                     v8::String::NewFromUtf8( isolate, "Dali binary built on:" __DATE__ ", at " __TIME__));
285
286 #ifdef DALI_DATA_READ_ONLY_DIR
287   // add the data data directory,
288   objTemplate->Set( v8::String::NewFromUtf8( isolate, "DALI_DATA_DIRECTORY"),
289                     v8::String::NewFromUtf8( isolate, DALI_DATA_READ_ONLY_DIR));
290 #endif
291   // add our constructor functions
292   ObjectTemplateHelper::InstallFunctions( isolate,
293                                           objTemplate,
294                                           ConstructorFunctionTable,
295                                           PropertyFunctionTableCount,
296                                           ObjectTemplateHelper::CONSTRUCTOR_FUNCTIONS);
297
298   return handleScope.Escape( objTemplate );
299 }
300
301 void DaliWrapper::Require(const v8::FunctionCallbackInfo< v8::Value >& args)
302 {
303   DaliWrapper& wrapper( DaliWrapper::Get() );
304   wrapper.mModuleLoader.Require( args );
305 }
306
307
308 } // namespace V8Plugin
309
310 } // namespace Dali