xur-runtime 0.1.5

Values, native-function registry and facet conversions for the Xur language
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! Converting between Rust values and Xur values, driven by facet shapes.
//!
//! | Rust                               | Xur                          |
//! | ---------------------------------- | ---------------------------- |
//! | `bool`                             | `bool`                       |
//! | integers                           | integers (range-checked)     |
//! | `f32`, `f64`                       | floats                       |
//! | `String`, `&str`, `char`           | strings                      |
//! | `()`                               | `()`                         |
//! | tuples                             | tuples                       |
//! | `Vec<T>`, arrays, slices           | lists                        |
//! | `Option<T>`, `Result<T, E>`        | the prelude's `Option`, `Result` |
//! | `Vec<u8>`, `&[u8]`, `[u8; N]`      | bytes                        |
//! | registered types                   | native objects               |
//! | other structs and enums            | records, named by a type registered for their shape |
//! | opaque types up to 8 bytes, like bitflags | their bits, as an integer |

use facet::{
    Def, Facet, Partial, Peek, ScalarType, Shape, ShapeLayout, StructKind, StructType, Type,
    UserType,
};
use xur_span::Symbol;

use crate::value::{Adt, FloatValue, IntValue, NativeObject, Record, lock};
use crate::{LangItems, Registry, RunResult, RuntimeError, Value};

/// What conversions need to know about the running program.
pub trait ConvertCx {
    fn lang(&self) -> &LangItems;
    /// The registry, for converting registered types.
    fn registry(&self) -> &Registry;
}

/// Converts a Rust value to a Xur value, cloning it if it is of a
/// registered type.
pub fn to_value<T: Facet<'static>>(cx: &dyn ConvertCx, value: &T) -> RunResult<Value> {
    peek_to_value(cx, Peek::new(value))
}

/// Converts a Rust value to a Xur value, moving it into a native object if
/// it is of a registered type.
pub fn into_value<T: Facet<'static> + Send + 'static>(
    cx: &dyn ConvertCx,
    value: T,
) -> RunResult<Value> {
    match cx.registry().type_of_shape(T::SHAPE) {
        Some(ty) if is_enum_shape(T::SHAPE) => {
            let (variant, fields) = peek_enum_fields(cx, Peek::new(&value))?;
            let object = NativeObject::new(ty, Box::new(value));
            Ok(Value::NativeEnum(
                crate::value::NativeEnum {
                    object: object.into(),
                    variant,
                    fields,
                }
                .into(),
            ))
        }
        Some(ty) => Ok(Value::Native(NativeObject::new(ty, Box::new(value)).into())),
        None => to_value(cx, &value),
    }
}

/// Converts a Xur value to a Rust value of type `T`, failing with a runtime
/// error if the value doesn't fit.
pub fn from_value<T: Facet<'static>>(cx: &dyn ConvertCx, value: &Value) -> RunResult<T> {
    let partial = Partial::alloc_owned::<T>().map_err(reflect_error)?;
    let partial = build(cx, partial, value)?;
    let heap_value = partial.build().map_err(reflect_error)?;
    heap_value.materialize::<T>().map_err(reflect_error)
}

