root/src/VM/cil/cilVMInitialization.cpp

Revision 138, 10.5 kB (checked in by hak, 6 months ago)

- Added CCilVm::reset()
- Fixed VM status management (ticket #5)
- Fixed API names not to have upper case as 1st letter.
- Fixed sample build break
- Added new sample, "vm_contol" to demonstrate VM status control API

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     : cilVMInitialization.cpp
15  * Date     :
16  * Version  :
17  *
18  ****************************************************************************/
19
20 /***************************************************************************
21  *      Include file
22  ***************************************************************************/
23 #include "stdafx.h"
24 #include "criscript.h"
25 #include "cilVm.h"
26 #include "float.h"
27 namespace cri {
28 /***************************************************************************
29  *      Variables
30  ***************************************************************************/
31
32 //Static members
33 RID CCilVm::m_ridObject;                                //Object RID
34 RID CCilVm::m_ridFunctionObject;                //Function Object RID
35 RID CCilVm::m_ridSystemObject;                  //System Object RID
36 RID CCilVm::m_ridStringObject;                  //String object RID
37 RID CCilVm::m_ridArrayObject;                   //Array object RID
38 RID CCilVm::m_ridErrorObject;                   //Error object RID
39 RID CCilVm::m_ridRegexObject;                   //Regex object RID
40 RID CCilVm::m_ridBooleanObject;                 //Boolean object RID
41 RID CCilVm::m_ridNumberObject;                  //Numer object RID
42 RID CCilVm::m_ridDateObject;                    //Date object RID
43 RID CCilVm::m_ridMathObject;                    //Math object RID
44
45 /***************************************************************************
46  *      Methods
47  ***************************************************************************/
48
49 /***************************************************************************
50  * ctor
51  ***************************************************************************/   
52 CCilVm::CCilVm()
53 : m_pCurrentInstruction( NULL ),
54   m_iTickCount( 0 ),
55   m_iCallStackRoot( 1 ),
56   m_status( VM_EXECUTE_DONE ),
57   m_iNumGivenArguments( 0 ),
58   m_ExceptionHandlerNestingLevel( 0 ),
59   m_pDebugger( NULL ),
60   m_iStaticFieldTalbeSize( 0 ),
61   m_pStaticFieldTable( NULL ),
62   m_iLocalVariableListIndex( 0 ),
63   m_iArgListIndex( 0 ),
64   m_iMaxLocalVariableListIndex( 0 ),
65   m_iMaxArgListIndex( 0 )
66 {
67         init();
68 }
69
70 /***************************************************************************
71  * dtor
72  ***************************************************************************/   
73 CCilVm::~CCilVm()
74 {
75         flushPrintBuffer();
76         clearEvalStack();
77         delete[] m_pStaticFieldTable;
78         clearLocalVariableList();
79         clearArgList();
80         m_SymbolInfoPool.clear();
81
82         m_BuiltinPrototypeObjectTable.clear();
83
84         //clearRefCount();
85         cleanObjectPool();
86         m_ObjectPool.clear();
87
88
89         m_MetaData.terminate();
90
91 }
92
93 /***************************************************************************
94  * Initializer
95  ***************************************************************************/   
96 bool CCilVm::init ()
97 {
98         //Version string not to be stripped out
99     static const int8_t * version = criscript_version;
100
101         m_status = VM_EXECUTE_INITIALIZING;
102        
103         //Initialize eval stack
104         clearEvalStack();
105         clearLocalVariableList();
106         clearArgList();
107        
108         //Initialize print buffer
109         m_strPrintBuffer = L"";
110
111         delete [] m_pStaticFieldTable;
112        
113         m_SymbolInfoPool.clear();
114
115         for( int32_t i = 0; i < ARRAYSIZE( m_Handler ); ++i )
116                 m_Handler[ i ] = NULL;
117
118         //Initialize prototype object table
119         m_BuiltinPrototypeObjectTable.clear();
120         //Note that initialization of prototype object table need to be after metadata is set
121         //because number of built in prototype object differs by metadata entries
122
123         m_stopwatch.Reset();
124         return true;
125 };
126
127 /***************************************************************************
128  *      Initialization of prototype objects
129  ***************************************************************************/
130 void CCilVm::initializePrototypeObjects()
131 {
132         bool bReturn;
133
134         //Initialize RIDs
135         m_ridObject = getMetaData().queryBuiltinObjectRid( wstring( NAME_BUILTIN_OBJECT_OBJECT ) );
136         assert( m_ridObject != RID_NOTDEFINED );
137         m_ridFunctionObject = getMetaData().queryBuiltinObjectRid( wstring( NAME_BUILTIN_FUNCTION_OBJECT ) );
138         assert( m_ridFunctionObject != RID_NOTDEFINED );
139         m_ridSystemObject = getMetaData().queryBuiltinObjectRid( wstring( NAME_BUILTIN_SYSTEM_OBJECT ) );
140         assert( m_ridSystemObject != RID_NOTDEFINED );
141         m_ridStringObject = getMetaData().queryBuiltinObjectRid( wstring( NAME_BUILTIN_STRING_OBJECT ) );
142         assert( m_ridStringObject != RID_NOTDEFINED );
143         m_ridArrayObject = getMetaData().queryBuiltinObjectRid( wstring( NAME_BUILTIN_ARRAY_OBJECT ) );
144         assert( m_ridArrayObject != RID_NOTDEFINED );
145         m_ridErrorObject = getMetaData().queryBuiltinObjectRid( wstring( NAME_BUILTIN_ERROR_OBJECT ) );
146         assert( m_ridErrorObject != RID_NOTDEFINED );
147         m_ridRegexObject = getMetaData().queryBuiltinObjectRid( wstring( NAME_BUILTIN_REGEX_OBJECT ) );
148         assert( m_ridRegexObject != RID_NOTDEFINED );
149         m_ridBooleanObject = getMetaData().queryBuiltinObjectRid( wstring( NAME_BUILTIN_BOOLEAN_OBJECT ) );
150         assert( m_ridBooleanObject != RID_NOTDEFINED );
151         m_ridNumberObject = getMetaData().queryBuiltinObjectRid( wstring( NAME_BUILTIN_NUMBER_OBJECT ) );
152         assert( m_ridNumberObject != RID_NOTDEFINED );
153         m_ridDateObject = getMetaData().queryBuiltinObjectRid( wstring( NAME_BUILTIN_DATE_OBJECT ) );
154         assert( m_ridDateObject != RID_NOTDEFINED );
155         m_ridMathObject = getMetaData().queryBuiltinObjectRid( wstring( NAME_BUILTIN_MATH_OBJECT ) );
156         assert( m_ridMathObject != RID_NOTDEFINED );
157
158         //Reserve the size
159         m_BuiltinPrototypeObjectTable.reserve( m_MetaData.getBuiltinObjectTable().size() + 1 );
160         m_BuiltinPrototypeObjectTable.resize( m_MetaData.getBuiltinObjectTable().size() + 1 );
161        
162         //Initialize this object
163         //Create function object and store it to static field
164         CVmObject newobject;
165         newobject.setRID( m_ridObject );
166         newobject.setObjectName( getMetaData().getObjectNameRID( m_ridObject ) );
167         newobject.setPrototypeObject( &getPrototypeObject( getObjectRID() ) );
168         getObjectPool().push_back( newobject );
169
170         CVariable var;
171         var.setObjectRef( &getObjectPool().back() );
172         storeArgumentList( var );
173
174         //Initialize global scope callstack
175         VM_CALLSTACK callstack;
176         callstack.type = VM_CALLSTACK_CALLSTACK;
177         callstack.status = VM_EHCLAUSE_NORMAL;
178         callstack.pInstruction = m_pCurrentInstruction;
179         callstack.ridMethod = 0;
180         callstack.iLocalVariableStartIndex = getCurrentLocalVariableIndex();
181         callstack.iArgumentListStartIndex = 0;
182         callstack.iNumArgument = getCurrentArgumentListIndex();         //include This object
183         callstack.bConstructor = false;
184         m_CallStack.push_back( callstack );
185
186         m_iCurrentThisPointerIndex = 0;
187
188         //
189         //NOTE: Function object should be initialized first.
190         //
191         // Initialize Function prototype Object
192         CFunctionObject function;
193         bReturn = function.InitializeVm( *this );
194
195         // Initialize System prototype Object
196         CSystemObject system;
197         bReturn = system.InitializeVm( *this );
198
199         // Initialize object prototype  Object
200         CObjectObject obj;
201         bReturn = obj.InitializeVm( *this );
202
203         // Initialize array prototype  Object
204         CArrayObject arrayObject;
205         bReturn = arrayObject.InitializeVm( *this );
206
207         // Initialize String prototype  Object
208         CStringObject stringobject;
209         bReturn = stringobject.InitializeVm( *this );
210
211         // Initialize Math prototype  Object
212         CMathObject mathobject;
213         bReturn = mathobject.InitializeVm( *this );
214
215         // Initialize Number prototype  Object
216         CNumberObject numberobject;
217         bReturn = numberobject.InitializeVm( *this );
218
219         // Initialize boolean prototype  Object
220         CBooleanObject booleanobject;
221         bReturn = booleanobject.InitializeVm( *this );
222
223         // Initialize error prototype  Object
224         CErrorObject errorobject;
225         bReturn = errorobject.InitializeVm( *this );
226
227         // Initialize date prototype  Object
228         CDateObject dateobject;
229         bReturn = dateobject.InitializeVm( *this );
230
231         // Initialize Regex prototype Object
232         CRegexObject regex;
233         bReturn = regex.InitializeVm( *this );
234
235         //Also initialize built in values
236         assert( OperandType(m_pStaticFieldTable[ RidFromToken( RID_CONSTANT_BOOL_TRUE ) ].iOperandType) == OPERAND_BOOLEAN );
237         m_pStaticFieldTable[ RidFromToken( RID_CONSTANT_BOOL_TRUE ) ].iValue = true;
238
239         assert( OperandType(m_pStaticFieldTable[ RidFromToken( RID_CONSTANT_BOOL_FALSE ) ].iOperandType) == OPERAND_BOOLEAN );
240         m_pStaticFieldTable[ RidFromToken( RID_CONSTANT_BOOL_FALSE ) ].iValue = false;
241
242         assert( OperandType(m_pStaticFieldTable[ RidFromToken( RID_CONSTANT_NAN ) ].iOperandType) == OPERAND_NAN );
243         m_pStaticFieldTable[ RidFromToken( RID_CONSTANT_NAN ) ].iValue = 0;
244
245         assert( OperandType(m_pStaticFieldTable[ RidFromToken( RID_CONSTANT_INFINITY ) ].iOperandType) == OPERAND_FLOAT );
246         m_pStaticFieldTable[ RidFromToken( RID_CONSTANT_INFINITY ) ].iValue = 0x7f800000;
247 }
248
249 /***************************************************************************
250  * Reset the VM execution status
251  ***************************************************************************/   
252 bool CCilVm::reset( const VM_RESET_TYPE iResetType )
253 {
254         assert( iResetType == VM_RESET_DEFAULT );
255
256         bool bReturn = true;
257
258         //Flush existing status
259         flushPrintBuffer();
260
261         if( m_status == VM_EXECUTE_INITIALIZING )
262                 return false;   //Could not set to Initialized status
263
264         if( m_status == VM_EXECUTE_INITIALIZED )
265                 return true;    //just return
266
267         m_status = VM_EXECUTE_INITIALIZED;
268
269         //Rest stacks
270         clearCallStack();
271         clearEvalStack();
272         clearStaticFields();
273         clearLocalVariableList();
274         clearArgList();
275
276         //Reset obj pool
277         cleanObjectPool();
278         m_ObjectPool.clear();
279
280         m_BuiltinPrototypeObjectTable.clear();
281
282         initializePrototypeObjects();
283
284         //Initialize objects in a static field table
285         for( uint32_t i = 0; i < m_iStaticFieldTalbeSize; ++i )
286         {
287                 if( m_pStaticFieldTable[ i ].ridConstraintType )
288                 {
289                         //Yep, i'm one of the ristricted objects
290                         CVmObject* pObj = createObject( m_pStaticFieldTable[ i ].GetConstraintRid() );
291                         m_pStaticFieldTable[ i ].setObjectRefWithFlags( pObj,
292                                 m_pStaticFieldTable[ i ].getFlag(),
293                                 m_pStaticFieldTable[ i ].GetConstraintRid() );
294                 }
295         }
296
297         //Reset IP
298         m_pCurrentInstruction = &m_CurrentILPool.ILPool[ 0 ];
299
300         //Reset timer
301         m_stopwatch.Reset();
302
303         return bReturn;
304 };
305
306 /***************************************************************************
307  * Verifier
308  ***************************************************************************/   
309 bool CCilVm::verify()
310 {
311         return true;
312 };
313
314 } //namespace CRI
Note: See TracBrowser for help on using the browser.