Last active
October 27, 2023 10:24
-
-
Save Exponential-Workload/00a3f2f81de6babac8efd0d927047116 to your computer and use it in GitHub Desktop.
Scope Functionality Testing Thing
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
let f; | |
let y; | |
let z; | |
let i = 0; | |
const j = ()=>{ | |
let s = ++i; | |
f = f ?? (() => s); | |
z = ()=>s++; | |
y = y ?? z; | |
} | |
j(); | |
if (i !== 1) throw new Error('i did not increment to 1 after j was called (i is in root scope)'); | |
if (f() !== i) throw new Error('f did not return value of i'); | |
j(); | |
if (f() !== 1) throw new Error('Scoped variable \'s\' changed after changing i (does j\'s scope leak?)'); | |
if (f() === i) throw new Error('Global variable \'i\' has same value as \'s\' after incrementing'); | |
y(); | |
if (f() !== 2) throw new Error('Scoped variable \'s\' did not change after incrementing in initial scope'); | |
z(); | |
if (f() !== 2) throw new Error('Scoped variable \'s\' changed in initial scope after changing \'s\' in 2nd j() scope'); |
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
local f; | |
local y; | |
local z; | |
local i = 0; | |
local j = function() | |
i=i+1; | |
local s = i; | |
f = f or function()return s;end; | |
z = function()s=s+1;end; | |
y = y or z; | |
end; | |
j(); | |
if (i ~= 1) then error('i did not increment to 1 after j was called (i is in root scope)'); end; | |
if (f() ~= i) then error('f did not return value of i'); end; | |
j(); | |
if (f() ~= 1) then error('Scoped variable \'s\' changed after changing i (does j\'s scope leak?)'); end; | |
if (f() == i) then error('Global variable \'i\' has same value as \'s\' after incrementing'); end; | |
y(); | |
if (f() ~= 2) then error('Scoped variable \'s\' did not change after incrementing in initial scope'); end; | |
z(); | |
if (f() ~= 2) then error('Scoped variable \'s\' changed in initial scope after changing \'s\' in 2nd j() scope'); end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment