1 // sol3
2
3 // The MIT License (MIT)
4
5 // Copyright (c) 2013-2021 Rapptz, ThePhD and contributors
6
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of
8 // this software and associated documentation files (the "Software"), to deal in
9 // the Software without restriction, including without limitation the rights to
10 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 // the Software, and to permit persons to whom the Software is furnished to do so,
12 // subject to the following conditions:
13
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 #include "sol_test.hpp"
25
26 #include <catch2/catch.hpp>
27
28 #include <list>
29 #include <vector>
30 #include <deque>
31
32 int dump_always_fail_number = -32;
33
dump_always_fail(lua_State *,const void *,size_t,void *)34 int dump_always_fail(lua_State*, const void*, size_t, void*) {
35 return dump_always_fail_number;
36 }
37
38 TEST_CASE("dump/dump transfer", "test that a function can be transferred from one place to another") {
39 SECTION("safe") {
40 sol::state lua;
41 sol::state lua2;
42 lua2.open_libraries(sol::lib::base);
43
44 sol::load_result lr = lua.load("a = function (v) print(v) return v end");
45 REQUIRE(lr.valid());
46 sol::protected_function target = lr.get<sol::protected_function>();
47 sol::bytecode target_bc = target.dump();
48
49 auto result2 = lua2.safe_script(target_bc.as_string_view(), sol::script_pass_on_error);
50 REQUIRE(result2.valid());
51 sol::protected_function pf = lua2["a"];
52 int v = pf(25557);
53 REQUIRE(v == 25557);
54 }
55 SECTION("unsafe") {
56 sol::state lua;
57 sol::state lua2;
58 lua2.open_libraries(sol::lib::base);
59
60 sol::load_result lr = lua.load("a = function (v) print(v) return v end");
61 REQUIRE(lr.valid());
62 sol::unsafe_function target = lr;
63 sol::bytecode target_bc = target.dump();
64
65 auto result2 = lua2.safe_script(target_bc.as_string_view(), sol::script_pass_on_error);
66 REQUIRE(result2.valid());
67 sol::unsafe_function pf = lua2["a"];
68 int v = pf(25557);
69 REQUIRE(v == 25557);
70 }
71 }
72
73 TEST_CASE("dump/failure", "test that failure is properly propagated") {
74 SECTION("safe") {
75 sol::state lua;
76 sol::load_result lr = lua.load("a = function (v) print(v) return v end");
77 REQUIRE(lr.valid());
78 sol::protected_function target = lr.get<sol::protected_function>();
79 int err = target.dump(&dump_always_fail, nullptr, false, sol::dump_pass_on_error);
80 REQUIRE(err == dump_always_fail_number);
81 }
82 SECTION("unsafe") {
83 sol::state lua;
84 sol::load_result lr = lua.load("a = function (v) print(v) return v end");
85 REQUIRE(lr.valid());
86 sol::unsafe_function target = lr;
87 int err = target.dump(&dump_always_fail, nullptr, false, sol::dump_pass_on_error);
88 REQUIRE(err == dump_always_fail_number);
89 }
90 }
91
92 TEST_CASE("dump/different containers", "test that dump inserter works for various kinds of containers") {
93 SECTION("safe") {
94 sol::state lua;
95
96 sol::load_result lr = lua.load("a = function (v) print(v) return v end");
97 REQUIRE(lr.valid());
98 sol::protected_function target = lr.get<sol::protected_function>();
99 sol::bytecode bytecode_dump = target.dump();
100 std::list<std::byte> list_dump = target.dump<std::list<std::byte>>();
101 std::vector<std::byte> vector_dump = target.dump<std::vector<std::byte>>();
102 std::deque<std::byte> deque_dump = target.dump<std::deque<std::byte>>();
103 REQUIRE(std::equal(bytecode_dump.cbegin(), bytecode_dump.cend(), vector_dump.cbegin(), vector_dump.cend()));
104 REQUIRE(std::equal(bytecode_dump.cbegin(), bytecode_dump.cend(), list_dump.cbegin(), list_dump.cend()));
105 REQUIRE(std::equal(bytecode_dump.cbegin(), bytecode_dump.cend(), deque_dump.cbegin(), deque_dump.cend()));
106 }
107 SECTION("unsafe") {
108 sol::state lua;
109
110 sol::load_result lr = lua.load("a = function (v) print(v) return v end");
111 REQUIRE(lr.valid());
112 sol::unsafe_function target = lr;
113 sol::bytecode bytecode_dump = target.dump();
114 std::list<std::byte> list_dump = target.dump<std::list<std::byte>>();
115 std::vector<std::byte> vector_dump = target.dump<std::vector<std::byte>>();
116 std::deque<std::byte> deque_dump = target.dump<std::deque<std::byte>>();
117 REQUIRE(std::equal(bytecode_dump.cbegin(), bytecode_dump.cend(), vector_dump.cbegin(), vector_dump.cend()));
118 REQUIRE(std::equal(bytecode_dump.cbegin(), bytecode_dump.cend(), list_dump.cbegin(), list_dump.cend()));
119 REQUIRE(std::equal(bytecode_dump.cbegin(), bytecode_dump.cend(), deque_dump.cbegin(), deque_dump.cend()));
120 }
121 }
122