mozilla / rhino

Rhino is an open-source implementation of JavaScript written entirely in Java
https://rhino.github.io
Other
4.18k stars 851 forks source link

Custom .toSting behavior for (overloaded) Java methods in the shell (for easier development/debugging) #1554

Open tuchida opened 3 months ago

tuchida commented 3 months ago

ref. https://github.com/mozilla/rhino/pull/1537#issuecomment-2260194670 Debugging with JavaScript toString is subject to the constraints of the ECMA 262 specification. Therefore, it is difficult to debug JavaObject/JavaClass/JavaMethod, etc. without violating the specification. With console.log, the implementation can decide on a useful format. https://console.spec.whatwg.org/#printer

This is a prototype. If it looks good to you, brush up and make a Pull Requst. https://github.com/mozilla/rhino/compare/master...tuchida:debugging-java-using-console?expand=1

$ ./gradlew run -q --console=plain --args="-version 200"
Rhino 1.7.16-SNAPSHOT
js> console.log(java.util.Arrays.copyOf)
INFO [JavaMethod copyOf]
  float[] copyOf(float[],int)
  char[] copyOf(char[],int)
  long[] copyOf(long[],int)
  byte[] copyOf(byte[],int)
  int[] copyOf(int[],int)
  double[] copyOf(double[],int)
  java.lang.Object[] copyOf(java.lang.Object[],int)
  boolean[] copyOf(boolean[],int)
  short[] copyOf(short[],int)
  java.lang.Object[] copyOf(java.lang.Object[],int,java.lang.Class)

js> console.log(java.math.BigInteger)
INFO [JavaClass java.math.BigInteger]
  java.math.BigInteger(int,byte[])
  java.math.BigInteger(java.lang.String,int)
  java.math.BigInteger(byte[],int,int)
  java.math.BigInteger(byte[])
  java.math.BigInteger(int,byte[],int,int)
  java.math.BigInteger(int,int,java.util.Random)
  java.math.BigInteger(int,java.util.Random)
  java.math.BigInteger(java.lang.String)

js> console.log(new java.math.BigInteger(0))
INFO [JavaObject java.math.BigInteger]
  0
js> console.log(new java.util.HashMap({ a: 1, b: 2 }))
INFO [JavaMap java.util.HashMap]
  {a=1.0, b=2}
p-bakker commented 2 months ago

Also see #1420, which is another case that requires custom toString handling in the shell