1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-26 07:03:22 +00:00

Proposal for replacing ILuaContext with a non-blocking alternative

This is an initial prototype for ways we could better integrate Lua's
asynchronous functionality (coroutines) with peripherals and other
Lua APIs.

The existing system assumes that coroutines each have a backing thread,
and so yields will suspect the current thread. This takes inspiration
from various "promise" libraries - asynchronous operations (such as
executing on the main thread or waiting for an event) return a promise -
the result of which can be consumed with `.then`.

While I am not aware of plans to change the Lua implementation, this
does give us greater flexibility in the future, leading the way for
Rembulan, single-threaded Cobalt, and even possibly OC support.
This commit is contained in:
SquidDev 2018-09-09 16:48:55 +01:00
parent 914df8b0c7
commit 3b4c1eac1c
12 changed files with 717 additions and 35 deletions

View File

@ -0,0 +1,31 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. 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.api.lua;
import javax.annotation.Nonnull;
/**
* An interface passed to peripherals and {@link ILuaObject}s by computers or turtles, providing methods that allow the
* method to interact with the invoking computer.
*/
public interface ICallContext
{
/**
* Queue a task to be executed on the main server thread at the beginning of next tick, but do not wait for it to
* complete. This should be used when you need to interact with the world in a thread-safe manner but do not care
* about the result or you wish to run asynchronously.
*
* When the task has finished, it will enqueue a {@code task_completed} event, which takes the task id, a success
* value and the return values, or an error message if it failed. If you need to wait on this event, it may be
* better to use {@link MethodResult#onMainThread(ILuaCallable)}.
*
* @param task The task to execute on the main thread.
* @return The "id" of the task. This will be the first argument to the {@code task_completed} event.
* @throws LuaException If the task could not be queued.
*/
long issueMainThreadTask( @Nonnull ILuaTask task ) throws LuaException;
}

View File

@ -0,0 +1,31 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. 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.api.lua;
import javax.annotation.Nonnull;
/**
* A function which calls performs an action in a specific context (such as on the server thread) and returns a result.
*
* @see MethodResult#onMainThread(ILuaCallable)
* @see ILuaContext#executeMainThreadTask(ILuaTask)
*/
@FunctionalInterface
public interface ILuaCallable
{
/**
* Run the code within the specified context and return the result to continue with.
*
* @return The result of executing this function. Note that this may not be evaluated within the same context as
* this call is.
* @throws LuaException If you throw any exception from this function, a lua error will be raised with the
* same message as your exception. Use this to throw appropriate errors if the wrong
* arguments are supplied to your method.
*/
@Nonnull
MethodResult execute() throws LuaException;
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2018. 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.
*/
@ -13,8 +13,11 @@
* An interface passed to peripherals and {@link ILuaObject}s by computers or turtles, providing methods
* that allow the peripheral call to wait for events before returning, just like in lua. This is very useful if you need
* to signal work to be performed on the main thread, and don't want to return until the work has been completed.
*
* This interface mostly exists for integrating with older code. One should use {@link MethodResult} instead, as this
* encourages an asynchronous way of interacting with Lua coroutines.
*/
public interface ILuaContext
public interface ILuaContext extends ICallContext
{
/**
* Wait for an event to occur on the computer, suspending the thread until it arises. This method is exactly
@ -30,8 +33,10 @@ public interface ILuaContext
* @throws InterruptedException If the user shuts down or reboots the computer while pullEvent() is waiting for an
* event, InterruptedException will be thrown. This exception must not be caught or
* intercepted, or the computer will leak memory and end up in a broken state.
* @deprecated Use {@link MethodResult#pullEvent(String, ILuaFunction)}
*/
@Nonnull
@Deprecated
Object[] pullEvent( @Nullable String filter ) throws LuaException, InterruptedException;
/**
@ -45,8 +50,10 @@ public interface ILuaContext
* an event, InterruptedException will be thrown. This exception must not be caught or
* intercepted, or the computer will leak memory and end up in a broken state.
* @see #pullEvent(String)
* @deprecated Use {@link MethodResult#pullEventRaw(String, ILuaFunction)}
*/
@Nonnull
@Deprecated
Object[] pullEventRaw( @Nullable String filter ) throws InterruptedException;
/**
@ -59,8 +66,10 @@ public interface ILuaContext
* InterruptedException will be thrown. This exception must not be caught or
* intercepted, or the computer will leak memory and end up in a broken state.
* @see #pullEvent(String)
* @deprecated Use {@link MethodResult#pullEventRaw(ILuaFunction)}
*/
@Nonnull
@Deprecated
Object[] yield( @Nullable Object[] arguments ) throws InterruptedException;
/**
@ -76,22 +85,9 @@ public interface ILuaContext
* @throws InterruptedException If the user shuts down or reboots the computer the coroutine is suspended,
* InterruptedException will be thrown. This exception must not be caught or
* intercepted, or the computer will leak memory and end up in a broken state.
* @deprecated Use {@link MethodResult#onMainThread(ILuaCallable)}
*/
@Nullable
@Deprecated
Object[] executeMainThreadTask( @Nonnull ILuaTask task ) throws LuaException, InterruptedException;
/**
* Queue a task to be executed on the main server thread at the beginning of next tick, but do not wait for it to
* complete. This should be used when you need to interact with the world in a thread-safe manner but do not care
* about the result or you wish to run asynchronously.
*
* When the task has finished, it will enqueue a {@code task_completed} event, which takes the task id, a success
* value and the return values, or an error message if it failed. If you need to wait on this event, it may be
* better to use {@link #executeMainThreadTask(ILuaTask)}.
*
* @param task The task to execute on the main thread.
* @return The "id" of the task. This will be the first argument to the {@code task_completed} event.
* @throws LuaException If the task could not be queued.
*/
long issueMainThreadTask( @Nonnull ILuaTask task ) throws LuaException;
}

View File

@ -0,0 +1,24 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. 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.api.lua;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* A function which executes using a {@link ILuaContext}.
*
* Like {@link ILuaContext}, this is not intended for use in the future - it purely exists as an argument for
* {@link MethodResult#withLuaContext(ILuaContextTask)}.
*/
@FunctionalInterface
public interface ILuaContextTask
{
@Nullable
@Deprecated
Object[] execute( @Nonnull ILuaContext context ) throws LuaException, InterruptedException;
}

View File

@ -0,0 +1,33 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. 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.api.lua;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* A Lua function which consumes some values and returns a result.
*
* @see MethodResult#then(ILuaFunction)
* @see MethodResult#pullEvent(ILuaFunction)
* @see MethodResult#pullEventRaw(String)
*/
@FunctionalInterface
public interface ILuaFunction
{
/**
* Accept the values and return another method result.
*
* @param values The inputs for this function.
* @return The result of executing this function.
* @throws LuaException If you throw any exception from this function, a lua error will be raised with the
* same message as your exception. Use this to throw appropriate errors if the wrong
* arguments are supplied to your method.
*/
@Nonnull
MethodResult call( @Nullable Object[] values ) throws LuaException;
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2018. 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.
*/
@ -41,16 +41,41 @@ public interface ILuaObject
* wishes to call. The integer indicates the index into the getMethodNames() table
* that corresponds to the string passed into peripheral.call()
* @param arguments The arguments for this method. See {@link IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[])}
* the possible values and conversion rules.
* @return An array of objects, representing the values you wish to return to the Lua program.
* See {@link IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[])} for the valid values and
* conversion rules.
* for the possible values and conversion rules.
* @return An array of objects, representing the values you wish to return to the Lua program. See
* {@link MethodResult#of(Object...)} for the valid values and conversion rules.
* @throws LuaException If the task could not be queued, or if the task threw an exception.
* @throws InterruptedException If the user shuts down or reboots the computer the coroutine is suspended,
* InterruptedException will be thrown. This exception must not be caught or
* intercepted, or the computer will leak memory and end up in a broken state.w
* @see IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[])
* @deprecated Use {@link #callMethod(ICallContext, int, Object[])} instead.
*/
@Nullable
@Deprecated
Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException;
/**
* Called when a user calls one of the methods that this object implements. This works the same as
* {@link IPeripheral#callMethod(IComputerAccess, ICallContext, int, Object[])}}. See that method for detailed
* documentation.
*
* @param context The context of the current call.
* @param method An integer identifying which of the methods from getMethodNames() the computercraft
* wishes to call. The integer indicates the index into the getMethodNames() table
* that corresponds to the string passed into peripheral.call()
* @param arguments The arguments for this method. See {@link IPeripheral#callMethod(IComputerAccess, ICallContext, int, Object[])}
* for the possible values and conversion rules.
* @return The result of calling this method. Use {@link MethodResult#empty()} to return nothing or
* {@link MethodResult#of(Object...)} to return several values.
* @throws LuaException If the task could not be queued, or if the task threw an exception.
* @see IPeripheral#callMethod(IComputerAccess, ICallContext, int, Object[])
* @see MethodResult
*/
@Nonnull
@SuppressWarnings({ "deprecation" })
default MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException
{
return MethodResult.withLuaContext( lua -> callMethod( lua, method, arguments ) );
}
}

View File

@ -0,0 +1,105 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. 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.api.lua;
import javax.annotation.Nonnull;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* Evaluates {@link MethodResult}s within a {@link ILuaContext}.
*
* @see MethodResult#evaluate(ILuaContext)
* @see MethodResult#withLuaContext(ILuaContextTask)
* @deprecated This should not be used except to interface between the two call call systems.
*/
@Deprecated
class LuaContextResultEvaluator
{
@Deprecated
public static Object[] evaluate( @Nonnull ILuaContext context, @Nonnull MethodResult future ) throws LuaException, InterruptedException
{
Deque<ILuaFunction> callbacks = null;
while( true )
{
if( future instanceof MethodResult.AndThen )
{
MethodResult.AndThen then = ((MethodResult.AndThen) future);
// Thens are "unwrapped", being pushed onto a stack
if( callbacks == null ) callbacks = new ArrayDeque<>();
callbacks.addLast( then.getCallback() );
future = then.getPrevious();
if( future == null ) throw new NullPointerException( "Null result from " + then.getCallback() );
}
else if( future instanceof MethodResult.Immediate )
{
Object[] values = ((MethodResult.Immediate) future).getResult();
// Immediate values values will attempt to call the previous "then", or return if nothing
// else needs to be done.
ILuaFunction callback = callbacks == null ? null : callbacks.pollLast();
if( callback == null ) return values;
future = callback.call( values );
if( future == null ) throw new NullPointerException( "Null result from " + callback );
}
else if( future instanceof MethodResult.OnEvent )
{
MethodResult.OnEvent onEvent = (MethodResult.OnEvent) future;
// Poll for an event, and then call the previous "then" or return if nothing else needs
// to be done.
Object[] values = onEvent.isRaw() ? context.pullEventRaw( onEvent.getFilter() ) : context.pullEvent( onEvent.getFilter() );
ILuaFunction callback = callbacks == null ? null : callbacks.pollLast();
if( callback == null ) return values;
future = callback.call( values );
if( future == null ) throw new NullPointerException( "Null result from " + callback );
}
else if( future instanceof MethodResult.OnMainThread )
{
MethodResult.OnMainThread onMainThread = (MethodResult.OnMainThread) future;
// Evaluate our task on the main thread and mark it as the next future to evaluate.
Reference temporary = new Reference();
context.executeMainThreadTask( () -> {
temporary.value = onMainThread.getTask().execute();
return null;
} );
future = temporary.value;
if( future == null ) throw new NullPointerException( "Null result from " + onMainThread.getTask() );
}
else if( future instanceof MethodResult.WithLuaContext )
{
MethodResult.WithLuaContext withContext = (MethodResult.WithLuaContext) future;
// Run the task, and then call the previous "then" or return if nothing else
// needs to be done.
Object[] values = withContext.getConsumer().execute( context );
ILuaFunction callback = callbacks == null ? null : callbacks.pollLast();
if( callback == null ) return values;
future = callback.call( values );
if( future == null ) throw new NullPointerException( "Null result from " + callback );
}
else
{
throw new IllegalStateException( "Unknown MethodResult " + future );
}
}
}
private static class Reference
{
MethodResult value;
}
}

View File

@ -0,0 +1,344 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. 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.api.lua;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.ListenableFuture;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;
/**
* The result of calling a method, such as {@link ILuaObject#callMethod(ICallContext, int, Object[])} or
* {@link IPeripheral#callMethod(IComputerAccess, ICallContext, int, Object[])}.
*
* This is non-dissimilar to a promise or {@link ListenableFuture}. One can either return an immediate value through
* {@link #of(Object...)}, wait for an external action with {@link #onMainThread(ILuaCallable)} or {@link #pullEvent()}
* and then act on the result of either of those by using {@link #then(ILuaFunction)}.
*/
public abstract class MethodResult
{
private static MethodResult empty;
MethodResult()
{
}
/**
* A result which returns immediately with no value.
*
* Use {@link #of(Object...)} if you need to return one or more values.
*
* @return The empty method result.
* @see #of(Object...)
*/
@Nonnull
public static MethodResult empty()
{
if( empty == null ) empty = new Immediate( null );
return empty;
}
/**
* A result which returns several values.
*
* @param result The values to return, this may be {@code null}. {@link Number}s, {@link String}s, {@link Boolean}s,
* {@link Map}s, {@link ILuaObject}s, and {@code null} be converted to their corresponding lua type.
* All other types will be converted to nil.
* @return A result which will return these values when evaluated.
* @see #empty()
*/
@Nonnull
public static MethodResult of( Object... result )
{
return result == null ? empty() : new Immediate( result );
}
/**
* Wait for an event to occur on the computer, suspending the coroutine until it arises. This method is equivalent
* to {@code os.pullEvent()} in Lua.
*
* Normally you'll wish to consume the event using {@link #then(ILuaFunction)}. This can be done slightly more
* easily with {@link #pullEvent(ILuaFunction)}.
*
* If you want to listen to a specific event, it's easier to use {@link #pullEvent(String)} rather than
* running until the desired event is found.
*
* @return The constructed method result. This evaluates to the name of the event that occurred, and any event
* parameters.
* @see #pullEvent(ILuaFunction)
* @see #pullEvent(String)
*/
@Nonnull
public static MethodResult pullEvent()
{
return new OnEvent( false, null );
}
/**
* Wait for the specified event to occur on the computer, suspending the coroutine until it arises. This method is
* equivalent to {@code os.pullEvent(event)} in Lua.
*
* Normally you'll wish to consume the event using {@link #then(ILuaFunction)}. This can be done slightly more
* easily with {@link #pullEvent(String, ILuaFunction)}.
*
* @return The constructed method result. This evaluates to the name of the event that occurred, and any event
* parameters.
* @see #pullEvent(String, ILuaFunction)
* @see #pullEvent()
*/
@Nonnull
public static MethodResult pullEvent( @Nonnull String event )
{
Preconditions.checkNotNull( event, "event cannot be null" );
return new OnEvent( false, event );
}
/**
* Wait for an event to occur on the computer, suspending the coroutine until it arises. This method to
* {@link #pullEvent()} and {@link #then(ILuaFunction)}.
*
* If you want to listen to a specific event, it's easier to use {@link #pullEvent(String, ILuaFunction)} rather
* than running until the desired event is found.
*
* @return The constructed method result. This evaluates to the result of the {@code callback}.
* @see #pullEvent()
* @see #pullEvent(String, ILuaFunction)
*/
@Nonnull
public static MethodResult pullEvent( @Nonnull ILuaFunction callback )
{
Preconditions.checkNotNull( callback, "callback cannot be null" );
return new OnEvent( false, null ).then( callback );
}
/**
* Wait for the specified event to occur on the computer, suspending the coroutine until it arises. This method to
* {@link #pullEvent(String)} and {@link #then(ILuaFunction)}.
*
* @return The constructed method result. This evaluates to the result of the {@code callback}.
* @see #pullEvent(String)
* @see #pullEvent(ILuaFunction)
*/
@Nonnull
public static MethodResult pullEvent( @Nullable String filter, @Nonnull ILuaFunction callback )
{
Preconditions.checkNotNull( callback, "callback cannot be null" );
return new OnEvent( false, filter ).then( callback );
}
/**
* The same as {@link #pullEvent()}, except {@code terminated} events are also passed to the callback, instead of
* throwing an error. Only use this if you want to prevent program termination, which is not recommended.
*
* @return The constructed method result. This evaluates to the name of the event that occurred, and any event
* parameters.
*/
@Nonnull
public static MethodResult pullEventRaw()
{
return new OnEvent( true, null );
}
/**
* The same as {@link #pullEvent(String)}, except {@code terminated} events are also passed to the callback, instead
* of throwing an error. Only use this if you want to prevent program termination, which is not recommended.
*
* @return The constructed method result. This evaluates to the name of the event that occurred, and any event
* parameters.
*/
@Nonnull
public static MethodResult pullEventRaw( @Nonnull String event )
{
return new OnEvent( true, event );
}
/**
* The same as {@link #pullEvent(ILuaFunction)}, except {@code terminated} events are also passed to the callback,
* instead of throwing an error. Only use this if you want to prevent program termination, which is not recommended.
*
* @return The constructed method result. This evaluates to the result of the {@code callback}.
*/
@Nonnull
public static MethodResult pullEventRaw( @Nonnull ILuaFunction callback )
{
Preconditions.checkNotNull( callback, "callback cannot be null" );
return new OnEvent( true, null ).then( callback );
}
/**
* The same as {@link #pullEvent(String, ILuaFunction)}, except {@code terminated} events are also passed to the
* callback, instead of throwing an error. Only use this if you want to prevent program termination, which is not
* recommended.
*
* @return The constructed method result. This evaluates to the result of the {@code callback}.
*/
@Nonnull
public static MethodResult pullEventRaw( @Nullable String filter, @Nonnull ILuaFunction callback )
{
Preconditions.checkNotNull( callback, "callback cannot be null" );
return new OnEvent( true, filter ).then( callback );
}
/**
* Queue a task to be executed on the main server thread at the beginning of next tick, waiting for it to complete.
* This should be used when you need to interact with the world in a thread-safe manner.
*
* @param callback The task to execute on the server thread.
* @return The constructed method result, which evaluates to the result of the {@code callback}.
*/
@Nonnull
public static MethodResult onMainThread( @Nonnull ILuaCallable callback )
{
Preconditions.checkNotNull( callback, "callback cannot be null" );
return new OnMainThread( callback );
}
/**
* Consume the result of this {@link MethodResult} and return another result.
*
* Note this does NOT modify the current method result, rather returning a new (wrapped) one. You must return the
* result of this call if you wish to use it.
*
* @param callback The function which consumes the provided values.
* @return The constructed method result.
*/
@Nonnull
public final MethodResult then( @Nonnull ILuaFunction callback )
{
Preconditions.checkNotNull( callback, "callback cannot be null" );
return new AndThen( this, callback );
}
/**
* Execute a blocking task within a {@link ILuaContext} and return its result.
*
* @param consumer The task to execute with the provided Lua context.
* @return The constructed method result.
* @see #evaluate(ILuaContext)
* @deprecated This should not be used except to interface between the two call systems.
*/
@Deprecated
public static MethodResult withLuaContext( @Nonnull ILuaContextTask consumer )
{
Preconditions.checkNotNull( consumer, "consumer cannot be null" );
return new WithLuaContext( consumer );
}
/**
* Evaluate this result task using {@link ILuaContext} and return its result.
*
* @param context The context to execute with.
* @return The resulting values.
* @see #withLuaContext(ILuaContextTask)
* @deprecated This should not be used except to interface between the two call systems.
*/
@Deprecated
public final Object[] evaluate( @Nonnull ILuaContext context ) throws LuaException, InterruptedException
{
return LuaContextResultEvaluator.evaluate( context, this );
}
public static class Immediate extends MethodResult
{
@Nullable
private final Object[] values;
@Nullable
private Immediate( Object[] values )
{
this.values = values;
}
public Object[] getResult()
{
return values;
}
}
public static class OnEvent extends MethodResult
{
private final boolean raw;
private final String filter;
private OnEvent( boolean raw, String filter )
{
this.raw = raw;
this.filter = filter;
}
public boolean isRaw()
{
return raw;
}
@Nullable
public String getFilter()
{
return filter;
}
}
public static class OnMainThread extends MethodResult
{
private final ILuaCallable task;
public OnMainThread( ILuaCallable task )
{
this.task = task;
}
@Nonnull
public ILuaCallable getTask()
{
return task;
}
}
public static class AndThen extends MethodResult
{
private final MethodResult previous;
private final ILuaFunction callback;
private AndThen( MethodResult previous, ILuaFunction callback )
{
this.previous = previous;
this.callback = callback;
}
@Nonnull
public MethodResult getPrevious()
{
return previous;
}
@Nonnull
public ILuaFunction getCallback()
{
return callback;
}
}
public static class WithLuaContext extends MethodResult
{
private final ILuaContextTask consumer;
private WithLuaContext( ILuaContextTask consumer )
{
this.consumer = consumer;
}
@Nonnull
public ILuaContextTask getConsumer()
{
return consumer;
}
}
}

View File

@ -1,13 +1,15 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2018. 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.api.peripheral;
import dan200.computercraft.api.lua.ICallContext;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -70,10 +72,46 @@ public interface IPeripheral
* InterruptedException will be thrown. This exception must not be caught or
* intercepted, or the computer will leak memory and end up in a broken state.
* @see #getMethodNames
* @deprecated Use {@link #callMethod(IComputerAccess, ICallContext, int, Object[])} instead.
*/
@Nullable
@Deprecated
Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException;
/**
* This is called when a lua program on an attached computer calls {@code peripheral.call()} with
* one of the methods exposed by {@link #getMethodNames()}.
*
* Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe
* when interacting with Minecraft objects.
*
* @param computer The interface to the computer that is making the call. Remember that multiple
* computers can be attached to a peripheral at once.
* @param context The context of the current call.
* @param method An integer identifying which of the methods from getMethodNames() the computercraft
* wishes to call. The integer indicates the index into the getMethodNames() table
* that corresponds to the string passed into peripheral.call()
* @param arguments An array of objects, representing the arguments passed into {@code peripheral.call()}.<br>
* Lua values of type "string" will be represented by Object type String.<br>
* Lua values of type "number" will be represented by Object type Double.<br>
* Lua values of type "boolean" will be represented by Object type Boolean.<br>
* Lua values of type "table" will be represented by Object type Map.<br>
* Lua values of any other type will be represented by a null object.<br>
* This array will be empty if no arguments are passed.
* @return The result of calling this method. Use {@link MethodResult#empty()} to return nothing or
* {@link MethodResult#of(Object...)} to return several values.
* @throws LuaException If you throw any exception from this function, a lua error will be raised with the
* same message as your exception. Use this to throw appropriate errors if the wrong
* arguments are supplied to your method.
* @see #getMethodNames
*/
@Nonnull
@SuppressWarnings({ "deprecation" })
default MethodResult callMethod( @Nonnull IComputerAccess computer, @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException
{
return MethodResult.withLuaContext( lua -> callMethod( computer, lua, method, arguments ) );
}
/**
* Is called when canAttachToSide has returned true, and a computer is attaching to the peripheral.
*

View File

@ -8,6 +8,7 @@
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.inventory.IInventory;
import net.minecraft.nbt.NBTTagCompound;
@ -148,7 +149,7 @@ public interface ITurtleAccess
* Get the inventory of this turtle as an {@link IItemHandlerModifiable}.
*
* @return This turtle's inventory
* @see #getInventory()
* @see #getInventory()
* @see IItemHandlerModifiable
* @see net.minecraftforge.items.CapabilityItemHandler#ITEM_HANDLER_CAPABILITY
*/
@ -229,10 +230,30 @@ public interface ITurtleAccess
* intercepted, or the computer will leak memory and end up in a broken state.
* @see ITurtleCommand
* @see ILuaContext#pullEvent(String)
* @deprecated Use {@link #executeCommand(ITurtleCommand)} instead.
*/
@Nonnull
@Deprecated
Object[] executeCommand( @Nonnull ILuaContext context, @Nonnull ITurtleCommand command ) throws LuaException, InterruptedException;
/**
* Adds a custom command to the turtles command queue. Unlike peripheral methods, these custom commands will be
* executed on the main thread, so are guaranteed to be able to access Minecraft objects safely, and will be queued
* up with the turtles standard movement and tool commands.
*
* An issued command will return an unique integer, which will be supplied as a parameter to a "turtle_response"
* event issued to the turtle after the command has completed. Look at the Lua source code for "rom/apis/turtle" for
* how to build a Lua wrapper around this functionality.
*
* @param command An object which will execute the custom command when its point in the queue is reached
* @return The constructed method result. This evaluates to the result of the provided {@code command}.
* @throws UnsupportedOperationException When attempting to execute a command on the client side.
* @see ITurtleCommand
* @see MethodResult#pullEvent(String)
*/
@Nonnull
MethodResult executeCommand( @Nonnull ITurtleCommand command );
/**
* Start playing a specific animation. This will prevent other turtle commands from executing until
* it is finished.

View File

@ -7,15 +7,11 @@
package dan200.computercraft.core.lua;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.ILuaObject;
import dan200.computercraft.api.lua.ILuaTask;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.*;
import dan200.computercraft.core.apis.ILuaAPI;
import dan200.computercraft.core.computer.Computer;
import dan200.computercraft.core.computer.ITask;
import dan200.computercraft.core.computer.MainThread;
import org.luaj.vm2.*;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.VarArgFunction;
@ -340,9 +336,10 @@ public Varargs invoke( Varargs _args )
Object[] results;
try
{
results = apiObject.callMethod( new ILuaContext() {
ILuaContext context = new ILuaContext() {
@Nonnull
@Override
@Deprecated
public Object[] pullEvent( String filter ) throws LuaException, InterruptedException
{
Object[] results = pullEventRaw( filter );
@ -352,16 +349,18 @@ public Object[] pullEvent( String filter ) throws LuaException, InterruptedExcep
}
return results;
}
@Nonnull
@Override
@Deprecated
public Object[] pullEventRaw( String filter ) throws InterruptedException
{
return yield( new Object[] { filter } );
}
@Nonnull
@Override
@Deprecated
public Object[] yield( Object[] yieldArgs ) throws InterruptedException
{
try
@ -437,6 +436,7 @@ public void execute()
}
@Override
@Deprecated
public Object[] executeMainThreadTask( @Nonnull final ILuaTask task ) throws LuaException, InterruptedException
{
// Issue task
@ -474,7 +474,10 @@ public Object[] executeMainThreadTask( @Nonnull final ILuaTask task ) throws Lua
}
}
}, method, arguments );
};
// TODO: Replace with custom interpreter
results = apiObject.callMethod( (ICallContext) context, method, arguments ).evaluate( context );
}
catch( InterruptedException e )
{

View File

@ -8,8 +8,7 @@
import com.google.common.base.Objects;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.turtle.*;
import dan200.computercraft.shared.computer.core.ComputerFamily;
@ -739,6 +738,38 @@ public Object[] executeCommand( @Nonnull ILuaContext context, @Nonnull ITurtleCo
}
}
@Nonnull
@Override
public MethodResult executeCommand( @Nonnull ITurtleCommand command )
{
if( getWorld().isRemote )
{
throw new UnsupportedOperationException();
}
// Issue command
int commandID = issueCommand( command );
// Wait for response
return MethodResult.pullEvent( "turtle_response", new ILuaFunction()
{
@Nonnull
@Override
public MethodResult call( Object[] response )
{
if( response.length >= 3 && response[ 1 ] instanceof Number && ((Number) response[ 1 ]).intValue() == commandID
&& response[ 2 ] instanceof Boolean )
{
Object[] returnValues = new Object[ response.length - 2 ];
System.arraycopy( response, 2, returnValues, 0, returnValues.length );
return MethodResult.of( returnValues );
}
return MethodResult.pullEvent( "turtle_response", this );
}
} );
}
@Override
public void playAnimation( @Nonnull TurtleAnimation animation )
{