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/window.hpp"
8 #include "interface/romtype.hpp"
9 #include "library/string.hpp"
14 std::string
luavalue_to_string(lua::state
& L
, int index
, std::set
<const void*>& printed
, bool quote
)
16 switch(L
.type(index
)) {
22 return L
.toboolean(index
) ? "true" : "false";
24 return (stringfmt() << L
.tonumber(index
)).str();
28 tmp2
= L
.tolstring(index
, &len
);
30 return "\"" + std::string(tmp2
, tmp2
+ len
) + "\"";
32 return std::string(tmp2
, tmp2
+ len
);
34 case LUA_TLIGHTUSERDATA
:
35 return (stringfmt() << "Lightuserdata:" << L
.touserdata(index
)).str();
37 return (stringfmt() << "Function:" << L
.topointer(index
)).str();
39 return (stringfmt() << "Thread:" << L
.topointer(index
)).str();
42 return (stringfmt() << "Userdata<" << try_recognize_userdata(L
, index
) << "@"
43 << L
.touserdata(index
) << ">:[" << try_print_userdata(L
, index
) << "]").str();
46 const void* ptr
= L
.topointer(index
);
47 if(printed
.count(ptr
))
48 return (stringfmt() << "<table:" << ptr
<< ">").str();
51 s
<< "<" << ptr
<< ">{";
54 while(L
.next(index
)) {
57 int stacktop
= L
.gettop();
58 s
<< "[" << luavalue_to_string(L
, stacktop
- 1, printed
, true) << "]="
59 << luavalue_to_string(L
, stacktop
, printed
, true);
67 return (stringfmt() << "???:" << L
.topointer(index
)).str();
71 int identify_class(lua::state
& L
, lua::parameters
& P
)
75 L
.pushlstring(try_recognize_userdata(L
, 1));
79 int tostringx(lua::state
& L
, lua::parameters
& P
)
81 std::set
<const void*> tmp2
;
82 std::string y
= luavalue_to_string(L
, 1, tmp2
, false);
87 int print2(lua::state
& L
, lua::parameters
& P
)
93 std::set
<const void*> tmp2
;
94 std::string tmp
= luavalue_to_string(L
, i
, tmp2
, false);
98 toprint
= toprint
+ "\t" + tmp
;
101 platform::message(toprint
);
105 int exec(lua::state
& L
, lua::parameters
& P
)
111 CORE().command
->invoke(text
);
115 int lookup_class(lua::state
& L
, lua::parameters
& P
)
121 return lua::class_base::lookup_and_push(L
, clazz
) ? 1 : 0;
124 int all_classes(lua::state
& L
, lua::parameters
& P
)
126 auto c
= lua::class_base::all_classes(L
);
135 int emulator_ready(lua::state
& L
, lua::parameters
& P
)
141 int utime(lua::state
& L
, lua::parameters
& P
)
143 uint64_t t
= framerate_regulator::get_utime();
144 L
.pushnumber(t
/ 1000000);
145 L
.pushnumber(t
% 1000000);
149 int set_idle_timeout(lua::state
& L
, lua::parameters
& P
)
156 core
.lua2
->idle_hook_time
= framerate_regulator::get_utime() + dt
;
160 int set_timer_timeout(lua::state
& L
, lua::parameters
& P
)
167 core
.lua2
->timer_hook_time
= framerate_regulator::get_utime() + dt
;
171 int bus_address(lua::state
& L
, lua::parameters
& P
)
177 auto busrange
= our_rom
.rtype
->get_bus_map();
179 throw std::runtime_error("This platform does not have bus mapping");
180 L
.pushnumber(busrange
.first
+ (addr
% busrange
.second
));
184 int get_lag_flag(lua::state
& L
, lua::parameters
& P
)
186 L
.pushboolean(!(our_rom
.rtype
&& our_rom
.rtype
->get_pflag()));
190 int set_lag_flag(lua::state
& L
, lua::parameters
& P
)
197 our_rom
.rtype
->set_pflag(!flag
);
201 lua::functions
LUA_misc_fns(lua_func_misc
, "", {
204 {"emulator_ready", emulator_ready
},
206 {"set_idle_timeout", set_idle_timeout
},
207 {"set_timer_timeout", set_timer_timeout
},
208 {"bus_address", bus_address
},
209 {"memory.get_lag_flag", get_lag_flag
},
210 {"memory.set_lag_flag", set_lag_flag
},
213 lua::functions
LUA_pure_fns(lua_func_bit
, "", {
214 {"identify_class", identify_class
},
215 {"tostringx", tostringx
},
216 {"lookup_class", lookup_class
},
217 {"all_classes", all_classes
},