diff options
author | Linus Groh <mail@linusgroh.de> | 2021-05-13 23:58:45 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-05-13 23:59:00 +0100 |
commit | a92dc4e30dad94df27c4d06633ac0b50ad6bf996 (patch) | |
tree | 9e3db404f8ec576542b9c746c1ce3feb111bc8ad /Userland/Libraries/LibJS/Tests/functions | |
parent | b221cad659eeb2da723d7dc16e8d6cee0c4a56c2 (diff) | |
download | serenity-a92dc4e30dad94df27c4d06633ac0b50ad6bf996.zip |
LibJS: Ensure function declarations don't leak outside function scopes
When using VM::set_variable() to put the created ScriptFunction onto a
ScopeObject, we would previously unexpectedly reach the global object as
set_variable() checks each traversed scope for an existing Variable with
the given name - which would cause a leak of the inner function past the
outer function (we even had a test expecting that behaviour!). Now we
first declare functions (as DeclarationKind::Var) before setting them.
This will need some more work to make hoisting across non-lexical scopes
work, but it fixes this specific issue for now.
Fixes #6766.
Diffstat (limited to 'Userland/Libraries/LibJS/Tests/functions')
-rw-r--r-- | Userland/Libraries/LibJS/Tests/functions/function-nesting.js | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/functions/function-nesting.js b/Userland/Libraries/LibJS/Tests/functions/function-nesting.js new file mode 100644 index 0000000000..daef3c7cd3 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/functions/function-nesting.js @@ -0,0 +1,15 @@ +test("issue #6766, nested functions should not leak to global object", () => { + function foo() { + function bar() { + function baz() { + return 42; + } + return baz(); + } + return bar(); + } + expect(foo()).toBe(42); + expect(globalThis.foo).toBeUndefined(); + expect(globalThis.bar).toBeUndefined(); + expect(globalThis.baz).toBeUndefined(); +}); |