Skip to main content

Module

Struct Module 

Source
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

Source

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);
Source

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);
Source

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"]);
Source

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)]);
Source

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)));
Source

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)]);
Source

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)));
Source

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);
Source

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));
Source

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);
Source

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));
Source

pub fn functions(&self) -> &[Function]

The function table.

§Examples
use bytecode_lang::ModuleBuilder;

assert!(ModuleBuilder::new().finish().unwrap().functions().is_empty());
Source

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());
Source

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));
Source

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);
Source

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);
Source

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"));
Source

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 Clone for Module

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Module

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Module

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Module

The disassembly listing; see crate::disassemble.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Module

Source§

impl PartialEq for Module

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Module

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.