-
Notifications
You must be signed in to change notification settings - Fork 23
Add optional WHATWG Streams polyfill #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matthargett
wants to merge
5
commits into
BabylonJS:main
Choose a base branch
from
rebeckerspecialties:web-streams-polyfill
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b5d69a8
Add optional WHATWG Streams polyfill
matthargett 9dc464a
Keep Streams implementations internally consistent
matthargett 54c2d6e
Preserve Streams bundle bytes across source splits
matthargett 9b273a4
Streams: emit the embedded bundle as <=16 KB literals; link Streams i…
matthargett 28f9deb
Merge upstream main (62818ee) and adopt the per-feature test layout (…
matthargett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| set(WEB_STREAMS_POLYFILL_FILE | ||
| "${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/web-streams-polyfill/ponyfill.es5.js") | ||
| set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS | ||
| "${WEB_STREAMS_POLYFILL_FILE}") | ||
| file(READ "${WEB_STREAMS_POLYFILL_FILE}" WEB_STREAMS_POLYFILL_SOURCE) | ||
| # MSVC caps a single string literal at 16,380 bytes (C2026) and a concatenated one at 65,535, so | ||
| # the bundle is emitted as consecutive raw-string parts of at most 16,000 bytes and joined back | ||
| # into one array at compile time. Parts abut with no separator so the split stays byte-transparent | ||
| # (a separator could land inside an identifier or string literal when the bundle is regenerated). | ||
| string(LENGTH "${WEB_STREAMS_POLYFILL_SOURCE}" _streams_length) | ||
| set(_streams_part_size 16000) | ||
| set(_streams_offset 0) | ||
| set(_streams_index 0) | ||
| set(WEB_STREAMS_POLYFILL_PARTS "") | ||
| set(WEB_STREAMS_POLYFILL_PART_NAMES "") | ||
| while(_streams_offset LESS _streams_length) | ||
| math(EXPR _streams_index "${_streams_index} + 1") | ||
| math(EXPR _streams_remaining "${_streams_length} - ${_streams_offset}") | ||
| if(_streams_remaining LESS _streams_part_size) | ||
| set(_streams_take ${_streams_remaining}) | ||
| else() | ||
| set(_streams_take ${_streams_part_size}) | ||
| endif() | ||
| string(SUBSTRING "${WEB_STREAMS_POLYFILL_SOURCE}" ${_streams_offset} ${_streams_take} _streams_part) | ||
| string(APPEND WEB_STREAMS_POLYFILL_PARTS | ||
| " inline constexpr char PonyfillPart${_streams_index}[] = R\"JSRHSTREAM(${_streams_part})JSRHSTREAM\";\n") | ||
| string(APPEND WEB_STREAMS_POLYFILL_PART_NAMES ", PonyfillPart${_streams_index}") | ||
| math(EXPR _streams_offset "${_streams_offset} + ${_streams_take}") | ||
| endwhile() | ||
| configure_file( | ||
| "Source/StreamsScripts.h.in" | ||
| "${CMAKE_CURRENT_BINARY_DIR}/Generated/StreamsScripts.h" | ||
| @ONLY) | ||
|
|
||
| set(SOURCES | ||
| "Include/Babylon/Polyfills/Streams.h" | ||
| "Source/Streams.cpp" | ||
| "Source/StreamsScripts.h.in" | ||
| "ThirdParty/web-streams-polyfill/LICENSE" | ||
| "ThirdParty/web-streams-polyfill/ponyfill.es5.js") | ||
|
|
||
| add_library(Streams ${SOURCES}) | ||
| warnings_as_errors(Streams) | ||
|
|
||
| target_include_directories(Streams | ||
| PUBLIC "Include" | ||
| PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/Generated") | ||
|
|
||
| target_link_libraries(Streams PUBLIC JsRuntime) | ||
|
|
||
| set_property(TARGET Streams PROPERTY FOLDER Polyfills) | ||
| source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| #pragma once | ||
|
|
||
| #include <Babylon/Api.h> | ||
| #include <napi/env.h> | ||
|
|
||
| namespace Babylon::Polyfills::Streams | ||
| { | ||
| // Installs the WHATWG Streams constructors that are not already supplied | ||
| // by the JavaScript engine. | ||
| void BABYLON_API Initialize(Napi::Env env); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Web Streams | ||
|
|
||
| Installs the standard `ReadableStream`, `WritableStream`, `TransformStream`, | ||
| reader, writer, controller, and queuing-strategy globals when the selected | ||
| JavaScript engine does not provide them. | ||
|
|
||
| The implementation is the ES5 ponyfill bundle from | ||
| [`web-streams-polyfill` 4.3.0](https://github.com/MattiasBuelens/web-streams-polyfill/tree/add69059ed08eae6a18559aba49575a280d1529e), | ||
| which is based on the WHATWG reference implementation. The vendored project | ||
| tests this release against the Streams portion of WPT at revision | ||
| [`c05b4473`](https://github.com/web-platform-tests/wpt/tree/c05b447326585237713013c66341eab2cdf967b6/streams). | ||
|
|
||
| `Initialize` preserves any Streams constructors already supplied by the host | ||
| engine and fills only missing globals. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #include <Babylon/Polyfills/Streams.h> | ||
|
|
||
| #include "StreamsScripts.h" | ||
|
|
||
| #include <array> | ||
| #include <string_view> | ||
|
|
||
| namespace Babylon::Polyfills::Streams | ||
| { | ||
| void BABYLON_API Initialize(Napi::Env env) | ||
| { | ||
| Napi::HandleScope scope{env}; | ||
| auto global = env.Global(); | ||
|
|
||
| static constexpr std::array<std::string_view, 13> constructorNames{ | ||
| "ReadableStream", | ||
| "ReadableStreamDefaultController", | ||
| "ReadableByteStreamController", | ||
| "ReadableStreamBYOBRequest", | ||
| "ReadableStreamDefaultReader", | ||
| "ReadableStreamBYOBReader", | ||
| "WritableStream", | ||
| "WritableStreamDefaultController", | ||
| "WritableStreamDefaultWriter", | ||
| "ByteLengthQueuingStrategy", | ||
| "CountQueuingStrategy", | ||
| "TransformStream", | ||
| "TransformStreamDefaultController", | ||
| }; | ||
|
|
||
| bool needsPonyfill{}; | ||
| for (const auto name : constructorNames) | ||
| { | ||
| const auto constructor = global.Get(name.data()); | ||
| if (constructor.IsUndefined() || constructor.IsNull()) | ||
| { | ||
| needsPonyfill = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (!needsPonyfill) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| const auto exports = Napi::Eval(env, Internal::StreamsScripts::Ponyfill.data(), "jsruntimehost://web-streams-polyfill.js").As<Napi::Object>(); | ||
| for (const auto name : constructorNames) | ||
| { | ||
| global.Set(name.data(), exports.Get(name.data())); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #pragma once | ||
|
|
||
| #include <array> | ||
| #include <cstddef> | ||
|
|
||
| namespace Babylon::Polyfills::Internal::StreamsScripts | ||
| { | ||
| // The vendored bundle is split by CMake into PonyfillPart1..N raw-string literals of at most | ||
| // 16,000 bytes each (MSVC C2026) and joined below. The prologue and epilogue wrap it as a | ||
| // CommonJS module so its exports can be read back. Nothing is inserted between parts: a | ||
| // separator could divide an identifier or string literal when the bundle is regenerated. | ||
| inline constexpr char PonyfillPrologue[] = R"JSRHSTREAM( | ||
| (function() { | ||
| var exports = {}; | ||
| var module = { exports: exports }; | ||
| )JSRHSTREAM"; | ||
|
|
||
| @WEB_STREAMS_POLYFILL_PARTS@ | ||
| inline constexpr char PonyfillEpilogue[] = R"JSRHSTREAM( | ||
| return module.exports; | ||
| })() | ||
| )JSRHSTREAM"; | ||
|
|
||
| template<std::size_t... Sizes> | ||
| consteval auto Join(const char (&... parts)[Sizes]) | ||
| { | ||
| std::array<char, (Sizes + ...) - sizeof...(Sizes) + 1> result{}; | ||
| std::size_t offset{}; | ||
| auto append = [&](const char* part, std::size_t size) { | ||
| for (std::size_t index{}; index + 1 < size; ++index) | ||
| { | ||
| result[offset++] = part[index]; | ||
| } | ||
| }; | ||
| (append(parts, Sizes), ...); | ||
| return result; | ||
| } | ||
|
|
||
| inline constexpr auto Ponyfill = Join(PonyfillPrologue@WEB_STREAMS_POLYFILL_PART_NAMES@, PonyfillEpilogue); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| The MIT License (MIT) | ||
|
|
||
| Copyright (c) 2026 Mattias Buelens | ||
| Copyright (c) 2016 Diwank Singh Tomer | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
8 changes: 8 additions & 0 deletions
8
Polyfills/Streams/ThirdParty/web-streams-polyfill/ponyfill.es5.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.