22use std:: collections:: HashMap ;
33use std:: io:: { self , Write } ;
44
5+ use super :: pyobject:: DictProtocol ;
56use super :: pyobject:: { Executor , PyContext , PyObject , PyObjectKind , PyObjectRef , PyResult } ;
67
78/*
@@ -38,6 +39,39 @@ pub fn print(args: Vec<Rc<NativeType>>) -> NativeType {
3839}
3940*/
4041
42+ fn get_locals ( rt : & mut Executor ) -> PyObjectRef {
43+ let mut d = rt. new_dict ( ) ;
44+ // TODO: implement dict_iter_items?
45+ let locals = rt. get_locals ( ) ;
46+ match locals. borrow ( ) . kind {
47+ PyObjectKind :: Dict { ref elements } => {
48+ for l in elements {
49+ d. set_item ( l. 0 , l. 1 . clone ( ) ) ;
50+ }
51+ }
52+ _ => { }
53+ } ;
54+ d
55+ }
56+
57+ fn dir_locals ( rt : & mut Executor ) -> PyObjectRef {
58+ get_locals ( rt)
59+ }
60+
61+ fn dir_object ( rt : & mut Executor , obj : PyObjectRef ) -> PyObjectRef {
62+ let d = rt. new_dict ( ) ;
63+ d
64+ }
65+
66+ pub fn dir ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
67+ if args. is_empty ( ) {
68+ Ok ( dir_locals ( rt) )
69+ } else {
70+ let obj = args. into_iter ( ) . next ( ) . unwrap ( ) ;
71+ Ok ( dir_object ( rt, obj) )
72+ }
73+ }
74+
4175pub fn print ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
4276 // println!("Woot: {:?}", args);
4377 trace ! ( "print called with {:?}" , args) ;
@@ -55,52 +89,35 @@ pub fn compile(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
5589}
5690
5791pub fn locals ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
58- // TODO
59- Ok ( rt. new_bool ( true ) )
92+ Ok ( rt. get_locals ( ) )
6093}
6194
62- /*
63- * TODO
64- pub fn len(args: Vec<Rc<NativeType>>) -> NativeType {
95+ pub fn len ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
6596 if args. len ( ) != 1 {
6697 panic ! ( "len(s) expects exactly one parameter" ) ;
6798 }
68- let len = match args[0].deref() {
69- &NativeType::List(ref l) => l.borrow().len(),
70- &NativeType::Tuple(ref t) => t.len(),
71- &NativeType::Str(ref s) => s.len(),
72- _ => panic!("TypeError: object of this type has no len()")
99+ let len = match args[ 0 ] . borrow ( ) . kind {
100+ PyObjectKind :: List { ref elements } => elements. len ( ) ,
101+ PyObjectKind :: Tuple { ref elements } => elements. len ( ) ,
102+ PyObjectKind :: String { ref value } => value. len ( ) ,
103+ _ => {
104+ return Err ( rt. context ( )
105+ . new_str ( "TypeError: object of this type has no len()" . to_string ( ) ) )
106+ }
73107 } ;
74- NativeType::Int( len as i32)
108+ Ok ( rt . context ( ) . new_int ( len as i32 ) )
75109}
76- */
77110
78111pub fn make_module ( ctx : & PyContext ) -> PyObjectRef {
79112 // scope[String::from("print")] = print;
80113 let mut dict = HashMap :: new ( ) ;
81- dict. insert (
82- String :: from ( "print" ) ,
83- PyObject :: new (
84- PyObjectKind :: RustFunction { function : print } ,
85- ctx. type_type . clone ( ) ,
86- ) ,
87- ) ;
88- // locals.insert("len".to_string(), Rc::new(NativeType::NativeFunction(builtins::len)));
114+ dict. insert ( String :: from ( "print" ) , ctx. new_rustfunc ( print) ) ;
89115 dict. insert ( String :: from ( "type" ) , ctx. type_type . clone ( ) ) ;
90- dict. insert (
91- String :: from ( "all" ) ,
92- PyObject :: new (
93- PyObjectKind :: RustFunction { function : all } ,
94- ctx. type_type . clone ( ) ,
95- ) ,
96- ) ;
97- dict. insert (
98- String :: from ( "any" ) ,
99- PyObject :: new (
100- PyObjectKind :: RustFunction { function : any } ,
101- ctx. type_type . clone ( ) ,
102- ) ,
103- ) ;
116+ dict. insert ( String :: from ( "all" ) , ctx. new_rustfunc ( all) ) ;
117+ dict. insert ( String :: from ( "any" ) , ctx. new_rustfunc ( any) ) ;
118+ dict. insert ( String :: from ( "dir" ) , ctx. new_rustfunc ( dir) ) ;
119+ dict. insert ( String :: from ( "locals" ) , ctx. new_rustfunc ( locals) ) ;
120+ dict. insert ( "len" . to_string ( ) , ctx. new_rustfunc ( len) ) ;
104121 let obj = PyObject :: new (
105122 PyObjectKind :: Module {
106123 name : "__builtins__" . to_string ( ) ,
0 commit comments