From 46aea95d99528ca61663262d1439ac46792c9931 Mon Sep 17 00:00:00 2001 From: "igor%mir2.org" Date: Fri, 4 Jun 2004 14:57:31 +0000 Subject: [PATCH] Allow to disable special treatment of __proto__ and __parent__ via overriding Context.hasFeature(). --- .../src/org/mozilla/javascript/Context.java | 37 ++++++++++++++----- .../org/mozilla/javascript/ScriptRuntime.java | 9 +++++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/js/rhino/src/org/mozilla/javascript/Context.java b/js/rhino/src/org/mozilla/javascript/Context.java index 7c8ce5f5075..a8ea7b22c8f 100644 --- a/js/rhino/src/org/mozilla/javascript/Context.java +++ b/js/rhino/src/org/mozilla/javascript/Context.java @@ -1953,7 +1953,7 @@ public class Context } /** - * If hasFeature(FEATURE_NON_ECMA_GET_YEAR) returns true, + * If hasFeature(FEATURE_NON_ECMA_GET_YEAR) returns true, * Date.prototype.getYear subtructs 1900 only if 1900 <= date < 2000. * The default behavior is always to subtruct 1900 as rquired * by Ecma B.2.4. @@ -1961,32 +1961,47 @@ public class Context public static final int FEATURE_NON_ECMA_GET_YEAR = 1; /** - * If hasFeature(FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME) returns true, - * allow 'function memberExpression(args) { body }' to be syntax sugar - * for - * 'memberExpression = function(args) { body }', when memberExpression - * is not simply identifier. + * If hasFeature(FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME) returns + * true, allow function memberExpression(args) { body } to be + * syntax sugar for memberExpression = function(args) { body }, + * when memberExpression is not a simple identifier. * See Ecma-262, section 11.2 for definition of memberExpression. */ public static final int FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME = 2; /** - * If hasFeature(RESERVED_KEYWORD_AS_IDENTIFIER) returns true, + * If hasFeature(RESERVED_KEYWORD_AS_IDENTIFIER) returns true, * treat future reserved keyword (see Ecma-262, section 7.5.3) as ordinary * identifiers but warn about this usage */ public static final int FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER = 3; /** - * If hasFeature(FEATURE_TO_STRING_AS_SOURCE) returns true, + * 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 ]". + * 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; + /** + * If hasFeature(FEATURE_PARENT_PROTO_PROPRTIES) returns true, + * treat __parent__ and __proto__ as special properties. + *

+ * The properties allow to query and set scope and prototype chains for the + * objects. The special meaning of the properties is available + * only when they are used as the right hand side of the dot operator. + * For example, while x.__proto__ = y changes the prototype + * chain of the object x to point to y, + * x["__proto__"] = y simply assigns a new value to the property + * __proto__ in x even when the feature is on. + * + * By default {@link #hasFeature(int)} returns true. + */ + public static final int FEATURE_PARENT_PROTO_PROPRTIES = 5; + /** * Controls certain aspects of script semantics. * Should be overwritten to alter default behavior. @@ -1996,6 +2011,7 @@ public class Context * @see #FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME * @see #FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER * @see #FEATURE_TO_STRING_AS_SOURCE + * @see #FEATURE_PARENT_PROTO_PROPRTIES */ public boolean hasFeature(int featureIndex) { @@ -2024,6 +2040,9 @@ public class Context case FEATURE_TO_STRING_AS_SOURCE: return version == VERSION_1_2; + + case FEATURE_PARENT_PROTO_PROPRTIES: + return true; } // It is a bug to call the method with unknown featureIndex throw new IllegalArgumentException(); diff --git a/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java b/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java index 32d8e4e80b0..2f4353ba8b8 100644 --- a/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java +++ b/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java @@ -1247,10 +1247,16 @@ public class ScriptRuntime { } else { throw Kit.codeBug(); } + final boolean + specials = cx.hasFeature(Context.FEATURE_PARENT_PROTO_PROPRTIES); + final Scriptable sobj = toObject(scope, obj); return new Reference() { public Object get() { + if (!specials) { + return getProp(sobj, specialProperty); + } if (id == PROTO) { return getProto(sobj, scope); } else { @@ -1260,6 +1266,9 @@ public class ScriptRuntime { public Object set(Object value) { + if (!specials) { + return setProp(sobj, specialProperty, value); + } Scriptable result; if (value == null) { result = null;