From 8d9014742d1f33e05bbd0c427959b1b9dd86c145 Mon Sep 17 00:00:00 2001 From: aspizu Date: Sat, 24 May 2025 05:51:59 +0530 Subject: [PATCH 1/2] feat: serialize debug info in sb3 archive --- src/codegen/debug_info.rs | 14 ++++++++++++++ src/codegen/sb3.rs | 19 ++++++++++++++++++- src/frontend.rs | 6 +++++- src/frontend/build.rs | 8 ++++++-- src/frontend/cli.rs | 2 ++ src/wasm.rs | 2 +- 6 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/codegen/debug_info.rs b/src/codegen/debug_info.rs index 2d0b3dfd..fd52b468 100644 --- a/src/codegen/debug_info.rs +++ b/src/codegen/debug_info.rs @@ -6,6 +6,12 @@ use serde::{ }; use tsify::Tsify; +use crate::{ + ast::Project, + diagnostic::SpriteDiagnostics, + misc::SmolStr, +}; + #[derive(Tsify, Serialize, Deserialize, Default)] #[tsify(into_wasm_abi, from_wasm_abi)] pub struct DebugInfo { @@ -15,3 +21,11 @@ pub struct DebugInfo { pub procs: FxHashMap, pub funcs: FxHashMap, } + +#[derive(Tsify, Serialize)] +#[tsify(into_wasm_abi, from_wasm_abi)] +pub struct ArtifactRef<'a> { + pub project: &'a Project, + pub stage_diagnostics: &'a SpriteDiagnostics, + pub sprites_diagnostics: &'a FxHashMap, +} diff --git a/src/codegen/sb3.rs b/src/codegen/sb3.rs index 3f7f2242..ba851136 100644 --- a/src/codegen/sb3.rs +++ b/src/codegen/sb3.rs @@ -27,6 +27,7 @@ use zip::{ use super::{ cmd::cmd_to_list, + debug_info::ArtifactRef, node::Node, node_id::NodeID, node_id_factory::NodeIDFactory, @@ -38,6 +39,7 @@ use crate::{ codegen::mutation::Mutation, config::Config, diagnostic::{ + Artifact, DiagnosticKind, SpriteDiagnostics, }, @@ -278,6 +280,7 @@ where T: Write + Seek pub costumes: FxHashMap, pub srcpkg_hash: Option, pub srcpkg: Option>, + pub release: bool, } impl Write for Sb3 @@ -295,7 +298,7 @@ where T: Write + Seek impl Sb3 where T: Write + Seek { - pub fn new(file: T) -> Self { + pub fn new(file: T, release: bool) -> Self { Self { zip: ZipWriter::new(file), id: NodeIDFactory::new(), @@ -304,6 +307,7 @@ where T: Write + Seek costumes: FxHashMap::default(), srcpkg_hash: None, srcpkg: None, + release, } } @@ -432,6 +436,19 @@ where T: Write + Seek write!(self, "}}")?; // meta write!(self, "}}")?; // project self.assets(fs.clone(), input)?; + if !self.release { + self.zip + .start_file("artifact.json", SimpleFileOptions::default())?; + write!( + self.zip, + "{}", + json!(ArtifactRef { + project, + stage_diagnostics, + sprites_diagnostics, + }) + )?; + } Ok(()) } diff --git a/src/frontend.rs b/src/frontend.rs index 3e89d7f5..b88fd90d 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -26,7 +26,11 @@ use crate::{ pub fn frontend() -> ExitCode { match Cli::parse().command { - Command::Build { input, output } => match build::build(input, output) { + Command::Build { + input, + output, + release, + } => match build::build(input, output, release) { Ok(artifact) => { artifact.eprint(); eprintln!(); diff --git a/src/frontend/build.rs b/src/frontend/build.rs index 6428f943..2bcabe26 100644 --- a/src/frontend/build.rs +++ b/src/frontend/build.rs @@ -39,12 +39,16 @@ use crate::{ visitor, }; -pub fn build(input: Option, output: Option) -> anyhow::Result { +pub fn build( + input: Option, + output: Option, + release: bool, +) -> anyhow::Result { let input = input.unwrap_or_else(|| env::current_dir().unwrap()); let canonical_input = input.canonicalize()?; let project_name = canonical_input.file_name().unwrap().to_str().unwrap(); let output = output.unwrap_or_else(|| input.join(format!("{project_name}.sb3"))); - let sb3 = Sb3::new(BufWriter::new(File::create(&output)?)); + let sb3 = Sb3::new(BufWriter::new(File::create(&output)?), release); let fs = Rc::new(RefCell::new(RealFS::new())); build_impl(fs, canonical_input, sb3, None) } diff --git a/src/frontend/cli.rs b/src/frontend/cli.rs index ae2edba6..1df725a9 100644 --- a/src/frontend/cli.rs +++ b/src/frontend/cli.rs @@ -25,6 +25,8 @@ pub enum Command { #[arg(short, long)] /// Output file, if not given, it will be the project directory's name + `.sb3` output: Option, + #[arg(short, long)] + release: bool, }, /// Run a goboscript file using the experimental interpreter. diff --git a/src/wasm.rs b/src/wasm.rs index 243de7a5..6e2243c5 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -40,7 +40,7 @@ pub struct Build { pub fn build(fs: JsValue) -> JsValue { let fs: MemFS = serde_wasm_bindgen::from_value(fs).unwrap(); let mut file = Vec::new(); - let sb3 = Sb3::new(Cursor::new(&mut file)); + let sb3 = Sb3::new(Cursor::new(&mut file), true); let stdlib = StandardLibrary { path: "stdlib".into(), version: Version::new(0, 0, 0), From 5fe052193e0e8ecf893f86939b9474c8c7f49131 Mon Sep 17 00:00:00 2001 From: aspizu Date: Sat, 24 May 2025 05:58:46 +0530 Subject: [PATCH 2/2] fix: skip serializing useless items into debug artifact --- src/ast/sprite.rs | 3 +++ src/translation_unit.rs | 1 + 2 files changed, 4 insertions(+) diff --git a/src/ast/sprite.rs b/src/ast/sprite.rs index 51b0fc2f..fb5e19cb 100644 --- a/src/ast/sprite.rs +++ b/src/ast/sprite.rs @@ -16,10 +16,12 @@ pub struct Sprite { pub costumes: Vec, pub sounds: Vec, pub procs: FxHashMap, + #[serde(skip)] pub proc_definitions: FxHashMap>, pub proc_references: FxHashMap, pub proc_args: FxHashMap>, pub funcs: FxHashMap, + #[serde(skip)] pub func_definitions: FxHashMap>, pub func_references: FxHashMap, pub func_args: FxHashMap>, @@ -29,6 +31,7 @@ pub struct Sprite { pub proc_locals: FxHashMap>, pub func_locals: FxHashMap>, pub lists: FxHashMap, + #[serde(skip)] pub events: Vec, pub used_procs: FxHashSet, pub used_funcs: FxHashSet, diff --git a/src/translation_unit.rs b/src/translation_unit.rs index 51edbd39..efbcb077 100644 --- a/src/translation_unit.rs +++ b/src/translation_unit.rs @@ -47,6 +47,7 @@ pub struct Include { #[tsify(into_wasm_abi, from_wasm_abi)] pub struct TranslationUnit { path: PathBuf, + #[serde(skip)] text: Vec, defines: FxHashSet, includes: Vec,