Lines
100 %
Functions
Branches
use super::dynamic::{Saved, dynamic_home, reject_missed_specials, restore_forms};
use crate::ast::{Expr, Number, WasmType};
use crate::runtime::{Symbol, SymbolKind, SymbolTable};
use std::collections::HashSet;
fn table_with_global() -> SymbolTable {
let mut symbols = SymbolTable::with_builtins_for_wasm();
symbols.define_special(
Symbol::new("X", SymbolKind::Variable).with_value(Expr::WasmGlobal(4, WasmType::Ratio)),
);
symbols
}
fn home_of(symbols: &SymbolTable, name: &str) -> Option<(u32, WasmType)> {
dynamic_home(symbols, name).map(|home| (home.global, home.ty))
#[test]
fn a_global_valued_special_has_a_dynamic_home() {
let symbols = table_with_global();
assert_eq!(home_of(&symbols, "X"), Some((4, WasmType::Ratio)));
assert_eq!(home_of(&symbols, "Y"), None);
fn a_global_that_is_not_special_yet_binds_lexically() {
symbols.define(
assert_eq!(home_of(&symbols, "X"), None);
symbols.mark_special("X");
fn a_macro_body_binds_lexically() {
let mut symbols = table_with_global();
symbols.mark_macro_expansion();
fn a_binder_of_a_unit_special_the_scan_did_not_see_is_refused() {
symbols.set_unit_specials(HashSet::from(["X".to_string()]), HashSet::new());
let refused = reject_missed_specials(&symbols, ["X"]).expect_err("scan miss");
assert!(
refused
.to_string()
.contains("a macro expansion rebinds the special X")
assert!(reject_missed_specials(&symbols, ["Y"]).is_ok());
symbols.set_unit_specials(
HashSet::from(["X".to_string()]),
assert!(reject_missed_specials(&symbols, ["X"]).is_ok());
fn restores_run_last_bound_first() {
let saved = [
Saved {
global: 1,
ty: WasmType::I32,
local: 10,
flag: None,
depth_local: 13,
depth: 20,
},
global: 2,
ty: WasmType::Ratio,
local: 11,
flag: Some((3, 12)),
depth_local: 14,
depth: 30,
];
let forms = restore_forms(&saved);
assert_eq!(
forms,
vec![
Expr::List(vec![
Expr::symbol("SETF"),
Expr::WasmGlobal(2, WasmType::Ratio),
Expr::WasmLocal(11, WasmType::Ratio),
]),
Expr::WasmGlobal(3, WasmType::I32),
Expr::WasmLocal(12, WasmType::I32),
Expr::WasmGlobal(30, WasmType::I32),
Expr::WasmLocal(14, WasmType::I32),
Expr::WasmGlobal(1, WasmType::I32),
Expr::WasmLocal(10, WasmType::I32),
Expr::WasmGlobal(20, WasmType::I32),
Expr::WasmLocal(13, WasmType::I32),
]
fn table_with_constant() -> SymbolTable {
Symbol::new("C", SymbolKind::Variable).with_value(Expr::Number(Number::Index(7.into()))),
fn a_special_of_an_earlier_unit_holds_a_constant_to_bind() {
let symbols = table_with_constant();
symbols.constant_global("C"),
Some(Expr::Number(Number::Index(7.into())))
assert_eq!(table_with_global().constant_global("X"), None);
assert_eq!(symbols.constant_global("Y"), None);
fn a_macro_body_binds_a_constant_global_lexically() {
let mut symbols = table_with_constant();
assert_eq!(symbols.constant_global("C"), None);
fn a_created_global_has_no_dynamic_home_until_declared_special() {
symbols.mark_created_global("X");