Created
December 9, 2013 05:08
-
-
Save ThePhD/7867624 to your computer and use it in GitHub Desktop.
Sol all tests to date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sol.hpp> | |
#include <iostream> | |
#include <tuple> | |
std::string free_func_yo() { | |
std::cout << "free_func_yo()" << std::endl; | |
return "test"; | |
} | |
struct member_test { | |
std::string operator() () { | |
std::cout << "member_test()" << std::endl; | |
return "test"; | |
} | |
}; | |
void run_test(sol::state& lua) { | |
lua.script("assert(os.fun() == \"test\")\n" | |
"assert(os.name == \"windows\")"); | |
} | |
int main( ) { | |
sol::state lua; | |
lua.open_libraries( sol::lib::base, sol::lib::os ); | |
lua.get<sol::table>( "os" ).set( "name", "windows" ); | |
lua.get<sol::table>( "os" ).set_function( "fun", | |
[ ] ( ) { | |
std::cout << "stateless lambda()" << std::endl; | |
return "test"; | |
} | |
); | |
run_test( lua ); | |
lua.get<sol::table>( "os" ).set_function( "fun", &free_func_yo ); | |
run_test( lua ); | |
// l-value, can optomize | |
auto lval = member_test( ); | |
lua.get<sol::table>( "os" ).set_function( "fun", &member_test::operator(), lval ); | |
run_test( lua ); | |
// Tests will begin failing here | |
// r-value, cannot optomize | |
lua.get<sol::table>( "os" ).set_function( "fun", &member_test::operator(), member_test( ) ); | |
run_test( lua ); | |
// r-value, cannot optomize | |
auto rval = member_test( ); | |
lua.get<sol::table>( "os" ).set_function( "fun", &member_test::operator(), std::move( rval ) ); | |
run_test( lua ); | |
// stateful lambda: non-convertible, unoptomizable | |
int breakit = 50; | |
lua.get<sol::table>( "os" ).set_function( "fun", | |
[ &breakit ] ( ) { | |
std::cout << "stateless lambda()" << std::endl; | |
return "test"; | |
} | |
); | |
run_test( lua ); | |
} | |
/* | |
void breaking() { | |
std::cout << "Nice!~\n"; | |
} | |
std::tuple<double, double> breaking_multi() { | |
return std::make_tuple(567.2, 2.567); | |
} | |
struct a_type { | |
std::tuple<double, double> breaking_multi() { | |
return std::make_tuple(567.2, 2.567); | |
} | |
}; | |
int main2() { | |
sol::state lua; | |
a_type at; | |
int a = 10; | |
int b = 11; | |
auto breaking_lambda = [ ] () { std::cout << "Nice Lambda!\n"; }; | |
lua.set_function("arf", breaking); | |
lua.set_function("arflambda", breaking_lambda); | |
lua.set_function("arfmulti", breaking_multi); | |
lua.set_function("arfmulti", &a_type::breaking_multi, at); | |
lua.script("arf()\n" | |
"arflambda()\n" | |
"x,y = arfmulti()\n" | |
"function add_double(a, b)\n" | |
" return a + b, a + b;\n" | |
"end\n" | |
"function add(a, b)\n" | |
" return a + b;\n" | |
"end\n" | |
"function ultra_add(a, b, c)\n" | |
" return a + b + c;\n" | |
"end"); | |
auto fd = lua.get<sol::function>("add_double"); | |
auto f = lua.get<sol::function>("add"); | |
auto fx = lua.get<sol::function>("ultra_add"); | |
std::tuple<int, int> arf = fd.invoke<int, int>(10, 10); // 20, 20 | |
std::cout << lua.get<double>("x") << ", " << lua.get<double>("y") << std::endl; | |
std::cout << std::get<0>(arf) << ", " << std::get<1>(arf) << std::endl; // 21 | |
std::cout << f.invoke<int>(a, b) << std::endl; // 21 | |
std::cout << fx.invoke<int>(a, a, b) << std::endl; // 31 | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment