ladybird/Userland/Libraries/LibJS/Tests/syntax/switch-as-statement.js
davidot 830ea0414c LibJS: Make scoping follow the spec
Before this we used an ad-hoc combination of references and 'variables'
stored in a hashmap. This worked in most cases but is not spec like.
Additionally hoisting, dynamically naming functions and scope analysis
was not done properly.

This patch fixes all of that by:
  - Implement BindingInitialization for destructuring assignment.
  - Implementing a new ScopePusher which tracks the lexical and var
    scoped declarations. This hoists functions to the top level if no
    lexical declaration name overlaps. Furthermore we do checking of
    redeclarations in the ScopePusher now requiring less checks all over
    the place.
  - Add methods for parsing the directives and statement lists instead
    of having that code duplicated in multiple places. This allows
    declarations to pushed to the appropriate scope more easily.
  - Remove the non spec way of storing 'variables' in
    DeclarativeEnvironment and make Reference follow the spec instead of
    checking both the bindings and 'variables'.
  - Remove all scoping related things from the Interpreter. And instead
    use environments as specified by the spec. This also includes fixing
    that NativeFunctions did not produce a valid FunctionEnvironment
    which could cause issues with callbacks and eval. All
    FunctionObjects now have a valid NewFunctionEnvironment
    implementation.
  - Remove execute_statements from Interpreter and instead use
    ASTNode::execute everywhere this simplifies AST.cpp as you no longer
    need to worry about which method to call.
  - Make ScopeNodes setup their own environment. This uses four
    different methods specified by the spec
    {Block, Function, Eval, Global}DeclarationInstantiation with the
    annexB extensions.
  - Implement and use NamedEvaluation where specified.

Additionally there are fixes to things exposed by these changes to eval,
{for, for-in, for-of} loops and assignment.

Finally it also fixes some tests in test-js which where passing before
but not now that we have correct behavior :^).
2021-09-30 08:16:32 +01:00

69 lines
1.6 KiB
JavaScript

describe("switch statement is a valid statement and gets executed", () => {
test("switch statement in a block", () => {
let hit = false;
{
switch (true) {
case true:
hit = true;
}
expect(hit).toBeTrue();
}
});
test("switch statement in an if statement when true", () => {
let hit = false;
var a = true;
if (a)
switch (true) {
case true:
hit = true;
}
else
switch (true) {
case true:
expect().fail();
}
expect(hit).toBeTrue();
});
test("switch statement in an if statement when false", () => {
let hit = false;
var a = false;
if (a)
switch (a) {
default:
expect().fail();
}
else
switch (a) {
default:
hit = true;
}
expect(hit).toBeTrue();
});
test("switch statement in an while statement", () => {
var a = 0;
var loops = 0;
while (a < 1 && loops++ < 5)
switch (a) {
case 0:
a = 1;
}
expect(a).toBe(1);
});
test("switch statement in an for statement", () => {
var loops = 0;
for (let a = 0; a < 1 && loops++ < 5; )
switch (a) {
case 0:
a = 1;
}
expect(loops).toBe(1);
});
});