Struct netsblox_vm::bytecode::ByteCode
source · pub struct ByteCode { /* private fields */ }Expand description
An interpreter-ready sequence of instructions.
Process is an execution primitive that can be used to execute generated ByteCode.
Generated ByteCode is highly optimized for size and sees frequent breaking changes.
Because of this, there is currently no support for touching the actual bytecode itself.
If you wish to view the generated bytecode, you may use the ByteCode::dump_code and ByteCode::dump_data utilities,
a wrapper for which is included in the standard netsblox-vm cli.
This type supports serde serialization if the serde feature flag is enabled.
Implementations§
source§impl ByteCode
impl ByteCode
sourcepub fn compile(
role: &Role
) -> Result<(ByteCode, InitInfo, Locations, ScriptInfo<'_>), CompileError<'_>>
pub fn compile( role: &Role ) -> Result<(ByteCode, InitInfo, Locations, ScriptInfo<'_>), CompileError<'_>>
Compiles a single project role into an executable form.
The core information is stored in ByteCode (instructions acting on a state) and InitInfo (the initial project state).
This also emits a Locations object mapping bytecode index to code location (e.g., the block collabId from project xml),
which is needed to provide human-readable error locations at runtime,
as well as a ScriptInfo object that contains a symbol table of functions and scripts
(needed to execute a specific segment of code).
Examples found in repository?
79 80 81 82 83 84 85 86 87 88 89 90 91 92
fn get_running_project(xml: &str, system: Rc<StdSystem<C>>) -> EnvArena {
EnvArena::new(|mc| {
let parser = ast::Parser::default();
let ast = parser.parse(xml).unwrap();
assert_eq!(ast.roles.len(), 1); // this should be handled more elegantly in practice - for the sake of this example, we only allow one role
let (bytecode, init_info, locs, _) = ByteCode::compile(&ast.roles[0]).unwrap();
let mut proj = Project::from_init(mc, &init_info, Rc::new(bytecode), Settings::default(), system);
proj.input(mc, Input::Start); // this is equivalent to clicking the green flag button
Env { proj: Gc::new(mc, RefLock::new(proj)), locs }
})
}sourcepub fn dump_code(&self, f: &mut dyn Write) -> Result<()>
pub fn dump_code(&self, f: &mut dyn Write) -> Result<()>
Generates a hex dump of the stored code, including instructions and addresses.
sourcepub fn dump_data(&self, f: &mut dyn Write) -> Result<()>
pub fn dump_data(&self, f: &mut dyn Write) -> Result<()>
Generate a hex dump of the stored program data, including string literals and meta values.
sourcepub fn total_size(&self) -> usize
pub fn total_size(&self) -> usize
Returns the total size of the ByteCode object (in bytes).