From 1626aad3032b3c8b6c2ddd4b01f3998e0572658c Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Tue, 29 Jan 2013 04:22:14 +0000 Subject: [PATCH] Add convenience function to load a String for execution. --- src/core/org/luaj/vm2/Globals.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/core/org/luaj/vm2/Globals.java b/src/core/org/luaj/vm2/Globals.java index ee0280ee..66b35aa2 100644 --- a/src/core/org/luaj/vm2/Globals.java +++ b/src/core/org/luaj/vm2/Globals.java @@ -100,7 +100,18 @@ public class Globals extends LuaTable { Varargs v = baselib.loadFile(filename, "bt", this); return !v.isnil(1)? v.arg1(): error(v.arg(2).tojstring()); } - + + /** Convenience function to load a string value as a script. + * @param script Contents of a lua script, such as "print 'hello, world.'" + * @param chunkname Name that will be used within the chunk as the source. + * @return LuaValue that may be executed via .call(), .invoke(), or .method() calls. + * @throws LuaError if the script could not be compiled. + */ + public LuaValue loadString(String script, String chunkname) { + Varargs v = baselib.loadStream(valueOf(script).toInputStream(), chunkname, "bt", this); + return !v.isnil(1)? v.arg1(): error(v.arg(2).tojstring()); + } + /** Function which yields the current thread. * @param args Arguments to supply as return values in the resume function of the resuming thread. * @return Values supplied as arguments to the resume() call that reactivates this thread.