diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs index c8a611f37..7f6fd824f 100644 --- a/vm/src/builtins.rs +++ b/vm/src/builtins.rs @@ -850,14 +850,9 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef { }, ctx.type_type(), ); - let obj = PyObject::new( - PyObjectKind::Module { - name: "__builtins__".to_string(), - dict: scope, - }, - ctx.type_type(), - ); - obj + let mod_name = "__builtins__".to_string(); + let py_mod = ctx.new_module(&mod_name, scope); + py_mod } pub fn builtin_build_class_(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult { diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index cc248c65b..f5ba47476 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -42,8 +42,15 @@ pub fn get_value(obj: &PyObjectRef) -> f64 { pub fn make_float(vm: &mut VirtualMachine, obj: &PyObjectRef) -> Result { if objtype::isinstance(obj, &vm.ctx.float_type()) { Ok(get_value(obj)) - } else if objtype::isinstance(obj, &vm.ctx.int_type()) { - Ok(objint::get_value(obj).to_f64().unwrap()) + } else if let Ok(method) = vm.get_method(obj.clone(), "__float__") { + let res = vm.invoke( + method, + PyFuncArgs { + args: vec![], + kwargs: vec![], + }, + )?; + Ok(get_value(&res)) } else { Err(vm.new_type_error(format!("Cannot cast {:?} to float", obj))) } diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index d62b3f121..c413c3929 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -201,6 +201,12 @@ fn int_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { } } +fn int_float(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!(vm, args, required = [(i, Some(vm.ctx.int_type()))]); + let i = get_value(i); + Ok(vm.ctx.new_float(i.to_f64().unwrap())) +} + fn int_floordiv(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!( vm, @@ -391,6 +397,7 @@ pub fn init(context: &PyContext) { int_type.set_attr("__add__", context.new_rustfunc(int_add)); int_type.set_attr("__and__", context.new_rustfunc(int_and)); int_type.set_attr("__divmod__", context.new_rustfunc(int_divmod)); + int_type.set_attr("__float__", context.new_rustfunc(int_float)); int_type.set_attr("__floordiv__", context.new_rustfunc(int_floordiv)); int_type.set_attr("__hash__", context.new_rustfunc(int_hash)); int_type.set_attr("__new__", context.new_rustfunc(int_new)); diff --git a/vm/src/obj/objtype.rs b/vm/src/obj/objtype.rs index 86818cf98..01fa4ecb9 100644 --- a/vm/src/obj/objtype.rs +++ b/vm/src/obj/objtype.rs @@ -81,7 +81,7 @@ pub fn get_type_name(typ: &PyObjectRef) -> String { { name.clone() } else { - panic!("Cannot get type_name of non-type type"); + panic!("Cannot get type_name of non-type type {:?}", typ); } } diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 6494bb977..8679e4545 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -42,10 +42,15 @@ impl VirtualMachine { /// Create a new `VirtualMachine` structure. pub fn new() -> VirtualMachine { let ctx = PyContext::new(); + + // Hard-core modules: let builtins = builtins::make_module(&ctx); let sysmod = sysmodule::mk_module(&ctx); + // Add builtins as builtins module: - // sysmod.get_attr("modules").unwrap().set_item("builtins", builtins.clone()); + let modules = sysmod.get_attr("modules").unwrap(); + modules.set_item("builtins", builtins.clone()); + let stdlib_inits = stdlib::get_module_inits(); VirtualMachine { builtins: builtins,