Improve documentation around loading.

This commit is contained in:
James Roseborough
2013-12-30 00:21:17 +00:00
parent de33943eaf
commit 337ff63dbb
2 changed files with 22 additions and 1 deletions

View File

@@ -259,6 +259,18 @@ Loading from a file is done via Globals.loadFile():
LuaValue chunk = globals.loadfile("examples/lua/hello.lua"); LuaValue chunk = globals.loadfile("examples/lua/hello.lua");
</pre> </pre>
Chunks can also be loaded from a <code>Reader</code> as text source
<pre>
chunk = globals.load(new StringReader("print 'hello, world'"), "main.lua");
</pre>
or an InputStream to be loaded as text source "t", or binary lua file "b":
<pre>
chunk = globals.load(new FileInputSStream("examples/lua/hello.lua"), "main.lua", "bt"));
</pre>
<p> <p>
A simple example may be found in A simple example may be found in
<pre> <pre>
@@ -271,7 +283,7 @@ You must include the library <b>lib/luaj-jse-3.0-beta2.jar</b> in your class pat
<h2>Run a script in a MIDlet</h2> <h2>Run a script in a MIDlet</h2>
<p> <p>
The for MIDlets the <em>JmePlatform</em> is used instead: For MIDlets the <em>JmePlatform</em> is used instead:
<pre> <pre>
import org.luaj.vm2.*; import org.luaj.vm2.*;

View File

@@ -197,6 +197,15 @@ public class Globals extends LuaTable {
return load(new StrReader(script), chunkname); return load(new StrReader(script), chunkname);
} }
/** Convenience function to load a string value as a script. Must be lua source.
* @param script Contents of a lua script, such as "print 'hello, world.'"
* @return LuaValue that may be executed via .call(), .invoke(), or .method() calls.
* @throws LuaError if the script could not be compiled.
*/
public LuaValue load(String script) {
return load(new StrReader(script), script);
}
/** Load the content form a reader as a text file. Must be lua source. /** Load the content form a reader as a text file. Must be lua source.
* The source is converted to UTF-8, so any characters appearing in quoted literals * The source is converted to UTF-8, so any characters appearing in quoted literals
* above the range 128 will be converted into multiple bytes. */ * above the range 128 will be converted into multiple bytes. */