feat(registry): checkpoint PLN-0176 foundations and validated partial integrations
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
use prometeu_bytecode::Value;
|
||||
use prometeu_bytecode::{
|
||||
Value,
|
||||
registry::{BuiltinId, StorageLayout},
|
||||
};
|
||||
use prometeu_hal::{HostContext, color::Color};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
@@ -53,14 +56,18 @@ impl IntrinsicKey {
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum BuiltinScalarType {
|
||||
Int,
|
||||
Int32,
|
||||
Int64,
|
||||
Float,
|
||||
Bool,
|
||||
Str,
|
||||
String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum AbiType {
|
||||
/// Internal group components; neither can establish a valid Dynamic alone.
|
||||
DynamicTag,
|
||||
DynamicPayload,
|
||||
Scalar(BuiltinScalarType),
|
||||
Builtin(BuiltinTypeKey),
|
||||
}
|
||||
@@ -75,6 +82,7 @@ pub enum BuiltinLayoutType {
|
||||
pub enum BuiltinTypeShape {
|
||||
Scalar,
|
||||
Aggregate,
|
||||
Dynamic,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -111,14 +119,33 @@ impl BuiltinTypeMeta {
|
||||
out
|
||||
}
|
||||
|
||||
pub fn storage_layout(
|
||||
&self,
|
||||
) -> Result<StorageLayout, prometeu_bytecode::load_limits::LoadFailure> {
|
||||
BuiltinId(self.id).layout()
|
||||
}
|
||||
|
||||
pub fn validate_flat_values(&self, values: &[Value]) -> Result<(), BuiltinValueError> {
|
||||
if self.shape == BuiltinTypeShape::Dynamic {
|
||||
if values.len() != 2 {
|
||||
return Err(BuiltinValueError::ArityMismatch { expected: 2, got: values.len() });
|
||||
}
|
||||
if matches!(values, [Value::Null, Value::Null]) {
|
||||
return Ok(());
|
||||
}
|
||||
// A non-null tag is a program TypeId, and its payload may be a box.
|
||||
// This context-free catalogue must never claim to validate it.
|
||||
return Err(BuiltinValueError::RegistryRequired);
|
||||
}
|
||||
let layout = self.flatten_layout();
|
||||
validate_values_against_layout(values, &layout)
|
||||
}
|
||||
|
||||
fn flatten_into(&self, out: &mut Vec<AbiType>) {
|
||||
match self.shape {
|
||||
BuiltinTypeShape::Scalar => out.extend_from_slice(self.flat_slot_layout),
|
||||
BuiltinTypeShape::Scalar | BuiltinTypeShape::Dynamic => {
|
||||
out.extend_from_slice(self.flat_slot_layout)
|
||||
}
|
||||
BuiltinTypeShape::Aggregate => {
|
||||
for field in self.fields {
|
||||
flatten_layout_type(field.field_type, out);
|
||||
@@ -134,7 +161,7 @@ pub enum BuiltinConstSlotValue {
|
||||
Int64(i64),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
NominalBuiltin { builtin: BuiltinTypeKey, carrier: i32 },
|
||||
NominalBuiltin { builtin: BuiltinTypeKey, carrier: u32 },
|
||||
}
|
||||
|
||||
pub type BuiltinConstHook = fn() -> Vec<Value>;
|
||||
@@ -178,12 +205,12 @@ impl BuiltinConstMeta {
|
||||
pub enum IntrinsicExecutionError {
|
||||
ArityMismatch { expected: usize, got: usize },
|
||||
TypeMismatch { index: usize, expected: AbiType },
|
||||
InvalidBuiltinCarrier { owner: &'static str, name: &'static str, carrier: i64 },
|
||||
HardwareUnavailable,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum BuiltinValueError {
|
||||
RegistryRequired,
|
||||
ArityMismatch { expected: usize, got: usize },
|
||||
TypeMismatch { index: usize, expected: AbiType },
|
||||
}
|
||||
@@ -218,14 +245,28 @@ impl IntrinsicMeta {
|
||||
}
|
||||
|
||||
pub fn validate_result_values(&self, values: &[Value]) -> Result<(), IntrinsicExecutionError> {
|
||||
if values.len() != self.ret_slots() {
|
||||
Self::validate_values(values, self.ret_layout)
|
||||
}
|
||||
|
||||
pub fn validate_argument_values(
|
||||
&self,
|
||||
values: &[Value],
|
||||
) -> Result<(), IntrinsicExecutionError> {
|
||||
Self::validate_values(values, self.arg_layout)
|
||||
}
|
||||
|
||||
fn validate_values(
|
||||
values: &[Value],
|
||||
layout: &[AbiType],
|
||||
) -> Result<(), IntrinsicExecutionError> {
|
||||
if values.len() != layout.len() {
|
||||
return Err(IntrinsicExecutionError::ArityMismatch {
|
||||
expected: self.ret_slots(),
|
||||
expected: layout.len(),
|
||||
got: values.len(),
|
||||
});
|
||||
}
|
||||
|
||||
for (index, (value, expected)) in values.iter().zip(self.ret_layout.iter()).enumerate() {
|
||||
for (index, (value, expected)) in values.iter().zip(layout.iter()).enumerate() {
|
||||
if !value_matches_abi_type(value, *expected) {
|
||||
return Err(IntrinsicExecutionError::TypeMismatch { index, expected: *expected });
|
||||
}
|
||||
@@ -242,7 +283,9 @@ impl BuiltinConstSlotValue {
|
||||
BuiltinConstSlotValue::Int64(value) => Value::Int64(value),
|
||||
BuiltinConstSlotValue::Float(value) => Value::Float(value),
|
||||
BuiltinConstSlotValue::Bool(value) => Value::Boolean(value),
|
||||
BuiltinConstSlotValue::NominalBuiltin { carrier, .. } => Value::Int32(carrier),
|
||||
BuiltinConstSlotValue::NominalBuiltin { carrier, .. } => {
|
||||
Value::Int64(i64::from(carrier))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -250,21 +293,15 @@ impl BuiltinConstSlotValue {
|
||||
const COLOR: BuiltinTypeKey = BuiltinTypeKey::new("color", 1);
|
||||
const VEC2: BuiltinTypeKey = BuiltinTypeKey::new("vec2", 1);
|
||||
const PIXEL: BuiltinTypeKey = BuiltinTypeKey::new("pixel", 1);
|
||||
const INPUT_PAD: BuiltinTypeKey = BuiltinTypeKey::new("input.pad", 1);
|
||||
const INPUT_TOUCH: BuiltinTypeKey = BuiltinTypeKey::new("input.touch", 1);
|
||||
const INPUT_BUTTON: BuiltinTypeKey = BuiltinTypeKey::new("input.button", 1);
|
||||
|
||||
const COLOR_LAYOUT: [AbiType; 1] = [AbiType::Builtin(COLOR)];
|
||||
const VEC2_LAYOUT: [AbiType; 2] =
|
||||
[AbiType::Scalar(BuiltinScalarType::Float), AbiType::Scalar(BuiltinScalarType::Float)];
|
||||
const PIXEL_LAYOUT: [AbiType; 3] = [
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Builtin(COLOR),
|
||||
];
|
||||
const INPUT_PAD_LAYOUT: [AbiType; 1] = [AbiType::Builtin(INPUT_PAD)];
|
||||
const INPUT_TOUCH_LAYOUT: [AbiType; 1] = [AbiType::Builtin(INPUT_TOUCH)];
|
||||
const INPUT_BUTTON_LAYOUT: [AbiType; 1] = [AbiType::Builtin(INPUT_BUTTON)];
|
||||
|
||||
const VEC2_FIELDS: [BuiltinFieldMeta; 2] = [
|
||||
BuiltinFieldMeta {
|
||||
@@ -285,13 +322,13 @@ const PIXEL_FIELDS: [BuiltinFieldMeta; 3] = [
|
||||
BuiltinFieldMeta {
|
||||
name: "x",
|
||||
start_slot: 0,
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Int),
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Int64),
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinFieldMeta {
|
||||
name: "y",
|
||||
start_slot: 1,
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Int),
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Int64),
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinFieldMeta {
|
||||
@@ -302,7 +339,7 @@ const PIXEL_FIELDS: [BuiltinFieldMeta; 3] = [
|
||||
},
|
||||
];
|
||||
|
||||
const BUILTIN_TYPES: [BuiltinTypeMeta; 6] = [
|
||||
const BUILTIN_TYPES: [BuiltinTypeMeta; 9] = [
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0001,
|
||||
name: COLOR.name,
|
||||
@@ -330,35 +367,34 @@ const BUILTIN_TYPES: [BuiltinTypeMeta; 6] = [
|
||||
flat_slot_layout: &PIXEL_LAYOUT,
|
||||
flat_slot_width: 3,
|
||||
},
|
||||
scalar_meta(0x10, "Int32", &[AbiType::Scalar(BuiltinScalarType::Int32)]),
|
||||
scalar_meta(0x11, "Int64", &[AbiType::Scalar(BuiltinScalarType::Int64)]),
|
||||
scalar_meta(0x12, "Float", &[AbiType::Scalar(BuiltinScalarType::Float)]),
|
||||
scalar_meta(0x13, "Bool", &[AbiType::Scalar(BuiltinScalarType::Bool)]),
|
||||
scalar_meta(0x14, "String", &[AbiType::Scalar(BuiltinScalarType::String)]),
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0100,
|
||||
name: INPUT_PAD.name,
|
||||
version: INPUT_PAD.version,
|
||||
shape: BuiltinTypeShape::Scalar,
|
||||
id: 0x15,
|
||||
name: "Dynamic",
|
||||
version: 1,
|
||||
shape: BuiltinTypeShape::Dynamic,
|
||||
fields: &[],
|
||||
flat_slot_layout: &INPUT_PAD_LAYOUT,
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0101,
|
||||
name: INPUT_TOUCH.name,
|
||||
version: INPUT_TOUCH.version,
|
||||
shape: BuiltinTypeShape::Scalar,
|
||||
fields: &[],
|
||||
flat_slot_layout: &INPUT_TOUCH_LAYOUT,
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0102,
|
||||
name: INPUT_BUTTON.name,
|
||||
version: INPUT_BUTTON.version,
|
||||
shape: BuiltinTypeShape::Scalar,
|
||||
fields: &[],
|
||||
flat_slot_layout: &INPUT_BUTTON_LAYOUT,
|
||||
flat_slot_width: 1,
|
||||
flat_slot_layout: &[AbiType::DynamicTag, AbiType::DynamicPayload],
|
||||
flat_slot_width: 2,
|
||||
},
|
||||
];
|
||||
|
||||
const fn scalar_meta(id: u32, name: &'static str, layout: &'static [AbiType]) -> BuiltinTypeMeta {
|
||||
BuiltinTypeMeta {
|
||||
id,
|
||||
name,
|
||||
version: 1,
|
||||
shape: BuiltinTypeShape::Scalar,
|
||||
fields: &[],
|
||||
flat_slot_layout: layout,
|
||||
flat_slot_width: 1,
|
||||
}
|
||||
}
|
||||
|
||||
const VEC2_ZERO_LAYOUT: [AbiType; 2] =
|
||||
[AbiType::Scalar(BuiltinScalarType::Float), AbiType::Scalar(BuiltinScalarType::Float)];
|
||||
const VEC2_ZERO_SLOTS: [BuiltinConstSlotValue; 2] =
|
||||
@@ -434,27 +470,20 @@ const NO_ARGS: [AbiType; 0] = [];
|
||||
const COLOR_ARGS: [AbiType; 1] = [AbiType::Builtin(COLOR)];
|
||||
const COLOR_RET: [AbiType; 1] = [AbiType::Builtin(COLOR)];
|
||||
const COLOR_RGB_ARGS: [AbiType; 3] = [
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
];
|
||||
const COLOR_RGBA_ARGS: [AbiType; 4] = [
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
];
|
||||
const COLOR_HTML_RGBA_ARGS: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::Str)];
|
||||
const PAD_RET: [AbiType; 1] = [AbiType::Builtin(INPUT_PAD)];
|
||||
const TOUCH_RET: [AbiType; 1] = [AbiType::Builtin(INPUT_TOUCH)];
|
||||
const BUTTON_RET: [AbiType; 1] = [AbiType::Builtin(INPUT_BUTTON)];
|
||||
const PAD_ARGS: [AbiType; 1] = [AbiType::Builtin(INPUT_PAD)];
|
||||
const TOUCH_ARGS: [AbiType; 1] = [AbiType::Builtin(INPUT_TOUCH)];
|
||||
const BUTTON_ARGS: [AbiType; 1] = [AbiType::Builtin(INPUT_BUTTON)];
|
||||
const BOOL_RET: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::Bool)];
|
||||
const INT_RET: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::Int)];
|
||||
const COLOR_HTML_RGBA_ARGS: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::String)];
|
||||
const INT_RET: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::Int64)];
|
||||
|
||||
const INTRINSICS: [IntrinsicMeta; 42] = [
|
||||
const INTRINSICS: [IntrinsicMeta; 21] = [
|
||||
IntrinsicMeta {
|
||||
id: 0x1000,
|
||||
owner: "vec2",
|
||||
@@ -686,237 +715,6 @@ const INTRINSICS: [IntrinsicMeta; 42] = [
|
||||
may_allocate: false,
|
||||
implementation: color_transparent,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2000,
|
||||
owner: "input",
|
||||
name: "pad",
|
||||
version: 1,
|
||||
arg_layout: &NO_ARGS,
|
||||
ret_layout: &PAD_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2001,
|
||||
owner: "input",
|
||||
name: "touch",
|
||||
version: 1,
|
||||
arg_layout: &NO_ARGS,
|
||||
ret_layout: &TOUCH_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_touch,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2010,
|
||||
owner: "input.pad",
|
||||
name: "up",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_up,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2011,
|
||||
owner: "input.pad",
|
||||
name: "down",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_down,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2012,
|
||||
owner: "input.pad",
|
||||
name: "left",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_left,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2013,
|
||||
owner: "input.pad",
|
||||
name: "right",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_right,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2014,
|
||||
owner: "input.pad",
|
||||
name: "a",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_a,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2015,
|
||||
owner: "input.pad",
|
||||
name: "b",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_b,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2016,
|
||||
owner: "input.pad",
|
||||
name: "x",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_x,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2017,
|
||||
owner: "input.pad",
|
||||
name: "y",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_y,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2018,
|
||||
owner: "input.pad",
|
||||
name: "l",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_l,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2019,
|
||||
owner: "input.pad",
|
||||
name: "r",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_r,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x201A,
|
||||
owner: "input.pad",
|
||||
name: "start",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_start,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x201B,
|
||||
owner: "input.pad",
|
||||
name: "select",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_select,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2020,
|
||||
owner: "input.touch",
|
||||
name: "button",
|
||||
version: 1,
|
||||
arg_layout: &TOUCH_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_touch_button,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2021,
|
||||
owner: "input.touch",
|
||||
name: "x",
|
||||
version: 1,
|
||||
arg_layout: &TOUCH_ARGS,
|
||||
ret_layout: &INT_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_touch_x,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2022,
|
||||
owner: "input.touch",
|
||||
name: "y",
|
||||
version: 1,
|
||||
arg_layout: &TOUCH_ARGS,
|
||||
ret_layout: &INT_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_touch_y,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2030,
|
||||
owner: "input.button",
|
||||
name: "pressed",
|
||||
version: 1,
|
||||
arg_layout: &BUTTON_ARGS,
|
||||
ret_layout: &BOOL_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_button_pressed,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2031,
|
||||
owner: "input.button",
|
||||
name: "released",
|
||||
version: 1,
|
||||
arg_layout: &BUTTON_ARGS,
|
||||
ret_layout: &BOOL_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_button_released,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2032,
|
||||
owner: "input.button",
|
||||
name: "down",
|
||||
version: 1,
|
||||
arg_layout: &BUTTON_ARGS,
|
||||
ret_layout: &BOOL_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_button_down,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2033,
|
||||
owner: "input.button",
|
||||
name: "hold",
|
||||
version: 1,
|
||||
arg_layout: &BUTTON_ARGS,
|
||||
ret_layout: &INT_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_button_hold,
|
||||
},
|
||||
];
|
||||
|
||||
pub fn lookup_builtin_type(name: &str, version: u16) -> Option<&'static BuiltinTypeMeta> {
|
||||
@@ -944,11 +742,15 @@ pub fn materialize_builtin_constant(
|
||||
pub fn lookup_intrinsic(owner: &str, name: &str, version: u16) -> Option<&'static IntrinsicMeta> {
|
||||
INTRINSICS
|
||||
.iter()
|
||||
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
|
||||
.find(|meta| meta.owner == owner && meta.name == name && meta.version == version)
|
||||
}
|
||||
|
||||
pub fn lookup_intrinsic_by_id(id: u32) -> Option<&'static IntrinsicMeta> {
|
||||
INTRINSICS.iter().find(|meta| meta.id == id)
|
||||
INTRINSICS
|
||||
.iter()
|
||||
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
|
||||
.find(|meta| meta.id == id)
|
||||
}
|
||||
|
||||
fn flatten_layout_type(layout_type: BuiltinLayoutType, out: &mut Vec<AbiType>) {
|
||||
@@ -967,20 +769,26 @@ fn expect_float_arg(args: &[Value], index: usize) -> Result<f64, IntrinsicExecut
|
||||
let value = args
|
||||
.get(index)
|
||||
.ok_or(IntrinsicExecutionError::ArityMismatch { expected: index + 1, got: args.len() })?;
|
||||
value.as_float().ok_or(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Float),
|
||||
})
|
||||
match value {
|
||||
Value::Float(value) => Ok(*value),
|
||||
_ => Err(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Float),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_int_arg(args: &[Value], index: usize) -> Result<i64, IntrinsicExecutionError> {
|
||||
let value = args
|
||||
.get(index)
|
||||
.ok_or(IntrinsicExecutionError::ArityMismatch { expected: index + 1, got: args.len() })?;
|
||||
value.as_integer().ok_or(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int),
|
||||
})
|
||||
match value {
|
||||
Value::Int64(value) => Ok(*value),
|
||||
_ => Err(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_string_arg(args: &[Value], index: usize) -> Result<&str, IntrinsicExecutionError> {
|
||||
@@ -991,18 +799,24 @@ fn expect_string_arg(args: &[Value], index: usize) -> Result<&str, IntrinsicExec
|
||||
Value::String(value) => Ok(value.as_ref()),
|
||||
_ => Err(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Str),
|
||||
expected: AbiType::Scalar(BuiltinScalarType::String),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn value_matches_abi_type(value: &Value, expected: AbiType) -> bool {
|
||||
match expected {
|
||||
AbiType::Scalar(BuiltinScalarType::Int) => value.as_integer().is_some(),
|
||||
AbiType::Scalar(BuiltinScalarType::Float) => value.as_float().is_some(),
|
||||
AbiType::Scalar(BuiltinScalarType::Int32) => matches!(value, Value::Int32(_)),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64) => matches!(value, Value::Int64(_)),
|
||||
AbiType::Scalar(BuiltinScalarType::Float) => matches!(value, Value::Float(_)),
|
||||
AbiType::Scalar(BuiltinScalarType::Bool) => matches!(value, Value::Boolean(_)),
|
||||
AbiType::Scalar(BuiltinScalarType::Str) => matches!(value, Value::String(_)),
|
||||
AbiType::Builtin(_) => value.as_integer().is_some(),
|
||||
AbiType::Scalar(BuiltinScalarType::String) => {
|
||||
matches!(value, Value::String(_) | Value::Null)
|
||||
}
|
||||
AbiType::Builtin(key) if key == COLOR => {
|
||||
matches!(value, Value::Int64(raw) if u32::try_from(*raw).is_ok())
|
||||
}
|
||||
AbiType::Builtin(_) | AbiType::DynamicTag | AbiType::DynamicPayload => false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1023,11 +837,6 @@ fn validate_values_against_layout(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
const PAD_HANDLE: i64 = 1;
|
||||
const TOUCH_HANDLE: i64 = 1;
|
||||
const PAD_BUTTON_BASE: i64 = 0x100;
|
||||
const TOUCH_BUTTON_CARRIER: i64 = 0x200;
|
||||
|
||||
fn vec2_dot(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
@@ -1069,7 +878,7 @@ fn expect_channel(args: &[Value], index: usize) -> Result<u8, IntrinsicExecution
|
||||
let value = expect_int_arg(args, index)?;
|
||||
u8::try_from(value).map_err(|_| IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int),
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1088,7 +897,7 @@ fn color_from_raw(
|
||||
let raw = u32::try_from(expect_int_arg(args, 0)?).map_err(|_| {
|
||||
IntrinsicExecutionError::TypeMismatch {
|
||||
index: 0,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int),
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
}
|
||||
})?;
|
||||
Ok(color_value(Color::from_raw(raw)))
|
||||
@@ -1124,7 +933,7 @@ fn color_html_rgba(
|
||||
let color = parse_html_rgba(expect_string_arg(args, 0)?).ok_or({
|
||||
IntrinsicExecutionError::TypeMismatch {
|
||||
index: 0,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Str),
|
||||
expected: AbiType::Scalar(BuiltinScalarType::String),
|
||||
}
|
||||
})?;
|
||||
Ok(color_value(color))
|
||||
@@ -1227,254 +1036,6 @@ fn color_transparent(
|
||||
color_const(Color::TRANSPARENT)
|
||||
}
|
||||
|
||||
fn input_pad(
|
||||
_args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
Ok(vec![Value::Int64(PAD_HANDLE)])
|
||||
}
|
||||
|
||||
fn input_touch(
|
||||
_args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
Ok(vec![Value::Int64(TOUCH_HANDLE)])
|
||||
}
|
||||
|
||||
fn input_pad_up(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "up")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE)])
|
||||
}
|
||||
|
||||
fn input_pad_down(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "down")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 1)])
|
||||
}
|
||||
|
||||
fn input_pad_left(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "left")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 2)])
|
||||
}
|
||||
|
||||
fn input_pad_right(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "right")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 3)])
|
||||
}
|
||||
|
||||
fn input_pad_a(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "a")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 4)])
|
||||
}
|
||||
|
||||
fn input_pad_b(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "b")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 5)])
|
||||
}
|
||||
|
||||
fn input_pad_x(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "x")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 6)])
|
||||
}
|
||||
|
||||
fn input_pad_y(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "y")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 7)])
|
||||
}
|
||||
|
||||
fn input_pad_l(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "l")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 8)])
|
||||
}
|
||||
|
||||
fn input_pad_r(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "r")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 9)])
|
||||
}
|
||||
|
||||
fn input_pad_start(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "start")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 10)])
|
||||
}
|
||||
|
||||
fn input_pad_select(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "select")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 11)])
|
||||
}
|
||||
|
||||
fn input_touch_button(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_touch_handle(args, "button")?;
|
||||
Ok(vec![Value::Int64(TOUCH_BUTTON_CARRIER)])
|
||||
}
|
||||
|
||||
fn input_touch_x(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_touch_handle(args, "x")?;
|
||||
let platform =
|
||||
ctx.require_platform().map_err(|_| IntrinsicExecutionError::HardwareUnavailable)?;
|
||||
Ok(vec![Value::Int64(platform.input().touch().x() as i64)])
|
||||
}
|
||||
|
||||
fn input_touch_y(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_touch_handle(args, "y")?;
|
||||
let platform =
|
||||
ctx.require_platform().map_err(|_| IntrinsicExecutionError::HardwareUnavailable)?;
|
||||
Ok(vec![Value::Int64(platform.input().touch().y() as i64)])
|
||||
}
|
||||
|
||||
fn input_button_pressed(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
let button = resolve_button(args, "pressed", ctx)?;
|
||||
Ok(vec![Value::Boolean(button.pressed)])
|
||||
}
|
||||
|
||||
fn input_button_released(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
let button = resolve_button(args, "released", ctx)?;
|
||||
Ok(vec![Value::Boolean(button.released)])
|
||||
}
|
||||
|
||||
fn input_button_down(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
let button = resolve_button(args, "down", ctx)?;
|
||||
Ok(vec![Value::Boolean(button.down)])
|
||||
}
|
||||
|
||||
fn input_button_hold(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
let button = resolve_button(args, "hold", ctx)?;
|
||||
Ok(vec![Value::Int64(button.hold_frames as i64)])
|
||||
}
|
||||
|
||||
fn expect_pad_handle(args: &[Value], name: &'static str) -> Result<(), IntrinsicExecutionError> {
|
||||
let carrier = expect_builtin_carrier(args, 0)?;
|
||||
if carrier != PAD_HANDLE {
|
||||
return Err(IntrinsicExecutionError::InvalidBuiltinCarrier {
|
||||
owner: "input.pad",
|
||||
name,
|
||||
carrier,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expect_touch_handle(args: &[Value], name: &'static str) -> Result<(), IntrinsicExecutionError> {
|
||||
let carrier = expect_builtin_carrier(args, 0)?;
|
||||
if carrier != TOUCH_HANDLE {
|
||||
return Err(IntrinsicExecutionError::InvalidBuiltinCarrier {
|
||||
owner: "input.touch",
|
||||
name,
|
||||
carrier,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expect_builtin_carrier(args: &[Value], index: usize) -> Result<i64, IntrinsicExecutionError> {
|
||||
let value = args
|
||||
.get(index)
|
||||
.ok_or(IntrinsicExecutionError::ArityMismatch { expected: index + 1, got: args.len() })?;
|
||||
value.as_integer().ok_or(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int),
|
||||
})
|
||||
}
|
||||
|
||||
fn resolve_button(
|
||||
args: &[Value],
|
||||
name: &'static str,
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<prometeu_hal::button::Button, IntrinsicExecutionError> {
|
||||
let carrier = expect_builtin_carrier(args, 0)?;
|
||||
let platform =
|
||||
ctx.require_platform().map_err(|_| IntrinsicExecutionError::HardwareUnavailable)?;
|
||||
let input = platform.input();
|
||||
if carrier == TOUCH_BUTTON_CARRIER {
|
||||
return Ok(*input.touch().f());
|
||||
}
|
||||
|
||||
let Some(index) = carrier.checked_sub(PAD_BUTTON_BASE) else {
|
||||
return Err(IntrinsicExecutionError::InvalidBuiltinCarrier {
|
||||
owner: "input.button",
|
||||
name,
|
||||
carrier,
|
||||
});
|
||||
};
|
||||
let button = match index {
|
||||
0 => *input.pad().up(),
|
||||
1 => *input.pad().down(),
|
||||
2 => *input.pad().left(),
|
||||
3 => *input.pad().right(),
|
||||
4 => *input.pad().a(),
|
||||
5 => *input.pad().b(),
|
||||
6 => *input.pad().x(),
|
||||
7 => *input.pad().y(),
|
||||
8 => *input.pad().l(),
|
||||
9 => *input.pad().r(),
|
||||
10 => *input.pad().start(),
|
||||
11 => *input.pad().select(),
|
||||
_ => {
|
||||
return Err(IntrinsicExecutionError::InvalidBuiltinCarrier {
|
||||
owner: "input.button",
|
||||
name,
|
||||
carrier,
|
||||
});
|
||||
}
|
||||
};
|
||||
Ok(button)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -1501,8 +1062,8 @@ mod tests {
|
||||
assert_eq!(
|
||||
pixel.flatten_layout(),
|
||||
vec![
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Builtin(COLOR),
|
||||
]
|
||||
);
|
||||
@@ -1570,8 +1131,8 @@ mod tests {
|
||||
assert_eq!(lookup_intrinsic_by_id(length.id).map(|meta| meta.key()), Some(length.key()));
|
||||
|
||||
let hold =
|
||||
lookup_intrinsic("input.button", "hold", 1).expect("input.button.hold must exist");
|
||||
assert_eq!(hold.arg_layout.len(), 1);
|
||||
lookup_intrinsic("input.pad.up", "hold", 1).expect("input.pad.up.hold must exist");
|
||||
assert_eq!(hold.arg_layout.len(), 0);
|
||||
assert_eq!(hold.ret_layout.len(), 1);
|
||||
assert!(hold.deterministic);
|
||||
assert!(!hold.may_allocate);
|
||||
@@ -1621,7 +1182,7 @@ mod tests {
|
||||
fn input_intrinsic_requires_hardware_context() {
|
||||
let mut ctx = HostContext::new(None);
|
||||
let touch_x = lookup_intrinsic("input.touch", "x", 1).expect("input.touch.x must exist");
|
||||
let err = (touch_x.implementation)(&[Value::Int64(TOUCH_HANDLE)], &mut ctx)
|
||||
let err = (touch_x.implementation)(&[], &mut ctx)
|
||||
.expect_err("touch.x must fail without hardware context");
|
||||
assert_eq!(err, IntrinsicExecutionError::HardwareUnavailable);
|
||||
}
|
||||
@@ -1629,9 +1190,23 @@ mod tests {
|
||||
#[test]
|
||||
fn pixel_flat_values_validate_with_color_carrier_slot() {
|
||||
let pixel = lookup_builtin_type("pixel", 1).expect("pixel builtin must exist");
|
||||
let values = vec![Value::Int32(1), Value::Int32(2), Value::Int32(0xF800)];
|
||||
let values = vec![Value::Int64(1), Value::Int64(2), Value::Int64(0xF800)];
|
||||
pixel.validate_flat_values(&values).expect("pixel flat values must validate");
|
||||
assert_eq!(pixel.flat_slot_width, 3);
|
||||
assert_eq!(pixel.field("color").map(|field| field.start_slot), Some(2));
|
||||
}
|
||||
}
|
||||
|
||||
/// Loader-side lookup charges each catalogue record before examining it.
|
||||
pub(crate) fn metered_lookup_intrinsic(
|
||||
id: u32,
|
||||
budget: &mut prometeu_bytecode::load_limits::LoadBudget,
|
||||
) -> Result<Option<&'static IntrinsicMeta>, prometeu_bytecode::load_limits::LoadFailure> {
|
||||
for meta in INTRINSICS.iter().chain(crate::input_intrinsics::INPUT_INTRINSICS.iter()) {
|
||||
budget.work(1)?;
|
||||
if meta.id == id {
|
||||
return Ok(Some(meta));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user