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.

Plugins in a sequential render: a component held for its frame lands in a fed render too; an import written in a string stays text

shinyobjectz20ff5785b6b5
diff --git a/runtime/bundle/src/rewrite.rs b/runtime/bundle/src/rewrite.rs
index 798920fd..76344349 100644
--- a/runtime/bundle/src/rewrite.rs
+++ b/runtime/bundle/src/rewrite.rs
@@ -32,8 +32,13 @@ pub fn rewrite_into(src: &str, from_dir: &Path, motion_src: &Path, to_motion: &s
let re = regex::Regex::new(r#"(?s)\b(?:(import)\s+([^;'"]*?)|(export)\s+((?:\*|\{|type\s*\{)[^;'"]*?))\s+from\s+(["'])([^"']+)["']"#).unwrap();
let mut out = String::new();
let mut last = 0;
+ // only real statements: an import written inside a string, a template or a comment (a code chip's text) is text
+ let text = text_spans(src);
for c in re.captures_iter(src) {
let whole = c.get(0).unwrap();
+ if text.iter().any(|&(a, b)| a <= whole.start() && whole.start() < b) {
+ continue;
+ }
let kw = c.get(1).or(c.get(3)).unwrap().as_str();
let what = c.get(2).or(c.get(4)).unwrap().as_str().trim();
let spec = c.get(6).unwrap().as_str();
@@ -142,3 +147,69 @@ pub fn normalize(p: &Path) -> PathBuf {
}
out
}
+
+/// The byte ranges of `src` that are text, not code: string literals, a template's text (not its `${}`), comments and
+/// regular expression literals. A lexer's pass, enough to tell an import statement from one written in a string.
+pub(crate) fn text_spans(src: &str) -> Vec<(usize, usize)> {
+ let b = src.as_bytes();
+ let (mut spans, mut i, mut braces) = (Vec::new(), 0, Vec::<usize>::new());
+ // whether a `/` here starts a regular expression: after an operator or opening punctuation, or at the start
+ let regex_ok = |i: usize| b[..i].iter().rev().find(|c| !c.is_ascii_whitespace()).is_none_or(|c| b"(,=:[!&|?{};+-*%<>~^".contains(c));
+ // a template's text from `at` up to its end or a `${`; returns where the code resumes
+ let template = |mut i: usize, spans: &mut Vec<(usize, usize)>| -> (usize, bool) {
+ let at = i;
+ while i < b.len() && b[i] != b'`' && !(b[i] == b'$' && b.get(i + 1) == Some(&b'{')) {
+ i += if b[i] == b'\\' { 2 } else { 1 };
+ }
+ spans.push((at, i.min(b.len())));
+ if i < b.len() && b[i] == b'$' { (i + 2, true) } else { (i + 1, false) }
+ };
+ while i < b.len() {
+ match b[i] {
+ b'\'' | b'"' => {
+ let (q, at) = (b[i], i);
+ i += 1;
+ while i < b.len() && b[i] != q && b[i] != b'\n' {
+ i += if b[i] == b'\\' { 2 } else { 1 };
+ }
+ i += 1;
+ spans.push((at, i.min(b.len())));
+ }
+ b'`' => {
+ let (next, open) = template(i + 1, &mut spans);
+ if open { braces.push(0) }
+ i = next;
+ }
+ b'{' if !braces.is_empty() => { *braces.last_mut().unwrap() += 1; i += 1; }
+ b'}' if braces.last() == Some(&0) => {
+ braces.pop();
+ let (next, open) = template(i + 1, &mut spans);
+ if open { braces.push(0) }
+ i = next;
+ }
+ b'}' if !braces.is_empty() => { *braces.last_mut().unwrap() -= 1; i += 1; }
+ b'/' if b.get(i + 1) == Some(&b'/') => {
+ let at = i;
+ while i < b.len() && b[i] != b'\n' { i += 1 }
+ spans.push((at, i));
+ }
+ b'/' if b.get(i + 1) == Some(&b'*') => {
+ let at = i;
+ i = src[i + 2..].find("*/").map_or(b.len(), |e| i + 2 + e + 2);
+ spans.push((at, i));
+ }
+ b'/' if regex_ok(i) => {
+ let (at, mut class) = (i, false);
+ i += 1;
+ while i < b.len() && b[i] != b'\n' && (class || b[i] != b'/') {
+ if b[i] == b'[' { class = true } else if b[i] == b']' { class = false }
+ i += if b[i] == b'\\' { 2 } else { 1 };
+ }
+ i += 1;
+ spans.push((at, i.min(b.len())));
+ }
+ _ => i += 1,
+ }
+ }
+ spans
+}
diff --git a/runtime/bundle/src/tests.rs b/runtime/bundle/src/tests.rs
index f0abf842..be589420 100644
--- a/runtime/bundle/src/tests.rs
+++ b/runtime/bundle/src/tests.rs
@@ -19,6 +19,19 @@ fn rewrite_into_points_imports_and_re_exports_at_the_copies() {
assert!(out.contains("export function f() { return \"from\"; }"));
}
+#[test]
+fn an_import_written_in_a_string_template_or_comment_is_left_as_text() {
+ // a code chip's text (a launch film types `import { RigidBody } from "raea/plugins/avian"`) isn't an import
+ let m = &raea_io_home::home().join("sdk/raea/src");
+ let src = "import { a } from \"raea\";\nconst chip = `import { b } from \"raea/clock\"; ${ {x: `import c from \"raea\"`}.x }`;\n// import d from \"raea\";\n/* import e from \"raea\"; */\nconst s = 'import f from \"raea\"';\nconst r = /import g from \"raea\"/;\nimport { h } from \"raea/clock\";\n";
+ let out = rewrite_into(src, Path::new("/nowhere/app"), m, "../motion", None).unwrap();
+ assert!(out.starts_with("import { a } from \"../motion/prelude\""), "{out}");
+ assert!(out.ends_with("import { h } from \"../app/clock\";\n"), "{out}");
+ for kept in ["`import { b } from \"raea/clock\"; ${", "`import c from \"raea\"`", "// import d from \"raea\";", "/* import e from \"raea\"; */", "'import f from \"raea\"'", "/import g from \"raea\"/"] {
+ assert!(out.contains(kept), "{kept} rewritten in {out}");
+ }
+}
+
#[test]
fn point_sdks_climbs_from_a_nested_file() {
let src = "import { useClockState } from \"@raea/app/clock\";\nimport { x } from \"@raea/nope\";\n";
diff --git a/runtime/plugins/src/lib.rs b/runtime/plugins/src/lib.rs
index cfbc7533..95149294 100644
--- a/runtime/plugins/src/lib.rs
+++ b/runtime/plugins/src/lib.rs
@@ -226,11 +226,10 @@ pub fn arm(world: &mut World) {
/// Keyed rows whose frame has come, set on the update that draws it (so a plugin's steps after it see them): with a
/// plugin, on the frame its clock steps to; without one, on the frame drawn, whichever way it was reached.
+/// A live engine (a fed render's, live play's) takes them too: its frames' rows set the values that change
+/// (apply_live_rows), but a row held for its `from` never changes after the load, so nothing else sets it (a render to
+/// MP4 left a rigid body from frame 36 asleep for good). Each lands once, on its frame.
fn keyed_rows(world: &mut World) {
- // live play sets its rows from the game's state each frame (apply_live_rows), not from a timeline
- if world.get_resource::<FrameRows>().is_some_and(|r| r.live.is_some()) {
- return;
- }
let f = match world.get_resource::<PluginClock>() {
Some(c) if !c.advancing => return,
Some(c) => c.stepped,
diff --git a/runtime/plugins/src/tests.rs b/runtime/plugins/src/tests.rs
index 093ff09f..5b2e2d50 100644
--- a/runtime/plugins/src/tests.rs
+++ b/runtime/plugins/src/tests.rs
@@ -88,6 +88,28 @@ fn keyed_components_land_on_their_frame_forwards_backwards_and_jumping() {
}
}
+/// A live engine's rows (a fed render to MP4) still take a row held for its frame: the frames' rows only carry values
+/// that change, and a `from` row never does, so a rigid body from frame 36 stayed asleep
+#[test]
+fn a_live_engine_takes_a_row_held_for_its_frame() {
+ let mut app = app();
+ let w = app.world_mut();
+ let a = w.spawn_empty().id();
+ let ty = "bevy_transform::components::transform::Transform".to_string();
+ w.insert_resource(Keyed { rows: vec![(Some(a), ty, json!({"translation": [7.0, 0.0, 0.0]}), 3)], ..default() });
+ w.insert_resource(Error(None));
+ let mut fed = FrameRows::default();
+ fed.live = Some(vec![]);
+ w.insert_resource(fed);
+ let x = |w: &World| w.get::<Transform>(a).map(|t| t.translation.x);
+ w.insert_resource(Clock(2));
+ keyed_rows(w);
+ assert_eq!(x(w), None, "not before its frame");
+ w.insert_resource(Clock(3));
+ keyed_rows(w);
+ assert_eq!(x(w), Some(7.0), "on its frame, in a live engine too");
+}
+
/// A `material:` row sets only the fields it gives on the part's own copy of its material (#38): it took the whole
/// StandardMaterial from them, every other field at its default, so a patch of emissive_exposure_weight blacked out the
/// emissive and whitened the base, and a render's frames after the first drew the glowing cube grey