From c5e45008df3bd6888a9737ec1f01c8416e13dec5 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sun, 12 Sep 2010 14:53:06 +0200 Subject: [PATCH] Add a luaL_loadfile wrapper to lua::state --- src/luamm.cc | 17 +++++++++++++++++ src/luamm.hh | 16 ++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/luamm.cc b/src/luamm.cc index bcc24652..ffc68b03 100644 --- a/src/luamm.cc +++ b/src/luamm.cc @@ -351,6 +351,23 @@ namespace lua { return safe_compare(&safe_compare_trampoline<&lua_lessthan>, index1, index2); } + void state::loadfile(const char *filename) + throw(lua::syntax_error, lua::file_error, std::bad_alloc) + { + switch(luaL_loadfile(cobj.get(), filename)) { + case 0: + return; + case LUA_ERRSYNTAX: + throw lua::syntax_error(this); + case LUA_ERRFILE: + throw lua::file_error(this); + case LUA_ERRMEM: + throw std::bad_alloc(); + default: + assert(0); + } + } + void state::loadstring(const char *s) throw(lua::syntax_error, std::bad_alloc) { switch(luaL_loadstring(cobj.get(), s)) { diff --git a/src/luamm.hh b/src/luamm.hh index 907fafef..3466f274 100644 --- a/src/luamm.hh +++ b/src/luamm.hh @@ -124,6 +124,21 @@ namespace lua { {} }; + // loadfile() encountered an error while opening/reading the file + class file_error: public lua::exception { + file_error(const file_error &) = delete; + const file_error& operator=(const file_error &) = delete; + + public: + file_error(state *L) + : lua::exception(L) + {} + + file_error(file_error &&other) + : lua::exception(std::move(other)) + {} + }; + // double fault, lua encountered an error while running the error handler function class errfunc_error: public lua::exception { errfunc_error(const errfunc_error &) = delete; @@ -259,6 +274,7 @@ namespace lua { void gettable(int index); void getglobal(const char *name) { getfield(GLOBALSINDEX, name); } bool lessthan(int index1, int index2); + void loadfile(const char *filename) throw(lua::syntax_error, lua::file_error, std::bad_alloc); void loadstring(const char *s) throw(lua::syntax_error, std::bad_alloc); bool next(int index); // register is a reserved word :/