mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-02-04 21:29:13 +00:00
Convert NamedMethod into a record
This commit is contained in:
parent
50d460624f
commit
4a5e03c11a
@ -19,8 +19,8 @@ final class SaturatedMethod {
|
|||||||
|
|
||||||
SaturatedMethod(Object target, NamedMethod<PeripheralMethod> method) {
|
SaturatedMethod(Object target, NamedMethod<PeripheralMethod> method) {
|
||||||
this.target = target;
|
this.target = target;
|
||||||
name = method.getName();
|
name = method.name();
|
||||||
this.method = method.getMethod();
|
this.method = method.method();
|
||||||
}
|
}
|
||||||
|
|
||||||
MethodResult apply(ILuaContext context, IComputerAccess computer, IArguments args) throws LuaException {
|
MethodResult apply(ILuaContext context, IComputerAccess computer, IArguments args) throws LuaException {
|
||||||
|
@ -328,7 +328,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
|||||||
methodMap.put(dynamicMethods[i], PeripheralMethod.DYNAMIC.get(i));
|
methodMap.put(dynamicMethods[i], PeripheralMethod.DYNAMIC.get(i));
|
||||||
}
|
}
|
||||||
for (var method : methods) {
|
for (var method : methods) {
|
||||||
methodMap.put(method.getName(), method.getMethod());
|
methodMap.put(method.name(), method.method());
|
||||||
}
|
}
|
||||||
return methodMap;
|
return methodMap;
|
||||||
}
|
}
|
||||||
|
@ -4,38 +4,23 @@
|
|||||||
|
|
||||||
package dan200.computercraft.core.asm;
|
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 dan200.computercraft.api.peripheral.PeripheralType;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public final class NamedMethod<T> {
|
/**
|
||||||
private final String name;
|
* A method generated from a {@link LuaFunction}.
|
||||||
private final T method;
|
*
|
||||||
private final boolean nonYielding;
|
* @param name The name of this method.
|
||||||
|
* @param method The underlying method implementation.
|
||||||
private final @Nullable PeripheralType genericType;
|
* @param nonYielding If this method is guaranteed to never yield, and will always return a
|
||||||
|
* {@linkplain MethodResult#of(Object...) basic result}.
|
||||||
NamedMethod(String name, T method, boolean nonYielding, @Nullable PeripheralType genericType) {
|
* @param genericType The peripheral type of this method. This is only set if this is a method on a
|
||||||
this.name = name;
|
* {@link GenericPeripheral}.
|
||||||
this.method = method;
|
* @param <T> The type of method, either a {@link LuaMethod} or {@link PeripheralMethod}.
|
||||||
this.nonYielding = nonYielding;
|
*/
|
||||||
this.genericType = genericType;
|
public record NamedMethod<T>(String name, T method, boolean nonYielding, @Nullable PeripheralType genericType) {
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public T getMethod() {
|
|
||||||
return method;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean nonYielding() {
|
|
||||||
return nonYielding;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public PeripheralType getGenericType() {
|
|
||||||
return genericType;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -172,9 +172,9 @@ public class CobaltLuaMachine implements ILuaMachine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ObjectSource.allMethods(LuaMethod.GENERATOR, object, (instance, method) ->
|
ObjectSource.allMethods(LuaMethod.GENERATOR, object, (instance, method) ->
|
||||||
table.rawset(method.getName(), method.nonYielding()
|
table.rawset(method.name(), method.nonYielding()
|
||||||
? new BasicFunction(this, method.getMethod(), instance, context, method.getName())
|
? new BasicFunction(this, method.method(), instance, context, method.name())
|
||||||
: new ResultInterpreterFunction(this, method.getMethod(), instance, context, method.getName())));
|
: new ResultInterpreterFunction(this, method.method(), instance, context, method.name())));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (table.next(Constants.NIL).first().isNil()) return null;
|
if (table.next(Constants.NIL).first().isNil()) return null;
|
||||||
|
@ -28,7 +28,7 @@ public class ObjectWrapper implements ILuaContext {
|
|||||||
methodMap.put(dynamicMethods[i], LuaMethod.DYNAMIC.get(i));
|
methodMap.put(dynamicMethods[i], LuaMethod.DYNAMIC.get(i));
|
||||||
}
|
}
|
||||||
for (var method : methods) {
|
for (var method : methods) {
|
||||||
methodMap.put(method.getName(), method.getMethod());
|
methodMap.put(method.name(), method.method());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ public class GeneratorTest {
|
|||||||
var methods = LuaMethod.GENERATOR.getMethods(Basic.class);
|
var methods = LuaMethod.GENERATOR.getMethods(Basic.class);
|
||||||
var methods2 = LuaMethod.GENERATOR.getMethods(Basic2.class);
|
var methods2 = LuaMethod.GENERATOR.getMethods(Basic2.class);
|
||||||
assertThat(methods, contains(named("go")));
|
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
|
@Test
|
||||||
@ -217,8 +217,8 @@ public class GeneratorTest {
|
|||||||
|
|
||||||
private static <T> T find(Collection<NamedMethod<T>> methods, String name) {
|
private static <T> T find(Collection<NamedMethod<T>> methods, String name) {
|
||||||
return methods.stream()
|
return methods.stream()
|
||||||
.filter(x -> x.getName().equals(name))
|
.filter(x -> x.name().equals(name))
|
||||||
.map(NamedMethod::getMethod)
|
.map(NamedMethod::method)
|
||||||
.findAny()
|
.findAny()
|
||||||
.orElseThrow(NullPointerException::new);
|
.orElseThrow(NullPointerException::new);
|
||||||
}
|
}
|
||||||
@ -235,7 +235,7 @@ public class GeneratorTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static <T> Matcher<NamedMethod<T>> named(String method) {
|
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() {
|
private static final ILuaContext CONTEXT = new ILuaContext() {
|
||||||
|
@ -71,7 +71,7 @@ public class GenericPeripheralProvider {
|
|||||||
|
|
||||||
// If we have a peripheral type, use it. Always pick the smallest one, so it's consistent (assuming mods
|
// If we have a peripheral type, use it. Always pick the smallest one, so it's consistent (assuming mods
|
||||||
// don't change).
|
// don't change).
|
||||||
var type = method.getGenericType();
|
var type = method.genericType();
|
||||||
if (type != null && type.getPrimaryType() != null) {
|
if (type != null && type.getPrimaryType() != null) {
|
||||||
var name = type.getPrimaryType();
|
var name = type.getPrimaryType();
|
||||||
if (this.name == null || this.name.compareTo(name) > 0) this.name = name;
|
if (this.name == null || this.name.compareTo(name) > 0) this.name = name;
|
||||||
|
@ -71,7 +71,7 @@ public class GenericPeripheralProvider {
|
|||||||
|
|
||||||
// If we have a peripheral type, use it. Always pick the smallest one, so it's consistent (assuming mods
|
// If we have a peripheral type, use it. Always pick the smallest one, so it's consistent (assuming mods
|
||||||
// don't change).
|
// don't change).
|
||||||
var type = method.getGenericType();
|
var type = method.genericType();
|
||||||
if (type != null && type.getPrimaryType() != null) {
|
if (type != null && type.getPrimaryType() != null) {
|
||||||
var name = type.getPrimaryType();
|
var name = type.getPrimaryType();
|
||||||
if (this.name == null || this.name.compareTo(name) > 0) this.name = name;
|
if (this.name == null || this.name.compareTo(name) > 0) this.name = name;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user