@@ -24,6 +24,7 @@ use crate::pyobject::{
2424} ;
2525use crate :: vm:: VirtualMachine ;
2626
27+ use crate :: obj:: objcode:: PyCodeRef ;
2728#[ cfg( not( target_arch = "wasm32" ) ) ]
2829use crate :: stdlib:: io:: io_open;
2930
@@ -112,22 +113,17 @@ fn builtin_chr(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
112113 Ok ( vm. new_str ( txt) )
113114}
114115
115- fn builtin_compile ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
116- arg_check ! (
117- vm,
118- args,
119- required = [
120- ( source, None ) ,
121- ( filename, Some ( vm. ctx. str_type( ) ) ) ,
122- ( mode, Some ( vm. ctx. str_type( ) ) )
123- ]
124- ) ;
125- let source = objstr:: get_value ( source) ;
116+ fn builtin_compile (
117+ source : PyStringRef ,
118+ filename : PyStringRef ,
119+ mode : PyStringRef ,
120+ vm : & VirtualMachine ,
121+ ) -> PyResult < PyCodeRef > {
126122 // TODO: fix this newline bug:
127- let source = format ! ( "{}\n " , source) ;
123+ let source = format ! ( "{}\n " , & source. value ) ;
128124
129125 let mode = {
130- let mode = objstr :: get_value ( mode) ;
126+ let mode = & mode. value ;
131127 if mode == "exec" {
132128 compile:: Mode :: Exec
133129 } else if mode == "eval" {
@@ -141,9 +137,7 @@ fn builtin_compile(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
141137 }
142138 } ;
143139
144- let filename = objstr:: get_value ( filename) ;
145-
146- compile:: compile ( & source, & mode, filename, vm. ctx . code_type ( ) ) . map_err ( |err| {
140+ compile:: compile ( vm, & source, & mode, filename. value . to_string ( ) ) . map_err ( |err| {
147141 let syntax_error = vm. context ( ) . exceptions . syntax_error . clone ( ) ;
148142 vm. new_exception ( syntax_error, err. to_string ( ) )
149143 } )
@@ -190,25 +184,23 @@ fn builtin_eval(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
190184 let scope = make_scope ( vm, globals, locals) ?;
191185
192186 // Determine code object:
193- let code_obj = if objtype :: isinstance ( source , & vm . ctx . code_type ( ) ) {
194- source . clone ( )
187+ let code_obj = if let Ok ( code_obj ) = PyCodeRef :: try_from_object ( vm , source . clone ( ) ) {
188+ code_obj
195189 } else if objtype:: isinstance ( source, & vm. ctx . str_type ( ) ) {
196190 let mode = compile:: Mode :: Eval ;
197191 let source = objstr:: get_value ( source) ;
198192 // TODO: fix this newline bug:
199193 let source = format ! ( "{}\n " , source) ;
200- compile:: compile ( & source, & mode, "<string>" . to_string ( ) , vm. ctx . code_type ( ) ) . map_err (
201- |err| {
202- let syntax_error = vm. context ( ) . exceptions . syntax_error . clone ( ) ;
203- vm. new_exception ( syntax_error, err. to_string ( ) )
204- } ,
205- ) ?
194+ compile:: compile ( vm, & source, & mode, "<string>" . to_string ( ) ) . map_err ( |err| {
195+ let syntax_error = vm. context ( ) . exceptions . syntax_error . clone ( ) ;
196+ vm. new_exception ( syntax_error, err. to_string ( ) )
197+ } ) ?
206198 } else {
207199 return Err ( vm. new_type_error ( "code argument must be str or code object" . to_string ( ) ) ) ;
208200 } ;
209201
210202 // Run the source:
211- vm. run_code_obj ( code_obj. clone ( ) , scope)
203+ vm. run_code_obj ( code_obj, scope)
212204}
213205
214206/// Implements `exec`
@@ -229,14 +221,12 @@ fn builtin_exec(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
229221 let source = objstr:: get_value ( source) ;
230222 // TODO: fix this newline bug:
231223 let source = format ! ( "{}\n " , source) ;
232- compile:: compile ( & source, & mode, "<string>" . to_string ( ) , vm. ctx . code_type ( ) ) . map_err (
233- |err| {
234- let syntax_error = vm. context ( ) . exceptions . syntax_error . clone ( ) ;
235- vm. new_exception ( syntax_error, err. to_string ( ) )
236- } ,
237- ) ?
238- } else if objtype:: isinstance ( source, & vm. ctx . code_type ( ) ) {
239- source. clone ( )
224+ compile:: compile ( vm, & source, & mode, "<string>" . to_string ( ) ) . map_err ( |err| {
225+ let syntax_error = vm. context ( ) . exceptions . syntax_error . clone ( ) ;
226+ vm. new_exception ( syntax_error, err. to_string ( ) )
227+ } ) ?
228+ } else if let Ok ( code_obj) = PyCodeRef :: try_from_object ( vm, source. clone ( ) ) {
229+ code_obj
240230 } else {
241231 return Err ( vm. new_type_error ( "source argument must be str or code object" . to_string ( ) ) ) ;
242232 } ;
0 commit comments