@@ -100,7 +100,13 @@ fn builtin_bin(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
100
100
}
101
101
102
102
// builtin_breakpoint
103
- // builtin_callable
103
+
104
+ fn builtin_callable ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
105
+ arg_check ! ( vm, args, required = [ ( obj, None ) ] ) ;
106
+ // TODO: is this a sufficiently thorough check?
107
+ let is_callable = obj. has_attr ( "__call__" ) ;
108
+ Ok ( vm. new_bool ( is_callable) )
109
+ }
104
110
105
111
fn builtin_chr ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
106
112
arg_check ! ( vm, args, required = [ ( i, Some ( vm. ctx. int_type( ) ) ) ] ) ;
@@ -116,12 +122,35 @@ fn builtin_chr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
116
122
}
117
123
118
124
fn builtin_compile ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
119
- arg_check ! ( vm, args, required = [ ( source, None ) ] ) ;
120
- // TODO:
121
- let mode = compile:: Mode :: Eval ;
122
- let source = source. borrow ( ) . str ( ) ;
125
+ arg_check ! (
126
+ vm,
127
+ args,
128
+ required = [
129
+ ( source, None ) ,
130
+ ( filename, Some ( vm. ctx. str_type( ) ) ) ,
131
+ ( mode, Some ( vm. ctx. str_type( ) ) )
132
+ ]
133
+ ) ;
134
+ let source = objstr:: get_value ( source) ;
135
+
136
+ let mode = {
137
+ let mode = objstr:: get_value ( mode) ;
138
+ if mode == String :: from ( "exec" ) {
139
+ compile:: Mode :: Exec
140
+ } else if mode == "eval" . to_string ( ) {
141
+ compile:: Mode :: Eval
142
+ } else if mode == "single" . to_string ( ) {
143
+ compile:: Mode :: Single
144
+ } else {
145
+ return Err (
146
+ vm. new_value_error ( "compile() mode must be 'exec', 'eval' or single'" . to_string ( ) )
147
+ ) ;
148
+ }
149
+ } ;
123
150
124
- compile:: compile ( vm, & source, mode, None )
151
+ let filename = objstr:: get_value ( filename) ;
152
+
153
+ compile:: compile ( vm, & source, mode, Some ( filename) )
125
154
}
126
155
127
156
fn builtin_delattr ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
@@ -150,7 +179,28 @@ fn builtin_divmod(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
150
179
}
151
180
}
152
181
153
- // builtin_enumerate
182
+ fn builtin_enumerate ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
183
+ arg_check ! (
184
+ vm,
185
+ args,
186
+ required = [ ( iterable, None ) ] ,
187
+ optional = [ ( start, None ) ]
188
+ ) ;
189
+ let items = vm. extract_elements ( iterable) ?;
190
+ let start = if let Some ( start) = start {
191
+ objint:: get_value ( start)
192
+ } else {
193
+ Zero :: zero ( )
194
+ } ;
195
+ let mut new_items = vec ! [ ] ;
196
+ for ( i, item) in items. into_iter ( ) . enumerate ( ) {
197
+ let element = vm
198
+ . ctx
199
+ . new_tuple ( vec ! [ vm. ctx. new_int( i. to_bigint( ) . unwrap( ) + & start) , item] ) ;
200
+ new_items. push ( element) ;
201
+ }
202
+ Ok ( vm. ctx . new_list ( new_items) )
203
+ }
154
204
155
205
fn builtin_eval ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
156
206
arg_check ! (
@@ -219,7 +269,6 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
219
269
220
270
// builtin_filter
221
271
// builtin_format
222
- // builtin_frozenset
223
272
224
273
fn builtin_getattr ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
225
274
arg_check ! (
@@ -550,6 +599,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
550
599
dict. insert ( String :: from ( "bool" ) , ctx. bool_type ( ) ) ;
551
600
dict. insert ( String :: from ( "bytearray" ) , ctx. bytearray_type ( ) ) ;
552
601
dict. insert ( String :: from ( "bytes" ) , ctx. bytes_type ( ) ) ;
602
+ dict. insert ( String :: from ( "callable" ) , ctx. new_rustfunc ( builtin_callable) ) ;
553
603
dict. insert ( String :: from ( "chr" ) , ctx. new_rustfunc ( builtin_chr) ) ;
554
604
dict. insert ( String :: from ( "classmethod" ) , ctx. classmethod_type ( ) ) ;
555
605
dict. insert ( String :: from ( "compile" ) , ctx. new_rustfunc ( builtin_compile) ) ;
@@ -558,9 +608,14 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
558
608
dict. insert ( String :: from ( "dict" ) , ctx. dict_type ( ) ) ;
559
609
dict. insert ( String :: from ( "divmod" ) , ctx. new_rustfunc ( builtin_divmod) ) ;
560
610
dict. insert ( String :: from ( "dir" ) , ctx. new_rustfunc ( builtin_dir) ) ;
611
+ dict. insert (
612
+ String :: from ( "enumerate" ) ,
613
+ ctx. new_rustfunc ( builtin_enumerate) ,
614
+ ) ;
561
615
dict. insert ( String :: from ( "eval" ) , ctx. new_rustfunc ( builtin_eval) ) ;
562
616
dict. insert ( String :: from ( "exec" ) , ctx. new_rustfunc ( builtin_exec) ) ;
563
617
dict. insert ( String :: from ( "float" ) , ctx. float_type ( ) ) ;
618
+ dict. insert ( String :: from ( "frozenset" ) , ctx. frozenset_type ( ) ) ;
564
619
dict. insert ( String :: from ( "getattr" ) , ctx. new_rustfunc ( builtin_getattr) ) ;
565
620
dict. insert ( String :: from ( "hasattr" ) , ctx. new_rustfunc ( builtin_hasattr) ) ;
566
621
dict. insert ( String :: from ( "hash" ) , ctx. new_rustfunc ( builtin_hash) ) ;
0 commit comments