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

browser: canvas getContext() returns null instead of being undefined

<canvas> elements now have getContext (null for every context type, as
in a browser with 2D/WebGL disabled), toDataURL, toBlob and
transferControlToOffscreen.  MapLibre on maps.nebulon.de used to die
with "TypeError: not a function" in _setupPainter; it now takes its
own "Failed to initialize WebGL" path.
Johannes Zellner 15 часов назад
Родитель
Сommit
5848ef6639
4 измененных файлов с 25 добавлено и 1 удалено
  1. 4 1
      AGENTS.md
  2. 0 0
      docs/userland.html
  3. 10 0
      user/apps/browser/js_browser.c
  4. 11 0
      user/apps/browser/js_prelude.js

+ 4 - 1
AGENTS.md

@@ -3253,7 +3253,10 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     an inert document (`.call(parsed, "body")`) answer for that document:
     an inert document (`.call(parsed, "body")`) answer for that document:
     DOMPurify used to get the page's real `<body>` and empty it.
     DOMPurify used to get the page's real `<body>` and empty it.
     Also missing: `defer`/`async` ordering,
     Also missing: `defer`/`async` ordering,
-    `document.implementation.createDocument`, canvas/2D contexts, user-state
+    `document.implementation.createDocument`, canvas/2D/WebGL contexts
+    (`<canvas>.getContext()` returns `null` for every type, like a browser
+    with them disabled, so MapLibre on maps.nebulon.de reports "Failed to
+    initialize WebGL" instead of a TypeError), user-state
     pseudo-classes (`:hover`, `:focus`, ...), `:nth-child(... of S)` and
     pseudo-classes (`:hover`, `:focus`, ...), `:nth-child(... of S)` and
     pseudo-elements other than `::before`/`::after`; generated content is
     pseudo-elements other than `::before`/`::after`; generated content is
     text, or an empty icon box with a background colour and a `data:`
     text, or an empty icon box with a background colour and a `data:`

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
docs/userland.html


+ 10 - 0
user/apps/browser/js_browser.c

@@ -2207,6 +2207,16 @@ static int node_has(js_State *state, void *p, const char *name)
 			return 1;
 			return 1;
 		}
 		}
 	}
 	}
+	/* <canvas>: getContext() and friends from the prelude
+	 * (__nb_canvas); there is no drawing backend. */
+	if (n->type == BR_NODE_ELEM && n->tag == BR_TAG_CANVAS &&
+	    (!strcmp(name, "getContext") || !strcmp(name, "toDataURL") ||
+	     !strcmp(name, "toBlob") ||
+	     !strcmp(name, "transferControlToOffscreen"))) {
+		js_getglobal(state, "__nb_canvas");
+		js_getproperty(state, -1, name);
+		return 1;
+	}
 	/* <video>/<audio> are placeholder boxes: give them the
 	/* <video>/<audio> are placeholder boxes: give them the
 	 * HTMLMediaElement methods from the prelude (__nb_media) and report a
 	 * HTMLMediaElement methods from the prelude (__nb_media) and report a
 	 * paused, never-loaded element.  The global object stays below the
 	 * paused, never-loaded element.  The global object stays below the

+ 11 - 0
user/apps/browser/js_prelude.js

@@ -1061,6 +1061,17 @@ domClass("HTMLAudioElement", tag("AUDIO"));
  * media elements only (so `"play" in div` stays false).  play() resolves
  * media elements only (so `"play" in div` stays false).  play() resolves
  * without playing: pages call it without a catch on scroll, and a
  * without playing: pages call it without a catch on scroll, and a
  * rejection would only add an "Uncaught" line per element. */
  * rejection would only add an "Uncaught" line per element. */
+/* <canvas> has no drawing backend: getContext() answers null for every
+ * context type, like a browser with 2D/WebGL disabled, so libraries take
+ * their "no canvas/WebGL" path (MapLibre reports "Failed to initialize
+ * WebGL") instead of calling an undefined method. */
+def("__nb_canvas", {
+	getContext: function () { return null; },
+	toDataURL: function () { return "data:,"; },
+	toBlob: function (cb) { if (typeof cb === "function") setTimeout(function () { cb(null); }, 0); },
+	transferControlToOffscreen: function () { throw new DOMException("not supported", "NotSupportedError"); }
+});
+
 def("__nb_media", {
 def("__nb_media", {
 	play: function () { return Promise.resolve(); },
 	play: function () { return Promise.resolve(); },
 	pause: function () {},
 	pause: function () {},

Некоторые файлы не были показаны из-за большого количества измененных файлов