Removed GetStretchBorders from NinePatchImage plugin API.
[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     { "ImageActor",         ActorWrapper::NewActor },
67     { "CameraActor",        ActorWrapper::NewActor },
68     { "Layer",              ActorWrapper::NewActor },
69     { "Control",            ActorWrapper::NewControl },
70     { "ResourceImage",      ImageWrapper::NewImage },
71     { "BufferImage",        ImageWrapper::NewImage },
72     { "NinePatchImage",     ImageWrapper::NewImage },
73     { "FrameBufferImage",   ImageWrapper::NewImage },
74     { "Animation",          AnimationWrapper::NewAnimation},
75     { "ShaderEffect",       ShaderEffectWrapper::NewShaderEffect},
76     { "Builder",            BuilderWrapper::NewBuilder},
77     { "PanGestureDetector", PanGestureDetectorWrapper::NewPanGestureDetector},
78
79 };
80
81 const unsigned int PropertyFunctionTableCount = sizeof(ConstructorFunctionTable)/sizeof(ConstructorFunctionTable[0]);
82
83 void FatalErrorCallback(const char* location, const char* message)
84 {
85   DALI_LOG_ERROR("%s, %s \n",location,message);
86   DALI_ASSERT_ALWAYS( 0 && "V8 fatal error");
87 }
88
89 #if defined(DEBUG_ENABLED)
90 // default to verbose logging
91 Integration::Log::Filter* gLogExecuteFilter( Integration::Log::Filter::New(Debug::Verbose, false, "EXECUTE_JAVASCRIPT") );
92 #endif
93 } // un-named name space
94
95
96
97 bool DaliWrapper::mInstanceCreated = false;
98 DaliWrapper* DaliWrapper::mWrapper = NULL;
99
100 DaliWrapper::DaliWrapper( RunMode runMode, v8::Isolate* isolate )
101 :mIsolate( isolate ),
102  mRunMode(runMode)
103 {
104 }
105
106 DaliWrapper::~DaliWrapper()
107 {
108   mInstanceCreated = false;
109 }
110
111 DaliWrapper& DaliWrapper::Get()
112 {
113   if( !mInstanceCreated )
114   {
115     mWrapper = new DaliWrapper( RUNNING_STANDALONE, NULL );
116
117     mInstanceCreated = true;
118
119     mWrapper->InitializeStandAlone();
120
121   }
122   return *mWrapper;
123 }
124
125 v8::Local<v8::Object> DaliWrapper::CreateWrapperForNodeJS( v8::Isolate* isolate )
126 {
127   v8::EscapableHandleScope handleScope( isolate);
128
129   mInstanceCreated = true;
130
131   mWrapper = new DaliWrapper( RUNNING_IN_NODE_JS, isolate );
132
133   v8::Local<v8::Object> dali = mWrapper->CreateDaliObject();
134
135   // As we running inside node, we already have an isolate and context
136   return handleScope.Escape( dali );
137 }
138
139 v8::Local<v8::Object>  DaliWrapper::CreateDaliObject()
140 {
141   v8::EscapableHandleScope handleScope( mIsolate  );
142
143   // Create dali object used for creating objects, and accessing constant values
144   // e.g. var x =  new dali.Actor(), or var col = dali.COLOR_RED;
145
146   v8::Local<v8::ObjectTemplate> daliObjectTemplate = NewDaliObjectTemplate( mIsolate );
147
148   // add dali.staqe
149   v8::Local<v8::Object> stageObject = StageWrapper::WrapStage( mIsolate, Stage::GetCurrent() );
150   daliObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "stage") , stageObject );
151
152   v8::Local<v8::Object> keyboardObject = KeyboardFocusManagerWrapper::WrapKeyboardFocusManager( mIsolate,Toolkit::KeyboardFocusManager::Get() );
153   daliObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "keyboardFocusManager") , keyboardObject );
154
155
156   //create an instance of the template
157   v8::Local<v8::Object> daliObject = daliObjectTemplate->NewInstance();
158
159   ConstantsWrapper::AddDaliConstants( mIsolate, daliObject);
160
161   daliObject->Set( v8::String::NewFromUtf8( mIsolate,  "V8_VERSION") ,v8::String::NewFromUtf8( mIsolate, v8::V8::GetVersion() ));
162
163   return handleScope.Escape( daliObject  );
164 }
165
166
167 void DaliWrapper::SetFlagsFromString(const std::string &flags)
168 {
169   v8::V8::SetFlagsFromString(flags.c_str(), flags.size());
170 }
171
172 void DaliWrapper::Shutdown()
173 {
174   // if we're running inside node then we don't have ownership of the context
175   if( mRunMode == RUNNING_IN_NODE_JS )
176   {
177     return;
178   }
179
180   DALI_LOG_WARNING("Destroying V8 DALi context\n");
181
182   if( !mContext.IsEmpty())
183   {
184     v8::HandleScope handleScope( mIsolate );
185     v8::Local<v8::Context> context = v8::Local<v8::Context>::New(mIsolate, mContext);
186     context->Exit();   // exit the context
187     mContext.Reset();  // destroys the context
188   }
189 }
190
191 bool DaliWrapper::ExecuteBuffer(const std::string &sourceCode, const std::string &sourceFileName)
192 {
193   return mModuleLoader.ExecuteScript( mIsolate,  sourceCode, sourceFileName );
194 }
195
196 bool DaliWrapper::ExecuteFile( const std::string& sourceFileName )
197 {
198   DALI_LOG_INFO( gLogExecuteFilter, Debug::Verbose, "Executing source file %s \n",sourceFileName.c_str() );
199
200   return mModuleLoader.ExecuteScriptFromFile( mIsolate,  sourceFileName );
201 }
202
203 GarbageCollectorInterface& DaliWrapper::GetDaliGarbageCollector()
204 {
205   return mGarbageCollector;
206 }
207
208 void DaliWrapper::ApplyGlobalObjectsToContext( v8::Local<v8::Context> context )
209 {
210   v8::HandleScope handleScope( mIsolate );
211
212   // Add global objects ( functions/ values ) e.g. log function
213   // create a console.log and console.error functions
214   v8::Local<v8::ObjectTemplate> consoleObjectTemplate = v8::ObjectTemplate::New( mIsolate );
215   consoleObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "log"),   v8::FunctionTemplate::New( mIsolate, V8Utils::Log));
216   consoleObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "error"), v8::FunctionTemplate::New( mIsolate, V8Utils::LogError));
217
218   context->Global()->Set( v8::String::NewFromUtf8( mIsolate, "console"), consoleObjectTemplate->NewInstance() );
219
220   // add require functionality
221   context->Global()->Set( v8::String::NewFromUtf8( mIsolate, "require"), v8::FunctionTemplate::New( mIsolate, DaliWrapper::Require)->GetFunction());
222
223   // Create the Dali object
224   // @todo consider forcing developers to perform require('dali') if we want to avoid polluting the global namespace
225   v8::Local<v8::Object> daliObject = CreateDaliObject();
226
227   // allow developers to require('dali'); // this is to maintain compatibility with node.js where dali is not part of the global namespace
228   mModuleLoader.StorePreBuiltModule( mIsolate, daliObject, DALI_API_NAME );
229
230   context->Global()->Set( v8::String::NewFromUtf8( mIsolate, DALI_API_NAME),daliObject );
231
232 }
233
234 void DaliWrapper::InitializeStandAlone()
235 {
236   if( !mIsolate )
237   {
238     v8::V8::InitializeICU();
239
240     v8::V8::Initialize();
241
242     // default isolate removed from V8 version 3.27.1 and beyond.
243     mIsolate = v8::Isolate::New();
244
245     mIsolate->Enter();
246
247     v8::V8::SetFatalErrorHandler( FatalErrorCallback );
248   }
249
250   // if context is null, create it and add dali object to the global object.
251   if( mContext.IsEmpty())
252   {
253      v8::HandleScope handleScope( mIsolate );
254
255      // create a new context.
256      // Isolate = isolated copy of the V8 including a heap manager, a garbage collector
257      // Only 1 thread can access a single Isolate at a given time. However, multiple Isolates can be run in parallel.
258      // Context = multiple contexts can exist in a given Isolate, and share data between contexts
259      v8::Local<v8::Context> context  = v8::Context::New( mIsolate );
260
261      context->Enter();
262
263      // Apply global objects like dali and console to the context
264      ApplyGlobalObjectsToContext(context);
265
266      mContext.Reset( mIsolate, context);
267   }
268
269   DALI_LOG_INFO( gLogExecuteFilter, Debug::Verbose, "V8 Library %s loaded \n", v8::V8::GetVersion() );
270 }
271
272
273 v8::Handle<v8::ObjectTemplate> DaliWrapper::NewDaliObjectTemplate( v8::Isolate* isolate )
274 {
275   v8::EscapableHandleScope handleScope( isolate );
276
277   // create the template
278   v8::Local< v8::ObjectTemplate > objTemplate = v8::ObjectTemplate::New( isolate );
279
280   // Add some value properties ( a property can be a primitive value, an object or a function).
281   objTemplate->Set( v8::String::NewFromUtf8( isolate, "BUILD"),
282                     v8::String::NewFromUtf8( isolate, "Dali binary built on:" __DATE__ ", at " __TIME__));
283
284 #ifdef DALI_DATA_READ_ONLY_DIR
285   // add the data data directory,
286   objTemplate->Set( v8::String::NewFromUtf8( isolate, "DALI_DATA_DIRECTORY"),
287                     v8::String::NewFromUtf8( isolate, DALI_DATA_READ_ONLY_DIR));
288 #endif
289   // add our constructor functions
290   ObjectTemplateHelper::InstallFunctions( isolate,
291                                           objTemplate,
292                                           ConstructorFunctionTable,
293                                           PropertyFunctionTableCount,
294                                           ObjectTemplateHelper::CONSTRUCTOR_FUNCTIONS);
295
296   return handleScope.Escape( objTemplate );
297 }
298
299 void DaliWrapper::Require(const v8::FunctionCallbackInfo< v8::Value >& args)
300 {
301   DaliWrapper& wrapper( DaliWrapper::Get() );
302   wrapper.mModuleLoader.Require( args );
303 }
304
305
306 } // namespace V8Plugin
307
308 } // namespace Dali