Last active
August 29, 2015 14:19
-
-
Save jordaaash/ebd98889293652cff919 to your computer and use it in GitHub Desktop.
Some crazy behavior discovered while playing around with "sandboxing" some indirectly eval'd code by shadowing in-scope variables. The first function works as expected (this and global have been shadowed in our indirect lexical scope). But if we run another call inside it without the same shadow, it doesn't inherit the indirect lexical scope, bu…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The outer function can't access shadowed variables from the original scope ... | |
> (new Function('global', "'use strict'; return [typeof this, typeof global];"))(); | |
[ 'undefined', 'undefined' ] | |
// ... but an inner function can (!) | |
> (new Function('global', "'use strict'; return (new Function(\"'use strict'; return [typeof this, typeof global];\"))();"))(); | |
[ 'undefined', 'object '] | |
// It can read them. | |
> var foo = 'foo'; | |
> (new Function('foo', "'use strict'; return (new Function(\"'use strict'; return foo;\"))();"))(); | |
'foo' | |
// It can write them. | |
> (new Function('foo', "'use strict'; return (new Function(\"'use strict'; foo = 'not foo anymore';\"))();"))(); | |
> foo | |
'not foo anymore' | |
// This applies to both local variables and shadowing parameters of the outer function. | |
> (new Function('foo', "'use strict'; var foo = 'foo'; return (new Function(\"'use strict'; return foo;\"))();"))(); | |
'not foo anymore' | |
// The outer indirect eval's scope just vanishes leaving only the original lexical scope. | |
> (new Function('a', "'use strict'; var bar = 'bar'; return (new Function(\"'use strict'; return bar;\"))();"))(); | |
ReferenceError: bar is not defined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment