A C++ implementation of the WHATWG Encoding API TextEncoder interface for use in Babylon Native JavaScript runtimes via Napi.
This is the encoding counterpart to the TextDecoder polyfill in this same repository. Both polyfills exist primarily for older Chakra-based runtimes where the WHATWG Encoding Standard globals are not built in. On modern V8 / JSC / Hermes runtimes the constructor is already exposed and Initialize() is a no-op.
- Constructing
TextEncoderwith no argument (UTF-8 is the only encoding the spec mandates). - The
encodingaccessor (always returns"utf-8"). encode(input)— returns aUint8Arraycontaining the UTF-8 bytes ofinput. Calling with no argument orundefinedreturns an emptyUint8Array(matches the spec, which defaultsinputto"").
- Encodings other than UTF-8 — the
TextEncoderconstructor in the spec only accepts UTF-8 anyway, so this is not a deviation. encodeInto(source, destination)— not implemented. Babylon.js does not call this entry point; the (substantially more involved) UTF-16-code-unit accounting it would require is not justified by any current consumer. If a future consumer needs it, it can be added at that time.
const encoder = new TextEncoder();
encoder.encoding; // "utf-8"
encoder.encode("Hello"); // Uint8Array(5) [72,101,108,108,111]
encoder.encode(); // Uint8Array(0) []