Another little idea for LuaJ that I like a lot - "named nils". Don't
you hate those non-speaking "attempt to call nil" messages, too? =)
Add to Lua.java:
/** disable for more speed */
public static final boolean useNamedNil = true;
public static LuaValue namedNil(LuaValue name) {
return useNamedNil ? new NamedNil(name) : NIL;
}
Create this class:
package org.luaj.vm2;
public class NamedNil extends LuaNil {
public LuaValue name;
public NamedNil(LuaValue name) {
this.name = name;
}
}
Change 2 functions in LuaTable:
public LuaValue rawget( LuaValue key ) {
if ( key.isinttype() ) {
int ikey = key.toint();
if ( ikey>0 && ikey<=array.length ) {
LuaValue v = m_metatable == null
? array[ikey-1] : m_metatable.arrayget(array, ikey-1);
return v != null ? v : Lua.namedNil(key);
}
}
return hashget( key );
}
protected LuaValue hashget(LuaValue key) {
if ( hashEntries > 0 ) {
for ( Slot slot = hash[ hashSlot(key) ]; slot != null; slot = slot.rest() ) {
StrongSlot foundSlot;
if ( ( foundSlot = slot.find(key) ) != null ) {
return foundSlot.value();
}
}
}
return Lua.namedNil(key);
}
Another little idea for LuaJ that I like a lot - "named nils". Don't
you hate those non-speaking "attempt to call nil" messages, too? =)
Add to Lua.java:
Create this class:
Change 2 functions in LuaTable:
Change LuaValue.checkmetatag:
And, voila, when running scripts with undefined functions (let's say
loadURL is undefined):
...you get very nice error messages like this:
Performance hit should be small when turned on - and zero when turned
off and HotSpot-optimized.
Cheers,
Stefan
Last edit: Stefan Reich 2015-03-22
Thank you for this help.
But in our environnement we needed to modify LuaScriptEngine BindingsMetatable class and put this ack to show which function is not defined :