Function Implementation Hiding Proposal: Goals, Directives, and Alternative Approaches
This article reviews the TC39 function‑implementation‑hiding proposal, explains the new hide‑source and sensitive directives that can conceal Function.prototype.toString and Error.stack details, evaluates their benefits and drawbacks, compares alternative hiding mechanisms, and answers common questions about the specification.
The article summarizes the current status of the TC39 function-implementation-hiding proposal, which is at stage‑2 and may advance to stage‑3 after the December meeting, urging developers to consider its impact on JavaScript APIs.
It introduces two new directives— hide source and sensitive —that allow developers to hide implementation details from Function.prototype.toString and from the file‑path information exposed by Error.prototype.stack . Example usage:
function foo() { /* ... */ }
console.assert(foo.toString().includes('...'))When the hide source directive is applied:
'hide source'
function foo() { /* ... */ }
console.assert(!foo.toString().includes('...'))The directives help library authors avoid breaking changes caused by exposing source code, enable more faithful polyfills, and improve security for sensitive projects.
Typical stack traces before applying the directives look like:
$ node
Welcome to Node.js v12.13.0.
> console.log((new Error).stack)
Error
at repl:1:14
at Script.runInThisContext (vm.js:116:20)
...After applying hide source (or sensitive ), the stack output no longer contains the source location of hidden functions.
$ node
Welcome to Node.js v12.13.0.
> console.log((new Error).stack)
Error
at repl:1:14
at Script.runInThisContext (vm.js:116:20)
... // locations of hidden functions are omittedThe article also discusses alternative solutions that were considered and rejected:
A one‑time hiding function such as Error.hideFromStackTraces or Function.prototype.hideSource .
A clone‑creating hiding function that returns a wrapped version of the original function.
Deleting Function.prototype.toString entirely.
Using a Symbol flag (e.g., Symbol.hideSource ) to toggle hiding.
Each alternative is evaluated for feasibility, compatibility, and tooling impact, with the directive approach favored for its file‑level granularity and backward compatibility.
Frequently asked questions address whether the directives hide function names or parameter counts (they do not), their effect on devtools, and the likelihood of widespread adoption. The proposal deliberately does not include a preserve source directive because the use‑case is rare.
Finally, the article lists relevant links to the proposal repository and discussion threads.
360 Tech Engineering
Official tech channel of 360, building the most professional technology aggregation platform for the brand.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.