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 "shader-wrapper.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>
34 v8::Persistent<v8::ObjectTemplate> ShaderWrapper::mShaderTemplate;
36 ShaderWrapper::ShaderWrapper( const Dali::Shader& shader, GarbageCollectorInterface& gc )
37 : HandleWrapper( BaseWrappedObject::SHADER , shader, gc )
42 v8::Handle<v8::Object> ShaderWrapper::WrapShader(v8::Isolate* isolate, const Dali::Shader& shader )
44 v8::EscapableHandleScope handleScope( isolate );
45 v8::Local<v8::ObjectTemplate> objectTemplate;
47 objectTemplate = GetShaderTemplate( isolate);
49 // create an instance of the template
50 v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
52 // create the Shader wrapper
53 ShaderWrapper* pointer = new ShaderWrapper( shader, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
55 // assign the JavaScript object to the wrapper.
56 pointer->SetJavascriptObject( isolate, localObject );
58 return handleScope.Escape( localObject );
61 v8::Local<v8::ObjectTemplate> ShaderWrapper::GetShaderTemplate( v8::Isolate* isolate)
63 v8::EscapableHandleScope handleScope( isolate );
64 v8::Local<v8::ObjectTemplate> objectTemplate;
66 if( mShaderTemplate.IsEmpty() )
68 objectTemplate = MakeShaderTemplate( isolate );
69 mShaderTemplate.Reset( isolate, objectTemplate );
73 // get the object template
74 objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, mShaderTemplate );
76 return handleScope.Escape( objectTemplate );
79 v8::Handle<v8::ObjectTemplate> ShaderWrapper::MakeShaderTemplate( v8::Isolate* isolate )
81 v8::EscapableHandleScope handleScope( isolate );
83 v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
85 // property handle intercepts property getters and setters and signals
86 HandleWrapper::AddInterceptsToTemplate( isolate, objTemplate );
88 objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
90 return handleScope.Escape( objTemplate );
93 void ShaderWrapper::NewShader( const v8::FunctionCallbackInfo< v8::Value >& args)
95 v8::Isolate* isolate = args.GetIsolate();
96 v8::HandleScope handleScope( isolate);
98 if(!args.IsConstructCall())
100 DALI_SCRIPT_EXCEPTION( isolate, "Shader constructor called without 'new'");
103 Dali::Shader shader = ShaderApi::New( isolate, args );
107 v8::Local<v8::Object> localObject = WrapShader( isolate, shader );
108 args.GetReturnValue().Set( localObject );
113 Shader ShaderWrapper::GetShader()
119 } // namespace V8Plugin