Custom marshal enc/decoding impl

This commit is contained in:
Noa 2022-12-15 13:25:02 -06:00
parent 50b5388711
commit 41b465dee1
14 changed files with 913 additions and 229 deletions

View file

@ -16,9 +16,8 @@ bitflags = { workspace = true }
indexmap = { workspace = true }
itertools = { workspace = true }
log = { workspace = true }
num-complex = { workspace = true, features = ["serde"] }
num-complex = { workspace = true }
num-traits = { workspace = true }
thiserror = { workspace = true }
[dev-dependencies]
rustpython-parser = { path = "../parser" }

View file

@ -249,9 +249,9 @@ impl Compiler {
fn push_output(
&mut self,
flags: bytecode::CodeFlags,
posonlyarg_count: usize,
arg_count: usize,
kwonlyarg_count: usize,
posonlyarg_count: u32,
arg_count: u32,
kwonlyarg_count: u32,
obj_name: String,
) {
let source_path = self.source_path.clone();
@ -936,9 +936,11 @@ impl Compiler {
self.push_output(
bytecode::CodeFlags::NEW_LOCALS | bytecode::CodeFlags::IS_OPTIMIZED,
args.posonlyargs.len(),
args.posonlyargs.len() + args.args.len(),
args.kwonlyargs.len(),
args.posonlyargs.len().try_into().unwrap(),
(args.posonlyargs.len() + args.args.len())
.try_into()
.unwrap(),
args.kwonlyargs.len().try_into().unwrap(),
name.to_owned(),
);
@ -2750,8 +2752,8 @@ impl Compiler {
self.current_source_location = location;
}
fn get_source_line_number(&self) -> usize {
self.current_source_location.row()
fn get_source_line_number(&self) -> u32 {
self.current_source_location.row() as u32
}
fn push_qualified_path(&mut self, name: &str) {

View file

@ -2,7 +2,7 @@ use std::fmt;
pub type CodegenError = rustpython_compiler_core::BaseError<CodegenErrorType>;
#[derive(Debug, thiserror::Error)]
#[derive(Debug)]
#[non_exhaustive]
pub enum CodegenErrorType {
/// Invalid assignment, cannot store value in target.
@ -33,6 +33,8 @@ pub enum CodegenErrorType {
NotImplementedYet, // RustPython marker for unimplemented features
}
impl std::error::Error for CodegenErrorType {}
impl fmt::Display for CodegenErrorType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use CodegenErrorType::*;

View file

@ -63,11 +63,11 @@ impl Default for Block {
pub struct CodeInfo {
pub flags: CodeFlags,
pub posonlyarg_count: usize, // Number of positional-only arguments
pub arg_count: usize,
pub kwonlyarg_count: usize,
pub posonlyarg_count: u32, // Number of positional-only arguments
pub arg_count: u32,
pub kwonlyarg_count: u32,
pub source_path: String,
pub first_line_number: usize,
pub first_line_number: u32,
pub obj_name: String, // Name of the object that created this code object
pub blocks: Vec<Block>,
@ -172,15 +172,15 @@ impl CodeInfo {
}
}
fn cell2arg(&self) -> Option<Box<[isize]>> {
fn cell2arg(&self) -> Option<Box<[i32]>> {
if self.cellvar_cache.is_empty() {
return None;
}
let total_args = self.arg_count
+ self.kwonlyarg_count
+ self.flags.contains(CodeFlags::HAS_VARARGS) as usize
+ self.flags.contains(CodeFlags::HAS_VARKEYWORDS) as usize;
+ self.flags.contains(CodeFlags::HAS_VARARGS) as u32
+ self.flags.contains(CodeFlags::HAS_VARKEYWORDS) as u32;
let mut found_cellarg = false;
let cell2arg = self
@ -190,10 +190,10 @@ impl CodeInfo {
self.varname_cache
.get_index_of(var)
// check that it's actually an arg
.filter(|i| *i < total_args)
.filter(|i| *i < total_args as usize)
.map_or(-1, |i| {
found_cellarg = true;
i as isize
i as i32
})
})
.collect::<Box<[_]>>();