-
Notifications
You must be signed in to change notification settings - Fork 22
Add TextDecoder polyfill with tests #138
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
944fd6e
Add TextDecoder polyfill with tests
CedricGuillemet 39595fd
missing dependency
CedricGuillemet 20d450b
android cmake
CedricGuillemet 0918386
Apply suggestions from code review
CedricGuillemet 845ecb1
PR feedback
CedricGuillemet 8720711
Merge branch 'TextDecoder' of https://github.com/CedricGuillemet/JsRu…
CedricGuillemet c91af6d
missing class name
CedricGuillemet 9985420
removed tests
CedricGuillemet a074873
feedback
CedricGuillemet 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,15 @@ | ||
| set(SOURCES | ||
| "Include/Babylon/Polyfills/TextDecoder.h" | ||
| "Source/TextDecoder.cpp") | ||
|
|
||
| add_library(TextDecoder ${SOURCES}) | ||
| warnings_as_errors(TextDecoder) | ||
|
|
||
| target_include_directories(TextDecoder PUBLIC "Include") | ||
|
|
||
| target_link_libraries(TextDecoder | ||
| PUBLIC napi | ||
| PUBLIC Foundation) | ||
|
|
||
| set_property(TARGET TextDecoder PROPERTY FOLDER Polyfills) | ||
| source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES}) |
9 changes: 9 additions & 0 deletions
9
Polyfills/TextDecoder/Include/Babylon/Polyfills/TextDecoder.h
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,9 @@ | ||
| #pragma once | ||
|
|
||
| #include <napi/env.h> | ||
| #include <Babylon/Api.h> | ||
|
|
||
| namespace Babylon::Polyfills::TextDecoder | ||
| { | ||
| 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,39 @@ | ||
| # TextDecoder Polyfill | ||
|
|
||
| A C++ implementation of the [WHATWG Encoding API](https://encoding.spec.whatwg.org/) `TextDecoder` interface for use in Babylon Native JavaScript runtimes via [Napi](https://github.com/nodejs/node-addon-api). | ||
|
|
||
| ## Current State | ||
|
|
||
| ### Supported | ||
|
|
||
| - Decoding `Uint8Array`, `Int8Array`, and other typed array views from a UTF-8 encoded byte sequence. | ||
| - Decoding raw `ArrayBuffer` objects. | ||
| - Constructing `TextDecoder` with no argument (defaults to `utf-8`). | ||
| - Constructing `TextDecoder` with the explicit encoding label `"utf-8"` or `"UTF-8"`. | ||
| - Calling `decode()` with no argument or `undefined` returns an empty string (matches the Web API). | ||
|
|
||
| ### Not Supported | ||
|
|
||
| - Encodings other than UTF-8 — passing any other label (e.g. `"utf-16"`, `"iso-8859-1"`) throws a JavaScript `Error`. | ||
| - `DataView` is not accepted by `decode()` — due to missing `Napi::DataView` support in the underlying JSI layer. | ||
| - Passing a non-BufferSource value (e.g. a string or number) to `decode()` throws a `TypeError`. | ||
| - The `fatal` option: decoding errors are not detected and do not throw a `TypeError`. | ||
| - The `ignoreBOM` option: the byte order mark is not stripped. | ||
| - Streaming decode (passing `{ stream: true }` to `decode()`) — each call is stateless. | ||
| - The `encoding` property on the `TextDecoder` instance is not exposed. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| const decoder = new TextDecoder(); // utf-8 | ||
| const decoder = new TextDecoder("utf-8"); // explicit, also fine | ||
|
|
||
| const bytes = new Uint8Array([72, 101, 108, 108, 111]); | ||
| decoder.decode(bytes); // "Hello" | ||
| ``` | ||
|
|
||
| Passing an unsupported encoding throws: | ||
|
|
||
| ```javascript | ||
| new TextDecoder("utf-16"); // Error: TextDecoder: unsupported encoding 'utf-16', only 'utf-8' is supported | ||
| ``` |
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,94 @@ | ||
| #include <Babylon/Polyfills/TextDecoder.h> | ||
|
|
||
| #include <napi/napi.h> | ||
| #include <cstring> | ||
| #include <string> | ||
|
|
||
| namespace | ||
| { | ||
| class TextDecoder final : public Napi::ObjectWrap<TextDecoder> | ||
| { | ||
| public: | ||
| static void Initialize(Napi::Env env) | ||
| { | ||
| Napi::HandleScope scope{env}; | ||
|
|
||
| static constexpr auto JS_TEXTDECODER_CONSTRUCTOR_NAME = "TextDecoder"; | ||
| if (env.Global().Get(JS_TEXTDECODER_CONSTRUCTOR_NAME).IsUndefined()) | ||
| { | ||
| Napi::Function func = DefineClass( | ||
| env, | ||
| JS_TEXTDECODER_CONSTRUCTOR_NAME, | ||
| { | ||
| InstanceMethod("decode", &TextDecoder::Decode), | ||
| }); | ||
|
|
||
| env.Global().Set(JS_TEXTDECODER_CONSTRUCTOR_NAME, func); | ||
| } | ||
| } | ||
CedricGuillemet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| explicit TextDecoder(const Napi::CallbackInfo& info) | ||
| : Napi::ObjectWrap<TextDecoder>{info} | ||
| { | ||
| if (info.Length() > 0 && info[0].IsString()) | ||
| { | ||
| auto encoding = info[0].As<Napi::String>().Utf8Value(); | ||
| if (encoding != "utf-8" && encoding != "UTF-8") | ||
| { | ||
| Napi::Error::New(info.Env(), "TextDecoder: unsupported encoding '" + encoding + "', only 'utf-8' is supported") | ||
| .ThrowAsJavaScriptException(); | ||
bghgary marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
CedricGuillemet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private: | ||
| Napi::Value Decode(const Napi::CallbackInfo& info) | ||
| { | ||
| if (info.Length() < 1 || info[0].IsUndefined()) | ||
| { | ||
| return Napi::String::New(info.Env(), ""); | ||
| } | ||
|
|
||
| std::string data; | ||
|
|
||
| if (info[0].IsTypedArray()) | ||
| { | ||
| auto typedArray = info[0].As<Napi::TypedArray>(); | ||
| auto arrayBuffer = typedArray.ArrayBuffer(); | ||
| auto byteOffset = typedArray.ByteOffset(); | ||
| auto byteLength = typedArray.ByteLength(); | ||
| data.resize(byteLength); | ||
| if (byteLength > 0) | ||
| { | ||
| std::memcpy(data.data(), static_cast<uint8_t*>(arrayBuffer.Data()) + byteOffset, byteLength); | ||
| } | ||
| } | ||
| else if (info[0].IsArrayBuffer()) | ||
| { | ||
| auto arrayBuffer = info[0].As<Napi::ArrayBuffer>(); | ||
| auto byteLength = arrayBuffer.ByteLength(); | ||
| data.resize(byteLength); | ||
| if (byteLength > 0) | ||
| { | ||
| std::memcpy(data.data(), arrayBuffer.Data(), byteLength); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Napi::TypeError::New(info.Env(), "TextDecoder.decode: input must be a BufferSource (ArrayBuffer or TypedArray)") | ||
| .ThrowAsJavaScriptException(); | ||
| return info.Env().Undefined(); | ||
| } | ||
|
|
||
| return Napi::String::New(info.Env(), data); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| namespace Babylon::Polyfills::TextDecoder | ||
| { | ||
| void BABYLON_API Initialize(Napi::Env env) | ||
| { | ||
| ::TextDecoder::Initialize(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
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
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.