Selaa lähdekoodia

Improve MuJS compatibility and defer browser scripts

Run classic scripts sloppy by default like browsers do; strict mode is
opt-in via a "use strict" directive.  This removes a common source of
jQuery/Bootstrap failures.

Teach the lexer which ')' closes an if/for/while/with/switch/catch head,
so a regex literal can start the body statement (`for (...) /re/.test()`),
which makes showdown.min.js parse.  The regex engine now follows Annex B
for identity escapes (`\_`, `\a`, ...) and braces that are not valid
quantifiers (`{}`, `{,n}`), fixing Angular's regex compilation errors.

Paint the parsed page before fetching and running scripts, so a slow
remote <script src> no longer hides the page behind the loading screen.
Cache successful remote scripts under /share/bjs-* for the current boot,
matching the stylesheet cache, so reloads and later visits skip the
download.

Verified on the host: every Cloudron script parses, all 448 regexes used
by its libraries compile, and make test-mujs / make test-libc pass.  The
demo page still runs inline and external scripts in order.  The jQuery/
Bootstrap/Angular errors that remain come from the still-minimal DOM
(createElement, querySelector, classList, events, timers).  The pack is
441580 bytes.
Johannes Zellner 1 viikko sitten
vanhempi
sitoutus
a53466697f
8 muutettua tiedostoa jossa 221 lisäystä ja 31 poistoa
  1. 14 6
      AGENTS.md
  2. 132 10
      browser.c
  3. 1 1
      docs/userland.html
  4. 3 1
      js_browser.c
  5. 7 0
      third_party/mujs/VENDOR
  6. 5 0
      third_party/mujs/jsi.h
  7. 28 4
      third_party/mujs/jslex.c
  8. 31 9
      third_party/mujs/regexp.c

+ 14 - 6
AGENTS.md

@@ -758,9 +758,14 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   before `heap_reset()`.  `js_setlimit()` caps every script at 8M
   instructions, so a runaway loop reports `script ran too long` instead of
   freezing the browser.  Math, Date and JSON come from the engine plus the
-  libc math/time layers.  There are no timers, event listeners, `innerHTML`
-  or forms yet; flex/grid, basic tables, inline SVG and CSS variables are
-  supported (see known issue 22 for the remaining CSS gaps).  `SYS_SPAWN`
+  libc math/time layers.  The parsed page is painted before scripts are
+  fetched and run, and remote `<script src>` bodies are cached under
+  `/share/bjs-*` for the current boot.  Classic scripts run sloppy by
+  default; the vendored engine also carries small Annex B parser fixes for
+  regex literals after control parentheses and identity escapes/braces.
+  There are no timers, event listeners, `innerHTML` or forms yet; flex/grid,
+  basic tables, inline SVG and CSS variables are supported (see known issue
+  22 for the remaining CSS gaps).  `SYS_SPAWN`
   loads FAT ELFs through `kmalloc` now (the old 16 KB `fat_load_buf` is gone)
   and wraps the load in `sys_fs_enter/exit`,
   since the floppy needs IRQ 6.
@@ -1307,8 +1312,8 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
    compressed pack directly to `0x900000` with INT 15h AH=87h, so the cap is
    `PACK_MAX` 1 MB (not the old ~255 KB low-gap limit).  The browser and
    its ring-3 dependencies are compiled with function sections and linked
-   with `--gc-sections`; the current pack with the UI fonts is ~430 KiB
-   (440736 bytes), leaving ~594 KiB under the cap.  `libdeflate-gzip -12` is
+   with `--gc-sections`; the current pack with the UI fonts is ~431 KiB
+   (441580 bytes), leaving ~593 KiB under the cap.  `libdeflate-gzip -12` is
    still preferred for the extra headroom, though the 1 MB cap leaves room
    for `gzip -9` as a fallback.
 9. SMP is **ACPI/MADT only**: no Intel MP-table fallback, so a Pentium II SMP
@@ -1369,7 +1374,10 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     external classic scripts, `document.getElementById(...).textContent`,
     `console.log`, `alert`, `location.href`; no timers, event listeners,
     `innerHTML` or form submission; `type="module"`, `defer` and `async` are
-    ignored).  Also no pseudo-classes/elements,
+    ignored).  The DOM bindings remain small: pages that expect
+    `document.createElement`, `querySelector(All)`, `classList`,
+    `addEventListener` or timers (jQuery/Bootstrap/Angular) log errors even
+    though their static HTML renders.  Also no pseudo-classes/elements,
     border-radius clipping of descendant content, gradients/box-shadow/
     transitions (a gradient background falls back to its last solid colour),
     or CSS `color-mix`.  CSS custom properties (`var()`), flex rows with

+ 132 - 10
browser.c

@@ -43,6 +43,9 @@
 #define CSS_CACHE_MAX      16
 #define CSS_CACHE_FILE_MAX (256u * 1024u)
 #define CSS_CACHE_PREFIX   "bcss-"
+#define SCRIPT_CACHE_MAX      16
+#define SCRIPT_CACHE_FILE_MAX (512u * 1024u)
+#define SCRIPT_CACHE_PREFIX   "bjs-"
 #define BR_MAX_SHEETS   6
 #define BR_HISTORY      16
 #define BR_URL_MAX      512
@@ -1368,6 +1371,112 @@ static int read_url(const char *url, unsigned int cap, unsigned char **out,
 	return 0;
 }
 
+/* Remote scripts are cached under /share for the current boot, like the
+ * stylesheet cache, so reloads and later visits skip the network. */
+static int script_cache_count;
+
+static unsigned int script_cache_hash(const char *s)
+{
+	unsigned int h = 2166136261u;
+
+	while (*s) {
+		h ^= (unsigned char)*s++;
+		h *= 16777619u;
+	}
+	return h;
+}
+
+static void script_cache_path(const char *url, char *out, unsigned int cap)
+{
+	unsigned int n = 0;
+
+	s_append(out, &n, "/share/" SCRIPT_CACHE_PREFIX, cap);
+	s_append_dec(out, &n, script_cache_hash(url), cap);
+}
+
+static void script_cache_clear(void)
+{
+	struct sys_dirent d;
+	char path[96];
+
+	for (;;) {
+		int i, found = 0;
+
+		for (i = 0; u_syscall3(SYS_LIST_DIR, (int)"/share", i,
+				       (int)&d) == 1; i++) {
+			unsigned int n = 0;
+
+			if (!starts_with(d.name, SCRIPT_CACHE_PREFIX))
+				continue;
+			s_append(path, &n, "/share/", sizeof(path));
+			s_append(path, &n, d.name, sizeof(path));
+			if (u_syscall1(SYS_UNLINK, (int)path) < 0)
+				return;
+			found = 1;
+			break;
+		}
+		if (!found)
+			break;
+	}
+	script_cache_count = 0;
+}
+
+static void script_cache_init(void)
+{
+	struct sys_dirent d;
+	int i, count = 0;
+
+	for (i = 0; u_syscall3(SYS_LIST_DIR, (int)"/share", i,
+			       (int)&d) == 1; i++)
+		if (starts_with(d.name, SCRIPT_CACHE_PREFIX))
+			count++;
+	if (count >= SCRIPT_CACHE_MAX) {
+		script_cache_clear();
+		return;
+	}
+	script_cache_count = count;
+}
+
+static int script_cache_get(const char *url, unsigned char **out,
+			    unsigned int *outlen)
+{
+	char path[96];
+	struct sys_stat st;
+	unsigned char *buf;
+	int rn;
+
+	script_cache_path(url, path, sizeof(path));
+	if (u_stat(path, &st) != 0 || st.is_dir || st.size == 0 ||
+	    st.size >= SCRIPT_CACHE_FILE_MAX)
+		return 0;
+	buf = (unsigned char *)malloc(st.size + 1);
+	if (!buf)
+		return 0;
+	rn = u_syscall3(SYS_READ_FILE, (int)path, (int)buf, (int)st.size);
+	if (rn != (int)st.size) {
+		free(buf);
+		return 0;
+	}
+	buf[st.size] = '\0';
+	*out = buf;
+	*outlen = st.size;
+	return 1;
+}
+
+static void script_cache_put(const char *url, const unsigned char *data,
+			     unsigned int len)
+{
+	char path[96];
+
+	if (script_cache_count >= SCRIPT_CACHE_MAX || len == 0 ||
+	    len >= SCRIPT_CACHE_FILE_MAX)
+		return;
+	script_cache_path(url, path, sizeof(path));
+	if (u_syscall3(SYS_WRITE_FILE, (int)path, (int)data,
+		       (int)len) >= 0)
+		script_cache_count++;
+}
+
 /* Fetch one external script into a heap buffer and NUL-terminate it. */
 static int fetch_script_text(const char *url, unsigned char **out,
 			     unsigned int *outlen, char *err,
@@ -1381,6 +1490,8 @@ static int fetch_script_text(const char *url, unsigned char **out,
 
 	*out = 0;
 	*outlen = 0;
+	if (url[0] != '/' && script_cache_get(url, out, outlen))
+		return 0;
 	rv = read_url(url, BR_SCRIPT_MAX, &buf, &len, &local, ctype,
 		      sizeof(ctype), final, sizeof(final), err, errcap,
 		      &truncated);
@@ -1392,6 +1503,8 @@ static int fetch_script_text(const char *url, unsigned char **out,
 		return -1;
 	}
 	buf[len] = '\0';
+	if (!local)
+		script_cache_put(url, buf, len);
 	*out = buf;
 	*outlen = len;
 	return 0;
@@ -1947,10 +2060,24 @@ static void load_page(const char *url, int local, int push)
 			return;
 		if (pending_action)
 			return;
-		run_page_scripts();
-		if (pending_action)
-			return;
 	}
+	preload_begin();
+	page_loading = 0;
+	s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
+	url_in.len = s_len(url_in.text);
+	url_in.cursor = url_in.len;
+	url_in.scroll = 0;
+	ngui_set_focus(&ui, 0);
+
+	/* Paint the parsed page before fetching/running scripts.  A slow
+	 * remote <script src> then does not hide the page behind the loading
+	 * screen; the fetch helper pumps this UI while it waits. */
+	set_status("Loading scripts...", 0, 0);
+	redraw_now();
+	run_page_scripts();
+	if (pending_action)
+		return;
+
 	post_status_text[0] = '\0';
 	post_status_error = 0;
 	post_status_ttl = 0;
@@ -1972,13 +2099,7 @@ static void load_page(const char *url, int local, int push)
 		       sizeof(post_status_text));
 		post_status_ttl = 6000;
 	}
-	preload_begin();
-	page_loading = 0;
-	s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
-	url_in.len = s_len(url_in.text);
-	url_in.cursor = url_in.len;
-	url_in.scroll = 0;
-	ngui_set_focus(&ui, 0);
+	set_status(post_status_text, post_status_error, post_status_ttl);
 	ngui_invalidate(&ui);
 }
 
@@ -4683,6 +4804,7 @@ void _start(const char *arg)
 	heap_clear();
 	origin_cache_load();
 	css_cache_init();
+	script_cache_init();
 	page_scroll.offset = 0;
 	page_scroll.content_h = 0;
 

+ 1 - 1
docs/userland.html

@@ -357,7 +357,7 @@
 		</p>
 		<ul>
 			<li><strong>HTML/CSS subset.</strong> External and inline stylesheets, tag/class/id and descendant/child selectors, colours, backgrounds, real <code>font-size</code> (8–48 px), bold/italic faces, <code>font-family: monospace</code>, margins, padding, borders, alignment, percentage widths, basic floats and <code>@media</code> blocks evaluated at the browser canvas width when the page is parsed. Text is Latin-1; there is no shaping, bidi or <code>@font-face</code> yet.</li>
-			<li><strong>JavaScript.</strong> Inline and external <code>&lt;script src&gt;</code> blocks run on the vendored MuJS engine in document order: <code>console.log</code>, <code>alert</code>, <code>document.getElementById(...).textContent</code> and <code>location.href</code>. External scripts use the same local/network fetch path as pages and CSS (512 KB cap). Math, Date and JSON are the engine's; each page gets one state, and an 8M-instruction limit turns a runaway loop into a <code>script ran too long</code> error instead of freezing the browser. Timers, event listeners, <code>innerHTML</code>, <code>type="module"</code> and <code>defer</code>/<code>async</code> semantics are not implemented yet.</li>
+			<li><strong>JavaScript.</strong> Inline and external <code>&lt;script src&gt;</code> blocks run on the vendored MuJS engine in document order: <code>console.log</code>, <code>alert</code>, <code>document.getElementById(...).textContent</code> and <code>location.href</code>. External scripts use the same local/network fetch path as pages and CSS (512 KB cap) and are cached under <code>/share/bjs-*</code> for the current boot; the parsed page is painted before scripts run. Classic scripts are sloppy by default, and the engine carries Annex B fixes for regex literals after control parentheses and identity escapes/braces. Math, Date and JSON are the engine's; each page gets one state, and an 8M-instruction limit turns a runaway loop into a <code>script ran too long</code> error instead of freezing the browser. The DOM surface is still small: <code>createElement</code>, <code>querySelector(All)</code>, <code>classList</code>, <code>addEventListener</code>, timers, <code>innerHTML</code>, <code>type="module"</code> and <code>defer</code>/<code>async</code> semantics are not implemented, so jQuery/Bootstrap/Angular pages log errors even though their static HTML renders.</li>
 			<li><strong>Not supported.</strong> Forms, pseudo-classes, border-radius clipping of descendants, gradients/box-shadow/transitions and CSS <code>color-mix</code>. Flex rows, card grids, basic tables, inline SVG and CSS variables are supported.</li>
 			<li><strong>Caps.</strong> 2 MB page body, 832 KB CSS, 6144 DOM nodes, 4608 rules, 16 images. Each image gets an <code>img_estimate()</code>-sized decode arena that is freed after the scaled bitmap is made; navigation calls <code>heap_reset()</code> and unmaps the page heap.</li>
 			<li><strong>Fetching.</strong> Each network fetch starts a one-shot helper process and a fresh TCP/TLS connection; images load one per loop within the viewport plus 160 px.</li>

+ 3 - 1
js_browser.c

@@ -215,7 +215,9 @@ int js_browser_start(struct br_doc *doc, const struct js_browser_host *h)
 	current_url = h->page_url;
 	panic_armed = 0;
 
-	J = js_newstate(0, 0, JS_STRICT);
+	/* Classic scripts are sloppy by default in browsers; strict mode is
+	 * opt-in via a "use strict" directive. */
+	J = js_newstate(0, 0, 0);
 	if (!J)
 		return -1;
 	js_setreport(J, report_cb);

+ 7 - 0
third_party/mujs/VENDOR

@@ -12,3 +12,10 @@ docs, the Unicode data files and the tools are not part of this copy.
 Build: one.c is compiled as a single object with the same freestanding flags
 as the browser (see MUJS_CFLAGS in the top-level Makefile), with
 JS_ENVLIMIT=256 and JS_ASTLIMIT=200 to bound recursion on the ring-3 stack.
+
+Local patches:
+- jslex.c/jsi.h track whether the last ')' closed an if/for/while/with/
+  switch/catch head, so a regex literal can start the body statement
+  (`for (...) /re/.test(x)`) as browsers allow.
+- regexp.c accepts Annex B identity escapes such as `\_` and `\a` instead
+  of rejecting identifier-character escapes.

+ 5 - 0
third_party/mujs/jsi.h

@@ -221,6 +221,11 @@ struct js_State
 	int lexchar;
 	int lasttoken;
 	int newline;
+	/* '(' context stack: whether each open paren followed if/for/while/
+	 * with/switch/catch, so a regex literal can start the body statement. */
+	int paren_control[64];
+	int paren_top;
+	int last_paren_control;
 
 	/* parser state */
 	int astdepth;

+ 28 - 4
third_party/mujs/jslex.c

@@ -455,8 +455,21 @@ static int lexstring(js_State *J)
 }
 
 /* the ugliest language wart ever... */
