Skip to content

feat: serialize debug info in sb3 archive #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ast/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ pub struct Sprite {
pub costumes: Vec<Costume>,
pub sounds: Vec<Sound>,
pub procs: FxHashMap<SmolStr, Proc>,
#[serde(skip)]
pub proc_definitions: FxHashMap<SmolStr, Vec<Stmt>>,
pub proc_references: FxHashMap<SmolStr, References>,
pub proc_args: FxHashMap<SmolStr, Vec<Arg>>,
pub funcs: FxHashMap<SmolStr, Func>,
#[serde(skip)]
pub func_definitions: FxHashMap<SmolStr, Vec<Stmt>>,
pub func_references: FxHashMap<SmolStr, References>,
pub func_args: FxHashMap<SmolStr, Vec<Arg>>,
Expand All @@ -29,6 +31,7 @@ pub struct Sprite {
pub proc_locals: FxHashMap<SmolStr, FxHashMap<SmolStr, Var>>,
pub func_locals: FxHashMap<SmolStr, FxHashMap<SmolStr, Var>>,
pub lists: FxHashMap<SmolStr, List>,
#[serde(skip)]
pub events: Vec<Event>,
pub used_procs: FxHashSet<SmolStr>,
pub used_funcs: FxHashSet<SmolStr>,
Expand Down
14 changes: 14 additions & 0 deletions src/codegen/debug_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -15,3 +21,11 @@ pub struct DebugInfo {
pub procs: FxHashMap<String, Span>,
pub funcs: FxHashMap<String, Span>,
}

#[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<SmolStr, SpriteDiagnostics>,
}
19 changes: 18 additions & 1 deletion src/codegen/sb3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use zip::{

use super::{
cmd::cmd_to_list,
debug_info::ArtifactRef,
node::Node,
node_id::NodeID,
node_id_factory::NodeIDFactory,
Expand All @@ -38,6 +39,7 @@ use crate::{
codegen::mutation::Mutation,
config::Config,
diagnostic::{
Artifact,
DiagnosticKind,
SpriteDiagnostics,
},
Expand Down Expand Up @@ -278,6 +280,7 @@ where T: Write + Seek
pub costumes: FxHashMap<SmolStr, SmolStr>,
pub srcpkg_hash: Option<String>,
pub srcpkg: Option<Vec<u8>>,
pub release: bool,
}

impl<T> Write for Sb3<T>
Expand All @@ -295,7 +298,7 @@ where T: Write + Seek
impl<T> Sb3<T>
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(),
Expand All @@ -304,6 +307,7 @@ where T: Write + Seek
costumes: FxHashMap::default(),
srcpkg_hash: None,
srcpkg: None,
release,
}
}

Expand Down Expand Up @@ -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(())
}

Expand Down
6 changes: 5 additions & 1 deletion src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
Expand Down
8 changes: 6 additions & 2 deletions src/frontend/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ use crate::{
visitor,
};

pub fn build(input: Option<PathBuf>, output: Option<PathBuf>) -> anyhow::Result<Artifact> {
pub fn build(
input: Option<PathBuf>,
output: Option<PathBuf>,
release: bool,
) -> anyhow::Result<Artifact> {
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)
}
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
#[arg(short, long)]
release: bool,
},

/// Run a goboscript file using the experimental interpreter.
Expand Down
3 changes: 2 additions & 1 deletion src/translation_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ pub struct Include {
#[derive(Tsify, Serialize, Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct TranslationUnit {
pub path: PathBuf,
path: PathBuf,
#[serde(skip)]
text: Vec<u8>,
defines: FxHashSet<String>,
includes: Vec<Include>,
Expand Down
2 changes: 1 addition & 1 deletion src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down