use crate::compiler::Program;
use crate::host::{BeginDef, ClassDef, MethodDef, ProcDef, RescueDef};
use fusevm::Chunk;
use rkyv::{Archive, Deserialize as RkyvDe, Serialize as RkyvSer};
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};
use std::path::PathBuf;
const SCHEMA: u64 = 1;
#[derive(Archive, RkyvSer, RkyvDe, Default)]
#[archive(check_bytes)]
struct Shard {
entries: Vec<Entry>,
}
#[derive(Archive, RkyvSer, RkyvDe)]
#[archive(check_bytes)]
struct Entry {
key: u64,
blob: Vec<u8>,
}
type CMethod = (
String,
Vec<String>,
Option<usize>,
Vec<String>,
Option<String>,
Option<String>,
Chunk,
);
type CClass = (
String,
Option<String>,
Vec<CMethod>,
Vec<String>,
Vec<CMethod>,
);
type CRescue = (Vec<String>, Option<String>, usize);
type CBegin = (usize, Vec<CRescue>, Option<usize>);
type CProc = (Vec<String>, Chunk);
#[derive(Serialize, Deserialize)]
struct CProg {
main: Chunk,
methods: Vec<CMethod>,
classes: Vec<CClass>,
begins: Vec<CBegin>,
procs: Vec<CProc>,
}
pub fn key_for(src: &str) -> u64 {
let mut h = rustc_hash::FxHasher::default();
SCHEMA.hash(&mut h);
src.hash(&mut h);
h.finish()
}
fn shard_path() -> Option<PathBuf> {
let dir = dirs::home_dir()?.join(".rubylang");
let _ = std::fs::create_dir_all(&dir);
Some(dir.join("scripts.rkyv"))
}
fn load_shard() -> Shard {
let Some(path) = shard_path() else {
return Shard::default();
};
let Ok(bytes) = std::fs::read(&path) else {
return Shard::default();
};
rkyv::from_bytes::<Shard>(&bytes).unwrap_or_default()
}
fn write_shard(shard: &Shard) -> Result<(), String> {
let path = shard_path().ok_or("no home dir for cache")?;
let bytes = rkyv::to_bytes::<_, 4096>(shard).map_err(|e| format!("cache serialize: {e}"))?;
std::fs::write(&path, &bytes).map_err(|e| format!("cache write: {e}"))
}
pub fn load(src: &str) -> Option<Program> {
let key = key_for(src);
let shard = load_shard();
let entry = shard.entries.iter().find(|e| e.key == key)?;
let cp: CProg = bincode::deserialize(&entry.blob).ok()?;
Some(from_cprog(cp))
}
pub fn store(src: &str, prog: &Program) -> Result<(), String> {
let key = key_for(src);
let blob = bincode::serialize(&to_cprog(prog)).map_err(|e| format!("cache encode: {e}"))?;
let mut shard = load_shard();
shard.entries.retain(|e| e.key != key);
shard.entries.push(Entry { key, blob });
write_shard(&shard)
}
fn m_to(name: &str, m: &MethodDef) -> CMethod {
(
name.to_string(),
m.params.clone(),
m.splat,
m.kwparams.clone(),
m.kwsplat.clone(),
m.blockparam.clone(),
m.chunk.clone(),
)
}
fn m_from(
(name, params, splat, kwparams, kwsplat, blockparam, chunk): CMethod,
) -> (String, MethodDef) {
(
name,
MethodDef {
params,
splat,
kwparams,
kwsplat,
blockparam,
chunk,
},
)
}
fn to_cprog(prog: &Program) -> CProg {
CProg {
main: prog.main.clone(),
methods: prog.methods.iter().map(|(n, m)| m_to(n, m)).collect(),
classes: prog
.classes
.iter()
.map(|(n, c)| {
let methods = c.methods.iter().map(|(mn, m)| m_to(mn, m)).collect();
let class_methods = c.class_methods.iter().map(|(mn, m)| m_to(mn, m)).collect();
(
n.clone(),
c.superclass.clone(),
methods,
c.includes.clone(),
class_methods,
)
})
.collect(),
begins: prog
.begins
.iter()
.map(|bd| {
let rescues = bd
.rescues
.iter()
.map(|r| (r.classes.clone(), r.binding.clone(), r.body))
.collect();
(bd.body, rescues, bd.ensure)
})
.collect(),
procs: prog
.procs
.iter()
.map(|p| (p.params.clone(), p.chunk.clone()))
.collect(),
}
}
fn from_cprog(cp: CProg) -> Program {
Program {
main: cp.main,
methods: cp.methods.into_iter().map(m_from).collect(),
classes: cp
.classes
.into_iter()
.map(|(n, superclass, methods, includes, class_methods)| {
let methods = methods.into_iter().map(m_from).collect();
let class_methods = class_methods.into_iter().map(m_from).collect();
(
n,
ClassDef {
superclass,
methods,
includes,
class_methods,
},
)
})
.collect(),
begins: cp
.begins
.into_iter()
.map(|(body, rescues, ensure)| {
let rescues = rescues
.into_iter()
.map(|(classes, binding, body)| RescueDef {
classes,
binding,
body,
})
.collect();
BeginDef {
body,
rescues,
ensure,
}
})
.collect(),
procs: cp
.procs
.into_iter()
.map(|(params, chunk)| ProcDef { params, chunk })
.collect(),
}
}