-static int isregexpcontext(int last)
+static int iscontrolparen(int token)
 {
+	return token == TK_IF || token == TK_WHILE || token == TK_FOR ||
+	       token == TK_WITH || token == TK_SWITCH || token == TK_CATCH;
+}
+
+static int isregexpcontext(js_State *J)
+{
+	int last = J->lasttoken;
+
+	/* A ')' that closed a control head starts a statement, so a regex
+	 * literal may follow it (`for (...) /re/.test(x)`). */
+	if (last == ')')
+		return J->last_paren_control;
+
 	switch (last) {
 	case ']':
 	case ')':
@@ -573,7 +586,7 @@ static int jsY_lexx(js_State *J)
 				if (lexcomment(J))
 					jsY_error(J, "multi-line comment not terminated");
 				continue;
-			} else if (isregexpcontext(J->lasttoken)) {
+			} else if (isregexpcontext(J)) {
 				return lexregexp(J);
 			} else if (jsY_accept(J, '=')) {
 				return TK_DIV_ASS;
@@ -587,8 +600,17 @@ static int jsY_lexx(js_State *J)
 		}
 
 		switch (J->lexchar) {
-		case '(': jsY_next(J); return '(';
-		case ')': jsY_next(J); return ')';
+		case '(':
+			if (J->paren_top < nelem(J->paren_control))
+				J->paren_control[J->paren_top++] =
+					iscontrolparen(J->lasttoken);
+			jsY_next(J);
+			return '(';
+		case ')':
+			J->last_paren_control = J->paren_top > 0 ?
+				J->paren_control[--J->paren_top] : 0;
+			jsY_next(J);
+			return ')';
 		case ',': jsY_next(J); return ',';
 		case ':': jsY_next(J); return ':';
 		case ';': jsY_next(J); return ';';
@@ -736,6 +758,8 @@ void jsY_initlex(js_State *J, const char *filename, const char *source)
 	J->source = source;
 	J->line = 1;
 	J->lasttoken = 0;
+	J->paren_top = 0;
+	J->last_paren_control = 0;
 	jsY_next(J); /* load first lookahead character */
 }
 

+ 31 - 9
third_party/mujs/regexp.c

@@ -116,11 +116,6 @@ static int dec(struct cstate *g, int c)
 
 #define ESCAPES "BbDdSsWw^$\\.*+?()[]{}|-0123456789"
 
-static int isunicodeletter(int c)
-{
-	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || isalpharune(c);
-}
-
 static int nextrune(struct cstate *g)
 {
 	if (!*g->source) {
@@ -171,13 +166,37 @@ static int nextrune(struct cstate *g)
 		}
 		if (strchr(ESCAPES, g->yychar))
 			return 1;
-		if (isunicodeletter(g->yychar) || g->yychar == '_') /* check identity escape */
-			die(g, "invalid escape character");
+		/* Annex B identity escape: \a, \_, ... match the character
+		 * itself, as browsers do for non-unicode regular expressions. */
 		return 0;
 	}
 	return 0;
 }
 
+/* Annex B: a '{' that does not start a well-formed quantifier is a literal
+ * brace, so /{}/ and /{,x}/ are valid patterns. */
+static int iscount(struct cstate *g)
+{
+	const char *s = g->source;
+
+	if (*s < '0' || *s > '9')
+		return 0;
+	while (*s >= '0' && *s <= '9')
+		s++;
+	if (*s == '}')
+		return 1;
+	if (*s != ',')
+		return 0;
+	s++;
+	if (*s == '}')
+		return 1;
+	if (*s < '0' || *s > '9')
+		return 0;
+	while (*s >= '0' && *s <= '9')
+		s++;
+	return *s == '}';
+}
+
 static int lexcount(struct cstate *g)
 {
 	g->yychar = *g->source++;
@@ -420,8 +439,11 @@ static int lex(struct cstate *g)
 		return g->yychar;
 	}
 
-	if (g->yychar == '{')
-		return lexcount(g);
+	if (g->yychar == '{') {
+		if (iscount(g))
+			return lexcount(g);
+		return L_CHAR;
+	}
 	if (g->yychar == '[')
 		return lexclass(g);
 	if (g->yychar == '(') {