@@ -155,68 +155,156 @@ ObjectDefineProperty(Console, SymbolHasInstance, {
155155 }
156156});
157157
158- // Eager version for the Console constructor
159- Console . prototype [ kBindStreamsEager ] = function ( stdout , stderr ) {
160- ObjectDefineProperties ( this , {
161- '_stdout' : { ...consolePropAttributes , value : stdout } ,
162- '_stderr' : { ...consolePropAttributes , value : stderr }
163- } ) ;
164- } ;
158+ const kColorInspectOptions = { colors: true };
159+ const kNoColorInspectOptions = {};
165160
166- // Lazily load the stdout and stderr from an object so we don't
167- // create the stdio streams when they are not even accessed
168- Console . prototype [ kBindStreamsLazy ] = function ( object ) {
169- let stdout ;
170- let stderr ;
171- ObjectDefineProperties ( this , {
172- '_stdout' : {
173- enumerable : false ,
174- configurable : true ,
175- get ( ) {
176- if ( ! stdout ) stdout = object . stdout ;
177- return stdout ;
178- } ,
179- set ( value ) { stdout = value ; }
180- } ,
181- '_stderr' : {
182- enumerable : false ,
183- configurable : true ,
184- get ( ) {
185- if ( ! stderr ) { stderr = object . stderr ; }
186- return stderr ;
187- } ,
188- set ( value ) { stderr = value ; }
161+ ObjectDefineProperties(Console.prototype, {
162+ [kBindStreamsEager]: {
163+ ...consolePropAttributes,
164+ // Eager version for the Console constructor
165+ value: function(stdout, stderr) {
166+ ObjectDefineProperties(this, {
167+ '_stdout': { ...consolePropAttributes, value: stdout },
168+ '_stderr': { ...consolePropAttributes, value: stderr }
169+ });
189170 }
190- } ) ;
191- } ;
171+ },
172+ [kBindStreamsLazy]: {
173+ ...consolePropAttributes,
174+ // Lazily load the stdout and stderr from an object so we don't
175+ // create the stdio streams when they are not even accessed
176+ value: function(object) {
177+ let stdout;
178+ let stderr;
179+ ObjectDefineProperties(this, {
180+ '_stdout': {
181+ enumerable: false,
182+ configurable: true,
183+ get() {
184+ if (!stdout) stdout = object.stdout;
185+ return stdout;
186+ },
187+ set(value) { stdout = value; }
188+ },
189+ '_stderr': {
190+ enumerable: false,
191+ configurable: true,
192+ get() {
193+ if (!stderr) { stderr = object.stderr; }
194+ return stderr;
195+ },
196+ set(value) { stderr = value; }
197+ }
198+ });
199+ }
200+ },
201+ [kBindProperties]: {
202+ ...consolePropAttributes,
203+ value: function(ignoreErrors, colorMode, groupIndentation = 2) {
204+ ObjectDefineProperties(this, {
205+ '_stdoutErrorHandler': {
206+ ...consolePropAttributes,
207+ value: createWriteErrorHandler(this, kUseStdout)
208+ },
209+ '_stderrErrorHandler': {
210+ ...consolePropAttributes,
211+ value: createWriteErrorHandler(this, kUseStderr)
212+ },
213+ '_ignoreErrors': {
214+ ...consolePropAttributes,
215+ value: Boolean(ignoreErrors)
216+ },
217+ '_times': { ...consolePropAttributes, value: new Map() },
218+ // Corresponds to https://console.spec.whatwg.org/#count-map
219+ [kCounts]: { ...consolePropAttributes, value: new Map() },
220+ [kColorMode]: { ...consolePropAttributes, value: colorMode },
221+ [kIsConsole]: { ...consolePropAttributes, value: true },
222+ [kGroupIndent]: { ...consolePropAttributes, value: '' },
223+ [kGroupIndentationWidth]: {
224+ ...consolePropAttributes,
225+ value: groupIndentation
226+ },
227+ });
228+ }
229+ },
230+ [kWriteToConsole]: {
231+ ...consolePropAttributes,
232+ value: function(streamSymbol, string) {
233+ const ignoreErrors = this._ignoreErrors;
234+ const groupIndent = this[kGroupIndent];
235+
236+ const useStdout = streamSymbol === kUseStdout;
237+ const stream = useStdout ? this._stdout : this._stderr;
238+ const errorHandler = useStdout ?
239+ this._stdoutErrorHandler : this._stderrErrorHandler;
240+
241+ if (groupIndent.length !== 0) {
242+ if (string.includes('\n')) {
243+ string = string.replace(/\n/g, `\n${groupIndent}`);
244+ }
245+ string = groupIndent + string;
246+ }
247+ string += '\n';
248+
249+ if (ignoreErrors === false) return stream.write(string);
250+
251+ // There may be an error occurring synchronously (e.g. for files or TTYs
252+ // on POSIX systems) or asynchronously (e.g. pipes on POSIX systems), so
253+ // handle both situations.
254+ try {
255+ // Add and later remove a noop error handler to catch synchronous
256+ // errors.
257+ if (stream.listenerCount('error') === 0)
258+ stream.once('error', noop);
259+
260+ stream.write(string, errorHandler);
261+ } catch (e) {
262+ // Console is a debugging utility, so it swallowing errors is not
263+ // desirable even in edge cases such as low stack space.
264+ if (isStackOverflowError(e))
265+ throw e;
266+ // Sorry, there's no proper way to pass along the error here.
267+ } finally {
268+ stream.removeListener('error', noop);
269+ }
270+ }
271+ },
272+ [kGetInspectOptions]: {
273+ ...consolePropAttributes,
274+ value: function(stream) {
275+ let color = this[kColorMode];
276+ if (color === 'auto') {
277+ color = stream.isTTY && (
278+ typeof stream.getColorDepth === 'function' ?
279+ stream.getColorDepth() > 2 : true);
280+ }
192281
193- Console . prototype [ kBindProperties ] = function ( ignoreErrors , colorMode ,
194- groupIndentation = 2 ) {
195- ObjectDefineProperties ( this , {
196- '_stdoutErrorHandler' : {
197- ...consolePropAttributes ,
198- value : createWriteErrorHandler ( this , kUseStdout )
199- } ,
200- '_stderrErrorHandler' : {
201- ...consolePropAttributes ,
202- value : createWriteErrorHandler ( this , kUseStderr )
203- } ,
204- '_ignoreErrors' : {
205- ...consolePropAttributes ,
206- value : Boolean ( ignoreErrors )
207- } ,
208- '_times' : { ...consolePropAttributes , value : new Map ( ) } ,
209- // Corresponds to https://console.spec.whatwg.org/#count-map
210- [ kCounts ] : { ...consolePropAttributes , value : new Map ( ) } ,
211- [ kColorMode ] : { ...consolePropAttributes , value : colorMode } ,
212- [ kIsConsole ] : { ...consolePropAttributes , value : true } ,
213- [ kGroupIndent ] : { ...consolePropAttributes , value : '' } ,
214- [ kGroupIndentationWidth ] : {
215- ...consolePropAttributes ,
216- value : groupIndentation
217- } ,
218- } ) ;
219- } ;
282+ const options = optionsMap.get(this);
283+ if (options) {
284+ if (options.colors === undefined) {
285+ options.colors = color;
286+ }
287+ return options;
288+ }
289+
290+ return color ? kColorInspectOptions : kNoColorInspectOptions;
291+ }
292+ },
293+ [kFormatForStdout]: {
294+ ...consolePropAttributes,
295+ value: function(args) {
296+ const opts = this[kGetInspectOptions](this._stdout);
297+ return formatWithOptions(opts, ...args);
298+ }
299+ },
300+ [kFormatForStderr]: {
301+ ...consolePropAttributes,
302+ value: function(args) {
303+ const opts = this[kGetInspectOptions](this._stderr);
304+ return formatWithOptions(opts, ...args);
305+ }
306+ },
307+ });
220308
221309// Make a function that can serve as the callback passed to `stream.write()`.
222310function createWriteErrorHandler(instance, streamSymbol) {
@@ -239,76 +327,6 @@ function createWriteErrorHandler(instance, streamSymbol) {
239327 };
240328}
241329
242- Console . prototype [ kWriteToConsole ] = function ( streamSymbol , string ) {
243- const ignoreErrors = this . _ignoreErrors ;
244- const groupIndent = this [ kGroupIndent ] ;
245-
246- const useStdout = streamSymbol === kUseStdout ;
247- const stream = useStdout ? this . _stdout : this . _stderr ;
248- const errorHandler = useStdout ?
249- this . _stdoutErrorHandler : this . _stderrErrorHandler ;
250-
251- if ( groupIndent . length !== 0 ) {
252- if ( string . includes ( '\n' ) ) {
253- string = string . replace ( / \n / g, `\n${ groupIndent } ` ) ;
254- }
255- string = groupIndent + string ;
256- }
257- string += '\n' ;
258-
259- if ( ignoreErrors === false ) return stream . write ( string ) ;
260-
261- // There may be an error occurring synchronously (e.g. for files or TTYs
262- // on POSIX systems) or asynchronously (e.g. pipes on POSIX systems), so
263- // handle both situations.
264- try {
265- // Add and later remove a noop error handler to catch synchronous errors.
266- if ( stream . listenerCount ( 'error' ) === 0 )
267- stream . once ( 'error' , noop ) ;
268-
269- stream . write ( string , errorHandler ) ;
270- } catch ( e ) {
271- // Console is a debugging utility, so it swallowing errors is not desirable
272- // even in edge cases such as low stack space.
273- if ( isStackOverflowError ( e ) )
274- throw e ;
275- // Sorry, there's no proper way to pass along the error here.
276- } finally {
277- stream . removeListener ( 'error' , noop ) ;
278- }
279- } ;
280-
281- const kColorInspectOptions = { colors : true } ;
282- const kNoColorInspectOptions = { } ;
283- Console . prototype [ kGetInspectOptions ] = function ( stream ) {
284- let color = this [ kColorMode ] ;
285- if ( color === 'auto' ) {
286- color = stream . isTTY && (
287- typeof stream . getColorDepth === 'function' ?
288- stream . getColorDepth ( ) > 2 : true ) ;
289- }
290-
291- const options = optionsMap . get ( this ) ;
292- if ( options ) {
293- if ( options . colors === undefined ) {
294- options . colors = color ;
295- }
296- return options ;
297- }
298-
299- return color ? kColorInspectOptions : kNoColorInspectOptions ;
300- } ;
301-
302- Console . prototype [ kFormatForStdout ] = function ( args ) {
303- const opts = this [ kGetInspectOptions ] ( this . _stdout ) ;
304- return formatWithOptions ( opts , ...args ) ;
305- } ;
306-
307- Console . prototype [ kFormatForStderr ] = function ( args ) {
308- const opts = this [ kGetInspectOptions ] ( this . _stderr ) ;
309- return formatWithOptions ( opts , ...args ) ;
310- } ;
311-
312330const consoleMethods = {
313331 log(...args) {
314332 this[kWriteToConsole](kUseStdout, this[kFormatForStdout](args));
0 commit comments