Improved error messages.

This commit is contained in:
Enyby
2018-09-16 15:52:14 +03:00
committed by GitHub
parent 9b77084109
commit d0ed5b80e5

View File

@@ -266,8 +266,8 @@ public class LuaTable extends LuaValue implements Metatable {
/** caller must ensure key is not nil */ /** caller must ensure key is not nil */
public void set( LuaValue key, LuaValue value ) { public void set( LuaValue key, LuaValue value ) {
if (!key.isvalidkey() && !metatag(NEWINDEX).isfunction()) if (key == null || !key.isvalidkey() && !metatag(NEWINDEX).isfunction())
typerror("table index"); throw new LuaError("value ('" + key + "') can not be used as a table index");
if ( m_metatable==null || ! rawget(key).isnil() || ! settable(this,key,value) ) if ( m_metatable==null || ! rawget(key).isnil() || ! settable(this,key,value) )
rawset(key, value); rawset(key, value);
} }
@@ -347,7 +347,12 @@ public class LuaTable extends LuaValue implements Metatable {
} }
public int length() { public int length() {
return m_metatable != null? len().toint(): rawlen(); if (m_metatable != null) {
LuaValue len = len();
if (!len.isint()) throw new LuaError("table length is not an integer: " + len);
return len.toint();
}
return rawlen();
} }
public LuaValue len() { public LuaValue len() {
@@ -390,7 +395,7 @@ public class LuaTable extends LuaValue implements Metatable {
} }
} }
if ( hash.length == 0 ) if ( hash.length == 0 )
error( "invalid key to 'next'" ); error( "invalid key to 'next' 1: " + key );
i = hashSlot( key ); i = hashSlot( key );
boolean found = false; boolean found = false;
for ( Slot slot = hash[i]; slot != null; slot = slot.rest() ) { for ( Slot slot = hash[i]; slot != null; slot = slot.rest() ) {
@@ -404,7 +409,7 @@ public class LuaTable extends LuaValue implements Metatable {
} }
} }
if ( !found ) { if ( !found ) {
error( "invalid key to 'next'" ); error( "invalid key to 'next' 2: " + key );
} }
i += 1+array.length; i += 1+array.length;
} }
@@ -780,6 +785,7 @@ public class LuaTable extends LuaValue implements Metatable {
* @param comparator {@link LuaValue} to be called to compare elements. * @param comparator {@link LuaValue} to be called to compare elements.
*/ */
public void sort(LuaValue comparator) { public void sort(LuaValue comparator) {
if (len().tolong() >= (long)Integer.MAX_VALUE) throw new LuaError("array too big: " + len().tolong());
if (m_metatable != null && m_metatable.useWeakValues()) { if (m_metatable != null && m_metatable.useWeakValues()) {
dropWeakArrayValues(); dropWeakArrayValues();
} }