|
1 | 1 | use core::marker::PhantomData;
|
2 | 2 | use core::mem;
|
3 | 3 | use core::ops::Deref;
|
| 4 | +use core::ptr; |
4 | 5 | use std::os::raw::c_ulong;
|
5 | 6 |
|
6 | 7 | use objc2_encode::{Encode, EncodeArguments};
|
7 | 8 |
|
8 | 9 | use super::{ffi, Block};
|
9 | 10 | use crate::BlockArguments;
|
10 | 11 |
|
11 |
| -#[doc(hidden)] |
12 |
| -pub const __GLOBAL_DESCRIPTOR: ffi::Block_descriptor_header = ffi::Block_descriptor_header { |
| 12 | +// TODO: Should this be a static to help the compiler deduplicating them? |
| 13 | +const GLOBAL_DESCRIPTOR: ffi::Block_descriptor_header = ffi::Block_descriptor_header { |
13 | 14 | reserved: 0,
|
14 | 15 | size: mem::size_of::<ffi::Block_layout>() as c_ulong,
|
15 | 16 | };
|
|
46 | 47 | // constructing the static in `global_block!` with an invalid `GlobalBlock`
|
47 | 48 | // triggers an error.
|
48 | 49 | impl<A, R> GlobalBlock<A, R> {
|
| 50 | + // TODO: Use new ABI with BLOCK_HAS_SIGNATURE |
| 51 | + const FLAGS: ffi::block_flags = ffi::BLOCK_IS_GLOBAL | ffi::BLOCK_USE_STRET; |
| 52 | + |
| 53 | + #[doc(hidden)] |
| 54 | + pub const __DEFAULT_LAYOUT: ffi::Block_layout = ffi::Block_layout { |
| 55 | + // Populated in `global_block!` |
| 56 | + isa: ptr::null_mut(), |
| 57 | + flags: Self::FLAGS, |
| 58 | + reserved: 0, |
| 59 | + // Populated in `global_block!` |
| 60 | + invoke: None, |
| 61 | + descriptor: &GLOBAL_DESCRIPTOR as *const _ as *mut _, |
| 62 | + }; |
| 63 | + |
49 | 64 | /// Use the [`global_block`] macro instead.
|
50 | 65 | #[doc(hidden)]
|
51 | 66 | pub const unsafe fn from_layout(layout: ffi::Block_layout) -> Self {
|
@@ -133,21 +148,18 @@ macro_rules! global_block {
|
133 | 148 | $(#[$m])*
|
134 | 149 | #[allow(unused_unsafe)]
|
135 | 150 | $vis static $name: $crate::GlobalBlock<($($t,)*) $(, $r)?> = unsafe {
|
136 |
| - $crate::GlobalBlock::from_layout($crate::ffi::Block_layout { |
137 |
| - isa: &$crate::ffi::_NSConcreteGlobalBlock as *const _ as *mut _, |
138 |
| - flags: $crate::ffi::BLOCK_IS_GLOBAL | $crate::ffi::BLOCK_USE_STRET, |
139 |
| - reserved: 0, |
140 |
| - invoke: { |
141 |
| - unsafe extern "C" fn inner(_: *mut $crate::ffi::Block_layout, $($a: $t),*) $(-> $r)? { |
142 |
| - $body |
143 |
| - } |
144 |
| - let inner: unsafe extern "C" fn(*mut $crate::ffi::Block_layout, $($a: $t),*) $(-> $r)? = inner; |
145 |
| - |
146 |
| - // TODO: SAFETY |
147 |
| - ::core::mem::transmute(inner) |
148 |
| - }, |
149 |
| - descriptor: &$crate::__GLOBAL_DESCRIPTOR as *const _ as *mut _, |
150 |
| - }) |
| 151 | + let mut layout = $crate::GlobalBlock::<($($t,)*) $(, $r)?>::__DEFAULT_LAYOUT; |
| 152 | + layout.isa = &$crate::ffi::_NSConcreteGlobalBlock as *const _ as *mut _; |
| 153 | + layout.invoke = Some({ |
| 154 | + unsafe extern "C" fn inner(_: *mut $crate::ffi::Block_layout, $($a: $t),*) $(-> $r)? { |
| 155 | + $body |
| 156 | + } |
| 157 | + let inner: unsafe extern "C" fn(*mut $crate::ffi::Block_layout, $($a: $t),*) $(-> $r)? = inner; |
| 158 | + |
| 159 | + // TODO: SAFETY |
| 160 | + ::core::mem::transmute(inner) |
| 161 | + }); |
| 162 | + $crate::GlobalBlock::from_layout(layout) |
151 | 163 | };
|
152 | 164 | };
|
153 | 165 | }
|
|
0 commit comments