root/src/VM/cil/cilVMBind.cpp

Revision 195, 6.5 kB (checked in by hak, 1 year 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 bind apis
14  * File     : cilVMBind.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 namespace cri {
27 /***************************************************************************
28  *      Variables
29  ***************************************************************************/
30
31 /***************************************************************************
32  *      Methods
33  ***************************************************************************/
34 /***************************************************************************
35  *      bindBuiltinMethod
36  ***************************************************************************/
37 bool CCilVm::bindBuiltinMethod( const wstring& strQualifiedName,
38                                                         PMETHOD const pMethod,
39                                                         const int32_t iLength )
40 {
41         bool bReturn = true;
42         RID ridMethod = getMetaData().queryBuiltinMethodRid( strQualifiedName );
43         if( ridMethod == RID_NULL )
44                 return false;
45
46         //Bind the API
47         METHOD_DEF& method = getMetaData().getMethod( ridMethod );
48         if( method.MethodType != METHOD_NATIVE )
49                 return false;
50         method.lpFunction = pMethod;
51        
52         if( strQualifiedName.find( STRING_INTERNAL_SCOPEDELIMITER ) != wstring::npos )
53         {
54                 wstring strRoot = strQualifiedName.substr( 0, strQualifiedName.rfind( STRING_INTERNAL_SCOPEDELIMITER ) );
55                 assert( strRoot.find( STRING_INTERNAL_SCOPEDELIMITER ) == wstring::npos );
56
57                 wstring strLeaf = strQualifiedName.substr( strQualifiedName.rfind( STRING_INTERNAL_SCOPEDELIMITER ) + 1,
58                                                                                                         strQualifiedName.length() );
59                 RID ridObject = getMetaData().queryBuiltinObjectRid( strRoot );
60                 if( ridObject == RID_NOTDEFINED )
61                         return false;
62
63                 //Create property
64                 getPrototypeObject( ridObject ).setProperty( this,
65                                                                                                                 strLeaf,
66                                                                                                                 ridMethod,
67                                                                                                                 iLength );             
68         }
69         else
70         {
71                 //Global method
72                 //e.g. print, eval etc
73                 //Those one need to have correspond static field to keep properties
74                 CG_SYMBOL_INFORMATION& info = querySymbolInformation( strQualifiedName + STRING_OBJECTNAME_POSTFIX );
75                 assert( info.Flags == CG_SYMBOL_FLAG_GLOBAL );
76
77                 RID rid = info.rid;
78                 assert( rid != RID_NOTDEFINED );
79                 if( rid )
80                         setStaticFieldFunctionObject( rid, ridMethod );
81         }
82
83         return bReturn;
84 }
85
86 /***************************************************************************
87  *      BindBuiltinMethodWithWrapper
88  ***************************************************************************/
89 bool CCilVm::bindBuiltinMethod( const wstring& strQualifiedName,
90                                                         PMETHOD const pMethod,
91                                                         PMETHOD const pMethodBody )
92 {
93         bool bReturn = true;
94         RID ridMethod = getMetaData().queryBuiltinMethodRid( strQualifiedName );
95         if( ridMethod == RID_NULL )
96                 return false;
97
98         //Bind the API
99         METHOD_DEF& method = getMetaData().getMethod( ridMethod );
100         if( method.MethodType != METHOD_WRAPPED )
101                 return false;
102         method.lpFunction = pMethod;
103         method.lpFunctionBody = pMethodBody;
104        
105         if( strQualifiedName.find( STRING_INTERNAL_SCOPEDELIMITER ) != wstring::npos )
106         {
107                 wstring strRoot = strQualifiedName.substr( 0, strQualifiedName.rfind( STRING_INTERNAL_SCOPEDELIMITER ) );
108                 assert( strRoot.find( STRING_INTERNAL_SCOPEDELIMITER ) == wstring::npos );
109
110                 wstring strLeaf = strQualifiedName.substr( strQualifiedName.rfind( STRING_INTERNAL_SCOPEDELIMITER ) + 1,
111                                                                                                         strQualifiedName.length() );
112                 RID ridObject = getMetaData().queryBuiltinObjectRid( strRoot );
113                 if( ridObject == RID_NOTDEFINED )
114                         return false;
115
116                 //Create property
117                 getPrototypeObject( ridObject ).setProperty( this,
118                                                                                                                 strLeaf,
119                                                                                                                 ridMethod,
120                                                                                                                 method.iNumArgments );         
121         }
122
123         return bReturn;
124 }
125
126 /***************************************************************************
127  *      bindBuiltinVariable
128  ***************************************************************************/
129 bool CCilVm::bindBuiltinVariable( const wstring& strQualifiedName,
130                                                 CVariable* const pvar )
131 {
132         bool bReturn = true;
133
134         //Bind the var
135         
136         if( strQualifiedName.find( STRING_INTERNAL_SCOPEDELIMITER ) != wstring::npos )
137         {
138                 wstring strRoot = strQualifiedName.substr( 0, strQualifiedName.rfind( STRING_INTERNAL_SCOPEDELIMITER ) );
139                 assert( strRoot.find( STRING_INTERNAL_SCOPEDELIMITER ) == wstring::npos );
140
141                 wstring strLeaf = strQualifiedName.substr( strQualifiedName.rfind( STRING_INTERNAL_SCOPEDELIMITER ) + 1,
142                                                                                                         strQualifiedName.length() );
143                 RID ridObject = getMetaData().queryBuiltinObjectRid( strRoot );
144                 if( ridObject == RID_NOTDEFINED )
145                         return false;
146
147                 //Create property
148                 getPrototypeObject( ridObject ).setProperty( strLeaf,
149                                                                                                                 *pvar );
150         }
151
152         return bReturn;
153 }
154
155 /***************************************************************************
156  *   setStaticFieldFunctionObject
157  ***************************************************************************/
158 bool CCilVm::setStaticFieldFunctionObject( const RID rid,
159                                                                                          const RID ridMethod )                                           
160 {
161         assert( TypeFromToken( ridMethod ) == MDT_METHODDEF );
162
163         //Create function object and store it to static field
164         CVmObject newobject;
165         RID ridObj = getFunctionObjectRID();
166
167         newobject.setRID( ridObj );
168         newobject.setObjectName( getMetaData().getObjectNameRID( ridObj ) );
169         newobject.setPrototypeObject( &getPrototypeObject( ridObj ) );
170         newobject.setConstructorProperty( ridMethod );
171         newobject.setCallProperty( ridMethod );
172
173         getObjectPool().push_back( newobject );
174
175         CVariable var;
176         var.setObjectRef( &getObjectPool().back() );
177
178         METHOD_DEF& method = getMetaData().getMethod( ridMethod );
179         int32_t iLength = method.iNumArgments;
180         assert( iLength >= 0 );
181
182         CVariable varInt( iLength, OPERAND_FLAG_DONTENUM );
183         var.refObject->setProperty( STRING_INTERNAL_LENGTH,
184                         varInt );
185
186         //Store the ObjectRef to a static field
187         setStaticField( rid, var );
188
189         return true;
190 }
191
192
193 } //namespace CRI
Note: See TracBrowser for help on using the browser.