Skip to content

Commit

Permalink
Merge pull request #461 from madsmtm/expand-runtime
Browse files Browse the repository at this point in the history
Improve `objc2::runtime`
  • Loading branch information
madsmtm authored Jun 18, 2023
2 parents ee2afe2 + 896d49b commit 0c7da00
Show file tree
Hide file tree
Showing 5 changed files with 385 additions and 133 deletions.
8 changes: 8 additions & 0 deletions crates/objc2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
implementations for `rc::Id`.
* Added `rc::IdFromIterator` helper trait for implementing `IntoIterator`
for `rc::Id`.
* Added `Display` impl for `runtime::Class`, `runtime::Sel` and
`runtime::Protocol`.
* Added `Debug` impl for `runtime::Method` and `runtime::Ivar`.
* Added `Method::set_implementation`.
* Added `Method::exchange_implementation`.
* Added `Object::set_class`.

### Changed
* **BREAKING**: `objc2::rc::AutoreleasePool` is now a zero-sized `Copy` type
Expand All @@ -69,6 +75,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* **BREAKING**: Moved `VerificationError`, `ProtocolObject` and
`ImplementedBy` into the `runtime` module.
* Relaxed a `fmt::Debug` bound on `WeakId`'s own `fmt::Debug` impl.
* Changed `Debug` impl for `runtime::Class`, `runtime::Sel` and
`runtime::Protocol` to give more information.

