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 <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>
49 * This string defines how the global DALi object/namespace is used from JavaScript
50 * E.g. new dali.Image or dali.stage.add( )
52 const char* const DALI_API_NAME = "dali";
55 * lookup table for setting up function calls for creating Dali objects
56 * e.g. new dali.TextActor()
58 const ApiFunction ConstructorFunctionTable[]=
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},
83 const unsigned int PropertyFunctionTableCount = sizeof(ConstructorFunctionTable)/sizeof(ConstructorFunctionTable[0]);
85 void FatalErrorCallback(const char* location, const char* message)
87 DALI_LOG_ERROR("%s, %s \n",location,message);
88 DALI_ASSERT_ALWAYS( 0 && "V8 fatal error");
91 #if defined(DEBUG_ENABLED)
92 // default to verbose logging
93 Integration::Log::Filter* gLogExecuteFilter( Integration::Log::Filter::New(Debug::Verbose, false, "EXECUTE_JAVASCRIPT") );
95 } // un-named name space
99 bool DaliWrapper::mInstanceCreated = false;
100 DaliWrapper* DaliWrapper::mWrapper = NULL;
102 DaliWrapper::DaliWrapper( RunMode runMode, v8::Isolate* isolate )
103 :mIsolate( isolate ),
108 DaliWrapper::~DaliWrapper()
110 mInstanceCreated = false;
113 DaliWrapper& DaliWrapper::Get()
115 if( !mInstanceCreated )
117 mWrapper = new DaliWrapper( RUNNING_STANDALONE, NULL );
119 mInstanceCreated = true;
121 mWrapper->InitializeStandAlone();
127 v8::Local<v8::Object> DaliWrapper::CreateWrapperForNodeJS( v8::Isolate* isolate )
129 v8::EscapableHandleScope handleScope( isolate);
131 mInstanceCreated = true;
133 mWrapper = new DaliWrapper( RUNNING_IN_NODE_JS, isolate );
135 v8::Local<v8::Object> dali = mWrapper->CreateDaliObject();
137 // As we running inside node, we already have an isolate and context
138 return handleScope.Escape( dali );
141 v8::Local<v8::Object> DaliWrapper::CreateDaliObject()
143 v8::EscapableHandleScope handleScope( mIsolate );
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;
148 v8::Local<v8::ObjectTemplate> daliObjectTemplate = NewDaliObjectTemplate( mIsolate );
151 v8::Local<v8::Object> stageObject = StageWrapper::WrapStage( mIsolate, Stage::GetCurrent() );
152 daliObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "stage") , stageObject );
154 v8::Local<v8::Object> keyboardObject = KeyboardFocusManagerWrapper::WrapKeyboardFocusManager( mIsolate,Toolkit::KeyboardFocusManager::Get() );
155 daliObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "keyboardFocusManager") , keyboardObject );
158 //create an instance of the template
159 v8::Local<v8::Object> daliObject = daliObjectTemplate->NewInstance();
161 ConstantsWrapper::AddDaliConstants( mIsolate, daliObject);
163 daliObject->Set( v8::String::NewFromUtf8( mIsolate, "V8_VERSION") ,v8::String::NewFromUtf8( mIsolate, v8::V8::GetVersion() ));
165 return handleScope.Escape( daliObject );
169 void DaliWrapper::SetFlagsFromString(const std::string &flags)
171 v8::V8::SetFlagsFromString(flags.c_str(), flags.size());
174 void DaliWrapper::Shutdown()
176 // if we're running inside node then we don't have ownership of the context
177 if( mRunMode == RUNNING_IN_NODE_JS )
182 DALI_LOG_WARNING("Destroying V8 DALi context\n");
184 if( !mContext.IsEmpty())
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
193 bool DaliWrapper::ExecuteBuffer(const std::string &sourceCode, const std::string &sourceFileName)
195 return mModuleLoader.ExecuteScript( mIsolate, sourceCode, sourceFileName );
198 bool DaliWrapper::ExecuteFile( const std::string& sourceFileName )
200 DALI_LOG_INFO( gLogExecuteFilter, Debug::Verbose, "Executing source file %s \n",sourceFileName.c_str() );
202 return mModuleLoader.ExecuteScriptFromFile( mIsolate, sourceFileName );
205 GarbageCollectorInterface& DaliWrapper::GetDaliGarbageCollector()
207 return mGarbageCollector;
210 void DaliWrapper::ApplyGlobalObjectsToContext( v8::Local<v8::Context> context )
212 v8::HandleScope handleScope( mIsolate );
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));
220 context->Global()->Set( v8::String::NewFromUtf8( mIsolate, "console"), consoleObjectTemplate->NewInstance() );
222 // add require functionality
223 context->Global()->Set( v8::String::NewFromUtf8( mIsolate, "require"), v8::FunctionTemplate::New( mIsolate, DaliWrapper::Require)->GetFunction());
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();
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 );
232 context->Global()->Set( v8::String::NewFromUtf8( mIsolate, DALI_API_NAME),daliObject );
236 void DaliWrapper::InitializeStandAlone()
240 v8::V8::InitializeICU();
242 v8::V8::Initialize();
244 // default isolate removed from V8 version 3.27.1 and beyond.
245 mIsolate = v8::Isolate::New();
249 v8::V8::SetFatalErrorHandler( FatalErrorCallback );
252 // if context is null, create it and add dali object to the global object.
253 if( mContext.IsEmpty())
255 v8::HandleScope handleScope( mIsolate );
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 );
265 // Apply global objects like dali and console to the context
266 ApplyGlobalObjectsToContext(context);
268 mContext.Reset( mIsolate, context);
271 DALI_LOG_INFO( gLogExecuteFilter, Debug::Verbose, "V8 Library %s loaded \n", v8::V8::GetVersion() );
275 v8::Handle<v8::ObjectTemplate> DaliWrapper::NewDaliObjectTemplate( v8::Isolate* isolate )
277 v8::EscapableHandleScope handleScope( isolate );
279 // create the template
280 v8::Local< v8::ObjectTemplate > objTemplate = v8::ObjectTemplate::New( isolate );
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__));
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));
291 // add our constructor functions
292 ObjectTemplateHelper::InstallFunctions( isolate,
294 ConstructorFunctionTable,
295 PropertyFunctionTableCount,
296 ObjectTemplateHelper::CONSTRUCTOR_FUNCTIONS);
298 return handleScope.Escape( objTemplate );
301 void DaliWrapper::Require(const v8::FunctionCallbackInfo< v8::Value >& args)
303 DaliWrapper& wrapper( DaliWrapper::Get() );
304 wrapper.mModuleLoader.Require( args );
308 } // namespace V8Plugin