mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-07-16 17:02:52 +00:00

When creating a peripheral or custom Lua object, one must implement two methods: - getMethodNames(): String[] - Returns the name of the methods - callMethod(int, ...): Object[] - Invokes the method using an index in the above array. This has a couple of problems: - It's somewhat unwieldy to use - you need to keep track of array indices, which leads to ugly code. - Functions which yield (for instance, those which run on the main thread) are blocking. This means we need to spawn new threads for each CC-side yield. We replace this system with a few changes: - @LuaFunction annotation: One may annotate a public instance method with this annotation. This then exposes a peripheral/lua object method. Furthermore, this method can accept and return a variety of types, which often makes functions cleaner (e.g. can return an int rather than an Object[], and specify and int argument rather than Object[]). - MethodResult: Instead of returning an Object[] and having blocking yields, functions return a MethodResult. This either contains an immediate return, or an instruction to yield with some continuation to resume with. MethodResult is then interpreted by the Lua runtime (i.e. Cobalt), rather than our weird bodgey hacks before. This means we no longer spawn new threads when yielding within CC. - Methods accept IArguments instead of a raw Object array. This has a few benefits: - Consistent argument handling - people no longer need to use ArgumentHelper (as it doesn't exist!), or even be aware of its existence - you're rather forced into using it. - More efficient code in some cases. We provide a Cobalt-specific implementation of IArguments, which avoids the boxing/unboxing when handling numbers and binary strings.
68 lines
2.2 KiB
Java
68 lines
2.2 KiB
Java
/*
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
* Copyright Daniel Ratcliffe, 2011-2020. Do not distribute without permission.
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
*/
|
|
package dan200.computercraft.core.apis;
|
|
|
|
import dan200.computercraft.api.lua.*;
|
|
import dan200.computercraft.core.asm.LuaMethod;
|
|
import dan200.computercraft.core.asm.NamedMethod;
|
|
|
|
import javax.annotation.Nonnull;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
|
|
public class ObjectWrapper implements ILuaContext
|
|
{
|
|
private final Object object;
|
|
private final Map<String, LuaMethod> methodMap;
|
|
|
|
public ObjectWrapper( Object object )
|
|
{
|
|
this.object = object;
|
|
String[] dynamicMethods = object instanceof IDynamicLuaObject
|
|
? Objects.requireNonNull( ((IDynamicLuaObject) object).getMethodNames(), "Methods cannot be null" )
|
|
: LuaMethod.EMPTY_METHODS;
|
|
|
|
List<NamedMethod<LuaMethod>> methods = LuaMethod.GENERATOR.getMethods( object.getClass() );
|
|
|
|
Map<String, LuaMethod> methodMap = this.methodMap = new HashMap<>( methods.size() + dynamicMethods.length );
|
|
for( int i = 0; i < dynamicMethods.length; i++ )
|
|
{
|
|
methodMap.put( dynamicMethods[i], LuaMethod.DYNAMIC.get( i ) );
|
|
}
|
|
for( NamedMethod<LuaMethod> method : methods )
|
|
{
|
|
methodMap.put( method.getName(), method.getMethod() );
|
|
}
|
|
}
|
|
|
|
public Object[] call( String name, Object... args ) throws LuaException
|
|
{
|
|
LuaMethod method = methodMap.get( name );
|
|
if( method == null ) throw new IllegalStateException( "No such method '" + name + "'" );
|
|
|
|
return method.apply( object, this, new ObjectArguments( args ) ).getResult();
|
|
}
|
|
|
|
@SuppressWarnings( "unchecked" )
|
|
public <T> T callOf( String name, Object... args ) throws LuaException
|
|
{
|
|
return (T) call( name, args )[0];
|
|
}
|
|
|
|
public <T> T callOf( Class<T> klass, String name, Object... args ) throws LuaException
|
|
{
|
|
return klass.cast( call( name, args )[0] );
|
|
}
|
|
|
|
@Override
|
|
public long issueMainThreadTask( @Nonnull ILuaTask task )
|
|
{
|
|
throw new IllegalStateException( "Method should never queue events" );
|
|
}
|
|
}
|