Compare commits
1 Commits
v3.0.2
...
mini2Dx/ma
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0e0533e2e6 |
@@ -11,13 +11,13 @@ import org.luaj.vm2.lib.Bit32Lib;
|
|||||||
import org.luaj.vm2.lib.CoroutineLib;
|
import org.luaj.vm2.lib.CoroutineLib;
|
||||||
import org.luaj.vm2.lib.PackageLib;
|
import org.luaj.vm2.lib.PackageLib;
|
||||||
import org.luaj.vm2.lib.ResourceFinder;
|
import org.luaj.vm2.lib.ResourceFinder;
|
||||||
|
import org.luaj.vm2.lib.StringLib;
|
||||||
import org.luaj.vm2.lib.TableLib;
|
import org.luaj.vm2.lib.TableLib;
|
||||||
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
|
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
|
||||||
import org.luaj.vm2.lib.jse.JseBaseLib;
|
import org.luaj.vm2.lib.jse.JseBaseLib;
|
||||||
import org.luaj.vm2.lib.jse.JseIoLib;
|
import org.luaj.vm2.lib.jse.JseIoLib;
|
||||||
import org.luaj.vm2.lib.jse.JseMathLib;
|
import org.luaj.vm2.lib.jse.JseMathLib;
|
||||||
import org.luaj.vm2.lib.jse.JseOsLib;
|
import org.luaj.vm2.lib.jse.JseOsLib;
|
||||||
import org.luaj.vm2.lib.jse.JseStringLib;
|
|
||||||
import org.luaj.vm2.lib.jse.LuajavaLib;
|
import org.luaj.vm2.lib.jse.LuajavaLib;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,7 +76,7 @@ public class SampleApplet extends Applet implements ResourceFinder {
|
|||||||
globals.load(new PackageLib());
|
globals.load(new PackageLib());
|
||||||
globals.load(new Bit32Lib());
|
globals.load(new Bit32Lib());
|
||||||
globals.load(new TableLib());
|
globals.load(new TableLib());
|
||||||
globals.load(new JseStringLib());
|
globals.load(new StringLib());
|
||||||
globals.load(new CoroutineLib());
|
globals.load(new CoroutineLib());
|
||||||
globals.load(new JseMathLib());
|
globals.load(new JseMathLib());
|
||||||
globals.load(new JseIoLib());
|
globals.load(new JseIoLib());
|
||||||
|
|||||||
@@ -1,21 +1,7 @@
|
|||||||
import org.luaj.vm2.Globals;
|
import org.luaj.vm2.*;
|
||||||
import org.luaj.vm2.LoadState;
|
|
||||||
import org.luaj.vm2.LuaBoolean;
|
|
||||||
import org.luaj.vm2.LuaString;
|
|
||||||
import org.luaj.vm2.LuaTable;
|
|
||||||
import org.luaj.vm2.LuaThread;
|
|
||||||
import org.luaj.vm2.LuaValue;
|
|
||||||
import org.luaj.vm2.Varargs;
|
|
||||||
import org.luaj.vm2.compiler.LuaC;
|
import org.luaj.vm2.compiler.LuaC;
|
||||||
import org.luaj.vm2.lib.Bit32Lib;
|
import org.luaj.vm2.lib.*;
|
||||||
import org.luaj.vm2.lib.DebugLib;
|
import org.luaj.vm2.lib.jse.*;
|
||||||
import org.luaj.vm2.lib.PackageLib;
|
|
||||||
import org.luaj.vm2.lib.TableLib;
|
|
||||||
import org.luaj.vm2.lib.TwoArgFunction;
|
|
||||||
import org.luaj.vm2.lib.ZeroArgFunction;
|
|
||||||
import org.luaj.vm2.lib.jse.JseBaseLib;
|
|
||||||
import org.luaj.vm2.lib.jse.JseMathLib;
|
|
||||||
import org.luaj.vm2.lib.jse.JseStringLib;
|
|
||||||
|
|
||||||
/** Simple program that illustrates basic sand-boxing of client scripts
|
/** Simple program that illustrates basic sand-boxing of client scripts
|
||||||
* in a server environment.
|
* in a server environment.
|
||||||
@@ -43,7 +29,7 @@ public class SampleSandboxed {
|
|||||||
server_globals = new Globals();
|
server_globals = new Globals();
|
||||||
server_globals.load(new JseBaseLib());
|
server_globals.load(new JseBaseLib());
|
||||||
server_globals.load(new PackageLib());
|
server_globals.load(new PackageLib());
|
||||||
server_globals.load(new JseStringLib());
|
server_globals.load(new StringLib());
|
||||||
|
|
||||||
// To load scripts, we occasionally need a math library in addition to compiler support.
|
// To load scripts, we occasionally need a math library in addition to compiler support.
|
||||||
// To limit scripts using the debug library, they must be closures, so we only install LuaC.
|
// To limit scripts using the debug library, they must be closures, so we only install LuaC.
|
||||||
@@ -96,7 +82,7 @@ public class SampleSandboxed {
|
|||||||
user_globals.load(new PackageLib());
|
user_globals.load(new PackageLib());
|
||||||
user_globals.load(new Bit32Lib());
|
user_globals.load(new Bit32Lib());
|
||||||
user_globals.load(new TableLib());
|
user_globals.load(new TableLib());
|
||||||
user_globals.load(new JseStringLib());
|
user_globals.load(new StringLib());
|
||||||
user_globals.load(new JseMathLib());
|
user_globals.load(new JseMathLib());
|
||||||
|
|
||||||
// This library is dangerous as it gives unfettered access to the
|
// This library is dangerous as it gives unfettered access to the
|
||||||
|
|||||||
@@ -177,14 +177,7 @@ public class LuaDouble extends LuaNumber {
|
|||||||
* @see #dmod_d(double, double)
|
* @see #dmod_d(double, double)
|
||||||
*/
|
*/
|
||||||
public static LuaValue dmod(double lhs, double rhs) {
|
public static LuaValue dmod(double lhs, double rhs) {
|
||||||
if (rhs == 0 || lhs == Double.POSITIVE_INFINITY || lhs == Double.NEGATIVE_INFINITY) return NAN;
|
return rhs!=0? valueOf( lhs-rhs*Math.floor(lhs/rhs) ): NAN;
|
||||||
if (rhs == Double.POSITIVE_INFINITY) {
|
|
||||||
return lhs < 0 ? POSINF : valueOf(lhs);
|
|
||||||
}
|
|
||||||
if (rhs == Double.NEGATIVE_INFINITY) {
|
|
||||||
return lhs > 0 ? NEGINF : valueOf(lhs);
|
|
||||||
}
|
|
||||||
return valueOf( lhs-rhs*Math.floor(lhs/rhs) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Take modulo for double numbers according to lua math, and return a double result.
|
/** Take modulo for double numbers according to lua math, and return a double result.
|
||||||
@@ -195,14 +188,7 @@ public class LuaDouble extends LuaNumber {
|
|||||||
* @see #dmod(double, double)
|
* @see #dmod(double, double)
|
||||||
*/
|
*/
|
||||||
public static double dmod_d(double lhs, double rhs) {
|
public static double dmod_d(double lhs, double rhs) {
|
||||||
if (rhs == 0 || lhs == Double.POSITIVE_INFINITY || lhs == Double.NEGATIVE_INFINITY) return Double.NaN;
|
return rhs!=0? lhs-rhs*Math.floor(lhs/rhs): Double.NaN;
|
||||||
if (rhs == Double.POSITIVE_INFINITY) {
|
|
||||||
return lhs < 0 ? Double.POSITIVE_INFINITY : lhs;
|
|
||||||
}
|
|
||||||
if (rhs == Double.NEGATIVE_INFINITY) {
|
|
||||||
return lhs > 0 ? Double.NEGATIVE_INFINITY : lhs;
|
|
||||||
}
|
|
||||||
return lhs-rhs*Math.floor(lhs/rhs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// relational operators
|
// relational operators
|
||||||
|
|||||||
@@ -391,23 +391,23 @@ public class LuaString extends LuaValue {
|
|||||||
public short toshort() { return (short) toint(); }
|
public short toshort() { return (short) toint(); }
|
||||||
|
|
||||||
public double optdouble(double defval) {
|
public double optdouble(double defval) {
|
||||||
return checkdouble();
|
return checknumber().checkdouble();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int optint(int defval) {
|
public int optint(int defval) {
|
||||||
return checkint();
|
return checknumber().checkint();
|
||||||
}
|
}
|
||||||
|
|
||||||
public LuaInteger optinteger(LuaInteger defval) {
|
public LuaInteger optinteger(LuaInteger defval) {
|
||||||
return checkinteger();
|
return checknumber().checkinteger();
|
||||||
}
|
}
|
||||||
|
|
||||||
public long optlong(long defval) {
|
public long optlong(long defval) {
|
||||||
return checklong();
|
return checknumber().checklong();
|
||||||
}
|
}
|
||||||
|
|
||||||
public LuaNumber optnumber(LuaNumber defval) {
|
public LuaNumber optnumber(LuaNumber defval) {
|
||||||
return checknumber();
|
return checknumber().checknumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
public LuaString optstring(LuaString defval) {
|
public LuaString optstring(LuaString defval) {
|
||||||
|
|||||||
@@ -643,7 +643,7 @@ public class LuaValue extends Varargs {
|
|||||||
* @see #isnumber()
|
* @see #isnumber()
|
||||||
* @see #TNUMBER
|
* @see #TNUMBER
|
||||||
*/
|
*/
|
||||||
public double optdouble(double defval) { argerror("number"); return 0; }
|
public double optdouble(double defval) { argerror("double"); return 0; }
|
||||||
|
|
||||||
/** Check that optional argument is a function and return as {@link LuaFunction}
|
/** Check that optional argument is a function and return as {@link LuaFunction}
|
||||||
* <p>
|
* <p>
|
||||||
@@ -855,7 +855,7 @@ public class LuaValue extends Varargs {
|
|||||||
* @see #optdouble(double)
|
* @see #optdouble(double)
|
||||||
* @see #TNUMBER
|
* @see #TNUMBER
|
||||||
*/
|
*/
|
||||||
public double checkdouble() { argerror("number"); return 0; }
|
public double checkdouble() { argerror("double"); return 0; }
|
||||||
|
|
||||||
/** Check that the value is a function , or throw {@link LuaError} if not
|
/** Check that the value is a function , or throw {@link LuaError} if not
|
||||||
* <p>
|
* <p>
|
||||||
|
|||||||
@@ -299,8 +299,7 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
|
|||||||
}
|
}
|
||||||
public LuaValue call(LuaValue table, LuaValue index, LuaValue value) {
|
public LuaValue call(LuaValue table, LuaValue index, LuaValue value) {
|
||||||
LuaTable t = table.checktable();
|
LuaTable t = table.checktable();
|
||||||
if (!index.isvalidkey()) argerror(2, "value");
|
t.rawset(index.checknotnil(), value);
|
||||||
t.rawset(index, value);
|
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ public class Bit32Lib extends TwoArgFunction {
|
|||||||
"arshift", "lrotate", "lshift", "rrotate", "rshift"
|
"arshift", "lrotate", "lshift", "rrotate", "rshift"
|
||||||
});
|
});
|
||||||
env.set("bit32", t);
|
env.set("bit32", t);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("bit32", t);
|
env.get("package").get("loaded").set("bit32", t);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ public class CoroutineLib extends TwoArgFunction {
|
|||||||
coroutine.set("yield", new yield());
|
coroutine.set("yield", new yield());
|
||||||
coroutine.set("wrap", new wrap());
|
coroutine.set("wrap", new wrap());
|
||||||
env.set("coroutine", coroutine);
|
env.set("coroutine", coroutine);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("coroutine", coroutine);
|
env.get("package").get("loaded").set("coroutine", coroutine);
|
||||||
return coroutine;
|
return coroutine;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
debug.set("upvalueid", new upvalueid());
|
debug.set("upvalueid", new upvalueid());
|
||||||
debug.set("upvaluejoin", new upvaluejoin());
|
debug.set("upvaluejoin", new upvaluejoin());
|
||||||
env.set("debug", debug);
|
env.set("debug", debug);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("debug", debug);
|
env.get("package").get("loaded").set("debug", debug);
|
||||||
return debug;
|
return debug;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -256,7 +256,7 @@ public class IoLib extends TwoArgFunction {
|
|||||||
|
|
||||||
// return the table
|
// return the table
|
||||||
env.set("io", t);
|
env.set("io", t);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("io", t);
|
env.get("package").get("loaded").set("io", t);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ public class MathLib extends TwoArgFunction {
|
|||||||
math.set("sqrt", new sqrt());
|
math.set("sqrt", new sqrt());
|
||||||
math.set("tan", new tan());
|
math.set("tan", new tan());
|
||||||
env.set("math", math);
|
env.set("math", math);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("math", math);
|
env.get("package").get("loaded").set("math", math);
|
||||||
return math;
|
return math;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,12 +163,10 @@ public class MathLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static final class fmod extends TwoArgFunction {
|
static final class fmod extends BinaryOp {
|
||||||
public LuaValue call(LuaValue xv, LuaValue yv) {
|
protected double call(double x, double y) {
|
||||||
if (xv.islong() && yv.islong()) {
|
double q = x/y;
|
||||||
return valueOf(xv.tolong() % yv.tolong());
|
return x - y * (q>=0? Math.floor(q): Math.ceil(q));
|
||||||
}
|
|
||||||
return valueOf(xv.checkdouble() % yv.checkdouble());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static final class ldexp extends BinaryOp {
|
static final class ldexp extends BinaryOp {
|
||||||
@@ -196,36 +194,27 @@ public class MathLib extends TwoArgFunction {
|
|||||||
|
|
||||||
static class max extends VarArgFunction {
|
static class max extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaValue m = args.checkvalue(1);
|
double m = args.checkdouble(1);
|
||||||
for ( int i=2,n=args.narg(); i<=n; ++i ) {
|
for ( int i=2,n=args.narg(); i<=n; ++i )
|
||||||
LuaValue v = args.checkvalue(i);
|
m = Math.max(m,args.checkdouble(i));
|
||||||
if (m.lt_b(v)) m = v;
|
return valueOf(m);
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class min extends VarArgFunction {
|
static class min extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaValue m = args.checkvalue(1);
|
double m = args.checkdouble(1);
|
||||||
for ( int i=2,n=args.narg(); i<=n; ++i ) {
|
for ( int i=2,n=args.narg(); i<=n; ++i )
|
||||||
LuaValue v = args.checkvalue(i);
|
m = Math.min(m,args.checkdouble(i));
|
||||||
if (v.lt_b(m)) m = v;
|
return valueOf(m);
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class modf extends VarArgFunction {
|
static class modf extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaValue n = args.arg1();
|
double x = args.checkdouble(1);
|
||||||
/* number is its own integer part, no fractional part */
|
|
||||||
if (n.islong()) return varargsOf(n, valueOf(0.0));
|
|
||||||
double x = n.checkdouble();
|
|
||||||
/* integer part (rounds toward zero) */
|
|
||||||
double intPart = ( x > 0 ) ? Math.floor( x ) : Math.ceil( x );
|
double intPart = ( x > 0 ) ? Math.floor( x ) : Math.ceil( x );
|
||||||
/* fractional part (test needed for inf/-inf) */
|
double fracPart = x - intPart;
|
||||||
double fracPart = x == intPart ? 0.0 : x - intPart;
|
|
||||||
return varargsOf( valueOf(intPart), valueOf(fracPart) );
|
return varargsOf( valueOf(intPart), valueOf(fracPart) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ public class OsLib extends TwoArgFunction {
|
|||||||
for (int i = 0; i < NAMES.length; ++i)
|
for (int i = 0; i < NAMES.length; ++i)
|
||||||
os.set(NAMES[i], new OsLibFunc(i, NAMES[i]));
|
os.set(NAMES[i], new OsLibFunc(i, NAMES[i]));
|
||||||
env.set("os", os);
|
env.set("os", os);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("os", os);
|
env.get("package").get("loaded").set("os", os);
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ package org.luaj.vm2.lib;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.luaj.vm2.Buffer;
|
|
||||||
import org.luaj.vm2.LuaClosure;
|
import org.luaj.vm2.LuaClosure;
|
||||||
|
import org.luaj.vm2.Buffer;
|
||||||
import org.luaj.vm2.LuaString;
|
import org.luaj.vm2.LuaString;
|
||||||
import org.luaj.vm2.LuaTable;
|
import org.luaj.vm2.LuaTable;
|
||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
@@ -49,7 +49,7 @@ import org.luaj.vm2.compiler.DumpState;
|
|||||||
* Globals globals = new Globals();
|
* Globals globals = new Globals();
|
||||||
* globals.load(new JseBaseLib());
|
* globals.load(new JseBaseLib());
|
||||||
* globals.load(new PackageLib());
|
* globals.load(new PackageLib());
|
||||||
* globals.load(new JseStringLib());
|
* globals.load(new StringLib());
|
||||||
* System.out.println( globals.get("string").get("upper").call( LuaValue.valueOf("abcde") ) );
|
* System.out.println( globals.get("string").get("upper").call( LuaValue.valueOf("abcde") ) );
|
||||||
* } </pre>
|
* } </pre>
|
||||||
* <p>
|
* <p>
|
||||||
@@ -99,7 +99,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
string.set("upper", new upper());
|
string.set("upper", new upper());
|
||||||
|
|
||||||
env.set("string", string);
|
env.set("string", string);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("string", string);
|
env.get("package").get("loaded").set("string", string);
|
||||||
if (LuaString.s_metatable == null) {
|
if (LuaString.s_metatable == null) {
|
||||||
LuaString.s_metatable = LuaValue.tableOf(new LuaValue[] { INDEX, string });
|
LuaString.s_metatable = LuaValue.tableOf(new LuaValue[] { INDEX, string });
|
||||||
}
|
}
|
||||||
@@ -173,7 +173,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
* TODO: port dumping code as optional add-on
|
* TODO: port dumping code as optional add-on
|
||||||
*/
|
*/
|
||||||
static final class dump extends VarArgFunction {
|
static final class dump extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public LuaValue invoke(Varargs args) {
|
||||||
LuaValue f = args.checkfunction(1);
|
LuaValue f = args.checkfunction(1);
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
try {
|
try {
|
||||||
@@ -230,7 +230,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
* This function does not accept string values containing embedded zeros,
|
* This function does not accept string values containing embedded zeros,
|
||||||
* except as arguments to the q option.
|
* except as arguments to the q option.
|
||||||
*/
|
*/
|
||||||
final class format extends VarArgFunction {
|
static final class format extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaString fmt = args.checkstring( 1 );
|
LuaString fmt = args.checkstring( 1 );
|
||||||
final int n = fmt.length();
|
final int n = fmt.length();
|
||||||
@@ -261,7 +261,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
case 'd':
|
case 'd':
|
||||||
fdsc.format( result, args.checklong( arg ) );
|
fdsc.format( result, args.checkint( arg ) );
|
||||||
break;
|
break;
|
||||||
case 'o':
|
case 'o':
|
||||||
case 'u':
|
case 'u':
|
||||||
@@ -330,7 +330,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
|
|
||||||
private static final String FLAGS = "-+ #0";
|
private static final String FLAGS = "-+ #0";
|
||||||
|
|
||||||
class FormatDesc {
|
static class FormatDesc {
|
||||||
|
|
||||||
private boolean leftAdjust;
|
private boolean leftAdjust;
|
||||||
private boolean zeroPad;
|
private boolean zeroPad;
|
||||||
@@ -470,7 +470,13 @@ public class StringLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void format(Buffer buf, double x) {
|
public void format(Buffer buf, double x) {
|
||||||
buf.append( StringLib.this.format(src, x) );
|
String out;
|
||||||
|
try {
|
||||||
|
out = String.format(src, x);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
out = String.valueOf( x );
|
||||||
|
}
|
||||||
|
buf.append( out );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void format(Buffer buf, LuaString s) {
|
public void format(Buffer buf, LuaString s) {
|
||||||
@@ -480,17 +486,13 @@ public class StringLib extends TwoArgFunction {
|
|||||||
buf.append(s);
|
buf.append(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void pad(Buffer buf, char c, int n) {
|
public static final void pad(Buffer buf, char c, int n) {
|
||||||
byte b = (byte)c;
|
byte b = (byte)c;
|
||||||
while ( n-- > 0 )
|
while ( n-- > 0 )
|
||||||
buf.append(b);
|
buf.append(b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String format(String src, double x) {
|
|
||||||
return String.valueOf(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string.gmatch (s, pattern)
|
* string.gmatch (s, pattern)
|
||||||
*
|
*
|
||||||
@@ -875,14 +877,8 @@ public class StringLib extends TwoArgFunction {
|
|||||||
lbuf.append( (byte) b );
|
lbuf.append( (byte) b );
|
||||||
} else {
|
} else {
|
||||||
++i; // skip ESC
|
++i; // skip ESC
|
||||||
b = (byte)(i < l ? news.luaByte( i ) : 0);
|
b = (byte) news.luaByte( i );
|
||||||
if ( !Character.isDigit( (char) b ) ) {
|
if ( !Character.isDigit( (char) b ) ) {
|
||||||
if (b != L_ESC) error( "invalid use of '" + (char)L_ESC +
|
|
||||||
"' in replacement string: after '" + (char)L_ESC +
|
|
||||||
"' must be '0'-'9' or '" + (char)L_ESC +
|
|
||||||
"', but found " + (i < l ? "symbol '" + (char)b + "' with code " + b +
|
|
||||||
" at pos " + (i + 1) :
|
|
||||||
"end of string"));
|
|
||||||
lbuf.append( b );
|
lbuf.append( b );
|
||||||
} else if ( b == '0' ) {
|
} else if ( b == '0' ) {
|
||||||
lbuf.append( s.substring( soff, e ) );
|
lbuf.append( s.substring( soff, e ) );
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ public class TableLib extends TwoArgFunction {
|
|||||||
table.set("sort", new sort());
|
table.set("sort", new sort());
|
||||||
table.set("unpack", new unpack());
|
table.set("unpack", new unpack());
|
||||||
env.set("table", table);
|
env.set("table", table);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("table", table);
|
env.get("package").get("loaded").set("table", table);
|
||||||
return NIL;
|
return NIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -189,8 +189,10 @@ public class luajc {
|
|||||||
|
|
||||||
public Class findClass(String classname) throws ClassNotFoundException {
|
public Class findClass(String classname) throws ClassNotFoundException {
|
||||||
byte[] bytes = (byte[]) t.get(classname);
|
byte[] bytes = (byte[]) t.get(classname);
|
||||||
if ( bytes != null )
|
if ( bytes != null ) {
|
||||||
|
classname = classname.replace('/', '.');
|
||||||
return defineClass(classname, bytes, 0, bytes.length);
|
return defineClass(classname, bytes, 0, bytes.length);
|
||||||
|
}
|
||||||
return super.findClass(classname);
|
return super.findClass(classname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,14 +98,7 @@ public class JseMathLib extends org.luaj.vm2.lib.MathLib {
|
|||||||
}
|
}
|
||||||
static final class cosh extends UnaryOp { protected double call(double d) { return Math.cosh(d); } }
|
static final class cosh extends UnaryOp { protected double call(double d) { return Math.cosh(d); } }
|
||||||
static final class exp extends UnaryOp { protected double call(double d) { return Math.exp(d); } }
|
static final class exp extends UnaryOp { protected double call(double d) { return Math.exp(d); } }
|
||||||
static final class log extends TwoArgFunction {
|
static final class log extends UnaryOp { protected double call(double d) { return Math.log(d); } }
|
||||||
public LuaValue call(LuaValue x, LuaValue base) {
|
|
||||||
double nat = Math.log(x.checkdouble());
|
|
||||||
double b = base.optdouble(Math.E);
|
|
||||||
if (b != Math.E) nat /= Math.log(b);
|
|
||||||
return valueOf(nat);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
static final class pow extends BinaryOp { protected double call(double x, double y) { return Math.pow(x, y); } }
|
static final class pow extends BinaryOp { protected double call(double x, double y) { return Math.pow(x, y); } }
|
||||||
static final class sinh extends UnaryOp { protected double call(double d) { return Math.sinh(d); } }
|
static final class sinh extends UnaryOp { protected double call(double d) { return Math.sinh(d); } }
|
||||||
static final class tanh extends UnaryOp { protected double call(double d) { return Math.tanh(d); } }
|
static final class tanh extends UnaryOp { protected double call(double d) { return Math.tanh(d); } }
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ package org.luaj.vm2.lib.jse;
|
|||||||
|
|
||||||
import org.luaj.vm2.Globals;
|
import org.luaj.vm2.Globals;
|
||||||
import org.luaj.vm2.LoadState;
|
import org.luaj.vm2.LoadState;
|
||||||
|
import org.luaj.vm2.LuaThread;
|
||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
import org.luaj.vm2.Varargs;
|
import org.luaj.vm2.Varargs;
|
||||||
import org.luaj.vm2.compiler.LuaC;
|
import org.luaj.vm2.compiler.LuaC;
|
||||||
@@ -97,7 +98,7 @@ public class JsePlatform {
|
|||||||
globals.load(new PackageLib());
|
globals.load(new PackageLib());
|
||||||
globals.load(new Bit32Lib());
|
globals.load(new Bit32Lib());
|
||||||
globals.load(new TableLib());
|
globals.load(new TableLib());
|
||||||
globals.load(new JseStringLib());
|
globals.load(new StringLib());
|
||||||
globals.load(new CoroutineLib());
|
globals.load(new CoroutineLib());
|
||||||
globals.load(new JseMathLib());
|
globals.load(new JseMathLib());
|
||||||
globals.load(new JseIoLib());
|
globals.load(new JseIoLib());
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ public class LuajavaLib extends VarArgFunction {
|
|||||||
LuaTable t = new LuaTable();
|
LuaTable t = new LuaTable();
|
||||||
bind( t, this.getClass(), NAMES, BINDCLASS );
|
bind( t, this.getClass(), NAMES, BINDCLASS );
|
||||||
env.set("luajava", t);
|
env.set("luajava", t);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("luajava", t);
|
env.get("package").get("loaded").set("luajava", t);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
case BINDCLASS: {
|
case BINDCLASS: {
|
||||||
|
|||||||
@@ -114,7 +114,24 @@ public class LuaJC implements Globals.Loader {
|
|||||||
StringBuffer classname = new StringBuffer();
|
StringBuffer classname = new StringBuffer();
|
||||||
for (int i = 0, n = stub.length(); i < n; ++i) {
|
for (int i = 0, n = stub.length(); i < n; ++i) {
|
||||||
final char c = stub.charAt(i);
|
final char c = stub.charAt(i);
|
||||||
classname.append((((i == 0) && Character.isJavaIdentifierStart(c)) || ((i > 0) && Character.isJavaIdentifierPart(c)))? c: '_');
|
switch(i) {
|
||||||
|
case 0:
|
||||||
|
if(Character.isJavaIdentifierStart(c)) {
|
||||||
|
classname.append(c);
|
||||||
|
} else {
|
||||||
|
classname.append('_');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if(c == '/') {
|
||||||
|
classname.append(c);
|
||||||
|
} else if(Character.isJavaIdentifierPart(c)) {
|
||||||
|
classname.append(c);
|
||||||
|
} else {
|
||||||
|
classname.append('_');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return classname.toString();
|
return classname.toString();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user