@@ -4,11 +4,12 @@ use std::io::{self, Write};
44
55use super :: compile;
66use super :: pyobject:: DictProtocol ;
7- use super :: pyobject:: { Executor , PyContext , PyObject , PyObjectKind , PyObjectRef , PyResult , Scope , IdProtocol } ;
7+ use super :: pyobject:: { PyContext , PyObject , PyObjectKind , PyObjectRef , PyResult , Scope , IdProtocol } ;
8+ use super :: vm:: VirtualMachine ;
89use super :: objbool;
910
1011
11- fn get_locals ( rt : & mut Executor ) -> PyObjectRef {
12+ fn get_locals ( rt : & mut VirtualMachine ) -> PyObjectRef {
1213 let mut d = rt. new_dict ( ) ;
1314 // TODO: implement dict_iter_items?
1415 let locals = rt. get_locals ( ) ;
@@ -23,16 +24,16 @@ fn get_locals(rt: &mut Executor) -> PyObjectRef {
2324 d
2425}
2526
26- fn dir_locals ( rt : & mut Executor ) -> PyObjectRef {
27+ fn dir_locals ( rt : & mut VirtualMachine ) -> PyObjectRef {
2728 get_locals ( rt)
2829}
2930
30- fn dir_object ( rt : & mut Executor , obj : PyObjectRef ) -> PyObjectRef {
31+ fn dir_object ( rt : & mut VirtualMachine , obj : PyObjectRef ) -> PyObjectRef {
3132 let d = rt. new_dict ( ) ;
3233 d
3334}
3435
35- pub fn builtin_dir ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
36+ pub fn builtin_dir ( rt : & mut VirtualMachine , args : Vec < PyObjectRef > ) -> PyResult {
3637 if args. is_empty ( ) {
3738 Ok ( dir_locals ( rt) )
3839 } else {
@@ -41,15 +42,15 @@ pub fn builtin_dir(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
4142 }
4243}
4344
44- pub fn builtin_id ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
45+ pub fn builtin_id ( rt : & mut VirtualMachine , args : Vec < PyObjectRef > ) -> PyResult {
4546 if args. len ( ) != 1 {
4647 return Err ( rt. new_exception ( "Expected only one argument" . to_string ( ) ) )
4748 }
4849
4950 Ok ( rt. context ( ) . new_int ( args[ 0 ] . get_id ( ) as i32 ) )
5051}
5152
52- pub fn builtin_print ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
53+ pub fn builtin_print ( rt : & mut VirtualMachine , args : Vec < PyObjectRef > ) -> PyResult {
5354 trace ! ( "print called with {:?}" , args) ;
5455 for a in args {
5556 print ! ( "{} " , a. borrow( ) . str ( ) ) ;
@@ -59,7 +60,7 @@ pub fn builtin_print(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
5960 Ok ( rt. get_none ( ) )
6061}
6162
62- pub fn builtin_compile ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
63+ pub fn builtin_compile ( rt : & mut VirtualMachine , args : Vec < PyObjectRef > ) -> PyResult {
6364 if args. len ( ) < 1 {
6465 return Err ( rt. new_exception ( "Expected more arguments" . to_string ( ) ) )
6566 }
@@ -73,11 +74,36 @@ pub fn builtin_compile(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
7374 }
7475}
7576
76- pub fn locals ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
77+ pub fn builtin_eval ( rt : & mut VirtualMachine , args : Vec < PyObjectRef > ) -> PyResult {
78+ if args. len ( ) > 3 {
79+ return Err ( rt. new_exception ( "Expected at maximum of 3 arguments" . to_string ( ) ) )
80+ } else if args. len ( ) > 2 {
81+ // TODO: handle optional global and locals
82+ } else {
83+ return Err ( rt. new_exception ( "Expected at least one argument" . to_string ( ) ) )
84+ }
85+ let source = args[ 0 ] . clone ( ) ;
86+ let _globals = args[ 1 ] . clone ( ) ;
87+ let locals = args[ 2 ] . clone ( ) ;
88+
89+ let code_obj = source; // if source.borrow().kind
90+
91+ // Construct new scope:
92+ let scope_inner = Scope {
93+ locals : locals,
94+ parent : None ,
95+ } ;
96+ let scope = PyObject { kind : PyObjectKind :: Scope { scope : scope_inner } , typ : None } . into_ref ( ) ;
97+
98+ // Run the source:
99+ rt. run_code_obj ( code_obj, scope)
100+ }
101+
102+ pub fn locals ( rt : & mut VirtualMachine , args : Vec < PyObjectRef > ) -> PyResult {
77103 Ok ( rt. get_locals ( ) )
78104}
79105
80- pub fn len ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
106+ pub fn len ( rt : & mut VirtualMachine , args : Vec < PyObjectRef > ) -> PyResult {
81107 if args. len ( ) != 1 {
82108 panic ! ( "len(s) expects exactly one parameter" ) ;
83109 }
@@ -105,6 +131,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
105131 dict. insert ( String :: from ( "dir" ) , ctx. new_rustfunc ( builtin_dir) ) ;
106132 dict. insert ( String :: from ( "locals" ) , ctx. new_rustfunc ( locals) ) ;
107133 dict. insert ( String :: from ( "compile" ) , ctx. new_rustfunc ( builtin_compile) ) ;
134+ dict. insert ( String :: from ( "eval" ) , ctx. new_rustfunc ( builtin_eval) ) ;
108135 dict. insert ( "len" . to_string ( ) , ctx. new_rustfunc ( len) ) ;
109136 let d2 = PyObject :: new ( PyObjectKind :: Dict { elements : dict } , ctx. type_type . clone ( ) ) ;
110137 let scope = PyObject :: new ( PyObjectKind :: Scope { scope : Scope { locals : d2, parent : None } } , ctx. type_type . clone ( ) ) ;
@@ -118,10 +145,10 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
118145 obj
119146}
120147
121- fn builtin_any ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
148+ fn builtin_any ( rt : & mut VirtualMachine , args : Vec < PyObjectRef > ) -> PyResult {
122149 Ok ( rt. new_bool ( args. into_iter ( ) . any ( |e| objbool:: boolval ( e) ) ) )
123150}
124151
125- fn builtin_all ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
152+ fn builtin_all ( rt : & mut VirtualMachine , args : Vec < PyObjectRef > ) -> PyResult {
126153 Ok ( rt. new_bool ( args. into_iter ( ) . all ( |e| objbool:: boolval ( e) ) ) )
127154}
0 commit comments