PPaste!

Home - All the pastes - Authored by Thooms

Raw version

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdbool.h>
#include <stdio.h>

#include <lauxlib.h>
#include <lua.h>
#include <luaconf.h>
#include <lualib.h>
#include <psp2/kernel/processmgr.h>

#define MAIN_SCRIPT "main.lua"

int pmain(lua_State *L);

int main(int argc, char *argv[]) {
    int status;

    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    lua_pushcfunction(L, &pmain);
    status = lua_pcall(L, 0, 0, 0);

    if (status != LUA_OK)
        printf("An error occured.\n");

    lua_close(L);

    return 0;
}

/* pmain
 * Protected main (https://www.lua.org/pil/24.3.1.html)
 */
int pmain(lua_State *L) {
    int status;

    luaL_checkversion(L);

    while (true) {
        status = luaL_loadfile(L, MAIN_SCRIPT);
        if (status == LUA_OK)
            status = lua_pcall(L, 0, LUA_MULTRET, 0);

        // Retry only if an error occured
        if (status == LUA_OK)
            break;

        printf("Error: %s\n", lua_tostring(L, -1));
        lua_pop(L, 1);

        // getchar();
    }

    return 0;
}