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

nl.weeaboo.lua2.compiler.ScriptLoader Maven / Gradle / Ivy

package nl.weeaboo.lua2.compiler;

import static nl.weeaboo.lua2.vm.LuaNil.NIL;
import static nl.weeaboo.lua2.vm.LuaValue.valueOf;
import static nl.weeaboo.lua2.vm.LuaValue.varargsOf;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;

import nl.weeaboo.lua2.LuaRunState;
import nl.weeaboo.lua2.lib.LuaResource;
import nl.weeaboo.lua2.vm.LuaThread;
import nl.weeaboo.lua2.vm.Varargs;

/**
 * Provides user friendly functions for loading Lua scripts.
 */
public final class ScriptLoader {

    private ScriptLoader() {
    }

    /**
     * Load from a named file, returning the loaded chunk or {@code nil,error} if unable to load.
     */
    public static Varargs loadFile(String filename) {
        LuaRunState lrs = LuaRunState.getCurrent();
        LuaResource r = lrs.findResource(filename);
        if (r == null) {
            return varargsOf(NIL, valueOf("cannot open " + filename));
        }

        try {
            final InputStream in = r.open();
            try {
                return loadStream(in, "@" + r.getCanonicalName());
            } finally {
                in.close();
            }
        } catch (IOException e) {
            return varargsOf(NIL, valueOf("cannot open " + filename));
        }
    }

    /**
     * Load from an unnamed input stream, returning the loaded chunk or {@code nil,error} if unable to load.
     */
    public static Varargs loadStream(InputStream is, String chunkname) {
        try {
            if (is == null) {
                return varargsOf(NIL, valueOf("not found: " + chunkname));
            }
            LuaThread running = LuaThread.getRunning();
            return LoadState.load(is, chunkname, running.getfenv());
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            return varargsOf(NIL, valueOf(sw.toString()));
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy