swiftui-native 0.0.1

SwiftUI from pure Rust — no Swift bridge, no shim, only swift-runtime calls
Documentation
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! Materials, Liquid Glass and theming.

use super::*;
use crate::abi;
use crate::resolve;
use core::ffi::c_void;
use crate::mangle;
mangle::modifier_types! {
    /// `.background(_: Material)`
    /// `ModifiedContent<AnyView, _BackgroundStyleModifier<Material>>`
    pub(crate) MATERIAL_BG_T = ty("_BackgroundStyleModifier").of(vec![ty("Material")]);
    /// `.background(_: AnyGradient)`
    /// `ModifiedContent<AnyView, _BackgroundStyleModifier<AnyGradient>>`
    pub(crate) GRADIENT_BG_T = ty("_BackgroundStyleModifier").of(vec![ty("AnyGradient")]);
    /// `.tint(_:)`
    /// `ModifiedContent<AnyView, _EnvironmentKeyWritingModifier<AnyShapeStyle?>>`
    pub(crate) TINT_T = ty("_EnvironmentKeyWritingModifier").of(vec![opt(ty("AnyShapeStyle"))]);
    /// `.preferredColorScheme(_:)`
    /// `ModifiedContent<AnyView, _PreferenceWritingModifier<PreferredColorSchemeKey>>`
    pub(crate) COLOR_SCHEME_T = ty("_PreferenceWritingModifier").of(vec![ty("PreferredColorSchemeKey")]);
    /// `.controlSize(_:)`
    /// `ModifiedContent<AnyView, _EnvironmentKeyWritingModifier<ControlSize>>`
    pub(crate) CONTROL_SIZE_T = ty("_EnvironmentKeyWritingModifier").of(vec![ty_enum("ControlSize")]);
    /// `.buttonStyle(.glass)`
    /// `ModifiedContent<AnyView, PrimitiveButtonStyleContainerModifier<GlassButtonStyle>>`
    pub(crate) GLASS_BUTTON_T = ty("PrimitiveButtonStyleContainerModifier").of(vec![ty("GlassButtonStyle")]);
    /// `.buttonStyle(.bordered)`
    /// `ModifiedContent<AnyView, PrimitiveButtonStyleContainerModifier<BorderedButtonStyle>>`
    pub(crate) BORDERED_BUTTON_T = ty("PrimitiveButtonStyleContainerModifier").of(vec![ty("BorderedButtonStyle")]);
    /// `.buttonStyle(.borderedProminent)`
    pub(crate) BORDERED_PROMINENT_T = ty("PrimitiveButtonStyleContainerModifier").of(vec![ty("BorderedProminentButtonStyle")]);
}






/// The blur materials SwiftUI exposes as `ShapeStyle`s.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum MaterialKind {
    UltraThin,
    Thin,
    Regular,
    Thick,
    UltraThick,
    Bar,
}

impl MaterialKind {
    pub(crate) fn getter(self) -> &'static core::ffi::CStr {
        match self {
            MaterialKind::UltraThin => c"$s7SwiftUI8MaterialV9ultraThinACvgZ",
            MaterialKind::Thin => c"$s7SwiftUI8MaterialV4thinACvgZ",
            MaterialKind::Regular => c"$s7SwiftUI8MaterialV7regularACvgZ",
            MaterialKind::Thick => c"$s7SwiftUI8MaterialV5thickACvgZ",
            MaterialKind::UltraThick => c"$s7SwiftUI8MaterialV10ultraThickACvgZ",
            MaterialKind::Bar => c"$s7SwiftUI8MaterialV3barACvgZ",
        }
    }
}

/// `SwiftUI.ColorScheme`, in declaration order.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u8)]
pub enum ColorScheme {
    Light = 0,
    Dark = 1,
}

/// `SwiftUI.ControlSize`, in declaration order.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u8)]
pub enum ControlSize {
    Mini = 0,
    Small = 1,
    Regular = 2,
    Large = 3,
    ExtraLarge = 4,
}

swift_statics! {
    /// `SwiftUI.Glass` — which Liquid Glass variant an effect uses.
    pub enum Glass {
        Regular = c"$s7SwiftUI5GlassV7regularACvgZ",
        Clear = c"$s7SwiftUI5GlassV5clearACvgZ",
        Identity = c"$s7SwiftUI5GlassV8identityACvgZ",
    }
}

/// Which primitive button style to apply.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ButtonStyle {
    /// The Liquid Glass button style.
    Glass,
    Bordered,
    /// `.borderedProminent` — the filled, tinted button.
    BorderedProminent,
}

/// Read a static getter for a resilient struct, which returns indirectly in x8.
///
/// The buffer is sized by *stride*, not size: a callee may write the full
/// stride including tail padding, and a size-sized buffer overflows the
/// allocation. That corrupts the heap quietly and only crashes several
/// allocations later, a long way from the cause.
pub(crate) unsafe fn static_getter_indirect(
    getter: *const c_void,
    meta: *const c_void,
    _size: usize,
) -> Vec<u8> {
    let mut buf = vec![0u8; unsafe { abi::type_bytes(meta) }];
    unsafe {
        core::arch::asm!("blr {f}", f = in(reg) getter,
            in("x8") buf.as_mut_ptr(),
            in("x20") meta,
            lateout("x0") _, lateout("x1") _, lateout("x2") _, lateout("x3") _,
            lateout("x4") _, lateout("x5") _, lateout("x6") _, lateout("x7") _,
            lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _,
            lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _,
            lateout("x17") _, lateout("lr") _, lateout("x21") _, clobber_abi("C"),
        );
    }
    buf
}

/// `.background(_:ignoresSafeAreaEdges:)` with an arbitrary `ShapeStyle`.
///
///   x0 = @in style, x1 = Edge.Set, x2 = Self.Type, x3 = S.Type,
///   x4 = Self: View WT, x5 = S: ShapeStyle WT, x8 = @out, x20 = self
pub(crate) unsafe fn background_style(
    view: &ViewHandle,
    style: *mut u8,
    style_meta: *const c_void,
    style_wt: *const c_void,
    modifier: &ModifierType,
) -> ViewHandle {
    unsafe {
        let (_, _, size) = modifier.applied_to(view.meta);
        let mut result = vec![0u8; size];
        let this = view.value_ptr();
        core::arch::asm!("blr {f}", f = in(reg) resolve::bg_fn(),
            in("x0") style,
            in("x1") EDGE_SET_ALL, // ignoresSafeAreaEdges: .all
            in("x2") view.meta,
            in("x3") style_meta,
            in("x4") view.wt,
            in("x5") style_wt,
            in("x8") result.as_mut_ptr(),
            in("x20") this,
            lateout("x6") _, lateout("x7") _,
            lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _,
            lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _,
            lateout("x17") _, lateout("lr") _, lateout("x21") _, clobber_abi("C"),
        );
        wrap_modified(result, modifier, view)
    }
}

/// Apply `.background(_:)` with an `AnyGradient` — what `.indigo.gradient` is.
pub fn background_any_gradient(view: &ViewHandle, color: u64) -> ViewHandle {
    unsafe {
        let (mut value, meta, wt) = super::color_gradient(color);
        background_style(view, value.as_mut_ptr(), meta, wt, &GRADIENT_BG_T)
    }
}

/// A `Material`'s value, metadata and `ShapeStyle` witness.
///
/// What [`super::Shape::fill_style`] wants, for `Rectangle().fill(.regularMaterial)`
/// — the same material that `.background(_:)` takes, used as a fill instead.
pub fn material_parts(kind: MaterialKind) -> (Vec<u8>, *const c_void, *const c_void) {
    unsafe {
        let meta = abi::metadata0(resolve::material_ma());
        let value = static_getter_indirect(resolve::require(kind.getter()), meta, 0);
        (value, meta, resolve::material_shapestyle_wt())
    }
}

/// Apply `.background(_: Material)` — the blur materials.
pub fn background_material(view: &ViewHandle, kind: MaterialKind) -> ViewHandle {
    unsafe {
        // Material is resilient, so both its size and its static getters are
        // only knowable at runtime.
        let meta = abi::metadata0(resolve::material_ma());
        let mut value =
            static_getter_indirect(resolve::require(kind.getter()), meta, 0);
        background_style(
            view,
            value.as_mut_ptr(),
            meta,
            resolve::material_shapestyle_wt(),
            &MATERIAL_BG_T,
        )
    }
}

/// Apply `.glassEffect()` — Liquid Glass, in its default shape.
///
/// The result type is a `StaticIf<GlassEnabledPredicate, ...>`: SwiftUI picks
/// between the glass-modified view and the original at runtime depending on
/// whether the platform has Liquid Glass enabled.
pub fn glass_effect(view: &ViewHandle) -> ViewHandle {
    glass_effect_kind(view, Glass::Regular)
}

/// The same, with the variant named.
///
/// `Identity` is the no-op: it still builds the `StaticIf`, so the view's type
/// is the one a glass effect gives it, with nothing drawn. That is what makes
/// it usable as the "off" end of a condition without the two branches
/// disagreeing about their type.
pub fn glass_effect_kind(view: &ViewHandle, glass: Glass) -> ViewHandle {
    unsafe {
        let glass_meta = abi::metadata0(resolve::glass_ma());
        let mut glass = static_getter_indirect(
            glass.resolved(),
            glass_meta,
            0,
        );

        let shape_meta = abi::metadata0(resolve::glass_shape_ma());
        let shape_wt = abi::get_view_wt_for_proto(shape_meta, resolve::shape_proto());
        assert!(!shape_wt.is_null(), "DefaultGlassEffectShape: Shape");
        // `DefaultGlassEffectShape` is not zero-size and not all-zero: it holds
        // a `RoundedCornerStyle`, whose zero case is `.circular` while the
        // initialiser picks `.continuous`. Both render as a capsule at this
        // size, so only reflecting over the value shows the difference — call
        // the initialiser rather than assuming an empty buffer.
        let mut shape = vec![0u8; abi::type_bytes(shape_meta)];
        core::arch::asm!("blr {f}", f = in(reg) resolve::glass_shape_init(),
            in("x8") shape.as_mut_ptr(),
            in("x20") shape_meta,
            lateout("x0") _, lateout("x1") _, lateout("x2") _, lateout("x3") _,
            lateout("x4") _, lateout("x5") _, lateout("x6") _, lateout("x7") _,
            lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _,
            lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _,
            lateout("x17") _, lateout("lr") _, lateout("x21") _, clobber_abi("C"),
        );

        let (meta, wt, size) = glass_result_type(view.meta, view.wt);
        let mut result = vec![0u8; size];
        let this = view.value_ptr();
        // glassEffect<S: Shape>(_ glass: Glass, in shape: S)
        //   x0 = @in Glass, x1 = @in S, x2 = Self.Type, x3 = S.Type,
        //   x4 = Self: View WT, x5 = S: Shape WT, x8 = @out, x20 = self
        core::arch::asm!("blr {f}", f = in(reg) resolve::glass_effect_fn2(),
            in("x0") glass.as_mut_ptr(),
            in("x1") shape.as_mut_ptr(),
            in("x2") view.meta,
            in("x3") shape_meta,
            in("x4") view.wt,
            in("x5") shape_wt,
            in("x8") result.as_mut_ptr(),
            in("x20") this,
            lateout("x6") _, lateout("x7") _,
            lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _,
            lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _,
            lateout("x17") _, lateout("lr") _, lateout("x21") _, clobber_abi("C"),
        );
        wrap_owned(result, meta, wt)
    }
}

/// The concrete type behind a `-> some View`, with its `View` witness and size.
///
/// Cached, and it was not. Every opaque-returning modifier — most of them — came
/// through here and paid two `dlsym`s, a metadata instantiation and a
/// conformance search, on every view built. Swift pays that once per site and
/// keeps the answer in a `…Md` word; this keeps it in a map, because the
/// substitutions are not always fixed at the site — a modifier generic over its
/// style resolves a different type for each style it is handed.
///
/// The key is the descriptor plus its substitutions, all of them already
/// resolved pointers, in a fixed array so a lookup allocates nothing.
/// # Safety
/// `oret` must be an opaque type descriptor, and `subst` must be the whole
/// generic signature's substitutions in declaration order — parameters, then
/// conformances. One missing entry reads the next off the end of the slice.
pub unsafe fn opaque_result_type(
    oret: *const c_void,
    subst: &[*const c_void],
) -> (*const c_void, *const c_void, usize) {
    use std::collections::HashMap;
    use std::sync::{Mutex, OnceLock};

    /// Descriptor, then substitutions. Eight is the longest list this crate
    /// builds; the assert says so rather than truncating quietly into a key that
    /// collides with a shorter one.
    type Key = [usize; 9];
    assert!(subst.len() < 9, "opaque substitution list longer than the cache key");
    let mut key: Key = [0; 9];
    key[0] = oret as usize;
    for (slot, p) in key[1..].iter_mut().zip(subst) {
        *slot = *p as usize;
    }

    static CACHE: OnceLock<Mutex<HashMap<Key, super::Resolved>>> = OnceLock::new();
    let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new()));
    if let Ok(map) = cache.lock() {
        if let Some(&(m, w, n)) = map.get(&key) {
            return (m as *const c_void, w as *const c_void, n);
        }
    }

    unsafe {
        type GetMeta =
            unsafe extern "C" fn(usize, *const c_void, *const c_void, usize) -> *const c_void;
        type GetConf = unsafe extern "C" fn(*const c_void, *const c_void, usize) -> *const c_void;
        // Resolved once. These were two `dlsym` calls per modifier.
        static SYMS: OnceLock<(usize, usize)> = OnceLock::new();
        let (meta_fn, conf_fn) = *SYMS.get_or_init(|| {
            (
                libc::dlsym((-2isize) as *mut c_void, c"swift_getOpaqueTypeMetadata".as_ptr())
                    as usize,
                libc::dlsym((-2isize) as *mut c_void, c"swift_getOpaqueTypeConformance2".as_ptr())
                    as usize,
            )
        });
        let get_meta: GetMeta = core::mem::transmute(meta_fn as *const c_void);
        let get_conf: GetConf = core::mem::transmute(conf_fn as *const c_void);

        let meta = get_meta(0, subst.as_ptr() as *const c_void, oret, 0);
        assert!(!meta.is_null(), "opaque result metadata");
        let wt = get_conf(subst.as_ptr() as *const c_void, oret, 1);
        assert!(!wt.is_null(), "opaque result View conformance");
        let n = abi::type_bytes(meta);
        if let Ok(mut map) = cache.lock() {
            map.insert(key, (meta as usize, wt as usize, n));
        }
        (meta, wt, n)
    }
}

/// Resolve `glassEffect`'s opaque result type.
pub(crate) unsafe fn glass_result_type(
    recv_meta: *const c_void,
    recv_wt: *const c_void,
) -> (*const c_void, *const c_void, usize) {
    unsafe {
        let oret = resolve::require(
            c"$s7SwiftUI4ViewPAAE11glassEffect_2inQrAA5GlassV_qd__tAA5ShapeRd__lFQOMQ",
        );
        let shape_meta = abi::metadata0(resolve::glass_shape_ma());
        let shape_wt = abi::get_view_wt_for_proto(shape_meta, resolve::shape_proto());
        // An opaque return is a function of `Self`; describing it as `AnyView`
        // while the call passes a concrete receiver sizes the result for a type
        // that was never built.
        let subst: [*const c_void; 4] = [recv_meta, shape_meta, recv_wt, shape_wt];
        type GetMeta = unsafe extern "C" fn(usize, *const c_void, *const c_void, usize) -> *const c_void;
        type GetConf = unsafe extern "C" fn(*const c_void, *const c_void, usize) -> *const c_void;
        let get_meta: GetMeta = core::mem::transmute(libc::dlsym(
            (-2isize) as *mut c_void, c"swift_getOpaqueTypeMetadata".as_ptr()));
        let get_conf: GetConf = core::mem::transmute(libc::dlsym(
            (-2isize) as *mut c_void, c"swift_getOpaqueTypeConformance2".as_ptr()));
        let meta = get_meta(0, subst.as_ptr() as *const c_void, oret, 0);
        assert!(!meta.is_null(), "glassEffect opaque metadata");
        let wt = get_conf(subst.as_ptr() as *const c_void, oret, 1);
        assert!(!wt.is_null(), "glassEffect opaque View conformance");
        (meta, wt, abi::type_bytes(meta))
    }
}

/// Apply `.tint(_:)`.
pub fn tint(view: &ViewHandle, c: crate::Color) -> ViewHandle {
    unsafe {
        let (_, _, size) = TINT_T.applied_to(view.meta);
        let mut result = vec![0u8; size];
        let this = view.value_ptr();
        // Optional<Color> is one class reference; nil is a null pointer.
        let mut style: u64 = color_value(c.r, c.g, c.b, c.a);
        // tint<S: ShapeStyle>(_: S?)
        //   x0 = @in S?, x1 = Self.Type, x2 = S.Type,
        //   x3 = Self: View WT, x4 = S: ShapeStyle WT
        core::arch::asm!("blr {f}", f = in(reg) resolve::tint_fn(),
            in("x0") &mut style as *mut u64,
            in("x1") view.meta,
            in("x2") resolve::color_meta(),
            in("x3") view.wt,
            in("x4") resolve::color_shapestyle_wt(),
            in("x8") result.as_mut_ptr(),
            in("x20") this,
            lateout("x5") _, lateout("x6") _, lateout("x7") _,
            lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _,
            lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _,
            lateout("x17") _, lateout("lr") _, lateout("x21") _, clobber_abi("C"),
        );
        wrap_modified(result, &TINT_T, view)
    }
}

/// Apply `.tint(_:)` through the `Color?` overload.
///
/// `View.tint` has two: one generic over `ShapeStyle`, which [`tint`] calls and
/// which yields `_EnvironmentKeyWritingModifier<AnyShapeStyle?>`, and one taking
/// a plain `Color?`, which yields `…<Color?>`. Swift picks the second for
/// `.tint(.white)`, so a reference written that way disagrees with the first on
/// both type and value.
pub fn tint_color(view: &ViewHandle, color: u64) -> ViewHandle {
    unsafe {
        super::opaque_modifier(
            resolve::tint_color_fn(),
            resolve::tint_color_oret(),
            view,
            &[color],
            [0.0; 4],
        )
    }
}

/// Apply `.tint(_:)` to a `Color` that is already built — one of SwiftUI's
/// named colours, which no set of components spells.
pub fn tint_value(view: &ViewHandle, tint: u64) -> ViewHandle {
    unsafe {
        let (_, _, size) = TINT_T.applied_to(view.meta);
        let mut result = vec![0u8; size];
        let this = view.value_ptr();
        // Optional<Color> is one class reference; nil is a null pointer.
        let mut style: u64 = tint;
        // tint<S: ShapeStyle>(_: S?)
        //   x0 = @in S?, x1 = Self.Type, x2 = S.Type,
        //   x3 = Self: View WT, x4 = S: ShapeStyle WT
        core::arch::asm!("blr {f}", f = in(reg) resolve::tint_fn(),
            in("x0") &mut style as *mut u64,
            in("x1") view.meta,
            in("x2") resolve::color_meta(),
            in("x3") view.wt,
            in("x4") resolve::color_shapestyle_wt(),
            in("x8") result.as_mut_ptr(),
            in("x20") this,
            lateout("x5") _, lateout("x6") _, lateout("x7") _,
            lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _,
            lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _,
            lateout("x17") _, lateout("lr") _, lateout("x21") _, clobber_abi("C"),
        );
        wrap_modified(result, &TINT_T, view)
    }
}

