2018-09-28 13:27:38 +08:00
|
|
|
// sol3
|
2017-12-20 17:58:32 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
// The MIT License (MIT)
|
2017-04-03 04:10:00 +08:00
|
|
|
|
2018-02-20 10:15:26 +08:00
|
|
|
// Copyright (c) 2013-2018 Rapptz, ThePhD and contributors
|
2017-04-03 04:10:00 +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_ENVIRONMENT_HPP
|
|
|
|
#define SOL_ENVIRONMENT_HPP
|
|
|
|
|
|
|
|
#include "table.hpp"
|
|
|
|
|
|
|
|
namespace sol {
|
|
|
|
|
|
|
|
template <typename base_type>
|
|
|
|
struct basic_environment : basic_table<base_type> {
|
|
|
|
private:
|
2017-04-20 01:00:47 +08:00
|
|
|
typedef basic_table<base_type> base_t;
|
|
|
|
|
2017-04-03 04:10:00 +08:00
|
|
|
public:
|
2017-08-06 07:20:28 +08:00
|
|
|
using base_t::lua_state;
|
|
|
|
|
2017-04-03 04:10:00 +08:00
|
|
|
basic_environment() noexcept = default;
|
|
|
|
basic_environment(const basic_environment&) = default;
|
|
|
|
basic_environment(basic_environment&&) = default;
|
|
|
|
basic_environment& operator=(const basic_environment&) = default;
|
|
|
|
basic_environment& operator=(basic_environment&&) = default;
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_environment(const stack_reference& r)
|
|
|
|
: basic_environment(r.lua_state(), r.stack_index()) {
|
|
|
|
}
|
|
|
|
basic_environment(stack_reference&& r)
|
|
|
|
: basic_environment(r.lua_state(), r.stack_index()) {
|
|
|
|
}
|
2017-04-03 04:10:00 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_environment(lua_State* L, new_table nt)
|
|
|
|
: base_t(L, std::move(nt)) {
|
|
|
|
}
|
2017-09-14 12:35:40 +08:00
|
|
|
template <bool b>
|
|
|
|
basic_environment(lua_State* L, new_table t, const basic_reference<b>& fallback)
|
2017-09-13 14:46:56 +08:00
|
|
|
: basic_environment(L, std::move(t)) {
|
2017-09-14 12:35:40 +08:00
|
|
|
stack_table mt(L, new_table(0, 1));
|
|
|
|
mt.set(meta_function::index, fallback);
|
2017-04-03 04:10:00 +08:00
|
|
|
this->set(metatable_key, mt);
|
|
|
|
mt.pop();
|
|
|
|
}
|
2017-04-20 01:00:47 +08:00
|
|
|
|
2019-02-17 06:27:03 +08:00
|
|
|
basic_environment(env_key_t, const stack_reference& extraction_target)
|
2017-09-13 14:46:56 +08:00
|
|
|
: base_t(detail::no_safety, extraction_target.lua_state(), (stack::push_environment_of(extraction_target), -1)) {
|
2018-04-18 00:29:03 +08:00
|
|
|
#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
|
2017-09-01 08:47:09 +08:00
|
|
|
constructor_handler handler{};
|
2019-02-17 06:27:03 +08:00
|
|
|
stack::check<env_key_t>(this->lua_state(), -1, handler);
|
2017-04-20 01:00:47 +08:00
|
|
|
#endif // Safety
|
|
|
|
lua_pop(this->lua_state(), 2);
|
|
|
|
}
|
2017-09-14 12:35:40 +08:00
|
|
|
template <bool b>
|
2019-02-17 06:27:03 +08:00
|
|
|
basic_environment(env_key_t, const basic_reference<b>& extraction_target)
|
2017-09-13 14:46:56 +08:00
|
|
|
: base_t(detail::no_safety, extraction_target.lua_state(), (stack::push_environment_of(extraction_target), -1)) {
|
2018-04-18 00:29:03 +08:00
|
|
|
#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
|
2017-09-01 08:47:09 +08:00
|
|
|
constructor_handler handler{};
|
2019-02-17 06:27:03 +08:00
|
|
|
stack::check<env_key_t>(this->lua_state(), -1, handler);
|
2017-04-20 01:00:47 +08:00
|
|
|
#endif // Safety
|
|
|
|
lua_pop(this->lua_state(), 2);
|
|
|
|
}
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_environment(lua_State* L, int index = -1)
|
|
|
|
: base_t(detail::no_safety, L, index) {
|
2018-04-18 00:29:03 +08:00
|
|
|
#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
|
2017-09-01 08:47:09 +08:00
|
|
|
constructor_handler handler{};
|
|
|
|
stack::check<basic_environment>(L, index, handler);
|
2017-04-20 01:00:47 +08:00
|
|
|
#endif // Safety
|
|
|
|
}
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_environment(lua_State* L, ref_index index)
|
|
|
|
: base_t(detail::no_safety, L, index) {
|
2018-04-18 00:29:03 +08:00
|
|
|
#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
|
2017-04-20 01:00:47 +08:00
|
|
|
auto pp = stack::push_pop(*this);
|
2017-09-01 08:47:09 +08:00
|
|
|
constructor_handler handler{};
|
|
|
|
stack::check<basic_environment>(L, -1, handler);
|
2017-04-20 01:00:47 +08:00
|
|
|
#endif // Safety
|
|
|
|
}
|
2018-01-29 11:21:13 +08:00
|
|
|
template <typename T, meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_environment>>, meta::neg<std::is_same<base_type, stack_reference>>, meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_environment(T&& r) noexcept
|
|
|
|
: base_t(detail::no_safety, std::forward<T>(r)) {
|
2018-04-18 00:29:03 +08:00
|
|
|
#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
|
2017-04-20 01:00:47 +08:00
|
|
|
if (!is_environment<meta::unqualified_t<T>>::value) {
|
|
|
|
auto pp = stack::push_pop(*this);
|
2017-09-01 08:47:09 +08:00
|
|
|
constructor_handler handler{};
|
|
|
|
stack::check<basic_environment>(lua_state(), -1, handler);
|
2017-04-20 01:00:47 +08:00
|
|
|
}
|
|
|
|
#endif // Safety
|
|
|
|
}
|
2018-01-29 11:21:13 +08:00
|
|
|
basic_environment(lua_nil_t r) noexcept
|
|
|
|
: base_t(detail::no_safety, r) {
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:45:44 +08:00
|
|
|
template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
|
|
|
|
basic_environment(lua_State* L, T&& r) noexcept
|
|
|
|
: base_t(detail::no_safety, L, std::forward<T>(r)) {
|
2018-04-18 00:29:03 +08:00
|
|
|
#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
|
2017-09-14 20:45:44 +08:00
|
|
|
if (!is_environment<meta::unqualified_t<T>>::value) {
|
|
|
|
auto pp = stack::push_pop(*this);
|
|
|
|
constructor_handler handler{};
|
|
|
|
stack::check<basic_environment>(lua_state(), -1, handler);
|
|
|
|
}
|
|
|
|
#endif // Safety
|
|
|
|
}
|
2017-04-03 04:10:00 +08:00
|
|
|
|
2017-04-03 10:37:01 +08:00
|
|
|
template <typename T>
|
|
|
|
void set_on(const T& target) const {
|
|
|
|
lua_State* L = target.lua_state();
|
2017-04-19 08:23:20 +08:00
|
|
|
auto pp = stack::push_pop(target);
|
2017-04-03 04:10:00 +08:00
|
|
|
#if SOL_LUA_VERSION < 502
|
2017-04-03 10:37:01 +08:00
|
|
|
// Use lua_setfenv
|
|
|
|
this->push();
|
|
|
|
lua_setfenv(L, -2);
|
2017-04-03 04:10:00 +08:00
|
|
|
#else
|
2017-04-03 10:37:01 +08:00
|
|
|
// Use upvalues as explained in Lua 5.2 and beyond's manual
|
|
|
|
this->push();
|
2017-05-10 01:24:56 +08:00
|
|
|
const char* name = lua_setupvalue(L, -2, 1);
|
|
|
|
if (name == nullptr) {
|
2017-04-03 10:37:01 +08:00
|
|
|
this->pop();
|
|
|
|
}
|
2017-04-03 04:10:00 +08:00
|
|
|
#endif
|
2017-04-03 10:37:01 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename E>
|
|
|
|
void set_environment(const basic_environment<E>& env, const T& target) {
|
|
|
|
env.set_on(target);
|
2017-04-03 04:10:00 +08:00
|
|
|
}
|
|
|
|
|
2017-04-19 08:23:20 +08:00
|
|
|
template <typename E = reference, typename T>
|
|
|
|
basic_environment<E> get_environment(const T& target) {
|
|
|
|
lua_State* L = target.lua_state();
|
|
|
|
auto pp = stack::pop_n(L, stack::push_environment_of(target));
|
|
|
|
return basic_environment<E>(L, -1);
|
|
|
|
}
|
|
|
|
|
2017-05-10 01:24:56 +08:00
|
|
|
struct this_environment {
|
|
|
|
optional<environment> env;
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
this_environment()
|
|
|
|
: env(nullopt) {
|
|
|
|
}
|
2017-09-14 12:35:40 +08:00
|
|
|
this_environment(environment e)
|
2017-09-13 14:46:56 +08:00
|
|
|
: env(std::move(e)) {
|
|
|
|
}
|
2017-05-10 01:24:56 +08:00
|
|
|
this_environment(const this_environment&) = default;
|
|
|
|
this_environment(this_environment&&) = default;
|
|
|
|
this_environment& operator=(const this_environment&) = default;
|
|
|
|
this_environment& operator=(this_environment&&) = default;
|
|
|
|
|
|
|
|
explicit operator bool() const {
|
|
|
|
return static_cast<bool>(env);
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
operator optional<environment>&() {
|
2017-05-10 01:24:56 +08:00
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
operator const optional<environment>&() const {
|
2017-05-10 01:24:56 +08:00
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
operator environment&() {
|
2017-05-10 01:24:56 +08:00
|
|
|
return env.value();
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
operator const environment&() const {
|
2017-05-10 01:24:56 +08:00
|
|
|
return env.value();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-19 08:23:20 +08:00
|
|
|
namespace stack {
|
|
|
|
template <>
|
2019-02-17 06:27:03 +08:00
|
|
|
struct unqualified_getter<env_key_t> {
|
2017-05-10 01:24:56 +08:00
|
|
|
static environment get(lua_State* L, int index, record& tracking) {
|
|
|
|
tracking.use(1);
|
2017-04-19 08:23:20 +08:00
|
|
|
return get_environment(stack_reference(L, raw_index(index)));
|
|
|
|
}
|
|
|
|
};
|
2017-05-10 01:24:56 +08:00
|
|
|
|
|
|
|
template <>
|
2018-12-20 12:17:15 +08:00
|
|
|
struct unqualified_getter<this_environment> {
|
2017-05-10 01:31:44 +08:00
|
|
|
static this_environment get(lua_State* L, int, record& tracking) {
|
2017-05-10 01:24:56 +08:00
|
|
|
tracking.use(0);
|
|
|
|
lua_Debug info;
|
|
|
|
// Level 0 means current function (this C function, which may or may not be useful for us?)
|
|
|
|
// Level 1 means next call frame up the stack. (Can be nothing if function called directly from C++ with lua_p/call)
|
|
|
|
int pre_stack_size = lua_gettop(L);
|
|
|
|
if (lua_getstack(L, 1, &info) != 1) {
|
|
|
|
if (lua_getstack(L, 0, &info) != 1) {
|
|
|
|
lua_settop(L, pre_stack_size);
|
|
|
|
return this_environment();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lua_getinfo(L, "f", &info) == 0) {
|
|
|
|
lua_settop(L, pre_stack_size);
|
|
|
|
return this_environment();
|
|
|
|
}
|
|
|
|
|
2017-09-14 12:35:40 +08:00
|
|
|
stack_reference f(L, -1);
|
|
|
|
environment env(env_key, f);
|
2017-05-10 01:24:56 +08:00
|
|
|
if (!env.valid()) {
|
|
|
|
lua_settop(L, pre_stack_size);
|
|
|
|
return this_environment();
|
|
|
|
}
|
|
|
|
return this_environment(std::move(env));
|
|
|
|
}
|
|
|
|
};
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace stack
|
|
|
|
} // namespace sol
|
2017-04-03 04:10:00 +08:00
|
|
|
|
|
|
|
#endif // SOL_ENVIRONMENT_HPP
|