1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// The contents of this file is licensed by its authors and copyright holders under the Apache
// License (Version 2.0), MIT license, or Mozilla Public License (Version 2.0), at your option. The
// contents of this file may not be copied, modified, or distributed except according to those
// terms. See the COPYRIGHT file at the top-level directory of this distribution for copies of these
// licenses and more information.

// See https://github.com/opensource-apple/objc4/blob/master/runtime/objc-runtime-new.h

extern crate libc;

use objc;

#[cfg(target_pointer_width = "64")]
#[allow(non_camel_case_types)]
pub type mask_t = u32;

#[cfg(target_pointer_width = "32")]
#[allow(non_camel_case_types)]
pub type mask_t = u16;

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct bucket_t {
  pub key: usize,
  pub imp: objc::IMP,
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct cache_t {
  pub buckets: *mut bucket_t,
  mask: mask_t,
  occupied: mask_t,
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct method_t {
  name: objc::SEL,
  types: *const libc::c_char,
  imp: objc::IMP,
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct ivar_t {
  offset: *mut i32,
  name: *const libc::c_char,
  encoded_type: *const libc::c_char,
  alignment: u32,
  size: u32,
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct property_t {
  name: *const libc::c_char,
  attributes: *const libc::c_char,
}

// TODO: Delete this and switch to core::mem::size_of once it's const.
impl method_t {
  #[cfg(target_pointer_width = "32")]
  pub const ENTSIZE: u32 = 12;

  #[cfg(target_pointer_width = "64")]
  pub const ENTSIZE: u32 = 24;
}

impl ivar_t {
  #[cfg(target_pointer_width = "32")]
  pub const ENTSIZE: u32 = 20;

  #[cfg(target_pointer_width = "64")]
  pub const ENTSIZE: u32 = 32;
}

impl property_t {
  #[cfg(target_pointer_width = "32")]
  pub const ENTSIZE: u32 = 8;

  #[cfg(target_pointer_width = "64")]
  pub const ENTSIZE: u32 = 16;
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct method_list_t {
  entsize_and_flags: u32,
  count: u32,
  first: [method_t],
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct ivar_list_t {
  entsize_and_flags: u32,
  count: u32,
  first: [ivar_t],
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct property_list_t {
  entsize_and_flags: u32,
  count: u32,
  first: [property_t],
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct protocol_t {
  mangled_name: *const libc::c_char,
  protocols: *mut usize,
  instance_methods: *mut method_list_t,
  class_methods: *mut method_list_t,
  optional_instance_methods: *mut method_list_t,
  optional_class_methods: *mut method_list_t,
  instance_properties: *mut property_list_t,
  size: u32,
  flags: u32,
  extended_method_types: *mut *const libc::c_char,
  demangled_name: *const libc::c_char,
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct protocol_list_t {
  count: usize,
  list: [usize],
}

// #[repr(C)]
// #[allow(non_camel_case_types)]
// pub struct locstamped_category_t {
//   cat: *mut category_t,
//   hi: *mut header_info,
// }

// #[repr(C)]
// #[allow(non_camel_case_types)]
// pub struct locstamped_category_list_t {
//   count: u32,
//   #[cfg(target_pointer_width = "64")]
//   reserved: u32,
//   list: [locstamped_category_t],
// }

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct class_ro_t {
  flags: u32,
  instance_start: u32,
  instance_size: u32,
  #[cfg(target_pointer_width = "64")]
  reserved: u32,

  ivar_layout: *const u8,

  name: *const libc::c_char,
  base_method_list: *mut method_list_t,
  base_protocols: *mut protocol_list_t,
  ivars: *const ivar_list_t,

  weak_ivar_layout: *const u8,
  base_properties: *mut property_list_t,
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub union method_array_t {
  list: *mut method_list_t,
  array_and_flag: usize,
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub union property_array_t {
  list: *mut property_list_t,
  array_and_flag: usize,
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub union protocol_array_t {
  list: *mut protocol_list_t,
  array_and_flag: usize,
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct class_rw_t {
  flags: u32,
  version: u32,
  ro: *const class_ro_t,
  methods: method_array_t,
  properties: property_array_t,
  protocols: protocol_array_t,
  first_subclass: objc::Class,
  next_sibling_class: objc::Class,
  demangled_name: *mut libc::c_char,
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct objc_class {
  isa: objc::Class,
  superclass: objc::Class,
  cache: cache_t,
  bits: usize,
}

#[allow(non_camel_case_types)]
enum classref {}
#[allow(non_camel_case_types)]
type classref_t = *mut classref;

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct category_t {
  name: *const libc::c_char,
  cls: classref_t,
  instance_methods: *mut method_list_t,
  class_methods: *mut method_list_t,
  protocols: *mut protocol_list_t,
  instance_properties: *mut property_list_t,
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct objc_super2 {
  receiver: objc::id,
  current_class: objc::Class,
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct message_ref_t {
  imp: objc::IMP,
  sel: objc::SEL,
}