156 lines
5.9 KiB
Rust
156 lines
5.9 KiB
Rust
use prometeu_bytecode::registry::{BuiltinId, DynamicSlot, StringSlot};
|
|
use prometeu_bytecode::{TRAP_TYPE, Value, assembler::assemble};
|
|
use prometeu_hal::{color::Color, vm_fault::VmFault};
|
|
use prometeu_vm::{
|
|
AbiType, BuiltinScalarType, BuiltinValueError, HostContext, HostReturn,
|
|
LogicalFrameEndingReason, NativeInterface, VirtualMachine, lookup_builtin_type,
|
|
lookup_intrinsic, materialize_builtin_constant,
|
|
};
|
|
|
|
#[test]
|
|
fn all_nine_descriptors_match_registry_storage_and_canonical_defaults() {
|
|
for (name, id, size, alignment, width, nullable) in [
|
|
("color", 1, 4, 4, 1, false),
|
|
("vec2", 2, 16, 8, 2, false),
|
|
("pixel", 3, 24, 8, 3, false),
|
|
("Int32", 0x10, 4, 4, 1, false),
|
|
("Int64", 0x11, 8, 8, 1, false),
|
|
("Float", 0x12, 8, 8, 1, false),
|
|
("Bool", 0x13, 1, 1, 1, false),
|
|
("String", 0x14, size_of::<StringSlot>(), align_of::<StringSlot>(), 1, true),
|
|
("Dynamic", 0x15, size_of::<DynamicSlot>(), align_of::<DynamicSlot>(), 2, true),
|
|
] {
|
|
let meta = lookup_builtin_type(name, 1).unwrap();
|
|
assert_eq!(meta.id, id);
|
|
let layout = meta.storage_layout().unwrap();
|
|
assert_eq!(layout, BuiltinId(id).layout().unwrap());
|
|
assert_eq!(
|
|
(layout.size, layout.alignment, layout.slots, layout.nullable()),
|
|
(size, alignment, width, nullable)
|
|
);
|
|
assert_eq!(u32::from(meta.flat_slot_width), width);
|
|
assert_eq!(meta.flatten_layout().len(), width as usize);
|
|
meta.validate_flat_values(layout.default_values()).unwrap();
|
|
assert!(lookup_builtin_type(name, 2).is_none());
|
|
}
|
|
for alias in [
|
|
"Int",
|
|
"Number",
|
|
"Void",
|
|
"Null",
|
|
"int",
|
|
"float",
|
|
"bool",
|
|
"str",
|
|
"input.pad",
|
|
"input.touch",
|
|
"input.button",
|
|
] {
|
|
assert!(lookup_builtin_type(alias, 1).is_none(), "{alias}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn primitive_carriers_are_exact_and_only_string_accepts_scalar_null() {
|
|
let cases = [
|
|
("Int32", Value::Int32(8)),
|
|
("Int64", Value::Int64(8)),
|
|
("Float", Value::Float(8.0)),
|
|
("Bool", Value::Boolean(true)),
|
|
("String", Value::String("eight".into())),
|
|
];
|
|
for (name, valid) in &cases {
|
|
let meta = lookup_builtin_type(name, 1).unwrap();
|
|
meta.validate_flat_values(std::slice::from_ref(valid)).unwrap();
|
|
for (other_name, invalid) in &cases {
|
|
if other_name != name {
|
|
assert!(
|
|
meta.validate_flat_values(std::slice::from_ref(invalid)).is_err(),
|
|
"{name} <- {other_name}"
|
|
);
|
|
}
|
|
}
|
|
assert_eq!(meta.validate_flat_values(&[Value::Null]).is_ok(), *name == "String");
|
|
}
|
|
let dynamic = lookup_builtin_type("Dynamic", 1).unwrap();
|
|
dynamic.validate_flat_values(&[Value::Null, Value::Null]).unwrap();
|
|
assert!(dynamic.validate_flat_values(&[Value::Null]).is_err());
|
|
assert_eq!(
|
|
dynamic.validate_flat_values(&[Value::Int32(0), Value::Int32(8)]),
|
|
Err(BuiltinValueError::RegistryRequired)
|
|
);
|
|
assert_eq!(
|
|
dynamic.validate_flat_values(&[Value::Null, Value::Int32(8)]),
|
|
Err(BuiltinValueError::RegistryRequired)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn color_range_and_aggregate_component_carriers_do_not_narrow() {
|
|
let color = lookup_builtin_type("color", 1).unwrap();
|
|
for raw in [0, 0x11223344, u32::MAX as i64] {
|
|
color.validate_flat_values(&[Value::Int64(raw)]).unwrap();
|
|
}
|
|
for value in [
|
|
Value::Int32(1),
|
|
Value::Int64(-1),
|
|
Value::Int64(u32::MAX as i64 + 1),
|
|
Value::Float(1.0),
|
|
Value::Null,
|
|
] {
|
|
assert!(color.validate_flat_values(&[value]).is_err());
|
|
}
|
|
let pixel = lookup_builtin_type("pixel", 1).unwrap();
|
|
let wide = [Value::Int64(i64::MIN), Value::Int64(i64::MAX), Value::Int64(u32::MAX as i64)];
|
|
pixel.validate_flat_values(&wide).unwrap();
|
|
assert!(
|
|
pixel.validate_flat_values(&[wide[2].clone(), wide[1].clone(), wide[0].clone()]).is_err()
|
|
);
|
|
for (field, slot) in [("x", 0), ("y", 1), ("color", 2)] {
|
|
assert_eq!(pixel.field(field).unwrap().start_slot, slot);
|
|
}
|
|
let vec2 = lookup_builtin_type("vec2", 1).unwrap();
|
|
vec2.validate_flat_values(&[Value::Float(1.25), Value::Float(-2.5)]).unwrap();
|
|
assert!(vec2.validate_flat_values(&[Value::Int64(1), Value::Float(2.0)]).is_err());
|
|
assert_eq!(
|
|
materialize_builtin_constant("color", "red", 1).unwrap().unwrap(),
|
|
vec![Value::Int64(Color::RED.raw() as i64)]
|
|
);
|
|
}
|
|
|
|
struct NoSyscalls;
|
|
impl NativeInterface for NoSyscalls {
|
|
fn syscall(
|
|
&mut self,
|
|
_: u32,
|
|
_: &[Value],
|
|
_: &mut HostReturn,
|
|
_: &mut HostContext,
|
|
) -> Result<(), VmFault> {
|
|
panic!("unexpected syscall")
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn intrinsic_boundaries_reject_implicit_carrier_conversions_in_execution() {
|
|
for source in [
|
|
"PUSH_I32 1\nINTRINSIC 0x1100", // color.from_raw requires Int64
|
|
"PUSH_I32 1\nINTRINSIC 0x1106", // color.alpha requires the color carrier
|
|
"PUSH_I64 1\nPUSH_F64 f64:0x4000000000000000\nINTRINSIC 0x1001", // Float/Float required
|
|
"PUSH_I64 -1\nINTRINSIC 0x1106", // out-of-range color
|
|
] {
|
|
let mut vm = VirtualMachine::new(assemble(source).unwrap(), vec![]);
|
|
vm.prepare_call("0").expect("prepare entry call");
|
|
let report = vm.run_budget(100, &mut NoSyscalls, &mut HostContext::new(None)).unwrap();
|
|
assert!(
|
|
matches!(report.reason, LogicalFrameEndingReason::Trap(trap) if trap.code == TRAP_TYPE),
|
|
"{source}"
|
|
);
|
|
}
|
|
let hold = lookup_intrinsic("input.pad.up", "hold", 1).unwrap();
|
|
assert_eq!(hold.ret_layout, &[AbiType::Scalar(BuiltinScalarType::Int64)]);
|
|
assert!(hold.validate_result_values(&[Value::Int32(1)]).is_err());
|
|
let color = lookup_intrinsic("color", "red", 1).unwrap();
|
|
assert!(color.validate_result_values(&[Value::Int64(-1)]).is_err());
|
|
}
|