The following call sequence is possible: C++ -> Lua -> C++ -> Lua -> C++ The first block of C++ is the Lean main function. The main function invokes the Lua interpreter. The Lua interpreter invokes a C++ Lean API. Then the Lean API invokes a callback implemented in Lua. The Lua callback invokes another Lean API. Now, suppose the Lean API throws an exception. We want the C++ exception to propagate over the mixed C++/Lua call stack. We use the clone/rethrow exception idiom to achieve this goal. Before this commit, the C++ exceptions were converted into strings using the method what(), and then they were propagated over the Lua stack using lua_error. A lua_error was then converted into a lua_exception when going back to C++. This solution was very unsatisfactory, since all C++ exceptions were being converted into a lua_exception, and consequently the structure of the exception was being lost. Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
62 lines
2.1 KiB
C++
62 lines
2.1 KiB
C++
/*
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
Author: Leonardo de Moura
|
|
*/
|
|
#pragma once
|
|
#include "util/lua.h"
|
|
#include <exception>
|
|
#include <string>
|
|
|
|
namespace lean {
|
|
class sstream;
|
|
/** \brief Base class for all Lean exceptions */
|
|
class exception : public std::exception {
|
|
protected:
|
|
std::string m_msg;
|
|
exception() {}
|
|
public:
|
|
exception(char const * msg);
|
|
exception(std::string const & msg);
|
|
exception(sstream const & strm);
|
|
virtual ~exception() noexcept;
|
|
virtual char const * what() const noexcept;
|
|
virtual exception * clone() const { return new exception(m_msg); }
|
|
virtual void rethrow() const { throw *this; }
|
|
};
|
|
/** \brief Exception produced by a Lean parser. */
|
|
class parser_exception : public exception {
|
|
protected:
|
|
unsigned m_line;
|
|
unsigned m_pos;
|
|
public:
|
|
parser_exception(char const * msg, unsigned l, unsigned p);
|
|
parser_exception(std::string const & msg, unsigned l, unsigned p);
|
|
parser_exception(sstream const & strm, unsigned l, unsigned p);
|
|
virtual ~parser_exception() noexcept;
|
|
virtual char const * what() const noexcept;
|
|
unsigned get_line() const { return m_line; }
|
|
unsigned get_pos() const { return m_pos; }
|
|
virtual exception * clone() const { return new parser_exception(m_msg, m_line, m_pos); }
|
|
virtual void rethrow() const { throw *this; }
|
|
};
|
|
/** \brief Exception used to sign that a computation was interrupted */
|
|
class interrupted : public exception {
|
|
public:
|
|
interrupted() {}
|
|
virtual ~interrupted() noexcept {}
|
|
virtual char const * what() const noexcept { return "interrupted"; }
|
|
virtual exception * clone() const { return new interrupted(); }
|
|
virtual void rethrow() const { throw *this; }
|
|
};
|
|
/** \brief Throw interrupted exception iff flag is true. */
|
|
inline void check_interrupted(bool flag) {
|
|
if (flag)
|
|
throw interrupted();
|
|
}
|
|
int push_exception(lua_State * L, exception const & e);
|
|
exception const & to_exception(lua_State * L, int i);
|
|
bool is_exception(lua_State * L, int i);
|
|
void open_exception(lua_State * L);
|
|
}
|