2024-10-30 07:08:25 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
const PANIC: u8 = 0b0000_0000;
|
|
|
|
const TO_STRING: u8 = 0b0000_0001;
|
2024-10-30 09:16:34 +00:00
|
|
|
const WRITE: u8 = 0b0000_0010;
|
|
|
|
const WRITE_LINE: u8 = 0b0000_0011;
|
2024-10-30 07:08:25 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
|
|
|
pub enum NativeFunction {
|
|
|
|
Panic = PANIC as isize,
|
|
|
|
ToString = TO_STRING as isize,
|
2024-10-30 09:16:34 +00:00
|
|
|
Write = WRITE as isize,
|
2024-10-30 07:08:25 +00:00
|
|
|
WriteLine = WRITE_LINE as isize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u8> for NativeFunction {
|
|
|
|
fn from(byte: u8) -> Self {
|
|
|
|
match byte {
|
|
|
|
PANIC => NativeFunction::Panic,
|
|
|
|
TO_STRING => NativeFunction::ToString,
|
2024-10-30 09:16:34 +00:00
|
|
|
WRITE => NativeFunction::Write,
|
2024-10-30 07:08:25 +00:00
|
|
|
WRITE_LINE => NativeFunction::WriteLine,
|
|
|
|
_ => {
|
|
|
|
if cfg!(test) {
|
2024-10-30 09:16:34 +00:00
|
|
|
panic!("Invalid native function byte: {}", byte)
|
2024-10-30 07:08:25 +00:00
|
|
|
} else {
|
|
|
|
NativeFunction::Panic
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<NativeFunction> for u8 {
|
|
|
|
fn from(native_function: NativeFunction) -> Self {
|
|
|
|
match native_function {
|
|
|
|
NativeFunction::Panic => PANIC,
|
|
|
|
NativeFunction::ToString => TO_STRING,
|
2024-10-30 09:16:34 +00:00
|
|
|
NativeFunction::Write => WRITE,
|
2024-10-30 07:08:25 +00:00
|
|
|
NativeFunction::WriteLine => WRITE_LINE,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|