Checking for 1.2 version in toString for objects and arrays is replaced by calling cx.hasFeature(Context.FEATURE_TO_STRING_AS_SOURCE) to allow to control toString from applications without switching on other JS 1.2 features.

This commit is contained in:
igor%mir2.org 2002-07-16 17:42:18 +00:00
Родитель 31a00f4fbb
Коммит b0651090a1
3 изменённых файлов: 20 добавлений и 6 удалений

Просмотреть файл

@ -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 <object-type>]".
* 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();

Просмотреть файл

@ -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,

Просмотреть файл

@ -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,