Skip to main content

siglus_scene_vm/runtime/
opcode.rs

1//! Numeric opcode/form plumbing.
2//!
3//! Siglus dispatches many operations by numeric form codes.
4//! When porting, a non-trivial subset of codes may be unknown.
5//!
6//! This module provides:
7//! - A stable representation of a numeric operation (OpCode)
8//! - A dispatcher hook (dispatch_code)
9
10use anyhow::Result;
11
12use super::{forms, CommandContext, Value};
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
15pub struct OpCode {
16    pub id: u32,
17}
18
19impl OpCode {
20    pub const fn form(id: u32) -> Self {
21        Self { id }
22    }
23}
24
25/// Dispatch a numeric form operation.
26///
27/// Returns true if the code was recognized and handled.
28pub fn dispatch_code(ctx: &mut CommandContext, code: OpCode, args: &[Value]) -> Result<bool> {
29    if let Some(h) = ctx.external_forms.clone() {
30        if h.dispatch_form(ctx, code.id, args)? {
31            return Ok(true);
32        }
33    }
34    forms::dispatch_form(ctx, code.id, args)
35}