-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement serde for interpreter (#1909)
* implement serde for interpreter * add bytecode check * remove bytecode check * remove defaults generic params * add sanity check + cargo fmt * modify panic message
- Loading branch information
Showing
7 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use super::ExtBytecode; | ||
use crate::interpreter::Jumps; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct ExtBytecodeSerde { | ||
base: bytecode::Bytecode, | ||
program_counter: usize, | ||
} | ||
|
||
impl Serialize for ExtBytecode { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
ExtBytecodeSerde { | ||
base: self.base.clone(), | ||
program_counter: self.pc(), | ||
} | ||
.serialize(serializer) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for ExtBytecode { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let ExtBytecodeSerde { | ||
base, | ||
program_counter, | ||
} = ExtBytecodeSerde::deserialize(deserializer)?; | ||
|
||
let mut bytecode = Self::new(base); | ||
|
||
if program_counter >= bytecode.base.bytecode().len() { | ||
panic!("serde pc: {program_counter} is greater than or equal to bytecode len"); | ||
} | ||
bytecode.absolute_jump(program_counter); | ||
Ok(bytecode) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters