JavaScript support for DALi
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / shared / base-wrapped-object.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 "base-wrapped-object.h"
20
21 namespace Dali
22 {
23
24 namespace V8Plugin
25 {
26
27 BaseWrappedObject::BaseWrappedObject( Type type, GarbageCollectorInterface& gc )
28 : mWrappedType( type ),
29   mGarbageCollector( gc  )
30 {
31   mGarbageCollector.Register( this );
32 }
33
34 BaseWrappedObject::~BaseWrappedObject()
35 {
36   mGarbageCollector.UnRegister( this );
37 }
38
39 bool BaseWrappedObject::IsReferenced()
40 {
41   if( mWeakPersistentHandle.IsEmpty() )
42   {
43     return false;
44   }
45   if( mWeakPersistentHandle.IsNearDeath() )
46   {
47     return false;
48   }
49   return true;
50 }
51 void BaseWrappedObject::WeakCallback( const v8::WeakCallbackData<v8::Object,BaseWrappedObject >& data)
52 {
53   BaseWrappedObject* wrappedObject = data.GetParameter();
54   wrappedObject->mWeakPersistentHandle.Reset();  // moved from destructor due to crash on shutdown
55   delete wrappedObject;
56 }
57
58 void BaseWrappedObject::SetJavascriptObject( v8::Isolate* isolate, v8::Local<v8::Object>& object )
59 {
60   v8::HandleScope handleScope( isolate );
61
62   v8::Handle<v8::External> ptr = v8::External::New( isolate, this );
63   object->SetInternalField( FIELD_POINTER, ptr );
64
65   v8::Local<v8::Value> theType = v8::Integer::New( isolate ,mWrappedType );
66   object->SetInternalField( FIELD_TYPE, theType );
67
68   // reset sets the the handle
69   mWeakPersistentHandle.Reset( isolate, object );
70
71   // set the weak call back which is triggered when nothing else is referencing the object
72   // note, this may never called.
73   mWeakPersistentHandle.SetWeak( this, &WeakCallback );
74 }
75
76 bool BaseWrappedObject::IsWrappedType(v8::Isolate* isolate, const v8::Local<v8::Object>& object,  BaseWrappedObject::Type type)
77 {
78   v8::HandleScope handleScope( isolate );
79
80   // we've been passed a javascript object, it could be anything!
81   // so we safely check the internal field count matches the number of fields
82   // in BaseWrappedObject. Then check if the second field is an integer value
83   // possibly alternative v8 functions we could use
84   // GetConstructorName
85   // GetIdentityHash
86   if( object->InternalFieldCount() == BaseWrappedObject::FIELD_COUNT )
87   {
88     v8::Handle<v8::Value> value = object->GetInternalField(BaseWrappedObject::FIELD_TYPE);
89
90     if( value->IsInt32() )
91     {
92       BaseWrappedObject::Type objectType = static_cast<BaseWrappedObject::Type>( value->ToInt32()->Value());
93       return (type == objectType);
94     }
95   }
96   return false;
97 }
98
99 bool BaseWrappedObject::IsWrappedTypeAPropertyValue(const v8::Local<v8::Object>& object)
100 {
101
102    if( object->InternalFieldCount() == BaseWrappedObject::FIELD_COUNT )
103    {
104      v8::Handle<v8::Value> value = object->GetInternalField(BaseWrappedObject::FIELD_TYPE);
105
106      if( value->IsInt32() )
107      {
108        BaseWrappedObject::Type objectType = static_cast<BaseWrappedObject::Type>( value->ToInt32()->Value());
109        return ( (objectType > PROPERTY_VALUE_START_RANGE) && ( objectType < PROPERTY_VALUE_END_RANGE) );
110      }
111    }
112    return false;
113 }
114
115 BaseWrappedObject* BaseWrappedObject::UnWrap( v8::Isolate* isolate, const v8::Local<v8::Object>& object)
116 {
117   v8::HandleScope handleScope( isolate );
118
119   v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(BaseWrappedObject::FIELD_POINTER) );
120   void* ptr = field->Value();
121   return static_cast< BaseWrappedObject *>(ptr);
122 }
123
124 BaseWrappedObject::Type BaseWrappedObject::GetType()
125 {
126   return  mWrappedType;
127 }
128
129 SignalManager* BaseWrappedObject::GetSignalManager()
130 {
131   return NULL;
132 }
133 } // V8Plugin
134
135 } // Dali
136