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?
84fn get_running_project(xml: &str, system: Rc<StdSystem<C>>) -> EnvArena {
85 EnvArena::new(|mc| {
86 let parser = ast::Parser::default();
87 let ast = parser.parse(xml).unwrap();
88 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
89
90 let (bytecode, init_info, locs, _) = ByteCode::compile(&ast.roles[0]).unwrap();
91
92 let mut proj = Project::from_init(mc, &init_info, Rc::new(bytecode), Settings::default(), system);
93 proj.input(mc, Input::Start); // this is equivalent to clicking the green flag button
94
95 Env { proj: Gc::new(mc, RefLock::new(proj)), locs }
96 })
97}
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).