#![cfg(any(
all(target_os = "macos", target_arch = "aarch64"),
all(target_os = "linux", target_arch = "x86_64", target_env = "gnu")
))]
use std::fs;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
use wings_abi::{RecordSchema, ValueType};
use wings_artifact::{BundleManifest, Manifest, direct_handler_symbol, library_name};
use wings_codegen::{build, build_app};
use wings_ir::{
Comparison, Function, Handler, Instruction, Operation, Operator, Procedure, Program,
};
static NEXT_DIRECTORY: AtomicU64 = AtomicU64::new(0);
struct Directory(PathBuf);
impl Directory {
fn new() -> Self {
let path = std::env::temp_dir().join(format!(
"wings-codegen-programs-test-{}-{}",
std::process::id(),
NEXT_DIRECTORY.fetch_add(1, Ordering::Relaxed)
));
fs::create_dir(&path).unwrap();
Self(path)
}
fn artifact(&self) -> PathBuf {
self.0.join("artifact")
}
}
impl Drop for Directory {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.0);
}
}
fn instruction(ty: ValueType, operation: Operation) -> Instruction {
Instruction { ty, operation }
}
fn int(operation: Operation) -> Instruction {
instruction(ValueType::Int, operation)
}
fn unit_function() -> Function {
Function {
instructions: vec![
int(Operation::Constant(0)),
instruction(ValueType::Unit, Operation::Constant(0)),
],
state: 0,
reply: 1,
}
}
fn program() -> Program {
Program {
records: Vec::new(),
external_actors: Vec::new(),
name: "Counter".into(),
state: ValueType::Int,
functions: Vec::new(),
initializer: unit_function(),
handlers: Vec::new(),
}
}
fn handler(
name: &str,
parameters: Vec<(&str, ValueType)>,
reply: Option<ValueType>,
function: Function,
) -> Handler {
Handler {
name: name.into(),
parameters: parameters
.into_iter()
.map(|(name, ty)| (name.into(), ty))
.collect(),
reply,
source_parameters: None,
completion: None,
function,
}
}
fn library_contains(directory: &Directory, symbol: &str) -> bool {
let bytes = fs::read(directory.artifact().join(library_name())).unwrap();
bytes
.windows(symbol.len())
.any(|window| window == symbol.as_bytes())
}
fn build_and_read(program: &Program, directory: &Directory) -> Manifest {
build(program, &directory.artifact()).unwrap();
Manifest::read(&directory.artifact()).unwrap()
}
#[test]
fn builds_loop_countdown_handler_without_direct_entry() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Countdown",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Constant(10)),
int(Operation::Loop(vec![1, 0])),
int(Operation::PhiValue(1)),
int(Operation::Constant(1)),
int(Operation::Binary(Operator::Subtract, 2, 4)),
int(Operation::Constant(0)),
instruction(
ValueType::Bool,
Operation::Compare(Comparison::Greater, 2, 6),
),
instruction(ValueType::Unit, Operation::While(7, vec![2, 3])),
int(Operation::EndLoop(vec![5, 3])),
int(Operation::PhiValue(1)),
],
state: 0,
reply: 9,
},
));
program.validate().unwrap();
let manifest = build_and_read(&program, &directory);
assert_eq!(manifest.handlers.len(), 1);
assert!(!library_contains(&directory, &direct_handler_symbol(0)));
}
#[test]
fn builds_nested_loop_handler() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Nested",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Constant(3)),
int(Operation::Loop(vec![1, 0])),
int(Operation::PhiValue(1)),
int(Operation::Constant(2)),
int(Operation::Loop(vec![4, 3])),
int(Operation::PhiValue(1)),
int(Operation::Constant(1)),
int(Operation::Binary(Operator::Add, 6, 7)),
int(Operation::Constant(0)),
instruction(
ValueType::Bool,
Operation::Compare(Comparison::Greater, 5, 9),
),
instruction(ValueType::Unit, Operation::While(10, vec![5, 6])),
int(Operation::Constant(1)),
int(Operation::Binary(Operator::Subtract, 5, 12)),
int(Operation::EndLoop(vec![13, 8])),
int(Operation::PhiValue(1)),
int(Operation::Constant(0)),
instruction(
ValueType::Bool,
Operation::Compare(Comparison::Greater, 2, 16),
),
instruction(ValueType::Unit, Operation::While(17, vec![2, 3])),
int(Operation::Constant(1)),
int(Operation::Binary(Operator::Subtract, 2, 19)),
int(Operation::EndLoop(vec![20, 3])),
int(Operation::PhiValue(1)),
],
state: 0,
reply: 21,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
#[test]
fn builds_multi_value_branch_merge_with_phi_results() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Branch",
vec![("delta", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Constant(0)),
instruction(
ValueType::Bool,
Operation::Compare(Comparison::Greater, 1, 2),
),
instruction(ValueType::Unit, Operation::If(3)),
int(Operation::Binary(Operator::Add, 0, 1)),
int(Operation::Constant(100)),
instruction(ValueType::Unit, Operation::Else(vec![5, 6])),
int(Operation::Binary(Operator::Subtract, 0, 1)),
int(Operation::Constant(200)),
int(Operation::EndIf(vec![8, 9])),
int(Operation::PhiValue(1)),
int(Operation::Binary(Operator::Add, 10, 11)),
],
state: 0,
reply: 12,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
assert!(library_contains(&directory, &direct_handler_symbol(0)));
}
#[test]
fn builds_nested_if_inside_if() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"NestedIf",
vec![("value", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Constant(0)),
instruction(
ValueType::Bool,
Operation::Compare(Comparison::Greater, 1, 2),
),
instruction(ValueType::Unit, Operation::If(3)),
int(Operation::Constant(10)),
instruction(
ValueType::Bool,
Operation::Compare(Comparison::Greater, 1, 5),
),
instruction(ValueType::Unit, Operation::If(6)),
int(Operation::Constant(1)),
instruction(ValueType::Unit, Operation::Else(vec![8])),
int(Operation::Constant(2)),
int(Operation::EndIf(vec![10])),
instruction(ValueType::Unit, Operation::Else(vec![11])),
int(Operation::Constant(3)),
int(Operation::EndIf(vec![13])),
],
state: 0,
reply: 14,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
assert!(library_contains(&directory, &direct_handler_symbol(0)));
}
#[test]
fn builds_string_literal_and_length_handler_without_direct_entry() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Greet",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
instruction(
ValueType::StringRef,
Operation::StringLiteral("hello".into()),
),
int(Operation::StringLen(1)),
int(Operation::Binary(Operator::Add, 0, 2)),
],
state: 3,
reply: 3,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
assert!(!library_contains(&directory, &direct_handler_symbol(0)));
}
#[test]
fn builds_bytes_literal_and_byte_manipulation_handler() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Bytes",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Constant(16)),
int(Operation::Constant(65)),
instruction(ValueType::BytesRef, Operation::FillBytes(1, 2)),
int(Operation::Constant(0)),
int(Operation::ByteAt(3, 4)),
int(Operation::BytesLen(3)),
int(Operation::Binary(Operator::Add, 5, 6)),
instruction(ValueType::BytesRef, Operation::ReplaceByte([3, 4, 2])),
int(Operation::Constant(0)),
int(Operation::ReadI64Be(8, 9)),
instruction(ValueType::BytesRef, Operation::ReplaceI64Be([8, 9, 7])),
int(Operation::Binary(Operator::Add, 7, 10)),
instruction(ValueType::BytesRef, Operation::BytesLiteral(vec![1, 2, 3])),
int(Operation::BytesLen(13)),
int(Operation::Binary(Operator::Add, 12, 14)),
],
state: 15,
reply: 15,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
assert!(!library_contains(&directory, &direct_handler_symbol(0)));
}
#[test]
fn builds_string_conversion_and_concatenation_handler() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Strings",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
instruction(ValueType::StringRef, Operation::IntToString(0)),
instruction(ValueType::Bool, Operation::Constant(1)),
instruction(ValueType::StringRef, Operation::BoolToString(2)),
instruction(ValueType::StringRef, Operation::ConcatStrings(1, 3)),
int(Operation::StringLen(4)),
instruction(ValueType::BytesRef, Operation::StringBytes(4)),
int(Operation::BytesLen(6)),
instruction(ValueType::StringRef, Operation::StringFromBytes(6)),
int(Operation::Constant(0)),
int(Operation::StringAt(8, 9)),
int(Operation::Binary(Operator::Add, 5, 7)),
],
state: 11,
reply: 11,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
#[test]
fn builds_string_slice_handler() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Slice",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
instruction(
ValueType::StringRef,
Operation::StringLiteral("abcdef".into()),
),
int(Operation::Constant(1)),
int(Operation::Constant(3)),
instruction(ValueType::StringRef, Operation::StringSlice([1, 2, 3])),
int(Operation::StringLen(4)),
],
state: 5,
reply: 5,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
#[test]
fn builds_list_operations_handler() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Lists",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
instruction(ValueType::ListRef, Operation::EmptyInts),
int(Operation::Constant(7)),
instruction(ValueType::ListRef, Operation::PrependInt(2, 1)),
int(Operation::Constant(8)),
instruction(ValueType::ListRef, Operation::PrependInt(4, 3)),
int(Operation::HeadInt(5)),
instruction(ValueType::ListRef, Operation::TailInts(5)),
instruction(ValueType::Bool, Operation::IsEmptyInts(7)),
int(Operation::HeadInt(7)),
int(Operation::Binary(Operator::Add, 6, 9)),
],
state: 10,
reply: 10,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
fn pair_record() -> RecordSchema {
RecordSchema {
name: "Pair".into(),
fields: vec![
("first".into(), ValueType::Int),
("second".into(), ValueType::Int),
],
}
}
#[test]
fn builds_record_roundtrip_handler_and_preserves_schemas_in_manifest() {
let directory = Directory::new();
let mut program = program();
program.records = vec![pair_record()];
program.handlers.push(handler(
"Records",
vec![("delta", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
instruction(
ValueType::RecordRef(0),
Operation::RecordNew {
schema: 0,
arguments: vec![0, 1],
},
),
int(Operation::RecordField { value: 2, field: 0 }),
int(Operation::RecordField { value: 2, field: 1 }),
int(Operation::Binary(Operator::Add, 3, 4)),
],
state: 5,
reply: 5,
},
));
program.validate().unwrap();
let manifest = build_and_read(&program, &directory);
assert_eq!(manifest.records.len(), 1);
assert_eq!(manifest.records[0].name, "Pair");
assert_eq!(manifest.records[0].fields.len(), 2);
}
#[test]
fn builds_handler_replying_with_escaping_record() {
let directory = Directory::new();
let mut program = program();
program.records = vec![pair_record()];
program.handlers.push(handler(
"MakePair",
vec![("delta", ValueType::Int)],
Some(ValueType::RecordRef(0)),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
instruction(
ValueType::RecordRef(0),
Operation::RecordNew {
schema: 0,
arguments: vec![0, 1],
},
),
int(Operation::Constant(0)),
],
state: 3,
reply: 2,
},
));
program.validate().unwrap();
let manifest = build_and_read(&program, &directory);
assert_eq!(manifest.handlers[0].reply, Some(ValueType::RecordRef(0)));
}
fn leaf_adder() -> Procedure {
Procedure {
name: "add".into(),
parameters: vec![("a".into(), ValueType::Int), ("b".into(), ValueType::Int)],
result: ValueType::Int,
function: Function {
instructions: vec![
instruction(ValueType::Unit, Operation::Constant(0)),
int(Operation::Argument(0)),
int(Operation::Argument(1)),
int(Operation::Binary(Operator::Add, 1, 2)),
],
state: 0,
reply: 3,
},
}
}
#[test]
fn builds_handler_calling_leaf_managed_procedure() {
let directory = Directory::new();
let mut program = program();
program.functions = vec![leaf_adder()];
program.handlers.push(handler(
"Call",
vec![("delta", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Call {
function: 0,
arguments: vec![0, 1],
}),
],
state: 2,
reply: 2,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
#[test]
fn builds_self_recursive_managed_procedure() {
let directory = Directory::new();
let mut program = program();
program.functions = vec![Procedure {
name: "fact".into(),
parameters: vec![("n".into(), ValueType::Int)],
result: ValueType::Int,
function: Function {
instructions: vec![
instruction(ValueType::Unit, Operation::Constant(0)),
int(Operation::Argument(0)),
int(Operation::Constant(0)),
instruction(
ValueType::Bool,
Operation::Compare(Comparison::LessEqual, 1, 2),
),
instruction(ValueType::Unit, Operation::If(3)),
int(Operation::Constant(1)),
instruction(ValueType::Unit, Operation::Else(vec![5])),
int(Operation::Constant(1)),
int(Operation::Binary(Operator::Subtract, 1, 7)),
int(Operation::Call {
function: 0,
arguments: vec![8],
}),
int(Operation::Binary(Operator::Multiply, 1, 9)),
int(Operation::EndIf(vec![10])),
],
state: 0,
reply: 11,
},
}];
program.handlers.push(handler(
"Fact",
vec![("n", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Call {
function: 0,
arguments: vec![1],
}),
],
state: 2,
reply: 2,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
fn even_odd_procedures() -> Vec<Procedure> {
let make = |name: &str, base: i64, callee: usize| Procedure {
name: name.into(),
parameters: vec![("n".into(), ValueType::Int)],
result: ValueType::Int,
function: Function {
instructions: vec![
instruction(ValueType::Unit, Operation::Constant(0)),
int(Operation::Argument(0)),
int(Operation::Constant(0)),
instruction(ValueType::Bool, Operation::Compare(Comparison::Equal, 1, 2)),
instruction(ValueType::Unit, Operation::If(3)),
int(Operation::Constant(base)),
instruction(ValueType::Unit, Operation::Else(vec![5])),
int(Operation::Constant(1)),
int(Operation::Binary(Operator::Subtract, 1, 7)),
int(Operation::Call {
function: callee,
arguments: vec![8],
}),
int(Operation::EndIf(vec![9])),
],
state: 0,
reply: 10,
},
};
vec![make("even", 1, 1), make("odd", 0, 0)]
}
#[test]
fn builds_mutually_recursive_managed_procedures() {
let directory = Directory::new();
let mut program = program();
program.functions = even_odd_procedures();
program.handlers.push(handler(
"IsEven",
vec![("n", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Call {
function: 0,
arguments: vec![1],
}),
],
state: 2,
reply: 2,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
#[test]
fn builds_procedure_with_loop_and_heap_operations() {
let directory = Directory::new();
let mut program = program();
program.functions = vec![Procedure {
name: "count_down".into(),
parameters: vec![("n".into(), ValueType::Int)],
result: ValueType::Int,
function: Function {
instructions: vec![
instruction(ValueType::Unit, Operation::Constant(0)),
int(Operation::Argument(0)),
int(Operation::Loop(vec![1])),
int(Operation::Constant(1)),
int(Operation::Binary(Operator::Subtract, 2, 3)),
int(Operation::Constant(0)),
instruction(
ValueType::Bool,
Operation::Compare(Comparison::Greater, 2, 5),
),
instruction(ValueType::Unit, Operation::While(6, vec![2])),
int(Operation::EndLoop(vec![4])),
],
state: 0,
reply: 8,
},
}];
program.handlers.push(handler(
"Run",
vec![("n", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Call {
function: 0,
arguments: vec![1],
}),
],
state: 2,
reply: 2,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
#[test]
fn builds_self_send_and_monitor_effect_handlers() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Ping",
vec![("value", ValueType::Int)],
None,
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Binary(Operator::Add, 0, 1)),
instruction(ValueType::Unit, Operation::Constant(0)),
],
state: 2,
reply: 3,
},
));
program.handlers.push(handler(
"Trigger",
vec![("delta", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
instruction(
ValueType::Unit,
Operation::SendSelf {
handler: 0,
arguments: vec![1],
},
),
int(Operation::Binary(Operator::Add, 0, 1)),
],
state: 3,
reply: 3,
},
));
program.handlers.push(handler(
"Watch",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
instruction(ValueType::Pid, Operation::SelfPid),
instruction(
ValueType::Unit,
Operation::Monitor {
target: 1,
handler: 3,
},
),
int(Operation::Constant(0)),
],
state: 3,
reply: 3,
},
));
program.handlers.push(handler(
"Down",
vec![("pid", ValueType::Pid), ("reason", ValueType::Int)],
None,
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
instruction(ValueType::Unit, Operation::Constant(0)),
],
state: 0,
reply: 1,
},
));
program.validate().unwrap();
let manifest = build_and_read(&program, &directory);
assert_eq!(manifest.handlers.len(), 4);
assert!(!library_contains(&directory, &direct_handler_symbol(1)));
}
#[test]
fn builds_send_after_self_effect_handler() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Ping",
vec![("value", ValueType::Int)],
None,
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Binary(Operator::Add, 0, 1)),
instruction(ValueType::Unit, Operation::Constant(0)),
],
state: 2,
reply: 3,
},
));
program.handlers.push(handler(
"Schedule",
vec![("delta", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Constant(50)),
instruction(
ValueType::Unit,
Operation::SendAfterSelf {
delay: 2,
handler: 0,
arguments: vec![1],
},
),
],
state: 0,
reply: 1,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
#[test]
fn builds_hostcall_effect_handler() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Invoke",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Constant(1000)),
instruction(
ValueType::StringRef,
Operation::StringLiteral("echo".into()),
),
instruction(ValueType::BytesRef, Operation::BytesLiteral(vec![1, 2, 3])),
instruction(
ValueType::BytesRef,
Operation::Hostcall {
timeout: 1,
arguments: vec![2, 3],
},
),
int(Operation::BytesLen(4)),
],
state: 5,
reply: 5,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
assert!(!library_contains(&directory, &direct_handler_symbol(0)));
}
#[test]
fn builds_pid_capability_operations_handler() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Pids",
vec![],
Some(ValueType::Bool),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
instruction(ValueType::Pid, Operation::SelfPid),
instruction(ValueType::Pid, Operation::NullPid),
instruction(
ValueType::Bool,
Operation::Compare(Comparison::NotEqual, 1, 2),
),
],
state: 0,
reply: 3,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
#[test]
fn builds_spawn_self_and_collect_handler() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Spawn",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
instruction(ValueType::Pid, Operation::SpawnSelf),
instruction(ValueType::Unit, Operation::Collect),
int(Operation::Constant(0)),
],
state: 0,
reply: 3,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
#[test]
fn builds_remainder_and_all_comparison_operators() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Ops",
vec![("a", ValueType::Int), ("b", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Argument(1)),
int(Operation::Binary(Operator::Remainder, 1, 2)),
instruction(ValueType::Bool, Operation::Compare(Comparison::Equal, 1, 2)),
instruction(
ValueType::Bool,
Operation::Compare(Comparison::NotEqual, 1, 2),
),
instruction(ValueType::Bool, Operation::Compare(Comparison::Less, 1, 2)),
instruction(
ValueType::Bool,
Operation::Compare(Comparison::LessEqual, 1, 2),
),
instruction(
ValueType::Bool,
Operation::Compare(Comparison::GreaterEqual, 1, 2),
),
int(Operation::Binary(Operator::Add, 3, 1)),
int(Operation::Binary(Operator::Multiply, 9, 2)),
],
state: 10,
reply: 10,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
assert!(library_contains(&directory, &direct_handler_symbol(0)));
}
#[test]
fn builds_division_by_zero_constants_without_folding_them_away() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Fault",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Constant(10)),
int(Operation::Constant(0)),
int(Operation::Binary(Operator::Divide, 1, 2)),
],
state: 3,
reply: 3,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
assert!(library_contains(&directory, &direct_handler_symbol(0)));
}
#[test]
fn builds_overflow_boundary_constant_arithmetic() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Boundary",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Constant(i64::MAX)),
int(Operation::Constant(i64::MIN)),
int(Operation::Constant(-1)),
int(Operation::Binary(Operator::Subtract, 1, 3)),
int(Operation::Negate(2)),
int(Operation::Binary(Operator::Add, 4, 5)),
],
state: 6,
reply: 6,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
#[test]
fn builds_minimal_state_passthrough_handler() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Echo",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![instruction(ValueType::Int, Operation::State)],
state: 0,
reply: 0,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
#[test]
fn builds_one_way_handler_and_manifest_records_absent_reply() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Sink",
vec![("value", ValueType::Int)],
None,
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Binary(Operator::Add, 0, 1)),
instruction(ValueType::Unit, Operation::Constant(0)),
],
state: 2,
reply: 3,
},
));
program.validate().unwrap();
let manifest = build_and_read(&program, &directory);
assert_eq!(manifest.handlers[0].reply, None);
}
#[test]
fn builds_list_state_program() {
let directory = Directory::new();
let mut program = program();
program.state = ValueType::ListRef;
program.initializer = Function {
instructions: vec![
instruction(ValueType::ListRef, Operation::EmptyInts),
instruction(ValueType::Unit, Operation::Constant(0)),
],
state: 0,
reply: 1,
};
program.handlers.push(handler(
"Push",
vec![("value", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::ListRef, Operation::State),
int(Operation::Argument(0)),
instruction(ValueType::ListRef, Operation::PrependInt(1, 0)),
int(Operation::HeadInt(2)),
],
state: 2,
reply: 3,
},
));
program.validate().unwrap();
let manifest = build_and_read(&program, &directory);
assert_eq!(manifest.state, ValueType::ListRef);
}
#[test]
fn builds_bytes_and_string_state_programs() {
for (state, initial) in [
(ValueType::BytesRef, Operation::BytesLiteral(vec![0u8; 4])),
(
ValueType::StringRef,
Operation::StringLiteral("init".into()),
),
] {
let directory = Directory::new();
let mut program = program();
program.state = state;
program.initializer = Function {
instructions: vec![
instruction(state, initial),
instruction(ValueType::Unit, Operation::Constant(0)),
],
state: 0,
reply: 1,
};
program.handlers.push(handler(
"Touch",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(state, Operation::State),
int(match state {
ValueType::BytesRef => Operation::BytesLen(0),
_ => Operation::StringLen(0),
}),
int(Operation::Constant(0)),
],
state: 0,
reply: 1,
},
));
program.validate().unwrap();
let manifest = build_and_read(&program, &directory);
assert_eq!(manifest.state, state);
}
}
#[test]
fn builds_many_parameter_handler_at_arity_boundary() {
let directory = Directory::new();
let mut program = program();
let mut instructions = vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Constant(0)),
];
for index in 0..wings_ir::MAX_PARAMETERS {
instructions.push(int(Operation::Argument(index)));
let sum = instructions.len();
let left = instructions.len() - 2;
instructions.push(int(Operation::Binary(Operator::Add, left, sum - 1)));
}
let last = instructions.len() - 1;
program.handlers.push(handler(
"Wide",
(0..wings_ir::MAX_PARAMETERS)
.map(|index| {
(
Box::leak(format!("p{index}").into_boxed_str()) as &'static str,
ValueType::Int,
)
})
.collect(),
Some(ValueType::Int),
Function {
instructions,
state: 0,
reply: last,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
#[test]
fn rejects_handlers_exceeding_operation_limit() {
let directory = Directory::new();
let mut program = program();
let mut instructions = vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Constant(1)),
];
while instructions.len() < wings_ir::MAX_OPERATIONS + 1 {
let last = instructions.len() - 1;
instructions.push(int(Operation::Binary(Operator::Add, last, 1)));
}
let last = instructions.len() - 1;
program.handlers.push(handler(
"Huge",
vec![],
Some(ValueType::Int),
Function {
instructions,
state: 0,
reply: last,
},
));
let error = build(&program, &directory.artifact()).unwrap_err();
assert!(error.contains("too many operations"), "{error}");
assert!(!directory.artifact().exists());
}
#[test]
fn rejects_effects_inside_managed_procedures() {
let directory = Directory::new();
let mut program = program();
let mut effectful = leaf_adder();
effectful.function.instructions = vec![
instruction(ValueType::Unit, Operation::Constant(0)),
instruction(ValueType::Pid, Operation::SelfPid),
int(Operation::Constant(0)),
];
effectful.function.reply = 2;
program.functions = vec![effectful];
let error = build(&program, &directory.artifact()).unwrap_err();
assert!(error.contains("effects are not supported"), "{error}");
assert!(!directory.artifact().exists());
}
#[test]
fn rejects_effects_inside_initializer() {
let directory = Directory::new();
let mut program = program();
program.initializer = Function {
instructions: vec![
instruction(ValueType::Int, Operation::Constant(0)),
instruction(ValueType::Pid, Operation::SelfPid),
instruction(ValueType::Unit, Operation::Constant(0)),
],
state: 0,
reply: 2,
};
let error = build(&program, &directory.artifact()).unwrap_err();
assert!(
error.contains("self-send is not supported in initializers"),
"{error}"
);
assert!(!directory.artifact().exists());
}
#[test]
fn rejects_string_literal_exceeding_per_literal_limit() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Big",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
instruction(
ValueType::StringRef,
Operation::StringLiteral("x".repeat(65537)),
),
int(Operation::StringLen(1)),
],
state: 2,
reply: 2,
},
));
let error = build(&program, &directory.artifact()).unwrap_err();
assert!(error.contains("invalid scalar IR"), "{error}");
assert!(!directory.artifact().exists());
}
#[test]
fn builds_string_literal_at_exactly_the_size_limit() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Exact",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
instruction(
ValueType::StringRef,
Operation::StringLiteral("x".repeat(65536)),
),
int(Operation::StringLen(1)),
],
state: 2,
reply: 2,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
#[test]
fn rejects_unterminated_conditional_and_missing_phi() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Broken",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
instruction(ValueType::Bool, Operation::Constant(1)),
instruction(ValueType::Unit, Operation::If(1)),
int(Operation::Constant(1)),
],
state: 0,
reply: 3,
},
));
let error = build(&program, &directory.artifact()).unwrap_err();
assert!(
error.contains("unterminated conditional") || error.contains("branch scope"),
"{error}"
);
assert!(!directory.artifact().exists());
}
#[test]
fn rejects_loop_in_initializer() {
let directory = Directory::new();
let mut program = program();
program.initializer = Function {
instructions: vec![
int(Operation::Constant(1)),
int(Operation::Loop(vec![0])),
instruction(ValueType::Bool, Operation::Constant(0)),
instruction(ValueType::Unit, Operation::While(2, vec![1])),
int(Operation::EndLoop(vec![1])),
instruction(ValueType::Unit, Operation::Constant(0)),
],
state: 0,
reply: 5,
};
let error = build(&program, &directory.artifact()).unwrap_err();
assert!(error.contains("invalid scalar IR"), "{error}");
assert!(!directory.artifact().exists());
}
#[test]
fn rejects_record_schema_mismatch_and_out_of_range_field() {
let directory = Directory::new();
let mut program = program();
program.records = vec![pair_record()];
program.handlers.push(handler(
"BadField",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Constant(0)),
instruction(
ValueType::RecordRef(0),
Operation::RecordNew {
schema: 0,
arguments: vec![0, 1],
},
),
int(Operation::RecordField { value: 2, field: 2 }),
],
state: 0,
reply: 3,
},
));
let error = build(&program, &directory.artifact()).unwrap_err();
assert!(
error.contains("invalid record field index or result type"),
"{error}"
);
assert!(!directory.artifact().exists());
}
#[test]
fn build_app_writes_single_dylib_bundle_with_embedded_actor_manifests() {
let directory = Directory::new();
let bundle = directory.0.join("bundle");
let mut main_program = program();
main_program.name = "Main".into();
let mut helper = program();
helper.name = "Helper".into();
build_app(&[main_program, helper], &bundle).unwrap();
let manifest = BundleManifest::read(&bundle).unwrap();
assert_eq!(manifest.entry, "Main");
let actor_names: Vec<&str> = manifest.actors.iter().map(|a| a.process.as_str()).collect();
assert_eq!(actor_names, vec!["Main", "Helper"]);
assert!(bundle.join(library_name()).is_file());
assert!(bundle.join("bundle.json").is_file());
assert!(!bundle.join("actors").exists());
}
#[test]
fn build_app_uses_last_actor_as_entry_without_main() {
let directory = Directory::new();
let bundle = directory.0.join("bundle");
let mut alpha = program();
alpha.name = "Alpha".into();
let mut omega = program();
omega.name = "Omega".into();
build_app(&[alpha, omega], &bundle).unwrap();
let manifest = BundleManifest::read(&bundle).unwrap();
assert_eq!(manifest.entry, "Omega");
}
#[test]
fn build_app_rejects_empty_and_oversized_bundles_without_output() {
let directory = Directory::new();
let bundle = directory.0.join("empty");
let error = build_app(&[], &bundle).unwrap_err();
assert!(error.contains("1..=32 actors"), "{error}");
assert!(!bundle.exists());
let many: Vec<Program> = (0..33)
.map(|index| {
let mut actor = program();
actor.name = format!("Actor{index}");
actor
})
.collect();
let bundle = directory.0.join("many");
let error = build_app(&many, &bundle).unwrap_err();
assert!(error.contains("1..=32 actors"), "{error}");
assert!(!bundle.exists());
}
#[test]
fn build_app_rejects_duplicate_actor_names_before_writing() {
let directory = Directory::new();
let bundle = directory.0.join("dupes");
let first = program();
let second = program();
let error = build_app(&[first, second], &bundle).unwrap_err();
assert!(error.contains("duplicate actor name"), "{error}");
assert!(!bundle.exists());
}
#[test]
fn build_app_cleans_up_bundle_directory_when_an_actor_fails() {
let directory = Directory::new();
let bundle = directory.0.join("partial");
let mut broken = program();
broken.name = "Broken".into();
broken.handlers.push(handler(
"Bad",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Bool, Operation::Constant(1)),
instruction(ValueType::Unit, Operation::If(0)),
int(Operation::Constant(1)),
int(Operation::EndIf(vec![2])),
instruction(ValueType::Unit, Operation::Constant(0)),
],
state: 2,
reply: 4,
},
));
let mut healthy = program();
healthy.name = "Main".into();
let error = build_app(&[broken, healthy], &bundle).unwrap_err();
assert!(!error.is_empty());
assert!(!bundle.exists());
}
#[test]
fn build_app_at_maximum_actor_count() {
let directory = Directory::new();
let bundle = directory.0.join("full");
let actors: Vec<Program> = (0..wings_artifact::MAX_BUNDLE_ACTORS)
.map(|index| {
let mut actor = program();
actor.name = format!("Actor{index}");
actor
})
.collect();
build_app(&actors, &bundle).unwrap();
let manifest = BundleManifest::read(&bundle).unwrap();
assert_eq!(manifest.actors.len(), 32);
}
#[test]
fn build_app_refuses_existing_bundle_directory() {
let directory = Directory::new();
let bundle = directory.0.join("occupied");
fs::create_dir(&bundle).unwrap();
fs::write(bundle.join("bundle.json"), b"original").unwrap();
let error = build_app(&[program()], &bundle).unwrap_err();
assert!(error.contains("create new bundle directory"), "{error}");
assert_eq!(fs::read(bundle.join("bundle.json")).unwrap(), b"original");
}
#[test]
fn direct_entry_absent_for_heap_handler_but_present_for_scalar_handler() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Scalar",
vec![("delta", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Binary(Operator::Add, 0, 1)),
],
state: 2,
reply: 2,
},
));
program.handlers.push(handler(
"Heap",
vec![],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
instruction(ValueType::ListRef, Operation::EmptyInts),
int(Operation::Constant(1)),
instruction(ValueType::ListRef, Operation::PrependInt(2, 1)),
int(Operation::HeadInt(3)),
],
state: 4,
reply: 4,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
assert!(library_contains(&directory, &direct_handler_symbol(0)));
assert!(!library_contains(&directory, &direct_handler_symbol(1)));
}
#[test]
fn direct_entry_absent_for_record_replying_handler() {
let directory = Directory::new();
let mut program = program();
program.records = vec![pair_record()];
program.handlers.push(handler(
"Make",
vec![("delta", ValueType::Int)],
Some(ValueType::RecordRef(0)),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
instruction(
ValueType::RecordRef(0),
Operation::RecordNew {
schema: 0,
arguments: vec![0, 1],
},
),
int(Operation::Constant(0)),
],
state: 3,
reply: 2,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
assert!(!library_contains(&directory, &direct_handler_symbol(0)));
}
#[test]
fn builds_long_straight_line_scalar_chain_crossing_superblock_boundaries() {
let directory = Directory::new();
let mut program = program();
let mut instructions = vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Constant(1)),
];
for _ in 0..80 {
let last = instructions.len() - 1;
instructions.push(int(Operation::Binary(Operator::Add, last, 1)));
}
let last = instructions.len() - 1;
program.handlers.push(handler(
"Chain",
vec![],
Some(ValueType::Int),
Function {
instructions,
state: 0,
reply: last,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
assert!(library_contains(&directory, &direct_handler_symbol(0)));
}
#[test]
fn builds_handler_mixing_heap_ops_with_scalar_superblocks() {
let directory = Directory::new();
let mut program = program();
program.handlers.push(handler(
"Mixed",
vec![("delta", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Binary(Operator::Add, 0, 1)),
int(Operation::Binary(Operator::Multiply, 2, 1)),
instruction(ValueType::ListRef, Operation::EmptyInts),
instruction(ValueType::ListRef, Operation::PrependInt(3, 4)),
int(Operation::HeadInt(5)),
int(Operation::Binary(Operator::Add, 6, 0)),
instruction(ValueType::Bool, Operation::IsEmptyInts(5)),
instruction(ValueType::Unit, Operation::If(8)),
int(Operation::Binary(Operator::Add, 7, 1)),
instruction(ValueType::Unit, Operation::Else(vec![10])),
int(Operation::Binary(Operator::Subtract, 7, 1)),
int(Operation::EndIf(vec![12])),
],
state: 13,
reply: 13,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
}
#[test]
fn dead_heap_branch_pruning_unlocks_direct_scalar_entry_and_tail_self_recursion_compiles() {
let directory = Directory::new();
let mut program = program();
program.functions = vec![
Procedure {
name: "sum_acc".into(),
parameters: vec![("n".into(), ValueType::Int), ("acc".into(), ValueType::Int)],
result: ValueType::Int,
function: Function {
instructions: vec![
instruction(ValueType::Unit, Operation::Constant(0)),
int(Operation::Argument(0)),
int(Operation::Argument(1)),
int(Operation::Constant(0)),
instruction(
ValueType::Bool,
Operation::Compare(Comparison::LessEqual, 1, 3),
),
instruction(ValueType::Unit, Operation::If(4)),
instruction(ValueType::Unit, Operation::Else(vec![2, 1])),
int(Operation::Constant(1)),
int(Operation::Binary(Operator::Subtract, 1, 7)),
int(Operation::Binary(Operator::Add, 2, 1)),
int(Operation::Call {
function: 0,
arguments: vec![8, 9],
}),
int(Operation::EndIf(vec![10, 8])),
int(Operation::PhiValue(1)),
],
state: 0,
reply: 11,
},
},
Procedure {
name: "spin_both_arms".into(),
parameters: vec![("n".into(), ValueType::Int)],
result: ValueType::Int,
function: Function {
instructions: vec![
instruction(ValueType::Unit, Operation::Constant(0)),
int(Operation::Argument(0)),
int(Operation::Constant(0)),
instruction(ValueType::Bool, Operation::Compare(Comparison::Equal, 1, 2)),
instruction(ValueType::Unit, Operation::If(3)),
int(Operation::Call {
function: 1,
arguments: vec![1],
}),
instruction(ValueType::Unit, Operation::Else(vec![5])),
int(Operation::Call {
function: 1,
arguments: vec![2],
}),
int(Operation::EndIf(vec![7])),
],
state: 0,
reply: 8,
},
},
];
program.handlers.push(handler(
"PrunedDirect",
vec![("delta", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Constant(0)),
instruction(
ValueType::Bool,
Operation::Compare(Comparison::NotEqual, 2, 2),
),
instruction(ValueType::Unit, Operation::If(3)),
instruction(ValueType::ListRef, Operation::EmptyInts),
instruction(ValueType::Unit, Operation::Collect),
int(Operation::Constant(0)),
instruction(ValueType::Unit, Operation::Else(vec![7])),
int(Operation::Binary(Operator::Add, 0, 1)),
int(Operation::EndIf(vec![9])),
],
state: 10,
reply: 10,
},
));
program.handlers.push(handler(
"TailSum",
vec![("n", ValueType::Int)],
Some(ValueType::Int),
Function {
instructions: vec![
instruction(ValueType::Int, Operation::State),
int(Operation::Argument(0)),
int(Operation::Call {
function: 0,
arguments: vec![1, 0],
}),
],
state: 2,
reply: 2,
},
));
program.validate().unwrap();
build_and_read(&program, &directory);
assert!(
library_contains(&directory, &direct_handler_symbol(0)),
"pruning dead heap branch must unlock DirectScalarEntry"
);
}