Changeset 29815 in webkit for trunk/JavaScriptCore/kjs


Ignore:
Timestamp:
Jan 26, 2008, 8:21:43 PM (17 years ago)
Author:
Darin Adler
Message:

JavaScriptCore:

Reviewed by Oliver.

Test: fast/js/function-toString-parentheses.html

The problem here was that a NumberNode with a negative number in it had the wrong
precedence. It's not a primary expression, it's a unary operator with a primary
expression after it.

Once the precedence of NumberNode was fixed, the cases from bug 17020 were also
fixed without trying to treat bracket nodes like dot nodes. That wasn't needed.
The reason we handle numbers before dot nodes specially is that the dot is a
legal character in a number. The same is not true of a bracket. Eventually we
could get smarter, and only add the parentheses when there is actual ambiguity.
There is none if the string form of the number already has a dot in it, or if
it's a number with a alphabetic name like infinity or NAN.

  • kjs/nodes.h: Renamed back from ObjectAccess to DotExpr. (KJS::NumberNode::precedence): Return PrecUnary for negative numbers, since they serialize as a unary operator, not a primary expression.
  • kjs/nodes2string.cpp: (KJS::SourceStream::operator<<): Clear m_numberNeedsParens if this adds parens; one set is enough. (KJS::bracketNodeStreamTo): Remove unneeded special flag here. Normal operator precedence suffices. (KJS::NewExprNode::streamTo): Ditto.

LayoutTests:

Reviewed by Oliver.

  • fast/js/function-toString-parentheses-expected.txt: Updated.
  • fast/js/resources/function-toString-parentheses.js: More test cases.
Location:
trunk/JavaScriptCore/kjs
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/nodes.h

    r29809 r29815  
    3131#include "SymbolTable.h"
    3232#include <wtf/ListRefPtr.h>
     33#include <wtf/MathExtras.h>
    3334#include <wtf/OwnPtr.h>
    3435#include <wtf/Vector.h>
     
    253254    virtual uint32_t evaluateToUInt32(ExecState*) KJS_FAST_CALL;
    254255    virtual void streamTo(SourceStream&) const KJS_FAST_CALL;
    255     virtual Precedence precedence() const { return PrecPrimary; }
     256    virtual Precedence precedence() const { return signbit(m_double) ? PrecUnary : PrecPrimary; }
    256257
    257258    virtual bool isNumber() const KJS_FAST_CALL { return true; }
  • trunk/JavaScriptCore/kjs/nodes2string.cpp

    r29813 r29815  
    11/*
    22 *  Copyright (C) 2002 Harri Porten ([email protected])
    3  *  Copyright (C) 2003, 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
     3 *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
    44 *  Copyright (C) 2007 Eric Seidel <[email protected]>
    55 *
     
    3838enum IndentType { Indent };
    3939enum UnindentType { Unindent };
    40 enum ObjectAccessType { ObjectAccess };
     40enum DotExprType { DotExpr };
    4141
    4242class SourceStream {
     
    5252    SourceStream& operator<<(IndentType);
    5353    SourceStream& operator<<(UnindentType);
    54     SourceStream& operator<<(ObjectAccessType);
     54    SourceStream& operator<<(DotExprType);
    5555    SourceStream& operator<<(Precedence);
    5656    SourceStream& operator<<(const Node*);
     
    201201    bool needParens = (m_precedence != PrecExpression && n->precedence() > m_precedence) || (m_atStartOfStatement && n->needsParensIfLeftmost());
    202202    m_precedence = PrecExpression;
    203     if (n) {
    204         if (needParens)
    205             m_string.append('(');
    206         n->streamTo(*this);
    207         if (needParens)
    208             m_string.append(')');
     203    if (!n)
     204        return *this;
     205    if (needParens) {
     206        m_numberNeedsParens = false;
     207        m_string.append('(');
    209208    }
     209    n->streamTo(*this);
     210    if (needParens)
     211        m_string.append(')');
    210212    return *this;
    211213}
     
    236238}
    237239
    238 inline SourceStream& SourceStream::operator<<(ObjectAccessType)
     240inline SourceStream& SourceStream::operator<<(DotExprType)
    239241{
    240242    m_numberNeedsParens = true;
     
    264266static inline void bracketNodeStreamTo(SourceStream& s, const RefPtr<ExpressionNode>& base, const RefPtr<ExpressionNode>& subscript)
    265267{
    266     s << ObjectAccess << PrecCall << base.get() << "[" << subscript.get() << "]";
     268    s << PrecCall << base.get() << "[" << subscript.get() << "]";
    267269}
    268270
    269271static inline void dotNodeStreamTo(SourceStream& s, const RefPtr<ExpressionNode>& base, const Identifier& ident)
    270272{
    271     s << ObjectAccess << PrecCall << base.get() << "." << ident;
     273    s << DotExpr << PrecCall << base.get() << "." << ident;
    272274}
    273275
     
    415417void NewExprNode::streamTo(SourceStream& s) const
    416418{
    417     s << "new " << ObjectAccess << PrecMember << expr << args;
     419    s << "new " << PrecMember << expr << args;
    418420}
    419421
Note: See TracChangeset for help on using the changeset viewer.