Skip to content

Commit

Permalink
Reverted "Optional Chaining Operator ?. mozilla#1593"
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabergia committed Oct 17, 2024
1 parent 3d7f155 commit 34c488e
Show file tree
Hide file tree
Showing 15 changed files with 20 additions and 390 deletions.
13 changes: 0 additions & 13 deletions rhino/src/main/java/org/mozilla/javascript/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,6 @@ private void visitExpression(Node node, int contextFlags) {

case Token.REF_CALL:
case Token.CALL:
case Token.CALL_OPTIONAL:
case Token.NEW:
{
if (type == Token.NEW) {
Expand Down Expand Up @@ -685,11 +684,6 @@ private void visitExpression(Node node, int contextFlags) {
resolveForwardGoto(afterElseJumpStart);
}
break;
case Token.GETPROP_OPTIONAL:
visitExpression(child, 0);
child = child.getNext();
addStringOp(type, child.getString());
break;

case Token.GETPROP:
case Token.GETPROPNOWARN:
Expand Down Expand Up @@ -966,7 +960,6 @@ private void visitExpression(Node node, int contextFlags) {
visitArrayComprehension(node, child, child.getNext());
break;

case Token.REF_SPECIAL_OPTIONAL:
case Token.REF_SPECIAL:
visitExpression(child, 0);
addStringOp(type, (String) node.getProp(Node.NAME_PROP));
Expand Down Expand Up @@ -1062,7 +1055,6 @@ private void generateCallFunAndThis(Node left) {
stackChange(2);
break;
}
case Token.GETPROP_OPTIONAL:
case Token.GETPROP:
case Token.GETELEM:
{
Expand All @@ -1074,11 +1066,6 @@ private void generateCallFunAndThis(Node left) {
// stack: ... target -> ... function thisObj
addStringOp(Icode_PROP_AND_THIS, property);
stackChange(1);
} else if (type == Token.GETPROP_OPTIONAL) {
String property = id.getString();
// stack: ... target -> ... function thisObj
addStringOp(Icode_PROP_AND_THIS_OPTIONAL, property);
stackChange(1);
} else {
visitExpression(id, 0);
// stack: ... target id -> ... function thisObj
Expand Down
28 changes: 7 additions & 21 deletions rhino/src/main/java/org/mozilla/javascript/IRFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ private Node transform(AstNode node) {
return transformBlock(node);
case Token.BREAK:
return transformBreak((BreakStatement) node);
case Token.CALL_OPTIONAL:
case Token.CALL:
return transformFunctionCall((FunctionCall) node);
case Token.CONTINUE:
Expand All @@ -162,7 +161,6 @@ private Node transform(AstNode node) {
return transformGenExpr((GeneratorExpression) node);
case Token.GETELEM:
return transformElementGet((ElementGet) node);
case Token.QUESTION_DOT:
case Token.GETPROP:
return transformPropertyGet((PropertyGet) node);
case Token.HOOK:
Expand Down Expand Up @@ -349,8 +347,7 @@ private Node arrayCompTransformHelper(ArrayComprehension node, String arrayName)
Node call =
createCallOrNew(
Token.CALL,
createPropertyGet(
parser.createName(arrayName), null, "push", 0, node.type));
createPropertyGet(parser.createName(arrayName), null, "push", 0));

Node body = new Node(Token.EXPR_VOID, call, lineno);

Expand Down Expand Up @@ -644,10 +641,7 @@ private Node transformFunction(FunctionNode fn) {
}

private Node transformFunctionCall(FunctionCall node) {
Node call =
createCallOrNew(
node.type == Token.CALL_OPTIONAL ? Token.CALL_OPTIONAL : Token.CALL,
transform(node.getTarget()));
Node call = createCallOrNew(Token.CALL, transform(node.getTarget()));
call.setLineno(node.getLineno());
List<AstNode> args = node.getArguments();
for (int i = 0; i < args.size(); i++) {
Expand Down Expand Up @@ -944,7 +938,7 @@ private Node transformComputedPropertyKey(ComputedPropertyKey node) {
private Node transformPropertyGet(PropertyGet node) {
Node target = transform(node.getTarget());
String name = node.getProperty().getIdentifier();
return createPropertyGet(target, null, name, 0, node.type);
return createPropertyGet(target, null, name, 0);
}

private Node transformTemplateLiteral(TemplateLiteral node) {
Expand Down Expand Up @@ -1268,7 +1262,7 @@ private Node transformXmlRef(Node pn, XmlRef node, int memberTypeFlags) {
String ns = namespace != null ? namespace.getIdentifier() : null;
if (node instanceof XmlPropRef) {
String name = ((XmlPropRef) node).getPropName().getIdentifier();
return createPropertyGet(pn, ns, name, memberTypeFlags, node.type);
return createPropertyGet(pn, ns, name, memberTypeFlags);
}
Node expr = transform(((XmlElemRef) node).getExpression());
return createElementGet(pn, ns, expr, memberTypeFlags);
Expand Down Expand Up @@ -1903,27 +1897,19 @@ private static Node createIncDec(int nodeType, boolean post, Node child) {
}

private Node createPropertyGet(
Node target, String namespace, String name, int memberTypeFlags, int type) {
Node target, String namespace, String name, int memberTypeFlags) {
if (namespace == null && memberTypeFlags == 0) {
if (target == null) {
return parser.createName(name);
}
parser.checkActivationName(name, Token.GETPROP);
if (ScriptRuntime.isSpecialProperty(name)) {
Node ref =
new Node(
type == Token.QUESTION_DOT
? Token.REF_SPECIAL_OPTIONAL
: Token.REF_SPECIAL,
target);
Node ref = new Node(Token.REF_SPECIAL, target);
ref.putProp(Node.NAME_PROP, name);
return new Node(Token.GET_REF, ref);
}

return new Node(
type == Token.QUESTION_DOT ? Token.GETPROP_OPTIONAL : Token.GETPROP,
target,
Node.newString(name));
return new Node(Token.GETPROP, target, Node.newString(name));
}
Node elem = Node.newString(name);
memberTypeFlags |= Node.PROPERTY_FLAG;
Expand Down
5 changes: 1 addition & 4 deletions rhino/src/main/java/org/mozilla/javascript/Icode.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,8 @@ abstract class Icode {
Icode_TEMPLATE_LITERAL_CALLSITE = Icode_REG_BIGINT4 - 1,
Icode_LITERAL_KEYS = Icode_TEMPLATE_LITERAL_CALLSITE - 1,
Icode_LITERAL_KEY_SET = Icode_LITERAL_KEYS - 1,
Icode_PROP_AND_THIS_OPTIONAL = Icode_LITERAL_KEY_SET - 1,
// Last icode
MIN_ICODE = Icode_PROP_AND_THIS_OPTIONAL;
MIN_ICODE = Icode_LITERAL_KEY_SET;

static String bytecodeName(int bytecode) {
if (!validBytecode(bytecode)) {
Expand Down Expand Up @@ -190,8 +189,6 @@ static String bytecodeName(int bytecode) {
return "NAME_AND_THIS";
case Icode_PROP_AND_THIS:
return "PROP_AND_THIS";
case Icode_PROP_AND_THIS_OPTIONAL:
return "PROP_AND_THIS_OPTIONAL";
case Icode_ELEM_AND_THIS:
return "ELEM_AND_THIS";
case Icode_VALUE_AND_THIS:
Expand Down
35 changes: 0 additions & 35 deletions rhino/src/main/java/org/mozilla/javascript/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1622,16 +1622,6 @@ private static Object interpretLoop(Context cx, CallFrame frame, Object throwabl
lhs, stringReg, cx, frame.scope);
continue Loop;
}
case Token.GETPROP_OPTIONAL:
{
Object lhs = stack[stackTop];
if (lhs == DBL_MRK)
lhs = ScriptRuntime.wrapNumber(sDbl[stackTop]);
stack[stackTop] =
ScriptRuntime.getObjectPropOptional(
lhs, stringReg, cx, frame.scope);
continue Loop;
}
case Token.SETPROP:
{
Object rhs = stack[stackTop];
Expand Down Expand Up @@ -1728,19 +1718,6 @@ private static Object interpretLoop(Context cx, CallFrame frame, Object throwabl
++stackTop;
stack[stackTop] = ScriptRuntime.lastStoredScriptable(cx);
continue Loop;
case Icode_PROP_AND_THIS_OPTIONAL:
{
Object obj = stack[stackTop];
if (obj == DBL_MRK)
obj = ScriptRuntime.wrapNumber(sDbl[stackTop]);
// stringReg: property
stack[stackTop] =
ScriptRuntime.getPropFunctionAndThisOptional(
obj, stringReg, cx, frame.scope);
++stackTop;
stack[stackTop] = ScriptRuntime.lastStoredScriptable(cx);
continue Loop;
}
case Icode_PROP_AND_THIS:
{
Object obj = stack[stackTop];
Expand Down Expand Up @@ -1791,7 +1768,6 @@ private static Object interpretLoop(Context cx, CallFrame frame, Object throwabl
continue Loop;
}
case Token.CALL:
case Token.CALL_OPTIONAL:
case Icode_TAIL_CALL:
case Token.REF_CALL:
{
Expand Down Expand Up @@ -2308,17 +2284,6 @@ private static Object interpretLoop(Context cx, CallFrame frame, Object throwabl
obj, stringReg, cx, frame.scope);
continue Loop;
}
case Token.REF_SPECIAL_OPTIONAL:
{
// stringReg: name of special property
Object obj = stack[stackTop];
if (obj == DBL_MRK)
obj = ScriptRuntime.wrapNumber(sDbl[stackTop]);
stack[stackTop] =
ScriptRuntime.optionalSpecialRef(
obj, stringReg, cx, frame.scope);
continue Loop;
}
case Token.REF_MEMBER:
{
// indexReg: flags
Expand Down
34 changes: 0 additions & 34 deletions rhino/src/main/java/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2439,8 +2439,6 @@ private AstNode assignExpr() throws IOException {

markDestructuring(pn);
int opPos = ts.tokenBeg;
if (isNotValidSimpleAssignmentTarget(pn))
reportError("msg.syntax.invalid.assignment.lhs");

pn = new Assignment(tt, pn, assignExpr(), opPos);

Expand All @@ -2463,12 +2461,6 @@ private AstNode assignExpr() throws IOException {
return pn;
}

private static boolean isNotValidSimpleAssignmentTarget(AstNode pn) {
if (pn.getType() == Token.GETPROP)
return isNotValidSimpleAssignmentTarget(((PropertyGet) pn).getLeft());
return pn.getType() == Token.QUESTION_DOT;
}

private AstNode condExpr() throws IOException {
AstNode pn = nullishCoalescingExpr();
if (matchToken(Token.HOOK, true)) {
Expand Down Expand Up @@ -2895,7 +2887,6 @@ private AstNode memberExprTail(boolean allowCallSyntax, AstNode pn) throws IOExc
int tt = peekToken();
switch (tt) {
case Token.DOT:
case Token.QUESTION_DOT:
case Token.DOTDOT:
lineno = ts.lineno;
pn = propertyAccess(tt, pn);
Expand Down Expand Up @@ -3026,27 +3017,6 @@ private AstNode propertyAccess(int tt, AstNode pn) throws IOException {

AstNode ref = null; // right side of . or .. operator
int token = nextToken();
if (token == Token.LP && tt == Token.QUESTION_DOT) {
// optional chaining operator method call, o.func?.()
var pos = pn.getPosition();
pn.setType(Token.QUESTION_DOT);
consumeToken();
checkCallRequiresActivation(pn);
FunctionCall f = new FunctionCall(pos);
f.setTarget(pn);
// Assign the line number for the function call to where
// the paren appeared, not where the name expression started.
f.setLineno(lineno);
f.setLp(ts.tokenBeg - pos);
List<AstNode> args = argumentList();
if (args != null && args.size() > ARGC_LIMIT) reportError("msg.too.many.function.args");
f.setArguments(args);
f.setRp(ts.tokenBeg - pos);
f.setLength(ts.tokenEnd - pos);
f.setType(Token.CALL_OPTIONAL);
return f;
}

switch (token) {
case Token.THROW:
// needed for generator.throw();
Expand Down Expand Up @@ -3102,10 +3072,6 @@ private AstNode propertyAccess(int tt, AstNode pn) throws IOException {
result.setLineno(pn.getLineno());
result.setLeft(pn); // do this after setting position
result.setRight(ref);

if (tt == Token.QUESTION_DOT && result instanceof PropertyGet) {
result.setType(Token.QUESTION_DOT);
}
return result;
}

Expand Down
35 changes: 0 additions & 35 deletions rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -1738,22 +1738,6 @@ public static Object getObjectProp(Object obj, String property, Context cx, Scri
return getObjectProp(sobj, property, cx);
}

public static Object getObjectPropOptional(
Object obj, String property, Context cx, Scriptable scope) {
if (obj == null || Undefined.isUndefined(obj)) {
return Undefined.instance;
}
return getObjectProp(obj, property, cx, scope);
}

public static Object getObjectPropOptional(
Scriptable obj, String property, Context cx, Scriptable scope) {
if (obj == null || Undefined.isUndefined(obj)) {
return Undefined.instance;
}
return getObjectProp(obj, property, cx);
}

public static Object getObjectProp(Scriptable obj, String property, Context cx) {

Object result = ScriptableObject.getProperty(obj, property);
Expand Down Expand Up @@ -1992,11 +1976,6 @@ public static Ref specialRef(Object obj, String specialProperty, Context cx, Scr
return SpecialRef.createSpecial(cx, scope, obj, specialProperty);
}

public static Ref optionalSpecialRef(
Object obj, String specialProperty, Context cx, Scriptable scope) {
return SpecialOptionalRef.create(cx, scope, obj, specialProperty);
}

/**
* @deprecated Use {@link #delete(Object, Object, Context, Scriptable, boolean)} instead
*/
Expand Down Expand Up @@ -2649,20 +2628,6 @@ public static Callable getElemFunctionAndThis(
return (Callable) value;
}

public static Callable getPropFunctionAndThisOptional(
Object obj, String property, Context cx, Scriptable scope) {

Scriptable thisObj = toObjectOrNull(cx, obj, scope);
if (thisObj == null) {
throw undefCallError(obj, property);
}

Object value = ScriptableObject.getProperty(thisObj, property);
if (Scriptable.NOT_FOUND == value || Undefined.isUndefined(value) || value == null)
return (cx1, scope1, thisObj2, args) -> Undefined.instance;
return getPropFunctionAndThisHelper(obj, property, cx, thisObj);
}

/**
* Prepare for calling obj.property(...): return function corresponding to obj.property and make
* obj properly converted to Scriptable available as ScriptRuntime.lastStoredScriptable() for
Expand Down
29 changes: 0 additions & 29 deletions rhino/src/main/java/org/mozilla/javascript/SpecialOptionalRef.java

This file was deleted.

Loading

0 comments on commit 34c488e

Please sign in to comment.