pub struct Module { /* private fields */ }Expand description
A complete LSB module: the unit that is encoded, decoded, verified, loaded, and disassembled.
A module holds a string table, a type table, a constant pool, imports,
globals, functions, exports, dynamic-operation hooks, an optional name and
start function, and per-function debug information. It is built with
ModuleBuilder or produced by
decode, and is read-only afterwards, which is what
lets encode be infallible. Display prints the
disassemble listing.
§Examples
use bytecode_lang::{Inst, IntOp, IntTy, ModuleBuilder, ValType};
let mut m = ModuleBuilder::new();
let mut f = m.function("add", &[ValType::I64, ValType::I64], &[ValType::I64]);
let (a, b) = (f.param(0), f.param(1));
let sum = f.reg(ValType::I64);
f.emit(Inst::IAdd { op: IntOp::new(IntTy::I64), dst: sum, lhs: a, rhs: b });
f.ret(sum);
m.add_function(f).unwrap();
let module = m.finish().unwrap();
assert_eq!(module.functions().len(), 1);
let bytes = bytecode_lang::encode(&module);
assert_eq!(bytecode_lang::decode(&bytes).unwrap(), module);Implementations§
Source§impl Module
impl Module
Sourcepub fn string(&self, id: StrId) -> Option<&str>
pub fn string(&self, id: StrId) -> Option<&str>
The string with this id, or None if it is out of range.
§Examples
use bytecode_lang::{ModuleBuilder, StrId};
let mut m = ModuleBuilder::new();
let hi = m.string("hi");
let module = m.finish().unwrap();
assert_eq!(module.string(hi), Some("hi"));
assert_eq!(module.string(StrId(99)), None);Sourcepub fn string_count(&self) -> usize
pub fn string_count(&self) -> usize
The number of strings in the string table.
§Examples
use bytecode_lang::ModuleBuilder;
let mut m = ModuleBuilder::new();
m.string("a");
m.string("a"); // deduplicated
assert_eq!(m.finish().unwrap().string_count(), 1);Sourcepub fn strings(&self) -> impl Iterator<Item = &str> + '_
pub fn strings(&self) -> impl Iterator<Item = &str> + '_
Every string, in id order.
§Examples
use bytecode_lang::ModuleBuilder;
let mut m = ModuleBuilder::new();
m.string("a");
m.string("b");
let module = m.finish().unwrap();
assert_eq!(module.strings().collect::<Vec<_>>(), ["a", "b"]);Sourcepub fn types(&self) -> &[TypeDef]
pub fn types(&self) -> &[TypeDef]
The type table.
§Examples
use bytecode_lang::{ModuleBuilder, TypeDef, ValType};
let mut m = ModuleBuilder::new();
m.add_type(TypeDef::Cell(ValType::Dyn));
assert_eq!(m.finish().unwrap().types(), &[TypeDef::Cell(ValType::Dyn)]);Sourcepub fn type_def(&self, id: TypeId) -> Option<&TypeDef>
pub fn type_def(&self, id: TypeId) -> Option<&TypeDef>
The type with this id, or None if it is out of range.
§Examples
use bytecode_lang::{ModuleBuilder, TypeDef, ValType};
let mut m = ModuleBuilder::new();
let t = m.add_type(TypeDef::Array(ValType::U8));
assert_eq!(m.finish().unwrap().type_def(t), Some(&TypeDef::Array(ValType::U8)));Sourcepub fn consts(&self) -> &[Const]
pub fn consts(&self) -> &[Const]
The constant pool.
§Examples
use bytecode_lang::{Const, ModuleBuilder};
let mut m = ModuleBuilder::new();
m.constant(Const::Int(7));
assert_eq!(m.finish().unwrap().consts(), &[Const::Int(7)]);Sourcepub fn constant(&self, id: ConstId) -> Option<&Const>
pub fn constant(&self, id: ConstId) -> Option<&Const>
The constant with this id, or None if it is out of range.
§Examples
use bytecode_lang::{Const, ModuleBuilder};
let mut m = ModuleBuilder::new();
let k = m.constant(Const::Bool(true));
assert_eq!(m.finish().unwrap().constant(k), Some(&Const::Bool(true)));Sourcepub fn imports(&self) -> &[Import]
pub fn imports(&self) -> &[Import]
The import table.
§Examples
use bytecode_lang::{ModuleBuilder, ValType};
let mut m = ModuleBuilder::new();
let sig = m.func_type(&[ValType::Str], &[]);
m.import("ls.io", "print", sig);
assert_eq!(m.finish().unwrap().imports().len(), 1);Sourcepub fn import(&self, id: ImportId) -> Option<&Import>
pub fn import(&self, id: ImportId) -> Option<&Import>
The import with this id, or None if it is out of range.
§Examples
use bytecode_lang::{ModuleBuilder, ValType};
let mut m = ModuleBuilder::new();
let sig = m.func_type(&[], &[]);
let id = m.import("env", "tick", sig);
let module = m.finish().unwrap();
assert_eq!(module.import(id).map(|i| i.sig), Some(sig));Sourcepub fn globals(&self) -> &[Global]
pub fn globals(&self) -> &[Global]
The global table.
§Examples
use bytecode_lang::{ModuleBuilder, ValType};
let mut m = ModuleBuilder::new();
m.global("count", ValType::I64, true, None);
assert_eq!(m.finish().unwrap().globals().len(), 1);Sourcepub fn global(&self, id: GlobalId) -> Option<&Global>
pub fn global(&self, id: GlobalId) -> Option<&Global>
The global with this id, or None if it is out of range.
§Examples
use bytecode_lang::{ModuleBuilder, ValType};
let mut m = ModuleBuilder::new();
let g = m.global("debug", ValType::Bool, false, None);
assert_eq!(m.finish().unwrap().global(g).map(|g| g.ty), Some(ValType::Bool));Sourcepub fn functions(&self) -> &[Function]
pub fn functions(&self) -> &[Function]
The function table.
§Examples
use bytecode_lang::ModuleBuilder;
assert!(ModuleBuilder::new().finish().unwrap().functions().is_empty());Sourcepub fn function(&self, id: FuncId) -> Option<&Function>
pub fn function(&self, id: FuncId) -> Option<&Function>
The function with this id, or None if it is out of range.
§Examples
use bytecode_lang::{FuncId, ModuleBuilder};
let module = ModuleBuilder::new().finish().unwrap();
assert!(module.function(FuncId(0)).is_none());Sourcepub fn exports(&self) -> &[Export]
pub fn exports(&self) -> &[Export]
The export table.
§Examples
use bytecode_lang::{ExportItem, ModuleBuilder};
let mut m = ModuleBuilder::new();
let mut f = m.function("main", &[], &[]);
f.ret_void();
let main = m.add_function(f).unwrap();
m.export("main", ExportItem::Func(main));
assert_eq!(m.finish().unwrap().exports()[0].item, ExportItem::Func(main));Sourcepub fn hooks(&self) -> &[HookBinding]
pub fn hooks(&self) -> &[HookBinding]
The bound dynamic-operation hooks, sorted by hook code.
§Examples
use bytecode_lang::{Callee, Hook, ModuleBuilder, ValType};
let mut m = ModuleBuilder::new();
let mut f = m.function("concat", &[ValType::Dyn, ValType::Dyn], &[ValType::Dyn]);
let a = f.param(0);
f.ret(a);
let id = m.add_function(f).unwrap();
m.hook(Hook::Concat, Callee::Func(id));
assert_eq!(m.finish().unwrap().hooks()[0].hook, Hook::Concat);Sourcepub fn hook(&self, hook: Hook) -> Option<Callee>
pub fn hook(&self, hook: Hook) -> Option<Callee>
The function bound to hook, if any.
§Examples
use bytecode_lang::{Hook, ModuleBuilder};
assert_eq!(ModuleBuilder::new().finish().unwrap().hook(Hook::Add), None);Sourcepub fn name(&self) -> Option<StrId>
pub fn name(&self) -> Option<StrId>
The module’s name, if it has one.
§Examples
use bytecode_lang::ModuleBuilder;
let mut m = ModuleBuilder::new();
m.set_name("app.main");
let module = m.finish().unwrap();
assert_eq!(module.name().and_then(|n| module.string(n)), Some("app.main"));Sourcepub fn start(&self) -> Option<FuncId>
pub fn start(&self) -> Option<FuncId>
The initializer the loader runs after binding imports, if any.
§Examples
use bytecode_lang::ModuleBuilder;
let mut m = ModuleBuilder::new();
let mut f = m.function("init", &[], &[]);
f.ret_void();
let init = m.add_function(f).unwrap();
m.set_start(init);
assert_eq!(m.finish().unwrap().start(), Some(init));Trait Implementations§
Source§impl Display for Module
The disassembly listing; see crate::disassemble.
impl Display for Module
The disassembly listing; see crate::disassemble.