1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-09-28 23:10:47 +00:00

Make executeMainThreadTask a default method

- Move TaskCallback into the API and make it package private. This
   effectively means it's not an API class, just exists there for
   convenience reasons.
 - Replace any usage of TaskCallback.make with
   ILuaContext.executeMainThreadTask.
 - Some minor formatting/checkstyle changes to bring us inline with
   IntelliJ config.
This commit is contained in:
Jonathan Coates 2021-11-21 11:19:02 +00:00
parent 2fe40f669d
commit 070479d901
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
13 changed files with 61 additions and 66 deletions

View File

@ -149,8 +149,12 @@
<property name="tokens" value="COMMA" />
</module>
<module name="WhitespaceAround">
<property name="allowEmptyConstructors" value="true" />
<property name="ignoreEnhancedForColon" value="false" />
<!-- Allow empty functions -->
<property name="allowEmptyLambdas" value="true" />
<property name="allowEmptyMethods" value="true" />
<property name="allowEmptyConstructors" value="true" />
<property name="tokens" value="ASSIGN,BAND,BAND_ASSIGN,BOR,BOR_ASSIGN,BSR,BSR_ASSIGN,BXOR,BXOR_ASSIGN,COLON,DIV,DIV_ASSIGN,EQUAL,GE,GT,LAMBDA,LAND,LCURLY,LE,LITERAL_RETURN,LOR,LT,MINUS,MINUS_ASSIGN,MOD,MOD_ASSIGN,NOT_EQUAL,PLUS,PLUS_ASSIGN,QUESTION,RCURLY,SL,SLIST,SL_ASSIGN,SR,SR_ASSIGN,STAR,STAR_ASSIGN,LITERAL_ASSERT,TYPE_EXTENSION_AND" />
</module>
</module>

View File

@ -40,5 +40,8 @@ public interface ILuaContext
* @throws LuaException If the task could not be queued, or if the task threw an exception.
*/
@Nonnull
MethodResult executeMainThreadTask( @Nonnull ILuaTask task ) throws LuaException;
default MethodResult executeMainThreadTask( @Nonnull ILuaTask task ) throws LuaException
{
return TaskCallback.make( this, task );
}
}

View File

@ -1,16 +1,14 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2021. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/
package dan200.computercraft.core.asm;
import dan200.computercraft.api.lua.*;
package dan200.computercraft.api.lua;
import javax.annotation.Nonnull;
import java.util.Arrays;
public final class TaskCallback implements ILuaCallback
final class TaskCallback implements ILuaCallback
{
private final MethodResult pull = MethodResult.pullEvent( "task_complete", this );
private final long task;
@ -47,19 +45,7 @@ public final class TaskCallback implements ILuaCallback
}
}
static Object[] checkUnwrap( MethodResult result )
{
if( result.getCallback() != null )
{
// Due to how tasks are implemented, we can't currently return a MethodResult. This is an
// entirely artificial limitation - we can remove it if it ever becomes an issue.
throw new IllegalStateException( "Cannot return MethodResult for mainThread task." );
}
return result.getResult();
}
public static MethodResult make( ILuaContext context, ILuaTask func ) throws LuaException
static MethodResult make( ILuaContext context, ILuaTask func ) throws LuaException
{
long task = context.issueMainThreadTask( func );
return new TaskCallback( task ).pull;

View File

@ -13,7 +13,7 @@ import java.util.Collections;
public interface LuaMethod
{
Generator<LuaMethod> GENERATOR = new Generator<>( LuaMethod.class, Collections.singletonList( ILuaContext.class ),
m -> ( target, context, args ) -> TaskCallback.make( context, () -> TaskCallback.checkUnwrap( m.apply( target, context, args ) ) )
m -> ( target, context, args ) -> context.executeMainThreadTask( () -> ResultHelpers.checkNormalResult( m.apply( target, context, args ) ) )
);
IntCache<LuaMethod> DYNAMIC = new IntCache<>(

View File

@ -18,7 +18,7 @@ import java.util.Arrays;
public interface PeripheralMethod
{
Generator<PeripheralMethod> GENERATOR = new Generator<>( PeripheralMethod.class, Arrays.asList( ILuaContext.class, IComputerAccess.class ),
m -> ( target, context, computer, args ) -> TaskCallback.make( context, () -> TaskCallback.checkUnwrap( m.apply( target, context, computer, args ) ) )
m -> ( target, context, computer, args ) -> context.executeMainThreadTask( () -> ResultHelpers.checkNormalResult( m.apply( target, context, computer, args ) ) )
);
IntCache<PeripheralMethod> DYNAMIC = new IntCache<>(

View File

@ -0,0 +1,27 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.core.asm;
import dan200.computercraft.api.lua.MethodResult;
final class ResultHelpers
{
private ResultHelpers()
{
}
static Object[] checkNormalResult( MethodResult result )
{
if( result.getCallback() != null )
{
// Due to how tasks are implemented, we can't currently return a MethodResult. This is an
// entirely artificial limitation - we can remove it if it ever becomes an issue.
throw new IllegalStateException( "Must return MethodResult.of from mainThread function." );
}
return result.getResult();
}
}

View File

@ -93,7 +93,7 @@ public final class MainThread
executor.updateTime();
// We're not currently on the queue, so update its current execution time to
// ensure its at least as high as the minimum.
// ensure it's at least as high as the minimum.
long newRuntime = minimumTime;
// Slow down new computers a little bit.

View File

@ -9,8 +9,6 @@ import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.ILuaTask;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.core.asm.TaskCallback;
import dan200.computercraft.core.computer.Computer;
import dan200.computercraft.core.computer.MainThread;
@ -68,11 +66,4 @@ class LuaContext implements ILuaContext
throw new LuaException( "Task limit exceeded" );
}
}
@Nonnull
@Override
public MethodResult executeMainThreadTask( @Nonnull ILuaTask task ) throws LuaException
{
return TaskCallback.make( this, task );
}
}

View File

@ -26,7 +26,7 @@ public class BasicCustomLoader<T extends ModelBuilder<T>> extends CustomLoaderBu
public static <T extends ModelBuilder<T>> BiFunction<T, ExistingFileHelper, CustomLoaderBuilder<T>> makeFactory( ResourceLocation id )
{
return makeFactory( id, j -> { } );
return makeFactory( id, j -> {} );
}
public static <T extends ModelBuilder<T>> BiFunction<T, ExistingFileHelper, CustomLoaderBuilder<T>> makeFactory( ResourceLocation id, Consumer<JsonObject> extra )

View File

@ -17,7 +17,7 @@ import java.util.Map;
public final class DataHelpers
{
private DataHelpers()
{ }
{}
@Nonnull
public static Map<String, Boolean> getTags( @Nonnull Collection<ResourceLocation> tags )

View File

@ -13,7 +13,6 @@ import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.event.TurtleActionEvent;
import dan200.computercraft.api.turtle.event.TurtleInspectItemEvent;
import dan200.computercraft.core.apis.IAPIEnvironment;
import dan200.computercraft.core.asm.TaskCallback;
import dan200.computercraft.core.tracking.TrackingField;
import dan200.computercraft.shared.peripheral.generic.data.ItemData;
import dan200.computercraft.shared.peripheral.generic.methods.InventoryMethods;
@ -770,7 +769,7 @@ public class TurtleAPI implements ILuaAPI
{
int actualSlot = checkSlot( slot ).orElse( turtle.getSelectedSlot() );
return detailed.orElse( false )
? TaskCallback.make( context, () -> getItemDetail( actualSlot, true ) )
? context.executeMainThreadTask( () -> getItemDetail( actualSlot, true ) )
: MethodResult.of( getItemDetail( actualSlot, false ) );
}

View File

@ -8,7 +8,6 @@ package dan200.computercraft.core.apis;
import dan200.computercraft.api.lua.*;
import dan200.computercraft.core.asm.LuaMethod;
import dan200.computercraft.core.asm.NamedMethod;
import dan200.computercraft.core.asm.TaskCallback;
import javax.annotation.Nonnull;
import java.util.HashMap;
@ -65,11 +64,4 @@ public class ObjectWrapper implements ILuaContext
{
throw new IllegalStateException( "Method should never queue events" );
}
@Nonnull
@Override
public MethodResult executeMainThreadTask( @Nonnull ILuaTask task ) throws LuaException
{
return TaskCallback.make( this, task );
}
}

View File

@ -124,7 +124,7 @@ public class GeneratorTest
{
@LuaFunction
public final void go()
{ }
{}
}
public static class Basic2 extends Basic
@ -139,14 +139,14 @@ public class GeneratorTest
{
@LuaFunction
public final void go()
{ }
{}
}
public static class NonInstance
{
@LuaFunction
public static void go()
{ }
{}
}
public static class IllegalThrows
@ -162,42 +162,42 @@ public class GeneratorTest
{
@LuaFunction( { "go1", "go2" } )
public final void go()
{ }
{}
}
public static class ArgKinds
{
@LuaFunction
public final void objectArg( Object arg )
{ }
{}
@LuaFunction
public final void intArg( int arg )
{ }
{}
@LuaFunction
public final void optIntArg( Optional<Integer> arg )
{ }
{}
@LuaFunction
public final void context( ILuaContext arg )
{ }
{}
@LuaFunction
public final void arguments( IArguments arg )
{ }
{}
@LuaFunction
public final void unknown( IComputerAccess arg )
{ }
{}
@LuaFunction
public final void illegalMap( Map<String, Integer> arg )
{ }
{}
@LuaFunction
public final void optIllegalMap( Optional<Map<String, Integer>> arg )
{ }
{}
}
public static class EnumMethods
@ -219,7 +219,7 @@ public class GeneratorTest
{
@LuaFunction( mainThread = true )
public final void go()
{ }
{}
}
private static <T> T find( Collection<NamedMethod<T>> methods, String name )
@ -256,12 +256,5 @@ public class GeneratorTest
{
return 0;
}
@Nonnull
@Override
public MethodResult executeMainThreadTask( @Nonnull ILuaTask task ) throws LuaException
{
return TaskCallback.make( this, task );
}
};
}