04617e3d5ee3bb042a9d8bcc7e827c6cb7ad3b44
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / toolkit / builder / builder-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 "builder-wrapper.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <dali-wrapper.h>
24 #include <toolkit/builder/builder-api.h>
25 #include <shared/api-function.h>
26 #include <shared/object-template-helper.h>
27
28 namespace Dali
29 {
30 namespace V8Plugin
31 {
32 namespace
33 {
34
35 /**
36  * Contains a list of all functions that can be called
37  */
38 const ApiFunction BuilderFunctionTable[]=
39 {
40     /**************************************
41     * Builder API (in order of builder.h)
42     **************************************/
43
44     { "LoadFromString"          , BuilderApi::LoadFromString           },
45     { "LoadFromFile"            , BuilderApi::LoadFromFile             },
46     { "AddConstants"            , BuilderApi::AddConstants             },
47     { "GetConstants"            , BuilderApi::GetConstants             },
48     { "CreateAnimation"         , BuilderApi::CreateAnimation          },
49     { "Create"                  , BuilderApi::Create                   },
50     { "ApplyStyle"              , BuilderApi::ApplyStyle               },
51     { "ApplyFromJson"           , BuilderApi::ApplyFromJson            },
52     { "AddActors"               , BuilderApi::AddActors                },
53     { "CreateRenderTask"        , BuilderApi::CreateRenderTask         },
54     { "GetShaderEffect"         , BuilderApi::GetShaderEffect          },
55     { "GetFrameBufferImage"     , BuilderApi::GetFrameBufferImage      }
56 };
57
58 const unsigned int BuilderFunctionTableCount = sizeof(BuilderFunctionTable)/sizeof(BuilderFunctionTable[0]);
59 } //un-named space
60
61
62 BuilderWrapper::BuilderWrapper( const Dali::Toolkit::Builder& builder, GarbageCollectorInterface& gc )
63 : BaseWrappedObject( BaseWrappedObject::BUILDER , gc )
64 {
65     mBuilder = builder;
66 }
67
68 BuilderWrapper::~BuilderWrapper()
69 {
70
71 }
72
73 v8::Handle<v8::Object> BuilderWrapper::WrapBuilder(v8::Isolate* isolate, const Dali::Toolkit::Builder& builder )
74 {
75   v8::EscapableHandleScope handleScope( isolate );
76   v8::Local<v8::ObjectTemplate> objectTemplate;
77
78   objectTemplate = GetBuilderTemplate( isolate);
79
80   // create an instance of the template
81   v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
82
83   // create the Builder wrapper
84   BuilderWrapper* pointer =  new BuilderWrapper( builder, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
85
86   // assign the JavaScript object to the wrapper.
87   pointer->SetJavascriptObject( isolate, localObject );
88
89   return handleScope.Escape( localObject );
90 }
91
92 v8::Local<v8::ObjectTemplate> BuilderWrapper::GetBuilderTemplate( v8::Isolate* isolate)
93 {
94   v8::EscapableHandleScope handleScope( isolate );
95   v8::Local<v8::ObjectTemplate> objectTemplate;
96
97   v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
98
99   objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
100
101   // add our function properties
102   ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, BuilderFunctionTable, BuilderFunctionTableCount );
103
104   return handleScope.Escape( objTemplate );
105
106 }
107
108 void BuilderWrapper::NewBuilder( const v8::FunctionCallbackInfo< v8::Value >& args)
109 {
110   v8::Isolate* isolate = args.GetIsolate();
111   v8::HandleScope handleScope( isolate);
112
113   if( !args.IsConstructCall() )
114   {
115     DALI_SCRIPT_EXCEPTION( isolate, "Builder constructor called without 'new'" );
116     return;
117   }
118
119   Dali::Toolkit::Builder builder = BuilderApi::New( args );
120   v8::Local<v8::Object> localObject = WrapBuilder( isolate, builder );
121   args.GetReturnValue().Set( localObject );
122 }
123
124
125 Dali::Toolkit::Builder BuilderWrapper::GetBuilder()
126 {
127   return mBuilder;
128 }
129
130
131 } // namespace V8Plugin
132
133 } // namespace Dali