Fix all warnings -Wall spews
[lsnes.git] / lua.cpp
blob3120f8557a3ea9f397f0f824781faf08e8099da8
1 #include "lua.hpp"
2 #include "misc.hpp"
3 #include "memorymanip.hpp"
4 #include "mainloop.hpp"
5 extern "C" {
6 #include <lua.h>
7 #include <lualib.h>
9 #include <iostream>
10 #include "fieldsplit.hpp"
11 #define BITWISE_BITS 48
12 #define BITWISE_MASK ((1ULL << (BITWISE_BITS)) - 1)
14 #define SETFIELDFUN(LSS, idx, name, fun) do { lua_pushcfunction(LSS, fun); lua_setfield(LSS, (idx) - 1, name); \
15 } while(0)
17 namespace
19 window* tmp_win;
20 lua_State* L;
21 lua_render_context* rctx = NULL;
22 bool recursive_flag = false;
23 commandhandler* cmdhnd;
24 const char* luareader_fragment = NULL;
25 controls_t* controllerdata = NULL;
27 const char* read_lua_fragment(lua_State* LS, void* dummy, size_t* size)
29 if(luareader_fragment) {
30 const char* ret = luareader_fragment;
31 *size = strlen(luareader_fragment);
32 luareader_fragment = NULL;
33 return ret;
34 } else {
35 *size = 0;
36 return NULL;
40 void* alloc(void* user, void* old, size_t olds, size_t news)
42 if(news)
43 return realloc(old, news);
44 else
45 free(old);
46 return NULL;
49 bool callback_exists(const char* name)
51 if(recursive_flag)
52 return false;
53 lua_getglobal(L, name);
54 int t = lua_type(L, -1);
55 if(t != LUA_TFUNCTION)
56 lua_pop(L, 1);
57 return (t == LUA_TFUNCTION);
60 void push_string(const std::string& s)
62 lua_pushlstring(L, s.c_str(), s.length());
65 void push_boolean(bool b)
67 lua_pushboolean(L, b ? 1 : 0);
70 #define TEMPORARY "LUAINTERP_INTERNAL_COMMAND_TEMPORARY"
72 const char* eval_lua_lua = "loadstring(" TEMPORARY ")();";
73 const char* run_lua_lua = "dofile(" TEMPORARY ");";
75 void run_lua_fragment(window* win) throw(std::bad_alloc)
77 if(recursive_flag)
78 return;
79 int t = lua_load(L, read_lua_fragment, NULL, "run_lua_fragment");
80 if(t == LUA_ERRSYNTAX) {
81 out(win) << "Can't run Lua: Internal syntax error: " << lua_tostring(L, -1) << std::endl;
82 lua_pop(L, 1);
83 return;
85 if(t == LUA_ERRMEM) {
86 out(win) << "Can't run Lua: Out of memory" << std::endl;
87 lua_pop(L, 1);
88 return;
90 recursive_flag = true;
91 tmp_win = win;
92 int r = lua_pcall(L, 0, 0, 0);
93 recursive_flag = false;
94 if(r == LUA_ERRRUN) {
95 out(win) << "Error running Lua hunk: " << lua_tostring(L, -1) << std::endl;
96 lua_pop(L, 1);
98 if(r == LUA_ERRMEM) {
99 out(win) << "Error running Lua hunk: Out of memory" << std::endl;
100 lua_pop(L, 1);
102 if(r == LUA_ERRERR) {
103 out(win) << "Error running Lua hunk: Double Fault???" << std::endl;
104 lua_pop(L, 1);
106 if(lua_requests_repaint && cmdhnd) {
107 std::string c = "repaint";
108 lua_requests_repaint = false;
109 cmdhnd->docommand(c, win);
113 template<typename T>
114 T get_numeric_argument(lua_State* LS, unsigned argindex, const char* fname)
116 if(lua_isnone(LS, argindex) || !lua_isnumber(LS, argindex)) {
117 lua_pushfstring(L, "argument #%i to %s must be numeric", argindex, fname);
118 lua_error(LS);
120 return static_cast<T>(lua_tonumber(LS, argindex));
123 std::string get_string_argument(lua_State* LS, unsigned argindex, const char* fname)
125 if(lua_isnone(LS, argindex)) {
126 lua_pushfstring(L, "argument #%i to %s must be string", argindex, fname);
127 lua_error(LS);
129 size_t len;
130 const char* f = lua_tolstring(LS, argindex, &len);
131 if(!f) {
132 lua_pushfstring(L, "argument #%i to %s must be string", argindex, fname);
133 lua_error(LS);
135 return std::string(f, f + len);
138 bool get_boolean_argument(lua_State* LS, unsigned argindex, const char* fname)
140 if(lua_isnone(LS, argindex) || !lua_isboolean(LS, argindex)) {
141 lua_pushfstring(L, "argument #%i to %s must be boolean", argindex, fname);
142 lua_error(LS);
144 return (lua_toboolean(LS, argindex) != 0);
147 template<typename T>
148 void get_numeric_argument(lua_State* LS, unsigned argindex, T& value, const char* fname)
150 if(lua_isnoneornil(LS, argindex))
151 return;
152 if(lua_isnone(LS, argindex) || !lua_isnumber(LS, argindex)) {
153 lua_pushfstring(L, "argument #%i to %s must be numeric if present", argindex, fname);
154 lua_error(LS);
156 value = static_cast<T>(lua_tonumber(LS, argindex));
159 void do_eval_lua(const std::string& c, window* win) throw(std::bad_alloc)
161 push_string(c);
162 lua_setglobal(L, TEMPORARY);
163 luareader_fragment = eval_lua_lua;
164 run_lua_fragment(win);
167 void do_run_lua(const std::string& c, window* win) throw(std::bad_alloc)
169 push_string(c);
170 lua_setglobal(L, TEMPORARY);
171 luareader_fragment = run_lua_lua;
172 run_lua_fragment(win);
175 void run_lua_cb(int args, window* win) throw()
177 recursive_flag = true;
178 tmp_win = win;
179 int r = lua_pcall(L, args, 0, 0);
180 recursive_flag = false;
181 if(r == LUA_ERRRUN) {
182 out(win) << "Error running Lua callback: " << lua_tostring(L, -1) << std::endl;
183 lua_pop(L, 1);
185 if(r == LUA_ERRMEM) {
186 out(win) << "Error running Lua callback: Out of memory" << std::endl;
187 lua_pop(L, 1);
189 if(r == LUA_ERRERR) {
190 out(win) << "Error running Lua callback: Double Fault???" << std::endl;
191 lua_pop(L, 1);
193 if(lua_requests_repaint && cmdhnd) {
194 std::string c = "repaint";
195 lua_requests_repaint = false;
196 cmdhnd->docommand(c, win);
200 int lua_symmetric_bitwise(lua_State* LS, uint64_t (*combine)(uint64_t chain, uint64_t arg), uint64_t init)
202 int stacksize = 0;
203 while(!lua_isnone(LS, stacksize + 1))
204 stacksize++;
205 uint64_t ret = init;
206 for(int i = 0; i < stacksize; i++)
207 ret = combine(ret, get_numeric_argument<uint64_t>(LS, i + 1, "<bitwise function>"));
208 lua_pushnumber(LS, ret);
209 return 1;
212 int lua_shifter(lua_State* LS, uint64_t (*shift)(uint64_t base, uint64_t amount, uint64_t bits))
214 uint64_t base;
215 uint64_t amount = 1;
216 uint64_t bits = BITWISE_BITS;
217 base = get_numeric_argument<uint64_t>(LS, 1, "<shift function>");
218 get_numeric_argument(LS, 2, amount, "<shift function>");
219 get_numeric_argument(LS, 3, bits, "<shift function>");
220 lua_pushnumber(LS, shift(base, amount, bits));
221 return 1;
224 uint64_t combine_none(uint64_t chain, uint64_t arg)
226 return (chain & ~arg) & BITWISE_MASK;
229 uint64_t combine_any(uint64_t chain, uint64_t arg)
231 return (chain | arg) & BITWISE_MASK;
234 uint64_t combine_all(uint64_t chain, uint64_t arg)
236 return (chain & arg) & BITWISE_MASK;
239 uint64_t combine_parity(uint64_t chain, uint64_t arg)
241 return (chain ^ arg) & BITWISE_MASK;
244 uint64_t shift_lrotate(uint64_t base, uint64_t amount, uint64_t bits)
246 uint64_t mask = ((1ULL << bits) - 1);
247 base &= mask;
248 base = (base << amount) | (base >> (bits - amount));
249 return base & mask & BITWISE_MASK;
252 uint64_t shift_rrotate(uint64_t base, uint64_t amount, uint64_t bits)
254 uint64_t mask = ((1ULL << bits) - 1);
255 base &= mask;
256 base = (base >> amount) | (base << (bits - amount));
257 return base & mask & BITWISE_MASK;
260 uint64_t shift_lshift(uint64_t base, uint64_t amount, uint64_t bits)
262 uint64_t mask = ((1ULL << bits) - 1);
263 base <<= amount;
264 return base & mask & BITWISE_MASK;
267 uint64_t shift_lrshift(uint64_t base, uint64_t amount, uint64_t bits)
269 uint64_t mask = ((1ULL << bits) - 1);
270 base &= mask;
271 base >>= amount;
272 return base & BITWISE_MASK;
275 uint64_t shift_arshift(uint64_t base, uint64_t amount, uint64_t bits)
277 uint64_t mask = ((1ULL << bits) - 1);
278 base &= mask;
279 bool negative = ((base >> (bits - 1)) != 0);
280 base >>= amount;
281 base |= ((negative ? BITWISE_MASK : 0) << (bits - amount));
282 return base & mask & BITWISE_MASK;
285 int lua_bit_none(lua_State* LS)
287 return lua_symmetric_bitwise(LS, combine_none, BITWISE_MASK);
290 int lua_bit_any(lua_State* LS)
292 return lua_symmetric_bitwise(LS, combine_any, 0);
295 int lua_bit_all(lua_State* LS)
297 return lua_symmetric_bitwise(LS, combine_all, BITWISE_MASK);
300 int lua_bit_parity(lua_State* LS)
302 return lua_symmetric_bitwise(LS, combine_parity, 0);
305 int lua_bit_lrotate(lua_State* LS)
307 return lua_shifter(LS, shift_lrotate);
310 int lua_bit_rrotate(lua_State* LS)
312 return lua_shifter(LS, shift_rrotate);
315 int lua_bit_lshift(lua_State* LS)
317 return lua_shifter(LS, shift_lshift);
320 int lua_bit_arshift(lua_State* LS)
322 return lua_shifter(LS, shift_arshift);
325 int lua_bit_lrshift(lua_State* LS)
327 return lua_shifter(LS, shift_lrshift);
330 int lua_print(lua_State* LS)
332 int stacksize = 0;
333 while(!lua_isnone(LS, stacksize + 1))
334 stacksize++;
335 std::string toprint;
336 bool first = true;
337 for(int i = 0; i < stacksize; i++) {
338 size_t len;
339 const char* tmp = NULL;
340 if(lua_isnil(LS, i + 1)) {
341 tmp = "nil";
342 len = 3;
343 } else if(lua_isboolean(LS, i + 1) && lua_toboolean(LS, i + 1)) {
344 tmp = "true";
345 len = 4;
346 } else if(lua_isboolean(LS, i + 1) && !lua_toboolean(LS, i + 1)) {
347 tmp = "false";
348 len = 5;
349 } else {
350 tmp = lua_tolstring(LS, i + 1, &len);
351 if(!tmp) {
352 tmp = "(unprintable)";
353 len = 13;
356 std::string localmsg(tmp, tmp + len);
357 if(first)
358 toprint = localmsg;
359 else
360 toprint = toprint + "\t" + localmsg;
361 first = false;
363 tmp_win->message(toprint);
364 return 0;
367 int lua_gui_resolution(lua_State* LS)
369 if(!rctx)
370 return 0;
371 lua_pushnumber(LS, rctx->width);
372 lua_pushnumber(LS, rctx->height);
373 lua_pushnumber(LS, rctx->rshift);
374 lua_pushnumber(LS, rctx->gshift);
375 lua_pushnumber(LS, rctx->bshift);
376 return 5;
379 int lua_gui_set_gap(lua_State* LS, uint32_t lua_render_context::*gap)
381 if(!rctx)
382 return 0;
383 uint32_t g = get_numeric_argument<uint32_t>(LS, 1, "gui.<direction>_gap");
384 if(g > 8192)
385 return 0; //Ignore ridiculous gap.
386 rctx->*gap = g;
387 return 0;
390 int lua_gui_set_left_gap(lua_State* LS)
392 return lua_gui_set_gap(LS, &lua_render_context::left_gap);
395 int lua_gui_set_right_gap(lua_State* LS)
397 return lua_gui_set_gap(LS, &lua_render_context::right_gap);
400 int lua_gui_set_top_gap(lua_State* LS)
402 return lua_gui_set_gap(LS, &lua_render_context::top_gap);
405 int lua_gui_set_bottom_gap(lua_State* LS)
407 return lua_gui_set_gap(LS, &lua_render_context::bottom_gap);
410 int lua_gui_text(lua_State* LS)
412 if(!rctx)
413 return 0;
414 uint32_t x255 = 255;
415 uint32_t fgc = (x255 << rctx->rshift) | (x255 << rctx->gshift) | (x255 << rctx->bshift);
416 uint32_t bgc = 0;
417 uint16_t fga = 256;
418 uint16_t bga = 0;
419 int32_t _x = get_numeric_argument<int32_t>(LS, 1, "gui.text");
420 int32_t _y = get_numeric_argument<int32_t>(LS, 2, "gui.text");
421 get_numeric_argument<uint32_t>(LS, 4, fgc, "gui.text");
422 get_numeric_argument<uint16_t>(LS, 5, fga, "gui.text");
423 get_numeric_argument<uint32_t>(LS, 6, bgc, "gui.text");
424 get_numeric_argument<uint16_t>(LS, 7, bga, "gui.text");
425 std::string text = get_string_argument(LS, 3, "gui.text");
426 rctx->queue->add(*new render_object_text(_x, _y, text, fgc, fga, bgc, bga));
427 return 0;
430 int lua_gui_request_repaint(lua_State* LS)
432 lua_requests_repaint = true;
433 return 0;
436 int lua_gui_update_subframe(lua_State* LS)
438 lua_requests_subframe_paint = get_boolean_argument(LS, 1, "gui.subframe_update");
439 return 0;
442 int lua_exec(lua_State* LS)
444 std::string text = get_string_argument(LS, 1, "exec");
445 cmdhnd->docommand(text, tmp_win);
446 return 0;
449 template<typename T, typename U>
450 int lua_read_memory(lua_State* LS, U (*rfun)(uint32_t addr))
452 uint32_t addr = get_numeric_argument<uint32_t>(LS, 1, "memory.read<type>");
453 lua_pushnumber(LS, static_cast<T>(rfun(addr)));
454 return 1;
457 template<typename T>
458 int lua_write_memory(lua_State* LS, bool (*wfun)(uint32_t addr, T value))
460 uint32_t addr = get_numeric_argument<uint32_t>(LS, 1, "memory.write<type>");
461 T value = get_numeric_argument<T>(LS, 2, "memory.write<type>");
462 wfun(addr, value);
463 return 0;
466 int lua_memory_readbyte(lua_State* LS)
468 return lua_read_memory<uint8_t, uint8_t>(LS, memory_read_byte);
471 int lua_memory_readsbyte(lua_State* LS)
473 return lua_read_memory<int8_t, uint8_t>(LS, memory_read_byte);
476 int lua_memory_readword(lua_State* LS)
478 return lua_read_memory<uint16_t, uint16_t>(LS, memory_read_word);
481 int lua_memory_readsword(lua_State* LS)
483 return lua_read_memory<int16_t, uint16_t>(LS, memory_read_word);
486 int lua_memory_readdword(lua_State* LS)
488 return lua_read_memory<uint32_t, uint32_t>(LS, memory_read_dword);
491 int lua_memory_readsdword(lua_State* LS)
493 return lua_read_memory<int32_t, uint32_t>(LS, memory_read_dword);
496 int lua_memory_readqword(lua_State* LS)
498 return lua_read_memory<uint64_t, uint64_t>(LS, memory_read_qword);
501 int lua_memory_readsqword(lua_State* LS)
503 return lua_read_memory<int64_t, uint64_t>(LS, memory_read_qword);
506 int lua_memory_writebyte(lua_State* LS)
508 return lua_write_memory(LS, memory_write_byte);
511 int lua_memory_writeword(lua_State* LS)
513 return lua_write_memory(LS, memory_write_word);
516 int lua_memory_writedword(lua_State* LS)
518 return lua_write_memory(LS, memory_write_dword);
521 int lua_memory_writeqword(lua_State* LS)
523 return lua_write_memory(LS, memory_write_qword);
526 int lua_input_set(lua_State* LS)
528 if(!controllerdata)
529 return 0;
530 unsigned controller = get_numeric_argument<unsigned>(LS, 1, "input.set");
531 unsigned index = get_numeric_argument<unsigned>(LS, 2, "input.set");
532 short value = get_numeric_argument<short>(LS, 3, "input.set");
533 if(controller > 7 || index > 11)
534 return 0;
535 (*controllerdata)(controller >> 2, controller & 3, index) = value;
536 return 0;
539 int lua_input_get(lua_State* LS)
541 if(!controllerdata)
542 return 0;
543 unsigned controller = get_numeric_argument<unsigned>(LS, 1, "input.set");
544 unsigned index = get_numeric_argument<unsigned>(LS, 2, "input.set");
545 if(controller > 7 || index > 11)
546 return 0;
547 lua_pushnumber(LS, (*controllerdata)(controller >> 2, controller & 3, index));
548 return 1;
551 int lua_input_reset(lua_State* LS)
553 if(!controllerdata)
554 return 0;
555 long cycles = 0;
556 get_numeric_argument(LS, 1, cycles, "input.reset");
557 if(cycles < 0)
558 return 0;
559 short lo = cycles % 10000;
560 short hi = cycles / 10000;
561 (*controllerdata)(CONTROL_SYSTEM_RESET) = 1;
562 (*controllerdata)(CONTROL_SYSTEM_RESET_CYCLES_HI) = hi;
563 (*controllerdata)(CONTROL_SYSTEM_RESET_CYCLES_LO) = lo;
564 return 0;
567 int lua_hostmemory_read(lua_State* LS)
569 size_t address = get_numeric_argument<size_t>(LS, 1, "hostmemory.read");
570 auto& h = get_host_memory();
571 if(address >= h.size()) {
572 lua_pushboolean(LS, 0);
573 return 1;
575 lua_pushnumber(LS, static_cast<uint8_t>(h[address]));
576 return 1;
579 int lua_hostmemory_write(lua_State* LS)
581 size_t address = get_numeric_argument<size_t>(LS, 1, "hostmemory.write");
582 uint8_t value = get_numeric_argument<uint8_t>(LS, 2, "hostmemory.write");
583 auto& h = get_host_memory();
584 if(address >= h.size())
585 h.resize(address + 1);
586 h[address] = value;
587 return 0;
590 int lua_movie_currentframe(lua_State* LS)
592 auto& m = get_movie();
593 lua_pushnumber(LS, m.get_current_frame());
594 return 1;
597 int lua_movie_framecount(lua_State* LS)
599 auto& m = get_movie();
600 lua_pushnumber(LS, m.get_frame_count());
601 return 1;
604 int lua_movie_readonly(lua_State* LS)
606 auto& m = get_movie();
607 lua_pushboolean(LS, m.readonly_mode() ? 1 : 0);
608 return 1;
611 int lua_movie_set_readwrite(lua_State* LS)
613 auto& m = get_movie();
614 m.readonly_mode(false);
615 return 0;
618 int lua_movie_frame_subframes(lua_State* LS)
620 uint64_t frame = get_numeric_argument<uint64_t>(LS, 1, "movie.frame_subframes");
621 auto& m = get_movie();
622 lua_pushnumber(LS, m.frame_subframes(frame));
623 return 1;
626 int lua_movie_read_subframe(lua_State* LS)
628 uint64_t frame = get_numeric_argument<uint64_t>(LS, 1, "movie.frame_subframes");
629 uint64_t subframe = get_numeric_argument<uint64_t>(LS, 2, "movie.frame_subframes");
630 auto& m = get_movie();
631 controls_t r = m.read_subframe(frame, subframe);
632 lua_newtable(LS);
633 for(size_t i = 0; i < TOTAL_CONTROLS; i++) {
634 lua_pushnumber(LS, i);
635 lua_pushnumber(LS, r(i));
636 lua_settable(L, -3);
638 return 1;
644 void lua_callback_do_paint(struct lua_render_context* ctx, window* win) throw()
646 if(!callback_exists("on_paint"))
647 return;
648 rctx = ctx;
649 run_lua_cb(0, win);
650 rctx = NULL;
653 void lua_callback_do_video(struct lua_render_context* ctx, window* win) throw()
655 if(!callback_exists("on_video"))
656 return;
657 rctx = ctx;
658 run_lua_cb(0, win);
659 rctx = NULL;
662 void lua_callback_do_reset(window* win) throw()
664 if(!callback_exists("on_reset"))
665 return;
666 run_lua_cb(0, win);
669 void lua_callback_do_readwrite(window* win) throw()
671 if(!callback_exists("on_readwrite"))
672 return;
673 run_lua_cb(0, win);
676 void lua_callback_startup(window* win) throw()
678 if(!callback_exists("on_startup"))
679 return;
680 run_lua_cb(0, win);
683 void lua_callback_pre_load(const std::string& name, window* win) throw()
685 if(!callback_exists("on_pre_load"))
686 return;
687 push_string(name);
688 run_lua_cb(1, win);
691 void lua_callback_err_load(const std::string& name, window* win) throw()
693 if(!callback_exists("on_err_load"))
694 return;
695 push_string(name);
696 run_lua_cb(1, win);
699 void lua_callback_post_load(const std::string& name, bool was_state, window* win) throw()
701 if(!callback_exists("on_post_load"))
702 return;
703 push_string(name);
704 push_boolean(was_state);
705 run_lua_cb(2, win);
708 void lua_callback_pre_save(const std::string& name, bool is_state, window* win) throw()
710 if(!callback_exists("on_pre_save"))
711 return;
712 push_string(name);
713 push_boolean(is_state);
714 run_lua_cb(2, win);
717 void lua_callback_err_save(const std::string& name, window* win) throw()
719 if(!callback_exists("on_err_save"))
720 return;
721 push_string(name);
722 run_lua_cb(1, win);
725 void lua_callback_post_save(const std::string& name, bool is_state, window* win) throw()
727 if(!callback_exists("on_post_save"))
728 return;
729 push_string(name);
730 push_boolean(is_state);
731 run_lua_cb(2, win);
734 void lua_callback_do_input(controls_t& data, bool subframe, window* win) throw()
736 if(!callback_exists("on_input"))
737 return;
738 controllerdata = &data;
739 push_boolean(subframe);
740 run_lua_cb(1, win);
741 controllerdata = NULL;
745 bool lua_command(const std::string& cmd, window* win) throw(std::bad_alloc)
747 if(is_cmd_prefix(cmd, "eval-lua")) {
748 tokensplitter t(cmd);
749 std::string dummy = t;
750 do_eval_lua(t.tail(), win);
751 return true;
753 if(is_cmd_prefix(cmd, "run-lua")) {
754 tokensplitter t(cmd);
755 std::string dummy = t;
756 do_run_lua(t.tail(), win);
757 return true;
759 return false;
762 void lua_callback_quit(window* win) throw()
764 if(!callback_exists("on_quit"))
765 return;
766 run_lua_cb(0, win);
769 void lua_set_commandhandler(commandhandler& cmdh) throw()
771 cmdhnd = &cmdh;
774 void init_lua(window* win) throw()
776 L = lua_newstate(alloc, NULL);
777 if(!L) {
778 out(win) << "Can't initialize Lua." << std::endl;
779 fatal_error(win);
781 luaL_openlibs(L);
783 //Some globals
784 lua_pushcfunction(L, lua_print);
785 lua_setglobal(L, "print");
786 lua_pushcfunction(L, lua_exec);
787 lua_setglobal(L, "exec");
789 //Bit table.
790 lua_newtable(L);
791 SETFIELDFUN(L, -1, "none", lua_bit_none);
792 SETFIELDFUN(L, -1, "bnot", lua_bit_none);
793 SETFIELDFUN(L, -1, "any", lua_bit_any);
794 SETFIELDFUN(L, -1, "bor", lua_bit_any);
795 SETFIELDFUN(L, -1, "all", lua_bit_all);
796 SETFIELDFUN(L, -1, "band", lua_bit_all);
797 SETFIELDFUN(L, -1, "parity", lua_bit_parity);
798 SETFIELDFUN(L, -1, "bxor", lua_bit_parity);
799 SETFIELDFUN(L, -1, "lrotate", lua_bit_lrotate);
800 SETFIELDFUN(L, -1, "rrotate", lua_bit_rrotate);
801 SETFIELDFUN(L, -1, "lshift", lua_bit_lshift);
802 SETFIELDFUN(L, -1, "arshift", lua_bit_arshift);
803 SETFIELDFUN(L, -1, "lrshift", lua_bit_lrshift);
804 lua_setglobal(L, "bit");
806 //Gui table.
807 lua_newtable(L);
808 SETFIELDFUN(L, -1, "resolution", lua_gui_resolution);
809 SETFIELDFUN(L, -1, "left_gap", lua_gui_set_left_gap);
810 SETFIELDFUN(L, -1, "right_gap", lua_gui_set_right_gap);
811 SETFIELDFUN(L, -1, "top_gap", lua_gui_set_top_gap);
812 SETFIELDFUN(L, -1, "bottom_gap", lua_gui_set_bottom_gap);
813 SETFIELDFUN(L, -1, "text", lua_gui_text);
814 SETFIELDFUN(L, -1, "repaint", lua_gui_request_repaint);
815 SETFIELDFUN(L, -1, "subframe_update", lua_gui_update_subframe);
816 lua_setglobal(L, "gui");
818 //Memory table.
819 lua_newtable(L);
820 SETFIELDFUN(L, -1, "readbyte", lua_memory_readbyte);
821 SETFIELDFUN(L, -1, "readsbyte", lua_memory_readsbyte);
822 SETFIELDFUN(L, -1, "writebyte", lua_memory_writebyte);
823 SETFIELDFUN(L, -1, "readword", lua_memory_readword);
824 SETFIELDFUN(L, -1, "readsword", lua_memory_readsword);
825 SETFIELDFUN(L, -1, "writeword", lua_memory_writeword);
826 SETFIELDFUN(L, -1, "readdword", lua_memory_readdword);
827 SETFIELDFUN(L, -1, "readsdword", lua_memory_readsdword);
828 SETFIELDFUN(L, -1, "writedword", lua_memory_writedword);
829 SETFIELDFUN(L, -1, "readqword", lua_memory_readqword);
830 SETFIELDFUN(L, -1, "readsqword", lua_memory_readsqword);
831 SETFIELDFUN(L, -1, "writeqword", lua_memory_writeqword);
832 lua_setglobal(L, "memory");
834 //Input table
835 lua_newtable(L);
836 SETFIELDFUN(L, -1, "get", lua_input_get);
837 SETFIELDFUN(L, -1, "set", lua_input_set);
838 SETFIELDFUN(L, -1, "reset", lua_input_reset);
839 lua_setglobal(L, "input");
841 //Hostmemory table.
842 lua_newtable(L);
843 SETFIELDFUN(L, -1, "read", lua_hostmemory_read);
844 SETFIELDFUN(L, -1, "write", lua_hostmemory_write);
845 lua_setglobal(L, "hostmemory");
847 //Movie table.
848 lua_newtable(L);
849 SETFIELDFUN(L, -1, "currentframe", lua_movie_currentframe);
850 SETFIELDFUN(L, -1, "frame_subframes", lua_movie_frame_subframes);
851 SETFIELDFUN(L, -1, "framecount", lua_movie_framecount);
852 SETFIELDFUN(L, -1, "read_subframe", lua_movie_read_subframe);
853 SETFIELDFUN(L, -1, "readonly", lua_movie_readonly);
854 SETFIELDFUN(L, -1, "set_readwrite", lua_movie_set_readwrite);
855 lua_setglobal(L, "movie");
857 //TODO: Add some functions into the Lua state.
860 bool lua_requests_repaint = false;
861 bool lua_requests_subframe_paint = false;