Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ option(JSRUNTIMEHOST_POLYFILL_FILE "Include JsRuntimeHost Polyfill File and File
option(JSRUNTIMEHOST_POLYFILL_PERFORMANCE "Include JsRuntimeHost Polyfill Performance." ON)
option(JSRUNTIMEHOST_POLYFILL_TEXTDECODER "Include JsRuntimeHost Polyfill TextDecoder." ON)
option(JSRUNTIMEHOST_POLYFILL_TEXTENCODER "Include JsRuntimeHost Polyfill TextEncoder." ON)
option(JSRUNTIMEHOST_POLYFILL_STREAMS "Include JsRuntimeHost Polyfill Web Streams." ON)

# Sanitizers
option(ENABLE_SANITIZERS "Enable AddressSanitizer and UBSan" OFF)
Expand Down
6 changes: 5 additions & 1 deletion Polyfills/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ endif()

if(JSRUNTIMEHOST_POLYFILL_TEXTENCODER)
add_subdirectory(TextEncoder)
endif()
endif()

if(JSRUNTIMEHOST_POLYFILL_STREAMS)
add_subdirectory(Streams)
endif()
52 changes: 52 additions & 0 deletions Polyfills/Streams/CMakeLists.txt
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})
11 changes: 11 additions & 0 deletions Polyfills/Streams/Include/Babylon/Polyfills/Streams.h
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);
}
14 changes: 14 additions & 0 deletions Polyfills/Streams/README.md
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.
53 changes: 53 additions & 0 deletions Polyfills/Streams/Source/Streams.cpp
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()));
}
Comment thread
matthargett marked this conversation as resolved.
}
}
40 changes: 40 additions & 0 deletions Polyfills/Streams/Source/StreamsScripts.h.in
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);
}
22 changes: 22 additions & 0 deletions Polyfills/Streams/ThirdParty/web-streams-polyfill/LICENSE
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.

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Tests/UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(SOURCES
"Source/Tests.NodeApi.cpp"
"Source/Tests.Scheduling.cpp"
"Source/Tests.StandardStreamLogger.cpp"
"Source/Tests.Streams.cpp"
"Source/Tests.TimeoutDispatcher.cpp")

if(APPLE)
Expand Down Expand Up @@ -95,6 +96,7 @@ target_link_libraries(UnitTests
PRIVATE Performance
PRIVATE TextDecoder
PRIVATE TextEncoder
PRIVATE Streams
${ADDITIONAL_LIBRARIES})

# See https://gitlab.kitware.com/cmake/cmake/-/issues/23543
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ add_library(UnitTestsJNI SHARED
${UNIT_TESTS_DIR}/Source/Tests.NodeApi.cpp
${UNIT_TESTS_DIR}/Source/Tests.Scheduling.cpp
${UNIT_TESTS_DIR}/Source/Tests.StandardStreamLogger.cpp
${UNIT_TESTS_DIR}/Source/Tests.Streams.cpp
${UNIT_TESTS_DIR}/Source/Tests.TimeoutDispatcher.cpp)

if(NAPI_JAVASCRIPT_ENGINE STREQUAL "V8")
Expand Down Expand Up @@ -63,4 +64,5 @@ target_link_libraries(UnitTestsJNI
PRIVATE File
PRIVATE TextDecoder
PRIVATE TextEncoder
PRIVATE Performance)
PRIVATE Performance
PRIVATE Streams)
1 change: 1 addition & 0 deletions Tests/UnitTests/Source/Scripts/tests.javaScript.all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "./tests.webSocket";
import "./tests.url";
import "./tests.urlSearchParams";
import "./tests.console";
import "./tests.streams";
import "./tests.blob";
import "./tests.nodeApi";
import "./tests.performance";
Expand Down
Loading