mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Protect against empty requires
This commit is contained in:
parent
e1950b9a55
commit
27c352149d
|
@ -86,7 +86,16 @@ namespace sol {
|
|||
optional<object> loaded = is_loaded_package(key);
|
||||
if (loaded && loaded->valid())
|
||||
return std::move(*loaded);
|
||||
int before = lua_gettop(L);
|
||||
action();
|
||||
int after = lua_gettop(L);
|
||||
if (before == after) {
|
||||
// I mean, you were supposed to return
|
||||
// something, ANYTHING, from your requires script. I guess I'll just
|
||||
// but some trash in here, it's on you after that?
|
||||
ensure_package(key, static_cast<void*>(L));
|
||||
return object(L, lua_nil);
|
||||
}
|
||||
stack_reference sr(L, -1);
|
||||
if (create_global)
|
||||
set(key, sr);
|
||||
|
|
|
@ -85,7 +85,7 @@ namespace sol {
|
|||
|
||||
template <typename T>
|
||||
table_proxy&& set(T&& item) && {
|
||||
tuple_set(std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>(), std::forward<T>(item));
|
||||
std::move(*this).tuple_set(std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>(), std::forward<T>(item));
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
|
|
|
@ -20,8 +20,8 @@
|
|||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
// This file was generated with a script.
|
||||
// Generated 2021-01-24 17:38:14.787587 UTC
|
||||
// This header was generated with sol v3.2.3 (revision d363ccd7)
|
||||
// Generated 2021-01-25 02:52:07.886776 UTC
|
||||
// This header was generated with sol v3.2.3 (revision e1950b9a)
|
||||
// https://github.com/ThePhD/sol2
|
||||
|
||||
#ifndef SOL_SINGLE_CONFIG_HPP
|
||||
|
|
|
@ -20,8 +20,8 @@
|
|||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
// This file was generated with a script.
|
||||
// Generated 2021-01-24 17:38:14.725587 UTC
|
||||
// This header was generated with sol v3.2.3 (revision d363ccd7)
|
||||
// Generated 2021-01-25 02:52:07.863772 UTC
|
||||
// This header was generated with sol v3.2.3 (revision e1950b9a)
|
||||
// https://github.com/ThePhD/sol2
|
||||
|
||||
#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP
|
||||
|
|
|
@ -20,8 +20,8 @@
|
|||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
// This file was generated with a script.
|
||||
// Generated 2021-01-24 17:38:13.604128 UTC
|
||||
// This header was generated with sol v3.2.3 (revision d363ccd7)
|
||||
// Generated 2021-01-25 02:52:07.000664 UTC
|
||||
// This header was generated with sol v3.2.3 (revision e1950b9a)
|
||||
// https://github.com/ThePhD/sol2
|
||||
|
||||
#ifndef SOL_SINGLE_INCLUDE_HPP
|
||||
|
@ -24145,7 +24145,7 @@ namespace sol {
|
|||
|
||||
template <typename T>
|
||||
table_proxy&& set(T&& item) && {
|
||||
tuple_set(std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>(), std::forward<T>(item));
|
||||
std::move(*this).tuple_set(std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>(), std::forward<T>(item));
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
|
@ -26553,7 +26553,16 @@ namespace sol {
|
|||
optional<object> loaded = is_loaded_package(key);
|
||||
if (loaded && loaded->valid())
|
||||
return std::move(*loaded);
|
||||
int before = lua_gettop(L);
|
||||
action();
|
||||
int after = lua_gettop(L);
|
||||
if (before == after) {
|
||||
// I mean, you were supposed to return
|
||||
// something, ANYTHING, from your requires script. I guess I'll just
|
||||
// but some trash in here, it's on you after that?
|
||||
ensure_package(key, static_cast<void*>(L));
|
||||
return object(L, lua_nil);
|
||||
}
|
||||
stack_reference sr(L, -1);
|
||||
if (create_global)
|
||||
set(key, sr);
|
||||
|
|
|
@ -145,7 +145,7 @@ TEST_CASE("state/require_file", "opening files as 'requires'") {
|
|||
}
|
||||
|
||||
TEST_CASE("state/require_script", "opening strings as 'requires' clauses") {
|
||||
std::string code = "return { modfunc = function () return 221 end }";
|
||||
const std::string code = "return { modfunc = function () return 221 end }";
|
||||
|
||||
sol::state lua;
|
||||
sol::stack_guard luasg(lua);
|
||||
|
@ -160,6 +160,29 @@ TEST_CASE("state/require_script", "opening strings as 'requires' clauses") {
|
|||
REQUIRE((thingy1 == thingy2));
|
||||
}
|
||||
|
||||
TEST_CASE("state/require_script empty", "opening strings as 'requires' clauses that return nothing insert a bogus value") {
|
||||
sol::state lua;
|
||||
lua.open_libraries(sol::lib::package, sol::lib::base);
|
||||
|
||||
lua.set_function("print_to_console", [](std::string const& message) { std::cout << message << std::endl; });
|
||||
|
||||
const std::string code = "print_to_console('hello from required file') i = 20";
|
||||
|
||||
{
|
||||
int begintop = 0, endtop = 0;
|
||||
test_stack_guard guard(lua, begintop, endtop);
|
||||
lua.require_script("require_test", code, false);
|
||||
}
|
||||
|
||||
sol::optional<sol::error> maybe_error = lua.safe_script(R"(
|
||||
require ("require_test")
|
||||
print_to_console("hello from script")
|
||||
assert(i == 20)
|
||||
)",
|
||||
sol::script_pass_on_error);
|
||||
REQUIRE_FALSE(maybe_error.has_value());
|
||||
}
|
||||
|
||||
TEST_CASE("state/require", "require using a function") {
|
||||
struct open {
|
||||
static int open_func(lua_State* L) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user