diff --git a/cldc11/src/se/krka/kahlua/cldc11/MathLib.java b/cldc11/src/se/krka/kahlua/cldc11/MathLib.java
index 94c3140..9751887 100644
--- a/cldc11/src/se/krka/kahlua/cldc11/MathLib.java
+++ b/cldc11/src/se/krka/kahlua/cldc11/MathLib.java
@@ -393,11 +393,7 @@ private static int ldexp(LuaCallFrame callFrame, int nArguments) {
return 1;
}
-
-
-
-
- public static final double EPS = 1e-15;
+ private static final double EPS = 1e-15;
/*
* Simple implementation of the taylor expansion of
@@ -502,19 +498,19 @@ public static double ipow(double base, int exponent) {
// constants
- static final double sq2p1 = 2.414213562373095048802e0;
- static final double sq2m1 = .414213562373095048802e0;
- static final double p4 = .161536412982230228262e2;
- static final double p3 = .26842548195503973794141e3;
- static final double p2 = .11530293515404850115428136e4;
- static final double p1 = .178040631643319697105464587e4;
- static final double p0 = .89678597403663861959987488e3;
- static final double q4 = .5895697050844462222791e2;
- static final double q3 = .536265374031215315104235e3;
- static final double q2 = .16667838148816337184521798e4;
- static final double q1 = .207933497444540981287275926e4;
- static final double q0 = .89678597403663861962481162e3;
- static final double PIO2 = 1.5707963267948966135E0;
+ private static final double sq2p1 = 2.414213562373095048802e0;
+ private static final double sq2m1 = .414213562373095048802e0;
+ private static final double p4 = .161536412982230228262e2;
+ private static final double p3 = .26842548195503973794141e3;
+ private static final double p2 = .11530293515404850115428136e4;
+ private static final double p1 = .178040631643319697105464587e4;
+ private static final double p0 = .89678597403663861959987488e3;
+ private static final double q4 = .5895697050844462222791e2;
+ private static final double q3 = .536265374031215315104235e3;
+ private static final double q2 = .16667838148816337184521798e4;
+ private static final double q1 = .207933497444540981287275926e4;
+ private static final double q0 = .89678597403663861962481162e3;
+ private static final double PIO2 = 1.5707963267948966135E0;
// reduce
private static double mxatan(double arg)
diff --git a/core/src/se/krka/kahlua/stdlib/BaseLib.java b/core/src/se/krka/kahlua/stdlib/BaseLib.java
index 482ace0..466b62d 100644
--- a/core/src/se/krka/kahlua/stdlib/BaseLib.java
+++ b/core/src/se/krka/kahlua/stdlib/BaseLib.java
@@ -179,7 +179,7 @@ private int rawequal(LuaCallFrame callFrame, int nArguments) {
Object o1 = callFrame.get(0);
Object o2 = callFrame.get(1);
- callFrame.push(KahluaUtil.toBoolean(KahluaUtil.luaEquals(o1, o2)));
+ callFrame.push(KahluaUtil.toBoolean(luaEquals(o1, o2)));
return 1;
}
@@ -460,5 +460,16 @@ private static int bytecodeloader(LuaCallFrame callFrame, int nArguments) {
return callFrame.push("Could not find the bytecode for '" + modname + "' in classpath");
}
-
+ /** @exclude */
+ public static boolean luaEquals(Object a, Object b) {
+ if (a == null || b == null) {
+ return a == b;
+ }
+ if (a instanceof Double && b instanceof Double) {
+ Double ad = (Double) a;
+ Double bd = (Double) b;
+ return ad.doubleValue() == bd.doubleValue();
+ }
+ return a == b;
+ }
}
diff --git a/core/src/se/krka/kahlua/stdlib/CoroutineLib.java b/core/src/se/krka/kahlua/stdlib/CoroutineLib.java
index 3997309..b11db34 100644
--- a/core/src/se/krka/kahlua/stdlib/CoroutineLib.java
+++ b/core/src/se/krka/kahlua/stdlib/CoroutineLib.java
@@ -167,7 +167,7 @@ private int resume(LuaCallFrame callFrame, int nArguments) {
return 0;
}
- public static int yield(LuaCallFrame callFrame, int nArguments) {
+ private static int yield(LuaCallFrame callFrame, int nArguments) {
Coroutine t = callFrame.coroutine;
Coroutine parent = t.parent;
diff --git a/core/src/se/krka/kahlua/stdlib/RandomLib.java b/core/src/se/krka/kahlua/stdlib/RandomLib.java
index 8024df6..9cd0daf 100644
--- a/core/src/se/krka/kahlua/stdlib/RandomLib.java
+++ b/core/src/se/krka/kahlua/stdlib/RandomLib.java
@@ -54,7 +54,7 @@ public int call(LuaCallFrame callFrame, int nArguments) {
}
private int randomSeed(LuaCallFrame callFrame, int nArguments) {
- Random random = getRandom(callFrame, nArguments);
+ Random random = getRandom(callFrame, "seed");
Object o = callFrame.get(1);
int hashCode = o == null ? 0 : o.hashCode();
random.setSeed(hashCode);
@@ -62,41 +62,33 @@ private int randomSeed(LuaCallFrame callFrame, int nArguments) {
}
private int random(LuaCallFrame callFrame, int nArguments) {
- Random random = getRandom(callFrame, nArguments);
-
- if (nArguments <= 1) {
- return callFrame.push(KahluaUtil.toDouble(random.nextDouble()));
- }
-
- double tmp = KahluaUtil.getDoubleArg(callFrame, 2, names[RANDOM]);
- int m = (int) tmp;
- int n;
- if (nArguments == 2) {
- n = m;
- m = 1;
- } else {
- tmp = KahluaUtil.getDoubleArg(callFrame, 3, names[RANDOM]);
- n = (int) tmp;
- }
+ Random random = getRandom(callFrame, "random");
+
+ Double min = KahluaUtil.getOptionalNumberArg(callFrame, 2);
+ Double max = KahluaUtil.getOptionalNumberArg(callFrame, 3);
+ if (min == null) {
+ return callFrame.push(KahluaUtil.toDouble(random.nextDouble()));
+ }
+ int m = min.intValue();
+ int n;
+ if (max == null) {
+ n = m;
+ m = 1;
+ } else {
+ n = max.intValue();
+ }
return callFrame.push(KahluaUtil.toDouble(m + random.nextInt(n - m + 1)));
}
- private Random getRandom(LuaCallFrame callFrame, int nArguments) {
- Random random = null;
- if (nArguments > 0) {
- Object o = callFrame.get(0);
- if (o instanceof Random) {
- random = (Random) o;
- }
- }
- if (random == null) {
- KahluaUtil.fail("First argument must be an object of type random.");
- }
- return random;
+ private Random getRandom(LuaCallFrame callFrame, String name) {
+ Object obj = KahluaUtil.getArg(callFrame, 1, name);
+ if (!(obj instanceof Random)) {
+ KahluaUtil.fail("First argument to " + name + " must be an object of type random.");
+ }
+ return (Random) obj;
}
private int newRandom(LuaCallFrame callFrame) {
- Random random = new Random();
- return callFrame.push(random);
+ return callFrame.push(new Random());
}
}
diff --git a/core/src/se/krka/kahlua/stdlib/StringLib.java b/core/src/se/krka/kahlua/stdlib/StringLib.java
index 6ff657a..5b0383b 100644
--- a/core/src/se/krka/kahlua/stdlib/StringLib.java
+++ b/core/src/se/krka/kahlua/stdlib/StringLib.java
@@ -52,10 +52,10 @@ public final class StringLib implements JavaFunction {
private static final int CAP_POSITION = ( -2 );
private static final String[] names;
- private static final StringLib[] functions;
+ private static final StringLib[] functions;
// NOTE: String.class won't work in J2ME - so this is used as a workaround
- public static final Class STRING_CLASS = "".getClass();
+ private static final Class STRING_CLASS = "".getClass();
static {
names = new String[NUM_FUNCTIONS];
@@ -76,6 +76,7 @@ public final class StringLib implements JavaFunction {
}
}
+ /** @exclude */
private final int methodId;
public StringLib(int index) {
this.methodId = index;
@@ -808,6 +809,7 @@ public static double roundToSignificantNumbers(double x, int precision) {
* Original code that this was adapted from is copyright (c) 2008 groundspeak, inc.
*/
+ /** @exclude */
public static class MatchState {
public final LuaCallFrame callFrame;
public final StringPointer src_init; /* init of source string */
@@ -827,6 +829,7 @@ public MatchState(LuaCallFrame callFrame, StringPointer srcInit, int endIndex) {
public int level; /* total number of captures (finished or unfinished) */
+ /** @exclude */
public static class Capture {
public StringPointer init;
public int len;
@@ -844,6 +847,7 @@ public Object getCapture(int i) {
}
}
+ /** @exclude */
public static class StringPointer {
private final String string;
private int index = 0;
@@ -1413,11 +1417,12 @@ private static int gsub(LuaCallFrame cf, int nargs) {
pattern.postIncrString ( 1 );
}
- String replType = KahluaUtil.type(repl);
- if (!(replType == KahluaUtil.TYPE_FUNCTION ||
- replType == KahluaUtil.TYPE_STRING ||
- replType == KahluaUtil.TYPE_TABLE)) {
- KahluaUtil.fail(("string/function/table expected, got "+replType));
+ if (!(repl instanceof Double ||
+ repl instanceof String ||
+ repl instanceof LuaClosure ||
+ repl instanceof JavaFunction ||
+ repl instanceof KahluaTable )) {
+ KahluaUtil.fail(("string/function/table expected, got " + repl));
}
MatchState ms = new MatchState(cf, src.getClone(), src.length());
@@ -1448,9 +1453,9 @@ private static int gsub(LuaCallFrame cf, int nargs) {
}
private static void addValue(MatchState ms, Object repl, StringBuffer b, StringPointer src, StringPointer e) {
- String type = KahluaUtil.type(repl);
- if (type == KahluaUtil.TYPE_NUMBER || type == KahluaUtil.TYPE_STRING) {
- b.append(addString(ms, repl, src, e));
+ String replString = KahluaUtil.rawTostring(repl);
+ if (replString != null) {
+ b.append(addString(ms, replString, src, e));
} else {
Object captures = ms.getCapture(0);
String match;
@@ -1460,10 +1465,10 @@ private static void addValue(MatchState ms, Object repl, StringBuffer b, StringP
match = src.getStringSubString(e.getIndex() - src.getIndex());
}
Object res = null;
- if (type == KahluaUtil.TYPE_FUNCTION) {
- res = ms.callFrame.coroutine.thread.call(repl, match, null, null);
- } else if (type == KahluaUtil.TYPE_TABLE) {
+ if (repl instanceof KahluaTable) {
res = ((KahluaTable)repl).rawget(match);
+ } else {
+ res = ms.callFrame.coroutine.thread.call(repl, match, null, null);
}
if (res == null) {
res = match;
@@ -1472,11 +1477,10 @@ private static void addValue(MatchState ms, Object repl, StringBuffer b, StringP
}
}
- private static String addString(MatchState ms, Object repl, StringPointer s, StringPointer e) {
- String replTemp = KahluaUtil.tostring(repl, ms.callFrame.coroutine.thread);
- StringPointer replStr = new StringPointer(replTemp);
+ private static String addString(MatchState ms, String repl, StringPointer s, StringPointer e) {
+ StringPointer replStr = new StringPointer(repl);
StringBuffer buf = new StringBuffer();
- for (int i = 0; i < replTemp.length(); i++) {
+ for (int i = 0; i < repl.length(); i++) {
char c = replStr.getChar(i);
if (c != L_ESC) {
buf.append(c);
diff --git a/core/src/se/krka/kahlua/vm/JavaFunction.java b/core/src/se/krka/kahlua/vm/JavaFunction.java
index 8e552c3..6a4f1ce 100644
--- a/core/src/se/krka/kahlua/vm/JavaFunction.java
+++ b/core/src/se/krka/kahlua/vm/JavaFunction.java
@@ -25,16 +25,21 @@ of this software and associated documentation files (the "Software"), to deal
public interface JavaFunction {
/**
- * General contract.
- *
- * Input:
- * callFrame = the frame that contains all the arguments, and where all the results should be put.
- * nArguments = number of function arguments
- * callFrame.get(i) = an argument (0 <= i < nArgs)
- *
- * @param callFrame - the current callframe for the function
+ * This interface defines functions which the Kahlua engine can call.
+ *
+ * General contract:
+ *
+ * callFrame.get(i) = an argument (0 <= i < nArguments)+ * Return (possibly) values to lua by calling: + *
+ *
+ * callFrame.push(value1); + * callFrame.push(value2); + * return 2; // number of pushed values + *+ * @param callFrame - the frame that contains all the arguments and where all the results should be put. * @param nArguments - number of function arguments - * @return N - number of return values. The top N objects on the stack are considered the return values + * @return N, number of return values. The top N objects on the stack are considered the return values. */ public abstract int call(LuaCallFrame callFrame, int nArguments); } diff --git a/core/src/se/krka/kahlua/vm/KahluaThread.java b/core/src/se/krka/kahlua/vm/KahluaThread.java index 5e4065a..bb9abe4 100644 --- a/core/src/se/krka/kahlua/vm/KahluaThread.java +++ b/core/src/se/krka/kahlua/vm/KahluaThread.java @@ -21,91 +21,52 @@ of this software and associated documentation files (the "Software"), to deal */ package se.krka.kahlua.vm; +import se.krka.kahlua.stdlib.BaseLib; + import java.io.PrintStream; public class KahluaThread { - /** @exclude */ - public static final int FIELDS_PER_FLUSH = 50; - /** @exclude */ - public static final int OP_MOVE = 0; - /** @exclude */ - public static final int OP_LOADK = 1; - /** @exclude */ - public static final int OP_LOADBOOL = 2; - /** @exclude */ - public static final int OP_LOADNIL = 3; - /** @exclude */ - public static final int OP_GETUPVAL = 4; - /** @exclude */ - public static final int OP_GETGLOBAL = 5; - /** @exclude */ - public static final int OP_GETTABLE = 6; - /** @exclude */ - public static final int OP_SETGLOBAL = 7; - /** @exclude */ - public static final int OP_SETUPVAL = 8; - /** @exclude */ - public static final int OP_SETTABLE = 9; - /** @exclude */ - public static final int OP_NEWTABLE = 10; - /** @exclude */ - public static final int OP_SELF = 11; - /** @exclude */ - public static final int OP_ADD = 12; - /** @exclude */ - public static final int OP_SUB = 13; - /** @exclude */ - public static final int OP_MUL = 14; - /** @exclude */ - public static final int OP_DIV = 15; - /** @exclude */ - public static final int OP_MOD = 16; - /** @exclude */ - public static final int OP_POW = 17; - /** @exclude */ - public static final int OP_UNM = 18; - /** @exclude */ - public static final int OP_NOT = 19; - /** @exclude */ - public static final int OP_LEN = 20; - /** @exclude */ - public static final int OP_CONCAT = 21; - /** @exclude */ - public static final int OP_JMP = 22; - /** @exclude */ - public static final int OP_EQ = 23; - /** @exclude */ - public static final int OP_LT = 24; - /** @exclude */ - public static final int OP_LE = 25; - /** @exclude */ - public static final int OP_TEST = 26; - /** @exclude */ - public static final int OP_TESTSET = 27; - /** @exclude */ - public static final int OP_CALL = 28; - /** @exclude */ - public static final int OP_TAILCALL = 29; - /** @exclude */ - public static final int OP_RETURN = 30; - /** @exclude */ - public static final int OP_FORLOOP = 31; - /** @exclude */ - public static final int OP_FORPREP = 32; - /** @exclude */ - public static final int OP_TFORLOOP = 33; - /** @exclude */ - public static final int OP_SETLIST = 34; - /** @exclude */ - public static final int OP_CLOSE = 35; - /** @exclude */ - public static final int OP_CLOSURE = 36; - /** @exclude */ - public static final int OP_VARARG = 37; - /** @exclude */ + private static final int FIELDS_PER_FLUSH = 50; + private static final int OP_MOVE = 0; + private static final int OP_LOADK = 1; + private static final int OP_LOADBOOL = 2; + private static final int OP_LOADNIL = 3; + private static final int OP_GETUPVAL = 4; + private static final int OP_GETGLOBAL = 5; + private static final int OP_GETTABLE = 6; + private static final int OP_SETGLOBAL = 7; + private static final int OP_SETUPVAL = 8; + private static final int OP_SETTABLE = 9; + private static final int OP_NEWTABLE = 10; + private static final int OP_SELF = 11; + private static final int OP_ADD = 12; + private static final int OP_SUB = 13; + private static final int OP_MUL = 14; + private static final int OP_DIV = 15; + private static final int OP_MOD = 16; + private static final int OP_POW = 17; + private static final int OP_UNM = 18; + private static final int OP_NOT = 19; + private static final int OP_LEN = 20; + private static final int OP_CONCAT = 21; + private static final int OP_JMP = 22; + private static final int OP_EQ = 23; + private static final int OP_LT = 24; + private static final int OP_LE = 25; + private static final int OP_TEST = 26; + private static final int OP_TESTSET = 27; + private static final int OP_CALL = 28; + private static final int OP_TAILCALL = 29; + private static final int OP_RETURN = 30; + private static final int OP_FORLOOP = 31; + private static final int OP_FORPREP = 32; + private static final int OP_TFORLOOP = 33; + private static final int OP_SETLIST = 34; + private static final int OP_CLOSE = 35; + private static final int OP_CLOSURE = 36; + private static final int OP_VARARG = 37; private static final int MAX_INDEX_RECURSION = 100; - /** @exclude */ private static final String meta_ops[]; static { meta_ops = new String[38]; @@ -123,9 +84,7 @@ public class KahluaThread { /** @exclude */ public Coroutine currentCoroutine; - /** @exclude */ private final PrintStream out; - /** @exclude */ private final Platform platform; public KahluaThread(Platform platform, KahluaTable environment) { @@ -555,7 +514,7 @@ private final void luaMainloop() { } if (metafun == null && opcode == OP_EQ) { - resBool = KahluaUtil.luaEquals(bo, co); + resBool = BaseLib.luaEquals(bo, co); } else { if (metafun == null) { KahluaUtil.fail((meta_op + " not defined for operand")); diff --git a/core/src/se/krka/kahlua/vm/KahluaUtil.java b/core/src/se/krka/kahlua/vm/KahluaUtil.java index 5ceba1d..4134199 100644 --- a/core/src/se/krka/kahlua/vm/KahluaUtil.java +++ b/core/src/se/krka/kahlua/vm/KahluaUtil.java @@ -4,27 +4,24 @@ import java.io.InputStream; public class KahluaUtil { - public static final Object WORKER_THREAD_KEY = new Object(); - public static final String TYPE_NIL = "nil"; - public static final String TYPE_STRING = "string"; - public static final String TYPE_NUMBER = "number"; - public static final String TYPE_BOOLEAN = "boolean"; - public static final String TYPE_FUNCTION = "function"; - public static final String TYPE_TABLE = "table"; - public static final String TYPE_COROUTINE = "coroutine"; - public static final String TYPE_USERDATA = "userdata"; - - public static boolean luaEquals(Object a, Object b) { - if (a == null || b == null) { - return a == b; - } - if (a instanceof Double && b instanceof Double) { - Double ad = (Double) a; - Double bd = (Double) b; - return ad.doubleValue() == bd.doubleValue(); - } - return a == b; - } + /** @exclude */ + private static final Object WORKER_THREAD_KEY = new Object(); + /** @exclude */ + private static final String TYPE_NIL = "nil"; + /** @exclude */ + private static final String TYPE_STRING = "string"; + /** @exclude */ + private static final String TYPE_NUMBER = "number"; + /** @exclude */ + private static final String TYPE_BOOLEAN = "boolean"; + /** @exclude */ + private static final String TYPE_FUNCTION = "function"; + /** @exclude */ + private static final String TYPE_TABLE = "table"; + /** @exclude */ + private static final String TYPE_COROUTINE = "coroutine"; + /** @exclude */ + private static final String TYPE_USERDATA = "userdata"; public static double fromDouble(Object o) { return ((Double) o).doubleValue(); @@ -122,6 +119,9 @@ public static KahluaThread getWorkerThread(Platform platform, KahluaTable env) { return (KahluaThread) workerThread; } + public static void setWorkerThread(KahluaTable env, KahluaThread thread) { + env.rawset(WORKER_THREAD_KEY, thread); + } public static KahluaTable getOrCreateTable(Platform platform, KahluaTable env, String name) { @@ -317,7 +317,7 @@ public static Object getArg(LuaCallFrame callFrame, int n, String function) { if (res == null) { throw new RuntimeException("missing argument #" + n + "to '" + function + "'"); } - return callFrame.get(n - 1); + return res; } public static int len(KahluaTable kahluaTable, int low, int high) { diff --git a/j2se/src/se/krka/kahlua/integration/LuaFail.java b/j2se/src/se/krka/kahlua/integration/LuaFail.java index a156749..0a0eb07 100644 --- a/j2se/src/se/krka/kahlua/integration/LuaFail.java +++ b/j2se/src/se/krka/kahlua/integration/LuaFail.java @@ -25,6 +25,7 @@ of this software and associated documentation files (the "Software"), to deal import se.krka.kahlua.vm.KahluaUtil; +/** @exclude */ public class LuaFail extends LuaReturn { LuaFail(Object[] returnValues) { super(returnValues); diff --git a/j2se/src/se/krka/kahlua/integration/LuaReturn.java b/j2se/src/se/krka/kahlua/integration/LuaReturn.java index 312b9dc..d1acbad 100644 --- a/j2se/src/se/krka/kahlua/integration/LuaReturn.java +++ b/j2se/src/se/krka/kahlua/integration/LuaReturn.java @@ -25,6 +25,7 @@ of this software and associated documentation files (the "Software"), to deal import java.util.AbstractList; public abstract class LuaReturn extends AbstractList