fixed issues with Input API

This commit is contained in:
2026-03-24 13:40:30 +00:00
parent 0a087d51fb
commit 2592b1a1d1
17 changed files with 457 additions and 90 deletions
@@ -59,7 +59,7 @@ impl ContractRegistry {
});
mappings.insert("Gfx".to_string(), gfx);
// Input mappings
// Input legacy mappings (kept for backward compatibility)
let mut input = HashMap::new();
input.insert("pad".to_string(), ContractMethod {
id: 0x2010,
@@ -73,37 +73,90 @@ impl ContractRegistry {
});
mappings.insert("Input".to_string(), input);
// Touch mappings
// New Pad service-based mappings (each button as a method returning Button)
let mut pad = HashMap::new();
// NOTE: The syscalls for per-button access must be handled by the runtime.
// We reserve the 0x2200..0x220B range for Pad buttons returning a Button struct (6 bytes).
pad.insert("up".to_string(), ContractMethod {
id: 0x2200,
params: vec![],
return_type: PbsType::Struct("Button".to_string()),
});
pad.insert("down".to_string(), ContractMethod {
id: 0x2201,
params: vec![],
return_type: PbsType::Struct("Button".to_string()),
});
pad.insert("left".to_string(), ContractMethod {
id: 0x2202,
params: vec![],
return_type: PbsType::Struct("Button".to_string()),
});
pad.insert("right".to_string(), ContractMethod {
id: 0x2203,
params: vec![],
return_type: PbsType::Struct("Button".to_string()),
});
pad.insert("a".to_string(), ContractMethod {
id: 0x2204,
params: vec![],
return_type: PbsType::Struct("Button".to_string()),
});
pad.insert("b".to_string(), ContractMethod {
id: 0x2205,
params: vec![],
return_type: PbsType::Struct("Button".to_string()),
});
pad.insert("x".to_string(), ContractMethod {
id: 0x2206,
params: vec![],
return_type: PbsType::Struct("Button".to_string()),
});
pad.insert("y".to_string(), ContractMethod {
id: 0x2207,
params: vec![],
return_type: PbsType::Struct("Button".to_string()),
});
pad.insert("l".to_string(), ContractMethod {
id: 0x2208,
params: vec![],
return_type: PbsType::Struct("Button".to_string()),
});
pad.insert("r".to_string(), ContractMethod {
id: 0x2209,
params: vec![],
return_type: PbsType::Struct("Button".to_string()),
});
pad.insert("start".to_string(), ContractMethod {
id: 0x220A,
params: vec![],
return_type: PbsType::Struct("Button".to_string()),
});
pad.insert("select".to_string(), ContractMethod {
id: 0x220B,
params: vec![],
return_type: PbsType::Struct("Button".to_string()),
});
mappings.insert("Pad".to_string(), pad);
// Touch mappings (service-based)
let mut touch = HashMap::new();
touch.insert("getX".to_string(), ContractMethod {
// SDK agora expõe screen_x/screen_y/finger()
touch.insert("screen_x".to_string(), ContractMethod {
id: 0x2101,
params: vec![],
return_type: PbsType::Int,
});
touch.insert("getY".to_string(), ContractMethod {
touch.insert("screen_y".to_string(), ContractMethod {
id: 0x2102,
params: vec![],
return_type: PbsType::Int,
});
touch.insert("isDown".to_string(), ContractMethod {
id: 0x2103,
// Novo syscall dedicado para retornar Button completo do touch (dedo)
touch.insert("finger".to_string(), ContractMethod {
id: 0x2107,
params: vec![],
return_type: PbsType::Bool,
});
touch.insert("isPressed".to_string(), ContractMethod {
id: 0x2104,
params: vec![],
return_type: PbsType::Bool,
});
touch.insert("isReleased".to_string(), ContractMethod {
id: 0x2105,
params: vec![],
return_type: PbsType::Bool,
});
touch.insert("getHold".to_string(), ContractMethod {
id: 0x2106,
params: vec![],
return_type: PbsType::Int,
return_type: PbsType::Struct("Button".to_string()),
});
mappings.insert("Touch".to_string(), touch);
@@ -57,6 +57,8 @@ impl<'a> Lowerer<'a> {
let mut struct_slots = HashMap::new();
struct_slots.insert("Color".to_string(), 1);
struct_slots.insert("ButtonState".to_string(), 4);
// New service-based input returns `Button` (same layout as legacy ButtonState: 4 slots)
struct_slots.insert("Button".to_string(), 4);
struct_slots.insert("Pad".to_string(), 48);
struct_slots.insert("Touch".to_string(), 6);
@@ -564,7 +566,46 @@ impl<'a> Lowerer<'a> {
fn lower_block(&mut self, _node: NodeId, n: &BlockNodeArena) -> Result<(), ()> {
self.local_vars.push(HashMap::new());
for stmt in &n.stmts {
// Lower the statement normally
self.lower_node(*stmt)?;
// Guardrail: if the statement is a standalone Call that returns values,
// discard them to keep the stack balanced. This is essential for host
// contract calls that return multi-slot structs (e.g., Button=4 slots).
if let NodeKind::Call(call) = self.arena.kind(*stmt) {
// Try to compute return slots conservatively
let mut return_slots: u32 = 0;
match self.arena.kind(call.callee) {
NodeKind::MemberAccess(ma) => {
// Static contract call: Contract.method(...)
if let NodeKind::Ident(obj_id) = self.arena.kind(ma.object) {
let contract_name = self.interner.resolve(obj_id.name);
let method_name = self.interner.resolve(ma.member);
if let Some(method) = self.contract_registry.get_method(contract_name, method_name) {
let ty = self.convert_pbs_type(&method.return_type);
return_slots = self.get_type_slots(&ty);
}
}
}
NodeKind::Ident(id_node) => {
// Calling a local function or imported symbol — attempt best effort via symbol table
let callee_name = self.interner.resolve(id_node.name).to_string();
if let Some(func_id) = self.function_ids.get(&callee_name) {
let _ = func_id; // unresolved return info in v0 → assume 0 (no discard)
} else {
// ImportCall or unknown — assume 0 (safe)
}
}
_ => {}
}
if return_slots > 0 {
for _ in 0..return_slots {
self.emit(InstrKind::Pop);
}
}
}
}
if let Some(tail) = n.tail {
self.lower_node(tail)?;
@@ -585,10 +626,16 @@ impl<'a> Lowerer<'a> {
if let NodeKind::Ident(obj) = self.arena.kind(ma.object) {
let obj_name = self.interner.resolve(obj.name);
let member_name = self.interner.resolve(ma.member);
match (obj_name, member_name) {
("Input", "pad") => Type::Struct("Pad".to_string()),
("Input", "touch") => Type::Struct("Touch".to_string()),
_ => Type::Int,
// 1) Prefer exact contract registry mapping (Pad/Touch services, etc.)
if let Some(method) = self.contract_registry.get_method(obj_name, member_name) {
self.convert_pbs_type(&method.return_type)
} else {
// 2) Legacy snapshot helpers
match (obj_name, member_name) {
("Input", "pad") => Type::Struct("Pad".to_string()),
("Input", "touch") => Type::Struct("Touch".to_string()),
_ => Type::Int,
}
}
} else {
Type::Int
@@ -747,6 +794,39 @@ impl<'a> Lowerer<'a> {
return Ok(());
}
// Fallback: Handle member access where the object is a Call to a host contract method
// Example: Pad.a().down — object is a Call(MemberAccess(Pad, a))
if let NodeKind::Call(call_node) = self.arena.kind(n.object) {
if let NodeKind::MemberAccess(inner_ma) = self.arena.kind(call_node.callee) {
if let NodeKind::Ident(obj_id) = self.arena.kind(inner_ma.object) {
let contract_name = self.interner.resolve(obj_id.name);
let method_name = self.interner.resolve(inner_ma.member);
if let Some(method) = self.contract_registry.get_method(contract_name, method_name) {
// Determine return type and slots
let ret_ty = self.convert_pbs_type(&method.return_type);
let slots = self.get_type_slots(&ret_ty);
if let Type::Struct(struct_name) = &ret_ty {
// Lower the call to push all slots
self.lower_call(n.object, &call_node)?;
// Store into a temp local to avoid leaving extra slots on the operand stack
let tmp_slot = self.add_local_to_scope(
format!("$tmp_{}", self.get_next_local_slot()),
ret_ty.clone(),
);
for i in (0..slots).rev() {
self.emit(InstrKind::SetLocal(tmp_slot + i));
}
// Load only the requested field
let field_name = self.interner.resolve(n.member);
let field_off = self.get_field_offset(struct_name, field_name);
self.emit(InstrKind::GetLocal(tmp_slot + field_off));
return Ok(());
}
}
}
}
}
Ok(())
}
@@ -789,6 +869,14 @@ impl<'a> Lowerer<'a> {
}
}
match struct_name {
// New `Button` mirrors legacy `ButtonState` offsets
"Button" => match field_name {
"pressed" => 0,
"released" => 1,
"down" => 2,
"hold_frames" => 3,
_ => 0,
},
"ButtonState" => match field_name {
"pressed" => 0,
"released" => 1,
@@ -828,13 +916,20 @@ impl<'a> Lowerer<'a> {
}
}
match struct_name {
"Pad" => Type::Struct("ButtonState".to_string()),
// Pad's per-button service returns a `Button` in the new API
"Pad" => Type::Struct("Button".to_string()),
// Field types for `Button` mirror legacy `ButtonState`
"Button" => match field_name {
"hold_frames" => Type::Bounded,
_ => Type::Bool,
},
"ButtonState" => match field_name {
"hold_frames" => Type::Bounded,
_ => Type::Bool,
},
"Touch" => match field_name {
"f" => Type::Struct("ButtonState".to_string()),
// Touch.f() now returns a `Button`
"f" => Type::Struct("Button".to_string()),
_ => Type::Int,
},
_ => Type::Int,
@@ -943,6 +1038,46 @@ impl<'a> Lowerer<'a> {
}
}
NodeKind::MemberAccess(ma) => {
// Special-case: Member access over a call expression that returns a struct from a host contract,
// e.g., Pad.a().down — we must:
// 1) lower the call (pushing all struct slots),
// 2) store it into a temporary local (all slots),
// 3) load only the requested field slot back to the stack.
if let NodeKind::Call(call_node) = self.arena.kind(ma.object) {
// Try to resolve if callee is a host contract method to infer return struct and slots
if let NodeKind::MemberAccess(inner_ma) = self.arena.kind(call_node.callee) {
if let NodeKind::Ident(obj_id) = self.arena.kind(inner_ma.object) {
let contract_name = self.interner.resolve(obj_id.name);
let method_name = self.interner.resolve(inner_ma.member);
if let Some(method) = self.contract_registry.get_method(contract_name, method_name) {
// Determine slots from the declared return type BEFORE lowering the call
let ret_ty = self.convert_pbs_type(&method.return_type);
let slots = self.get_type_slots(&ret_ty);
let struct_name = match &ret_ty { Type::Struct(s) => s.clone(), _ => String::new() };
// Lower the call first (this will push all return slots from HostCall)
self.lower_call(n.callee, &call_node)?;
// Allocate a temp local to capture the struct
let tmp_slot = self.add_local_to_scope(
format!("$tmp_{}", self.get_next_local_slot()),
ret_ty.clone(),
);
// Store all slots (top of stack has the last slot). We must store in reverse order.
for i in (0..slots).rev() {
self.emit(InstrKind::SetLocal(tmp_slot + i));
}
// Compute field offset/type and load only that field
let field_name = self.interner.resolve(ma.member);
let field_off = self.get_field_offset(&struct_name, field_name);
let _field_ty = self.get_field_type(&struct_name, field_name);
self.emit(InstrKind::GetLocal(tmp_slot + field_off));
return Ok(());
}
}
}
}
// Check if it's a constructor alias: TypeName.Alias(...)
let ctor = if let NodeKind::Ident(obj_id) = self.arena.kind(ma.object) {
let obj_name = self.interner.resolve(obj_id.name);
@@ -995,10 +1130,17 @@ impl<'a> Lowerer<'a> {
}
if let Some(method) = self.contract_registry.get_method(obj_name, member_name) {
let id = method.id;
let return_slots = if matches!(method.return_type, PbsType::Void) {
0
} else {
1
// Compute return slots from declared return type. Structs may span >1 slot (e.g., Button=4)
let return_slots = match &method.return_type {
PbsType::Void | PbsType::None => 0,
PbsType::Struct(name) => {
// Prefer builtin struct slots, then fallback to struct_slots map, default 1
if let Some(bi) = self.get_builtin_struct_slots(name) { bi } else { *self.struct_slots.get(name).unwrap_or(&1) }
}
other => {
let ty = self.convert_pbs_type(other);
self.get_type_slots(&ty)
}
};
self.emit(InstrKind::HostCall(id, return_slots));
return Ok(());
@@ -362,7 +362,8 @@ impl<'a> TypeChecker<'a> {
_ => {}
}
}
"ButtonState" => {
// New `Button` mirrors legacy `ButtonState` for field typing
"Button" | "ButtonState" => {
match member_str {
"pressed" | "released" | "down" => return PbsType::Bool,
"hold_frames" => return PbsType::Bounded,
@@ -372,7 +373,7 @@ impl<'a> TypeChecker<'a> {
"Pad" => {
match member_str {
"up" | "down" | "left" | "right" | "a" | "b" | "x" | "y" | "l" | "r" | "start" | "select" => {
return PbsType::Struct("ButtonState".to_string());
return PbsType::Struct("Button".to_string());
}
"any" => {
return PbsType::Function {
@@ -385,7 +386,7 @@ impl<'a> TypeChecker<'a> {
}
"Touch" => {
match member_str {
"f" => return PbsType::Struct("ButtonState".to_string()),
"f" => return PbsType::Struct("Button".to_string()),
"x" | "y" => return PbsType::Int,
_ => {}
}