Bug toClass(1) returns none

Veracity

Developer
Staff member
Code:
> ash to_class(1)

Returned: Seal Clubber
primestat => Muscle
path => none

> js toClass(1)

Returned: none
primestat => none
Unexpected error, debug log printed.

DEBUG log:

Code:
Unexpected error, debug log printed.
class java.lang.NullPointerException: Cannot invoke "net.sourceforge.kolmafia.textui.AshRuntime.runtimeException(String)" because "interpreter" is null
java.lang.NullPointerException: Cannot invoke "net.sourceforge.kolmafia.textui.AshRuntime.runtimeException(String)" because "interpreter" is null
    at net.sourceforge.kolmafia.textui.parsetree.ProxyRecordValue.aref(ProxyRecordValue.java:64)
    at net.sourceforge.kolmafia.textui.parsetree.ProxyRecordValue.aref(ProxyRecordValue.java:49)
    at net.sourceforge.kolmafia.textui.parsetree.CompositeValue.aref(CompositeValue.java:22)
    at net.sourceforge.kolmafia.textui.RuntimeLibrary.dump(RuntimeLibrary.java:3065)
    at net.sourceforge.kolmafia.textui.RuntimeLibrary.dump(RuntimeLibrary.java:3055)
    at net.sourceforge.kolmafia.textui.command.JavaScriptCommand.run(JavaScriptCommand.java:29)
    at net.sourceforge.kolmafia.KoLmafiaCLI.doExecuteCommand(KoLmafiaCLI.java:453)
    at net.sourceforge.kolmafia.KoLmafiaCLI.executeCommand(KoLmafiaCLI.java:419)
    at net.sourceforge.kolmafia.KoLmafiaCLI.executeLine(KoLmafiaCLI.java:338)
    at net.sourceforge.kolmafia.KoLmafiaCLI.executeLine(KoLmafiaCLI.java:225)
    at net.sourceforge.kolmafia.swingui.CommandDisplayFrame$CommandQueueHandler.handleQueue(CommandDisplayFrame.java:139)
    at net.sourceforge.kolmafia.swingui.CommandDisplayFrame$CommandQueueHandler.run(CommandDisplayFrame.java:116)
 

heeheehee

Developer
Staff member
I think that debug log is a red herring, in that it was triggered when trying to throw a different exception.

Custom build tells me:
Unable to invoke attribute getter: java.lang.NullPointerException: Cannot invoke "net.sourceforge.kolmafia.AscensionClass.getPath()" because the return value of "net.sourceforge.kolmafia.textui.parsetree.ProxyRecordValue$ClassProxy.getAscensionClass()" is null
 

heeheehee

Developer
Staff member
Looks like JS thinks that 1 is a float, while 2 is considered to be an int.

Code:
 > js toClass(toInt(1))
Returned: Seal Clubber
primestat => Muscle
path => none
 

heeheehee

Developer
Staff member
Note that this is a similar bug to the one reported here and dealt with in https://github.com/kolmafia/kolmafia/pull/96.

That said: one confounding problem (as Veracity discovered) seems to be that within a script, arguments get coerced (using special rules), while when run directly from the CLI, they do not. You can reproduce that debug log in ASH via `ash to_class(1.0)`. There's another issue where we're not catching the exception at the right level in ASH, e.g. `ash to_class("not a real class")` -- that shouldn't generate a debug log, either.

The problem you reported is that `0` and `1` in JS is converted into a double (which to_class then treats as a string), while '2' etc are converted into longs. https://github.com/mozilla/rhino/issues/350 seems to discuss Rhino handling this specific behavior (apparently -1, 0, and 1 are hardcoded blocks in pushNumberAsObject).

I don't have time to poke at this right now, but if anyone wants to submit a PR: the simple immediate fix is to broaden the check in RuntimeLibrary#to_class that currently only checks value.getType().equals(DataTypes.INT_TYPE) to also match FLOAT_TYPE. I think the other problems are also worth fixing at some point.
 
Top