1 #include "core/command.hpp"
2 #include "lua/internal.hpp"
3 #include "core/framerate.hpp"
4 #include "core/instance.hpp"
5 #include "core/moviefile.hpp"
6 #include "core/moviedata.hpp"
7 #include "core/rom.hpp"
8 #include "core/runmode.hpp"
9 #include "core/window.hpp"
10 #include "interface/romtype.hpp"
11 #include "library/string.hpp"
16 std::string
luavalue_to_string(lua::state
& L
, int index
, std::set
<const void*>& printed
, bool quote
)
18 switch(L
.type(index
)) {
24 return L
.toboolean(index
) ? "true" : "false";
26 if(L
.isinteger(index
))
27 return (stringfmt() << L
.tointeger(index
)).str();
29 return (stringfmt() << L
.tonumber(index
)).str();
33 tmp2
= L
.tolstring(index
, &len
);
35 return "\"" + std::string(tmp2
, tmp2
+ len
) + "\"";
37 return std::string(tmp2
, tmp2
+ len
);
39 case LUA_TLIGHTUSERDATA
:
40 return (stringfmt() << "Lightuserdata:" << L
.touserdata(index
)).str();
42 return (stringfmt() << "Function:" << L
.topointer(index
)).str();
44 return (stringfmt() << "Thread:" << L
.topointer(index
)).str();
47 return (stringfmt() << "Userdata<" << try_recognize_userdata(L
, index
) << "@"
48 << L
.touserdata(index
) << ">:[" << try_print_userdata(L
, index
) << "]").str();
51 const void* ptr
= L
.topointer(index
);
52 if(printed
.count(ptr
))
53 return (stringfmt() << "<table:" << ptr
<< ">").str();
56 s
<< "<" << ptr
<< ">{";
59 while(L
.next(index
)) {
62 int stacktop
= L
.gettop();
63 s
<< "[" << luavalue_to_string(L
, stacktop
- 1, printed
, true) << "]="
64 << luavalue_to_string(L
, stacktop
, printed
, true);
72 return (stringfmt() << "???:" << L
.topointer(index
)).str();
76 int identify_class(lua::state
& L
, lua::parameters
& P
)
80 L
.pushlstring(try_recognize_userdata(L
, 1));
84 int tostringx(lua::state
& L
, lua::parameters
& P
)
86 std::set
<const void*> tmp2
;
87 std::string y
= luavalue_to_string(L
, 1, tmp2
, false);
92 int print2(lua::state
& L
, lua::parameters
& P
)
98 std::set
<const void*> tmp2
;
99 std::string tmp
= luavalue_to_string(L
, i
, tmp2
, false);
103 toprint
= toprint
+ "\t" + tmp
;
106 platform::message(toprint
);
110 int exec(lua::state
& L
, lua::parameters
& P
)
116 CORE().command
->invoke(text
);
120 int lookup_class(lua::state
& L
, lua::parameters
& P
)
126 return lua::class_base::lookup_and_push(L
, clazz
) ? 1 : 0;
129 int all_classes(lua::state
& L
, lua::parameters
& P
)
131 auto c
= lua::class_base::all_classes(L
);
140 int emulator_ready(lua::state
& L
, lua::parameters
& P
)
146 int utime(lua::state
& L
, lua::parameters
& P
)
148 uint64_t t
= framerate_regulator::get_utime();
149 L
.pushnumber(t
/ 1000000);
150 L
.pushnumber(t
% 1000000);
154 int set_idle_timeout(lua::state
& L
, lua::parameters
& P
)
161 core
.lua2
->idle_hook_time
= framerate_regulator::get_utime() + dt
;
165 int set_timer_timeout(lua::state
& L
, lua::parameters
& P
)
172 core
.lua2
->timer_hook_time
= framerate_regulator::get_utime() + dt
;
176 int bus_address(lua::state
& L
, lua::parameters
& P
)
183 auto busrange
= core
.rom
->get_bus_map();
185 throw std::runtime_error("This platform does not have bus mapping");
186 L
.pushnumber(busrange
.first
+ (addr
% busrange
.second
));
190 int get_lag_flag(lua::state
& L
, lua::parameters
& P
)
193 L
.pushboolean(!core
.rom
->get_pflag());
197 int set_lag_flag(lua::state
& L
, lua::parameters
& P
)
204 core
.rom
->set_pflag(!flag
);
208 int get_lua_memory_use(lua::state
& L
, lua::parameters
& P
)
210 L
.pushnumber(L
.get_memory_use());
211 L
.pushnumber(L
.get_memory_limit());
215 int get_runmode(lua::state
& L
, lua::parameters
& P
)
218 auto m
= core
.runmode
->get();
219 if(m
== emulator_runmode::QUIT
) L
.pushstring("quit");
220 else if(m
== emulator_runmode::NORMAL
) L
.pushstring("normal");
221 else if(m
== emulator_runmode::LOAD
) L
.pushstring("load");
222 else if(m
== emulator_runmode::ADVANCE_FRAME
) L
.pushstring("advance_frame");
223 else if(m
== emulator_runmode::ADVANCE_SUBFRAME
) L
.pushstring("advance_subframe");
224 else if(m
== emulator_runmode::SKIPLAG
) L
.pushstring("skiplag");
225 else if(m
== emulator_runmode::SKIPLAG_PENDING
) L
.pushstring("skiplag_pending");
226 else if(m
== emulator_runmode::PAUSE
) L
.pushstring("pause");
227 else if(m
== emulator_runmode::PAUSE_BREAK
) L
.pushstring("pause_break");
228 else if(m
== emulator_runmode::CORRUPT
) L
.pushstring("corrupt");
229 else L
.pushstring("unknown");
233 int lsnes_features(lua::state
& L
, lua::parameters
& P
)
238 if(arg
== "text-halos") ok
= true;
243 lua::functions
LUA_misc_fns(lua_func_misc
, "", {
246 {"emulator_ready", emulator_ready
},
248 {"set_idle_timeout", set_idle_timeout
},
249 {"set_timer_timeout", set_timer_timeout
},
250 {"bus_address", bus_address
},
251 {"get_lua_memory_use", get_lua_memory_use
},
252 {"lsnes_features", lsnes_features
},
253 {"memory.get_lag_flag", get_lag_flag
},
254 {"memory.set_lag_flag", set_lag_flag
},
255 {"gui.get_runmode", get_runmode
},
258 lua::functions
LUA_pure_fns(lua_func_bit
, "", {
259 {"identify_class", identify_class
},
260 {"tostringx", tostringx
},
261 {"lookup_class", lookup_class
},
262 {"all_classes", all_classes
},