void vm_hw_lua_write(vm_t* vm, uint16_t pos, void* ud) { struct lua_hardware* hw = (struct lua_hardware*)ud; int write; lua_getglobal(hw->state, "write"); write = lua_gettop(hw->state); if (lua_isnoneornil(hw->state, write)) { lua_pop(hw->state, 1); return; } // Load function and arguments. lua_pushvalue(hw->state, write); vm_hw_lua_cpu_push_cpu(hw->state, vm); lua_pushnumber(hw->state, pos); // Call the function. if (lua_pcall(hw->state, 2, 0, 0) != 0) { printf("lua error in write was %s.\n", lua_tostring(hw->state, -1)); lua_pop(hw->state, 1); } lua_pop(hw->state, 1); }
void dbg_lua_push_state(struct lua_debugst* ds, struct dbg_state* state, void* ud) { int tbl, tbl_mt; // Create the new table and metatable. lua_newtable(ds->state); tbl = lua_gettop(ds->state); lua_newtable(ds->state); tbl_mt = lua_gettop(ds->state); // Push userdata into metatable. lua_pushlightuserdata(ds->state, state); lua_rawseti(ds->state, tbl_mt, 0); lua_pushlightuserdata(ds->state, ud); lua_rawseti(ds->state, tbl_mt, 1); // Push C functions into table. lua_pushcfunction(ds->state, &dbg_lua_handle_break); lua_setfield(ds->state, tbl, "_break"); lua_pushcfunction(ds->state, &dbg_lua_handle_run); lua_setfield(ds->state, tbl, "run"); lua_pushcfunction(ds->state, &dbg_lua_handle_symbols); lua_setfield(ds->state, tbl, "symbols"); lua_pushcfunction(ds->state, &dbg_lua_handle_lines); lua_setfield(ds->state, tbl, "lines"); lua_pushcfunction(ds->state, &dbg_lua_handle_raise); lua_setfield(ds->state, tbl, "raise"); // Push CPU state into table. vm_hw_lua_cpu_push_cpu(ds->state, state->get_vm()); lua_setfield(ds->state, tbl, "cpu"); // Associate metatables. lua_pushvalue(ds->state, tbl_mt); lua_setmetatable(ds->state, tbl); // Clean up stack. lua_pop(ds->state, lua_gettop(ds->state) - tbl); // New table is now at the top of the stack. }
void vm_hw_lua_interrupt(vm_t* vm, void* ud) { struct lua_hardware* hw = (struct lua_hardware*)ud; int interrupt; lua_getglobal(hw->state, "interrupt"); interrupt = lua_gettop(hw->state); if (lua_isnoneornil(hw->state, interrupt)) { lua_pop(hw->state, 1); return; } // Load function and arguments. lua_pushvalue(hw->state, interrupt); vm_hw_lua_cpu_push_cpu(hw->state, vm); // Call the function. if (lua_pcall(hw->state, 1, 0, 0) != 0) { printf("lua error in interrupt was %s.\n", lua_tostring(hw->state, -1)); lua_pop(hw->state, 1); } lua_pop(hw->state, 1); }