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 <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>
54 * This string defines how the global DALi object/namespace is used from JavaScript
55 * E.g. new dali.Image or dali.stage.add( )
57 const char* const DALI_API_NAME = "dali";
60 * lookup table for setting up function calls for creating Dali objects
61 * e.g. new dali.TextActor()
63 const ApiFunction ConstructorFunctionTable[]=
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},
89 const unsigned int PropertyFunctionTableCount = sizeof(ConstructorFunctionTable)/sizeof(ConstructorFunctionTable[0]);
91 void FatalErrorCallback(const char* location, const char* message)
93 DALI_LOG_ERROR("%s, %s \n",location,message);
94 DALI_ASSERT_ALWAYS( 0 && "V8 fatal error");
97 #if defined(DEBUG_ENABLED)
98 // default to verbose logging
99 Integration::Log::Filter* gLogExecuteFilter( Integration::Log::Filter::New(Debug::Verbose, false, "EXECUTE_JAVASCRIPT") );
101 } // un-named name space
105 bool DaliWrapper::mInstanceCreated = false;
106 DaliWrapper* DaliWrapper::mWrapper = NULL;
108 DaliWrapper::DaliWrapper( RunMode runMode, v8::Isolate* isolate )
109 :mIsolate( isolate ),
114 DaliWrapper::~DaliWrapper()
116 mInstanceCreated = false;
119 DaliWrapper& DaliWrapper::Get()
121 if( !mInstanceCreated )
123 mWrapper = new DaliWrapper( RUNNING_STANDALONE, NULL );
125 mInstanceCreated = true;
127 mWrapper->InitializeStandAlone();
133 v8::Local<v8::Object> DaliWrapper::CreateWrapperForNodeJS( v8::Isolate* isolate )
135 v8::EscapableHandleScope handleScope( isolate);
137 mInstanceCreated = true;
139 mWrapper = new DaliWrapper( RUNNING_IN_NODE_JS, isolate );
141 v8::Local<v8::Object> dali = mWrapper->CreateDaliObject();
143 // As we running inside node, we already have an isolate and context
144 return handleScope.Escape( dali );
147 v8::Local<v8::Object> DaliWrapper::CreateDaliObject()
149 v8::EscapableHandleScope handleScope( mIsolate );
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;
154 v8::Local<v8::ObjectTemplate> daliObjectTemplate = NewDaliObjectTemplate( mIsolate );
157 v8::Local<v8::Object> stageObject = StageWrapper::WrapStage( mIsolate, Stage::GetCurrent() );
158 daliObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "stage") , stageObject );
160 v8::Local<v8::Object> keyboardObject = KeyboardFocusManagerWrapper::WrapKeyboardFocusManager( mIsolate,Toolkit::KeyboardFocusManager::Get() );
161 daliObjectTemplate->Set( v8::String::NewFromUtf8( mIsolate, "keyboardFocusManager") , keyboardObject );
164 //create an instance of the template
165 v8::Local<v8::Object> daliObject = daliObjectTemplate->NewInstance();
167 ConstantsWrapper::AddDaliConstants( mIsolate, daliObject);
169 daliObject->Set( v8::String::NewFromUtf8( mIsolate, "V8_VERSION") ,v8::String::NewFromUtf8( mIsolate, v8::V8::GetVersion() ));
171 return handleScope.Escape( daliObject );
175 void DaliWrapper::SetFlagsFromString(const std::string &flags)
177 v8::V8::SetFlagsFromString(flags.c_str(), flags.size());
180 void DaliWrapper::Shutdown()
182 // if we're running inside node then we don't have ownership of the context
183 if( mRunMode == RUNNING_IN_NODE_JS )
188 DALI_LOG_WARNING("Destroying V8 DALi context\n");
190 if( !mContext.IsEmpty())
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
199 bool DaliWrapper::ExecuteBuffer(const std::string &sourceCode, const std::string &sourceFileName)
201 return mModuleLoader.ExecuteScript( mIsolate, sourceCode, sourceFileName );
204 bool DaliWrapper::ExecuteFile( const std::string& sourceFileName )
206 DALI_LOG_INFO( gLogExecuteFilter, Debug::Verbose, "Executing source file %s \n",sourceFileName.c_str() );
208 return mModuleLoader.ExecuteScriptFromFile( mIsolate, sourceFileName );
211 GarbageCollectorInterface& DaliWrapper::GetDaliGarbageCollector()
213 return mGarbageCollector;
216 void DaliWrapper::ApplyGlobalObjectsToContext( v8::Local<v8::Context> context )
218 v8::HandleScope handleScope( mIsolate );
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));
226 context->Global()->Set( v8::String::NewFromUtf8( mIsolate, "console"), consoleObjectTemplate->NewInstance() );
228 // add require functionality
229 context->Global()->Set( v8::String::NewFromUtf8( mIsolate, "require"), v8::FunctionTemplate::New( mIsolate, DaliWrapper::Require)->GetFunction());
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();
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 );
238 context->Global()->Set( v8::String::NewFromUtf8( mIsolate, DALI_API_NAME),daliObject );
242 void DaliWrapper::InitializeStandAlone()
246 v8::V8::InitializeICU();
248 v8::V8::Initialize();
250 // default isolate removed from V8 version 3.27.1 and beyond.
251 mIsolate = v8::Isolate::New();
255 v8::V8::SetFatalErrorHandler( FatalErrorCallback );
258 // if context is null, create it and add dali object to the global object.
259 if( mContext.IsEmpty())
261 v8::HandleScope handleScope( mIsolate );
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 );
271 // Apply global objects like dali and console to the context
272 ApplyGlobalObjectsToContext(context);
274 mContext.Reset( mIsolate, context);
277 DALI_LOG_INFO( gLogExecuteFilter, Debug::Verbose, "V8 Library %s loaded \n", v8::V8::GetVersion() );
281 v8::Handle<v8::ObjectTemplate> DaliWrapper::NewDaliObjectTemplate( v8::Isolate* isolate )
283 v8::EscapableHandleScope handleScope( isolate );
285 // create the template
286 v8::Local< v8::ObjectTemplate > objTemplate = v8::ObjectTemplate::New( isolate );
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__));
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));
297 // add our constructor functions
298 ObjectTemplateHelper::InstallFunctions( isolate,
300 ConstructorFunctionTable,
301 PropertyFunctionTableCount,
302 ObjectTemplateHelper::CONSTRUCTOR_FUNCTIONS);
304 return handleScope.Escape( objTemplate );
307 void DaliWrapper::Require(const v8::FunctionCallbackInfo< v8::Value >& args)
309 DaliWrapper& wrapper( DaliWrapper::Get() );
310 wrapper.mModuleLoader.Require( args );
314 } // namespace V8Plugin