/// Apply `.preferredColorScheme(_:)` — light or dark for this subtree.
pub fn preferred_color_scheme(view: &ViewHandle, scheme: ColorScheme) -> ViewHandle {
    unsafe {
        let (_, _, size) = COLOR_SCHEME_T.applied_to(view.meta);
        // ColorScheme is resilient, so the optional is passed by pointer.
        let mut buf = [scheme as u8];
        let r = abi::call_modifier(resolve::color_scheme_fn(), view.value_ptr(), view.meta, view.wt,
            &[buf.as_mut_ptr() as u64],
            [0.0; 4],
            size,
        );
        wrap_modified(r, &COLOR_SCHEME_T, view)
    }
}

/// Apply `.controlSize(_:)`.
pub fn control_size(view: &ViewHandle, size_kind: ControlSize) -> ViewHandle {
    unsafe {
        let (_, _, size) = CONTROL_SIZE_T.applied_to(view.meta);
        let mut buf = [size_kind as u8];
        let r = abi::call_modifier(resolve::control_size_fn(), view.value_ptr(), view.meta, view.wt,
            &[buf.as_mut_ptr() as u64],
            [0.0; 4],
            size,
        );
        wrap_modified(r, &CONTROL_SIZE_T, view)
    }
}

/// Apply `.buttonStyle(_:)` to a subtree.
pub fn button_style(view: &ViewHandle, style: ButtonStyle) -> ViewHandle {
    unsafe {
        let (accessor, init, modifier) = match style {
            ButtonStyle::Glass => {
                (resolve::glass_button_ma(), resolve::glass_button_init(), &GLASS_BUTTON_T)
            }
            ButtonStyle::Bordered => {
                (resolve::bordered_button_ma(), resolve::bordered_button_init(), &BORDERED_BUTTON_T)
            }
            ButtonStyle::BorderedProminent => (
                resolve::bordered_prominent_ma(),
                resolve::bordered_prominent_init(),
                &BORDERED_PROMINENT_T,
            ),
        };
        let style_meta = abi::metadata0(accessor);
        let style_wt =
            abi::get_view_wt_for_proto(style_meta, resolve::primitive_button_style_proto());
        assert!(!style_wt.is_null(), "button style: PrimitiveButtonStyle");

        // The styles are resilient structs, so `init()` writes through x8.
        let mut value = vec![0u8; abi::type_bytes(style_meta)];
        core::arch::asm!("blr {f}", f = in(reg) init,
            in("x8") value.as_mut_ptr(),
            in("x20") style_meta,
            lateout("x0") _, lateout("x1") _, lateout("x2") _, lateout("x3") _,
            lateout("x4") _, lateout("x5") _, lateout("x6") _, lateout("x7") _,
            lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _,
            lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _,
            lateout("x17") _, lateout("lr") _, lateout("x21") _, clobber_abi("C"),
        );

        let (_, _, size) = modifier.applied_to(view.meta);
        let mut result = vec![0u8; size];
        let this = view.value_ptr();
        // buttonStyle<S: PrimitiveButtonStyle>(_: S)
        //   x0 = @in S, x1 = Self.Type, x2 = S.Type,
        //   x3 = Self: View WT, x4 = S: PrimitiveButtonStyle WT
        core::arch::asm!("blr {f}", f = in(reg) resolve::button_style_fn(),
            in("x0") value.as_mut_ptr(),
            in("x1") view.meta,
            in("x2") style_meta,
            in("x3") view.wt,
            in("x4") style_wt,
            in("x8") result.as_mut_ptr(),
            in("x20") this,
            lateout("x5") _, lateout("x6") _, lateout("x7") _,
            lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _,
            lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _,
            lateout("x17") _, lateout("lr") _, lateout("x21") _, clobber_abi("C"),
        );
        wrap_modified(result, modifier, view)
    }
}