2014-03-27 03:24:37 +01:00
|
|
|
// Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
|
2012-11-26 19:46:03 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
#include <libaegisub/fs.h>
|
|
|
|
|
2014-04-27 16:20:11 +02:00
|
|
|
#include <boost/exception/detail/attribute_noreturn.hpp>
|
|
|
|
#include <lua.hpp>
|
2012-11-26 19:46:03 +01:00
|
|
|
#include <string>
|
2014-04-27 15:47:00 +02:00
|
|
|
#include <vector>
|
2013-04-25 04:45:21 +02:00
|
|
|
#include <type_traits>
|
2014-03-27 03:24:37 +01:00
|
|
|
|
|
|
|
namespace agi { namespace lua {
|
2014-04-27 16:20:11 +02:00
|
|
|
// Exception type for errors where the error details are on the lua stack
|
|
|
|
struct error_tag {};
|
|
|
|
|
|
|
|
// Below are functionally equivalent to the luaL_ functions, but using a C++
|
|
|
|
// exception for stack unwinding
|
|
|
|
int BOOST_ATTRIBUTE_NORETURN error(lua_State *L, const char *fmt, ...);
|
|
|
|
int BOOST_ATTRIBUTE_NORETURN argerror(lua_State *L, int narg, const char *extramsg);
|
|
|
|
int BOOST_ATTRIBUTE_NORETURN typerror(lua_State *L, int narg, const char *tname);
|
|
|
|
void argcheck(lua_State *L, bool cond, int narg, const char *msg);
|
2012-11-26 19:46:03 +01:00
|
|
|
|
|
|
|
inline void push_value(lua_State *L, bool value) { lua_pushboolean(L, value); }
|
|
|
|
inline void push_value(lua_State *L, const char *value) { lua_pushstring(L, value); }
|
|
|
|
inline void push_value(lua_State *L, double value) { lua_pushnumber(L, value); }
|
|
|
|
inline void push_value(lua_State *L, int value) { lua_pushinteger(L, value); }
|
|
|
|
inline void push_value(lua_State *L, void *p) { lua_pushlightuserdata(L, p); }
|
|
|
|
|
2013-04-25 04:45:21 +02:00
|
|
|
template<typename Integer>
|
|
|
|
typename std::enable_if<std::is_integral<Integer>::value>::type
|
2014-03-27 03:24:37 +01:00
|
|
|
push_value(lua_State *L, Integer value) {
|
|
|
|
lua_pushinteger(L, static_cast<lua_Integer>(value));
|
2012-11-26 19:46:03 +01:00
|
|
|
}
|
|
|
|
|
2014-03-27 03:24:37 +01:00
|
|
|
inline void push_value(lua_State *L, fs::path const& value) {
|
|
|
|
lua_pushstring(L, value.string().c_str());
|
2012-11-26 19:46:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void push_value(lua_State *L, std::string const& value) {
|
2014-04-24 23:49:36 +02:00
|
|
|
lua_pushlstring(L, value.c_str(), value.size());
|
2012-11-26 19:46:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void push_value(lua_State *L, lua_CFunction value) {
|
|
|
|
if (lua_type(L, -2) == LUA_TUSERDATA) {
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
lua_pushcclosure(L, value, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lua_pushcclosure(L, value, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2014-04-27 15:47:00 +02:00
|
|
|
void push_value(lua_State *L, std::vector<T> const& value) {
|
|
|
|
lua_createtable(L, value.size(), 0);
|
|
|
|
for (size_t i = 0; i < value.size(); ++i) {
|
|
|
|
push_value(L, value[i]);
|
|
|
|
lua_rawseti(L, -2, i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 16:20:11 +02:00
|
|
|
/// Wrap a function which may throw exceptions and make it trigger lua errors
|
|
|
|
/// whenever it throws
|
|
|
|
template<int (*func)(lua_State *L)>
|
|
|
|
int exception_wrapper(lua_State *L) {
|
|
|
|
try {
|
|
|
|
return func(L);
|
|
|
|
}
|
|
|
|
catch (agi::Exception const& e) {
|
|
|
|
push_value(L, e.GetChainedMessage());
|
|
|
|
return lua_error(L);
|
|
|
|
}
|
|
|
|
catch (std::exception const& e) {
|
|
|
|
push_value(L, e.what());
|
|
|
|
return lua_error(L);
|
|
|
|
}
|
|
|
|
catch (error_tag) {
|
|
|
|
// Error message is already on the stack
|
|
|
|
return lua_error(L);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
std::terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 15:47:00 +02:00
|
|
|
template<typename T>
|
|
|
|
void set_field(lua_State *L, const char *name, T value) {
|
2012-11-26 19:46:03 +01:00
|
|
|
push_value(L, value);
|
|
|
|
lua_setfield(L, -2, name);
|
|
|
|
}
|
|
|
|
|
2014-04-27 16:20:11 +02:00
|
|
|
template<int (*func)(lua_State *L)>
|
|
|
|
void set_field(lua_State *L, const char *name) {
|
|
|
|
push_value(L, exception_wrapper<func>);
|
|
|
|
lua_setfield(L, -2, name);
|
|
|
|
}
|
|
|
|
|
2014-04-26 00:40:43 +02:00
|
|
|
std::string get_string_or_default(lua_State *L, int idx);
|
|
|
|
std::string get_string(lua_State *L, int idx);
|
|
|
|
std::string get_global_string(lua_State *L, const char *name);
|
2012-11-26 19:46:03 +01:00
|
|
|
|
2014-04-27 16:20:11 +02:00
|
|
|
std::string check_string(lua_State *L, int idx);
|
|
|
|
int check_int(lua_State *L, int idx);
|
|
|
|
size_t check_uint(lua_State *L, int idx);
|
|
|
|
void *check_udata(lua_State *L, int idx, const char *mt);
|
|
|
|
|
2014-03-27 03:24:37 +01:00
|
|
|
template<typename T, typename... Args>
|
|
|
|
T *make(lua_State *L, const char *mt, Args&&... args) {
|
|
|
|
auto obj = static_cast<T*>(lua_newuserdata(L, sizeof(T)));
|
|
|
|
new(obj) T(std::forward<Args>(args)...);
|
|
|
|
luaL_getmetatable(L, mt);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T& get(lua_State *L, int idx, const char *mt) {
|
2014-04-27 16:20:11 +02:00
|
|
|
return *static_cast<T *>(check_udata(L, idx, mt));
|
2014-03-27 03:24:37 +01:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:31:48 +02:00
|
|
|
struct LuaForEachBreak {};
|
|
|
|
|
|
|
|
template<typename Func>
|
|
|
|
void lua_for_each(lua_State *L, Func&& func) {
|
|
|
|
lua_pushnil(L); // initial key
|
|
|
|
while (lua_next(L, -2)) {
|
|
|
|
try {
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
catch (LuaForEachBreak) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lua_pop(L, 1); // pop value, leave key
|
|
|
|
}
|
|
|
|
lua_pop(L, 1); // pop table
|
|
|
|
}
|
|
|
|
|
2014-04-27 16:20:11 +02:00
|
|
|
/// Lua error handler which adds the stack trace to the error message, with
|
|
|
|
/// moonscript line rewriting support
|
2014-04-26 00:40:43 +02:00
|
|
|
int add_stack_trace(lua_State *L);
|
|
|
|
|
2012-11-26 19:46:03 +01:00
|
|
|
#ifdef _DEBUG
|
|
|
|
struct LuaStackcheck {
|
|
|
|
lua_State *L;
|
|
|
|
int startstack;
|
|
|
|
|
2014-04-26 00:40:43 +02:00
|
|
|
void check_stack(int additional);
|
|
|
|
void dump();
|
2012-11-26 19:46:03 +01:00
|
|
|
|
|
|
|
LuaStackcheck(lua_State *L) : L(L), startstack(lua_gettop(L)) { }
|
|
|
|
~LuaStackcheck() { check_stack(0); }
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
struct LuaStackcheck {
|
|
|
|
void check_stack(int) { }
|
|
|
|
void dump() { }
|
|
|
|
LuaStackcheck(lua_State*) { }
|
|
|
|
};
|
|
|
|
#endif
|
2014-03-27 03:24:37 +01:00
|
|
|
|
|
|
|
} }
|