From 69961414488ac2a1fbeabbccb4b5c8861a8a3bd0 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Thu, 20 Apr 2023 21:09:17 +0900 Subject: [PATCH] debug-friendly tweaks --- stdlib/src/json/machinery.rs | 6 ++---- vm/src/builtins/bytearray.rs | 3 ++- vm/src/builtins/module.rs | 3 ++- vm/src/builtins/str.rs | 5 +++-- vm/src/builtins/type.rs | 3 ++- vm/src/function/protocol.rs | 3 ++- vm/src/object/core.rs | 30 +++++++++++++++++++++++------- vm/src/protocol/callable.rs | 6 ++++-- vm/src/protocol/object.rs | 3 ++- vm/src/stdlib/marshal.rs | 3 ++- vm/src/stdlib/os.rs | 9 ++++++--- vm/src/vm/context.rs | 16 ++++++---------- vm/src/vm/mod.rs | 5 +++-- wasm/lib/src/js_module.rs | 5 ++--- 14 files changed, 61 insertions(+), 39 deletions(-) diff --git a/stdlib/src/json/machinery.rs b/stdlib/src/json/machinery.rs index 0568bab618..fc6b530866 100644 --- a/stdlib/src/json/machinery.rs +++ b/stdlib/src/json/machinery.rs @@ -105,10 +105,8 @@ pub struct DecodeError { } impl DecodeError { fn new(msg: impl Into, pos: usize) -> Self { - Self { - msg: msg.into(), - pos, - } + let msg = msg.into(); + Self { msg, pos } } } diff --git a/vm/src/builtins/bytearray.rs b/vm/src/builtins/bytearray.rs index 8d749e5369..87757fae36 100644 --- a/vm/src/builtins/bytearray.rs +++ b/vm/src/builtins/bytearray.rs @@ -326,7 +326,8 @@ impl PyByteArray { fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult { let bytes = PyBytesInner::fromhex(string.as_str(), vm)?; let bytes = vm.ctx.new_bytes(bytes); - PyType::call(&cls, vec![bytes.into()].into(), vm) + let args = vec![bytes.into()].into(); + PyType::call(&cls, args, vm) } #[pymethod] diff --git a/vm/src/builtins/module.rs b/vm/src/builtins/module.rs index a6187382cc..3de1e21417 100644 --- a/vm/src/builtins/module.rs +++ b/vm/src/builtins/module.rs @@ -80,7 +80,8 @@ impl Py { } pub fn get_attr<'a>(&self, attr_name: impl AsPyStr<'a>, vm: &VirtualMachine) -> PyResult { - self.getattr_inner(attr_name.as_pystr(&vm.ctx), vm) + let attr_name = attr_name.as_pystr(&vm.ctx); + self.getattr_inner(attr_name, vm) } pub fn set_attr<'a>( diff --git a/vm/src/builtins/str.rs b/vm/src/builtins/str.rs index 022503a416..ca6dc1a634 100644 --- a/vm/src/builtins/str.rs +++ b/vm/src/builtins/str.rs @@ -310,8 +310,9 @@ impl PyStr { Self::new_str_unchecked(bytes, PyStrKind::Ascii) } - pub fn new_ref(s: impl Into, ctx: &Context) -> PyRef { - PyRef::new_ref(s.into(), ctx.types.str_type.to_owned(), None) + pub fn new_ref(zelf: impl Into, ctx: &Context) -> PyRef { + let zelf = zelf.into(); + PyRef::new_ref(zelf, ctx.types.str_type.to_owned(), None) } fn new_substr(&self, s: String) -> Self { diff --git a/vm/src/builtins/type.rs b/vm/src/builtins/type.rs index ab6a44c436..590f0394a1 100644 --- a/vm/src/builtins/type.rs +++ b/vm/src/builtins/type.rs @@ -301,7 +301,8 @@ impl PyType { value: V, ctx: impl AsRef, ) { - let attr_name = ctx.as_ref().intern_str(attr_name); + let ctx = ctx.as_ref(); + let attr_name = ctx.intern_str(attr_name); self.set_attr(attr_name, value.into()) } diff --git a/vm/src/function/protocol.rs b/vm/src/function/protocol.rs index 5d6c0df8af..838414ada8 100644 --- a/vm/src/function/protocol.rs +++ b/vm/src/function/protocol.rs @@ -18,7 +18,8 @@ pub struct ArgCallable { impl ArgCallable { #[inline(always)] pub fn invoke(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult { - (self.call)(&self.obj, args.into_args(vm), vm) + let args = args.into_args(vm); + (self.call)(&self.obj, args, vm) } } diff --git a/vm/src/object/core.rs b/vm/src/object/core.rs index c855509da3..c46ed10e9a 100644 --- a/vm/src/object/core.rs +++ b/vm/src/object/core.rs @@ -471,7 +471,6 @@ cfg_if::cfg_if! { } } -#[derive(Debug)] #[repr(transparent)] pub struct PyObject(PyInner); @@ -522,7 +521,7 @@ impl PyObjectRef { #[inline(always)] pub fn downcast(self) -> Result, Self> { if self.payload_is::() { - Ok(unsafe { PyRef::from_obj_unchecked(self) }) + Ok(unsafe { self.downcast_unchecked() }) } else { Err(self) } @@ -539,6 +538,8 @@ impl PyObjectRef { } } + /// Force to downcast this reference to a subclass. + /// /// # Safety /// T must be the exact payload type #[inline(always)] @@ -637,13 +638,22 @@ impl PyObject { self.0.typeid == TypeId::of::() } + /// Force to return payload as T. + /// + /// # Safety + /// The actual payload type must be T. + #[inline(always)] + pub unsafe fn payload_unchecked(&self) -> &T { + // we cast to a PyInner first because we don't know T's exact offset because of + // varying alignment, but once we get a PyInner the compiler can get it for us + let inner = unsafe { &*(&self.0 as *const PyInner as *const PyInner) }; + &inner.payload + } + #[inline(always)] pub fn payload(&self) -> Option<&T> { if self.payload_is::() { - // we cast to a PyInner first because we don't know T's exact offset because of - // varying alignment, but once we get a PyInner the compiler can get it for us - let inner = unsafe { &*(&self.0 as *const PyInner as *const PyInner) }; - Some(&inner.payload) + Some(unsafe { self.payload_unchecked() }) } else { None } @@ -854,7 +864,7 @@ impl Drop for PyObjectRef { } } -impl fmt::Debug for PyObjectRef { +impl fmt::Debug for PyObject { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // SAFETY: the vtable contains functions that accept payload types that always match up // with the payload of the object @@ -862,6 +872,12 @@ impl fmt::Debug for PyObjectRef { } } +impl fmt::Debug for PyObjectRef { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.as_object().fmt(f) + } +} + #[repr(transparent)] pub struct Py(PyInner); diff --git a/vm/src/protocol/callable.rs b/vm/src/protocol/callable.rs index 481e4559f1..eb404d0214 100644 --- a/vm/src/protocol/callable.rs +++ b/vm/src/protocol/callable.rs @@ -17,7 +17,8 @@ impl PyObject { /// PyObject_Call*Arg* series pub fn call(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult { - self.call_with_args(args.into_args(vm), vm) + let args = args.into_args(vm); + self.call_with_args(args, vm) } /// PyObject_Call @@ -45,8 +46,9 @@ impl<'a> PyCallable<'a> { } pub fn invoke(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult { + let args = args.into_args(vm); vm.trace_event(TraceEvent::Call)?; - let result = (self.call)(self.obj, args.into_args(vm), vm); + let result = (self.call)(self.obj, args, vm); vm.trace_event(TraceEvent::Return)?; result } diff --git a/vm/src/protocol/object.rs b/vm/src/protocol/object.rs index a0e7221fbb..bebfd21446 100644 --- a/vm/src/protocol/object.rs +++ b/vm/src/protocol/object.rs @@ -133,7 +133,8 @@ impl PyObject { vm: &VirtualMachine, ) -> PyResult<()> { let attr_name = attr_name.as_pystr(&vm.ctx); - self.call_set_attr(vm, attr_name, PySetterValue::Assign(attr_value.into())) + let attr_value = attr_value.into(); + self.call_set_attr(vm, attr_name, PySetterValue::Assign(attr_value)) } // int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value) diff --git a/vm/src/stdlib/marshal.rs b/vm/src/stdlib/marshal.rs index db071243ea..87e244a42a 100644 --- a/vm/src/stdlib/marshal.rs +++ b/vm/src/stdlib/marshal.rs @@ -149,7 +149,8 @@ mod decl { self.0.ctx.new_int(value).into() } fn make_tuple(&self, elements: impl Iterator) -> Self::Value { - self.0.ctx.new_tuple(elements.collect()).into() + let elements = elements.collect(); + self.0.ctx.new_tuple(elements).into() } fn make_code(&self, code: CodeObject) -> Self::Value { self.0.ctx.new_code(code).into() diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 6c822822f4..bebea93c04 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -54,8 +54,9 @@ pub struct OsPath { impl OsPath { pub fn new_str(path: impl Into) -> Self { + let path = path.into(); Self { - path: path.into(), + path, mode: OutputMode::String, } } @@ -178,11 +179,13 @@ impl IOErrorBuilder { } } pub(crate) fn filename(mut self, filename: impl Into) -> Self { - self.filename.replace(filename.into()); + let filename = filename.into(); + self.filename.replace(filename); self } pub(crate) fn filename2(mut self, filename: impl Into) -> Self { - self.filename2.replace(filename.into()); + let filename = filename.into(); + self.filename2.replace(filename); self } } diff --git a/vm/src/vm/context.rs b/vm/src/vm/context.rs index 596bd3a932..4e359476bb 100644 --- a/vm/src/vm/context.rs +++ b/vm/src/vm/context.rs @@ -558,11 +558,9 @@ impl Context { where F: IntoPyGetterFunc, { - PyRef::new_ref( - PyGetSet::new(name.into(), class).with_get(f), - self.types.getset_type.to_owned(), - None, - ) + let name = name.into(); + let getset = PyGetSet::new(name, class).with_get(f); + PyRef::new_ref(getset, self.types.getset_type.to_owned(), None) } pub fn new_getset( @@ -576,11 +574,9 @@ impl Context { G: IntoPyGetterFunc, S: IntoPySetterFunc, { - PyRef::new_ref( - PyGetSet::new(name.into(), class).with_get(g).with_set(s), - self.types.getset_type.to_owned(), - None, - ) + let name = name.into(); + let getset = PyGetSet::new(name, class).with_get(g).with_set(s); + PyRef::new_ref(getset, self.types.getset_type.to_owned(), None) } pub fn new_base_object(&self, class: PyTypeRef, dict: Option) -> PyObjectRef { diff --git a/vm/src/vm/mod.rs b/vm/src/vm/mod.rs index 11991fa6c8..989163c828 100644 --- a/vm/src/vm/mod.rs +++ b/vm/src/vm/mod.rs @@ -469,11 +469,12 @@ impl VirtualMachine { #[inline] pub fn import<'a>( &self, - module: impl AsPyStr<'a>, + module_name: impl AsPyStr<'a>, from_list: Option>, level: usize, ) -> PyResult { - self.import_inner(module.as_pystr(&self.ctx), from_list, level) + let module_name = module_name.as_pystr(&self.ctx); + self.import_inner(module_name, from_list, level) } fn import_inner( diff --git a/wasm/lib/src/js_module.rs b/wasm/lib/src/js_module.rs index e689b60379..1744355707 100644 --- a/wasm/lib/src/js_module.rs +++ b/wasm/lib/src/js_module.rs @@ -94,9 +94,8 @@ mod _js { impl PyJsValue { #[inline] pub fn new(value: impl Into) -> PyJsValue { - PyJsValue { - value: value.into(), - } + let value = value.into(); + PyJsValue { value } } #[pymethod]