From 46ddb16128811ba9c95dafbc134cd0afbd8e036f Mon Sep 17 00:00:00 2001 From: falsycat Date: Wed, 14 Sep 2022 15:25:08 +0900 Subject: [PATCH] implement exception handling in some complicated cases --- common/life.hh | 7 +------ file/luajit_inline_node.cc | 2 +- file/luajit_node.cc | 2 +- file/sequencer_call.cc | 2 +- main.cc | 4 ++-- nf7.cc | 4 ++-- nf7.hh | 9 +++++---- 7 files changed, 13 insertions(+), 17 deletions(-) diff --git a/common/life.hh b/common/life.hh index 331632e..aa2f8c2 100644 --- a/common/life.hh +++ b/common/life.hh @@ -8,11 +8,6 @@ namespace nf7 { -class LifeExpiredException final : public nf7::Exception { - public: - using nf7::Exception::Exception; -}; - template class Life final { public: @@ -57,7 +52,7 @@ class Life::Ref final { void EnforceAlive() const { if (!data_->ptr) { - throw LifeExpiredException {"target expired"}; + throw nf7::ExpiredException {"target expired"}; } } diff --git a/file/luajit_inline_node.cc b/file/luajit_inline_node.cc index 8f69eef..c20a007 100644 --- a/file/luajit_inline_node.cc +++ b/file/luajit_inline_node.cc @@ -186,7 +186,7 @@ class InlineNode::Lambda final : public nf7::Node::Lambda, th->Resume(thL, 3); }); - } catch (nf7::LifeExpiredException&) { + } catch (nf7::ExpiredException&) { } catch (nf7::Exception& e) { log_->Error(e.msg()); } diff --git a/file/luajit_node.cc b/file/luajit_node.cc index dacc5f3..596aeb5 100644 --- a/file/luajit_node.cc +++ b/file/luajit_node.cc @@ -174,7 +174,7 @@ class Node::Lambda final : public nf7::Node::Lambda, log_->Error("failed to call lua function: "+e.msg()); } }); - } catch (nf7::LifeExpiredException&) { + } catch (nf7::ExpiredException&) { } catch (nf7::Exception& e) { log_->Error(e.msg()); } diff --git a/file/sequencer_call.cc b/file/sequencer_call.cc index 3210d0d..eb61885 100644 --- a/file/sequencer_call.cc +++ b/file/sequencer_call.cc @@ -201,7 +201,7 @@ try { ssla_ = nullptr; la_ = nullptr; } -} catch (nf7::LifeExpiredException&) { +} catch (nf7::ExpiredException&) { ss->Finish(); } catch (nf7::FileHolder::EmptyException&) { ss->Finish(); diff --git a/main.cc b/main.cc index b6eb014..f6d7c00 100644 --- a/main.cc +++ b/main.cc @@ -97,7 +97,7 @@ class Env final : public nf7::Env { // trigger global watcher for (auto w : watchers_map_[0]) w->Handle(e); - } catch (ExpiredException&) { + } catch (nf7::ExpiredException&) { } void Exit() noexcept override { @@ -105,7 +105,7 @@ class Env final : public nf7::Env { } void Save() noexcept override { try { - nf7::Serializer::Save(kFileName, root_); + nf7::Serializer::Save(*this, kFileName, root_); } catch (nf7::Exception&) { Panic(); } diff --git a/nf7.cc b/nf7.cc index 9fec4bd..fb14a6c 100644 --- a/nf7.cc +++ b/nf7.cc @@ -293,7 +293,7 @@ Serializer::ChunkGuard::~ChunkGuard() noexcept { *ar_ & static_cast(end - begin_); ar_->st_->Seek(end); } catch (nf7::Exception&) { - // TODO + ar_->env_->Throw(std::current_exception()); } } @@ -316,7 +316,7 @@ Deserializer::ChunkGuard::~ChunkGuard() { ar_->st_->Seek(end); } } catch (nf7::Exception&) { - // TODO + ar_->env_->Throw(std::current_exception()); } } void Deserializer::ChunkGuard::ValidateEnd() { diff --git a/nf7.hh b/nf7.hh index 985feb6..fa17768 100644 --- a/nf7.hh +++ b/nf7.hh @@ -383,14 +383,14 @@ class Serializer final : size_t begin_; }; - static void Save(const char* path, auto& v) { + static void Save(nf7::Env& env, const char* path, auto& v) { SerializerStream st {path, "wb"}; - Serializer ar {st}; + Serializer ar {env, st}; ar(v); } - Serializer(nf7::SerializerStream& st) : - binary_ostream(st), oarchive_header(st), st_(&st) { + Serializer(nf7::Env& env, nf7::SerializerStream& st) : + binary_ostream(st), oarchive_header(st), env_(&env), st_(&st) { } template @@ -413,6 +413,7 @@ class Serializer final : } private: + nf7::Env* const env_; nf7::SerializerStream* const st_; }; class Deserializer final :