Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // include psp stuff
- #include <pspkernel.h>
- #include <pspdebug.h>
- #include <pspdisplay.h>
- #include <pspctrl.h>
- // include lua stuff
- #include "lua.h"
- #include "lualib.h"
- #include "lauxlib.h"
- #include "lua_lib.h"
- // configure PSP stuff
- #define VERS 1
- #define REVS 0
- PSP_MODULE_INFO("LUATEST", PSP_MODULE_USER, VERS, REVS);
- PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER | THREAD_ATTR_VFPU);
- // make printing easier on us
- #define printf pspDebugScreenPrintf
- // basic callback stuff
- static int exitRequest = 0;
- int isRunning()
- {
- return !exitRequest;
- }
- int exitCallback(int arg1, int arg2, void *common)
- {
- exitRequest = 1;
- return 0;
- }
- int callbackThread(SceSize args, void *argp)
- {
- int callbackID;
- callbackID = sceKernelCreateCallback("Exit Callback", exitCallback, NULL);
- sceKernelRegisterExitCallback(callbackID);
- sceKernelSleepThreadCB();
- return 0;
- }
- int setupExitCallback()
- {
- int threadID = 0;
- threadID = sceKernelCreateThread("Callback Update Thread", callbackThread, 0x11, 0xFA0, THREAD_ATTR_USER, 0);
- if(threadID >= 0)
- {
- sceKernelStartThread(threadID, 0, 0);
- }
- return threadID;
- }
- static void stackDump(lua_State *L) {
- int i=lua_gettop(L);
- printf(" ---------------- Stack Dump ----------------\n" );
- while(i) {
- int t = lua_type(L, i);
- switch (t) {
- case LUA_TSTRING:
- printf("%d:`%s'\n", i, lua_tostring(L, i));
- break;
- case LUA_TBOOLEAN:
- printf("%d: %s\n",i,lua_toboolean(L, i) ? "true" : "false");
- break;
- case LUA_TNUMBER:
- printf("%d: %g\n", i, lua_tonumber(L, i));
- break;
- default: printf("%d: %s\n", i, lua_typename(L, t)); break;
- }
- i--;
- }
- printf("--------------- Stack Dump Finished ---------------\n" );
- }
- int main(int argc, char** argv)
- {
- // basic init
- setupExitCallback();
- pspDebugScreenInit();
- // script location
- const char* scriptPath = "umd0:/script.lua";
- // init Lua, its libraries, and do a dry run for initialization
- lua_State *L = lua_lib_newstate();
- int status = 0;
- // cache the file in lua registry
- status = luaL_loadfile(L, scriptPath);
- if(status != 0) {
- // break for alerting we have a compilation error
- while(isRunning()) {
- pspDebugScreenSetXY(0, 0);
- stackDump(L);
- printf("error code %d: %s", status, lua_tostring(L, -1));
- sceDisplayWaitVblankStart();
- }
- }
- int lua_script = luaL_ref(L, LUA_REGISTRYINDEX);
- // run main logic, call script, check for errors
- while (isRunning()) {
- pspDebugScreenSetXY(0, 0);
- lua_rawgeti(L, LUA_REGISTRYINDEX, lua_script); // get script into stack
- status = lua_pcall(L, 0, 0, 0); // run the script
- if(status != 0) {
- // alerting that we have a runtime error
- stackDump(L); // a dirty stack with error string
- printf(lua_tostring(L, -1)); // print last push, ie the error
- lua_pop(L, 1); // pop the error arg
- stackDump(L); // a clean stack
- }
- sceDisplayWaitVblankStart();
- }
- // clean up
- lua_close(L);
- sceKernelExitGame();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement