Skip to content
Merged
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
anyhow = "1.0.91"
arcstr = { version = "1.2.0", features = ["serde"] }
base64 = "0.22.1"
chrono = "0.4"

Check warning on line 18 in Cargo.toml

View workflow job for this annotation

GitHub Actions / check

unused dependency `chrono`

Check warning on line 18 in Cargo.toml

View workflow job for this annotation

GitHub Actions / check

unused dependency `chrono`
clap = { version = "4.5.20", features = ["derive"] }
clap_complete_command = "0.6.1"
clap_derive = "4.5.18"
Expand All @@ -36,20 +36,26 @@
serde_json = { version = "1.0.128", features = ["preserve_order"] }
suggestions = "0.1.1"
toml = "0.8.19"
walkdir = "2.5.0"

Check warning on line 39 in Cargo.toml

View workflow job for this annotation

GitHub Actions / check

unused dependency `walkdir`

Check warning on line 39 in Cargo.toml

View workflow job for this annotation

GitHub Actions / check

unused dependency `walkdir`
zip = { version = "3.0.0", default-features = false, features = ["deflate"] }
wasm-bindgen = "0.2"
console_error_panic_hook = "0.1.7"
js-sys = "0.3.77"
tsify = { version = "0.5.7", default-features = false, features = ["js"] }
tempfile = "3.27.0"

Check warning on line 45 in Cargo.toml

View workflow job for this annotation

GitHub Actions / check

unused dependency `tempfile`

Check warning on line 45 in Cargo.toml

View workflow job for this annotation

GitHub Actions / check

unused dependency `tempfile`

[target.'cfg(target_arch = "wasm32")'.dependencies]
console_log = "1.0.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
rand = "0.9.1"

Check warning on line 51 in Cargo.toml

View workflow job for this annotation

GitHub Actions / check

unused dependency `rand`

Check warning on line 51 in Cargo.toml

View workflow job for this annotation

GitHub Actions / check

unused dependency `rand`

[target.'cfg(unix)'.dependencies]
libc = "0.2"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.60", features = ["Win32_Storage_FileSystem"] }

[build-dependencies]
lalrpop = "0.22.0"

Expand Down
15 changes: 9 additions & 6 deletions src/codegen/input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::io::{
self,
Write,
use std::{
borrow::Cow,
io::{
self,
Write,
},
};

use serde_json::json;
Expand Down Expand Up @@ -67,11 +70,11 @@ pub fn is_expr_boolean(expr: &Expr, s: S) -> bool {
)
}

pub fn coerce_condition(expr: &Expr, s: S) -> Expr {
pub fn coerce_condition<'a>(expr: &'a Expr, s: S) -> Cow<'a, Expr> {
if is_expr_boolean(expr, s) {
return expr.clone();
return Cow::Borrowed(expr);
}
BinOp::Eq.to_expr(0..0, expr.clone(), Value::from(true).to_expr(0..0))
Cow::Owned(BinOp::Eq.to_expr(0..0, expr.clone(), Value::from(true).to_expr(0..0)))
}

impl Sb3 {
Expand Down
19 changes: 8 additions & 11 deletions src/codegen/sb3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,20 +369,17 @@ impl Sb3 {
}

pub fn single_field(&mut self, name: &'static str, value: &str) -> io::Result<()> {
write!(
self.json,
r#","fields":{{"{name}":[{},null]}}"#,
json!(value)
)
write!(self.json, r#","fields":{{"{name}":["#)?;
serde_json::to_writer(&mut self.json, value)?;
self.json.write_all(b",null]}")
}

pub fn single_field_id(&mut self, name: &'static str, value: &str) -> io::Result<()> {
write!(
self.json,
r#","fields":{{"{name}":[{},{}]}}"#,
json!(value),
json!(value)
)
write!(self.json, r#","fields":{{"{name}":["#)?;
serde_json::to_writer(&mut self.json, value)?;
self.json.write_all(b",")?;
serde_json::to_writer(&mut self.json, value)?;
self.json.write_all(b"]}")
}

pub fn substack(&mut self, name: &str, this_id: Option<NodeID>) -> io::Result<()> {
Expand Down
54 changes: 50 additions & 4 deletions src/frontend/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
use std::{
cell::RefCell,
env,
fs::File,
io::BufWriter,
path::PathBuf,
fs::{
File,
OpenOptions,
},
io::{
self,
BufWriter,
},
path::{
Path,
PathBuf,
},
rc::Rc,
};

Expand All @@ -19,6 +28,43 @@ pub fn build(input: Option<PathBuf>, output: Option<PathBuf>) -> anyhow::Result<
let project_name = canonical_input.file_name().unwrap().to_str().unwrap();
let output = output.unwrap_or_else(|| input.join(format!("{project_name}.sb3")));
let fs = Rc::new(RefCell::new(RealFS));
let file = BufWriter::new(File::create(&output)?);
let file = BufWriter::new(open_output(&output)?);
build_impl(fs, canonical_input, file, None)
}

fn open_output(path: &Path) -> io::Result<File> {
let mut options = OpenOptions::new();
options.write(true).create(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.custom_flags(libc::O_NOFOLLOW);
}
#[cfg(windows)]
{
use std::os::windows::fs::OpenOptionsExt;

use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_OPEN_REPARSE_POINT;
options.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT);
}
#[cfg(not(any(unix, windows)))]
return Err(io::Error::new(
io::ErrorKind::Unsupported,
"opening output without following symlinks is unsupported",
));

let file = options.open(path)?;
// Inspect and truncate the same handle; never check the path before opening it.
let metadata = file.metadata()?;
#[cfg(windows)]
if metadata.file_type().is_symlink() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"output path is a symlink",
));
}
if metadata.is_file() {
file.set_len(0)?;
}
Ok(file)
}
25 changes: 5 additions & 20 deletions src/visitor/transformations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,37 +514,22 @@ pub fn keyword_arguments(
d: D,
) {
if let Some(sig) = signature {
// Build a new vector of arguments in the order given by the signature.
let mut new_args = Vec::with_capacity(sig.len());
let mut pos = 0;
let positional_count = args.len();

for param in sig {
if pos < args.len() {
for (index, param) in sig.iter().enumerate() {
if index < positional_count {
// If there is both a positional and keyword argument, we prefer the positional one.
// Remove the keyword argument from the map.
kwargs.remove(&param.name);
// Use the next positional argument.
new_args.push(args[pos].clone());
pos += 1;
} else if let Some((_, kw_expr)) = kwargs.remove(&param.name) {
// No more positional args, but there is a matching keyword argument.
new_args.push(kw_expr);
args.push(kw_expr);
} else if let Some(default) = &param.default {
// Compute the default value if one is provided.
new_args.push(default.clone().into());
args.push(default.clone().into());
}
// If no positional, keyword, or default value exists, then
// we simply do not insert anything (and no error is raised).
}

// Append any extra positional arguments that exceed the signature length.
while pos < args.len() {
new_args.push(args[pos].clone());
pos += 1;
}

// Replace the original args with the re-ordered version.
*args = new_args;
}

// Generate diagnostics for any remaining unknown keyword arguments
Expand Down
Loading