Skip to content

Commit ddbe136

Browse files
authored
util: reduce TextEncoder.encodeInto function size
PR-URL: #60339 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent b19525a commit ddbe136

2 files changed

Lines changed: 27 additions & 24 deletions

File tree

‎lib/internal/encoding.js‎

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const kHandle = Symbol('handle');
2626
constkFlags=Symbol('flags');
2727
constkEncoding=Symbol('encoding');
2828
constkDecoder=Symbol('decoder');
29-
constkEncoder=Symbol('encoder');
3029
constkFatal=Symbol('kFatal');
3130
constkUTF8FastPath=Symbol('kUTF8FastPath');
3231
constkLatin1FastPath=Symbol('kLatin1FastPath');
@@ -61,11 +60,6 @@ const {
6160

6261
const{ Buffer }=require('buffer');
6362

64-
functionvalidateEncoder(obj){
65-
if(obj==null||obj[kEncoder]!==true)
66-
thrownewERR_INVALID_THIS('TextEncoder');
67-
}
68-
6963
functionvalidateDecoder(obj){
7064
if(obj==null||obj[kDecoder]!==true)
7165
thrownewERR_INVALID_THIS('TextDecoder');
@@ -338,45 +332,50 @@ function getEncodingFromLabel(label) {
338332
returnencodings.get(trimAsciiWhitespace(label.toLowerCase()));
339333
}
340334

335+
letlazyInspect;
336+
341337
classTextEncoder{
342-
constructor(){
343-
this[kEncoder]=true;
338+
#encoding ='utf-8';
339+
340+
#encode(input){
341+
returnencodeUtf8String(`${input}`);
342+
}
343+
344+
#encodeInto(input,dest){
345+
encodeInto(input,dest);
346+
// We need to read from the binding here since the buffer gets refreshed
347+
// from the snapshot.
348+
const{0: read,1: written}=encodeIntoResults;
349+
return{ read, written };
344350
}
345351

346352
getencoding(){
347-
validateEncoder(this);
348-
return'utf-8';
353+
returnthis.#encoding;
349354
}
350355

351356
encode(input=''){
352-
validateEncoder(this);
353-
returnencodeUtf8String(`${input}`);
357+
returnthis.#encode(input);
354358
}
355359

356360
encodeInto(src,dest){
357-
validateEncoder(this);
358361
validateString(src,'src');
359362
if(!dest||!isUint8Array(dest))
360363
thrownewERR_INVALID_ARG_TYPE('dest','Uint8Array',dest);
361364

362-
encodeInto(src,dest);
363-
// We need to read from the binding here since the buffer gets refreshed
364-
// from the snapshot.
365-
const{0: read,1: written}=encodeIntoResults;
366-
return{ read, written };
365+
returnthis.#encodeInto(src,dest);
367366
}
368367

369368
[inspect](depth,opts){
370-
validateEncoder(this);
371369
if(typeofdepth==='number'&&depth<0)
372370
returnthis;
373371
constctor=getConstructorOf(this);
374372
constobj={__proto__: {
375373
constructor: ctor===null ? TextEncoder : ctor,
376374
}};
377-
obj.encoding=this.encoding;
375+
obj.encoding=this.#encoding;
378376
// Lazy to avoid circular dependency
379-
returnrequire('internal/util/inspect').inspect(obj,opts);
377+
lazyInspect??=require('internal/util/inspect').inspect;
378+
returnlazyInspect(obj,opts);
380379
}
381380
}
382381

‎test/parallel/test-whatwg-encoding-custom-interop.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ assert(TextEncoder);
4646
constinstance=newTextEncoder();
4747

4848
constexpectedError={
49-
code: 'ERR_INVALID_THIS',
5049
name: 'TypeError',
51-
message: 'Value of "this" must be of type TextEncoder'
50+
message: /fromanobjectwhoseclassdidnotdeclareit/,
5251
};
5352

5453
inspectFn.call(instance,Infinity,{});
@@ -58,7 +57,12 @@ assert(TextEncoder);
5857
constinvalidThisArgs=[{},[],true,1,'',newTextDecoder()];
5958
for(constiofinvalidThisArgs){
6059
assert.throws(()=>inspectFn.call(i,Infinity,{}),expectedError);
61-
assert.throws(()=>encodeFn.call(i),expectedError);
6260
assert.throws(()=>encodingGetter.call(i),expectedError);
6361
}
62+
for(constiofinvalidThisArgs){
63+
assert.throws(()=>encodeFn.call(i),{
64+
name: 'TypeError',
65+
message: 'Receiver must be an instance of class TextEncoder',
66+
});
67+
}
6468
}

0 commit comments

Comments
 (0)