2121'use strict' ;
2222
2323const {
24+ ArrayPrototypeIndexOf,
25+ ArrayPrototypeJoin,
26+ ArrayPrototypePush,
27+ ArrayPrototypeShift,
28+ ArrayPrototypeSlice,
2429 Error,
2530 ErrorCaptureStackTrace,
31+ FunctionPrototypeBind,
32+ NumberIsNaN,
2633 ObjectAssign,
2734 ObjectIs,
2835 ObjectKeys,
2936 ObjectPrototypeIsPrototypeOf,
30- Map,
31- NumberIsNaN,
37+ ReflectApply,
3238 RegExpPrototypeTest,
39+ SafeMap,
3340 String,
41+ StringPrototypeCharCodeAt,
42+ StringPrototypeIncludes,
43+ StringPrototypeIndexOf,
44+ StringPrototypeReplace,
45+ StringPrototypeSlice,
46+ StringPrototypeSplit,
3447} = primordials ;
3548
3649const { Buffer } = require ( 'buffer' ) ;
@@ -52,7 +65,7 @@ const { EOL } = require('internal/constants');
5265const { NativeModule } = require ( 'internal/bootstrap/loaders' ) ;
5366const { isError } = require ( 'internal/util' ) ;
5467
55- const errorCache = new Map ( ) ;
68+ const errorCache = new SafeMap ( ) ;
5669const CallTracker = require ( 'internal/assert/calltracker' ) ;
5770
5871let isDeepEqual ;
@@ -81,7 +94,7 @@ const meta = [
8194'\\u001e' , '\\u001f'
8295] ;
8396
84- const escapeFn = ( str ) => meta [ str . charCodeAt ( 0 ) ] ;
97+ const escapeFn = ( str ) => meta [ StringPrototypeCharCodeAt ( str , 0 ) ] ;
8598
8699let warned = false ;
87100
@@ -240,7 +253,7 @@ function parseCode(code, offset) {
240253classFields ,
241254staticClassFeatures
242255) ;
243- parseExpressionAt = Parser . parseExpressionAt . bind ( Parser ) ;
256+ parseExpressionAt = FunctionPrototypeBind ( Parser . parseExpressionAt , Parser ) ;
244257}
245258let node ;
246259let start = 0 ;
@@ -265,8 +278,9 @@ function parseCode(code, offset) {
265278
266279return [
267280node . node . start ,
268- code . slice ( node . node . start , node . node . end )
269- . replace ( escapeSequencesRegExp , escapeFn )
281+ StringPrototypeReplace ( StringPrototypeSlice ( code ,
282+ node . node . start , node . node . end ) ,
283+ escapeSequencesRegExp , escapeFn )
270284] ;
271285}
272286
@@ -330,23 +344,24 @@ function getErrMessage(message, fn) {
330344decoder . end ( ) ;
331345} else {
332346for ( let i = 0 ; i < line ; i ++ ) {
333- code = code . slice ( code . indexOf ( '\n' ) + 1 ) ;
347+ code = StringPrototypeSlice ( code ,
348+ StringPrototypeIndexOf ( code , '\n' ) + 1 ) ;
334349}
335350[ column , message ] = parseCode ( code , column ) ;
336351}
337352// Always normalize indentation, otherwise the message could look weird.
338- if ( message . includes ( '\n' ) ) {
353+ if ( StringPrototypeIncludes ( message , '\n' ) ) {
339354if ( EOL === '\r\n' ) {
340- message = message . replace ( / \r \n / g, '\n' ) ;
355+ message = StringPrototypeReplace ( message , / \r \n / g, '\n' ) ;
341356}
342- const frames = message . split ( '\n' ) ;
343- message = frames . shift ( ) ;
357+ const frames = StringPrototypeSplit ( message , '\n' ) ;
358+ message = ArrayPrototypeShift ( frames ) ;
344359for ( const frame of frames ) {
345360let pos = 0 ;
346361while ( pos < column && ( frame [ pos ] === ' ' || frame [ pos ] === '\t' ) ) {
347362pos ++ ;
348363}
349- message += `\n ${ frame . slice ( pos ) } ` ;
364+ message += `\n ${ StringPrototypeSlice ( frame , pos ) } ` ;
350365}
351366}
352367message = `The expression evaluated to a falsy value:\n\n ${ message } \n` ;
@@ -670,7 +685,7 @@ function expectedException(actual, expected, message, fn) {
670685// Special handle errors to make sure the name and the message are
671686// compared as well.
672687if ( expected instanceof Error ) {
673- keys . push ( 'name' , 'message' ) ;
688+ ArrayPrototypePush ( keys , 'name' , 'message' ) ;
674689} else if ( keys . length === 0 ) {
675690throw new ERR_INVALID_ARG_VALUE ( 'error' ,
676691expected , 'may not be an empty object' ) ;
@@ -713,7 +728,7 @@ function expectedException(actual, expected, message, fn) {
713728throwError = true ;
714729} else {
715730// Check validation functions return value.
716- const res = expected . call ( { } , actual ) ;
731+ const res = ReflectApply ( expected , { } , [ actual ] ) ;
717732if ( res !== true ) {
718733if ( ! message ) {
719734generatedMessage = true ;
@@ -858,7 +873,7 @@ function hasMatchingError(actual, expected) {
858873if ( ObjectPrototypeIsPrototypeOf ( Error , expected ) ) {
859874return false ;
860875}
861- return expected . call ( { } , actual ) === true ;
876+ return ReflectApply ( expected , { } , [ actual ] ) === true ;
862877}
863878
864879function expectsNoError ( stackStartFn , actual , error , message ) {
@@ -959,20 +974,21 @@ assert.ifError = function ifError(err) {
959974// This will remove any duplicated frames from the error frames taken
960975// from within `ifError` and add the original error frames to the newly
961976// created ones.
962- const tmp2 = origStack . split ( '\n' ) ;
963- tmp2 . shift ( ) ;
977+ const tmp2 = StringPrototypeSplit ( origStack , '\n' ) ;
978+ ArrayPrototypeShift ( tmp2 ) ;
964979// Filter all frames existing in err.stack.
965- let tmp1 = newErr . stack . split ( '\n' ) ;
980+ let tmp1 = StringPrototypeSplit ( newErr . stack , '\n' ) ;
966981for ( const errFrame of tmp2 ) {
967982// Find the first occurrence of the frame.
968- const pos = tmp1 . indexOf ( errFrame ) ;
983+ const pos = ArrayPrototypeIndexOf ( tmp1 , errFrame ) ;
969984if ( pos !== - 1 ) {
970985// Only keep new frames.
971- tmp1 = tmp1 . slice ( 0 , pos ) ;
986+ tmp1 = ArrayPrototypeSlice ( tmp1 , 0 , pos ) ;
972987break ;
973988}
974989}
975- newErr . stack = `${ tmp1 . join ( '\n' ) } \n${ tmp2 . join ( '\n' ) } ` ;
990+ newErr . stack =
991+ `${ ArrayPrototypeJoin ( tmp1 , '\n' ) } \n${ ArrayPrototypeJoin ( tmp2 , '\n' ) } ` ;
976992}
977993
978994throw newErr ;
0 commit comments