docs: static int getConversionWeight(Object fromObj, Class<?> to) {
          int fromCode = getJSTypeCode(fromObj);
  
          switch (fromCode) {
  
          case JSTYPE_UNDEFINED:
              if (to == ScriptRuntime.StringClass ||
                  to == ScriptRuntime.ObjectClass) {
                  return 1;
              }
              break;
  
          case JSTYPE_NULL:
              if (!to.isPrimitive()) {
                  return 1;
              }
              break;
  
          case JSTYPE_BOOLEAN:
              // "boolean" is #1
              if (to == Boolean.TYPE) {
                  return 1;
              }
              else if (to == ScriptRuntime.BooleanClass) {
                  return 2;
              }
              else if (to == ScriptRuntime.ObjectClass) {
                  return 3;
              }
              else if (to == ScriptRuntime.StringClass) {
                  return 4;
              }
              break;
  
          case JSTYPE_NUMBER:
              if (to.isPrimitive()) {
                  if (to == Double.TYPE) {
                      return 1;
                  }
                  else if (to != Boolean.TYPE) {
                      return 1 + getSizeRank(to);
                  }
              }
              else {
                  if (to == ScriptRuntime.StringClass) {
                      // native numbers are #1-8
                      return 9;
                  }
                  else if (to == ScriptRuntime.ObjectClass) {
                      return 10;
                  }
                  else if (ScriptRuntime.NumberClass.isAssignableFrom(to)) {
                      // "double" is #1
                      return 2;
                  }
              }
              break;
  
          case JSTYPE_STRING:
              if (to == ScriptRuntime.StringClass) {
                  return 1;
              }
              else if (to.isInstance(fromObj)) {
                  return 2;
              }
              else if (to.isPrimitive()) {
                  if (to == Character.TYPE) {
                      return 3;
                  } else if (to != Boolean.TYPE) {
                      return 4;
                  }
              }
              break;
  
          case JSTYPE_JAVA_CLASS:
              if (to == ScriptRuntime.ClassClass) {
                  return 1;
              }
              else if (to == ScriptRuntime.ObjectClass) {
                  return 3;
              }
              else if (to == ScriptRuntime.StringClass) {
                  return 4;
              }
              break;
  
          case JSTYPE_JAVA_OBJECT:
          case JSTYPE_JAVA_ARRAY:
              Object javaObj = fromObj;
              if (javaObj instanceof Wrapper) {
                  javaObj = ((Wrapper)javaObj).unwrap();
              }
              if (to.isInstance(javaObj)) {
                  return CONVERSION_NONTRIVIAL;
              }
              if (to == ScriptRuntime.StringClass) {
                  return 2;
              }
              else if (to.isPrimitive() && to != Boolean.TYPE) {
                  return (fromCode == JSTYPE_JAVA_ARRAY)
                         ? CONVERSION_NONE : 2 + getSizeRank(to);
              }
              break;
  
          case JSTYPE_OBJECT:
              // Other objects takes #1-#3 spots
              if (to != ScriptRuntime.ObjectClass && to.isInstance(fromObj)) {
                  // No conversion required, but don't apply for java.lang.Object
                  return 1;
              }
              if (to.isArray()) {
                  if (fromObj instanceof NativeArray) {
                      // This is a native array conversion to a java array
                      // Array conversions are all equal, and preferable to object
                      // and string conversion, per LC3.
                      return 2;
                  }
              }
              else if (to == ScriptRuntime.ObjectClass) {
                  return 3;
              }
              else if (to == ScriptRuntime.StringClass) {
                  return 4;
              }
              else if (to == ScriptRuntime.DateClass) {
                  if (fromObj instanceof NativeDate) {
                      // This is a native date to java date conversion
                      return 1;
                  }
              }
              else if (to.isInterface()) {
  
                  if (fromObj instanceof NativeFunction) {
                      // See comments in createInterfaceAdapter
                      return 1;
                  }
                  if (fromObj instanceof NativeObject) {
                      return 2;
                  }
                  return 12;
              }
              else if (to.isPrimitive() && to != Boolean.TYPE) {
                  return 4 + getSizeRank(to);
              }
              break;
          }
  
          return CONVERSION_NONE;
      }