Skip to content

Commit

Permalink
Replace error name strings used for constructing EcmaError with value…
Browse files Browse the repository at this point in the history
…s of NativeErrors enum
  • Loading branch information
sainaen committed Apr 25, 2015
1 parent fd699d1 commit 36cd306
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 76 deletions.
7 changes: 3 additions & 4 deletions src/org/mozilla/javascript/DefaultErrorReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ public void error(String message, String sourceURI, int line,
// Assume error message strings that start with "TypeError: "
// should become TypeError exceptions. A bit of a hack, but we
// don't want to change the ErrorReporter interface.
String error = "SyntaxError";
final String TYPE_ERROR_NAME = "TypeError";
TopLevel.NativeErrors error = TopLevel.NativeErrors.SyntaxError;
final String DELIMETER = ": ";
final String prefix = TYPE_ERROR_NAME + DELIMETER;
final String prefix = TopLevel.NativeErrors.TypeError.name() + DELIMETER;
if (message.startsWith(prefix)) {
error = TYPE_ERROR_NAME;
error = TopLevel.NativeErrors.TypeError;
message = message.substring(prefix.length());
}
throw ScriptRuntime.constructError(error, message, sourceURI,
Expand Down
6 changes: 3 additions & 3 deletions src/org/mozilla/javascript/EcmaError.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public class EcmaError extends RhinoException
* @param lineSource the source of the line containing the error (may be
* null if unknown)
*/
EcmaError(String errorName, String errorMessage,
EcmaError(TopLevel.NativeErrors errorName, String errorMessage,
String sourceName, int lineNumber,
String lineSource, int columnNumber)
{
recordErrorOrigin(sourceName, lineNumber, lineSource, columnNumber);
this.errorName = errorName;
this.errorName = errorName.name();
this.errorMessage = errorMessage;
}

Expand All @@ -49,7 +49,7 @@ public class EcmaError extends RhinoException
public EcmaError(Scriptable nativeError, String sourceName,
int lineNumber, int columnNumber, String lineSource)
{
this("InternalError", ScriptRuntime.toString(nativeError),
this(TopLevel.NativeErrors.InternalError, ScriptRuntime.toString(nativeError),
sourceName, lineNumber, lineSource, columnNumber);
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/IdScriptableObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ protected void setInstanceIdValue(int id, Object value)
* @param attr the new attribute bitset
*/
protected void setInstanceIdAttributes(int id, int attr) {
throw ScriptRuntime.constructError("InternalError",
throw ScriptRuntime.constructError(TopLevel.NativeErrors.InternalError,
"Changing attributes not supported for " + getClassName()
+ " " + getInstanceIdName(id) + " property");
}
Expand Down
4 changes: 2 additions & 2 deletions src/org/mozilla/javascript/NativeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ private static Object jsConstructor(Context cx, Scriptable scope,
long len = ScriptRuntime.toUint32(arg0);
if (len != ((Number)arg0).doubleValue()) {
String msg = ScriptRuntime.getMessage0("msg.arraylength.bad");
throw ScriptRuntime.constructError("RangeError", msg);
throw ScriptRuntime.constructError(TopLevel.NativeErrors.RangeError, msg);
}
return new NativeArray(len);
}
Expand Down Expand Up @@ -656,7 +656,7 @@ private void setLength(Object val) {
long longVal = ScriptRuntime.toUint32(d);
if (longVal != d) {
String msg = ScriptRuntime.getMessage0("msg.arraylength.bad");
throw ScriptRuntime.constructError("RangeError", msg);
throw ScriptRuntime.constructError(TopLevel.NativeErrors.RangeError, msg);
}

if (denseOnly) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/NativeDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
return js_toISOString(t);
}
String msg = ScriptRuntime.getMessage0("msg.invalid.date");
throw ScriptRuntime.constructError("RangeError", msg);
throw ScriptRuntime.constructError(TopLevel.NativeErrors.RangeError, msg);

default: throw new IllegalArgumentException(String.valueOf(id));
}
Expand Down
10 changes: 5 additions & 5 deletions src/org/mozilla/javascript/NativeGlobal.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,12 @@ static boolean isEvalFunction(Object functionObj)
}

/**
* @deprecated Use {@link ScriptRuntime#constructError(String,String)}
* @deprecated Use {@link ScriptRuntime#constructError(TopLevel.NativeErrors,String)}
* instead.
*/
@Deprecated
public static EcmaError constructError(Context cx,
String error,
TopLevel.NativeErrors error,
String message,
Scriptable scope)
{
Expand All @@ -515,12 +515,12 @@ public static EcmaError constructError(Context cx,

/**
* @deprecated Use
* {@link ScriptRuntime#constructError(String,String,String,int,String,int)}
* {@link ScriptRuntime#constructError(TopLevel.NativeErrors, String, String, int, String, int)}
* instead.
*/
@Deprecated
public static EcmaError constructError(Context cx,
String error,
TopLevel.NativeErrors error,
String message,
Scriptable scope,
String sourceName,
Expand Down Expand Up @@ -722,7 +722,7 @@ private static boolean encodeUnescaped(char c, boolean fullUri) {
}

private static EcmaError uriError() {
return ScriptRuntime.constructError("URIError",
return ScriptRuntime.constructError(TopLevel.NativeErrors.URIError,
ScriptRuntime.getMessage0("msg.bad.uri"));
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/NativeJSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private static Object parse(Context cx, Scriptable scope, String jtext) {
try {
return new JsonParser(cx, scope).parseValue(jtext);
} catch (JsonParser.ParseException ex) {
throw ScriptRuntime.constructError("SyntaxError", ex.getMessage());
throw ScriptRuntime.constructError(TopLevel.NativeErrors.SyntaxError, ex.getMessage());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/NativeNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private static String num_to(double val,
if (p < precisionMin || p > MAX_PRECISION) {
String msg = ScriptRuntime.getMessage1(
"msg.bad.precision", ScriptRuntime.toString(args[0]));
throw ScriptRuntime.constructError("RangeError", msg);
throw ScriptRuntime.constructError(TopLevel.NativeErrors.RangeError, msg);
}
precision = ScriptRuntime.toInt32(p);
}
Expand Down
27 changes: 14 additions & 13 deletions src/org/mozilla/javascript/ScriptRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Locale;
import java.util.ResourceBundle;

import org.mozilla.javascript.TopLevel.NativeErrors;
import org.mozilla.javascript.ast.FunctionNode;
import org.mozilla.javascript.v8dtoa.DoubleConversion;
import org.mozilla.javascript.v8dtoa.FastDtoa;
Expand Down Expand Up @@ -1127,7 +1128,7 @@ public static Scriptable newBuiltinObject(Context cx, Scriptable scope,
}

static Scriptable newNativeError(Context cx, Scriptable scope,
TopLevel.NativeErrors type, Object[] args)
NativeErrors type, Object[] args)
{
scope = ScriptableObject.getTopLevelScope(scope);
Function ctor = TopLevel.getNativeErrorCtor(cx, scope, type);
Expand Down Expand Up @@ -2052,7 +2053,7 @@ public static Object strictSetName(Scriptable bound, Object value,
} else {
// See ES5 8.7.2
String msg = "Assignment to undefined \"" + id + "\" in strict mode";
throw constructError("ReferenceError", msg);
throw constructError(NativeErrors.ReferenceError, msg);
}
}

Expand Down Expand Up @@ -2485,7 +2486,7 @@ public static Ref callRef(Callable function, Scriptable thisObj,
// No runtime support for now
String msg = getMessage1("msg.no.ref.from.function",
toString(function));
throw constructError("ReferenceError", msg);
throw constructError(NativeErrors.ReferenceError, msg);
}

/**
Expand Down Expand Up @@ -3429,33 +3430,33 @@ public static Scriptable newCatchScope(Throwable t,
}

RhinoException re;
TopLevel.NativeErrors type;
NativeErrors type;
String errorMsg;
Throwable javaException = null;

if (t instanceof EcmaError) {
EcmaError ee = (EcmaError)t;
re = ee;
type = TopLevel.NativeErrors.valueOf(ee.getName());
type = NativeErrors.valueOf(ee.getName());
errorMsg = ee.getErrorMessage();
} else if (t instanceof WrappedException) {
WrappedException we = (WrappedException)t;
re = we;
javaException = we.getWrappedException();
type = TopLevel.NativeErrors.JavaException;
type = NativeErrors.JavaException;
errorMsg = javaException.getClass().getName()
+": "+javaException.getMessage();
} else if (t instanceof EvaluatorException) {
// Pure evaluator exception, nor WrappedException instance
EvaluatorException ee = (EvaluatorException)t;
re = ee;
type = TopLevel.NativeErrors.InternalError;
type = NativeErrors.InternalError;
errorMsg = ee.getMessage();
} else if (cx.hasFeature(Context.FEATURE_ENHANCED_JAVA_ACCESS)) {
// With FEATURE_ENHANCED_JAVA_ACCESS, scripts can catch
// all exception types
re = new WrappedException(t);
type = TopLevel.NativeErrors.JavaException;
type = NativeErrors.JavaException;
errorMsg = t.toString();
} else {
// Script can catch only instances of JavaScriptException,
Expand Down Expand Up @@ -3920,14 +3921,14 @@ public String getMessage(String messageId, Object[] arguments) {
}
}

public static EcmaError constructError(String error, String message)
public static EcmaError constructError(NativeErrors error, String message)
{
int[] linep = new int[1];
String filename = Context.getSourcePositionFromStack(linep);
return constructError(error, message, filename, linep[0], null, 0);
}

public static EcmaError constructError(String error,
public static EcmaError constructError(NativeErrors error,
String message,
int lineNumberDelta)
{
Expand All @@ -3939,7 +3940,7 @@ public static EcmaError constructError(String error,
return constructError(error, message, filename, linep[0], null, 0);
}

public static EcmaError constructError(String error,
public static EcmaError constructError(NativeErrors error,
String message,
String sourceName,
int lineNumber,
Expand All @@ -3952,7 +3953,7 @@ public static EcmaError constructError(String error,

public static EcmaError typeError(String message)
{
return constructError("TypeError", message);
return constructError(NativeErrors.TypeError, message);
}

public static EcmaError typeError0(String messageId)
Expand Down Expand Up @@ -4009,7 +4010,7 @@ public static RuntimeException notFoundError(Scriptable object,
{
// XXX: use object to improve the error message
String msg = getMessage1("msg.is.not.defined", property);
throw constructError("ReferenceError", msg);
throw constructError(NativeErrors.ReferenceError, msg);
}

public static RuntimeException notFunctionError(Object value)
Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/TopLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public enum Builtins {
/**
* An enumeration of built-in native errors. [ECMAScript 5 - 15.11.6]
*/
enum NativeErrors {
public enum NativeErrors {
/** Basic Error */
Error,
/** The native EvalError. */
Expand Down
6 changes: 3 additions & 3 deletions src/org/mozilla/javascript/regexp/NativeRegExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ else if (!input.regionMatches(parenContent, input, gData.cp, len)) {
{
int byteIndex = (c / 8);
if (c >= cs.length) {
throw ScriptRuntime.constructError("SyntaxError",
throw ScriptRuntime.constructError(TopLevel.NativeErrors.SyntaxError,
"invalid range in character class");
}
cs.bits[byteIndex] |= 1 << (c & 0x7);
Expand All @@ -1508,7 +1508,7 @@ else if (!input.regionMatches(parenContent, input, gData.cp, len)) {
int byteIndex2 = (c2 / 8);

if ((c2 >= cs.length) || (c1 > c2)) {
throw ScriptRuntime.constructError("SyntaxError",
throw ScriptRuntime.constructError(TopLevel.NativeErrors.SyntaxError,
"invalid range in character class");
}

Expand Down Expand Up @@ -2562,7 +2562,7 @@ private static void reportWarning(Context cx, String messageId, String arg)
private static void reportError(String messageId, String arg)
{
String msg = ScriptRuntime.getMessage1(messageId, arg);
throw ScriptRuntime.constructError("SyntaxError", msg);
throw ScriptRuntime.constructError(TopLevel.NativeErrors.SyntaxError, msg);
}

// #string_id_map#
Expand Down
9 changes: 2 additions & 7 deletions src/org/mozilla/javascript/typedarrays/NativeArrayBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@

package org.mozilla.javascript.typedarrays;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.IdFunctionObject;
import org.mozilla.javascript.IdScriptableObject;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.Undefined;
import org.mozilla.javascript.*;

/**
* A NativeArrayBuffer is the backing buffer for a typed array. Used inside JavaScript code,
Expand Down Expand Up @@ -57,7 +52,7 @@ public NativeArrayBuffer()
public NativeArrayBuffer(int len)
{
if (len < 0) {
throw ScriptRuntime.constructError("RangeError", "Negative array length " + len);
throw ScriptRuntime.constructError(TopLevel.NativeErrors.RangeError, "Negative array length " + len);
}
if (len == 0) {
buffer = EMPTY_BUF;
Expand Down
25 changes: 12 additions & 13 deletions src/org/mozilla/javascript/typedarrays/NativeDataView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

package org.mozilla.javascript.typedarrays;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.IdFunctionObject;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.Undefined;
import org.mozilla.javascript.*;

import static org.mozilla.javascript.TopLevel.NativeErrors.RangeError;
import static org.mozilla.javascript.TopLevel.NativeErrors.TypeError;

/**
* This class represents the JavaScript "DataView" interface, which allows direct manipulations of the
Expand Down Expand Up @@ -50,27 +49,27 @@ public static void init(Context cx, Scriptable scope, boolean sealed)
private void rangeCheck(int offset, int len)
{
if ((offset < 0) || ((offset + len) > byteLength)) {
throw ScriptRuntime.constructError("RangeError", "offset out of range");
throw ScriptRuntime.constructError(RangeError, "offset out of range");
}
}

private void checkOffset(Object[] args, int pos)
{
if (args.length <= pos) {
throw ScriptRuntime.constructError("TypeError", "missing required offset parameter");
throw ScriptRuntime.constructError(TypeError, "missing required offset parameter");
}
if (Undefined.instance.equals(args[pos])) {
throw ScriptRuntime.constructError("RangeError", "invalid offset");
throw ScriptRuntime.constructError(RangeError, "invalid offset");
}
}

private void checkValue(Object[] args, int pos)
{
if (args.length <= pos) {
throw ScriptRuntime.constructError("TypeError", "missing required value parameter");
throw ScriptRuntime.constructError(TypeError, "missing required value parameter");
}
if (Undefined.instance.equals(args[pos])) {
throw ScriptRuntime.constructError("RangeError", "invalid value parameter");
throw ScriptRuntime.constructError(RangeError, "invalid value parameter");
}
}

Expand All @@ -84,10 +83,10 @@ private static NativeDataView realThis(Scriptable thisObj, IdFunctionObject f)
private NativeDataView js_constructor(NativeArrayBuffer ab, int offset, int length)
{
if (length < 0) {
throw ScriptRuntime.constructError("RangeError", "length out of range");
throw ScriptRuntime.constructError(RangeError, "length out of range");
}
if ((offset < 0) || ((offset + length) > ab.getLength())) {
throw ScriptRuntime.constructError("RangeError", "offset out of range");
throw ScriptRuntime.constructError(RangeError, "offset out of range");
}
return new NativeDataView(ab, offset, length);
}
Expand Down Expand Up @@ -217,7 +216,7 @@ public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
int len = isArg(args, 2) ? ScriptRuntime.toInt32(args[2]) : ab.getLength() - off;
return js_constructor(ab, off, len);
} else {
throw ScriptRuntime.constructError("TypeError", "Missing parameters");
throw ScriptRuntime.constructError(TypeError, "Missing parameters");
}
case Id_getInt8:
return realThis(thisObj, f).js_getInt(1, true, args);
Expand Down
Loading

0 comments on commit 36cd306

Please sign in to comment.