Lines
100 %
Functions
Branches
use super::*;
use crate::ast::{ClosureSigId, Number};
use crate::compiler::expr::classify_stack_type;
use crate::compiler::{CompileMode, Compiler};
use crate::reader::Reader;
use super::super::fold::probe_value;
#[test]
fn an_argument_of_the_parameter_type_passes_as_it_is() {
assert_eq!(fit(WasmType::Ratio, WasmType::Ratio), Some(Fit::Pass));
assert_eq!(fit(WasmType::I32, WasmType::I32), Some(Fit::Pass));
}
fn an_argument_boxes_into_an_any_parameter() {
assert_eq!(
fit(WasmType::I32, WasmType::AnyRef),
Some(Fit::Box(WasmType::I32))
);
fn an_index_argument_fits_neither_a_scalar_nor_a_money_parameter() {
assert_eq!(fit(WasmType::I32, WasmType::Ratio), None);
assert_eq!(fit(WasmType::I32, WasmType::Commodity), None);
fn an_any_argument_narrows_to_the_types_the_is_narrowing_throws_for() {
fit(WasmType::AnyRef, WasmType::I32),
Some(Fit::Narrow(Target::Index))
fit(WasmType::AnyRef, WasmType::StringRef),
Some(Fit::Narrow(Target::String))
fit(WasmType::AnyRef, WasmType::PairRef(PairElement::AnyRef)),
Some(Fit::List)
fn nothing_else_converts() {
let typed_list = WasmType::PairRef(PairElement::I32);
assert_eq!(fit(WasmType::AnyRef, typed_list), None);
fit(typed_list, WasmType::PairRef(PairElement::AnyRef)),
None
fit(
WasmType::Closure(ClosureSigId(0)),
WasmType::Closure(ClosureSigId(1))
),
fn a_signature_that_cannot_take_the_shape_has_no_arm() {
let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
let sig = ctx
.intern_closure_signature(&[WasmType::I32], WasmType::I32)
.expect("sig");
let entry = ctx.closure_sig(sig).expect("entry").clone();
assert_eq!(arm_fits(&[WasmType::I32], &entry), Some(vec![Fit::Pass]));
assert_eq!(arm_fits(&[WasmType::Ratio], &entry), None);
fn local_any(ctx: &mut CompileContext) -> Place {
Place::Local(ctx.alloc_local(WasmType::AnyRef).expect("local"))
fn a_call_pushes_each_argument_at_its_own_type_and_returns_any() {
let callee = local_any(&mut ctx);
let mut emit = FunctionEmitter::new();
let mut symbols = SymbolTable::with_builtins_for_wasm();
let args = [
Reader::parse_expr("2").expect("parses"),
Reader::parse_expr("1/2").expect("parses"),
Reader::parse_expr("\"s\"").expect("parses"),
];
let ty = compile_any_call(&mut ctx, &mut emit, &mut symbols, callee, &args).expect("compiles");
assert_eq!(ty, WasmType::AnyRef);
let helpers = ctx.take_any_call_helpers();
assert_eq!(helpers.len(), 1);
helpers[0].shape,
[WasmType::I32, WasmType::Ratio, WasmType::StringRef]
fn an_integer_beyond_the_index_range_is_no_argument() {
let big = Expr::Number(Number::Index(i64::from(i32::MAX) + 1));
assert!(compile_any_call(&mut ctx, &mut emit, &mut symbols, callee, &[big]).is_err());
fn compiled(source: &str, mode: CompileMode) -> Vec<u8> {
let program = Reader::parse(source).expect("parses");
Compiler::new()
.compile_with_mode(&program, &mut symbols, mode)
.expect("compiles")
fn both_drivers_fill_every_reserved_helper() {
let source = "(funcall (car (list (lambda () 1) (lambda (x) x) nil)))";
for mode in [CompileMode::Script, CompileMode::Eval] {
assert!(!compiled(source, mode).is_empty());
fn a_unit_without_any_call_reserves_no_helper() {
fill_any_call_helpers(&mut ctx).expect("fills");
assert!(ctx.take_any_call_helpers().is_empty());
/// The mirror: eval, the stack classifier and codegen agree that a call of an
/// `any` value is `any`, whatever the candidates return.
fn an_any_call_is_any_on_every_surface() {
let forms = [
"(funcall (car (list (lambda () 1) nil)))",
"(let ((g (car (list (lambda () 1) nil)))) (funcall g))",
"(funcall (if (< (entity-count) 1) (lambda () 1) (lambda (x) x)))",
for source in forms {
let form = Reader::parse_expr(source).expect("parses");
let evaluated = probe_value(&mut symbols.clone(), &form).expect("probes");
let pushed = compile_for_stack(&mut ctx, &mut emit, &mut symbols, &form).expect("compiles");
classify_stack_type(&evaluated),
Some(WasmType::AnyRef),
"{source}"
assert_eq!(pushed, WasmType::AnyRef, "{source}");
fn signatures_of_one_wasm_shape_test_by_their_own_id() {
let boolean = ctx
.intern_closure_signature(&[], WasmType::Bool)
let index = ctx
.intern_closure_signature(&[], WasmType::I32)
let sigs = ctx.closure_sigs_of_arity(0);
assert_eq!(sigs.len(), 2);
assert_ne!(sigs[0].id, sigs[1].id);
assert_eq!((sigs[0].id, sigs[1].id), (boolean, index));
emit_sig_test(&ctx, &mut emit, &sigs[1]).expect("emits");
emit.finish(&[]).expect("a balanced test");