Skip to content

Commit

Permalink
improved javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristofer Karlsson committed Jun 28, 2010
1 parent a653c5e commit c9c4fed
Show file tree
Hide file tree
Showing 38 changed files with 206 additions and 201 deletions.
32 changes: 14 additions & 18 deletions cldc11/src/se/krka/kahlua/cldc11/MathLib.java
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 13 additions & 2 deletions core/src/se/krka/kahlua/stdlib/BaseLib.java
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion core/src/se/krka/kahlua/stdlib/CoroutineLib.java
Expand Up @@ -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;

Expand Down
54 changes: 23 additions & 31 deletions core/src/se/krka/kahlua/stdlib/RandomLib.java
Expand Up @@ -54,49 +54,41 @@ 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);
return 0;
}

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());
}
}
38 changes: 21 additions & 17 deletions core/src/se/krka/kahlua/stdlib/StringLib.java
Expand Up @@ -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];
Expand All @@ -76,6 +76,7 @@ public final class StringLib implements JavaFunction {
}
}

/** @exclude */
private final int methodId;
public StringLib(int index) {
this.methodId = index;
Expand Down Expand Up @@ -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 */
Expand All @@ -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;
Expand All @@ -844,6 +847,7 @@ public Object getCapture(int i) {
}
}

/** @exclude */
public static class StringPointer {
private final String string;
private int index = 0;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down
23 changes: 14 additions & 9 deletions core/src/se/krka/kahlua/vm/JavaFunction.java
Expand Up @@ -25,16 +25,21 @@ of this software and associated documentation files (the "Software"), to deal

public interface JavaFunction {
/**
* General contract.<br>
* <br>
* Input:<br>
* callFrame = the frame that contains all the arguments, and where all the results should be put.<br>
* nArguments = number of function arguments<br>
* callFrame.get(i) = an argument (0 <= i < nArgs)<br>
*
* @param callFrame - the current callframe for the function
* This interface defines functions which the Kahlua engine can call.
*
* General contract:
* <pre>
* callFrame.get(i) = an argument (0 <= i < nArguments)<br>
* </pre>
* Return (possibly) values to lua by calling:
* <pre>
* callFrame.push(value1);
* callFrame.push(value2);
* return 2; // number of pushed values
* </pre>
* @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);
}

0 comments on commit c9c4fed

Please sign in to comment.