let/const scoping when running JS from C++

32 views
Skip to first unread message

Robbie McElrath

unread,
Apr 11, 2022, 7:48:55 PM4/11/22
to v8-u...@googlegroups.com
Hello!

I'm trying to implement a replacement for the eval call in EvalJs that won't be blocked by CSP. To do this, I'm reimplementing the JS wrapper (see the previous link) in C++ and having the ClassicScript run the test-provided JS unmodified without an eval.

This mostly works, but the scoping behavior is different than eval's, which is causing issues. I want vars to be defined in the global scope, but let/const to be scoped to the JS being executed. I tried creating a new Context that shared a global object with the existing one (my understanding is that scopes map to Contexts), but Chrome was unhappy about defining a new Context for the main world, and I'm not sure that would make vars behave correctly.

Is there a way to get the scoping behavior I want, or a better approach to work around CSP?


Thanks!
  Robbie

Toon Verwaest

unread,
Apr 13, 2022, 5:45:13 AM4/13/22
to v8-u...@googlegroups.com
What about just wrapping the code in a block: `"{"+actual_source+"}"` Let and const will stay in the block while var will float out to the global scope.

--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-users/CANtkjcRi8O3Z9aPzeEX0sFPmCfdhbB1%2Bp-zKnYVnFHnry1D3NA%40mail.gmail.com.

Robbie McElrath

unread,
Apr 22, 2022, 5:15:04 PM4/22/22
to v8-users
Thanks for your response Toon! Sorry for the late reply, I didn't get an email about it for some reason.

Wrapping the code in a block worked! I had tried that previously but must have messed it up somehow, so I'm glad I tried again.

Reply all
Reply to author
Forward
0 new messages