Lines
100 %
Functions
Branches
use super::*;
use crate::ast::Number;
use crate::ast::{ClosureSigId, EntityKind, Fraction, PairElement};
use crate::compiler::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::call::compile_closure_arg;
use crate::runtime::{Symbol, SymbolKind};
const TYPES: [WasmType; 10] = [
WasmType::I32,
WasmType::Bool,
WasmType::Ratio,
WasmType::Commodity,
WasmType::StringRef,
WasmType::SymbolRef,
WasmType::PairRef(PairElement::I32),
WasmType::EntityRef(EntityKind::Account),
WasmType::Closure(ClosureSigId(0)),
WasmType::AnyRef,
];
fn arguments() -> Vec<Expr> {
let mut args = vec![
Expr::Nil,
Expr::T,
Expr::Number(Number::Index(3)),
Expr::Number(Number::Scalar(Fraction::new(1, 2))),
Expr::String("s".to_string()),
Expr::List(vec![
Expr::Symbol("QUOTE".to_string()),
Expr::Symbol("A".to_string()),
]),
args.extend(TYPES.iter().map(|ty| Expr::WasmLocal(0, *ty)));
args
}
#[test]
fn call_fits_matches_the_closure_argument() {
for arg in arguments() {
for param in TYPES {
let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
let mut emit = FunctionEmitter::new();
let mut symbols = SymbolTable::with_builtins_for_wasm();
let accepted = compile_closure_arg(&mut ctx, &mut emit, &mut symbols, &arg, param);
let mut probe = SymbolTable::with_builtins_for_wasm();
let value = probe_value(&mut probe, &arg).expect("probe");
let fits = call_fits(&mut probe, &arg, &value, param);
assert_eq!(
fits.expect("probe"),
accepted.is_ok(),
"call_fits disagrees with compile_closure_arg for {arg:?} into {param:?}"
);
fn a_call_without_a_recorded_source_is_never_respecialised() {
let symbols = SymbolTable::with_builtins_for_wasm();
let found = respecialised(&symbols, Place::Local(9), &[], &[]);
assert_eq!(found.expect("no probe runs"), None);
fn lambda(params: &[&str], body: &str) -> (LambdaParams, Expr) {
let params = LambdaParams::simple(params.iter().map(|p| p.to_uppercase()).collect());
let body = crate::reader::Reader::parse_expr(body).expect("parses");
(params, body)
fn nil_into_a_never_nil_parameter_the_body_typed_keeps_the_call() {
for param in [WasmType::I32, WasmType::Ratio, WasmType::Commodity] {
assert!(!call_fits(&mut probe, &Expr::Nil, &Expr::Nil, param).expect("fits"));
assert!(keeps_closure_call(&mut probe, &Expr::Nil, param, || true).expect("probe"));
assert!(!keeps_closure_call(&mut probe, &Expr::Nil, param, || false).expect("probe"));
fn only_arithmetic_comparison_and_pins_are_evidence() {
let evidence = |params: &[&str], body: &str, index| {
let (params, body) = lambda(params, body);
has_type_evidence(&symbols, ¶ms, &body, index)
};
assert!(evidence(&["n"], "(+ n 1)", 0));
assert!(evidence(&["n"], "(if t (< 1 n) 2)", 0));
assert!(evidence(&["n"], "(cons 1 n)", 0));
assert!(!evidence(&["n"], "(if n 1 2)", 0));
assert!(!evidence(&["n"], "(let ((n 2)) (+ n 1))", 0));
assert!(!evidence(&["n"], "'(+ n 1)", 0));
assert!(!evidence(&["a", "b"], "(+ a 1)", 1));
fn closure_calling_h(local: bool) -> SymbolTable {
let (params, body) = lambda(&["z"], "(h z)");
symbols.record_closure_body(Place::Local(0), params, body);
let mut function = Symbol::new("H", SymbolKind::Function).with_function(Expr::Nil);
if local {
function = function.local_function();
symbols.define(function);
symbols
fn a_source_calling_a_global_defun_is_respecialised_for_a_scalar() {
let symbols = closure_calling_h(false);
let scalar = Expr::Number(Number::Scalar(Fraction::new(1, 2)));
let found = respecialised(&symbols, Place::Local(0), &[WasmType::I32], &[scalar]);
assert!(found.expect("probe").is_some());
fn a_source_calling_a_labels_function_is_never_respecialised() {
let symbols = closure_calling_h(true);
assert_eq!(found.expect("probe"), None);