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/window.hpp"
9 #include "interface/romtype.hpp"
10 #include "library/string.hpp"
15 std::string
luavalue_to_string(lua::state
& L
, int index
, std::set
<const void*>& printed
, bool quote
)
17 switch(L
.type(index
)) {
23 return L
.toboolean(index
) ? "true" : "false";
25 return (stringfmt() << L
.tonumber(index
)).str();
29 tmp2
= L
.tolstring(index
, &len
);
31 return "\"" + std::string(tmp2
, tmp2
+ len
) + "\"";
33 return std::string(tmp2
, tmp2
+ len
);
35 case LUA_TLIGHTUSERDATA
:
36 return (stringfmt() << "Lightuserdata:" << L
.touserdata(index
)).str();
38 return (stringfmt() << "Function:" << L
.topointer(index
)).str();
40 return (stringfmt() << "Thread:" << L
.topointer(index
)).str();
43 return (stringfmt() << "Userdata<" << try_recognize_userdata(L
, index
) << "@"
44 << L
.touserdata(index
) << ">:[" << try_print_userdata(L
, index
) << "]").str();
47 const void* ptr
= L
.topointer(index
);
48 if(printed
.count(ptr
))
49 return (stringfmt() << "<table:" << ptr
<< ">").str();
52 s
<< "<" << ptr
<< ">{";
55 while(L
.next(index
)) {
58 int stacktop
= L
.gettop();
59 s
<< "[" << luavalue_to_string(L
, stacktop
- 1, printed
, true) << "]="
60 << luavalue_to_string(L
, stacktop
, printed
, true);
68 return (stringfmt() << "???:" << L
.topointer(index
)).str();
72 int identify_class(lua::state
& L
, lua::parameters
& P
)
76 L
.pushlstring(try_recognize_userdata(L
, 1));
80 int tostringx(lua::state
& L
, lua::parameters
& P
)
82 std::set
<const void*> tmp2
;
83 std::string y
= luavalue_to_string(L
, 1, tmp2
, false);
88 int print2(lua::state
& L
, lua::parameters
& P
)
94 std::set
<const void*> tmp2
;
95 std::string tmp
= luavalue_to_string(L
, i
, tmp2
, false);
99 toprint
= toprint
+ "\t" + tmp
;
102 platform::message(toprint
);
106 int exec(lua::state
& L
, lua::parameters
& P
)
112 CORE().command
->invoke(text
);
116 int lookup_class(lua::state
& L
, lua::parameters
& P
)
122 return lua::class_base::lookup_and_push(L
, clazz
) ? 1 : 0;
125 int all_classes(lua::state
& L
, lua::parameters
& P
)
127 auto c
= lua::class_base::all_classes(L
);
136 int emulator_ready(lua::state
& L
, lua::parameters
& P
)
142 int utime(lua::state
& L
, lua::parameters
& P
)
144 uint64_t t
= framerate_regulator::get_utime();
145 L
.pushnumber(t
/ 1000000);
146 L
.pushnumber(t
% 1000000);
150 int set_idle_timeout(lua::state
& L
, lua::parameters
& P
)
157 core
.lua2
->idle_hook_time
= framerate_regulator::get_utime() + dt
;
161 int set_timer_timeout(lua::state
& L
, lua::parameters
& P
)
168 core
.lua2
->timer_hook_time
= framerate_regulator::get_utime() + dt
;
172 int bus_address(lua::state
& L
, lua::parameters
& P
)
179 auto busrange
= core
.rom
->get_bus_map();
181 throw std::runtime_error("This platform does not have bus mapping");
182 L
.pushnumber(busrange
.first
+ (addr
% busrange
.second
));
186 int get_lag_flag(lua::state
& L
, lua::parameters
& P
)
189 L
.pushboolean(!core
.rom
->get_pflag());
193 int set_lag_flag(lua::state
& L
, lua::parameters
& P
)
200 core
.rom
->set_pflag(!flag
);
204 lua::functions
LUA_misc_fns(lua_func_misc
, "", {
207 {"emulator_ready", emulator_ready
},
209 {"set_idle_timeout", set_idle_timeout
},
210 {"set_timer_timeout", set_timer_timeout
},
211 {"bus_address", bus_address
},
212 {"memory.get_lag_flag", get_lag_flag
},
213 {"memory.set_lag_flag", set_lag_flag
},
216 lua::functions
LUA_pure_fns(lua_func_bit
, "", {
217 {"identify_class", identify_class
},
218 {"tostringx", tostringx
},
219 {"lookup_class", lookup_class
},
220 {"all_classes", all_classes
},