Evdev joystick plugin
[lsnes.git] / lua / core.cpp
blob91b676483bec53916a4951d795f5646d70731270
1 #include "lua-int.hpp"
2 #include "command.hpp"
3 #include "window.hpp"
5 namespace
7 function_ptr_luafun lua_print("print", [](lua_State* LS, const std::string& fname) -> int {
8 int stacksize = 0;
9 while(!lua_isnone(LS, stacksize + 1))
10 stacksize++;
11 std::string toprint;
12 bool first = true;
13 for(int i = 0; i < stacksize; i++) {
14 size_t len;
15 const char* tmp = NULL;
16 if(lua_isnil(LS, i + 1)) {
17 tmp = "nil";
18 len = 3;
19 } else if(lua_isboolean(LS, i + 1) && lua_toboolean(LS, i + 1)) {
20 tmp = "true";
21 len = 4;
22 } else if(lua_isboolean(LS, i + 1) && !lua_toboolean(LS, i + 1)) {
23 tmp = "false";
24 len = 5;
25 } else {
26 tmp = lua_tolstring(LS, i + 1, &len);
27 if(!tmp) {
28 tmp = "(unprintable)";
29 len = 13;
32 std::string localmsg(tmp, tmp + len);
33 if(first)
34 toprint = localmsg;
35 else
36 toprint = toprint + "\t" + localmsg;
37 first = false;
39 window::message(toprint);
40 return 0;
41 });
43 function_ptr_luafun lua_exec("exec", [](lua_State* LS, const std::string& fname) -> int {
44 std::string text = get_string_argument(LS, 1, fname.c_str());
45 command::invokeC(text);
46 return 0;
47 });