@@ -48,10 +48,11 @@ pub enum ObjectContent {
48
48
Set ( Vec < ObjectRef > ) ,
49
49
FrozenSet ( Vec < ObjectRef > ) ,
50
50
Bytes ( Vec < u8 > ) ,
51
- Function ( ObjectRef ) ,
51
+ Function ( String , ObjectRef ) , // module, code
52
52
PrimitiveNamespace , // __primitives__
53
53
PrimitiveFunction ( String ) ,
54
54
Class ( Option < ObjectRef > ) ,
55
+ Module ,
55
56
OtherObject ,
56
57
}
57
58
@@ -114,10 +115,10 @@ impl ObjectRef {
114
115
ObjectContent :: Code ( _) => "<code object>" . to_string ( ) ,
115
116
ObjectContent :: Set ( ref l) => format ! ( "set({})" , ObjectRef :: repr_vec( l, store) ) ,
116
117
ObjectContent :: FrozenSet ( ref l) => format ! ( "frozenset({})" , ObjectRef :: repr_vec( l, store) ) ,
117
- ObjectContent :: Function ( _ ) => {
118
+ ObjectContent :: Function ( ref module , ref _code ) => {
118
119
match obj. name {
119
- None => "<anonymous function>" . to_string ( ) ,
120
- Some ( ref s) => format ! ( "<function {}>" , s) ,
120
+ None => format ! ( "<anonymous function in module {}>" , module ) ,
121
+ Some ( ref s) => format ! ( "<function {} in module {} >" , s, module ) ,
121
122
}
122
123
} ,
123
124
ObjectContent :: PrimitiveNamespace => "__primitives__" . to_string ( ) ,
@@ -128,9 +129,25 @@ impl ObjectRef {
128
129
Some ( ref s) => format ! ( "<class {}>" , s) ,
129
130
}
130
131
} ,
132
+ ObjectContent :: Module => {
133
+ match obj. name {
134
+ None => "<anonymous module>" . to_string ( ) ,
135
+ Some ( ref s) => format ! ( "<module {}" , s) ,
136
+ }
137
+ } ,
131
138
ObjectContent :: OtherObject => format ! ( "<{} instance>" , obj. class. repr( store) ) ,
132
139
}
133
140
}
141
+
142
+ pub fn module ( & self , store : & ObjectStore ) -> String {
143
+ let func = store. deref ( self ) ;
144
+ let ref name = func. name ;
145
+ match func. content {
146
+ ObjectContent :: Function ( ref module_name, ref _code) => module_name. clone ( ) ,
147
+ ObjectContent :: Module => name. clone ( ) . unwrap ( ) ,
148
+ _ => panic ! ( format!( "Not a function/module: {:?}" , func) ) ,
149
+ }
150
+ }
134
151
}
135
152
136
153
@@ -203,6 +220,8 @@ pub struct PrimitiveObjects {
203
220
pub baseexception : ObjectRef ,
204
221
pub runtimeerror : ObjectRef ,
205
222
223
+ pub module : ObjectRef ,
224
+
206
225
pub names_map : HashMap < String , ObjectRef > ,
207
226
}
208
227
@@ -236,6 +255,8 @@ impl PrimitiveObjects {
236
255
let baseexception = store. allocate ( Object :: new_class ( "BaseException" . to_string ( ) , None , type_ref. clone ( ) , vec ! [ obj_ref. clone( ) ] ) ) ;
237
256
let runtimeerror = store. allocate ( Object :: new_class ( "RuntimeError" . to_string ( ) , None , type_ref. clone ( ) , vec ! [ baseexception. clone( ) ] ) ) ;
238
257
258
+ let module = store. allocate ( Object :: new_class ( "module" . to_string ( ) , None , type_ref. clone ( ) , vec ! [ obj_ref. clone( ) ] ) ) ;
259
+
239
260
let mut map = HashMap :: new ( ) ;
240
261
map. insert ( "object" . to_string ( ) , obj_ref. clone ( ) ) ;
241
262
map. insert ( "tuple" . to_string ( ) , type_ref. clone ( ) ) ;
@@ -255,6 +276,7 @@ impl PrimitiveObjects {
255
276
map. insert ( "code" . to_string ( ) , code_type. clone ( ) ) ;
256
277
map. insert ( "BaseException" . to_string ( ) , baseexception. clone ( ) ) ;
257
278
map. insert ( "RuntimeError" . to_string ( ) , runtimeerror. clone ( ) ) ;
279
+ map. insert ( "module" . to_string ( ) , module. clone ( ) ) ;
258
280
259
281
PrimitiveObjects {
260
282
object : obj_ref, type_ : type_ref,
@@ -265,6 +287,7 @@ impl PrimitiveObjects {
265
287
bytes_type : bytes_type, str_type : str_type,
266
288
function_type : function_type, code_type : code_type,
267
289
baseexception : baseexception, runtimeerror : runtimeerror,
290
+ module : module,
268
291
names_map : map,
269
292
}
270
293
}
@@ -293,4 +316,10 @@ impl PrimitiveObjects {
293
316
pub fn new_code ( & self , c : Code ) -> Object {
294
317
Object :: new_instance ( None , self . code_type . clone ( ) , ObjectContent :: Code ( Box :: new ( c) ) )
295
318
}
319
+ pub fn new_function ( & self , name : String , module_name : String , code : ObjectRef ) -> Object {
320
+ Object :: new_instance ( Some ( name) , self . function_type . clone ( ) , ObjectContent :: Function ( module_name, code) )
321
+ }
322
+ pub fn new_module ( & self , name : String ) -> Object {
323
+ Object :: new_instance ( Some ( name) , self . module . clone ( ) , ObjectContent :: Module )
324
+ }
296
325
}
0 commit comments