1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-16 10:09:55 +00:00

Convert NamedMethod into a record

This commit is contained in:
Jonathan Coates 2023-06-26 18:51:14 +01:00
parent 50d460624f
commit 4a5e03c11a
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
8 changed files with 28 additions and 43 deletions

View File

@ -19,8 +19,8 @@ final class SaturatedMethod {
SaturatedMethod(Object target, NamedMethod<PeripheralMethod> method) {
this.target = target;
name = method.getName();
this.method = method.getMethod();
name = method.name();
this.method = method.method();
}
MethodResult apply(ILuaContext context, IComputerAccess computer, IArguments args) throws LuaException {

View File

@ -328,7 +328,7 @@ public static Map<String, PeripheralMethod> getMethods(IPeripheral peripheral) {
methodMap.put(dynamicMethods[i], PeripheralMethod.DYNAMIC.get(i));
}
for (var method : methods) {
methodMap.put(method.getName(), method.getMethod());
methodMap.put(method.name(), method.method());
}
return methodMap;
}

View File

@ -4,38 +4,23 @@
package dan200.computercraft.core.asm;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.api.peripheral.GenericPeripheral;
import dan200.computercraft.api.peripheral.PeripheralType;
import javax.annotation.Nullable;
public final class NamedMethod<T> {
private final String name;
private final T method;
private final boolean nonYielding;
private final @Nullable PeripheralType genericType;
NamedMethod(String name, T method, boolean nonYielding, @Nullable PeripheralType genericType) {
this.name = name;
this.method = method;
this.nonYielding = nonYielding;
this.genericType = genericType;
}
public String getName() {
return name;
}
public T getMethod() {
return method;
}
public boolean nonYielding() {
return nonYielding;
}
@Nullable
public PeripheralType getGenericType() {
return genericType;
}
/**
* A method generated from a {@link LuaFunction}.
*
* @param name The name of this method.
* @param method The underlying method implementation.
* @param nonYielding If this method is guaranteed to never yield, and will always return a
* {@linkplain MethodResult#of(Object...) basic result}.
* @param genericType The peripheral type of this method. This is only set if this is a method on a
* {@link GenericPeripheral}.
* @param <T> The type of method, either a {@link LuaMethod} or {@link PeripheralMethod}.
*/
public record NamedMethod<T>(String name, T method, boolean nonYielding, @Nullable PeripheralType genericType) {
}

View File

@ -172,9 +172,9 @@ private LuaTable wrapLuaObject(Object object) {
}
ObjectSource.allMethods(LuaMethod.GENERATOR, object, (instance, method) ->
table.rawset(method.getName(), method.nonYielding()
? new BasicFunction(this, method.getMethod(), instance, context, method.getName())
: new ResultInterpreterFunction(this, method.getMethod(), instance, context, method.getName())));
table.rawset(method.name(), method.nonYielding()
? new BasicFunction(this, method.method(), instance, context, method.name())
: new ResultInterpreterFunction(this, method.method(), instance, context, method.name())));
try {
if (table.next(Constants.NIL).first().isNil()) return null;

View File

@ -28,7 +28,7 @@ public ObjectWrapper(Object object) {
methodMap.put(dynamicMethods[i], LuaMethod.DYNAMIC.get(i));
}
for (var method : methods) {
methodMap.put(method.getName(), method.getMethod());
methodMap.put(method.name(), method.method());
}
}

View File

@ -44,7 +44,7 @@ public void testIdenticalMethods() {
var methods = LuaMethod.GENERATOR.getMethods(Basic.class);
var methods2 = LuaMethod.GENERATOR.getMethods(Basic2.class);
assertThat(methods, contains(named("go")));
assertThat(methods.get(0).getMethod(), sameInstance(methods2.get(0).getMethod()));
assertThat(methods.get(0).method(), sameInstance(methods2.get(0).method()));
}
@Test
@ -217,8 +217,8 @@ public final void invalid(LuaTable<?, ?> table) {
private static <T> T find(Collection<NamedMethod<T>> methods, String name) {
return methods.stream()
.filter(x -> x.getName().equals(name))
.map(NamedMethod::getMethod)
.filter(x -> x.name().equals(name))
.map(NamedMethod::method)
.findAny()
.orElseThrow(NullPointerException::new);
}
@ -235,7 +235,7 @@ public static Matcher<MethodResult> one(Matcher<Object> object) {
}
public static <T> Matcher<NamedMethod<T>> named(String method) {
return contramap(is(method), "name", NamedMethod::getName);
return contramap(is(method), "name", NamedMethod::name);
}
private static final ILuaContext CONTEXT = new ILuaContext() {

View File

@ -71,7 +71,7 @@ void addMethods(Object target, List<NamedMethod<PeripheralMethod>> methods) {
// If we have a peripheral type, use it. Always pick the smallest one, so it's consistent (assuming mods
// don't change).
var type = method.getGenericType();
var type = method.genericType();
if (type != null && type.getPrimaryType() != null) {
var name = type.getPrimaryType();
if (this.name == null || this.name.compareTo(name) > 0) this.name = name;

View File

@ -71,7 +71,7 @@ void addMethods(Object target, List<NamedMethod<PeripheralMethod>> methods) {
// If we have a peripheral type, use it. Always pick the smallest one, so it's consistent (assuming mods
// don't change).
var type = method.getGenericType();
var type = method.genericType();
if (type != null && type.getPrimaryType() != null) {
var name = type.getPrimaryType();
if (this.name == null || this.name.compareTo(name) > 0) this.name = name;