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.

registerRoot finds the compositions of a root whose components are written inline: a hook inside component={() => ...} runs when React renders that component, not when the root is called, but the walk read the root's whole source for a hook call, so any such root was taken for content and registered one composition, "main". looksHooked now reads only the function's own body: the bodies of the arrow functions and function expressions written inside it are left out (past strings, template literals, comments and regex literals), so a hook in an inline component, a render prop or a callback isn't the root's. A root that calls a hook itself is still content, and a component in the tree that does is still not run. Four new registry tests: inline block, expression and named-function bodies; a root's own hook; a component with an inline render prop beside one with its own hook; braces, arrows and slashes in strings, a regex, a comment and a template.

shinyobjectz63e1547df49d
diff --git a/sdk/raea/conformance/registry.test.tsx b/sdk/raea/conformance/registry.test.tsx
index 8acc848c..ffd062c8 100644
--- a/sdk/raea/conformance/registry.test.tsx
+++ b/sdk/raea/conformance/registry.test.tsx
@@ -75,3 +75,50 @@ test("an edit's scale of 1 changes nothing, and one outside (0, 1] fails", () =>
expect(() => registerRoot(() => <Composition id="bad" component={Uses} width={1080} height={1920} fps={30} durationInFrames={9} edit={{ scale }} />)).toThrow(/edit.scale/);
}
});
+
+// A hook inside a component written inline (component={() => ...}) runs when React renders that component, not when
+// the root is called, so the root is still looked inside: before 11-17 its source's hook made the whole root content,
+// and every such program registered one composition, "main".
+test("a hook in an inline component doesn't hide the root's compositions", () => {
+ registerRoot(() => (
+ <>
+ <Composition id="block" component={() => { const f = useCurrentFrame(); return <>{f}</>; }} width={1080} height={1920} fps={30} durationInFrames={60} />
+ <Composition id="expr" component={() => <>{useVideoConfig().width}</>} width={1920} height={1080} fps={30} durationInFrames={90} />
+ <Composition id="fn" component={function Inline() { useCurrentFrame(); return null; }} width={720} height={720} fps={24} durationInFrames={48} />
+ </>
+ ));
+ expect(getCompositions().map((c) => c.id)).toEqual(["block", "expr", "fn"]);
+});
+
+test("a root that calls a hook itself is still content", () => {
+ registerRoot(() => { useCurrentFrame(); return <Composition id="never" component={Uses} width={1080} height={1920} fps={30} durationInFrames={60} />; });
+ expect(getCompositions().map((c) => c.id)).toEqual(["main"]);
+});
+
+test("a component in the tree with an inline render prop is expanded, and its own hook still stops the walk", () => {
+ function Shell({ children }: { children?: unknown }) { return <>{children}</>; }
+ function Clocked() { useCurrentFrame(); return <Composition id="hidden" component={Uses} width={1080} height={1920} fps={30} durationInFrames={60} />; }
+ registerRoot(() => (
+ <Shell>
+ <Composition id="shown" component={(p: { n?: number }) => <>{useCurrentFrame() + (p.n ?? 0)}</>} width={1080} height={1920} fps={30} durationInFrames={30} />
+ <Clocked />
+ </Shell>
+ ));
+ expect(getCompositions().map((c) => c.id)).toEqual(["shown"]);
+});
+
+test("braces and arrows in strings, a regex, a comment and a template don't throw the reading off", () => {
+ registerRoot(() => {
+ const label = "a { b => c ) ]";
+ const tick = `${label.length} } =>`;
+ const half = label.length / 2; // } => (
+ const braces = /[{}()]=>/g.test(label + tick + half);
+ return (
+ <>
+ <Composition id="one" component={() => { const f = useCurrentFrame(); return <>{f + (braces ? 1 : 0)}</>; }} width={1080} height={1920} fps={30} durationInFrames={60} />
+ <Composition id="two" component={() => <>{`${useVideoConfig().fps} }`}</>} width={1080} height={1920} fps={30} durationInFrames={60} />
+ </>
+ );
+ });
+ expect(getCompositions().map((c) => c.id)).toEqual(["one", "two"]);
+});
diff --git a/sdk/video/src/registry.tsx b/sdk/video/src/registry.tsx
index d72a15e5..3bd23068 100644
--- a/sdk/video/src/registry.tsx
+++ b/sdk/video/src/registry.tsx
@@ -166,11 +166,146 @@ const HOOKED: ReadonlySet<unknown> = new Set<unknown>([
// A call to a hook in a component's source: `use(` or `useSomething(` (also after a dot, as bundlers write it).
const HOOK_CALL = /\buse(?:[A-Z][\w$]*)?\s*\(/;
+const CLOSE: Record<string, string> = { "(": ")", "[": "]", "{": "}" };
+// what a `/` follows when it starts a regex literal rather than dividing
+const REGEX_AFTER = /[(,=:[!&|?{};+\-*%<>~^]/;
+const REGEX_WORD = /\b(?:return|typeof|case|do|else|in|of|new|delete|void|throw|yield|await)$/;
+
+/** Where the string, template literal, comment or regex literal at `i` ends (the index after it); -1 when it's code. */
+function opaqueEnd(s: string, i: number): number {
+ const c = s[i];
+ if (c === '"' || c === "'") {
+ for (let j = i + 1; j < s.length; j++) {
+ if (s[j] === "\\") j++;
+ else if (s[j] === c || s[j] === "\n") return j + 1;
+ }
+ return s.length;
+ }
+ if (c === "`") {
+ for (let j = i + 1; j < s.length; j++) {
+ if (s[j] === "\\") j++;
+ else if (s[j] === "`") return j + 1;
+ else if (s[j] === "$" && s[j + 1] === "{") j = closeOf(s, j + 1) - 1;
+ }
+ return s.length;
+ }
+ if (c !== "/") return -1;
+ if (s[i + 1] === "/") {
+ const e = s.indexOf("\n", i);
+ return e < 0 ? s.length : e + 1;
+ }
+ if (s[i + 1] === "*") {
+ const e = s.indexOf("*/", i + 2);
+ return e < 0 ? s.length : e + 2;
+ }
+ let k = i - 1;
+ while (k >= 0 && /\s/.test(s[k])) k--;
+ if (k >= 0 && !REGEX_AFTER.test(s[k]) && !REGEX_WORD.test(s.slice(Math.max(0, k - 7), k + 1))) return -1; // division
+ let inClass = false;
+ for (let j = i + 1; j < s.length; j++) {
+ if (s[j] === "\\") j++;
+ else if (s[j] === "[") inClass = true;
+ else if (s[j] === "]") inClass = false;
+ else if (s[j] === "/" && !inClass) {
+ let end = j + 1;
+ while (end < s.length && /[a-z]/i.test(s[end])) end++;
+ return end;
+ }
+ else if (s[j] === "\n") return j + 1;
+ }
+ return s.length;
+}
+
+/** The index after the bracket that closes the one at `i`, past strings, comments and the like. */
+function closeOf(s: string, i: number): number {
+ let depth = 0;
+ for (let j = i; j < s.length; ) {
+ const e = opaqueEnd(s, j);
+ if (e >= 0) {
+ j = e;
+ continue;
+ }
+ if (CLOSE[s[j]]) depth++;
+ else if (s[j] === ")" || s[j] === "]" || s[j] === "}") {
+ depth--;
+ if (depth === 0) return j + 1;
+ }
+ j++;
+ }
+ return s.length;
+}
+
+/** Where an arrow function's expression body starting at `i` ends: at a `,`, `;` or closing bracket outside brackets. */
+function expressionEnd(s: string, i: number): number {
+ for (let j = i; j < s.length; ) {
+ const e = opaqueEnd(s, j);
+ if (e >= 0) {
+ j = e;
+ continue;
+ }
+ if (CLOSE[s[j]]) {
+ j = closeOf(s, j);
+ continue;
+ }
+ if (s[j] === "," || s[j] === ";" || s[j] === ")" || s[j] === "]" || s[j] === "}") return j;
+ j++;
+ }
+ return s.length;
+}
+
+/**
+ * A function's source without the bodies of the functions written inside it: a component given inline
+ * (`component={() => ...}`), a render prop or a callback runs when React or the caller runs it, not when this function
+ * is called, so a hook there isn't this function's. Each inner `=>` body and `function` body becomes empty.
+ */
+function ownSource(s: string): string {
+ // this function's own body: after its first `=>`, or inside its first `{`, outside its parameters' brackets
+ let body = -1;
+ for (let j = 0; j < s.length && body < 0; ) {
+ const e = opaqueEnd(s, j);
+ if (e >= 0) j = e;
+ else if (s.startsWith("=>", j)) body = j + 2;
+ else if (s[j] === "{") body = j + 1;
+ else if (s[j] === "(" || s[j] === "[") j = closeOf(s, j);
+ else j++;
+ }
+ if (body < 0) return s;
+ let out = s.slice(0, body);
+ for (let j = body; j < s.length; ) {
+ const e = opaqueEnd(s, j);
+ if (e >= 0) {
+ out += s.slice(j, e);
+ j = e;
+ continue;
+ }
+ if (s.startsWith("=>", j)) {
+ let k = j + 2;
+ while (k < s.length && /\s/.test(s[k])) k++;
+ out += "=>0";
+ j = s[k] === "{" ? closeOf(s, k) : expressionEnd(s, k);
+ continue;
+ }
+ if (s.startsWith("function", j) && !/[\w$]/.test(s[j - 1] ?? "") && !/[\w$]/.test(s[j + 8] ?? "")) {
+ const open = s.indexOf("(", j);
+ let k = open < 0 ? s.length : closeOf(s, open);
+ while (k < s.length && /\s/.test(s[k])) k++;
+ if (s[k] === "{") {
+ out += "function(){}";
+ j = closeOf(s, k);
+ continue;
+ }
+ }
+ out += s[j];
+ j++;
+ }
+ return out;
+}
+
function looksHooked(fn: Function): boolean {
if (HOOKED.has(fn)) return true;
let source = "";
try {
- source = Function.prototype.toString.call(fn);
+ source = ownSource(Function.prototype.toString.call(fn));
} catch {
return true; // can't read it: don't risk running it
}