/// Converts the value `peek` looks at, as [`to_value`] does.
pub fn peek_to_value(cx: &dyn ConvertCx, peek: Peek<'_, 'static>) -> RunResult<Value> {
    let shape = peek.shape();
    let registry = cx.registry();
    if let Some(ty) = registry.type_of_shape(shape) {
        if is_enum_shape(shape) {
            let (variant, fields) = peek_enum_fields(cx, peek)?;
            let object = registry
                .object_from_peek(ty, peek)
                .ok_or_else(|| unsupported(shape))?;
            return Ok(Value::NativeEnum(
                crate::value::NativeEnum {
                    object: object.into(),
                    variant,
                    fields,
                }
                .into(),
            ));
        }
        let object = registry
            .object_from_peek(ty, peek)
            .ok_or_else(|| unsupported(shape))?;
        return Ok(Value::Native(object.into()));
    }
    if let Some(convert) = registry.converter_of(shape) {
        return convert(cx, peek);
    }
    if let Some(scalar) = peek.scalar_type() {
        return scalar_to_value(peek, scalar);
    }
    if shape == <Vec<u8>>::SHAPE {
        let bytes: &Vec<u8> = peek.get().map_err(reflect_error)?;
        return Ok(Value::bytes(bytes.clone()));
    }
    if let Some(bytes) = peek.as_bytes() {
        return Ok(Value::bytes(bytes.to_vec()));
    }
    let lang = cx.lang();
    match shape.def {
        Def::Option(_) => {
            return Ok(match peek.into_option().map_err(reflect_error)?.value() {
                Some(inner) => Value::adt(lang.some, vec![peek_to_value(cx, inner)?]),
                None => Value::adt(lang.none, Vec::new()),
            });
        }
        Def::Result(_) => {
            let result = peek.into_result().map_err(reflect_error)?;
            return Ok(match (result.ok(), result.err()) {
                (Some(ok), _) => Value::adt(lang.ok, vec![peek_to_value(cx, ok)?]),
                (_, Some(err)) => Value::adt(lang.err, vec![peek_to_value(cx, err)?]),
                (None, None) => unreachable!("a result is either ok or err"),
            });
        }
        Def::List(_) | Def::Array(_) | Def::Slice(_) => {
            let items = peek.into_list_like().map_err(reflect_error)?;
            if let Some(bytes) = items.as_bytes() {
                return Ok(Value::bytes(bytes.to_vec()));
            }
            let items = items
                .iter()
                .map(|item| peek_to_value(cx, item))
                .collect::<RunResult<_>>()?;
            return Ok(Value::list(items));
        }
        _ => {}
    }
    let ty = registry.record_type_of_shape(shape);
    let record = |variant: Option<&str>, fields: &[facet::Field], values: Vec<Value>| {
        let fields = fields
            .iter()
            .map(|field| Symbol::intern(field.name))
            .zip(values)
            .collect();
        Value::Record(
            Record {
                ty,
                variant: variant.map(Symbol::intern),
                fields,
            }
            .into(),
        )
    };
    match shape.ty {
        Type::User(UserType::Struct(StructType { kind, fields, .. })) => {
            let peek = peek.into_struct().map_err(reflect_error)?;
            let values = (0..fields.len())
                .map(|i| peek_to_value(cx, peek.field(i).map_err(reflect_error)?))
                .collect::<RunResult<Vec<_>>>()?;
            Ok(match kind {
                StructKind::Tuple => Value::tuple(values),
                StructKind::Unit | StructKind::Struct | StructKind::TupleStruct => {
                    record(None, fields, values)
                }
            })
        }
        Type::User(UserType::Enum(_)) => {
            let peek = peek.into_enum().map_err(reflect_error)?;
            let variant = peek.active_variant().map_err(reflect_error)?;
            let fields = variant.data.fields;
            let values = (0..fields.len())
                .map(|i| match peek.field(i).map_err(reflect_error)? {
                    Some(field) => peek_to_value(cx, field),
                    None => Err(unsupported(shape)),
                })
                .collect::<RunResult<Vec<_>>>()?;
            Ok(record(Some(variant.name), fields, values))
        }
        Type::User(UserType::Opaque) => opaque_bits(peek).ok_or_else(|| unsupported(shape)),
        _ => Err(unsupported(shape)),
    }
}

fn is_enum_shape(shape: &'static Shape) -> bool {
    matches!(shape.ty, Type::User(UserType::Enum(_)))
}

fn peek_enum_fields(
    cx: &dyn ConvertCx,
    peek: Peek<'_, 'static>,
) -> RunResult<(Symbol, Vec<(Symbol, Value)>)> {
    let peek = peek.into_enum().map_err(reflect_error)?;
    let variant = peek.active_variant().map_err(reflect_error)?;
    let fields: Vec<(Symbol, Value)> = (0..variant.data.fields.len())
        .map(|i| {
            let field_name = Symbol::intern(variant.data.fields[i].name);
            match peek.field(i).map_err(reflect_error)? {
                Some(field) => Ok((field_name, peek_to_value(cx, field)?)),
                None => Err(unsupported(peek.shape())),
            }
        })
        .collect::<RunResult<_>>()?;
    Ok((Symbol::intern(variant.name), fields))
}

/// The bits of a small opaque value, such as a set of bitflags, which is
/// all of it that can be shown.
fn opaque_bits(peek: Peek<'_, 'static>) -> Option<Value> {
    let ShapeLayout::Sized(layout) = peek.shape().layout else {
        return None;
    };
    let size = layout.size();
    if !matches!(size, 1 | 2 | 4 | 8) {
        return None;
    }
    let mut bits = [0u8; 8];
    // SAFETY: the value is `layout.size()` initialised bytes; a type this
    // small with no visible fields is a plain integer, like bitflags.
    let raw = unsafe { std::slice::from_raw_parts(peek.data().as_byte_ptr(), size) };
    bits[..raw.len()].copy_from_slice(raw);
    let int = match size {
        1 => IntValue::U8(bits[0]),
        2 => IntValue::U16(u16::from_ne_bytes([bits[0], bits[1]])),
        4 => IntValue::U32(u32::from_ne_bytes([bits[0], bits[1], bits[2], bits[3]])),
        8 => IntValue::U64(u64::from_ne_bytes(bits)),
        _ => return None,
    };
    Some(Value::Int(int))
}

fn scalar_to_value(peek: Peek<'_, 'static>, scalar: ScalarType) -> RunResult<Value> {
    fn get<T: Facet<'static> + Copy>(peek: Peek<'_, 'static>) -> RunResult<T> {
        peek.get::<T>().copied().map_err(reflect_error)
    }
    Ok(match scalar {
        ScalarType::Unit => Value::Unit,
        ScalarType::Bool => Value::Bool(get(peek)?),
        ScalarType::Char => Value::str(get::<char>(peek)?.encode_utf8(&mut [0; 4])),
        ScalarType::Str | ScalarType::String | ScalarType::CowStr => {
            Value::str(peek.as_str().ok_or_else(|| unsupported(peek.shape()))?)
        }
        ScalarType::F32 => Value::Float(FloatValue::F32(get(peek)?)),
        ScalarType::F64 => Value::Float(FloatValue::F64(get(peek)?)),
        ScalarType::U8 => Value::Int(IntValue::U8(get(peek)?)),
        ScalarType::U16 => Value::Int(IntValue::U16(get(peek)?)),
        ScalarType::U32 => Value::Int(IntValue::U32(get(peek)?)),
        ScalarType::U64 => Value::Int(IntValue::U64(get(peek)?)),
        ScalarType::U128 => {
            let v = get::<u128>(peek)?;
            let v = u64::try_from(v)
                .map_err(|_| RuntimeError::new(format!("{v} does not fit in a Xur integer")))?;
            Value::Int(IntValue::U64(v))
        }
        ScalarType::USize => Value::Int(IntValue::U64(get::<usize>(peek)? as u64)),
        ScalarType::I8 => Value::Int(IntValue::I8(get(peek)?)),
        ScalarType::I16 => Value::Int(IntValue::I16(get(peek)?)),
        ScalarType::I32 => Value::Int(IntValue::I32(get(peek)?)),
        ScalarType::I64 => Value::Int(IntValue::I64(get(peek)?)),
        ScalarType::I128 => {
            let v = get::<i128>(peek)?;
            let v = i64::try_from(v)
                .map_err(|_| RuntimeError::new(format!("{v} does not fit in a Xur integer")))?;
            Value::Int(IntValue::I64(v))
        }
        ScalarType::ISize => Value::Int(IntValue::I64(get::<isize>(peek)? as i64)),
        _ => return Err(unsupported(peek.shape())),
    })
}

/// A value of some Rust type under construction.
pub(crate) type Builder = Partial<'static, false>;

/// Fills in the value under construction in `partial` from `value`.
fn build(cx: &dyn ConvertCx, partial: Builder, value: &Value) -> RunResult<Builder> {
    let shape = partial.shape();
    let registry = cx.registry();
    if let Some(ty) = registry.type_of_shape(shape) {
        return match value {
            Value::Native(object) if object.ty == ty => registry.set_object(partial, object),
            Value::NativeEnum(ne) if ne.object.ty == ty => registry.set_object(partial, &ne.object),
            _ => Err(mismatch(shape, value)),
        };
    }
    if let Some(scalar) = ScalarType::try_from_shape(shape) {
        return set_scalar(partial, scalar, value);
    }
    // A `Vec<u8>` takes bytes, or a list of byte values.
    if shape == <Vec<u8>>::SHAPE {
        let bytes = match value {
            Value::Bytes(bytes) => lock(bytes).clone(),
            Value::List(items) => lock(items)
                .iter()
                .map(|item| match item {
                    Value::Int(i) => u8::try_from(i.to_i128())
                        .map_err(|_| RuntimeError::new(format!("{i} does not fit in `u8`"))),
                    other => Err(mismatch(u8::SHAPE, other)),
                })
                .collect::<RunResult<_>>()?,
            _ => return Err(mismatch(shape, value)),
        };
        return partial.set(bytes).map_err(reflect_error);
    }
    let lang = cx.lang();
    match (&shape.def, value) {
        (Def::Option(_), Value::Adt(adt)) if adt.ctor == lang.some => {
            return build_inner(cx, partial.begin_some(), adt);
        }
        (Def::Option(_), Value::Adt(adt)) if adt.ctor == lang.none => {
            return partial.set_default().map_err(reflect_error);
        }
        (Def::Result(_), Value::Adt(adt)) if adt.ctor == lang.ok => {
            return build_inner(cx, partial.begin_ok(), adt);
        }
        (Def::Result(_), Value::Adt(adt)) if adt.ctor == lang.err => {
            return build_inner(cx, partial.begin_err(), adt);
        }
        (Def::Option(_) | Def::Result(_), _) => return Err(mismatch(shape, value)),
        (Def::List(_), Value::List(items)) => {
            let mut partial = partial.init_list().map_err(reflect_error)?;
            for item in lock(items).iter() {
                let element = partial.begin_list_item().map_err(reflect_error)?;
                partial = build(cx, element, item)?.end().map_err(reflect_error)?;
            }
            return Ok(partial);
        }
        (Def::List(_), _) => return Err(mismatch(shape, value)),
        _ => {}
    }
    match (shape.ty, value) {
        (Type::User(UserType::Struct(st)), Value::Tuple(items)) if st.kind == StructKind::Tuple => {
            if items.len() != st.fields.len() {
                return Err(mismatch(shape, value));
            }
            build_fields(cx, partial, items.iter())
        }
        (Type::User(UserType::Enum(_)), Value::Record(record)) => {
            let Some(variant) = record.variant else {
                return Err(mismatch(shape, value));
            };
            let mut partial = partial
                .select_variant_named(variant.as_str())
                .map_err(reflect_error)?;
            let fields = partial
                .selected_variant()
                .map(|variant| variant.data.fields)
                .unwrap_or_default();
            for (i, field) in fields.iter().enumerate() {
                let value = record.field(Symbol::intern(field.name)).ok_or_else(|| {
                    RuntimeError::new(format!("missing field `{}` for `{shape}`", field.name))
                })?;
                let inner = partial.begin_nth_field(i).map_err(reflect_error)?;
                partial = build(cx, inner, value)?.end().map_err(reflect_error)?;
            }
            Ok(partial)
        }
        (Type::User(UserType::Struct(st)), Value::Record(record))
            if st.kind != StructKind::Tuple =>
        {
            let values = st
                .fields
                .iter()
                .map(|field| {
                    record.field(Symbol::intern(field.name)).ok_or_else(|| {
                        RuntimeError::new(format!("missing field `{}` for `{shape}`", field.name))
                    })
                })
                .collect::<RunResult<Vec<_>>>()?;
            build_fields(cx, partial, values.into_iter())
        }
        _ => Err(mismatch(shape, value)),
    }
}

/// Builds the single field of a `Some`, `Ok` or `Err`, entered with `begin`.
fn build_inner(
    cx: &dyn ConvertCx,
    begin: Result<Builder, facet::ReflectError>,
    adt: &Adt,
) -> RunResult<Builder> {
    let inner = begin.map_err(reflect_error)?;
    build(cx, inner, &adt.fields[0])?
        .end()
        .map_err(reflect_error)
}

fn build_fields<'v>(
    cx: &dyn ConvertCx,
    mut partial: Builder,
    values: impl Iterator<Item = &'v Value>,
) -> RunResult<Builder> {
    for (i, value) in values.enumerate() {
        let field = partial.begin_nth_field(i).map_err(reflect_error)?;
        partial = build(cx, field, value)?.end().map_err(reflect_error)?;
    }
    Ok(partial)
}

fn set_scalar(partial: Builder, scalar: ScalarType, value: &Value) -> RunResult<Builder> {
    let shape = partial.shape();
    macro_rules! int {
        ($ty:ty) => {{
            let wide = match *value {
                Value::Int(i) => i.to_i128(),
                Value::Ptr(p) => p as i128,
                _ => return Err(mismatch(shape, value)),
            };
            let int = <$ty>::try_from(wide).map_err(|_| {
                RuntimeError::new(format!("{wide} does not fit in `{}`", stringify!($ty)))
            })?;
            partial.set(int)
        }};
    }
    let result = match (scalar, value) {
        (ScalarType::Unit, Value::Unit) => partial.set(()),
        (ScalarType::Bool, &Value::Bool(b)) => partial.set(b),
        (ScalarType::String, Value::Str(s)) => partial.set(s.to_string()),
        (ScalarType::Char, Value::Str(s)) if s.chars().count() == 1 => {
            partial.set(s.chars().next().expect("one char"))
        }
        (ScalarType::F64, Value::Float(f)) => partial.set(f.as_f64()),
        (ScalarType::F32, Value::Float(f)) => partial.set(f.as_f32()),
        (ScalarType::U8, _) => int!(u8),
        (ScalarType::U16, _) => int!(u16),
        (ScalarType::U32, _) => int!(u32),
        (ScalarType::U64, _) => int!(u64),
        (ScalarType::U128, _) => int!(u128),
        (ScalarType::USize, _) => int!(usize),
        (ScalarType::I8, _) => int!(i8),
        (ScalarType::I16, _) => int!(i16),
        (ScalarType::I32, _) => int!(i32),
        (ScalarType::I64, _) => int!(i64),
        (ScalarType::I128, _) => int!(i128),
        (ScalarType::ISize, _) => int!(isize),
        _ => return Err(mismatch(shape, value)),
    };
    result.map_err(reflect_error)
}

fn mismatch(shape: &Shape, value: &Value) -> RuntimeError {
    RuntimeError::new(format!("expected `{shape}`, found {}", value.kind()))
}

fn unsupported(shape: &Shape) -> RuntimeError {
    RuntimeError::new(format!("`{shape}` cannot be converted to a Xur value"))
}

