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