Skip to content

Commit

Permalink
Fix accessing raw FFI functions
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Aug 1, 2023
1 parent 0165987 commit cf665fa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
32 changes: 15 additions & 17 deletions src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@
use std::ffi::CString;
use std::mem;

use objc::{class, msg_send, sel, Encode, Encoding, EncodeArguments, Message};
use objc::runtime::{Class, Sel, Method, Object, Imp};
use objc::runtime::{
objc_getClass,
class_addMethod,
class_getInstanceMethod,
method_exchangeImplementations
};
use objc::ffi;
use objc::runtime::{Class, Imp, Object, Sel};
use objc::{class, msg_send, sel, Encode, EncodeArguments, Encoding, Message};

use crate::foundation::{id, nil, BOOL, YES, NSString};

Expand Down Expand Up @@ -73,24 +68,27 @@ extern "C" fn get_bundle_id(this: &Object, s: Sel, v: id) -> id {
}
}

unsafe fn swizzle_bundle_id<F>(bundle_id: &str, func: F) where F: MethodImplementation<Callee=Object> {
unsafe fn swizzle_bundle_id<F>(bundle_id: &str, func: F)
where
F: MethodImplementation<Callee = Object>,
{
let name = CString::new("NSBundle").unwrap();
let cls = objc_getClass(name.as_ptr());
let cls = ffi::objc_getClass(name.as_ptr());

// let mut cls = class!(NSBundle) as *mut Class;
// Class::get("NSBundle").unwrap();
// let types = format!("{}{}{}", Encoding::String, <*mut Object>::ENCODING, Sel::ENCODING);

let added = class_addMethod(
cls as *mut Class,
sel!(__bundleIdentifier),
let added = ffi::class_addMethod(
cls as *mut ffi::objc_class,
sel!(__bundleIdentifier).as_ptr(),
func.imp(),
CString::new("*@:").unwrap().as_ptr()
CString::new("*@:").unwrap().as_ptr(),
);

let method1 = class_getInstanceMethod(cls, sel!(bundleIdentifier)) as *mut Method;
let method2 = class_getInstanceMethod(cls, sel!(__bundleIdentifier)) as *mut Method;
method_exchangeImplementations(method1, method2);
let method1 = ffi::class_getInstanceMethod(cls, sel!(bundleIdentifier).as_ptr()) as *mut ffi::objc_method;
let method2 = ffi::class_getInstanceMethod(cls, sel!(__bundleIdentifier).as_ptr()) as *mut ffi::objc_method;
ffi::method_exchangeImplementations(method1, method2);
}

pub fn set_bundle_id(bundle_id: &str) {
Expand Down
7 changes: 4 additions & 3 deletions src/foundation/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use std::time::Instant;

use lazy_static::lazy_static;
use objc::declare::ClassDecl;
use objc::runtime::{objc_getClass, Class};
use objc::ffi;
use objc::runtime::Class;

lazy_static! {
static ref CLASSES: ClassMap = ClassMap::new();
Expand Down Expand Up @@ -83,7 +84,7 @@ impl ClassMap {
// runtime, and we can wind up in a situation where we're attempting to register a Class
// that already exists but we can't see.
let objc_class_name = CString::new(class_name).unwrap();
let class = unsafe { objc_getClass(objc_class_name.as_ptr() as *const _) };
let class = unsafe { ffi::objc_getClass(objc_class_name.as_ptr() as *const _) };

// This should not happen for our use-cases, but it's conceivable that this could actually
// be expected, so just return None and let the caller panic if so desired.
Expand All @@ -101,7 +102,7 @@ impl ClassMap {
});
}

Some(class)
Some(class.cast())
}

/// Store a newly created subclass type.
Expand Down

0 comments on commit cf665fa

Please sign in to comment.