root/src/VM/cil/cilVMObject.cpp

Revision 195, 10.7 kB (checked in by hak, 4 months ago)

Ver.0.91.0.137
- Added OSX/GCC support!
- Can compile in XCode 3.1/GCC 4.0.1 & Visual Studio 2005.
- Runs in OSX 10.5.4 & Windows XP/Vista
- Number::toString support a radix.
- BRFALSE bug fix
- Minor code clean up

Line 
1 /****************************************************************************
2  *
3  * CRI Middleware SDK
4  *
5  * Copyright (c) 2008 CRI Middleware, Inc. All rights reserved.
6  *
7  * Use, modification and distribution are subject to the CRI Script Software
8  * License, Version 1.0(see accompanying file "CriScript_License_1_0.txt" or
9  * copy at www.criscript.com/trac/wiki/CRI%20Script%20Software%20License%201.0 ).
10  *
11  *
12  * Library  : CRIScript
13  * Module   : cil Virtual Machine
14  * File     : CCilVmObject.cpp
15  * Date     :
16  * Version  :
17  *
18  ****************************************************************************/
19
20 /***************************************************************************
21  *      Include file
22  ***************************************************************************/
23 #include "stdafx.h"
24 #include "cilVm.h"
25 namespace cri {
26 /***************************************************************************
27  *      Variables
28  ***************************************************************************/
29
30 /***************************************************************************
31  *      Methods
32  ***************************************************************************/
33
34 /***************************************************************************
35  *      VMObject ctor
36  ***************************************************************************/
37 CVmObject::CVmObject(): m_iRefCount(0),
38                                                 m_varPrototype( NULL ),
39                                                 m_varParent( NULL )
40 {
41         //setProperty Accessor
42         setPropertyAccessor( STRING_INTERNAL_PROTOTYPE, prototypeAccessor );
43 };
44
45 /***************************************************************************
46  *      VMObject dtor
47  ***************************************************************************/
48 CVmObject::~CVmObject()
49 {
50 //Should check refcount...
51 //      assert( m_iRefCount == 0 );
52 }
53
54 /***************************************************************************
55  *      Copy ctor
56  ***************************************************************************/
57 CVmObject::CVmObject( const CVmObject& right ) : CObjectTypeInfo( right )
58 {
59         m_iRefCount = right.m_iRefCount;
60         m_varPrototype = right.m_varPrototype;
61         m_varParent = NULL;
62         addRef();
63
64         m_mapProperty = right.m_mapProperty;
65         m_varValue = right.m_varValue;
66 }
67 /***************************************************************************
68  *      VMObject Addref
69  ***************************************************************************/
70 int32_t CVmObject::addRef()
71 {
72         assert( m_iRefCount <= INT_MAX );
73         return ++m_iRefCount;
74 }
75
76 /***************************************************************************
77  *      VMObject Release
78  ***************************************************************************/
79 int32_t CVmObject::release()
80 {
81 //      assert( m_iRefCount > 0 );
82         if( m_iRefCount > 0 )
83         {
84                 m_iRefCount--;
85
86 //              delete this;
87                 return 0;
88
89                 //TODO:
90                 //Invoke Garbage collector if necessary
91         }
92         return m_iRefCount;
93 }
94
95 /***************************************************************************
96  *      Clear Ref Count Release
97  ***************************************************************************/
98 void CVmObject::clearRefCount( void )
99 {
100         m_iRefCount = 0;
101         hash_map< wstring, CVariable >& map = getPropertyMap();
102         if( map.size() )
103         {
104                 hash_map< wstring, CVariable >::iterator itSt = map.begin();
105                 hash_map< wstring, CVariable >::iterator itEd = map.end();
106                 while( itSt != itEd )
107                 {
108                         if( OperandType( itSt->second.iOperandType ) == OPERAND_OBJECTREF )
109                         {
110                                 itSt->second.refObject->clearRefCount();
111                         }
112                         itSt ++;
113                 }
114         }
115
116         if( OperandType( m_varPrototype.iOperandType ) == OPERAND_OBJECTREF )
117                 m_varPrototype.refObject->clearRefCount();
118
119         if( OperandType( m_varValue.iOperandType ) == OPERAND_OBJECTREF )
120                 m_varValue.refObject->clearRefCount();
121 }
122
123 /***************************************************************************
124  *      Retrieve a property info from a property name
125  ***************************************************************************/
126 CVariable* CVmObject::lookupProperty( const wstring& name )
127 {
128         //Note that prototype object is also stored in the map
129         hash_map< wstring, CVariable >::iterator it;
130         it = m_mapProperty.find( name );
131         if( it != m_mapProperty.end() )
132         {
133                 if( OperandType( it->second.iOperandType ) == OPERAND_SYSTEM_ACCESSOR )
134                 {
135                         //Invoke accessor method
136                         return PACCESSORMETHOD( it->second.pPointerInternal )( NULL, this, &name, NULL, true );
137                 }
138                 return &it->second;
139         }
140         else
141         {
142                 if( m_varPrototype.refObject != NULL && m_varPrototype.refObject != this )
143                 {
144                         return m_varPrototype.refObject->lookupProperty( name );
145                 }
146                 else
147                 {
148                         //return null
149                         return NULL;
150                 }
151         }
152 }
153
154 hash_map< wstring, CVariable >::iterator CVmObject::lookupPropertyIterator( const wstring& name )
155 {
156         hash_map< wstring, CVariable >::iterator it;
157         it = m_mapProperty.find( name );
158         if( it != m_mapProperty.end() )
159         {
160                 assert( it->second.iOperandType != OPERAND_SYSTEM_ACCESSOR );
161                 return it;
162         }
163         else
164         {
165                 if( m_varPrototype.refObject != NULL && m_varPrototype.refObject != this )
166                 {
167                         return m_varPrototype.refObject->lookupPropertyIterator( name );
168                 }
169                 else
170                 {
171                         return m_mapProperty.end();
172                 }
173         }
174 }
175
176 /***************************************************************************
177  *      Retrieve a property info from a property name with setter/getter involved
178  ***************************************************************************/
179 CVariable* CVmObject::lookupProperty( CCilVm* const pVm, const wstring& name, CVariable* const pThis )
180 {
181         //Note that prototype object is also stored in the map
182         hash_map< wstring, CVariable >::iterator it;
183         it = m_mapProperty.find( name );
184         if( it != m_mapProperty.end() )
185         {
186                 if( OperandType( it->second.iOperandType ) == OPERAND_SYSTEM_ACCESSOR )
187                 {
188                         //Invoke accessor method
189                         return PACCESSORMETHOD( it->second.pPointerInternal )( pVm, this, &name, pThis, true );
190                 }
191                 return &it->second;
192         }
193         else
194         {
195                 if( m_varPrototype.refObject != NULL && m_varPrototype.refObject != this )
196                 {
197                         return m_varPrototype.refObject->lookupProperty( pVm, name, pThis );
198                 }
199                 else
200                 {
201                         //return null
202                         return NULL;
203                 }
204         }
205 }
206
207 /***************************************************************************
208  *      Update property info
209  ***************************************************************************/
210 void CVmObject::setProperty( const wstring& name, CVariable& var )
211 {
212         hash_map< wstring, CVariable >::iterator it;
213         it = m_mapProperty.find( name );
214
215         if( it != m_mapProperty.end() )
216         {
217                 if( OperandType( it->second.iOperandType ) == OPERAND_SYSTEM_ACCESSOR )
218                 {
219                         //Invoke accessor method
220                         PACCESSORMETHOD( it->second.pPointerInternal )( NULL, this, &name, &var, false );
221                         return;
222                 }
223                 it->second = var;
224         }
225         else
226         {
227                 m_mapProperty[ name ] = var;
228         }
229         return;
230 }
231
232 /***************************************************************************
233  *      Update property info with getter
234  ***************************************************************************/
235 void CVmObject::setProperty( CCilVm* const pVm, const wstring& name, CVariable& var )
236 {
237         hash_map< wstring, CVariable >::iterator it;
238         it = m_mapProperty.find( name );
239
240         if( it != m_mapProperty.end() )
241         {
242                 if( OperandType( it->second.iOperandType ) == OPERAND_SYSTEM_ACCESSOR )
243                 {
244                         //Invoke accessor method
245                         PACCESSORMETHOD( it->second.pPointerInternal )( pVm, this, &name, &var, false );
246                         return;
247                 }
248
249                 it->second = var;
250         }
251         else
252         {
253                 it = m_mapProperty.find( STRING_PROPERTY_INTERNAL_ANY );
254                 if( it != m_mapProperty.end() )
255                 {
256                         assert( OperandType( it->second.iOperandType ) == OPERAND_SYSTEM_ACCESSOR );
257                         //Invoke accessor method
258                         PACCESSORMETHOD( it->second.pPointerInternal )( pVm, this, &name, &var, false );
259                         return;
260                 }
261
262                 m_mapProperty[ name ] = var;
263         }
264         return;
265 }
266
267 /***************************************************************************
268  *      Update property info
269  ***************************************************************************/
270 void CVmObject::setProperty( CCilVm* const pVm,
271                                  const wstring& name,
272                                  const RID ridMethod,
273                                  const int32_t iLength )
274 {
275         assert( pVm != NULL );
276         assert( TypeFromToken( ridMethod ) == MDT_METHODDEF );
277
278         //Create function object and store it to static field
279         CVmObject newobject;
280         RID ridObj = pVm->getFunctionObjectRID();
281
282         newobject.setRID( ridObj );
283         newobject.setObjectName( pVm->getMetaData().getObjectNameRID( ridObj ) );
284         newobject.setPrototypeObject( &pVm->getPrototypeObject( ridObj ) );
285         newobject.setConstructorProperty( ridMethod );
286         newobject.setCallProperty( ridMethod );
287
288         pVm->getObjectPool().push_back( newobject );
289
290         CVariable var;
291         var.setObjectRef( &pVm->getObjectPool().back() );
292
293         assert( iLength >= 0 );
294         CVariable varInt( iLength, OPERAND_FLAG_DONTENUM );
295         var.refObject->setProperty( STRING_INTERNAL_LENGTH, varInt );
296
297         //Store the ObjectRef to a static field
298         m_mapProperty[ name ] = var;
299 }
300
301 /***************************************************************************
302  *      Remove property info
303  ***************************************************************************/
304 void CVmObject::removeProperty( const wstring& name )
305 {
306         hash_map< wstring, CVariable >::iterator it;
307         it = m_mapProperty.find( name );
308         if( it != m_mapProperty.end() )
309         {
310                 //Remove it
311                 if( !(it->second.iOperandFlag & OPERAND_FLAG_DONTDELETE ) )
312                 {
313                         m_mapProperty.erase( it );
314                 }
315         }
316
317         return;
318 }
319
320 /***************************************************************************
321  *      Set Property  accessor
322  ***************************************************************************/
323 void CVmObject::setPropertyAccessor( const wstring& name, PACCESSORMETHOD pAccessor )
324 {
325         m_mapProperty[ name ].iOperandType = OPERAND_SYSTEM_ACCESSOR;
326         m_mapProperty[ name ].pPointerInternal = (void*)pAccessor;
327         m_mapProperty[ name ].iOperandFlag = OPERAND_FLAG( OPERAND_FLAG_DONTDELETE | OPERAND_FLAG_DONTENUM );
328 }
329
330 /***************************************************************************
331  *      Prototype accessor
332  ***************************************************************************/
333 CVariable* CVmObject::prototypeAccessor(CCilVm* const pVm,
334                                                                                 CVmObject* const pObject,
335                                                                                 const wstring* const pName,
336                                                                                 CVariable* const pVar,
337                                                                                 bool bSet )
338 {
339         assert( pObject != NULL );
340         if( bSet )
341         {
342                 //Getter implementation
343                 assert( pVar == NULL );
344                 return &pObject->m_varPrototype;
345         }
346         else
347         {
348                 //Setter implementation
349                 assert( pVar->iOperandType == OPERAND_OBJECTREF );
350                 pObject->m_varPrototype.setObjectRef( pVar->refObject );
351
352                 return NULL;
353         }
354 }
355
356 } //namespace CRI
Note: See TracBrowser for help on using the browser.