Skip to content

Commit fee12a7

Browse files
ChALkeRmertcanaltin
andcommitted
src: move all 1-byte encodings to native
Co-authored-by: Mert Can Altin <mertgold60@gmail.com>
1 parent 77e8d44 commit fee12a7

File tree

10 files changed

+542
-160
lines changed

10 files changed

+542
-160
lines changed

lib/internal/encoding.js

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
// https://encoding.spec.whatwg.org
55

66
const {
7+
ArrayPrototypeMap,
78
Boolean,
89
ObjectDefineProperties,
910
ObjectGetOwnPropertyDescriptors,
1011
ObjectSetPrototypeOf,
1112
ObjectValues,
13+
SafeArrayIterator,
1214
SafeMap,
1315
StringPrototypeSlice,
1416
Symbol,
@@ -32,8 +34,6 @@ const kFatal = Symbol('kFatal');
3234
const kUTF8FastPath = Symbol('kUTF8FastPath');
3335
const kIgnoreBOM = Symbol('kIgnoreBOM');
3436

35-
const { isSinglebyteEncoding, createSinglebyteDecoder } = require('internal/encoding/single-byte');
36-
3737
const {
3838
getConstructorOf,
3939
customInspectSymbol: inspect,
@@ -58,6 +58,7 @@ const {
5858
encodeIntoResults,
5959
encodeUtf8String,
6060
decodeUTF8,
61+
decodeSingleByte,
6162
} = binding;
6263

6364
function validateDecoder(obj) {
@@ -71,6 +72,47 @@ const CONVERTER_FLAGS_IGNORE_BOM = 0x4;
7172

7273
const empty = new FastBuffer();
7374

75+
// Has to be synced with src/
76+
const encodingsSinglebyte = new SafeMap(new SafeArrayIterator(ArrayPrototypeMap([
77+
'ibm866',
78+
'koi8-r',
79+
'koi8-u',
80+
'macintosh',
81+
'x-mac-cyrillic',
82+
'iso-8859-2',
83+
'iso-8859-3',
84+
'iso-8859-4',
85+
'iso-8859-5',
86+
'iso-8859-6',
87+
'iso-8859-7',
88+
'iso-8859-8',
89+
'iso-8859-8-i',
90+
'iso-8859-10',
91+
'iso-8859-13',
92+
'iso-8859-14',
93+
'iso-8859-15',
94+
'iso-8859-16',
95+
'windows-874',
96+
'windows-1250',
97+
'windows-1251',
98+
'windows-1252',
99+
'windows-1253',
100+
'windows-1254',
101+
'windows-1255',
102+
'windows-1256',
103+
'windows-1257',
104+
'windows-1258',
105+
'x-user-defined', // Has to be last, special case
106+
], (e, i) => [e, i])));
107+
108+
const isSinglebyteEncoding = (enc) => encodingsSinglebyte.has(enc);
109+
110+
function createSinglebyteDecoder(encoding, fatal) {
111+
const key = encodingsSinglebyte.get(encoding);
112+
if (key === undefined) throw new ERR_ENCODING_NOT_SUPPORTED(encoding);
113+
return (buf) => decodeSingleByte(buf, key, fatal);
114+
}
115+
74116
const encodings = new SafeMap([
75117
['unicode-1-1-utf-8', 'utf-8'],
76118
['unicode11utf8', 'utf-8'],
@@ -462,7 +504,7 @@ function makeTextDecoderICU() {
462504
validateDecoder(this);
463505
validateObject(options, 'options', kValidateObjectAllowObjectsAndNull);
464506

465-
if (this[kMethod]) return this[kMethod](parseInput(input));
507+
if (this[kMethod]) return this[kMethod](input);
466508

467509
this[kUTF8FastPath] &&= !(options?.stream);
468510

@@ -532,11 +574,12 @@ function makeTextDecoderJS() {
532574

533575
decode(input = empty, options = kEmptyObject) {
534576
validateDecoder(this);
535-
input = parseInput(input);
536577
validateObject(options, 'options', kValidateObjectAllowObjectsAndNull);
537578

538579
if (this[kMethod]) return this[kMethod](input);
539580

581+
input = parseInput(input);
582+
540583
if (this[kFlags] & CONVERTER_FLAGS_FLUSH) {
541584
this[kBOMSeen] = false;
542585
}

lib/internal/encoding/single-byte.js

Lines changed: 0 additions & 155 deletions
This file was deleted.

node.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
'src/debug_utils.cc',
8989
'src/embedded_data.cc',
9090
'src/encoding_binding.cc',
91+
'src/encoding_singlebyte.cc',
9192
'src/env.cc',
9293
'src/fs_event_wrap.cc',
9394
'src/handle_wrap.cc',
@@ -219,6 +220,7 @@
219220
'src/debug_utils-inl.h',
220221
'src/embedded_data.h',
221222
'src/encoding_binding.h',
223+
'src/encoding_singlebyte.h',
222224
'src/env_properties.h',
223225
'src/env.h',
224226
'src/env-inl.h',

0 commit comments

Comments
 (0)