Add options to load to prototype
This commit is contained in:
@@ -24,6 +24,7 @@ package org.luaj.vm2;
|
|||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Loader to load compiled function prototypes
|
** Loader to load compiled function prototypes
|
||||||
@@ -275,10 +276,18 @@ public class LoadState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static LuaFunction load( InputStream stream, String name, LuaValue env ) throws IOException {
|
public static LuaFunction load( InputStream stream, String name, LuaValue env ) throws IOException {
|
||||||
|
Prototype p = compile( stream, name );
|
||||||
|
if ( compiler != null )
|
||||||
|
return compiler.load(p, name, env);
|
||||||
|
else
|
||||||
|
return new LuaClosure( p, env );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Prototype compile( InputStream stream, String name ) throws IOException {
|
||||||
int c = stream.read();
|
int c = stream.read();
|
||||||
if ( c != LUA_SIGNATURE[0] ) {
|
if ( c != LUA_SIGNATURE[0] ) {
|
||||||
if ( compiler != null )
|
if ( compiler != null )
|
||||||
return compiler.load(c, stream, name, env);
|
return compiler.compile(c, stream, name);
|
||||||
throw new LuaError("no compiler");
|
throw new LuaError("no compiler");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,14 +311,9 @@ public class LoadState {
|
|||||||
default:
|
default:
|
||||||
throw new LuaError("unsupported int size");
|
throw new LuaError("unsupported int size");
|
||||||
}
|
}
|
||||||
|
return s.loadFunction( LuaString.valueOf(sname) );
|
||||||
Prototype p = s.loadFunction( LuaString.valueOf(sname) );
|
|
||||||
if ( compiler != null )
|
|
||||||
return compiler.load(p, name, env);
|
|
||||||
else
|
|
||||||
return new LuaClosure( p, env );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getSourceName(String name) {
|
public static String getSourceName(String name) {
|
||||||
String sname = name;
|
String sname = name;
|
||||||
if ( name.startsWith("@") || name.startsWith("=") )
|
if ( name.startsWith("@") || name.startsWith("=") )
|
||||||
|
|||||||
@@ -23,11 +23,11 @@ package org.luaj.vm2.luajc;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
|
||||||
import org.luaj.vm2.LoadState;
|
import org.luaj.vm2.LoadState;
|
||||||
import org.luaj.vm2.LuaClosure;
|
import org.luaj.vm2.LuaClosure;
|
||||||
import org.luaj.vm2.LuaFunction;
|
import org.luaj.vm2.LuaFunction;
|
||||||
import org.luaj.vm2.LuaTable;
|
|
||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
import org.luaj.vm2.Prototype;
|
import org.luaj.vm2.Prototype;
|
||||||
import org.luaj.vm2.LoadState.LuaCompiler;
|
import org.luaj.vm2.LoadState.LuaCompiler;
|
||||||
@@ -92,7 +92,21 @@ public class JavaBytecodeCompiler implements LuaCompiler {
|
|||||||
return gen.generateBytecode(p, toClassname(filename), toSourcename(filename));
|
return gen.generateBytecode(p, toClassname(filename), toSourcename(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Compile all classes produced by a prototype, and put results in a hashtable */
|
||||||
|
public static Hashtable loadClasses( InputStream stream, String filename ) throws IOException {
|
||||||
|
Prototype p = LoadState.compile( stream, filename );
|
||||||
|
Hashtable t = new Hashtable();
|
||||||
|
getInstance().genClass(t, p, toClassname(filename), toSourcename(filename));
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void genClass( Hashtable t, Prototype p, String className, String sourceName ) throws IOException {
|
||||||
|
for ( int i=0, n=p.p!=null? p.p.length: 0; i<n; i++ ) {
|
||||||
|
String name = className + "$" + i;
|
||||||
|
t.put( name, gen.generateBytecode( p.p[i], name, sourceName ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public LuaFunction load(Prototype p, String filename, LuaValue env) {
|
public LuaFunction load(Prototype p, String filename, LuaValue env) {
|
||||||
try {
|
try {
|
||||||
Class c = gen.toJavaBytecode(p, toClassname(filename), toSourcename(filename));
|
Class c = gen.toJavaBytecode(p, toClassname(filename), toSourcename(filename));
|
||||||
|
|||||||
Reference in New Issue
Block a user