tc39 / proposal-function-implementation-hiding

JavaScript language proposal: function implementation hiding
https://ci.tc39.es/preview/tc39/ecma262/pull/1739
MIT License
100 stars 7 forks source link

The behavior of ‘sensitive‘ on `Error.p.stack` #33

Closed lidongjies closed 4 years ago

lidongjies commented 5 years ago

The "hide source" directive hides the file attribution and position information revealed by Error.prototype.stack.

The "sensitive" directive omits the function entirely from Error.prototype.stack.

I don't know clearly the difference between hide source and sensitive apply on Error.prototype.stack.

michaelficarra commented 4 years ago

A stack trace:

$ node
Welcome to Node.js v12.13.0.
Type ".help" for more information.
> console.log((new Error).stack)
Error
    at repl:1:14
    at Script.runInThisContext (vm.js:116:20)
    at REPLServer.defaultEval (repl.js:404:29)
    at bound (domain.js:420:14)
    at REPLServer.runBound [as eval] (domain.js:433:12)
    at REPLServer.onLine (repl.js:715:10)
    at REPLServer.emit (events.js:215:7)
    at REPLServer.EventEmitter.emit (domain.js:476:20)
    at REPLServer.Interface._onLine (readline.js:316:10)
    at REPLServer.Interface._line (readline.js:693:8)
undefined
> 

The same stack trace if bound was marked as "sensitive":

$ node
Welcome to Node.js v12.13.0.
Type ".help" for more information.
> console.log((new Error).stack)
Error
    at repl:1:14
    at Script.runInThisContext (vm.js:116:20)
    at REPLServer.defaultEval (repl.js:404:29)
    at REPLServer.runBound [as eval] (domain.js:433:12)
    at REPLServer.onLine (repl.js:715:10)
    at REPLServer.emit (events.js:215:7)
    at REPLServer.EventEmitter.emit (domain.js:476:20)
    at REPLServer.Interface._onLine (readline.js:316:10)
    at REPLServer.Interface._line (readline.js:693:8)
undefined
> 

The same stack trace if bound was marked as "hide source":

$ node
Welcome to Node.js v12.13.0.
Type ".help" for more information.
> console.log((new Error).stack)
Error
    at repl:1:14
    at Script.runInThisContext (vm.js:116:20)
    at REPLServer.defaultEval (repl.js:404:29)
    at anonymous
    at REPLServer.runBound [as eval] (domain.js:433:12)
    at REPLServer.onLine (repl.js:715:10)
    at REPLServer.emit (events.js:215:7)
    at REPLServer.EventEmitter.emit (domain.js:476:20)
    at REPLServer.Interface._onLine (readline.js:316:10)
    at REPLServer.Interface._line (readline.js:693:8)
undefined
> 
hax commented 4 years ago

@michaelficarra Can we add this example to README? It will be very helpful for the developers to understand the behavior.

michaelficarra commented 4 years ago

@hax done, thanks for the suggestion.

lidongjies commented 4 years ago

why use anonymous instead of the function name? As I know Error.prototype.stack is non-standard though de facto.

try {
  ;[1, 2, 3].forEach(i => {
    if (i > 1) {
      throw new Error()
    }
  })
} catch (e) {
  console.log('%s', e.stack)
}

Chrome

Error at file:///Users/makeco/Github/function-implementation-hiding/index.js:4:10 at Array.forEach (\<anonymous>) at file:///Users/makeco/Github/function-implementation-hiding/index.js:2:13


Safari

safari file:///Users/makeco/Github/function-implementation-hiding/index.js:4:19 forEach@[native code] global code@file:///Users/makeco/Github/function-implementation-hiding/index.js:2:20


Firefox

firefox @file:///Users/makeco/Github/function-implementation-hiding/index.js:4:10 @file:///Users/makeco/Github/function-implementation-hiding/index.js:2:13

michaelficarra commented 4 years ago

@makeco Sorry, this was my error. The name will still appear in the stack trace, but file/position information will not. So the correct example for "hide source" would be something like

$ node
Welcome to Node.js v12.13.0.
Type ".help" for more information.
> console.log((new Error).stack)
Error
    at repl:1:14
    at Script.runInThisContext (vm.js:116:20)
    at REPLServer.defaultEval (repl.js:404:29)
    at bound (<anonymous>)
    at REPLServer.runBound [as eval] (domain.js:433:12)
    at REPLServer.onLine (repl.js:715:10)
    at REPLServer.emit (events.js:215:7)
    at REPLServer.EventEmitter.emit (domain.js:476:20)
    at REPLServer.Interface._onLine (readline.js:316:10)
    at REPLServer.Interface._line (readline.js:693:8)
undefined
> 

matching the output for built-in functions. I will update the example in the README to match.

edit: See https://github.com/tc39/proposal-function-implementation-hiding/commit/9c8d1d4236736bdb17e771f99579fdf32da5c7da.