Skip to content

Commit

Permalink
Remove some unused method on Varargs
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Jan 18, 2024
1 parent c51075b commit 937ee1c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 73 deletions.
3 changes: 1 addition & 2 deletions src/main/java/org/squiddev/cobalt/LuaValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ public final LuaString luaTypeName() {
* @see Constants#NIL
* @see Constants#NONE
* @see #optValue(LuaValue)
* @see Varargs#isNoneOrNil(int)
* @see Constants#TNIL
*/
public final boolean isNil() {
Expand Down Expand Up @@ -499,7 +498,6 @@ public final LuaThread optThread(LuaThread defval) throws LuaError {
* @see Constants#NIL
* @see Constants#NONE
* @see #isNil()
* @see Varargs#isNoneOrNil(int)
* @see Constants#TNIL
*/
public final LuaValue optValue(LuaValue defval) {
Expand Down Expand Up @@ -700,6 +698,7 @@ public int count() {
}

@Override
@Deprecated
public LuaValue first() {
return this;
}
Expand Down
61 changes: 0 additions & 61 deletions src/main/java/org/squiddev/cobalt/Varargs.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
*
* @see ValueFactory#varargsOf(LuaValue[])
* @see ValueFactory#varargsOf(LuaValue, Varargs)
* @see ValueFactory#varargsOf(LuaValue[], Varargs)
* @see ValueFactory#varargsOf(LuaValue, LuaValue, Varargs)
* @see ValueFactory#varargsOfCopy(LuaValue[], int, int)
* @see ValueFactory#varargsOfCopy(LuaValue[], int, int, Varargs)
Expand Down Expand Up @@ -79,42 +78,6 @@ public abstract class Varargs {

public abstract void fill(LuaValue[] array, int offset);

// -----------------------------------------------------------------------
// utilities to get specific arguments and type-check them.
// -----------------------------------------------------------------------

/**
* Gets the type of argument {@code i}
*
* @param i the index of the argument to convert, 1 is the first argument
* @return int value corresponding to one of the LuaValue integer type values
* @see LuaValue#type()
*/
public int type(int i) {
return arg(i).type();
}

/**
* Tests if argument i is nil.
*
* @param i the index of the argument to test, 1 is the first argument
* @return true if the argument is nil or does not exist, false otherwise
* @see Constants#TNIL
*/
public boolean isNil(int i) {
return arg(i).isNil();
}

/**
* Tests if a value exists at argument i.
*
* @param i the index of the argument to test, 1 is the first argument
* @return true if the argument exists, false otherwise
*/
public boolean exists(int i) {
return i > 0 && i <= count();
}

/**
* Return argument i as a LuaValue if it exists, or throw an error.
*
Expand All @@ -130,30 +93,6 @@ public LuaValue checkValue(int i) throws LuaError {
}
}

/**
* Throw an error if {@code test} fails
*
* @param test user supplied assertion to test against
* @param i the index to report in any error message
* @param msg the error message to use when the test fails
* @throws LuaError if the the value of {@code test} is {@code false}
*/
public static void argCheck(boolean test, int i, String msg) throws LuaError {
if (!test) {
throw ErrorFactory.argError(i, msg);
}
}

/**
* Return true if there is no argument or nil at argument i.
*
* @param i the index of the argument to test, 1 is the first argument
* @return true if argument i contains either no argument or nil
*/
public boolean isNoneOrNil(int i) {
return i > count() || arg(i).isNil();
}

/**
* Convert the list of varargs values to a human readable java String.
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/squiddev/cobalt/lib/BaseLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ private static LuaValue getfenvobj(LuaState state, LuaValue arg, boolean optiona
if (arg instanceof LuaFunction) return arg;

int level = optional ? arg.optInteger(1) : arg.checkInteger();
Varargs.argCheck(level >= 0, 1, "level must be non-negative");
if (level < 0) throw ErrorFactory.argError(1, "level must be non-negative");
if (level == 0) return state.getCurrentThread();
LuaValue f = LuaThread.getCallstackFunction(state, level);
Varargs.argCheck(f != null, 1, "invalid level");
if (f == null) throw ErrorFactory.argError(1, "invalid level");
return f;
}

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/squiddev/cobalt/lib/TableLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import cc.tweaked.cobalt.internal.unwind.AutoUnwind;
import cc.tweaked.cobalt.internal.unwind.SuspendedAction;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.squiddev.cobalt.*;
import org.squiddev.cobalt.debug.DebugFrame;
import org.squiddev.cobalt.function.Dispatch;
Expand Down Expand Up @@ -275,14 +276,14 @@ private static Varargs sort(LuaState state, DebugFrame di, Varargs args) throws
return SuspendedAction.run(di, () -> {
int n = OperationHelper.intLength(state, table);

LuaValue compare = args.isNoneOrNil(2) ? NIL : args.arg(2).checkFunction();
LuaFunction compare = args.arg(2).optFunction(null);
if (n > 1) heapSort(state, table, n, compare);
return NONE;
});
}

@AutoUnwind
private static void heapSort(LuaState state, LuaValue table, int count, LuaValue compare) throws LuaError, UnwindThrowable {
private static void heapSort(LuaState state, LuaValue table, int count, @Nullable LuaFunction compare) throws LuaError, UnwindThrowable {
for (int start = count / 2 - 1; start >= 0; start--) {
siftDown(state, table, start, count - 1, compare);
}
Expand All @@ -298,7 +299,7 @@ private static void heapSort(LuaState state, LuaValue table, int count, LuaValue
}

@AutoUnwind
private static void siftDown(LuaState state, LuaValue table, int start, int end, LuaValue compare) throws LuaError, UnwindThrowable {
private static void siftDown(LuaState state, LuaValue table, int start, int end, @Nullable LuaFunction compare) throws LuaError, UnwindThrowable {
LuaValue rootValue = OperationHelper.getTable(state, table, start + 1);

for (int root = start; root * 2 + 1 <= end; ) {
Expand All @@ -325,8 +326,8 @@ private static void siftDown(LuaState state, LuaValue table, int start, int end,
}

@AutoUnwind
private static boolean compare(LuaState state, LuaValue compare, LuaValue a, LuaValue b) throws LuaError, UnwindThrowable {
return compare.isNil()
private static boolean compare(LuaState state, @Nullable LuaFunction compare, LuaValue a, LuaValue b) throws LuaError, UnwindThrowable {
return compare == null
? OperationHelper.lt(state, a, b)
: Dispatch.call(state, compare, a, b).toBoolean();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ private static LuaValue collectgarbage(LuaState state, LuaValue arg1, LuaValue a

private Varargs loadfile(LuaState state, Varargs args) throws LuaError {
// loadfile( [filename] ) -> chunk | nil, msg
return args.isNil(1) ?
return args.first().isNil() ?
BaseLib.loadStream(state, in, STDIN_STR) :
SystemBaseLib.loadFile(state, resources, args.arg(1).checkString());
}

private Varargs dofile(LuaState state, Varargs args) throws LuaError, UnwindThrowable {
// dofile( filename ) -> result1, ...
Varargs v = args.isNil(1) ?
Varargs v = args.first().isNil() ?
BaseLib.loadStream(state, in, STDIN_STR) :
SystemBaseLib.loadFile(state, resources, args.arg(1).checkString());
if (v.isNil(1)) {
if (v.first().isNil()) {
throw new LuaError(v.arg(2).toString());
} else {
return Dispatch.invoke(state, v.first(), Constants.NONE);
Expand Down

0 comments on commit 937ee1c

Please sign in to comment.