Fix several AIOB exceptions

Closes #450
This commit is contained in:
SquidDev 2020-05-17 16:58:19 +01:00
parent 87aa839b60
commit 53477fd3a1
3 changed files with 63 additions and 2 deletions

View File

@ -24,7 +24,7 @@ public T get( int index )
{
if( index < 0 ) throw new IllegalArgumentException( "index < 0" );
if( index <= cache.length )
if( index < cache.length )
{
T current = (T) cache[index];
if( current != null ) return current;
@ -32,7 +32,7 @@ public T get( int index )
synchronized( this )
{
if( index > cache.length ) cache = Arrays.copyOf( cache, Math.max( cache.length * 2, index ) );
if( index >= cache.length ) cache = Arrays.copyOf( cache, Math.max( cache.length * 2, index + 1 ) );
T current = (T) cache[index];
if( current == null ) cache[index] = current = factory.apply( index );
return current;

View File

@ -0,0 +1,27 @@
/*
* 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.asm;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class IntCacheTest
{
@Test
public void testCache()
{
IntCache<Object> c = new IntCache<>( i -> new Object() );
assertEquals( c.get( 0 ), c.get( 0 ) );
}
@Test
public void testMassive()
{
assertEquals( 40, new IntCache<>( i -> i ).get( 40 ) );
}
}

View File

@ -78,6 +78,15 @@ public void testPeripheralThrow()
);
}
@Test
public void testMany()
{
ComputerBootstrap.run(
"assert(many.method_0)\n" +
"assert(many.method_39)",
x -> x.addApi( new ManyMethods() ), 50 );
}
public static class MainThread implements ILuaAPI, IPeripheral
{
public final String thread = Thread.currentThread().getName();
@ -205,4 +214,29 @@ public boolean equals( @Nullable IPeripheral other )
return this == other;
}
}
public static class ManyMethods implements IDynamicLuaObject, ILuaAPI
{
@Nonnull
@Override
public String[] getMethodNames()
{
String[] methods = new String[40];
for( int i = 0; i < methods.length; i++ ) methods[i] = "method_" + i;
return methods;
}
@Nonnull
@Override
public MethodResult callMethod( @Nonnull ILuaContext context, int method, @Nonnull IArguments arguments ) throws LuaException
{
return MethodResult.of();
}
@Override
public String[] getNames()
{
return new String[] { "many" };
}
}
}