pub fn reflect_error(error: impl std::fmt::Display) -> RuntimeError {
    RuntimeError::new(format!("conversion failed: {error}"))
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use xur_resolve::Program;
    use xur_span::MemoryFileLoader;

    use super::*;

    const TEST_PRELUDE: &str = "\
#[try(unwrap = \"Ok\")]
pub enum Result<T, E = str> { Ok(T) Err(E) }
pub use Result::Ok;
pub use Result::Err;
#[try(unwrap = \"Some\")]
pub enum Option<T> { Some(T) None }
pub use Option::Some;
pub use Option::None;
";

    #[derive(Facet, Debug, Clone, PartialEq)]
    struct Point {
        x: f64,
        label: String,
    }

    #[derive(Facet, Debug, Clone, PartialEq)]
    struct Label {
        text: String,
    }

    #[derive(Facet, Debug, Clone, PartialEq)]
    #[repr(C)]
    enum Shape {
        Empty,
        Circle(f64),
        Rect { w: u32, h: u32 },
    }

    #[derive(Facet, Debug, Clone, PartialEq)]
    struct Drawing {
        shape: Shape,
        data: Vec<u8>,
        magic: [u8; 2],
    }

    /// Registers `Point`; `Label` stays unregistered.
    struct Cx(LangItems, Registry);

    impl ConvertCx for Cx {
        fn lang(&self) -> &LangItems {
            &self.0
        }

        fn registry(&self) -> &Registry {
            &self.1
        }
    }

    fn cx() -> Cx {
        let loader = MemoryFileLoader::new([("main.xur", "")]);
        let (program, _) = Program::load(
            Path::new("main.xur"),
            Box::new(loader),
            Box::new(Vec::new()),
            Some(TEST_PRELUDE),
        )
        .unwrap();
        let registry = Registry::new();
        registry.module("geo").ty::<Point>("Point");
        registry.module("geo").shape_type("Drawing", Drawing::SHAPE);
        Cx(program.lang_items(), registry)
    }

    fn round_trip<T: Facet<'static> + PartialEq + std::fmt::Debug>(value: T) -> Value {
        let cx = cx();
        let converted = to_value(&cx, &value).unwrap();
        assert_eq!(from_value::<T>(&cx, &converted).unwrap(), value);
        converted
    }

    #[test]
    fn scalars() {
        assert_eq!(round_trip(true), Value::Bool(true));
        assert_eq!(round_trip(42u8), Value::Int(IntValue::U8(42)));
        assert_eq!(round_trip(-7i64), Value::int(-7));
        assert_eq!(round_trip(1.5f64), Value::float(1.5));
        assert_eq!(round_trip(String::from("hi")), Value::str("hi"));
        assert_eq!(round_trip('x'), Value::str("x"));
        assert_eq!(round_trip(()), Value::Unit);
    }

    #[test]
    fn integer_widths_preserved() {
        let cx = cx();
        let v = to_value(&cx, &42u64).unwrap();
        assert!(matches!(v, Value::Int(IntValue::U64(42))));
        let v = to_value(&cx, &-1i8).unwrap();
        assert!(matches!(v, Value::Int(IntValue::I8(-1))));
        assert_eq!(
            from_value::<u64>(&cx, &Value::Int(IntValue::U64(u64::MAX))).unwrap(),
            u64::MAX
        );
    }

    #[test]
    fn compound_values() {
        assert_eq!(
            round_trip(vec![1u32, 2]),
            Value::list(vec![
                Value::Int(IntValue::U32(1)),
                Value::Int(IntValue::U32(2))
            ])
        );
        assert_eq!(
            round_trip((1i32, String::from("a"))),
            Value::tuple(vec![Value::Int(IntValue::I32(1)), Value::str("a")])
        );
        let lang = cx().0;
        assert_eq!(
            round_trip(Some(3u8)),
            Value::adt(lang.some, vec![Value::Int(IntValue::U8(3))])
        );
        assert_eq!(round_trip(None::<u8>), Value::adt(lang.none, vec![]));
        assert_eq!(
            round_trip(Err::<u8, String>("bad".into())),
            Value::adt(lang.err, vec![Value::str("bad")])
        );
    }

    #[test]
    fn byte_vectors_are_bytes() {
        assert_eq!(round_trip(vec![1u8, 255]), Value::bytes(vec![1, 255]));
    }

    #[test]
    fn registered_types_become_native_objects() {
        let point = Point {
            x: 1.0,
            label: "p".into(),
        };
        let cx = cx();
        let value = into_value(&cx, point.clone()).unwrap();
        let Value::Native(object) = &value else {
            panic!("expected a native object, found {value:?}");
        };
        assert_eq!(object.with(Point::clone), Some(point.clone()));
        assert_eq!(from_value::<Point>(&cx, &value).unwrap(), point);
        // Nested in other data, they are cloned in and out.
        let list = to_value(&cx, &vec![point.clone()]).unwrap();
        assert_eq!(from_value::<Vec<Point>>(&cx, &list).unwrap(), [point]);
    }

    #[test]
    fn other_structs_become_records() {
        let value = round_trip(Label { text: "l".into() });
        let Value::Record(record) = value else {
            panic!("expected a record, found {value:?}");
        };
        assert_eq!(record.field(Symbol::intern("text")), Some(&Value::str("l")));
    }

    #[test]
    fn out_of_range_integers_are_errors() {
        let error = from_value::<u8>(&cx(), &Value::Int(IntValue::I32(300))).unwrap_err();
        assert_eq!(error.message, "300 does not fit in `u8`");
    }

    #[test]
    fn mismatched_values_are_errors() {
        let error = from_value::<u32>(&cx(), &Value::str("1")).unwrap_err();
        assert_eq!(error.message, "expected `u32`, found string");
        let error = from_value::<Vec<u32>>(&cx(), &Value::int(1)).unwrap_err();
        assert_eq!(error.message, "expected `Vec<u32>`, found integer");
    }

    #[test]
    fn enums_become_records_with_their_variant() {
        let value = round_trip(Shape::Rect { w: 2, h: 3 });
        let Value::Record(record) = &value else {
            panic!("expected a record, found {value:?}");
        };
        assert_eq!(record.variant, Some(Symbol::intern("Rect")));
        assert_eq!(
            record.field(Symbol::intern("h")),
            Some(&Value::Int(IntValue::U32(3)))
        );
        round_trip(Shape::Empty);
        round_trip(Shape::Circle(1.5));
    }

    #[test]
    fn shape_types_name_their_records() {
        let cx = cx();
        let drawing = Drawing {
            shape: Shape::Circle(2.0),
            data: vec![1, 2],
            magic: *b"MZ",
        };
        let value = to_value(&cx, &drawing).unwrap();
        let Value::Record(record) = &value else {
            panic!("expected a record, found {value:?}");
        };
        assert_eq!(record.ty, cx.1.record_type_of_shape(Drawing::SHAPE));
        assert!(record.ty.is_some());
        assert_eq!(
            record.field(Symbol::intern("magic")),
            Some(&Value::bytes(b"MZ".to_vec()))
        );
        // Not a native object: `Drawing` was registered by shape only.
        assert_eq!(cx.1.type_of_shape(Drawing::SHAPE), None);
    }

    #[test]
    fn converters_replace_the_conversion_of_a_shape() {
        let cx = cx();
        cx.1.converter(Label::SHAPE, |_, peek| {
            let label: &Label = peek.get().map_err(reflect_error)?;
            Ok(Value::str(&label.text.to_uppercase()))
        });
        let label = Label { text: "hi".into() };
        assert_eq!(to_value(&cx, &label), Ok(Value::str("HI")));
    }
}