All Downloads are FREE. Search and download functionalities are using the official Maven repository.

party.iroiro.luajava.Lua51 Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
package party.iroiro.luajava;

import java.util.concurrent.atomic.AtomicReference;

import static party.iroiro.luajava.Lua51Consts.*;

/**
 * A thin wrapper around a Lua 5.1 Lua state
 */
public class Lua51 extends AbstractLua {
    private final static AtomicReference natives = new AtomicReference<>();

    /**
     * Creates a new Lua state
     *
     * @throws UnsatisfiedLinkError if Lua 5.1 natives unavailable
     */
    public Lua51() throws UnsatisfiedLinkError {
        super(getNatives());
    }

    protected Lua51(long L, int id, AbstractLua main) {
        super(main.getLuaNative(), L, id, main);
    }

    private static LuaNative getNatives() throws UnsatisfiedLinkError {
        synchronized (natives) {
            if (natives.get() == null) {
                try {
                    natives.set(new Lua51Natives());
                } catch (IllegalStateException e) {
                    throw new UnsatisfiedLinkError("Unable to find natives or init");
                }
            }
            return natives.get();
        }
    }

    @Override
    protected AbstractLua newThread(long L, int id, AbstractLua mainThread) {
        return new Lua51(L, id, mainThread);
    }

    @Override
    public LuaError convertError(int code) {
        switch (code) {
            case 0:
                return LuaError.OK;
            case LUA_YIELD:
                return LuaError.YIELD;
            case LUA_ERRRUN:
                return LuaError.RUNTIME;
            case LUA_ERRSYNTAX:
                return LuaError.SYNTAX;
            case LUA_ERRMEM:
                return LuaError.MEMORY;
            case LUA_ERRERR:
                return LuaError.HANDLER;
            default:
                return null;
        }
    }

    @Override
    public LuaType convertType(int code) {
        switch (code) {
            case LUA_TBOOLEAN:
                return LuaType.BOOLEAN;
            case LUA_TFUNCTION:
                return LuaType.FUNCTION;
            case LUA_TLIGHTUSERDATA:
                return LuaType.LIGHTUSERDATA;
            case LUA_TNIL:
                return LuaType.NIL;
            case LUA_TNONE:
                return LuaType.NONE;
            case LUA_TNUMBER:
                return LuaType.NUMBER;
            case LUA_TSTRING:
                return LuaType.STRING;
            case LUA_TTABLE:
                return LuaType.TABLE;
            case LUA_TTHREAD:
                return LuaType.THREAD;
            case LUA_TUSERDATA:
                return LuaType.USERDATA;
            default:
                return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy