Skip to content

Use native fetch() polyfill from JsRuntimeHost in Playground#1707

Draft
bkaradzic-microsoft wants to merge 1 commit into
BabylonJS:masterfrom
bkaradzic-microsoft:weekend/tpc-1581-fetch-shim
Draft

Use native fetch() polyfill from JsRuntimeHost in Playground#1707
bkaradzic-microsoft wants to merge 1 commit into
BabylonJS:masterfrom
bkaradzic-microsoft:weekend/tpc-1581-fetch-shim

Conversation

@bkaradzic-microsoft
Copy link
Copy Markdown
Contributor

@bkaradzic-microsoft bkaradzic-microsoft commented May 18, 2026

Wires Babylon Native''s Playground up to the native fetch() polyfill that now lives in JsRuntimeHost (BabylonJS/JsRuntimeHost#188), so playgrounds depending on fetch stop hitting ReferenceError: ''fetch'' is not defined.

This supersedes the original JS XMLHttpRequest-based shim approach: rather than shipping a fetch_polyfill.js script, fetch is now provided natively (built on the same UrlLib transport as XMLHttpRequest), matching how File/FileReader landed in #1706.

Changes:

  • Apps/Playground/Shared/AppContext.cpp - calls Babylon::Polyfills::Fetch::Initialize(env) alongside the other polyfills (right after XMLHttpRequest).
  • Apps/Playground/CMakeLists.txt - links the Fetch target; drops fetch_polyfill.js from the SCRIPTS list.
  • Removes Apps/Playground/Scripts/fetch_polyfill.js and its script-loader entry.
  • CMakeLists.txt - bumps the JsRuntimeHost pin to pick up the native polyfill.

⚠️ Temporary pin: the JsRuntimeHost dependency currently points at the #188 branch (bkaradzic-microsoft/JsRuntimeHost@1118799). This must be switched back to a BabylonJS/JsRuntimeHost commit once #188 merges. Do not merge this PR before #188 lands and the pin is repointed.

Verified: Playground target configures, builds and links cleanly (Win32/Chakra) against the native polyfill.


Landing context

This PR is one of the splits from the proven CI-green combined preview in draft PR #1702 (see #1702 for the full intended end-state).

The original split also included #1709 (ES2020+ -> ES2019 syntax-repair polyfill for Chakra), closed in favour of investigating @babel/standalone properly (#1711).

Landing order / status

Tier 1 - parallel-reviewable, no source conflicts:

  1. Fix ExternalTexture_OpenGL throw-stubs to avoid MSVC C4702 under /WX #1703 - ExternalTexture C4702 build fix ✅ merged
  2. Document accurate root cause for post-#1695 pixel-diff fallouts #1704 - config.json reason rewrites (5 entries) ✅ merged
  3. Document accurate root cause for 17 subtle pixel-diff tests #1705 - config.json reason rewrites (17 entries) ✅ merged

Tier 2 - sequential, each touches Apps/Playground/CMakeLists.txt + AppContext.cpp:

  1. Bump JsRuntimeHost for File/FileReader polyfill (re-enables 19 GLTF/OBJ tests) #1706 - File/FileReader polyfill ✅ merged (native JsRuntimeHost bump)
  2. Use native fetch() polyfill from JsRuntimeHost in Playground #1707 - native fetch polyfill (this PR) - blocked on JsRuntimeHost#188 merging + pin repoint
  3. Playground: link AbortController + TextEncoder polyfills from JsRuntimeHost #1708 - link AbortController + TextEncoder polyfills from JsRuntimeHost
  4. Add cubemap auto-expand polyfill for Playground (re-enables 7 PBR tests) #1710 - Cubemap auto-expand polyfill (loaded after babylon.max.js)

Reference policy reminder

Reference PNGs come from Babylon.js; never re-baked by BN. Combined diff: 0 PNGs.

Copy link
Copy Markdown
Member

@ryantrem ryantrem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar question as the other PR - should we just add a full native fetch polyfill? Maybe we could add it to XMLHttpRequest (@bghgary might say no though 😅), or a new polyfill. It would be good to have this in BN anyway so the BJS codebase can use fetch rather than XMLHttpRequest.

Copy link
Copy Markdown
Contributor

@bghgary bghgary left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Reviewed by Copilot on behalf of @bghgary]

Same as #1700 and #1706fetch is a standard web API that Babylon.js code paths depend on. Should be a proper C++ polyfill under Polyfills/, not a Playground-only JS script. See BabylonJS/JsRuntimeHost#98.

bkaradzic-microsoft added a commit that referenced this pull request May 21, 2026
Per-test PIL-composite triage of all 17 ``subtle pixel-diff`` tests.
None of them are deterministic-cosmetic (no re-bakes possible - all show
real visible regressions).

Updates the `reason` field in `Apps/Playground/Scripts/config.json` for
17 tests with accurate symptom descriptions, classifying into recurring
root-cause clusters:

- 9 GUI green->red color regressions (idx 160, 174, 175, 196, 197, 370,
402, 566, 587)
- 4 OpenPBR analytic-lights right-column red rendering (580, 584, 587,
592)
- 1 instanced billboard foliage red (169)
- 1 LineEdgesRenderer extra red lines (179)
- 1 Background blur red splotches (602)
- 1 Clip planes GUI sliders red (182)
- 1 Instanced Bones edge-AA (256, borderline)

**No source changes, no test re-enables, no PNGs.** Metadata-only
correction so the issue tracker reflects actionable root causes.
---

## Landing context

This PR is one of **7 splits** from the proven CI-green combined preview
in **draft PR #1702** (see
[#1702](#1702) for the
full intended end-state and verified CI run
[26044922430](https://github.com/BabylonJS/BabylonNative/actions/runs/26044922430)).

> Note: the original split included an 8th PR (#1709, ES2020+ -> ES2019
syntax-repair polyfill for Chakra). It was closed in favour of
investigating `@babel/standalone` properly (#1711).

### Recommended landing order

**Tier 1 - parallel-reviewable, no source conflicts:**
1. #1703 - ExternalTexture C4702 build fix
2. #1704 - config.json `reason` rewrites (5 entries)
3. #1705 - config.json `reason` rewrites (17 entries)

**Tier 2 - sequential, each touches `Apps/Playground/CMakeLists.txt`
SCRIPTS list + `Apps/Playground/Shared/AppContext.cpp` LoadScript order;
rebase the next branch after the previous merges:**

4. #1706 - File/Blob/FileReader polyfill (largest test impact: 19
re-enables)
5. #1707 - fetch polyfill
6. #1708 - DOM globals + native AbortController + Android CMake link
7. #1710 - Cubemap auto-expand polyfill (loaded after babylon.max.js)

### Reference policy reminder

Reference PNGs across all 7 PRs come from Babylon.js; never re-baked by
BN. Combined diff: **0 PNGs**.
bkaradzic-microsoft added a commit that referenced this pull request May 21, 2026
Per-test triage of 5 post-#1695 pixel-diff fallouts. Updates the
`reason` field in `Apps/Playground/Scripts/config.json` for 3 entries to
name the real BN rendering regression instead of generic "Pixel
comparison fails":

- idx 363 SSR2 - SSR not rendering wet floor.
- idx 369 Sprites Pixel Perfect - sprite alpha-blending broken.
- idx 395 soft-transparent-shadows - soft-shadow filter precision
degraded.

**No source changes, no test re-enables, no PNGs.** Metadata-only
correction so the issue tracker reflects actionable root causes for
follow-up engineering work.
---

## Landing context

This PR is one of **7 splits** from the proven CI-green combined preview
in **draft PR #1702** (see
[#1702](#1702) for the
full intended end-state and verified CI run
[26044922430](https://github.com/BabylonJS/BabylonNative/actions/runs/26044922430)).

> Note: the original split included an 8th PR (#1709, ES2020+ -> ES2019
syntax-repair polyfill for Chakra). It was closed in favour of
investigating `@babel/standalone` properly (#1711).

### Recommended landing order

**Tier 1 - parallel-reviewable, no source conflicts:**
1. #1703 - ExternalTexture C4702 build fix
2. #1704 - config.json `reason` rewrites (5 entries)
3. #1705 - config.json `reason` rewrites (17 entries)

**Tier 2 - sequential, each touches `Apps/Playground/CMakeLists.txt`
SCRIPTS list + `Apps/Playground/Shared/AppContext.cpp` LoadScript order;
rebase the next branch after the previous merges:**

4. #1706 - File/Blob/FileReader polyfill (largest test impact: 19
re-enables)
5. #1707 - fetch polyfill
6. #1708 - DOM globals + native AbortController + Android CMake link
7. #1710 - Cubemap auto-expand polyfill (loaded after babylon.max.js)

### Reference policy reminder

Reference PNGs across all 7 PRs come from Babylon.js; never re-baked by
BN. Combined diff: **0 PNGs**.
bkaradzic-microsoft added a commit that referenced this pull request May 28, 2026
…1703)

Fixes MSVC C4702 (unreachable code) under `/WX` in
`Plugins/ExternalTexture/Source/ExternalTexture_OpenGL.cpp` so the
OpenGL TU builds cleanly without the project-wide `/wd4702` workaround.

## What changed

`Plugins/ExternalTexture/Source/ExternalTexture_OpenGL.cpp` keeps the
unimplemented `Impl` stubs as `throw std::runtime_error{"not
implemented"}` (so callers get an unambiguous diagnostic matching every
other backend's unimplemented path) and wraps the `#include
"ExternalTexture_Shared.h"` line in `#pragma warning(push)/(disable:
4702)/(pop)`. The shared dispatchers instantiated by that include
unconditionally call the stubs, so MSVC's flow analysis flags the
dispatcher's post-call code as unreachable; the pragma suppresses C4702
only for the dispatcher code instantiated in this translation unit. Any
other code in this file is still subject to `/WX` C4702 enforcement.

`Plugins/ExternalTexture/CMakeLists.txt` - drop the `/wd4702` target
compile option, which previously silenced C4702 across the whole target.

## Alternatives considered

- **`[[noreturn]]` on the stubs**: MSVC propagates "never returns"
through the shared dispatcher, which flags *more* post-call statements
as unreachable. Tried and rejected (it made the warning worse).
- **TU-wide `/wd4702` via `target_compile_options`**: silences any
future legitimate C4702 elsewhere in the same TU, defeating `/WX`. The
current localized `#pragma warning(push/pop)` block keeps the rest of
the TU under `/WX` enforcement.
- **Replacing the throws with inert return-default stubs**: changes
product behaviour (callers would silently receive a null texture instead
of a clear "not implemented" error) to work around a compiler warning.
Rejected on principle.

## Verified

Clean under Debug + Release + RelWithDebInfo on OpenGL (`win32-gl`,
`GRAPHICS_API=OpenGLWindowsDevOnly`) and D3D11 (`win32`).

---

## Landing context

This PR is one of **7 splits** from the proven CI-green combined preview
in **draft PR #1702** (see
[#1702](#1702) for the
full intended end-state and verified CI run
[26044922430](https://github.com/BabylonJS/BabylonNative/actions/runs/26044922430)).

> Note: the original split included an 8th PR (#1709, ES2020+ -> ES2019
syntax-repair polyfill for Chakra). It was closed in favour of
investigating `@babel/standalone` properly (#1711).

### Recommended landing order

**Tier 1 - parallel-reviewable, no source conflicts:**
1. #1703 - ExternalTexture C4702 build fix
2. #1704 - config.json `reason` rewrites (5 entries)
3. #1705 - config.json `reason` rewrites (17 entries)

**Tier 2 - sequential, each touches `Apps/Playground/CMakeLists.txt`
SCRIPTS list + `Apps/Playground/Shared/AppContext.cpp` LoadScript order;
rebase the next branch after the previous merges:**

4. #1706 - File/Blob/FileReader polyfill (largest test impact: 19
re-enables)
5. #1707 - fetch polyfill
6. #1708 - DOM globals + native AbortController + Android CMake link
7. #1710 - Cubemap auto-expand polyfill (loaded after babylon.max.js)

### Reference policy reminder

Reference PNGs across all 7 PRs come from Babylon.js; never re-baked by
BN. Combined diff: **0 PNGs**.

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bkaradzic-microsoft
Copy link
Copy Markdown
Contributor Author

Thanks @bghgary @ryantrem — agreed. Same pivot pattern as #1706/#1708: this will move to a native polyfill under JsRuntimeHost/Polyfills/Fetch/ (mirroring the XMLHttpRequest layout), closing the long-standing JsRuntimeHost#98.

Plan:

  1. Land the in-flight pivots first to avoid stacking yet another review on the same reviewer:
  2. Then open JsRuntimeHost/Polyfills/Fetch/ (likely wrapping the existing XHR transport so libcurl/WinHTTP behavior stays identical).
  3. Replace this PR with a small BN bump that initializes Polyfills::Fetch in AppRuntime.

Parking this PR until step 2 lands; will convert to draft. The current JS-shim diff is superseded and not worth re-reviewing.

@bkaradzic-microsoft bkaradzic-microsoft marked this pull request as draft June 1, 2026 23:57
bkaradzic-microsoft added a commit that referenced this pull request Jun 5, 2026
…BJ tests) (#1706)

Re-enables the 19 GLTF/OBJ serializer round-trip tests that fail under
BN today because the runtime is missing `File` and `FileReader`.
Babylon.js code paths (`new File([blob], 'scene.glb')` followed by
`LoadAssetContainerAsync(file, scene)`, which internally reads via
`FileReader.readAsArrayBuffer`) need both APIs to work.

The actual polyfill **lives in JsRuntimeHost**
([bkaradzic-microsoft/JsRuntimeHost @
670084e5](bkaradzic-microsoft/JsRuntimeHost@670084e)),
next to `Polyfills/Blob/`. That's the architecturally correct home —
`File` and `FileReader` are standard WHATWG web APIs in the same
category as the existing `XMLHttpRequest` / `URL` / `WebSocket` /
`TextDecoder` / `AbortController` polyfills hosted there, and `File` is
layered directly on top of `Blob`. Hosting them there means every
JsRuntimeHost consumer (BN and any other embedder) gets them.

> Addresses [@bghgary's
review](#1706 (review)):
_"this should be a proper C++ polyfill under `Polyfills/` so every
consumer gets it, not a Playground-only JS script"_.

**This PR (BN-side) is intentionally tiny:**
- `CMakeLists.txt` — bump JsRuntimeHost `GIT_TAG` to `670084e5` and
temporarily point `GIT_REPOSITORY` at the
[`bkaradzic-microsoft/JsRuntimeHost`](https://github.com/bkaradzic-microsoft/JsRuntimeHost)
fork until the upstream JsRuntimeHost PR is merged. Will be re-pinned to
a `BabylonJS/JsRuntimeHost` commit before this PR merges.
- `Apps/Playground/Shared/AppContext.cpp` —
`Babylon::Polyfills::File::Initialize(env)` immediately after
`Blob::Initialize(env)`. The old
`LoadScript("app:///Scripts/file_polyfill.js")` line is gone.
- `Apps/Playground/CMakeLists.txt` — drops `file_polyfill.js` from
SCRIPTS, links the new JsRuntimeHost `File` target into the Playground
binary.
- `Apps/Playground/Scripts/file_polyfill.js` — deleted (308 lines of JS
subsumed by the C++ polyfill in JsRuntimeHost).
- `Apps/Playground/Scripts/config.json` — re-enables 19 affected tests;
adds `excludedGraphicsApis: ["OpenGL"]` to 5 GLTF Serializer tests (KHR
gpu instancing + 4 Camera variants) for OpenGL-specific backend issues
unrelated to this polyfill.

**Verified:** 19/19 affected tests pass on Win32 D3D11 Release headless
without `--include-excluded` (indices 208-216, 219-226, 275-276).
Remaining 9 (in the same family) fail with separate root causes —
tracked separately.

## Landing sequence

1. ✅ Push JsRuntimeHost work to fork branch + this BN PR (← here,
validating CI against fork branch).
2. ⏳ Open PR to `BabylonJS/JsRuntimeHost` adding `Polyfills/File/` once
BN CI is green here.
3. ⏳ After the JsRuntimeHost PR merges, re-pin this BN PR's
`GIT_REPOSITORY` / `GIT_TAG` back to `BabylonJS/JsRuntimeHost` at the
merged SHA.
4. ⏳ Merge this BN PR.

---

## Series landing context

This PR is one of **7 splits** from the proven CI-green combined preview
in **draft PR #1702** (see
[#1702](#1702) for the
full intended end-state and verified CI run
[26044922430](https://github.com/BabylonJS/BabylonNative/actions/runs/26044922430)).

> Note: the original split included an 8th PR (#1709, ES2020+ -> ES2019
syntax-repair polyfill for Chakra). It was closed in favour of
investigating `@babel/standalone` properly (#1711).

### Recommended landing order

**Tier 1 - parallel-reviewable, no source conflicts:**
1. #1703 - ExternalTexture C4702 build fix
2. #1704 - config.json `reason` rewrites (5 entries)
3. #1705 - config.json `reason` rewrites (17 entries)

**Tier 2 - sequential, each touches `Apps/Playground/CMakeLists.txt`
SCRIPTS list + `Apps/Playground/Shared/AppContext.cpp` LoadScript order;
rebase the next branch after the previous merges:**

4. #1706 - File/FileReader polyfill (JsRuntimeHost bump + Playground
wiring; largest test impact: 19 re-enables)
5. #1707 - fetch polyfill
6. #1708 - DOM globals + native AbortController + Android CMake link
7. #1710 - Cubemap auto-expand polyfill (loaded after babylon.max.js)

### Reference policy reminder

Reference PNGs across all 7 PRs come from Babylon.js; never re-baked by
BN. Combined diff: **0 PNGs**.

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Branimir Karadzic <branimirkaradzic@gmail.com>
@bkaradzic-microsoft bkaradzic-microsoft force-pushed the weekend/tpc-1581-fetch-shim branch 2 times, most recently from b32bf39 to c2d8675 Compare June 6, 2026 01:13
@bkaradzic-microsoft bkaradzic-microsoft changed the title Add fetch() polyfill over XMLHttpRequest for Playground Use native fetch() polyfill from JsRuntimeHost in Playground Jun 6, 2026
Replaces the JS XMLHttpRequest-based fetch shim with the native fetch()
polyfill that landed in JsRuntimeHost (PR BabylonJS#188). AppContext now calls
Babylon::Polyfills::Fetch::Initialize(env) alongside the other polyfills,
and Playground links the Fetch target.

Removes Apps/Playground/Scripts/fetch_polyfill.js and its SCRIPTS / script
loader entries. Bumps the JsRuntimeHost pin to pick up the native polyfill.

NOTE: the JsRuntimeHost pin temporarily points at the PR BabylonJS#188 branch
(bkaradzic-microsoft/JsRuntimeHost@1118799); switch it back to a
BabylonJS/JsRuntimeHost commit once BabylonJS#188 merges.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bkaradzic-microsoft bkaradzic-microsoft force-pushed the weekend/tpc-1581-fetch-shim branch from c2d8675 to 14afc73 Compare June 6, 2026 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants