Skip to content

Commit d7517d2

Browse files
rbrigbrail
authored andcommitted
make '__parent__' property no longer available in ES6
'__parent__' was already removed in 2010 from the browsers (https://whereswalden.com/2010/05/07/spidermonkey-change-du-jour-the-special-__parent__-property-has-been-removed/)
1 parent 5cefc25 commit d7517d2

7 files changed

Lines changed: 39 additions & 22 deletions

File tree

rhino-tools/src/main/java/org/mozilla/javascript/tools/debugger/Dim.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ private Object getObjectPropertyImpl(Context cx, Object object, Object id) {
545545
result = scriptable;
546546
} else if (name.equals(NativeObject.PROTO_PROPERTY)) {
547547
result = scriptable.getPrototype();
548-
} else if (name.equals("__parent__")) {
548+
} else if (name.equals(NativeObject.PARENT_PROPERTY)) {
549549
result = scriptable.getParentScope();
550550
} else {
551551
result = ScriptableObject.getProperty(scriptable, name);
@@ -595,7 +595,7 @@ private Object[] getObjectIdsImpl(Context cx, Object object) {
595595
ids[extra++] = NativeObject.PROTO_PROPERTY;
596596
}
597597
if (parent != null) {
598-
ids[extra++] = "__parent__";
598+
ids[extra++] = NativeObject.PARENT_PROPERTY;
599599
}
600600
}
601601

rhino/src/main/java/org/mozilla/javascript/IRFactory.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,8 +2025,8 @@ private Node createPropertyGet(
20252025
}
20262026
parser.checkActivationName(name, Token.GETPROP);
20272027

2028-
if (target.getType() == Token.SUPER && ScriptRuntime.isSpecialSuperProperty(name)) {
2029-
// We have access to super.__proto__ or super.__parent__.
2028+
if (target.getType() == Token.SUPER && NativeObject.PROTO_PROPERTY.equals(name)) {
2029+
// We have access to super.__proto__.
20302030
// This needs to behave in the same way as this.__proto__ - it really is not
20312031
// obvious why, but you can test it in v8 or any other engine. So, we just
20322032
// replace SUPER with THIS in the AST. It's a bit hacky, but it works - see the
@@ -2048,8 +2048,10 @@ private Node createPropertyGet(
20482048
getRef.putIntProp(Node.OPTIONAL_CHAINING, 1);
20492049
}
20502050
return getRef;
2051-
} else if (ScriptRuntime.isSpecialProperty(
2052-
name, parser.compilerEnv.getLanguageVersion())) {
2051+
}
2052+
2053+
if (parser.compilerEnv.getLanguageVersion() < Context.VERSION_ES6
2054+
&& ScriptRuntime.isSpecialProperty(name)) {
20532055
Node ref = new Node(Token.REF_SPECIAL, target);
20542056
ref.putProp(Node.NAME_PROP, name);
20552057
Node getRef = new Node(Token.GET_REF, ref);

rhino/src/main/java/org/mozilla/javascript/NativeObject.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class NativeObject extends ScriptableObject implements Map {
3131
private static final String CLASS_NAME = "Object";
3232

3333
public static final String PROTO_PROPERTY = "__proto__";
34+
public static final String PARENT_PROPERTY = "__parent__";
3435

3536
static LambdaConstructor init(Context cx, Scriptable s, boolean sealed) {
3637
LambdaConstructor ctor =

rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,19 +2143,8 @@ public static Object refDel(Ref ref, Context cx) {
21432143
return wrapBoolean(ref.delete(cx));
21442144
}
21452145

2146-
static boolean isSpecialProperty(String s, int languageVersion) {
2147-
if (languageVersion >= Context.VERSION_ES6) {
2148-
return s.equals("__parent__");
2149-
}
2150-
2151-
return s.equals(NativeObject.PROTO_PROPERTY) || s.equals("__parent__");
2152-
}
2153-
2154-
// for the super handling, we still not language dependent
2155-
// have a look at the comment in for more details
2156-
// org.mozilla.javascript.IRFactory.createPropertyGet()
2157-
static boolean isSpecialSuperProperty(String s) {
2158-
return s.equals(NativeObject.PROTO_PROPERTY) || s.equals("__parent__");
2146+
static boolean isSpecialProperty(String s) {
2147+
return s.equals(NativeObject.PROTO_PROPERTY) || s.equals(NativeObject.PARENT_PROPERTY);
21592148
}
21602149

21612150
/**
@@ -5429,7 +5418,8 @@ public static void fillObjectLiteral(
54295418
object.put(s.index, object, value);
54305419
} else {
54315420
String stringId = s.stringId;
5432-
if (isSpecialProperty(stringId, cx.getLanguageVersion())) {
5421+
if (cx.getLanguageVersion() < Context.VERSION_ES6
5422+
&& isSpecialProperty(stringId)) {
54335423
Ref ref = specialRef(object, stringId, cx, scope);
54345424
ref.set(cx, scope, value);
54355425
} else if (cx.getLanguageVersion() >= Context.VERSION_ES6

rhino/src/main/java/org/mozilla/javascript/SpecialRef.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static Ref createSpecial(Context cx, Scriptable scope, Object object, String nam
3232
int type;
3333
if (name.equals(NativeObject.PROTO_PROPERTY)) {
3434
type = SPECIAL_PROTO;
35-
} else if (name.equals("__parent__")) {
35+
} else if (name.equals(NativeObject.PARENT_PROPERTY)) {
3636
type = SPECIAL_PARENT;
3737
} else {
3838
throw new IllegalArgumentException(name);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
/*
6+
* Tests for the __parent__ property.
7+
*/
8+
package org.mozilla.javascript.tests.es6;
9+
10+
import org.junit.Test;
11+
import org.mozilla.javascript.testutils.Utils;
12+
13+
public class ParentPropertyTest {
14+
15+
@Test
16+
public void parentGet() {
17+
// __parent__ was removed in 2010 from the browsers
18+
// https://whereswalden.com/2010/05/07/spidermonkey-change-du-jour-the-special-__parent__-property-has-been-removed/
19+
String script = "var a = {};" + "'' + a.__parent__;";
20+
21+
Utils.assertWithAllModes_1_8("[object Object]", script);
22+
Utils.assertWithAllModes_ES6("undefined", script);
23+
}
24+
}

tests/src/test/java/org/mozilla/javascript/tests/es6/ProtoProperty2Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* https://exploringjs.com/es6/ch_oop-besides-classes.html#sec_proto
1414
*
1515
* <p>Test are written with asserts for older language versions, because the old code has some
16-
* switches for the used versions. This shold help to maintain backward compatibility.
16+
* switches for the used versions. This should help to maintain backward compatibility.
1717
*/
1818
public class ProtoProperty2Test {
1919

0 commit comments

Comments
 (0)