From 53477fd3a185ea9243fccae8af6e724a25d3e1a5 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Sun, 17 May 2020 16:58:19 +0100 Subject: [PATCH] Fix several AIOB exceptions Closes #450 --- .../computercraft/core/asm/IntCache.java | 4 +-- .../computercraft/core/asm/IntCacheTest.java | 27 +++++++++++++++ .../computercraft/core/asm/MethodTest.java | 34 +++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 src/test/java/dan200/computercraft/core/asm/IntCacheTest.java diff --git a/src/main/java/dan200/computercraft/core/asm/IntCache.java b/src/main/java/dan200/computercraft/core/asm/IntCache.java index d50f54b2a..3652a530d 100644 --- a/src/main/java/dan200/computercraft/core/asm/IntCache.java +++ b/src/main/java/dan200/computercraft/core/asm/IntCache.java @@ -24,7 +24,7 @@ public final class IntCache { 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 final class IntCache 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; diff --git a/src/test/java/dan200/computercraft/core/asm/IntCacheTest.java b/src/test/java/dan200/computercraft/core/asm/IntCacheTest.java new file mode 100644 index 000000000..94f17da65 --- /dev/null +++ b/src/test/java/dan200/computercraft/core/asm/IntCacheTest.java @@ -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 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 ) ); + } +} diff --git a/src/test/java/dan200/computercraft/core/asm/MethodTest.java b/src/test/java/dan200/computercraft/core/asm/MethodTest.java index 58be91ecd..cd2e780a4 100644 --- a/src/test/java/dan200/computercraft/core/asm/MethodTest.java +++ b/src/test/java/dan200/computercraft/core/asm/MethodTest.java @@ -78,6 +78,15 @@ public class MethodTest ); } + @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 class MethodTest 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" }; + } + } }