Fix Render API for node addon
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / rendering / shader-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 "shader-wrapper.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <rendering/shader-api.h>
24 #include <shared/api-function.h>
25 #include <shared/object-template-helper.h>
26 #include <dali-wrapper.h>
27
28 namespace Dali
29 {
30
31 namespace V8Plugin
32 {
33
34 v8::Persistent<v8::ObjectTemplate> ShaderWrapper::mShaderTemplate;
35
36 ShaderWrapper::ShaderWrapper( const Dali::Shader& shader, GarbageCollectorInterface& gc )
37 :  HandleWrapper(  BaseWrappedObject::SHADER , shader, gc )
38 {
39     mShader = shader;
40 }
41
42 v8::Handle<v8::Object> ShaderWrapper::WrapShader(v8::Isolate* isolate, const Dali::Shader& shader )
43 {
44   v8::EscapableHandleScope handleScope( isolate );
45   v8::Local<v8::ObjectTemplate> objectTemplate;
46
47   objectTemplate = GetShaderTemplate( isolate);
48
49   // create an instance of the template
50   v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
51
52   // create the Shader wrapper
53   ShaderWrapper* pointer =  new ShaderWrapper( shader, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
54
55   // assign the JavaScript object to the wrapper.
56   pointer->SetJavascriptObject( isolate, localObject );
57
58   return handleScope.Escape( localObject );
59 }
60
61 v8::Local<v8::ObjectTemplate> ShaderWrapper::GetShaderTemplate( v8::Isolate* isolate)
62 {
63   v8::EscapableHandleScope handleScope( isolate );
64   v8::Local<v8::ObjectTemplate> objectTemplate;
65
66   if( mShaderTemplate.IsEmpty() )
67   {
68     objectTemplate = MakeShaderTemplate( isolate );
69     mShaderTemplate.Reset( isolate, objectTemplate );
70   }
71   else
72   {
73     // get the object template
74     objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, mShaderTemplate );
75   }
76   return handleScope.Escape( objectTemplate );
77 }
78
79 v8::Handle<v8::ObjectTemplate> ShaderWrapper::MakeShaderTemplate( v8::Isolate* isolate )
80 {
81   v8::EscapableHandleScope handleScope( isolate );
82
83   v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
84
85   // property handle intercepts property getters and setters and signals
86   HandleWrapper::AddInterceptsToTemplate( isolate, objTemplate );
87
88   objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
89
90   return handleScope.Escape( objTemplate );
91 }
92
93 void ShaderWrapper::NewShader( const v8::FunctionCallbackInfo< v8::Value >& args)
94 {
95   v8::Isolate* isolate = args.GetIsolate();
96   v8::HandleScope handleScope( isolate);
97
98   if(!args.IsConstructCall())
99   {
100       DALI_SCRIPT_EXCEPTION( isolate, "Shader constructor called without 'new'");
101       return;
102   }
103   Dali::Shader shader = ShaderApi::New( isolate, args );
104
105   if(shader)
106   {
107     v8::Local<v8::Object> localObject = WrapShader( isolate, shader );
108     args.GetReturnValue().Set( localObject );
109   }
110 }
111
112
113 Shader ShaderWrapper::GetShader()
114 {
115   return mShader;
116 }
117
118
119 } // namespace V8Plugin
120
121 } // namespace Dali