mirror of
https://github.com/storytold/effectcraft.git
synced 2026-10-26 11:07:54 -06:00
Merge pull request #1594 from rahal/fix/script-host-files
fix(web): scripts can open files in browser storage; stored footage isn't reported missing (#1593)
This commit is contained in:
@@ -106,5 +106,9 @@ mod tests_ui_more;
|
||||
// #760: `$.evalFile` and `BridgeTalk`.
|
||||
#[cfg(test)]
|
||||
mod tests_evalfile;
|
||||
// `/browser/…` is the web editor's file table, an absolute path on wasm; on Windows it has no
|
||||
// drive, so the script runtime resolves it under the current drive, where the host holds nothing.
|
||||
#[cfg(all(test, not(windows)))]
|
||||
mod tests_services;
|
||||
#[cfg(test)]
|
||||
mod tests_ui_mouse;
|
||||
|
||||
@@ -400,11 +400,14 @@ fn native_file(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsV
|
||||
}
|
||||
let s = &a.session;
|
||||
match op.as_str() {
|
||||
"access" => Ok(json!(check_access(s, &path, false)?.exists())),
|
||||
// Through the session's services, like the reads: hosts without a file system (the
|
||||
// browser's file table) answer for their own files.
|
||||
"access" => Ok(json!(s.services.exists(&check_access(s, &path, false)?.to_string_lossy()))),
|
||||
"exists" | "isDir" => {
|
||||
// Unreadable locations report "missing" instead of failing.
|
||||
let Ok(p) = check_access(s, &path, false) else { return Ok(json!(false)) };
|
||||
Ok(json!(if op == "exists" { p.exists() } else { p.is_dir() }))
|
||||
let p = p.to_string_lossy();
|
||||
Ok(json!(if op == "exists" { s.services.exists(&p) } else { s.services.is_dir(&p) }))
|
||||
}
|
||||
"read" | "readBinary" => {
|
||||
let p = check_access(s, &path, false)?;
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
//! Scripts see the host's files: existence checks go through `Services`, like reads, so a host
|
||||
//! that keeps files in memory (the web editor's browser storage) answers for its own files.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use effectcraft_engine::{Services, Session};
|
||||
|
||||
use crate::run_code;
|
||||
|
||||
#[derive(Default)]
|
||||
struct Mem(Mutex<HashMap<String, Vec<u8>>>);
|
||||
|
||||
impl Services for Mem {
|
||||
fn read_file(&self, path: &str) -> std::io::Result<Vec<u8>> {
|
||||
self.0.lock().unwrap().get(path).cloned().ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, path.to_string()))
|
||||
}
|
||||
fn write_file(&self, path: &str, data: &[u8]) -> std::io::Result<()> {
|
||||
self.0.lock().unwrap().insert(path.into(), data.into());
|
||||
Ok(())
|
||||
}
|
||||
fn exists(&self, path: &str) -> bool {
|
||||
self.0.lock().unwrap().contains_key(path)
|
||||
}
|
||||
fn is_dir(&self, path: &str) -> bool {
|
||||
path == "/browser/footage"
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_script_opens_a_file_only_the_host_holds() {
|
||||
let mem = Arc::new(Mem::default());
|
||||
mem.write_file("/browser/data.txt", b"hello").unwrap();
|
||||
let mut s = Session { services: mem, ..Default::default() };
|
||||
crate::install(&mut s);
|
||||
// A project in a folder that isn't on disk: scripts may read files next to it.
|
||||
s.path = Some("/browser/project.ecproj".into());
|
||||
let out = run_code(
|
||||
&mut s,
|
||||
r#"var f = new File("/browser/data.txt"); var ok = f.open("r");
|
||||
writeLn(ok + " " + (ok ? f.read() : f.error));
|
||||
writeLn(new File("/browser/missing.txt").exists + " " + new Folder("/browser/footage").exists);"#,
|
||||
"t.jsx",
|
||||
);
|
||||
assert!(out.error.is_none(), "{:?}", out.error);
|
||||
assert_eq!(out.output, vec!["true hello".to_string(), "false true".to_string()]);
|
||||
}
|
||||
Reference in New Issue
Block a user