I really want a way to get an opaque callsite handle of a function invocation from within the function in JavaScript. Currently this is something locked up in the tagged template literal syntax, but it'd be extremely useful for builder-patterns that don't use template literals.
What do you exactly mean with "handle". JS does not have a concept of call site so far, except for the unspec'ed Error.stack. How would you expect it to be exposed? As a string describing the source location?
-
-
I mean an opaque object unique per callsite. More like a Symbol than a string. It would only useful as a key for maps, or for host integration like Jump to Source in dev tools.
-
Imagine a rough "polyfill": let callsites = new WeakMap(); let callsite = (s) => { let cs = callsites.get(s); if (cs === undefined) { callsites.set(s, cs = Symbol()) } return cs; } And transform: f() to very-roughly: f(callsite``)
-
So iiuc you are interested in the source location of the call? Disregarding how call sites are represented, finding it on-demand should be easy by just waking the stack and not have runtime overhead. Not sure about security implications though.
-
Yep, but I don't care about understanding the source location object. Security may be helped by the handle being opaque (can't report to a server who's calling you) and the fact that a function should only ever generate a callsite source location for itself.
-
What's your use case? Just to distinguish call sites for the same function?
-
Yeah. Think of JSX / VDOM. You have a deeply nested call hierarchy that represents some state kept elsewhere. You invoke this repeatedly over time to get the new state, but sometimes you need to access the old state. Keying that old state by source location would work perfectly.
-
Not sure that's a common use case to justify as language feature, but for sure not too hard to implement without runtime overhead.
-
VDOM is extremely common though, and if it could more easily reconcile without the diff a significant amount of the web could benefit.
- 2 more replies
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.