2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "dali-wrapper.h"
23 #include <object/property-value-wrapper.h>
24 #include <dali/integration-api/debug.h>
25 #include <actors/actor-wrapper.h>
26 #include <controls/control-wrapper.h>
27 #include <stage/stage-wrapper.h>
28 #include <image/image-wrapper.h>
29 #include <animation/linear-constrainer-wrapper.h>
30 #include <animation/path-constrainer-wrapper.h>
31 #include <animation/path-wrapper.h>
32 #include <animation/animation-wrapper.h>
33 #include <controls/item-factory-wrapper.h>
34 #include <events/pan-gesture-detector-wrapper.h>
35 #include <object/property-buffer-wrapper.h>
36 #include <rendering/geometry-wrapper.h>
37 #include <rendering/texture-set-wrapper.h>
38 #include <rendering/renderer-wrapper.h>
39 #include <rendering/shader-wrapper.h>
40 #include <rendering/sampler-wrapper.h>
41 #include <shared/object-template-helper.h>
42 #include <constants/constants-wrapper.h>
43 #include <toolkit/builder/builder-wrapper.h>
44 #include <toolkit/focus-manager/keyboard-focus-manager-wrapper.h>
56 * This string defines how the global DALi object/namespace is used from JavaScript
57 * E.g. new dali.Image or dali.stage.add( )
59 const char* const DALI_API_NAME = "dali";
62 * lookup table for setting up function calls for creating Dali objects
63 * e.g. new dali.TextActor()
65 const ApiFunction ConstructorFunctionTable[]=
67 { "Rotation", PropertyValueWrapper::NewRotation},
68 { "Matrix", PropertyValueWrapper::NewMatrix},
69 { "Path", PathWrapper::NewPath },
70 { "PathConstrainer", PathConstrainerWrapper::NewPathConstrainer},
71 { "LinearConstrainer", LinearConstrainerWrapper::NewLinearConstrainer},
72 { "Actor", ActorWrapper::NewActor },
73 { "CameraActor", ActorWrapper::NewActor },
74 { "Layer", ActorWrapper::NewActor },
75 { "Control", ControlWrapper::NewControl },
76 { "ResourceImage", ImageWrapper::NewImage },
77 { "BufferImage", ImageWrapper::NewImage },
78 { "FrameBufferImage", ImageWrapper::NewImage },
79 { "Animation", AnimationWrapper::NewAnimation},
80 { "ItemFactory", ItemFactoryWrapper::NewItemFactory},
81 { "Shader", ShaderWrapper::NewShader},
82 { "Sampler", SamplerWrapper::NewSampler},
83 { "TextureSet", TextureSetWrapper::NewTextureSet},
84 { "Geometry", GeometryWrapper::NewGeometry},
85 { "Renderer", RendererWrapper::NewRenderer},
86 { "PropertyBuffer", PropertyBufferWrapper::NewPropertyBuffer},
87 { "Builder", BuilderWrapper::NewBuilder},
88 { "PanGestureDetector", PanGestureDetectorWrapper::NewPanGestureDetector},
92 const unsigned int PropertyFunctionTableCount = sizeof(ConstructorFunctionTable)/sizeof(ConstructorFunctionTable[0]);
94 void FatalErrorCallback(const char* location, const char* message)
96 DALI_LOG_ERROR("%s, %s \n",location,message);
97 DALI_ASSERT_ALWAYS( 0 && "V8 fatal error");
100 #if defined(DEBUG_ENABLED)
101 // default to verbose logging
102 Integration::Log::Filter* gLogExecuteFilter( Integration::Log::Filter::New(Debug::Verbose, false, "EXECUTE_JAVASCRIPT") );
104 } // un-named name space
108 bool DaliWrapper::mInstanceCreated = false;
109 DaliWrapper* DaliWrapper::mWrapper = NULL;
111 DaliWrapper::DaliWrapper( RunMode runMode, v8::Isolate* isolate )
112 :mIsolate( isolate ),
117 DaliWrapper::~DaliWrapper()
119 mInstanceCreated = false;
122 DaliWrapper& DaliWrapper::Get()
124 if( !mInstanceCreated )
126 mWrapper = new DaliWrapper( RUNNING_STANDALONE, NULL );
128 mInstanceCreated = true;
130 mWrapper->InitializeStandAlone();
136 v8::Local<v8::Object> DaliWrapper::CreateWrapperForNodeJS( v8::Isolate* isolate )
138 v8::EscapableHandleScope handleScope( isolate);
140 mInstanceCreated = true;
142 mWrapper = new DaliWrapper( RUNNING_IN_NODE_JS, isolate );
144 v8::Local<v8::Object> dali = mWrapper->CreateDaliObject();
146 // As we running inside node, we already have an isolate and context
147 return handleScope.Escape( dali );
150 v8::Local<v8::Object> DaliWrapper::CreateDaliObject()
152 v8::EscapableHandleScope handleScope( mIsolate );
154 // Create dali object used for creating objects, and accessing constant values
155 // e.g. var x = new dali.Actor(), or var col = dali.COLOR_RED;
157 v8::Local<v8::ObjectTemplate> daliObjectTemplate = NewDaliObjectTemplate( mIsolate );
160 v8::Local<v8::Object> stageObject = StageWrapper::WrapStage( mIsolate, Stage::GetCurrent() );
161 daliObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "stage") , stageObject );
163 v8::Local<v8::Object> keyboardObject = KeyboardFocusManagerWrapper::WrapKeyboardFocusManager( mIsolate,Toolkit::KeyboardFocusManager::Get() );
164 daliObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "keyboardFocusManager") , keyboardObject );
167 //create an instance of the template
168 v8::Local<v8::Object> daliObject = daliObjectTemplate->NewInstance();
170 ConstantsWrapper::AddDaliConstants( mIsolate, daliObject);
172 daliObject->Set( v8::String::NewFromUtf8( mIsolate, "V8_VERSION") ,v8::String::NewFromUtf8( mIsolate, v8::V8::GetVersion() ));
174 return handleScope.Escape( daliObject );
178 void DaliWrapper::SetFlagsFromString(const std::string &flags)
180 v8::V8::SetFlagsFromString(flags.c_str(), flags.size());
183 void DaliWrapper::Shutdown()
185 // if we're running inside node then we don't have ownership of the context
186 if( mRunMode == RUNNING_IN_NODE_JS )
191 DALI_LOG_WARNING("Destroying V8 DALi context\n");
193 if( !mContext.IsEmpty())
195 v8::HandleScope handleScope( mIsolate );
196 v8::Local<v8::Context> context = v8::Local<v8::Context>::New(mIsolate, mContext);
197 context->Exit(); // exit the context
198 mContext.Reset(); // destroys the context
202 bool DaliWrapper::ExecuteBuffer(const std::string &sourceCode, const std::string &sourceFileName)
204 return mModuleLoader.ExecuteScript( mIsolate, sourceCode, sourceFileName );
207 bool DaliWrapper::ExecuteFile( const std::string& sourceFileName )
209 DALI_LOG_INFO( gLogExecuteFilter, Debug::Verbose, "Executing source file %s \n",sourceFileName.c_str() );
211 return mModuleLoader.ExecuteScriptFromFile( mIsolate, sourceFileName );
214 GarbageCollectorInterface& DaliWrapper::GetDaliGarbageCollector()
216 return mGarbageCollector;
219 void DaliWrapper::ApplyGlobalObjectsToContext( v8::Local<v8::Context> context )
221 v8::HandleScope handleScope( mIsolate );
223 // Add global objects ( functions/ values ) e.g. log function
224 // create a console.log and console.error functions
225 v8::Local<v8::ObjectTemplate> consoleObjectTemplate = v8::ObjectTemplate::New( mIsolate );
226 consoleObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "log"), v8::FunctionTemplate::New( mIsolate, V8Utils::Log));
227 consoleObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "error"), v8::FunctionTemplate::New( mIsolate, V8Utils::LogError));
229 context->Global()->Set( v8::String::NewFromUtf8( mIsolate, "console"), consoleObjectTemplate->NewInstance() );
231 // add require functionality
232 context->Global()->Set( v8::String::NewFromUtf8( mIsolate, "require"), v8::FunctionTemplate::New( mIsolate, DaliWrapper::Require)->GetFunction());
234 // Create the Dali object
235 // @todo consider forcing developers to perform require('dali') if we want to avoid polluting the global namespace
236 v8::Local<v8::Object> daliObject = CreateDaliObject();
238 // allow developers to require('dali'); // this is to maintain compatibility with node.js where dali is not part of the global namespace
239 mModuleLoader.StorePreBuiltModule( mIsolate, daliObject, DALI_API_NAME );
241 context->Global()->Set( v8::String::NewFromUtf8( mIsolate, DALI_API_NAME),daliObject );
245 void DaliWrapper::InitializeStandAlone()
249 v8::V8::InitializeICU();
251 v8::V8::Initialize();
253 // default isolate removed from V8 version 3.27.1 and beyond.
254 mIsolate = v8::Isolate::New();
258 v8::V8::SetFatalErrorHandler( FatalErrorCallback );
261 // if context is null, create it and add dali object to the global object.
262 if( mContext.IsEmpty())
264 v8::HandleScope handleScope( mIsolate );
266 // create a new context.
267 // Isolate = isolated copy of the V8 including a heap manager, a garbage collector
268 // Only 1 thread can access a single Isolate at a given time. However, multiple Isolates can be run in parallel.
269 // Context = multiple contexts can exist in a given Isolate, and share data between contexts
270 v8::Local<v8::Context> context = v8::Context::New( mIsolate );
274 // Apply global objects like dali and console to the context
275 ApplyGlobalObjectsToContext(context);
277 mContext.Reset( mIsolate, context);
280 DALI_LOG_INFO( gLogExecuteFilter, Debug::Verbose, "V8 Library %s loaded \n", v8::V8::GetVersion() );
284 v8::Handle<v8::ObjectTemplate> DaliWrapper::NewDaliObjectTemplate( v8::Isolate* isolate )
286 v8::EscapableHandleScope handleScope( isolate );
288 // create the template
289 v8::Local< v8::ObjectTemplate > objTemplate = v8::ObjectTemplate::New( isolate );
291 // Add some value properties ( a property can be a primitive value, an object or a function).
292 objTemplate->Set( v8::String::NewFromUtf8( isolate, "BUILD"),
293 v8::String::NewFromUtf8( isolate, "Dali binary built on:" __DATE__ ", at " __TIME__));
295 #ifdef DALI_DATA_READ_ONLY_DIR
296 // add the data data directory,
297 objTemplate->Set( v8::String::NewFromUtf8( isolate, "DALI_DATA_DIRECTORY"),
298 v8::String::NewFromUtf8( isolate, DALI_DATA_READ_ONLY_DIR));
300 // add our constructor functions
301 ObjectTemplateHelper::InstallFunctions( isolate,
303 ConstructorFunctionTable,
304 PropertyFunctionTableCount,
305 ObjectTemplateHelper::CONSTRUCTOR_FUNCTIONS);
307 return handleScope.Escape( objTemplate );
310 void DaliWrapper::Require(const v8::FunctionCallbackInfo< v8::Value >& args)
312 DaliWrapper& wrapper( DaliWrapper::Get() );
313 wrapper.mModuleLoader.Require( args );
317 } // namespace V8Plugin