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 { "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},
81 const unsigned int PropertyFunctionTableCount = sizeof(ConstructorFunctionTable)/sizeof(ConstructorFunctionTable[0]);
83 void FatalErrorCallback(const char* location, const char* message)
85 DALI_LOG_ERROR("%s, %s \n",location,message);
86 DALI_ASSERT_ALWAYS( 0 && "V8 fatal error");
89 #if defined(DEBUG_ENABLED)
90 // default to verbose logging
91 Integration::Log::Filter* gLogExecuteFilter( Integration::Log::Filter::New(Debug::Verbose, false, "EXECUTE_JAVASCRIPT") );
93 } // un-named name space
97 bool DaliWrapper::mInstanceCreated = false;
98 DaliWrapper* DaliWrapper::mWrapper = NULL;
100 DaliWrapper::DaliWrapper( RunMode runMode, v8::Isolate* isolate )
101 :mIsolate( isolate ),
106 DaliWrapper::~DaliWrapper()
108 mInstanceCreated = false;
111 DaliWrapper& DaliWrapper::Get()
113 if( !mInstanceCreated )
115 mWrapper = new DaliWrapper( RUNNING_STANDALONE, NULL );
117 mInstanceCreated = true;
119 mWrapper->InitializeStandAlone();
125 v8::Local<v8::Object> DaliWrapper::CreateWrapperForNodeJS( v8::Isolate* isolate )
127 v8::EscapableHandleScope handleScope( isolate);
129 mInstanceCreated = true;
131 mWrapper = new DaliWrapper( RUNNING_IN_NODE_JS, isolate );
133 v8::Local<v8::Object> dali = mWrapper->CreateDaliObject();
135 // As we running inside node, we already have an isolate and context
136 return handleScope.Escape( dali );
139 v8::Local<v8::Object> DaliWrapper::CreateDaliObject()
141 v8::EscapableHandleScope handleScope( mIsolate );
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;
146 v8::Local<v8::ObjectTemplate> daliObjectTemplate = NewDaliObjectTemplate( mIsolate );
149 v8::Local<v8::Object> stageObject = StageWrapper::WrapStage( mIsolate, Stage::GetCurrent() );
150 daliObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "stage") , stageObject );
152 v8::Local<v8::Object> keyboardObject = KeyboardFocusManagerWrapper::WrapKeyboardFocusManager( mIsolate,Toolkit::KeyboardFocusManager::Get() );
153 daliObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "keyboardFocusManager") , keyboardObject );
156 //create an instance of the template
157 v8::Local<v8::Object> daliObject = daliObjectTemplate->NewInstance();
159 ConstantsWrapper::AddDaliConstants( mIsolate, daliObject);
161 daliObject->Set( v8::String::NewFromUtf8( mIsolate, "V8_VERSION") ,v8::String::NewFromUtf8( mIsolate, v8::V8::GetVersion() ));
163 return handleScope.Escape( daliObject );
167 void DaliWrapper::SetFlagsFromString(const std::string &flags)
169 v8::V8::SetFlagsFromString(flags.c_str(), flags.size());
172 void DaliWrapper::Shutdown()
174 // if we're running inside node then we don't have ownership of the context
175 if( mRunMode == RUNNING_IN_NODE_JS )
180 DALI_LOG_WARNING("Destroying V8 DALi context\n");
182 if( !mContext.IsEmpty())
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
191 bool DaliWrapper::ExecuteBuffer(const std::string &sourceCode, const std::string &sourceFileName)
193 return mModuleLoader.ExecuteScript( mIsolate, sourceCode, sourceFileName );
196 bool DaliWrapper::ExecuteFile( const std::string& sourceFileName )
198 DALI_LOG_INFO( gLogExecuteFilter, Debug::Verbose, "Executing source file %s \n",sourceFileName.c_str() );
200 return mModuleLoader.ExecuteScriptFromFile( mIsolate, sourceFileName );
203 GarbageCollectorInterface& DaliWrapper::GetDaliGarbageCollector()
205 return mGarbageCollector;
208 void DaliWrapper::ApplyGlobalObjectsToContext( v8::Local<v8::Context> context )
210 v8::HandleScope handleScope( mIsolate );
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));
218 context->Global()->Set( v8::String::NewFromUtf8( mIsolate, "console"), consoleObjectTemplate->NewInstance() );
220 // add require functionality
221 context->Global()->Set( v8::String::NewFromUtf8( mIsolate, "require"), v8::FunctionTemplate::New( mIsolate, DaliWrapper::Require)->GetFunction());
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();
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 );
230 context->Global()->Set( v8::String::NewFromUtf8( mIsolate, DALI_API_NAME),daliObject );
234 void DaliWrapper::InitializeStandAlone()
238 v8::V8::InitializeICU();
240 v8::V8::Initialize();
242 // default isolate removed from V8 version 3.27.1 and beyond.
243 mIsolate = v8::Isolate::New();
247 v8::V8::SetFatalErrorHandler( FatalErrorCallback );
250 // if context is null, create it and add dali object to the global object.
251 if( mContext.IsEmpty())
253 v8::HandleScope handleScope( mIsolate );
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 );
263 // Apply global objects like dali and console to the context
264 ApplyGlobalObjectsToContext(context);
266 mContext.Reset( mIsolate, context);
269 DALI_LOG_INFO( gLogExecuteFilter, Debug::Verbose, "V8 Library %s loaded \n", v8::V8::GetVersion() );
273 v8::Handle<v8::ObjectTemplate> DaliWrapper::NewDaliObjectTemplate( v8::Isolate* isolate )
275 v8::EscapableHandleScope handleScope( isolate );
277 // create the template
278 v8::Local< v8::ObjectTemplate > objTemplate = v8::ObjectTemplate::New( isolate );
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__));
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));
289 // add our constructor functions
290 ObjectTemplateHelper::InstallFunctions( isolate,
292 ConstructorFunctionTable,
293 PropertyFunctionTableCount,
294 ObjectTemplateHelper::CONSTRUCTOR_FUNCTIONS);
296 return handleScope.Escape( objTemplate );
299 void DaliWrapper::Require(const v8::FunctionCallbackInfo< v8::Value >& args)
301 DaliWrapper& wrapper( DaliWrapper::Get() );
302 wrapper.mModuleLoader.Require( args );
306 } // namespace V8Plugin