Просмотр исходного кода

Load external scripts in the browser

browser_html.c now records the src attribute of JavaScript script
elements (still filtering non-JS types and leaving inline bodies alone).
browser.c resolves each src against the page URL and fetches it through
the same path as pages and CSS: local paths use SYS_READ_FILE, http(s)
uses the curl __fetch helper, with a 512 KB cap.  External and inline
bodies run in document order in the same MuJS state; a failed fetch is
reported and later scripts still run.

The script filename is used for error messages while location.href
stays the page URL.  A missing script logs "Cannot load script ..."
instead of aborting the page.

JS.HTM now loads JSEXT.JS first and checks that it ran before the inline
block; root.img ships JSEXT.JS.  Docs updated with the 512 KB cap and the
remaining type=module/defer/async limits.  Verified in QEMU: external
script and inline order, correct location.href, and a missing script
continuing to the inline script.  The pack is 440736 bytes.
Johannes Zellner 1 неделя назад
Родитель
Сommit
904b639a48
11 измененных файлов с 186 добавлено и 45 удалено
  1. 18 15
      AGENTS.md
  2. 2 1
      Makefile
  3. 99 18
      browser.c
  4. 53 5
      browser_html.c
  5. 2 1
      browser_html.h
  6. 1 1
      docs/graphics.html
  7. 2 2
      docs/userland.html
  8. 1 2
      js_browser.c
  9. 1 0
      js_browser.h
  10. 4 0
      testdata/browser/js.html
  11. 3 0
      testdata/browser/jsext.js

+ 18 - 15
AGENTS.md

@@ -743,20 +743,22 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   an `img_estimate()`-sized decode arena, freed as soon as the scaled bitmap
   exists; up to 16 bitmaps (512 KB files / 8 MP decodes) are kept until the
   page heap is reset.  Navigation calls `heap_reset()` and unmaps the whole
-  arena, so a large image no longer reserves a fixed 24 MB.  Inline
-  `<script>` blocks are collected by `browser_html.c` and run in document
-  order through the vendored MuJS engine in `third_party/mujs/` (linked only
-  into `browser.elf`); `js_browser.c/h` exposes `console.log`, `alert`,
+  arena, so a large image no longer reserves a fixed 24 MB.  `<script>`
+  blocks are collected by `browser_html.c`; inline bodies run directly and
+  `<script src>` bodies are fetched through the same local/network path as
+  pages and CSS (512 KB cap), in document order, through the vendored MuJS
+  engine in `third_party/mujs/` (linked only into `browser.elf`);
+  `js_browser.c/h` exposes `console.log`, `alert`,
   `document.getElementById(...).textContent` and `location.href`.  A state
   is created after `heap_start()`, shared by the page's scripts and freed
   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`,
-  external `<script src>` 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`,
+  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`
+  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.
 - **Image library + viewer**: `img.h`, `img.c`, `img_mem.c`, `img_blit.c`,
   `img_png.c`, `img_bmp.c`, `img_gif.c`, `img_jpeg.c`, `img_tga.c`,
@@ -1264,8 +1266,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 ~429 KiB
-   (439476 bytes), leaving ~595 KiB under the cap.  `libdeflate-gzip -12` is
+   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
    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
@@ -1322,10 +1324,11 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     pack is out of the kernel now, so `_end` keeps ~170 KB of headroom below
     the `0x90000` wall; the remaining limit is kernel code/buffers only.
 
-22. Browser limits: JavaScript is only the first MuJS slice (inline scripts,
-    `document.getElementById(...).textContent`, `console.log`, `alert`,
-    `location.href`; no timers, event listeners, `innerHTML`, external
-    `<script src>` or forms).  Also no forms, pseudo-classes/elements,
+22. Browser limits: JavaScript is only the first MuJS slice (inline and
+    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,
     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

+ 2 - 1
Makefile

@@ -917,7 +917,7 @@ hello.elf: hello.o
 	$(LD) -T hello.ld -nostdlib -m elf_i386 -o $@ $<
 	$(OBJCOPY) --strip-all $@ $@
 
-root.img: browser.elf gallery.elf testdata/browser/demo.html testdata/browser/js.html testdata/images/rgb.bmp
+root.img: browser.elf gallery.elf testdata/browser/demo.html testdata/browser/js.html testdata/browser/jsext.js testdata/images/rgb.bmp
 	dd if=/dev/zero of=$@ bs=512 count=2880 2>/dev/null
 	mkfs.fat -F 12 -n "ROOTDISK" $@
 	echo "Hello from the second floppy!" | mcopy -i $@ - ::HELLO.TXT
@@ -926,6 +926,7 @@ root.img: browser.elf gallery.elf testdata/browser/demo.html testdata/browser/js
 	mcopy -i $@ gallery.elf ::GALLERY
 	mcopy -i $@ testdata/browser/demo.html ::DEMO.HTM
 	mcopy -i $@ testdata/browser/js.html ::JS.HTM
+	mcopy -i $@ testdata/browser/jsext.js ::JSEXT.JS
 	mcopy -i $@ testdata/images/rgb.bmp ::PIC.BMP
 
 run: image.bin root.img

+ 99 - 18
browser.c

@@ -37,6 +37,8 @@
 #define BR_IMG_LAYOUT_W 1000
 #define BR_CSS_MAX      (832u * 1024u)
 #define BR_CSS_COMP_MAX (192u * 1024u)
+#define BR_SCRIPT_MAX   (512u * 1024u)
+#define FETCH_ABORT     (-2)
 /* Session cache for remote stylesheets (per launch; cleared at startup). */
 #define CSS_CACHE_MAX      16
 #define CSS_CACHE_FILE_MAX (256u * 1024u)
@@ -377,10 +379,29 @@ static void js_host_changed(void)
 	ngui_invalidate(&ui);
 }
 
-/* Run the page's inline <script> blocks in document order.  The state is
- * page-scoped: scripts share globals, then it is freed.  The source is
- * copied because MuJS needs NUL-terminated text and the parser's offsets
- * point into the page body. */
+static int fetch_script_text(const char *url, unsigned char **out,
+			     unsigned int *outlen, char *err,
+			     unsigned int errcap);
+
+static void script_error(const char *what, const char *url, const char *why)
+{
+	char msg[192];
+	unsigned int n = 0;
+
+	s_append(msg, &n, what, sizeof(msg));
+	if (url && url[0]) {
+		s_append(msg, &n, " ", sizeof(msg));
+		s_append(msg, &n, url, sizeof(msg));
+	}
+	if (why && why[0]) {
+		s_append(msg, &n, ": ", sizeof(msg));
+		s_append(msg, &n, why, sizeof(msg));
+	}
+	js_host_report(msg);
+}
+
+/* Run the page's scripts in document order: external <script src> bodies are
+ * fetched, inline bodies are copied because MuJS needs NUL-terminated text. */
 static void run_page_scripts(void)
 {
 	struct js_browser_host host;
@@ -392,25 +413,57 @@ static void run_page_scripts(void)
 	host.report = js_host_report;
 	host.navigate = js_host_navigate;
 	host.changed = js_host_changed;
+	host.page_url = page_url;
 	if (js_browser_start(&doc, &host) != 0) {
 		js_host_status("JavaScript: out of memory", 1);
 		return;
 	}
 	for (i = 0; i < doc.nscripts; i++) {
-		unsigned int off = doc.scripts[i].off;
-		unsigned int len = doc.scripts[i].len;
-		unsigned int k;
-		char *src = (char *)malloc(len + 1);
+		struct br_script *script = &doc.scripts[i];
 
-		if (!src) {
-			js_host_status("JavaScript: out of memory", 1);
-			break;
+		if (script->src_off) {
+			const char *src_attr = doc.attrs + script->src_off;
+			char url[BR_URL_MAX];
+			unsigned char *data;
+			unsigned int len;
+			char err[64];
+			int rv;
+
+			if (!src_attr[0])
+				continue;
+			if (resolve_link(page_url, src_attr, url,
+					 sizeof(url)) != 0) {
+				script_error("Cannot load script", src_attr,
+					     "unsupported URL");
+				continue;
+			}
+			set_status("Loading script...", 0, 0);
+			rv = fetch_script_text(url, &data, &len, err,
+					       sizeof(err));
+			if (rv == FETCH_ABORT)
+				break;
+			if (rv != 0) {
+				script_error("Cannot load script", url, err);
+				continue;
+			}
+			js_browser_run((const char *)data, url);
+			free(data);
+		} else {
+			unsigned int off = script->off;
+			unsigned int len = script->len;
+			unsigned int k;
+			char *src = (char *)malloc(len + 1);
+
+			if (!src) {
+				js_host_status("JavaScript: out of memory", 1);
+				break;
+			}
+			for (k = 0; k < len; k++)
+				src[k] = doc.script_src[off + k];
+			src[len] = '\0';
+			js_browser_run(src, page_url);
+			free(src);
 		}
-		for (k = 0; k < len; k++)
-			src[k] = doc.script_src[off + k];
-		src[len] = '\0';
-		js_browser_run(src, page_url);
-		free(src);
 		if (pending_action)
 			break;
 	}
@@ -986,7 +1039,6 @@ static int inflate_trunc(const unsigned char *src, unsigned int len,
 #define FETCH_BODY "/share/bfbody"
 #define FETCH_META "/share/bfmeta"
 #define FETCH_DONE "/share/bfdone"
-#define FETCH_ABORT (-2)
 
 struct fetch_meta {
 	int status;
@@ -1316,6 +1368,35 @@ static int read_url(const char *url, unsigned int cap, unsigned char **out,
 	return 0;
 }
 
+/* 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,
+			     unsigned int errcap)
+{
+	unsigned char *buf;
+	unsigned int len;
+	int local, truncated = 0;
+	char ctype[64], final[BR_URL_MAX];
+	int rv;
+
+	*out = 0;
+	*outlen = 0;
+	rv = read_url(url, BR_SCRIPT_MAX, &buf, &len, &local, ctype,
+		      sizeof(ctype), final, sizeof(final), err, errcap,
+		      &truncated);
+	if (rv != 0)
+		return rv;
+	if (truncated || len >= BR_SCRIPT_MAX) {
+		free(buf);
+		s_copy(err, "script is too large", errcap);
+		return -1;
+	}
+	buf[len] = '\0';
+	*out = buf;
+	*outlen = len;
+	return 0;
+}
+
 /* Fetch an image into the fixed scratch buffers.  The scaled bitmaps live on
  * the heap, so images must not allocate their file body from it as well. */
 static int fetch_image(const char *url, unsigned int cap,
@@ -1959,7 +2040,7 @@ static void load_home(int push)
 			"</ul></div>"
 			"<p>Type a URL in the bar above.  Esc closes the "
 			"window, Ctrl+L focuses the address bar.</p>"
-			"<pre>javascript: inline scripts (limited DOM)\nforms: not supported"
+			"<pre>javascript: inline + external scripts (limited DOM)\nforms: not supported"
 			"\nimages: PNG JPEG GIF BMP TGA PNM ICO</pre>"
 			"</body></html>";
 		br_set_viewport(ui.w);

+ 53 - 5
browser_html.c

@@ -4029,9 +4029,56 @@ static unsigned int skip_element(const char *html, unsigned int len,
 	return p;
 }
 
-/* Inline JavaScript only: skip structured-data/template script blocks
- * (JSON-LD, application/json, text/template, ...) and external sources,
- * which this browser does not fetch yet. */
+/* Store a <script src=...> value in the attribute arena.  Kept separate from
+ * parse_attrs() so script-only attributes cannot be confused with the
+ * value/d attributes that also use src_off on other elements. */
+static unsigned int script_src_attr(struct br_doc *d, const char *s,
+				    unsigned int len)
+{
+	unsigned int i = 0;
+
+	while (i < len) {
+		unsigned int ns, ne, vs, ve;
+		char quote = 0;
+
+		while (i < len && is_space(s[i]))
+			i++;
+		if (i >= len || s[i] == '/')
+			break;
+		ns = i;
+		while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
+			i++;
+		ne = i;
+		while (i < len && is_space(s[i]))
+			i++;
+		if (i >= len || s[i] != '=')
+			continue;
+		i++;
+		while (i < len && is_space(s[i]))
+			i++;
+		if (i < len && (s[i] == '"' || s[i] == '\'')) {
+			quote = s[i++];
+			vs = i;
+			while (i < len && s[i] != quote)
+				i++;
+			ve = i;
+			if (i < len)
+				i++;
+		} else {
+			vs = i;
+			while (i < len && !is_space(s[i]) && s[i] != '>')
+				i++;
+			ve = i;
+		}
+		if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
+			return store_attr(d, s + vs, ve - vs, 400);
+	}
+	return 0;
+}
+
+/* JavaScript only: skip structured-data/template script blocks (JSON-LD,
+ * application/json, text/template, ...) and non-JS type values.  External
+ * src scripts are allowed; the browser fetches them separately. */
 static int script_is_javascript(const char *s, unsigned int len)
 {
 	unsigned int i = 0;
@@ -4073,8 +4120,6 @@ static int script_is_javascript(const char *s, unsigned int len)
 		} else if (i == ne) {
 			i++;
 		}
-		if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
-			return 0;
 		if (ne > ns && ci_eq_n(s + ns, "type", ne - ns)) {
 			unsigned int vn = has_val ? ve - vs : 0;
 
@@ -4240,6 +4285,9 @@ int br_parse(struct br_doc *d, const char *html, unsigned int len)
 
 						script->off = i;
 						script->len = end > i ? end - i : 0;
+						script->src_off = script_src_attr(
+							d, html + attr_start,
+							attr_end - attr_start);
 					} else {
 						d->scripts_truncated = 1;
 					}

+ 2 - 1
browser_html.h

@@ -170,8 +170,9 @@ struct br_rule {
  * br_parse(); the caller must keep it alive for as long as the document (the
  * browser keeps the page body until the next navigation). */
 struct br_script {
-	unsigned int off;
+	unsigned int off;       /* inline body, or empty for src scripts */
 	unsigned int len;
+	unsigned int src_off;   /* src attribute in the attr arena, 0 if inline */
 };
 
 struct br_doc {

+ 1 - 1
docs/graphics.html

@@ -328,7 +328,7 @@
 			</div>
 			<div class="card">
 				<h3>browser</h3>
-				<p>HTML/CSS renderer with flex rows, auto-fit card grids, inline-block runs, basic tables and inline SVG diagrams. It decodes inline images and fetches through a headless helper; inline scripts run on the vendored MuJS engine, while form submission is not supported. The largest ring-3 application in the tree.</p>
+				<p>HTML/CSS renderer with flex rows, auto-fit card grids, inline-block runs, basic tables and inline SVG diagrams. It decodes inline images and fetches through a headless helper; inline and external scripts run on the vendored MuJS engine, while form submission is not supported. The largest ring-3 application in the tree.</p>
 			</div>
 		</div>
 	</section>

+ 2 - 2
docs/userland.html

@@ -350,14 +350,14 @@
 			ngui, the image library, inflate, TLS, uhttp and the vendored MuJS
 			engine. It opens at 1100×700 (960×640 and 780×540 fallbacks on
 			smaller screens), with a URL bar, history, a status bar, inline
-			images and inline scripts. Network
+			images and inline/external scripts. Network
 			work runs in a headless <code>curl __fetch</code> helper that writes
 			the body and metadata under <code>/share</code>, so the UI keeps
 			drawing and scrolling.
 		</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 <code>&lt;script&gt;</code> blocks run on the vendored MuJS engine: <code>console.log</code>, <code>alert</code>, <code>document.getElementById(...).textContent</code> and <code>location.href</code>. 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> and external <code>&lt;script src&gt;</code> 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). 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>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>

+ 1 - 2
js_browser.c

@@ -212,7 +212,7 @@ int js_browser_start(struct br_doc *doc, const struct js_browser_host *h)
 		return -1;
 	D = doc;
 	host = *h;
-	current_url = 0;
+	current_url = h->page_url;
 	panic_armed = 0;
 
 	J = js_newstate(0, 0, JS_STRICT);
@@ -255,7 +255,6 @@ int js_browser_run(const char *source, const char *url)
 	if (!J || !D)
 		return -1;
 
-	current_url = url;
 	js_setlimit(J, JS_RUN_LIMIT, 0);
 
 	panic_armed = 1;

+ 1 - 0
js_browser.h

@@ -13,6 +13,7 @@ struct js_browser_host {
 	void (*report)(const char *msg);
 	void (*navigate)(const char *url);
 	void (*changed)(void);
+	const char *page_url;   /* location.href for every script on the page */
 };
 
 /* Create a state for this document.  Returns 0 on success. */

+ 4 - 0
testdata/browser/js.html

@@ -14,11 +14,15 @@ li { margin: 6px 0; }
 <h1>MuJS demo</h1>
 <p id="out">The script has not run yet.</p>
 <ul>
+<li>External: <b id="ext">?</b></li>
 <li>2 + 3 = <b id="sum">?</b></li>
 <li>Math.sqrt(16) = <b id="sqrt">?</b></li>
 <li>Date: <span id="date">?</span></li>
 </ul>
+<script src="JSEXT.JS"></script>
 <script>
+if (typeof extLoaded === 'undefined')
+	throw new Error('external script did not run first');
 document.getElementById('sum').textContent = String(2 + 3);
 document.getElementById('sqrt').textContent = String(Math.sqrt(16));
 document.getElementById('date').textContent =

+ 3 - 0
testdata/browser/jsext.js

@@ -0,0 +1,3 @@
+var extLoaded = true;
+document.getElementById('ext').textContent = 'external script OK';
+console.log('external script: ' + location.href);