include!(concat!(env!("OUT_DIR"), "/cli_bytecode_table.rs"));
pub(crate) fn find_cli_script_bytecode(name: &str) -> Option<&'static [u8]> {
STDLIB_CLI_SCRIPT_BYTECODE
.iter()
.find_map(|(entry_name, bytes)| (*entry_name == name).then_some(*bytes))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_cli_script_has_non_empty_bytecode_when_aot_enabled() {
if STDLIB_CLI_SCRIPT_BYTECODE.is_empty() {
return;
}
for script in harn_stdlib::STDLIB_CLI_SCRIPTS {
let bytes = find_cli_script_bytecode(script.name).unwrap_or_else(|| {
panic!(
"no AOT bytecode for CLI script `{}` despite AOT being enabled",
script.name
)
});
assert!(
!bytes.is_empty(),
"AOT bytecode for `{}` is empty",
script.name
);
}
}
#[test]
fn find_cli_script_bytecode_returns_none_for_unknown_name() {
assert!(find_cli_script_bytecode("definitely-not-a-real-script").is_none());
}
#[test]
fn every_emitted_artifact_starts_with_cache_magic() {
if STDLIB_CLI_SCRIPT_BYTECODE.is_empty() {
return;
}
for (name, bytes) in STDLIB_CLI_SCRIPT_BYTECODE {
assert!(
bytes.len() >= harn_vm::bytecode_cache::MAGIC.len(),
"AOT bytecode for `{name}` is shorter than the cache magic"
);
assert_eq!(
&bytes[..harn_vm::bytecode_cache::MAGIC.len()],
harn_vm::bytecode_cache::MAGIC,
"AOT bytecode for `{name}` is missing the HARNBC magic"
);
}
}
}