Skip to content

Commit b45c314

Browse files
committed
util: move TextEncoder class to C++
1 parent 4dc0d20 commit b45c314

File tree

3 files changed

+374
-203
lines changed

3 files changed

+374
-203
lines changed

lib/internal/encoding.js

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,16 @@ const {
4646
const {
4747
isAnyArrayBuffer,
4848
isArrayBufferView,
49-
isUint8Array,
5049
} = require('internal/util/types');
5150

5251
const {
53-
validateString,
5452
validateObject,
5553
kValidateObjectAllowObjectsAndNull,
5654
} = require('internal/validators');
5755

5856
const { hasIntl } = internalBinding('config');
5957
const binding = internalBinding('encoding_binding');
60-
const {
61-
encodeInto,
62-
encodeIntoResults,
63-
encodeUtf8String,
64-
decodeUTF8,
65-
} = binding;
58+
const { decodeUTF8, TextEncoder } = binding;
6659

6760
function validateDecoder(obj) {
6861
if (obj == null || obj[kDecoder] !== true)
@@ -336,61 +329,6 @@ function getEncodingFromLabel(label) {
336329
return encodings.get(trimAsciiWhitespace(label.toLowerCase()));
337330
}
338331

339-
let lazyInspect;
340-
341-
class TextEncoder {
342-
#encoding = 'utf-8';
343-
344-
#encode(input) {
345-
return encodeUtf8String(`${input}`);
346-
}
347-
348-
#encodeInto(input, dest) {
349-
encodeInto(input, dest);
350-
// We need to read from the binding here since the buffer gets refreshed
351-
// from the snapshot.
352-
const { 0: read, 1: written } = encodeIntoResults;
353-
return { read, written };
354-
}
355-
356-
get encoding() {
357-
return this.#encoding;
358-
}
359-
360-
encode(input = '') {
361-
return this.#encode(input);
362-
}
363-
364-
encodeInto(src, dest) {
365-
validateString(src, 'src');
366-
if (!dest || !isUint8Array(dest))
367-
throw new ERR_INVALID_ARG_TYPE('dest', 'Uint8Array', dest);
368-
369-
return this.#encodeInto(src, dest);
370-
}
371-
372-
[inspect](depth, opts) {
373-
if (typeof depth === 'number' && depth < 0)
374-
return this;
375-
const ctor = getConstructorOf(this);
376-
const obj = { __proto__: {
377-
constructor: ctor === null ? TextEncoder : ctor,
378-
} };
379-
obj.encoding = this.#encoding;
380-
// Lazy to avoid circular dependency
381-
lazyInspect ??= require('internal/util/inspect').inspect;
382-
return lazyInspect(obj, opts);
383-
}
384-
}
385-
386-
ObjectDefineProperties(
387-
TextEncoder.prototype, {
388-
'encode': kEnumerableProperty,
389-
'encodeInto': kEnumerableProperty,
390-
'encoding': kEnumerableProperty,
391-
[SymbolToStringTag]: { __proto__: null, configurable: true, value: 'TextEncoder' },
392-
});
393-
394332
function parseInput(input) {
395333
if (isAnyArrayBuffer(input)) {
396334
try {

0 commit comments

Comments
 (0)