Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

Source stamps (work in progress, parked): engine/bundle's stamp.rs and its lib.rs and test hunks, parked by the orchestrator so #51's fix can land in the same crate; not for landing until its owner finishes it

shinyobjectze1914cabca63
diff --git a/engine/bundle/src/lib.rs b/engine/bundle/src/lib.rs
index 3a11c0e5..84129cdb 100644
--- a/engine/bundle/src/lib.rs
+++ b/engine/bundle/src/lib.rs
@@ -17,6 +17,7 @@ use std::sync::{Arc, Mutex};
pub mod edit;
pub mod script;
+pub mod stamp;
pub mod z3;
/// A bundle: one browser script (an IIFE) and the CSS its modules imported (empty when none)
@@ -38,6 +39,8 @@ pub struct Options {
pub defines: Vec<(String, String)>,
/// a composition's bundle: the edit's own packages (edit.rs)
pub edit: Option<edit::Edit>,
+ /// each element the edit's own files write stamped with its place in the code (stamp.rs; the app's editor asks)
+ pub stamps: bool,
}
/// `entry` bundled into one browser script and its CSS.
@@ -60,6 +63,13 @@ pub fn bundle_with(entry: &Path, o: Options) -> Result<Bundle, String> {
experimental: Some(ExperimentalOptions { attach_debug_info: Some(AttachDebugInfo::None), ..Default::default() }),
..Default::default()
};
+ let stamps = if o.stamps {
+ let helper = cwd.join("raea-stamp.tsx");
+ std::fs::write(&helper, stamp::HELPER).map_err(|e| format!("{}: {e}", helper.display()))?;
+ Some(Stamp { src: cwd.join("src"), helper, z3: o.z3 && std::env::var("RAEA_LITE_COMPILE").as_deref() != Ok("0") })
+ } else {
+ None
+ };
let sheets: Sheets = Default::default();
let order: Arc<Mutex<Vec<String>>> = Default::default();
let css = Css { root: cwd, sheets: sheets.clone(), order: order.clone() };
@@ -74,6 +84,9 @@ pub fn bundle_with(entry: &Path, o: Options) -> Result<Bundle, String> {
s.spawn(move || {
rt.block_on(async move {
let mut plugins = vec![Pluginable::new_shared(css)];
+ if let Some(s) = stamps {
+ plugins.push(Pluginable::new_shared(s));
+ }
if o.z3 && std::env::var("RAEA_LITE_COMPILE").as_deref() != Ok("0") {
plugins.push(Pluginable::new_shared(Z3));
}
@@ -259,6 +272,44 @@ impl Plugin for Z3 {
}
}
+/// The stamp plugin (stamp.rs): the edit's own TSX and JSX loaded stamped (and Z3-compiled after, for a live bundle)
+#[derive(Debug)]
+struct Stamp {
+ /// the work folder's copy of the edit's folder
+ src: PathBuf,
+ helper: PathBuf,
+ z3: bool,
+}
+
+impl Plugin for Stamp {
+ fn name(&self) -> Cow<'static, str> {
+ Cow::Borrowed("raea:stamp")
+ }
+
+ fn register_hook_usage(&self) -> HookUsage {
+ HookUsage::Load
+ }
+
+ async fn load(&self, _ctx: SharedLoadPluginContext, args: &HookLoadArgs<'_>) -> HookLoadReturn {
+ let path = args.id.split(['?', '#']).next().unwrap_or(args.id);
+ let jsx = path.ends_with(".jsx");
+ if !(jsx || path.ends_with(".tsx")) {
+ return Ok(None);
+ }
+ let file = Path::new(path);
+ // the resolver names files with symlinks resolved (/var is /private/var on the Mac)
+ let src = self.src.canonicalize().unwrap_or_else(|_| self.src.clone());
+ let Some(rel) = stamp::own(file, &src).or_else(|| stamp::own(file, &self.src)) else { return Ok(None) };
+ let text = std::fs::read_to_string(file)?;
+ let Some(stamped) = stamp::stamp(&text, &rel, jsx, &self.helper.to_string_lossy()) else { return Ok(None) };
+ if self.z3 {
+ let code = z3::compile(&stamped, jsx).map_err(|e| anyhow::anyhow!("{path}: {e}"))?;
+ return Ok(Some(HookLoadOutput { code: code.into(), module_type: Some(ModuleType::Js), ..Default::default() }));
+ }
+ Ok(Some(HookLoadOutput { code: stamped.into(), module_type: Some(if jsx { ModuleType::Jsx } else { ModuleType::Tsx }), ..Default::default() }))
+ }
+}
+
/// The C API raea loads (libraea_bundle.dylib), so raea links none of Rolldown's crates.
pub mod ffi {
use std::ffi::{c_char, CStr, CString};
@@ -269,7 +320,7 @@ pub mod ffi {
1
}
- /// `entry` (a path, UTF-8) bundled with `opts` (JSON: {"z3": bool, "edit": {"dir", "js"}, "script": {..}, "defines"}); returns JSON, {"js","css"} or {"error"}, to
+ /// `entry` (a path, UTF-8) bundled with `opts` (JSON: {"z3": bool, "edit": {"dir", "js"}, "script": {..}, "defines", "stamps": bool}); returns JSON, {"js","css"} or {"error"}, to
/// be freed with raea_bundle_free
///
/// # Safety
@@ -293,7 +344,7 @@ pub mod ffi {
js: path(&e["js"]),
work: std::path::absolute(&entry).ok().and_then(|p| p.parent().map(|d| d.to_path_buf())).unwrap_or_default(),
});
- let o = super::Options { z3: opts["z3"].as_bool().unwrap_or(false), script, defines, edit };
+ let o = super::Options { z3: opts["z3"].as_bool().unwrap_or(false), script, defines, edit, stamps: opts["stamps"].as_bool().unwrap_or(false) };
match super::bundle_with(std::path::Path::new(&entry), o) {
Ok(b) => serde_json::json!({"js": b.js, "css": b.css}),
Err(e) => serde_json::json!({"error": e}),
diff --git a/engine/bundle/src/stamp.rs b/engine/bundle/src/stamp.rs
new file mode 100644
index 00000000..4bc9ab26
--- /dev/null
+++ b/engine/bundle/src/stamp.rs
@@ -0,0 +1,238 @@
+//! Source stamps (docs/editing.feature, E2): each element a composition's own files write carries the place in the code
+//! that wrote it, so the render's manifest can say which JSX drew a run of text or a video, whatever made its words.
+//!
+//! The stamp is a source-to-source pass over a TSX file of the edit (its copy under the work folder's src/), before any
+//! compile, that keeps every line where it was:
+//! - an HTML element (`<h1>`, `<span>`, `<video>`…) gets `data-raea-loc="<file>:<line>:<col>"` and
+//! `data-raea-use={__raeaUse()}`: the use sites of the edit's components it was drawn inside, nearest first
+//! ("card.tsx:3:5|promo.tsx:12:7"), read from a context;
+//! - a use of one of the edit's own components (`<Card>`, defined in the file or imported from a relative path) is
+//! wrapped in that context's provider with its place added, so a component used twice draws two different chains.
+//! Not inside an element of a package's component (raea's <Series>, <TransitionSeries>), which may read its children's
+//! types: there the use isn't wrapped, and its elements name only where they're written.
+//!
+//! Lines and columns are 1-based, columns in UTF-16 units as TypeScript counts them (the host's edit.ts finds the
+//! element again with the TypeScript compiler). The helper module is written beside the entry (`raea-stamp.tsx`) and
+//! imported on the file's first line, so no line moves.
+use oxc::allocator::Allocator;
+use oxc::ast::ast::*;
+use oxc::ast_visit::{walk, Visit};
+use oxc::parser::Parser;
+use oxc::span::{GetSpan, SourceType};
+use std::collections::HashMap;
+use std::path::Path;
+
+/// HTML elements stamped: what a composition's page draws. Other lowercase tags (bevy-react's `node`/`text`/`image`, an
+/// R3F `mesh`, SVG's parts) are left alone, since their props may mean something to what draws them.
+const HTML: &[&str] = &[
+ "div", "span", "p", "h1", "h2", "h3", "h4", "h5", "h6", "section", "article", "header", "footer", "main", "aside", "nav", "figure", "figcaption", "blockquote", "pre", "code", "strong", "em", "b", "i", "u", "s", "small", "mark", "sub", "sup", "label", "ul", "ol", "li", "dl", "dt", "dd", "img", "video", "audio", "picture", "a", "button", "table", "tr", "td", "th", "svg", "canvas", "br", "hr",
+];
+
+/// The helper module stamped files import: the context and its two readers.
+pub const HELPER: &str = r#"// raea's source stamps (engine/bundle stamp.rs): the use sites of the edit's components, nearest first
+import { createContext, useContext } from "react";
+const At = createContext("");
+const read = (): string => {
+ try {
+ return useContext(At);
+ } catch {
+ // JSX made outside a render (a module's constant): no component around it
+ return "";
+ }
+};
+export const __RaeaAt = At.Provider;
+export const __raeaUse = (): string | undefined => read() || undefined;
+export const __raeaChain = (loc: string): string => {
+ const up = read();
+ return up ? `${loc}|${up}` : loc;
+};
+"#;
+
+/// `source` (TSX, or JSX when `jsx`), the file at `rel` (the edit's folder's path, with `/`), stamped; `helper` is
+/// the import path of the helper module. None when there was nothing to stamp or it doesn't parse (it then compiles
+/// as written, and says why there).
+pub fn stamp(source: &str, rel: &str, jsx: bool, helper: &str) -> Option<String> {
+ let alloc = Allocator::default();
+ let st = if jsx { SourceType::jsx() } else { SourceType::tsx() };
+ let parsed = Parser::new(&alloc, source, st).parse();
+ if !parsed.diagnostics.is_empty() {
+ return None;
+ }
+ let mut imports = HashMap::new();
+ for s in &parsed.program.body {
+ let Statement::ImportDeclaration(d) = s else { continue };
+ let local = d.source.value.starts_with('.');
+ for sp in d.specifiers.iter().flatten() {
+ imports.insert(sp.local().name.to_string(), local);
+ }
+ }
+ let mut v = Stamper { src: source, rel, imports, parents: Vec::new(), edits: Vec::new(), lines: line_starts(source) };
+ v.visit_program(&parsed.program);
+ if v.edits.is_empty() {
+ return None;
+ }
+ let mut out = String::with_capacity(source.len() + v.edits.len() * 64);
+ out.push_str(&format!("import {{ __RaeaAt, __raeaUse, __raeaChain }} from {};", json(helper)));
+ let mut edits = v.edits;
+ // by place; at one place, a wrapper's close before a later element's open (stable sort keeps the visit's order)
+ edits.sort_by_key(|e| e.0);
+ let mut at = 0;
+ for (pos, end, text) in edits {
+ if pos < at {
+ continue;
+ }
+ out.push_str(&source[at..pos]);
+ out.push_str(&text);
+ at = end;
+ }
+ out.push_str(&source[at..]);
+ Some(out)
+}
+
+#[derive(Clone, Copy, PartialEq)]
+enum Kind {
+ /// an HTML element, a fragment, or one of the edit's components: its children may be wrapped
+ Open,
+ /// a package's component: its children are left as written
+ Package,
+}
+
+struct Stamper<'s> {
+ src: &'s str,
+ rel: &'s str,
+ /// each imported name: true when it's from a relative path (the edit's own)
+ imports: HashMap<String, bool>,
+ parents: Vec<Kind>,
+ /// (at, end, text): `src[at..end]` replaced by `text`
+ edits: Vec<(usize, usize, String)>,
+ lines: Vec<usize>,
+}
+
+impl Stamper<'_> {
+ fn loc(&self, at: usize) -> String {
+ let line = self.lines.partition_point(|&s| s <= at);
+ let start = self.lines[line - 1];
+ let col = self.src[start..at].encode_utf16().count() + 1;
+ format!("{}:{line}:{col}", self.rel)
+ }
+}
+
+impl<'a> Visit<'a> for Stamper<'_> {
+ fn visit_jsx_element(&mut self, it: &JSXElement<'a>) {
+ let open = &it.opening_element;
+ let mut kind = Kind::Open;
+ match &open.name {
+ JSXElementName::Identifier(id) if HTML.contains(&id.name.as_str()) => {
+ let text = format!(" data-raea-loc={} data-raea-use={{__raeaUse()}}", json(&self.loc(it.span.start as usize)));
+ let at = open.name.span().end as usize;
+ self.edits.push((at, at, text));
+ }
+ JSXElementName::Identifier(_) => {}
+ JSXElementName::IdentifierReference(r) => {
+ let name = r.name.as_str();
+ // the edit's own: defined here (not imported) or imported from a relative path
+ let own = self.imports.get(name).copied().unwrap_or(true);
+ if !own {
+ kind = Kind::Package;
+ } else if self.parents.last() != Some(&Kind::Package) {
+ let (start, end) = (it.span.start as usize, it.span.end as usize);
+ // a key belongs on what's in the list: the wrapper
+ let key = open.attributes.iter().find_map(|a| match a {
+ JSXAttributeItem::Attribute(x) if matches!(&x.name, JSXAttributeName::Identifier(n) if n.name == "key") => Some(x.span),
+ _ => None,
+ });
+ let mut wrap = format!("<__RaeaAt value={{__raeaChain({})}}", json(&self.loc(start)));
+ if let Some(k) = key {
+ wrap.push(' ');
+ wrap.push_str(&self.src[k.start as usize..k.end as usize]);
+ self.edits.push((k.start as usize, k.end as usize, String::new()));
+ }
+ wrap.push('>');
+ self.edits.push((start, start, wrap));
+ self.edits.push((end, end, "</__RaeaAt>".into()));
+ }
+ }
+ // <Foo.Bar>, <a:b>, <this>: a package's or unknown, left alone
+ _ => kind = Kind::Package,
+ }
+ self.parents.push(kind);
+ walk::walk_jsx_element(self, it);
+ self.parents.pop();
+ }
+
+ fn visit_jsx_fragment(&mut self, it: &JSXFragment<'a>) {
+ self.parents.push(Kind::Open);
+ walk::walk_jsx_fragment(self, it);
+ self.parents.pop();
+ }
+}
+
+fn line_starts(s: &str) -> Vec<usize> {
+ std::iter::once(0).chain(s.match_indices('\n').map(|(i, _)| i + 1)).collect()
+}
+
+fn json(s: &str) -> String {
+ serde_json::to_string(s).unwrap_or_else(|_| "\"\"".into())
+}
+
+/// Whether `file` is one of the edit's own sources under `src` (the work folder's copy of its folder), and its path
+/// there with `/`.
+pub fn own(file: &Path, src: &Path) -> Option<String> {
+ let rel = file.strip_prefix(src).ok()?;
+ if rel.components().any(|c| c.as_os_str() == "node_modules") {
+ return None;
+ }
+ Some(rel.components().map(|c| c.as_os_str().to_string_lossy()).collect::<Vec<_>>().join("/"))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ const H: &str = "/w/raea-stamp.tsx";
+
+ #[test]
+ fn html_elements_carry_their_place_and_lines_stay() {
+ let src = "export default function A() {\n return <div>\n <h1>Hi</h1>\n </div>\n}\n";
+ let out = stamp(src, "a.tsx", false, H).unwrap();
+ assert_eq!(out.lines().count(), src.lines().count());
+ assert!(out.contains(r#"<div data-raea-loc="a.tsx:2:10" data-raea-use={__raeaUse()}>"#), "{out}");
+ assert!(out.contains(r#"<h1 data-raea-loc="a.tsx:3:5" data-raea-use={__raeaUse()}>Hi</h1>"#), "{out}");
+ assert!(out.starts_with("import { __RaeaAt, __raeaUse, __raeaChain } from \"/w/raea-stamp.tsx\";export default"));
+ }
+
+ #[test]
+ fn own_components_are_wrapped_with_their_use_and_keep_the_key() {
+ let src = "import { Card } from \"./card\";\nimport { Series } from \"raea\";\nconst X = () => <>{[1, 2].map((i) => <Card key={i} n={i} />)}</>;\n";
+ let out = stamp(src, "p.tsx", false, H).unwrap();
+ assert!(out.contains(r#"<__RaeaAt value={__raeaChain("p.tsx:3:38")} key={i}><Card n={i} /></__RaeaAt>"#), "{out}");
+ }
+
+ #[test]
+ fn a_package_components_children_are_left_as_written() {
+ let src = "import { Series } from \"raea\";\nfunction Own() { return null }\nconst X = () => <Series><Own /></Series>;\nconst Y = () => <div><Own /></div>;\n";
+ let out = stamp(src, "p.tsx", false, H).unwrap();
+ assert!(out.contains("<Series><Own /></Series>"), "{out}");
+ assert!(out.contains(r#"<__RaeaAt value={__raeaChain("p.tsx:4:22")}><Own /></__RaeaAt>"#), "{out}");
+ }
+
+ #[test]
+ fn other_tags_and_unparsable_files_are_left_alone() {
+ assert_eq!(stamp("const a = <mesh><boxGeometry /></mesh>;\n", "a.tsx", false, H), None);
+ assert_eq!(stamp("const a = <div>;\n", "a.tsx", false, H), None);
+ }
+
+ #[test]
+ fn columns_count_as_typescript_does() {
+ let out = stamp("const a = \"é😀\"; const b = <p>x</p>;\n", "a.tsx", false, H).unwrap();
+ // é is 1 unit, 😀 is 2: the <p> is at UTF-16 column 28
+ assert!(out.contains(r#"data-raea-loc="a.tsx:1:28""#), "{out}");
+ }
+
+ #[test]
+ fn only_the_edits_own_files() {
+ assert_eq!(own(Path::new("/w/src/lib/card.tsx"), Path::new("/w/src")).as_deref(), Some("lib/card.tsx"));
+ assert_eq!(own(Path::new("/w/src/node_modules/x/a.tsx"), Path::new("/w/src")), None);
+ assert_eq!(own(Path::new("/w/motion/index.tsx"), Path::new("/w/src")), None);
+ }
+}
diff --git a/engine/bundle/tests/bundle.rs b/engine/bundle/tests/bundle.rs
index 8180c74c..a837bd34 100644
--- a/engine/bundle/tests/bundle.rs
+++ b/engine/bundle/tests/bundle.rs
@@ -130,7 +130,7 @@ import { version } from "react";
aliases: [("~lib".to_string(), d.join("lib/aliased.ts"))].into_iter().collect(),
stubs: vec!["fs".into()],
};
- let o = raea_bundle::Options { z3: false, script: Some(script.clone()), defines: vec![("FLAG".into(), "\"ON\"".into())], edit: None };
+ let o = raea_bundle::Options { z3: false, script: Some(script.clone()), defines: vec![("FLAG".into(), "\"ON\"".into())], edit: None, stamps: false };
let b = raea_bundle::bundle_with(&d.join("work/entry.ts"), o).expect("bundles");
for want in ["MOTION_INDEX", "MOTION_ECS", "MOTION_PRELUDE", "TEST_SHIM:", "ZLIB_SHIM", "ALIASED", "ENGINE_REACT", "isn't available in raea's V8", "\"ON\""] {
assert!(b.js.contains(want), "{want} in the bundle: {}", b.js);
@@ -138,7 +138,7 @@ import { version } from "react";
assert!(!b.js.contains("OTHER_REACT"), "one React: the engine's");
// a Node built-in raea doesn't carry, not listed as a stub: the bundle fails, naming it
w("work/bad.ts", "import { createServer } from \"http\";\nconsole.log(createServer);\n");
- let e = raea_bundle::bundle_with(&d.join("work/bad.ts"), raea_bundle::Options { z3: false, script: Some(script), defines: vec![], edit: None }).expect_err("http isn't carried");
+ let e = raea_bundle::bundle_with(&d.join("work/bad.ts"), raea_bundle::Options { z3: false, script: Some(script), defines: vec![], edit: None, stamps: false }).expect_err("http isn't carried");
assert!(e.contains("\"http\" is a Node built-in") && e.contains("--node-stub http"), "{e}");
let _ = std::fs::remove_dir_all(&d);
}