public static LuaValue runInputStreamLua(InputStream inputStream) throws Exception { Prototype luaScriptPrototype = LuaC.instance.compile(inputStream, ""); Globals globals = JsePlatform.standardGlobals(); LuaValue chunk = globals.load("require 'com.example.import-me';"); chunk.call(); LuaClosure luaClosure = new LuaClosure(luaScriptPrototype, globals); return luaClosure.call(); }
public LuaValue getPackage(String packageName) { return globals.get(packageName); }
protected final void register(ILApi api) { if (api instanceof LuaValue) { mGlobals.set(api.apiName(), (LuaValue) api); } }
/** Convenience function for loading a file that is either binary lua or lua source. * @param filename Name of the file to load. * @return LuaValue that can be call()'ed or invoke()'ed. * @throws LuaError if the file could not be loaded. */ public LuaValue loadfile(String filename) { try { return load(finder.findResource(filename), "@"+filename, "bt", this); } catch (Exception e) { return error("load "+filename+": "+e); } }
/** Load the content form an input stream as a binary chunk or text file. * @param is InputStream containing a lua script or compiled lua" * @param chunkname Name that will be used within the chunk as the source. * @param mode String containing 'b' or 't' or both to control loading as binary or text or either. * @param environment LuaTable to be used as the environment for the loaded function. * */ public LuaValue load(InputStream is, String chunkname, String mode, LuaValue environment) { try { Prototype p = loadPrototype(is, chunkname, mode); return loader.load(p, chunkname, environment); } catch (LuaError l) { throw l; } catch (Exception e) { return error("load "+chunkname+": "+e); } }
public LuaInstance() throws AerospikeException { globals.load(new JseBaseLib()); globals.load(new PackageLib()); //globals.load(new Bit32Lib()); // not needed for 5.1 compatibility globals.load(new TableLib()); globals.load(new StringLib()); globals.load(new CoroutineLib()); globals.load(new JseMathLib()); globals.load(new JseIoLib()); globals.load(new JseOsLib()); globals.load(new LuajavaLib()); globals.load(new DebugLib()); LuaTable packageTable = (LuaTable)globals.get("package"); loadedTable = (LuaTable)packageTable.get("loaded"); globals.load(new LuaBytesLib(this)); globals.load(new LuaListLib(this)); globals.load(new LuaMapLib(this)); globals.load(new LuaStreamLib(this)); LuaC.install(globals); ClassLoader resourceLoader = LuaInstance.class.getClassLoader(); loadSystemPackage(resourceLoader, "compat52"); loadSystemPackage(resourceLoader, "stream_ops"); loadSystemPackage(resourceLoader, "aerospike"); globals.load(new LuaAerospikeLib(this)); LoadState.install(globals); }
private Globals createLimitGlobals(){ Globals globals = new Globals(); globals.load(new JseBaseLib()); globals.load(new PackageLib()); globals.load(new Bit32Lib()); globals.load(new TableLib()); globals.load(new StringLib()); globals.load(new CoroutineLib()); globals.load(new JseMathLib()); globals.load(new JseOsLib()); globals.load(new LuajavaLib(RapidLuaLimitPath.msLimitArray, null)); LoadState.install(globals); LuaC.install(globals); return globals; }
this.pluginMain = globals.load(new InputStreamReader(this.pluginFile.getResource(this.descriptor.getMain()), "UTF-8"), this.descriptor.getMain()); } catch (UnsupportedEncodingException e) { throw new InvalidPluginException("File could not be loaded using UTF-8.", e.getCause()); this.loadConfigWithChecks(); globals.set("plugin", new PluginWrapper(this)); globals.set("logger", new LoggerWrapper(this)); globals.set("util", new UtilitiesWrapper(this)); globals.set("config", new ConfigWrapper(this)); OneArgFunction oldRequire = (OneArgFunction) globals.get("require"); globals.set("require", new OneArgFunction() { @Override public LuaValue call(LuaValue luaValue) { globals.set("import", new OneArgFunction() { @Override public LuaValue call(LuaValue luaValue) { globals.set("newInstance", new VarArgFunction() { @Override public LuaValue call(LuaValue cls, LuaValue args) {
private static Globals getGlobals(Context context, XHook hook, Map<String, String> settings) { Globals globals = JsePlatform.standardGlobals(); // base, bit32, coroutine, io, math, os, package, string, table, luajava if (BuildConfig.DEBUG) globals.load(new DebugLib()); globals.set("log", new LuaLog(context.getPackageName(), context.getApplicationInfo().uid, hook.getId())); globals.set("hook", new LuaHook(context, settings)); return new LuaLocals(globals); }
private Globals createSandboxedGlobals() { Globals result = new Globals(); result.load(new JseBaseLib()); result.load(new PackageLib()); result.load(new Bit32Lib()); result.load(new TableLib()); result.load(new StringLib()); result.load(new JseMathLib()); result.load(new DebugLib()); result.set("debug", LuaValue.NIL); return result; } }
globals.set(variableName, CoerceJavaToLua.coerce(bindings.get(variableName))); globals.set(variableName, CoerceJavaToLua.coerce(null)); LuaValue [] keys = globals.keys(); for(int i = 0; i < keys.length; i++) { LuaValue key = keys[i]; LuaValue value = globals.get(key); switch ( value.type() ) { case LuaValue.TNIL: globals.set(variableName, CoerceJavaToLua.coerce(null));
private void extendGlobals(final Globals globals) { globals.set("setfenv", new TwoArgFunction() { @Override public LuaValue call(LuaValue f, LuaValue env) { globals.set("gefenv", new OneArgFunction() { public LuaValue call(LuaValue f) { return globals; globals.set("unpack", new unpack()); LuaValue math = globals.get("math"); math.set("log10", new OneArgFunction() { @Override LuaValue table = globals.get("table"); table.set("maxn", new OneArgFunction() { @Override public LuaValue call(LuaValue arg) {
/** Compile lua source from a Reader into a Prototype. The characters in the reader * are converted to bytes using the UTF-8 encoding, so a string literal containing * characters with codepoints 128 or above will be converted into multiple bytes. */ public Prototype compilePrototype(Reader reader, String chunkname) throws IOException { return compilePrototype(new UTF8Stream(reader), chunkname); }
/** Load lua source or lua binary from an input stream into a Prototype. * The InputStream is either a binary lua chunk starting with the lua binary chunk signature, * or a text input file. If it is a text input file, it is interpreted as a UTF-8 byte sequence. * @param is Input stream containing a lua script or compiled lua" * @param chunkname Name that will be used within the chunk as the source. * @param mode String containing 'b' or 't' or both to control loading as binary or text or either. */ public Prototype loadPrototype(InputStream is, String chunkname, String mode) throws IOException { if (mode.indexOf('b') >= 0) { if (undumper == null) error("No undumper."); if (!is.markSupported()) is = new BufferedStream(is); is.mark(4); final Prototype p = undumper.undump(is, chunkname); if (p != null) return p; is.reset(); } if (mode.indexOf('t') >= 0) { return compilePrototype(is, chunkname); } error("Failed to load prototype "+chunkname+" using mode '"+mode+"'"); return null; }
binary = global.loadPrototype(streamFile, name, "b"); binary = global.compilePrototype(streamFile, name);
/** Compile lua source from an InputStream into a Prototype. * The input is assumed to be UTf-8, but since bytes in the range 128-255 are passed along as * literal bytes, any ASCII-compatible encoding such as ISO 8859-1 may also be used. */ public Prototype compilePrototype(InputStream stream, String chunkname) throws IOException { if (compiler == null) error("No compiler."); return compiler.compile(stream, chunkname); }
private Globals createGlobals(KeyValueObjectCache aObjectCache) { Globals theGlobals = new Globals(); // globals.load(new BaseLib()); // globals.load(new PackageLib()); // globals.load(new Bit32Lib()); // globals.load(new OsLib()); // globals.load(new MathLib()); // globals.load(new TableLib()); // globals.load(new StringLib()); // globals.load(new CoroutineLib()); // globals.load(new JmeIoLib()); // LoadState.install(globals); LuaC.install(theGlobals); LuaJScriptEngine.registerTo(theGlobals, buildInFunctions, aObjectCache); return theGlobals; }
public Varargs invoke(Varargs args) { LuaString name = args.checkstring(1); InputStream is = null; // get package path LuaValue path = package_.get(_PATH); if ( ! path.isstring() ) return valueOf("package.path is not a string"); // get the searchpath function. Varargs v = package_.get(_SEARCHPATH).invoke(varargsOf(name, path)); // Did we get a result? if (!v.isstring(1)) return v.arg(2).tostring(); LuaString filename = v.arg1().strvalue(); // Try to load the file. v = globals.loadfile(filename.tojstring()); if ( v.arg1().isfunction() ) return LuaValue.varargsOf(v.arg1(), filename); // report error return varargsOf(NIL, valueOf("'"+filename+"': "+v.arg(2).tojstring())); } }
public LuaInstance() throws AerospikeException { globals.load(new JseBaseLib()); globals.load(new PackageLib()); globals.load(new TableLib()); globals.load(new StringLib()); globals.load(new CoroutineLib()); globals.load(new JseMathLib()); globals.load(new JseIoLib()); globals.load(new JseOsLib()); globals.load(new LuajavaLib()); globals.load(new DebugLib()); LuaTable packageTable = (LuaTable)globals.get("package"); loadedTable = (LuaTable)packageTable.get("loaded"); globals.load(new LuaBytesLib(this)); globals.load(new LuaListLib(this)); globals.load(new LuaMapLib(this)); globals.load(new LuaStreamLib(this)); loadSystemPackage(resourceLoader, "aerospike"); globals.load(new LuaAerospikeLib(this)); LoadState.install(globals);