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.

HTML: a page whose script throws before any timeline registers fails to open, so raea render exits non-zero with the script's error and the unregistered composition, instead of writing its first frame held for the whole video; data-no-timeline on the root keeps a still page with a failing script rendering. A script's error message is printed beside its stack, which JavaScriptCore's leaves out (closes #15)

shinyobjectzb7c4052f309c
diff --git a/sdk/ui/src/html/page/load.ts b/sdk/ui/src/html/page/load.ts
index 5da21e9f..6797e865 100644
--- a/sdk/ui/src/html/page/load.ts
+++ b/sdk/ui/src/html/page/load.ts
@@ -61,7 +61,11 @@ async function load() {
// the registry a page's scripts put their timelines in (window.__timelines["main"] = tl), made before they run as
// the original runtime makes it: without it every such script threw at that line and the page never moved
g.__timelines ??= {};
- for (let i = 0; i < compiled.scripts.length; i++) await runScript(compiled.scripts[i], i);
+ const threw: string[] = [];
+ for (let i = 0; i < compiled.scripts.length; i++) {
+ const e = await runScript(compiled.scripts[i], i);
+ if (e) threw.push(e);
+ }
try {
document.dispatchEvent(new g.Event("DOMContentLoaded", { bubbles: true }));
g.dispatchEvent(new g.Event("load"));
@@ -71,6 +75,12 @@ async function load() {
const comps = Array.from(document.querySelectorAll("[data-composition-id]"));
const root = comps.find((e) => e.getAttribute("data-root") === "true") ?? comps[0] ?? null;
setupTime(root, compiled.aliases);
+ // a script threw and no timeline registered: the video would be the first frame held, so the open fails (as the
+ // original runtime's render does); a page still on purpose says so with data-no-timeline
+ if (threw.length && !S.rootTl && !S.nested.length && !root?.hasAttribute("data-no-timeline")) {
+ const id = root?.getAttribute("data-composition-id");
+ throw new Error(`${threw.join("; ")}, and no timeline registered for the composition ${id ?? "(none)"} (window.__timelines${id ? `[${JSON.stringify(id)}]` : ""}), so nothing would move; a page that is still on purpose marks its root data-no-timeline`);
+ }
await seek(0);
const w = parseFloat(root?.getAttribute("data-width") ?? "") || g.innerWidth || 1920;
const h = parseFloat(root?.getAttribute("data-height") ?? "") || g.innerHeight || 1080;
@@ -103,6 +113,7 @@ wire.on<{ t: number }>("html.seek", ({ t }) => {
});
S.queue = S.queue.then(load).catch((e) => {
- warn(`load: ${(e as Error)?.stack ?? e}`);
- wire.emit("html.ready", { error: String(e), warnings });
+ // the message on its own: JavaScriptCore's stack leaves it out
+ warn(`load: ${e}\n${(e as Error)?.stack ?? ""}`);
+ wire.emit("html.ready", { error: (e as Error)?.message ?? String(e), warnings });
});
diff --git a/sdk/ui/src/html/page/scripts.ts b/sdk/ui/src/html/page/scripts.ts
index ce131155..a0efb81f 100644
--- a/sdk/ui/src/html/page/scripts.ts
+++ b/sdk/ui/src/html/page/scripts.ts
@@ -70,27 +70,36 @@ export function namedAccess() {
}
}
-export async function runScript(s: Script, i: number) {
+/** Runs one page script; what it threw (its message, which JavaScriptCore's stack leaves out), if it threw. */
+export async function runScript(s: Script, i: number): Promise<string | undefined> {
let code = s.text;
let origin = `inline #${i}`;
if (s.src) {
origin = s.src;
const got = await read(isRemote(s.src) ? abs(s.src) : pagePath(s.src));
- if (got?.text === undefined) return warn(`script ${s.src} not loaded`);
+ if (got?.text === undefined) {
+ warn(`script ${s.src} not loaded`);
+ return;
+ }
code = got.text;
}
if (!code.trim()) return;
+ let threw: string | undefined;
try {
if (s.scope !== undefined) {
g.__hfScope = scope;
(0, eval)(`(function(){var __s=__hfScope(${JSON.stringify(s.scope)});(function(document,gsap){\n${code}\n}).call(window,__s.document,__s.gsap);})();`);
} else (0, eval)(code);
} catch (e) {
- warn(`script ${origin} threw: ${(e as Error)?.stack ?? e}`);
+ const stack = (e as Error)?.stack ?? "";
+ threw = `script ${origin} threw: ${e}`;
+ // V8's stack starts with the message, JavaScriptCore's doesn't
+ warn(stack.includes(String(e)) ? `script ${origin} threw: ${stack}` : stack ? `${threw}\n${stack}` : threw);
}
// GSAP's ticker on the page clock, its jumps taken whole
if (g.gsap?.ticker && !g.__hfLag) {
g.__hfLag = true;
g.gsap.ticker.lagSmoothing(0);
}
+ return threw;
}
diff --git a/sdk/ui/test/html-script-error.test.ts b/sdk/ui/test/html-script-error.test.ts
new file mode 100644
index 00000000..61a962f6
--- /dev/null
+++ b/sdk/ui/test/html-script-error.test.ts
@@ -0,0 +1,49 @@
+// An HTML page whose script throws before its timeline registers fails to open (issue #15): rendered, it would be
+// its first frame held for the whole duration, with exit 0. Each case loads on the stand-in host through
+// html-run.ts in its own process (src/html.ts keeps the page in module state).
+import { describe, expect, test } from "bun:test";
+import { mkdtempSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+
+// a timeline as GSAP's looks to the runtime: a length and a seek
+const TL = "{ totalDuration: () => 2, totalTime() {} }";
+
+async function ready(script: string, rootAttrs = ""): Promise<any> {
+ const dir = mkdtempSync(join(tmpdir(), "raea-html-throw-"));
+ writeFileSync(
+ join(dir, "index.html"),
+ `<!doctype html><html><body><div data-composition-id="main" data-start="0" data-duration="2" data-width="640" data-height="360"${rootAttrs}><div id="box"></div></div><script>${script}</script></body></html>`,
+ );
+ const run = Bun.spawn(["bun", join(import.meta.dir, "html-run.ts"), dir], { stdout: "pipe", stderr: "pipe" });
+ const out = await new Response(run.stdout).text();
+ await run.exited;
+ // html-run prints html.ready's JSON first, then the host's tree
+ return JSON.parse(out.slice(0, out.indexOf("\n}") + 2));
+}
+
+describe("a page script that throws (#15)", () => {
+ test("before its timeline registers: the open fails, naming the error and the composition", async () => {
+ const r = await ready(`throw new Error("REPRO: setup failed"); window.__timelines.main = ${TL};`);
+ expect(r.error).toContain("REPRO: setup failed");
+ expect(r.error).toContain("no timeline registered for the composition main");
+ });
+
+ test("after its timeline registers: the page opens", async () => {
+ const r = await ready(`window.__timelines.main = ${TL}; throw new Error("late");`);
+ expect(r.error).toBeUndefined();
+ expect(r.duration).toBe(2);
+ expect(r.warnings.join("\n")).toContain("late");
+ });
+
+ test("a page with no timeline and no error opens (still on purpose)", async () => {
+ const r = await ready(`document.getElementById("box").textContent = "hi";`);
+ expect(r.error).toBeUndefined();
+ });
+
+ test("data-no-timeline: a throw is a warning only", async () => {
+ const r = await ready(`throw new Error("ignored");`, ` data-no-timeline`);
+ expect(r.error).toBeUndefined();
+ expect(r.warnings.join("\n")).toContain("ignored");
+ });
+});