diff --git a/js/rhino/src/org/mozilla/javascript/Context.java b/js/rhino/src/org/mozilla/javascript/Context.java index 5fe1ab5dd68..6604c5c28f8 100644 --- a/js/rhino/src/org/mozilla/javascript/Context.java +++ b/js/rhino/src/org/mozilla/javascript/Context.java @@ -1766,6 +1766,16 @@ public class Context { */ public static final int FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER = 3; + /** + * if hasFeature(FEATURE_TO_STRING_AS_SOURCE) returns true, + * calling toString on JS objects gives JS source with code to create an + * object with all enumeratable fields of the original object instead of + * printing "[object ]". + * By default {@link #hasFeature(int)} returns true only if + * the current JS version is set to {@link #VERSION_1_2}. + */ + public static final int FEATURE_TO_STRING_AS_SOURCE = 4; + /** * Controls certain aspects of script semantics. * Should be overwritten to alter default behavior. @@ -1774,6 +1784,7 @@ public class Context { * @see #FEATURE_NON_ECMA_GET_YEAR * @see #FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME * @see #FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER + * @see #FEATURE_TO_STRING_AS_SOURCE */ public boolean hasFeature(int featureIndex) { switch (featureIndex) { @@ -1798,6 +1809,9 @@ public class Context { case FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER: return false; + + case FEATURE_TO_STRING_AS_SOURCE: + return version == VERSION_1_2; } // It is a bug to call the method with unknown featureIndex throw new IllegalArgumentException(); diff --git a/js/rhino/src/org/mozilla/javascript/NativeArray.java b/js/rhino/src/org/mozilla/javascript/NativeArray.java index ab1c7813c26..d22aec8ae49 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeArray.java +++ b/js/rhino/src/org/mozilla/javascript/NativeArray.java @@ -438,8 +438,8 @@ public class NativeArray extends IdScriptable { throws JavaScriptException { return toStringHelper(cx, thisObj, - cx.getLanguageVersion() == cx.VERSION_1_2, - false); + cx.hasFeature(Context.FEATURE_TO_STRING_AS_SOURCE), + false); } private static String jsFunction_toLocaleString(Context cx, diff --git a/js/rhino/src/org/mozilla/javascript/NativeObject.java b/js/rhino/src/org/mozilla/javascript/NativeObject.java index 7a5f89f75aa..3a982ff17cb 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeObject.java +++ b/js/rhino/src/org/mozilla/javascript/NativeObject.java @@ -126,10 +126,10 @@ public class NativeObject extends IdScriptable { private static String jsFunction_toString(Context cx, Scriptable thisObj) { - if (cx.getLanguageVersion() != cx.VERSION_1_2) - return "[object " + thisObj.getClassName() + "]"; - - return toSource(cx, thisObj); + if (cx.hasFeature(Context.FEATURE_TO_STRING_AS_SOURCE)) { + return toSource(cx, thisObj); + } + return "[object " + thisObj.getClassName() + "]"; } private static String jsFunction_toLocaleString(Context cx,