1 #include "core/command.hpp"
2 #include "lua/internal.hpp"
3 #include "core/framerate.hpp"
4 #include "core/moviefile.hpp"
5 #include "core/moviedata.hpp"
6 #include "core/window.hpp"
7 #include "interface/romtype.hpp"
8 #include "library/string.hpp"
13 std::string
luavalue_to_string(lua::state
& L
, int index
, std::set
<const void*>& printed
, bool quote
)
15 switch(L
.type(index
)) {
21 return L
.toboolean(index
) ? "true" : "false";
23 return (stringfmt() << L
.tonumber(index
)).str();
27 tmp2
= L
.tolstring(index
, &len
);
29 return "\"" + std::string(tmp2
, tmp2
+ len
) + "\"";
31 return std::string(tmp2
, tmp2
+ len
);
33 case LUA_TLIGHTUSERDATA
:
34 return (stringfmt() << "Lightuserdata:" << L
.touserdata(index
)).str();
36 return (stringfmt() << "Function:" << L
.topointer(index
)).str();
38 return (stringfmt() << "Thread:" << L
.topointer(index
)).str();
41 return (stringfmt() << "Userdata<" << try_recognize_userdata(L
, index
) << "@"
42 << L
.touserdata(index
) << ">:[" << try_print_userdata(L
, index
) << "]").str();
45 const void* ptr
= L
.topointer(index
);
46 if(printed
.count(ptr
))
47 return (stringfmt() << "<table:" << ptr
<< ">").str();
50 s
<< "<" << ptr
<< ">{";
53 while(L
.next(index
)) {
56 int stacktop
= L
.gettop();
57 s
<< "[" << luavalue_to_string(L
, stacktop
- 1, printed
, true) << "]="
58 << luavalue_to_string(L
, stacktop
, printed
, true);
66 return (stringfmt() << "???:" << L
.topointer(index
)).str();
70 int identify_class(lua::state
& L
, lua::parameters
& P
)
74 L
.pushlstring(try_recognize_userdata(L
, 1));
78 int tostringx(lua::state
& L
, lua::parameters
& P
)
80 std::set
<const void*> tmp2
;
81 std::string y
= luavalue_to_string(L
, 1, tmp2
, false);
86 int print2(lua::state
& L
, lua::parameters
& P
)
92 std::set
<const void*> tmp2
;
93 std::string tmp
= luavalue_to_string(L
, i
, tmp2
, false);
97 toprint
= toprint
+ "\t" + tmp
;
100 platform::message(toprint
);
104 int exec(lua::state
& L
, lua::parameters
& P
)
110 lsnes_cmd
.invoke(text
);
114 int lookup_class(lua::state
& L
, lua::parameters
& P
)
120 return lua::class_base::lookup_and_push(L
, clazz
) ? 1 : 0;
123 int all_classes(lua::state
& L
, lua::parameters
& P
)
125 auto c
= lua::class_base::all_classes(L
);
134 int emulator_ready(lua::state
& L
, lua::parameters
& P
)
136 L
.pushboolean(lua_booted_flag
? 1 : 0);
140 int utime(lua::state
& L
, lua::parameters
& P
)
142 uint64_t t
= get_utime();
143 L
.pushnumber(t
/ 1000000);
144 L
.pushnumber(t
% 1000000);
148 int set_idle_timeout(lua::state
& L
, lua::parameters
& P
)
154 lua_idle_hook_time
= get_utime() + dt
;
158 int set_timer_timeout(lua::state
& L
, lua::parameters
& P
)
164 lua_timer_hook_time
= get_utime() + dt
;
168 int bus_address(lua::state
& L
, lua::parameters
& P
)
174 auto busrange
= our_rom
.rtype
->get_bus_map();
176 throw std::runtime_error("This platform does not have bus mapping");
177 L
.pushnumber(busrange
.first
+ (addr
% busrange
.second
));
181 int get_lag_flag(lua::state
& L
, lua::parameters
& P
)
183 L
.pushboolean(!(our_rom
.rtype
&& our_rom
.rtype
->get_pflag()));
187 int set_lag_flag(lua::state
& L
, lua::parameters
& P
)
194 our_rom
.rtype
->set_pflag(!flag
);
198 lua::functions
misc_fns(lua_func_misc
, "", {
201 {"emulator_ready", emulator_ready
},
203 {"set_idle_timeout", set_idle_timeout
},
204 {"set_timer_timeout", set_timer_timeout
},
205 {"bus_address", bus_address
},
206 {"memory.get_lag_flag", get_lag_flag
},
207 {"memory.set_lag_flag", set_lag_flag
},
210 lua::functions
pure_fns(lua_func_bit
, "", {
211 {"identify_class", identify_class
},
212 {"tostringx", tostringx
},
213 {"lookup_class", lookup_class
},
214 {"all_classes", all_classes
},