(
vm: &VirtualMachine,
write_f: impl Fn(&str, &VirtualMachine) -> PyResult<()> + 'static,
)
| 17 | } |
| 18 | |
| 19 | pub fn make_stdout_object( |
| 20 | vm: &VirtualMachine, |
| 21 | write_f: impl Fn(&str, &VirtualMachine) -> PyResult<()> + 'static, |
| 22 | ) -> PyObjectRef { |
| 23 | let ctx = &vm.ctx; |
| 24 | // there's not really any point to storing this class so that there's a consistent type object, |
| 25 | // we just want a half-decent repr() output |
| 26 | let cls = PyRef::leak(py_class!( |
| 27 | ctx, |
| 28 | "JSStdout", |
| 29 | vm.ctx.types.object_type.to_owned(), |
| 30 | {} |
| 31 | )); |
| 32 | let write_method = vm.new_method( |
| 33 | "write", |
| 34 | cls, |
| 35 | move |_self: PyObjectRef, data: PyStrRef, vm: &VirtualMachine| -> PyResult<()> { |
| 36 | write_f(data.expect_str(), vm) |
| 37 | }, |
| 38 | ); |
| 39 | let flush_method = vm.new_method("flush", cls, |_self: PyObjectRef| {}); |
| 40 | extend_class!(ctx, cls, { |
| 41 | "write" => write_method, |
| 42 | "flush" => flush_method, |
| 43 | }); |
| 44 | ctx.new_base_object(cls.to_owned(), None) |
| 45 | } |
no test coverage detected