2016-03-14 21:53:24 +08:00
|
|
|
// The MIT License (MIT)
|
2016-01-30 17:21:33 +08:00
|
|
|
|
2016-02-27 15:43:53 +08:00
|
|
|
// Copyright (c) 2013-2016 Rapptz, ThePhD and contributors
|
2016-01-30 17:21:33 +08:00
|
|
|
|
|
|
|
// 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_VIEW_HPP
|
|
|
|
#define SOL_STATE_VIEW_HPP
|
|
|
|
|
|
|
|
#include "error.hpp"
|
|
|
|
#include "table.hpp"
|
2016-04-24 06:18:02 +08:00
|
|
|
#include "load_result.hpp"
|
2016-01-30 17:21:33 +08:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace sol {
|
|
|
|
enum class lib : char {
|
|
|
|
base,
|
|
|
|
package,
|
|
|
|
coroutine,
|
|
|
|
string,
|
|
|
|
os,
|
|
|
|
math,
|
|
|
|
table,
|
|
|
|
debug,
|
|
|
|
bit32,
|
|
|
|
io,
|
2016-04-18 09:00:24 +08:00
|
|
|
ffi,
|
|
|
|
jit,
|
2016-01-30 17:21:33 +08:00
|
|
|
count
|
|
|
|
};
|
|
|
|
|
|
|
|
class state_view {
|
|
|
|
private:
|
|
|
|
lua_State* L;
|
|
|
|
table reg;
|
2016-03-12 00:34:44 +08:00
|
|
|
global_table global;
|
2016-05-20 05:27:12 +08:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void ensure_package(const std::string& key, T&& sr) {
|
|
|
|
auto pkg = (*this)["package"];
|
|
|
|
if (!pkg.valid()) {
|
|
|
|
pkg = create_table_with("loaded", create_table_with(key, sr));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
auto ld = pkg["loaded"];
|
|
|
|
if (!ld.valid()) {
|
|
|
|
ld = create_table_with(key, sr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ld[key] = sr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 17:21:33 +08:00
|
|
|
public:
|
2016-05-12 10:09:17 +08:00
|
|
|
typedef global_table::iterator iterator;
|
|
|
|
typedef global_table::const_iterator const_iterator;
|
2016-04-07 17:21:49 +08:00
|
|
|
|
2016-01-30 17:21:33 +08:00
|
|
|
state_view(lua_State* L):
|
|
|
|
L(L),
|
|
|
|
reg(L, LUA_REGISTRYINDEX),
|
2016-03-12 00:34:44 +08:00
|
|
|
global(L, detail::global_) {
|
2016-01-30 17:21:33 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_State* lua_state() const {
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
void open_libraries(Args&&... args) {
|
2016-02-24 12:39:46 +08:00
|
|
|
static_assert(meta::are_same<lib, Args...>::value, "all types must be libraries");
|
2016-01-30 17:21:33 +08:00
|
|
|
if(sizeof...(args) == 0) {
|
|
|
|
luaL_openlibs(L);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lib libraries[1 + sizeof...(args)] = { lib::count, std::forward<Args>(args)... };
|
|
|
|
|
|
|
|
for(auto&& library : libraries) {
|
|
|
|
switch(library) {
|
|
|
|
#if SOL_LUA_VERSION <= 501 && defined(SOL_LUAJIT)
|
|
|
|
case lib::coroutine:
|
|
|
|
#endif // luajit opens coroutine base stuff
|
|
|
|
case lib::base:
|
|
|
|
luaL_requiref(L, "base", luaopen_base, 1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
break;
|
|
|
|
case lib::package:
|
|
|
|
luaL_requiref(L, "package", luaopen_package, 1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
break;
|
2016-03-30 15:24:07 +08:00
|
|
|
#if !defined(SOL_LUAJIT)
|
2016-01-30 17:21:33 +08:00
|
|
|
case lib::coroutine:
|
2016-03-30 15:24:07 +08:00
|
|
|
#if SOL_LUA_VERSION > 501
|
2016-01-30 17:21:33 +08:00
|
|
|
luaL_requiref(L, "coroutine", luaopen_coroutine, 1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
#endif // Lua 5.2+ only
|
2016-03-30 15:24:07 +08:00
|
|
|
break;
|
|
|
|
#endif // Not LuaJIT
|
2016-01-30 17:21:33 +08:00
|
|
|
case lib::string:
|
|
|
|
luaL_requiref(L, "string", luaopen_string, 1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
break;
|
|
|
|
case lib::table:
|
|
|
|
luaL_requiref(L, "table", luaopen_table, 1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
break;
|
|
|
|
case lib::math:
|
|
|
|
luaL_requiref(L, "math", luaopen_math, 1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
break;
|
|
|
|
case lib::bit32:
|
2016-04-18 09:14:33 +08:00
|
|
|
#ifdef SOL_LUAJIT
|
2016-04-18 09:00:24 +08:00
|
|
|
luaL_requiref(L, "bit32", luaopen_bit, 1);
|
|
|
|
lua_pop(L, 1);
|
2016-04-18 09:14:33 +08:00
|
|
|
#elif SOL_LUA_VERSION == 502
|
|
|
|
luaL_requiref(L, "bit32", luaopen_bit32, 1);
|
|
|
|
lua_pop(L, 1);
|
2016-01-30 17:21:33 +08:00
|
|
|
#else
|
2016-04-18 09:14:33 +08:00
|
|
|
#endif // Lua 5.2 only (deprecated in 5.3 (503))
|
2016-01-30 17:21:33 +08:00
|
|
|
break;
|
|
|
|
case lib::io:
|
|
|
|
luaL_requiref(L, "io", luaopen_io, 1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
break;
|
|
|
|
case lib::os:
|
|
|
|
luaL_requiref(L, "os", luaopen_os, 1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
break;
|
|
|
|
case lib::debug:
|
|
|
|
luaL_requiref(L, "debug", luaopen_debug, 1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
break;
|
2016-04-24 05:40:22 +08:00
|
|
|
case lib::ffi:
|
2016-04-18 09:00:24 +08:00
|
|
|
#ifdef SOL_LUAJIT
|
|
|
|
luaL_requiref(L, "ffi", luaopen_ffi, 1);
|
2016-05-20 05:27:12 +08:00
|
|
|
lua_pop(L, 1);
|
2016-04-18 09:00:24 +08:00
|
|
|
#endif
|
|
|
|
break;
|
2016-04-24 05:40:22 +08:00
|
|
|
case lib::jit:
|
2016-04-18 09:00:24 +08:00
|
|
|
#ifdef SOL_LUAJIT
|
|
|
|
luaL_requiref(L, "jit", luaopen_jit, 1);
|
2016-05-20 05:27:12 +08:00
|
|
|
lua_pop(L, 1);
|
2016-04-18 09:00:24 +08:00
|
|
|
#endif
|
|
|
|
break;
|
2016-01-30 17:21:33 +08:00
|
|
|
case lib::count:
|
2016-03-30 15:25:22 +08:00
|
|
|
default:
|
2016-01-30 17:21:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 05:27:12 +08:00
|
|
|
template <typename Fx>
|
|
|
|
object require(const std::string& key, Fx&& open_function, bool is_global_library = true) {
|
|
|
|
auto openfx = [fx = std::forward<Fx>(open_function)](lua_State* L){
|
|
|
|
typedef lua_bind_traits<meta::unqualified_t<Fx>> traits;
|
|
|
|
return stack::call(typename traits::return_type(), typename traits::args_type(), L, fx);
|
|
|
|
};
|
|
|
|
stack::push(L, function_args<function_sig<>>(std::forward<Fx>(openfx)));
|
|
|
|
lua_CFunction openf = stack::pop<lua_CFunction>(L);
|
|
|
|
luaL_requiref(L, key.c_str(), openf, is_global_library ? 1 : 0);
|
|
|
|
object r stack::pop<object>(L);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
object require_script(const std::string& key, const std::string& code) {
|
|
|
|
optional<object> loaded = traverse_get<optional<object>>("package", "loaded", key);
|
|
|
|
bool ismod = loaded && !(loaded->is<bool>() && !loaded->as<bool>());
|
|
|
|
if (ismod)
|
|
|
|
return std::move(*loaded);
|
|
|
|
script(code);
|
|
|
|
auto sr = stack::get<stack_reference>(L);
|
|
|
|
set(key, sr);
|
|
|
|
ensure_package(key, sr);
|
|
|
|
return stack::pop<object>(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
object require_file(const std::string& key, const std::string& file) {
|
|
|
|
auto loaded = traverse_get<optional<object>>("package", "loaded", key);
|
|
|
|
bool ismod = loaded && !(loaded->is<bool>() && !loaded->as<bool>());
|
|
|
|
if (loaded)
|
|
|
|
return std::move(*loaded);
|
|
|
|
script_file(file);
|
|
|
|
auto sr = stack::get<stack_reference>(L);
|
|
|
|
set(key, sr);
|
|
|
|
ensure_package(key, sr);
|
|
|
|
return stack::pop<object>(L);
|
|
|
|
}
|
|
|
|
|
2016-01-30 17:21:33 +08:00
|
|
|
void script(const std::string& code) {
|
|
|
|
if(luaL_dostring(L, code.c_str())) {
|
|
|
|
lua_error(L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 00:34:44 +08:00
|
|
|
void script_file(const std::string& filename) {
|
2016-01-30 17:21:33 +08:00
|
|
|
if(luaL_dofile(L, filename.c_str())) {
|
|
|
|
lua_error(L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-24 06:18:02 +08:00
|
|
|
load_result load(const std::string& code) {
|
|
|
|
load_status x = static_cast<load_status>(luaL_loadstring(L, code.c_str()));
|
|
|
|
return load_result(L, -1, 1, 1, x);
|
2016-04-24 05:07:51 +08:00
|
|
|
}
|
|
|
|
|
2016-04-24 06:18:02 +08:00
|
|
|
load_result load_file(const std::string& filename) {
|
|
|
|
load_status x = static_cast<load_status>(luaL_loadfile(L, filename.c_str()));
|
|
|
|
return load_result(L, -1, 1, 1, x);
|
2016-04-24 05:07:51 +08:00
|
|
|
}
|
|
|
|
|
2016-05-06 04:14:13 +08:00
|
|
|
load_result load_buffer(const char *buff, size_t size, const char *name, const char* mode = nullptr) {
|
|
|
|
load_status x = static_cast<load_status>(luaL_loadbufferx(L, buff, size, name, mode));
|
|
|
|
return load_result(L, -1, 1, 1, x);
|
|
|
|
}
|
2016-05-05 21:12:25 +08:00
|
|
|
|
2016-04-07 17:21:49 +08:00
|
|
|
iterator begin () const {
|
2016-03-12 00:34:44 +08:00
|
|
|
return global.begin();
|
|
|
|
}
|
|
|
|
|
2016-04-07 17:21:49 +08:00
|
|
|
iterator end() const {
|
2016-03-12 00:34:44 +08:00
|
|
|
return global.end();
|
|
|
|
}
|
|
|
|
|
2016-04-07 17:21:49 +08:00
|
|
|
const_iterator cbegin() const {
|
2016-03-12 00:34:44 +08:00
|
|
|
return global.cbegin();
|
|
|
|
}
|
|
|
|
|
2016-04-07 17:21:49 +08:00
|
|
|
const_iterator cend() const {
|
2016-03-12 00:34:44 +08:00
|
|
|
return global.cend();
|
|
|
|
}
|
|
|
|
|
|
|
|
global_table globals() const {
|
|
|
|
return global;
|
|
|
|
}
|
|
|
|
|
|
|
|
table registry() const {
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
2016-04-12 06:07:25 +08:00
|
|
|
operator lua_State* () const {
|
|
|
|
return lua_state();
|
|
|
|
}
|
|
|
|
|
2016-03-12 00:34:44 +08:00
|
|
|
void set_panic(lua_CFunction panic){
|
|
|
|
lua_atpanic(L, panic);
|
|
|
|
}
|
|
|
|
|
2016-01-30 17:21:33 +08:00
|
|
|
template<typename... Args, typename... Keys>
|
2016-02-14 09:14:31 +08:00
|
|
|
decltype(auto) get(Keys&&... keys) const {
|
2016-03-12 00:34:44 +08:00
|
|
|
return global.get<Args...>(std::forward<Keys>(keys)...);
|
2016-01-30 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
2016-03-30 13:40:40 +08:00
|
|
|
template<typename T, typename Key>
|
|
|
|
decltype(auto) get_or(Key&& key, T&& otherwise) const {
|
|
|
|
return global.get_or(std::forward<Key>(key), std::forward<T>(otherwise));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename Key, typename D>
|
|
|
|
decltype(auto) get_or(Key&& key, D&& otherwise) const {
|
|
|
|
return global.get_or<T>(std::forward<Key>(key), std::forward<D>(otherwise));
|
|
|
|
}
|
|
|
|
|
2016-02-09 16:38:11 +08:00
|
|
|
template<typename... Args>
|
|
|
|
state_view& set(Args&&... args) {
|
2016-03-12 00:34:44 +08:00
|
|
|
global.set(std::forward<Args>(args)...);
|
2016-01-30 17:21:33 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template<typename T, typename... Keys>
|
|
|
|
decltype(auto) traverse_get(Keys&&... keys) const {
|
2016-03-12 00:34:44 +08:00
|
|
|
return global.traverse_get<T>(std::forward<Keys>(keys)...);
|
2016-01-30 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template<typename... Args>
|
|
|
|
state_view& traverse_set(Args&&... args) {
|
2016-03-12 00:34:44 +08:00
|
|
|
global.traverse_set(std::forward<Args>(args)...);
|
2016-02-14 09:14:31 +08:00
|
|
|
return *this;
|
2016-01-30 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
state_view& set_usertype(usertype<T>& user) {
|
|
|
|
return set_usertype(usertype_traits<T>::name, user);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, typename T>
|
|
|
|
state_view& set_usertype(Key&& key, usertype<T>& user) {
|
2016-03-12 00:34:44 +08:00
|
|
|
global.set_usertype(std::forward<Key>(key), user);
|
2016-01-30 17:21:33 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-02-22 08:26:58 +08:00
|
|
|
template<typename Class, typename... Args>
|
2016-01-30 17:21:33 +08:00
|
|
|
state_view& new_usertype(const std::string& name, Args&&... args) {
|
2016-03-12 00:34:44 +08:00
|
|
|
global.new_usertype<Class>(name, std::forward<Args>(args)...);
|
2016-02-22 08:26:58 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Class, typename CTor0, typename... CTor, typename... Args>
|
|
|
|
state_view& new_usertype(const std::string& name, Args&&... args) {
|
2016-03-12 00:34:44 +08:00
|
|
|
global.new_usertype<Class, CTor0, CTor...>(name, std::forward<Args>(args)...);
|
|
|
|
return *this;
|
2016-01-30 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Class, typename... CArgs, typename... Args>
|
|
|
|
state_view& new_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
|
2016-03-12 00:34:44 +08:00
|
|
|
global.new_usertype<Class>(name, ctor, std::forward<Args>(args)...);
|
2016-01-30 17:21:33 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Fx>
|
|
|
|
void for_each(Fx&& fx) {
|
2016-03-12 00:34:44 +08:00
|
|
|
global.for_each(std::forward<Fx>(fx));
|
2016-01-30 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
proxy<global_table&, T> operator[](T&& key) {
|
2016-03-12 00:34:44 +08:00
|
|
|
return global[std::forward<T>(key)];
|
2016-01-30 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-03-12 00:34:44 +08:00
|
|
|
proxy<const global_table&, T> operator[](T&& key) const {
|
|
|
|
return global[std::forward<T>(key)];
|
2016-01-30 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
2016-03-30 12:31:18 +08:00
|
|
|
template<typename Sig, typename... Args, typename Key>
|
|
|
|
state_view& set_function(Key&& key, Args&&... args) {
|
|
|
|
global.set_function<Sig>(std::forward<Key>(key), std::forward<Args>(args)...);
|
2016-01-30 17:21:33 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-03-30 12:31:18 +08:00
|
|
|
template<typename... Args, typename Key>
|
|
|
|
state_view& set_function(Key&& key, Args&&... args) {
|
|
|
|
global.set_function(std::forward<Key>(key), std::forward<Args>(args)...);
|
2016-01-30 17:21:33 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2016-03-12 00:34:44 +08:00
|
|
|
|
2016-03-15 19:32:59 +08:00
|
|
|
template <typename Name>
|
|
|
|
table create_table(Name&& name, int narr = 0, int nrec = 0) {
|
|
|
|
return global.create(std::forward<Name>(name), narr, nrec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Name, typename Key, typename Value, typename... Args>
|
|
|
|
table create_table(Name&& name, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
|
|
|
|
return global.create(std::forward<Name>(name), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2016-03-30 12:31:18 +08:00
|
|
|
template <typename Name, typename... Args>
|
|
|
|
table create_named_table(Name&& name, Args&&... args) {
|
|
|
|
table x = global.create_with(std::forward<Args>(args)...);
|
2016-04-01 04:16:07 +08:00
|
|
|
global.set(std::forward<Name>(name), x);
|
|
|
|
return x;
|
2016-03-30 12:31:18 +08:00
|
|
|
}
|
|
|
|
|
2016-03-15 04:10:08 +08:00
|
|
|
table create_table(int narr = 0, int nrec = 0) {
|
|
|
|
return create_table(lua_state(), narr, nrec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Key, typename Value, typename... Args>
|
|
|
|
table create_table(int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
|
|
|
|
return create_table(lua_state(), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2016-03-25 03:45:44 +08:00
|
|
|
template <typename... Args>
|
|
|
|
table create_table_with(Args&&... args) {
|
|
|
|
return create_table_with(lua_state(), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2016-03-12 00:34:44 +08:00
|
|
|
static inline table create_table(lua_State* L, int narr = 0, int nrec = 0) {
|
|
|
|
return global_table::create(L, narr, nrec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Key, typename Value, typename... Args>
|
|
|
|
static inline table create_table(lua_State* L, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
|
|
|
|
return global_table::create(L, narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
|
|
|
|
}
|
2016-03-25 03:45:44 +08:00
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
static inline table create_table_with(lua_State* L, Args&&... args) {
|
|
|
|
return global_table::create_with(L, std::forward<Args>(args)...);
|
|
|
|
}
|
2016-01-30 17:21:33 +08:00
|
|
|
};
|
|
|
|
} // sol
|
|
|
|
|
|
|
|
#endif // SOL_STATE_VIEW_HPP
|