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 "sampler-wrapper.h"
23 #include <dali-wrapper.h>
24 #include <shared/api-function.h>
25 #include <shared/object-template-helper.h>
26 #include <rendering/sampler-api.h>
33 v8::Persistent<v8::ObjectTemplate> SamplerWrapper::mSamplerTemplate;
39 * Contains a list of all functions that can be called
41 const ApiFunction SamplerFunctionTable[]=
43 /**************************************
44 * Sampler API (in order of sampler.h)
45 **************************************/
47 { "SetFilterMode", SamplerApi::SetFilterMode },
48 { "SetWrapMode", SamplerApi::SetWrapMode },
51 const unsigned int SamplerFunctionTableCount = sizeof(SamplerFunctionTable)/sizeof(SamplerFunctionTable[0]);
55 SamplerWrapper::SamplerWrapper( const Sampler& sampler, GarbageCollectorInterface& gc )
56 : BaseWrappedObject( BaseWrappedObject::SAMPLER , gc )
61 v8::Handle<v8::Object> SamplerWrapper::WrapSampler(v8::Isolate* isolate, const Sampler& sampler )
63 v8::EscapableHandleScope handleScope( isolate );
64 v8::Local<v8::ObjectTemplate> objectTemplate;
66 objectTemplate = GetSamplerTemplate( isolate);
68 // create an instance of the template
69 v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
71 // create the Sampler wrapper
72 SamplerWrapper* pointer = new SamplerWrapper( sampler, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
74 // assign the JavaScript object to the wrapper.
75 pointer->SetJavascriptObject( isolate, localObject );
77 return handleScope.Escape( localObject );
80 v8::Local<v8::ObjectTemplate> SamplerWrapper::GetSamplerTemplate( v8::Isolate* isolate)
82 v8::EscapableHandleScope handleScope( isolate );
83 v8::Local<v8::ObjectTemplate> objectTemplate;
85 if( mSamplerTemplate.IsEmpty() )
87 objectTemplate = MakeSamplerTemplate( isolate );
88 mSamplerTemplate.Reset( isolate, objectTemplate );
92 // get the object template
93 objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, mSamplerTemplate );
95 return handleScope.Escape( objectTemplate );
98 v8::Handle<v8::ObjectTemplate> SamplerWrapper::MakeSamplerTemplate( v8::Isolate* isolate )
100 v8::EscapableHandleScope handleScope( isolate );
102 v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
104 objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
106 // add our function properties
107 ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, SamplerFunctionTable, SamplerFunctionTableCount );
109 return handleScope.Escape( objTemplate );
112 void SamplerWrapper::NewSampler( const v8::FunctionCallbackInfo< v8::Value >& args)
114 v8::Isolate* isolate = args.GetIsolate();
115 v8::HandleScope handleScope( isolate);
117 if(!args.IsConstructCall())
119 DALI_SCRIPT_EXCEPTION( isolate, "Sampler constructor called without 'new'");
122 Dali::Sampler sampler = SamplerApi::New( args );
126 v8::Local<v8::Object> localObject = WrapSampler( isolate, sampler );
127 args.GetReturnValue().Set( localObject );
131 Sampler SamplerWrapper::GetSampler()
137 } // namespace V8Plugin