// Lua 5.1.5 smoke test on W65816 / Apple IIgs target.
//
// Minimal API test: create state, push/pop, simple type ops.  Doesn't
// exercise the VM — Lua's full interp on a 1 MHz IIgs is workable but
// extremely slow (full script execution measured in minutes for non-
// trivial code).  Verifying that the Lua C API initializes, allocates,
// and tears down correctly is the goal here — that already exercises
// 14 of Lua's 24 .c files (lstate, lapi, lobject, lmem, lstring,
// ltable, ltm, lgc, lzio, lauxlib, lmathlib, llex, lparser, ldo).

#include "lua-5.1.5/src/lua.h"
#include "lua-5.1.5/src/lauxlib.h"


int main(void) {
    lua_State *L = luaL_newstate();
    if (!L) {
        *(volatile unsigned short *)0x025000 = 0xDEAD;
        while (1) {}
    }
    // Push three integers + a string, sum the integers, check the
    // string is properly interned.
    lua_pushinteger(L, 100);
    lua_pushinteger(L, 200);
    lua_pushinteger(L, 300);
    lua_Integer sum = lua_tointeger(L, -1) +
                      lua_tointeger(L, -2) +
                      lua_tointeger(L, -3);
    lua_pushstring(L, "lua");
    int isStr = lua_isstring(L, -1);
    lua_pop(L, 4);

    int stackTop = lua_gettop(L);   // should be 0 after pop-all

    unsigned short result = 0;
    if (sum == 600 && isStr && stackTop == 0) {
        result = 0xC0DE;   // sentinel for full pass
    } else {
        result = 0xBAD0 | ((sum != 600) << 0)
                       | ((!isStr) << 1)
                       | ((stackTop != 0) << 2);
    }
    lua_close(L);

    *(volatile unsigned short *)0x025000 = result;
    while (1) {}
}
