2013-12-03 03:22:51 +08:00
|
|
|
// The MIT License (MIT)
|
|
|
|
|
|
|
|
// Copyright (c) 2013 Danny Y., Rapptz
|
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef SOL_STATE_HPP
|
|
|
|
#define SOL_STATE_HPP
|
|
|
|
|
|
|
|
#include "error.hpp"
|
|
|
|
#include "table.hpp"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace sol {
|
|
|
|
namespace detail {
|
2014-04-24 20:15:12 +08:00
|
|
|
inline int atpanic(lua_State* L) {
|
2014-12-18 05:57:14 +08:00
|
|
|
const char* message = lua_tostring(L, -1);
|
|
|
|
std::string err = message ? message : "An unexpected error occured and forced the lua state to call atpanic";
|
2014-05-22 10:24:15 +08:00
|
|
|
throw error(err);
|
2013-12-03 03:22:51 +08:00
|
|
|
}
|
|
|
|
} // detail
|
|
|
|
|
|
|
|
enum class lib : char {
|
|
|
|
base,
|
|
|
|
package,
|
|
|
|
coroutine,
|
|
|
|
string,
|
|
|
|
os,
|
|
|
|
math,
|
|
|
|
table,
|
|
|
|
debug,
|
|
|
|
bit32,
|
|
|
|
io,
|
|
|
|
count
|
|
|
|
};
|
|
|
|
|
|
|
|
class state {
|
|
|
|
private:
|
|
|
|
std::unique_ptr<lua_State, void(*)(lua_State*)> L;
|
|
|
|
table reg;
|
|
|
|
table global;
|
|
|
|
public:
|
2014-04-27 16:11:30 +08:00
|
|
|
state():
|
|
|
|
L(luaL_newstate(), lua_close),
|
|
|
|
reg(L.get(), LUA_REGISTRYINDEX),
|
2013-12-03 03:22:51 +08:00
|
|
|
global(reg.get<table>(LUA_RIDX_GLOBALS)) {
|
|
|
|
lua_atpanic(L.get(), detail::atpanic);
|
|
|
|
}
|
2014-04-27 16:11:30 +08:00
|
|
|
|
2014-06-29 14:16:48 +08:00
|
|
|
lua_State* lua_state() const {
|
|
|
|
return L.get();
|
|
|
|
}
|
|
|
|
|
2013-12-03 03:22:51 +08:00
|
|
|
template<typename... Args>
|
|
|
|
void open_libraries(Args&&... args) {
|
2014-05-31 07:10:08 +08:00
|
|
|
static_assert(are_same<lib, Args...>::value, "all types must be libraries");
|
2013-12-03 03:22:51 +08:00
|
|
|
if(sizeof...(args) == 0) {
|
|
|
|
luaL_openlibs(L.get());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lib libraries[1 + sizeof...(args)] = { lib::count, std::forward<Args>(args)... };
|
|
|
|
|
|
|
|
for(auto&& library : libraries) {
|
|
|
|
switch(library) {
|
|
|
|
case lib::base:
|
|
|
|
luaL_requiref(L.get(), "base", luaopen_base, 1);
|
2014-07-28 03:56:24 +08:00
|
|
|
lua_pop(L.get(), 1);
|
2013-12-03 03:22:51 +08:00
|
|
|
break;
|
|
|
|
case lib::package:
|
|
|
|
luaL_requiref(L.get(), "package", luaopen_package, 1);
|
2014-07-28 03:56:24 +08:00
|
|
|
lua_pop(L.get(), 1);
|
2013-12-03 03:22:51 +08:00
|
|
|
break;
|
|
|
|
case lib::coroutine:
|
|
|
|
luaL_requiref(L.get(), "coroutine", luaopen_coroutine, 1);
|
2014-07-28 03:56:24 +08:00
|
|
|
lua_pop(L.get(), 1);
|
2013-12-03 03:22:51 +08:00
|
|
|
break;
|
|
|
|
case lib::string:
|
|
|
|
luaL_requiref(L.get(), "string", luaopen_string, 1);
|
2014-07-28 03:56:24 +08:00
|
|
|
lua_pop(L.get(), 1);
|
2013-12-03 03:22:51 +08:00
|
|
|
break;
|
|
|
|
case lib::table:
|
|
|
|
luaL_requiref(L.get(), "table", luaopen_table, 1);
|
2014-07-28 03:56:24 +08:00
|
|
|
lua_pop(L.get(), 1);
|
2013-12-03 03:22:51 +08:00
|
|
|
break;
|
|
|
|
case lib::math:
|
|
|
|
luaL_requiref(L.get(), "math", luaopen_math, 1);
|
2014-07-28 03:56:24 +08:00
|
|
|
lua_pop(L.get(), 1);
|
2013-12-03 03:22:51 +08:00
|
|
|
break;
|
|
|
|
case lib::bit32:
|
|
|
|
luaL_requiref(L.get(), "bit32", luaopen_bit32, 1);
|
2014-07-28 03:56:24 +08:00
|
|
|
lua_pop(L.get(), 1);
|
2013-12-03 03:22:51 +08:00
|
|
|
break;
|
|
|
|
case lib::io:
|
|
|
|
luaL_requiref(L.get(), "io", luaopen_io, 1);
|
2014-07-28 03:56:24 +08:00
|
|
|
lua_pop(L.get(), 1);
|
2013-12-03 03:22:51 +08:00
|
|
|
break;
|
|
|
|
case lib::os:
|
|
|
|
luaL_requiref(L.get(), "os", luaopen_os, 1);
|
2014-07-28 03:56:24 +08:00
|
|
|
lua_pop(L.get(), 1);
|
2013-12-03 03:22:51 +08:00
|
|
|
break;
|
|
|
|
case lib::debug:
|
|
|
|
luaL_requiref(L.get(), "debug", luaopen_debug, 1);
|
2014-07-28 03:56:24 +08:00
|
|
|
lua_pop(L.get(), 1);
|
2013-12-03 03:22:51 +08:00
|
|
|
break;
|
|
|
|
case lib::count:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void script(const std::string& code) {
|
|
|
|
if(luaL_dostring(L.get(), code.c_str())) {
|
|
|
|
lua_error(L.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 18:31:35 +08:00
|
|
|
void open_file(const std::string& filename) {
|
2014-08-11 08:49:34 +08:00
|
|
|
if(luaL_dofile(L.get(), filename.c_str())) {
|
2013-12-03 03:22:51 +08:00
|
|
|
lua_error(L.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-16 11:09:06 +08:00
|
|
|
template<typename... Args, typename... Keys>
|
2014-05-09 22:48:55 +08:00
|
|
|
auto get(Keys&&... keys) const
|
|
|
|
-> decltype(global.get(types<Args...>(), std::forward<Keys>(keys)...)) {
|
2013-12-16 11:09:06 +08:00
|
|
|
return global.get(types<Args...>(), std::forward<Keys>(keys)...);
|
2013-12-03 03:22:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename U>
|
|
|
|
state& set(T&& key, U&& value) {
|
|
|
|
global.set(std::forward<T>(key), std::forward<U>(value));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-09-19 23:08:44 +08:00
|
|
|
template<typename T>
|
|
|
|
SOL_DEPRECATED table& set_userdata(usertype<T>& user) {
|
|
|
|
return set_usertype(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, typename T>
|
|
|
|
SOL_DEPRECATED table& set_userdata(Key&& key, usertype<T>& user) {
|
|
|
|
return set_usertype(std::forward<Key>(key), user);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Class, typename... CTor, typename... Args>
|
|
|
|
SOL_DEPRECATED state& new_userdata(const std::string& name, Args&&... args) {
|
|
|
|
return new_usertype<Class>(name, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Class, typename... CArgs, typename... Args>
|
|
|
|
SOL_DEPRECATED state& new_userdata(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
|
|
|
|
return new_usertype(name, std::move(ctor), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2014-04-26 08:20:35 +08:00
|
|
|
template<typename T>
|
2014-09-19 20:22:21 +08:00
|
|
|
state& set_usertype(usertype<T>& user) {
|
|
|
|
return set_usertype(usertype_traits<T>::name, user);
|
2014-06-29 14:16:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, typename T>
|
2014-09-19 20:22:21 +08:00
|
|
|
state& set_usertype(Key&& key, usertype<T>& user) {
|
|
|
|
global.set_usertype(std::forward<Key>(key), user);
|
2013-12-03 08:15:23 +08:00
|
|
|
return *this;
|
2013-12-03 03:22:51 +08:00
|
|
|
}
|
|
|
|
|
2014-04-27 17:09:28 +08:00
|
|
|
template<typename Class, typename... CTor, typename... Args>
|
2014-09-19 20:22:21 +08:00
|
|
|
state& new_usertype(const std::string& name, Args&&... args) {
|
2014-06-28 12:27:48 +08:00
|
|
|
constructors<types<CTor...>> ctor{};
|
2014-09-19 20:22:21 +08:00
|
|
|
return new_usertype<Class>(name, ctor, std::forward<Args>(args)...);
|
2014-06-28 12:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Class, typename... CArgs, typename... Args>
|
2014-09-19 20:22:21 +08:00
|
|
|
state& new_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
|
|
|
|
usertype<Class> utype(ctor, std::forward<Args>(args)...);
|
|
|
|
set_usertype(name, utype);
|
2014-04-27 17:09:28 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-12-03 03:22:51 +08:00
|
|
|
template<typename T>
|
|
|
|
table create_table(T&& key, int narr = 0, int nrec = 0) {
|
2013-12-12 18:12:45 +08:00
|
|
|
lua_createtable(L.get(), narr, nrec);
|
2013-12-03 03:22:51 +08:00
|
|
|
table result(L.get());
|
|
|
|
lua_pop(L.get(), 1);
|
|
|
|
global.set(std::forward<T>(key), result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
table create_table(int narr = 0, int nrec = 0) {
|
2013-12-12 18:12:45 +08:00
|
|
|
lua_createtable(L.get(), narr, nrec);
|
2013-12-03 03:22:51 +08:00
|
|
|
table result(L.get());
|
|
|
|
lua_pop(L.get(), 1);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
table global_table() const {
|
|
|
|
return global;
|
|
|
|
}
|
|
|
|
|
|
|
|
table registry() const {
|
|
|
|
return reg;
|
|
|
|
}
|
2013-12-22 08:30:30 +08:00
|
|
|
|
2014-04-27 06:23:56 +08:00
|
|
|
template<typename T>
|
2013-12-22 11:11:20 +08:00
|
|
|
proxy<table, T> operator[](T&& key) {
|
|
|
|
return global[std::forward<T>(key)];
|
2013-12-22 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2014-04-27 06:23:56 +08:00
|
|
|
template<typename T>
|
2013-12-22 11:11:20 +08:00
|
|
|
proxy<const table, T> operator[](T&& key) const {
|
|
|
|
return global[std::forward<T>(key)];
|
2013-12-22 08:30:30 +08:00
|
|
|
}
|
2014-01-26 13:25:46 +08:00
|
|
|
|
2014-06-29 14:16:48 +08:00
|
|
|
template<typename... Args, typename R, typename Key>
|
|
|
|
state& set_function(Key&& key, R fun_ptr(Args...)){
|
|
|
|
global.set_function(std::forward<Key>(key), fun_ptr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Sig, typename Key>
|
|
|
|
state& set_function(Key&& key, Sig* fun_ptr){
|
|
|
|
global.set_function(std::forward<Key>(key), fun_ptr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args, typename R, typename C, typename T, typename Key>
|
|
|
|
state& set_function(Key&& key, R (C::*mem_ptr)(Args...), T&& obj) {
|
|
|
|
global.set_function(std::forward<Key>(key), mem_ptr, std::forward<T>(obj));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Sig, typename C, typename T, typename Key>
|
|
|
|
state& set_function(Key&& key, Sig C::* mem_ptr, T&& obj) {
|
|
|
|
global.set_function(std::forward<Key>(key), mem_ptr, std::forward<T>(obj));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Sig, typename Fx, typename Key>
|
|
|
|
state& set_function(Key&& key, Fx&& fx) {
|
|
|
|
global.set_function<Sig...>(std::forward<Key>(key), std::forward<Fx>(fx));
|
|
|
|
return *this;
|
2014-01-26 13:25:46 +08:00
|
|
|
}
|
2013-12-03 03:22:51 +08:00
|
|
|
};
|
|
|
|
} // sol
|
|
|
|
|
2014-05-31 03:30:14 +08:00
|
|
|
#endif // SOL_STATE_HPP
|