mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-20 01:37:39 +03:00
LibJS: Restore the environment if an exception is thrown in 'with' block
This commit is contained in:
parent
4175be6960
commit
db0a48d34c
Notes:
sideshowbarker
2024-07-18 04:25:27 +09:00
Author: https://github.com/davidot Commit: https://github.com/SerenityOS/serenity/commit/db0a48d34c3 Pull-request: https://github.com/SerenityOS/serenity/pull/9895 Reviewed-by: https://github.com/linusg ✅
@ -379,12 +379,13 @@ Value WithStatement::execute(Interpreter& interpreter, GlobalObject& global_obje
|
||||
|
||||
// 6. Let C be the result of evaluating Statement.
|
||||
auto result = interpreter.execute_statement(global_object, m_body).value_or(js_undefined());
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
|
||||
// 7. Set the running execution context's LexicalEnvironment to oldEnv.
|
||||
interpreter.vm().running_execution_context().lexical_environment = old_environment;
|
||||
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
|
||||
// 8. Return Completion(UpdateEmpty(C, undefined)).
|
||||
return result;
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ test("basic with statement functionality", () => {
|
||||
}
|
||||
|
||||
expect(object.bar).toBe(2);
|
||||
expect(() => foo).toThrowWithMessage(ReferenceError, "'foo' is not defined");
|
||||
|
||||
expect(bar).toBe(99);
|
||||
});
|
||||
@ -22,3 +23,23 @@ test("basic with statement functionality", () => {
|
||||
test("syntax error in strict mode", () => {
|
||||
expect("'use strict'; with (foo) {}").not.toEval();
|
||||
});
|
||||
|
||||
test("restores lexical environment even when exception is thrown", () => {
|
||||
var object = {
|
||||
foo: 1,
|
||||
get bar() {
|
||||
throw Error();
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
with (object) {
|
||||
expect(foo).toBe(1);
|
||||
bar;
|
||||
}
|
||||
expect().fail();
|
||||
} catch (e) {
|
||||
expect(() => foo).toThrowWithMessage(ReferenceError, "'foo' is not defined");
|
||||
}
|
||||
expect(() => foo).toThrowWithMessage(ReferenceError, "'foo' is not defined");
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user