mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 22:27:35 +00:00
LibJS: Add TemporaryClearException helper class
This is very similar to AK::TemporaryChange (and in fact replaces one use of it), but since we can't directly set VM's m_exception from outside of the VM, we need something more sophisticated. Sometimes we need to temporarily remove the stored exception for some other operation to succeed (e.g. anything that uses call(), as well as get_without_side_effects()) and later restore it - the boilerplate code required for this is annoying enough to justify a helper.
This commit is contained in:
parent
3a4d42bbbb
commit
5caab0148c
2 changed files with 36 additions and 2 deletions
34
Userland/Libraries/LibJS/Runtime/TemporaryClearException.h
Normal file
34
Userland/Libraries/LibJS/Runtime/TemporaryClearException.h
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Linus Groh <mail@linusgroh.de>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibJS/Runtime/Exception.h>
|
||||
#include <LibJS/Runtime/VM.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
class TemporaryClearException {
|
||||
public:
|
||||
explicit TemporaryClearException(VM& vm)
|
||||
: m_vm(vm)
|
||||
, m_previous_exception(vm.exception())
|
||||
{
|
||||
m_vm.clear_exception();
|
||||
}
|
||||
|
||||
~TemporaryClearException()
|
||||
{
|
||||
if (m_previous_exception)
|
||||
m_vm.set_exception(*m_previous_exception);
|
||||
}
|
||||
|
||||
private:
|
||||
VM& m_vm;
|
||||
Exception* m_previous_exception;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue