1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:47:35 +00:00

LibJS: Get initial_value from local variables if id represents a local

If identifier represents local variable we should get its value from
`local_variables` in `ExecutionContext` instead of environment.
This commit is contained in:
Aliaksandr Kalenik 2023-09-18 16:59:02 +02:00 committed by Andreas Kling
parent 675d919dd2
commit a4a94de942
2 changed files with 14 additions and 1 deletions

View file

@ -661,7 +661,11 @@ ThrowCompletionOr<void> ECMAScriptFunctionObject::function_declaration_instantia
// 4. Else, // 4. Else,
else { else {
// a. Let initialValue be ! env.GetBindingValue(n, false). // a. Let initialValue be ! env.GetBindingValue(n, false).
initial_value = MUST(environment->get_binding_value(vm, id.string(), false)); if (id.is_local()) {
initial_value = callee_context.local_variables[id.local_variable_index()];
} else {
initial_value = MUST(environment->get_binding_value(vm, id.string(), false));
}
} }
// 5. Perform ! varEnv.InitializeBinding(n, initialValue). // 5. Perform ! varEnv.InitializeBinding(n, initialValue).

View file

@ -151,3 +151,12 @@ test("use variable as default function parameter", () => {
expect(func()).toBe(a); expect(func()).toBe(a);
}); });
test("variable is initialized to the value of the parameter if one with the same name exists", () => {
function func(a = 1) {
var a;
return a;
}
expect(func()).toBe(1);
});