JavaScript binding for new mesh APIs
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / object / property-buffer-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 "property-buffer-wrapper.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <object/property-buffer-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> PropertyBufferWrapper::mPropertyBufferTemplate;
35
36 namespace // un-named name space
37 {
38
39 /**
40  * Contains a list of all functions that can be called
41  */
42 const ApiFunction PropertyBufferFunctionTable[]=
43 {
44     /**************************************
45     * PropertyBuffer API (in order of property-buffer.h)
46     **************************************/
47
48    { "SetData"             , PropertyBufferApi::SetData },
49 };
50
51 const unsigned int PropertyBufferFunctionTableCount = sizeof(PropertyBufferFunctionTable)/sizeof(PropertyBufferFunctionTable[0]);
52 } //un-named space
53
54
55 PropertyBufferWrapper::PropertyBufferWrapper( const Dali::PropertyBuffer& buffer, GarbageCollectorInterface& gc )
56 :  BaseWrappedObject(  BaseWrappedObject::PROPERTY_BUFFER , gc )
57 {
58     mPropertyBuffer = buffer;
59 }
60
61 v8::Handle<v8::Object> PropertyBufferWrapper::WrapPropertyBuffer(v8::Isolate* isolate, const Dali::PropertyBuffer& buffer )
62 {
63   v8::EscapableHandleScope handleScope( isolate );
64   v8::Local<v8::ObjectTemplate> objectTemplate;
65
66   objectTemplate = GetPropertyBufferTemplate( isolate);
67
68   // create an instance of the template
69   v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
70
71   // create the PropertyBuffer wrapper
72   PropertyBufferWrapper* pointer =  new PropertyBufferWrapper( buffer, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
73
74   // assign the JavaScript object to the wrapper.
75   pointer->SetJavascriptObject( isolate, localObject );
76
77   return handleScope.Escape( localObject );
78 }
79
80 v8::Local<v8::ObjectTemplate> PropertyBufferWrapper::GetPropertyBufferTemplate( v8::Isolate* isolate)
81 {
82   v8::EscapableHandleScope handleScope( isolate );
83   v8::Local<v8::ObjectTemplate> objectTemplate;
84
85   if( mPropertyBufferTemplate.IsEmpty() )
86   {
87     objectTemplate = MakePropertyBufferTemplate( isolate );
88     mPropertyBufferTemplate.Reset( isolate, objectTemplate );
89   }
90   else
91   {
92     // get the object template
93     objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, mPropertyBufferTemplate );
94   }
95   return handleScope.Escape( objectTemplate );
96 }
97
98 v8::Handle<v8::ObjectTemplate> PropertyBufferWrapper::MakePropertyBufferTemplate( v8::Isolate* isolate )
99 {
100   v8::EscapableHandleScope handleScope( isolate );
101
102   v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
103
104   objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
105
106   // add our function properties
107   ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, PropertyBufferFunctionTable, PropertyBufferFunctionTableCount );
108
109   return handleScope.Escape( objTemplate );
110 }
111
112 void PropertyBufferWrapper::NewPropertyBuffer( const v8::FunctionCallbackInfo< v8::Value >& args)
113 {
114   v8::Isolate* isolate = args.GetIsolate();
115   v8::HandleScope handleScope( isolate);
116
117   if(!args.IsConstructCall())
118   {
119       DALI_SCRIPT_EXCEPTION( isolate, "PropertyBuffer constructor called without 'new'");
120       return;
121   }
122   Dali::PropertyBuffer buffer = PropertyBufferApi::New( isolate, args );
123
124   if(buffer)
125   {
126     v8::Local<v8::Object> localObject = WrapPropertyBuffer( isolate, buffer );
127     args.GetReturnValue().Set( localObject );
128   }
129 }
130
131
132 PropertyBuffer PropertyBufferWrapper::GetPropertyBuffer()
133 {
134   return mPropertyBuffer;
135 }
136
137
138 } // namespace V8Plugin
139
140 } // namespace Dali