Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 02:21
    sabrehagen reopened #33408
  • 01:04
    austincummings opened #33441
  • 00:00
    typescript-bot closed #33301
  • Sep 15 23:54
    sabrehagen closed #33408
  • Sep 15 23:20
    jtenner opened #33440
  • Sep 15 20:00
    typescript-bot closed #33279
  • Sep 15 20:00
    typescript-bot closed #33317
  • Sep 15 20:00
    typescript-bot closed #33403
  • Sep 15 18:08
    ahejlsberg synchronize #32695
  • Sep 15 18:08

    ahejlsberg on assertionsInControlFlow

    for-in or for-of expression is … (compare)

  • Sep 15 18:00
    typescript-bot closed #33296
  • Sep 15 16:50
    ahejlsberg synchronize #32695
  • Sep 15 16:50

    ahejlsberg on assertionsInControlFlow

    Fix inference circularity error… (compare)

  • Sep 15 15:38
    ahejlsberg synchronize #32695
  • Sep 15 15:38

    ahejlsberg on assertionsInControlFlow

    Further CFA handling of exhaust… Accept new baselines Fix call to Debug.fail in compi… (compare)

  • Sep 15 10:23
    jack-williams synchronize #33434
  • Sep 15 08:34
    whzx5byb edited #33439
  • Sep 15 08:34
    whzx5byb edited #33439
  • Sep 15 08:33
    whzx5byb opened #33439
  • Sep 15 00:00
    typescript-bot closed #33355
AnyhowStep
@AnyhowStep
That syntax just looks crazy to me
What the heck is (0, eval)
Bruce Pascoe
@fatcerberus
Comma operator
Per spec the JS engine looks specifically for eval(... to trigger direct eval
(0, eval) bypasses it
AnyhowStep
@AnyhowStep
Huh
Interesting
Thanks for the heads up!
Bruce Pascoe
@fatcerberus
n/p. In case it wasn't clear the 0 can be anything you want
But 0 is most concise
AnyhowStep
@AnyhowStep
Definitely going in a huge comment block
Bruce Pascoe
@fatcerberus

You can also use this trick to prevent this from being bound:

obj.foo();  // this = obj
(0, obj.foo)();  // this = undefined (or window in sloppy mode)

which I mentioned here https://github.com/webpack/webpack/issues/9615#issuecomment-524167958

Keith Layne
@keithlayne
all you need is
// Betcha can't figure this out
AnyhowStep
@AnyhowStep
I don't think I'll ever use that construct. But it's cool to know
Well, I just did use it. But you know what I mean
Bruce Pascoe
@fatcerberus
I can see why webpack would want to do it in generated code
But yeah I'd probably never write it myself (except for eval)
Keith Layne
@keithlayne
couldn't you just const INeverCalledItDirectEval = eval?
Bruce Pascoe
@fatcerberus
Well it would be const indirectEval = eval but yes
(0,eval) just avoids creating a temporary
Keith Layne
@keithlayne
yeah I can't read
Bruce Pascoe
@fatcerberus
I'm not sure why using the comma operator suppresses the direct eval actually, I thought it was just that the engine wanted to be able to verify statically that this was an eval call
Keith Layne
@keithlayne
I was thinking that you'd probably use it more than once, just stash that somewhere
AnyhowStep
@AnyhowStep

Now I'm even more confused.
Why would const indirectEval = eval be any different from eval?

It doesn't affect other functions.

Or is it just because eval is special?
Keith Layne
@keithlayne
or is it that eval is somehow bound to its scope?
Bruce Pascoe
@fatcerberus
I used to know the answer to that. I've forgotten the rationale
eval(...) by itself inherits the containing scope
AnyhowStep
@AnyhowStep
Okay, magic
Bruce Pascoe
@fatcerberus
Anything other than that will trigger indirect eval and only get globals
Bruce Pascoe
@fatcerberus
The direct eval behavior is basically legacy magic left in because people used to do stupid stuff like foo = eval('x' + id)
Where the x* vars are local
AnyhowStep
@AnyhowStep
Ah, right. I think I have seen code like that before
Keith Layne
@keithlayne
that may be the weirdest JS thing I've seen.
Bruce Pascoe
@fatcerberus
But they only enable that behavior if you're actually using eval itself, i.e. it's like a pseudo-keyword
Keith Layne
@keithlayne
that part
that it can be escaped with this one weird trick
Dude I might still have code in our repos that looks like eval('foo.' + bar)
Bruce Pascoe
@fatcerberus
I thought people knew JS well enough in 2019 that nobody would ever write code like that ever again
Keith Layne
@keithlayne
hold my beer
AnyhowStep
@AnyhowStep
function foo () {
  const x = 4
  const indirectEval = eval
  eval("console.log(x)") //4
  indirectEval("console.log(x)") //x is not defined
}
foo()
Hmmm
Giving eval access to the global scope still seems too permissive
I need to start an eval-as-a-service company
Pass your string to my servers to eval and I'll give you back the result
(Unless the result is a function, or cannot be JSON stringified)
Every invocation starts a new sandbox, runs eval, stringifies the result, and sends it back to you
Anyone in Gitter want to invest?
Bruce Pascoe
@fatcerberus
I could have sworn there was a design document in the Duktape repo discussing the direct vs. indirect eval thing in depth (including why aliasing triggers indirectness) but I can't find it