Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide console API in shell. #1103

Closed
wants to merge 10 commits into from
108 changes: 71 additions & 37 deletions src/org/mozilla/javascript/NativeConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.mozilla.javascript;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -216,61 +217,94 @@ public static String format(Context cx, Scriptable scope, Object[] args) {
while (matcher.find()) {
String placeHolder = matcher.group();
String replaceArg;
switch (placeHolder) {
case "%%":
replaceArg = "%";
break;

case "%s":
replaceArg = ScriptRuntime.toString(args, argIndex);
break;

case "%d":
case "%i":
double number = ScriptRuntime.toNumber(args, argIndex);
if (Double.isInfinite(number) || Double.isNaN(number)) {
replaceArg = "0";
} else {
replaceArg = String.valueOf(((Double) number).intValue());
}
break;

case "%f":
replaceArg = String.valueOf(ScriptRuntime.toNumber(args, argIndex));
break;

case "%o":
case "%O":
replaceArg = formatObj(cx, scope, args, argIndex);
break;

default:
replaceArg = "";
break;
if (placeHolder.equals("%%")) {
replaceArg = "%";
} else if (argIndex >= args.length) {
replaceArg = placeHolder;
argIndex++;
} else {
Object val = args[argIndex];
switch (placeHolder) {
case "%s":
replaceArg = ScriptRuntime.toString(val);
break;

case "%d":
case "%i":
replaceArg = formatInt(val);
break;

case "%f":
replaceArg = formatFloat(val);
break;

case "%o":
case "%O":
replaceArg = formatObj(cx, scope, args, argIndex);
break;

default:
replaceArg = "";
break;
}
argIndex++;
}

argIndex++;
matcher.appendReplacement(buffer, Matcher.quoteReplacement(replaceArg));
}
matcher.appendTail(buffer);

return buffer.toString();
}

private static String formatObj(Context cx, Scriptable scope, Object[] args, int argIndex) {
if (argIndex >= args.length) {
return "";
private static String formatFloat(Object val) {
if (val instanceof BigInteger || val instanceof Symbol) {
return ScriptRuntime.NaNobj.toString();
}

return String.valueOf(ScriptRuntime.toNumber(val));
}

private static String formatInt(Object val) {
if (val instanceof BigInteger) {
return ScriptRuntime.bigIntToString((BigInteger) val, 10) + "n";
}

if (val instanceof Symbol) {
return ScriptRuntime.NaNobj.toString();
}

double number = ScriptRuntime.toNumber(val);

if (Double.isInfinite(number) || Double.isNaN(number)) {
return ScriptRuntime.toString(number);
}

return String.valueOf((int) number);
MaxisTekfield marked this conversation as resolved.
Show resolved Hide resolved
}

private static String formatObj(Context cx, Scriptable scope, Object[] args, int argIndex) {
Object arg = args[argIndex];
if (arg == null) {
return "null";
}
if (arg == Undefined.instance || arg == Undefined.SCRIPTABLE_UNDEFINED) {
return "undefined";

if (Undefined.isUndefined(arg)) {
return Undefined.SCRIPTABLE_UNDEFINED.toString();
}

return ScriptRuntime.toString(NativeJSON.stringify(cx, scope, arg, null, null));
try {
Object stringify = NativeJSON.stringify(cx, scope, arg, null, null);
return ScriptRuntime.toString(stringify);
} catch (EcmaError e) {
if ("TypeError".equals(e.getName())) {
// Fall back to use ScriptRuntime.toString() in some case such as
// NativeJSON.stringify not support BigInt yet.
return ScriptRuntime.toString(arg);
}
throw e;
}
}

private void jsAssert(Context cx, Scriptable scope, Object[] args) {
Expand Down