1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! `globals()` - builtin function
//!
//! Return the tuple of names defined in the builtin module. This is not CPython behavior but
//! until scopes are implemented, it serves as an example.
use std::ops::Deref;
use std::borrow::Borrow;

use ::api::result::Error;
use ::api::result::{ObjectResult};
use ::api::RtObject;
use ::modules::builtins::Type;
use ::modules::precondition::{check_args, check_kwargs};
use ::resources::strings;
use ::runtime::Runtime;
use ::runtime::traits::{TupleProvider, ModuleImporter};
use ::system::primitives as rs;
use ::system::primitives::{Func, FuncType, SignatureBuilder};


const FUNC_NAME: &'static str = "globals";

pub struct GlobalsFn;


impl GlobalsFn {
    pub fn create() -> rs::Func {
        trace!("create builtin"; "function" => FUNC_NAME);
        let callable: Box<rs::WrapperFn> = Box::new(rs_builtin_globals);

        Func {
            name: String::from(FUNC_NAME),
            module: String::from(strings::BUILTINS_MODULE),
            callable: FuncType::Wrapper(callable),
            signature: [].as_args()
        }
    }
}


fn rs_builtin_globals(rt: &Runtime, pos_args: &RtObject, starargs: &RtObject, kwargs: &RtObject) -> ObjectResult {
    trace!("call"; "native_builtin" => FUNC_NAME);
    check_args(0, &pos_args)?;
    check_args(0, &starargs)?;
    check_kwargs(0, &kwargs)?;

    let builtins = rt.import_module(strings::BUILTINS_MODULE)?;

    let attrs = match builtins.as_ref() {
        &Type::Module(ref object) => object.dir()?,
        _ => return Err(Error::system(
                &format!("Module was not an object; file: {}, line: {}", file!(), line!())))
    };
    Ok(rt.tuple(attrs))
}