### Fixed
* Fixed using autorelease pools on 32bit macOS and older macOS versions.
Expand Down
34 changes: 13 additions & 21 deletions crates/objc2/src/__macro_helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,15 @@ impl<'a> MsgSendIdFailed<'a> for New {
let cls = obj.class();
if cls.is_metaclass() {
if sel == new_sel() {
panic!("failed creating new instance of {:?}", cls)
panic!("failed creating new instance of {cls}")
} else {
panic!("failed creating new instance using +[{:?} {:?}]", cls, sel)
panic!("failed creating new instance using +[{cls} {sel}]")
}
} else {
panic!("unexpected NULL returned from -[{:?} {:?}]", cls, sel)
panic!("unexpected NULL returned from -[{cls} {sel}]")
}
} else {
panic!("unexpected NULL {:?}; receiver was NULL", sel);
panic!("unexpected NULL {sel}; receiver was NULL");
}
}
}
Expand All @@ -348,9 +348,9 @@ impl<'a> MsgSendIdFailed<'a> for Alloc {
#[cold]
fn failed((cls, sel): Self::Args) -> ! {
if sel == alloc_sel() {
panic!("failed allocating {:?}", cls)
panic!("failed allocating {cls}")
} else {
panic!("failed allocating with +[{:?} {:?}]", cls, sel)
panic!("failed allocating with +[{cls} {sel}]")
}
}
}
Expand All @@ -368,7 +368,7 @@ impl MsgSendIdFailed<'_> for Init {
if sel == init_sel() {
panic!("failed initializing object")
} else {
panic!("failed initializing object with -{:?}", sel)
panic!("failed initializing object with -{sel}")
}
}
}
Expand All @@ -391,13 +391,11 @@ impl<'a> MsgSendIdFailed<'a> for Other {
if let Some(obj) = obj {
let cls = obj.class();
panic!(
"unexpected NULL returned from {}[{:?} {:?}]",
"unexpected NULL returned from {}[{cls} {sel}]",
if cls.is_metaclass() { "+" } else { "-" },
cls,
sel,
)
} else {
panic!("unexpected NULL {:?}; receiver was NULL", sel);
panic!("unexpected NULL {sel}; receiver was NULL");
}
}
}
Expand Down Expand Up @@ -554,9 +552,7 @@ impl ClassProtocolMethodsBuilder<'_, '_> {
.map(|desc| desc.types)
.unwrap_or_else(|| {
panic!(
"failed overriding protocol method -[{} {:?}]: method not found",
protocol.name(),
sel
"failed overriding protocol method -[{protocol} {sel}]: method not found"
)
});
}
Expand Down Expand Up @@ -585,9 +581,7 @@ impl ClassProtocolMethodsBuilder<'_, '_> {
.map(|desc| desc.types)
.unwrap_or_else(|| {
panic!(
"failed overriding protocol method +[{} {:?}]: method not found",
protocol.name(),
sel
"failed overriding protocol method +[{protocol} {sel}]: method not found"
)
});
}
Expand All @@ -607,8 +601,7 @@ impl ClassProtocolMethodsBuilder<'_, '_> {
for desc in &self.required_instance_methods {
if !self.registered_instance_methods.contains(&desc.sel) {
panic!(
"must implement required protocol method -[{} {:?}]",
protocol.name(),
"must implement required protocol method -[{protocol} {}]",
desc.sel
)
}
Expand All @@ -620,8 +613,7 @@ impl ClassProtocolMethodsBuilder<'_, '_> {
for desc in &self.required_class_methods {
if !self.registered_class_methods.contains(&desc.sel) {
panic!(
"must implement required protocol method +[{} {:?}]",
protocol.name(),
"must implement required protocol method +[{protocol} {}]",
desc.sel
)
}
Expand Down
16 changes: 8 additions & 8 deletions crates/objc2/src/declare/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ impl ClassBuilder {
assert_eq!(
sel_args,
enc_args.len(),
"Selector {:?} accepts {} arguments, but function accepts {}",
"Selector {} accepts {} arguments, but function accepts {}",
sel,
sel_args,
enc_args.len(),
Expand All @@ -439,7 +439,7 @@ impl ClassBuilder {
if let Some(method) = superclass.instance_method(sel) {
if let Err(err) = crate::verify::verify_method_signature(method, enc_args, &enc_ret)
{
panic!("declared invalid method -[{} {sel:?}]: {err}", self.name())
panic!("declared invalid method -[{} {sel}]: {err}", self.name())
}
}
}
Expand All @@ -453,7 +453,7 @@ impl ClassBuilder {
types.as_ptr(),
)
});
assert!(success.as_bool(), "Failed to add method {sel:?}");
assert!(success.as_bool(), "Failed to add method {sel}");
}

fn metaclass_mut(&mut self) -> *mut ffi::objc_class {
Expand Down Expand Up @@ -483,7 +483,7 @@ impl ClassBuilder {
assert_eq!(
sel_args,
enc_args.len(),
"Selector {:?} accepts {} arguments, but function accepts {}",
"Selector {} accepts {} arguments, but function accepts {}",
sel,
sel_args,
enc_args.len(),
Expand All @@ -496,7 +496,7 @@ impl ClassBuilder {
if let Some(method) = superclass.class_method(sel) {
if let Err(err) = crate::verify::verify_method_signature(method, enc_args, &enc_ret)
{
panic!("declared invalid method +[{} {sel:?}]: {err}", self.name())
panic!("declared invalid method +[{} {sel}]: {err}", self.name())
}
}
}
Expand All @@ -510,7 +510,7 @@ impl ClassBuilder {
types.as_ptr(),
)
});
assert!(success.as_bool(), "Failed to add class method {sel:?}");
assert!(success.as_bool(), "Failed to add class method {sel}");
}

/// Adds an ivar with type `T` and the provided name.
Expand Down Expand Up @@ -574,7 +574,7 @@ impl ClassBuilder {
pub fn add_protocol(&mut self, proto: &Protocol) {
let success = unsafe { ffi::class_addProtocol(self.as_mut_ptr(), proto.as_ptr()) };
let success = Bool::from_raw(success).as_bool();
assert!(success, "Failed to add protocol {proto:?}");
assert!(success, "Failed to add protocol {proto}");
}

// fn add_property(&self, name: &str, attributes: &[ffi::objc_property_attribute_t]);
Expand Down Expand Up @@ -649,7 +649,7 @@ impl ProtocolBuilder {
assert_eq!(
sel_args,
encs.len(),
"Selector {:?} accepts {} arguments, but function accepts {}",
"Selector {} accepts {} arguments, but function accepts {}",
sel,
sel_args,
encs.len(),
Expand Down
4 changes: 2 additions & 2 deletions crates/objc2/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ fn msg_send_check(
#[cfg(debug_assertions)]
#[track_caller]
fn panic_null(sel: Sel) -> ! {
panic!("messsaging {sel:?} to nil")
panic!("messsaging {sel} to nil")
}

#[cfg(debug_assertions)]
#[track_caller]
fn panic_verify(cls: &Class, sel: Sel, err: crate::runtime::VerificationError) -> ! {
panic!(
"invalid message send to {}[{cls:?} {sel:?}]: {err}",
"invalid message send to {}[{cls} {sel}]: {err}",
if cls.is_metaclass() { "+" } else { "-" },
)
}
Expand Down
Loading

0 comments on commit 0c7da00

Please sign in to comment.