Changeset 10416 in webkit for trunk/JavaScriptCore/kjs


Ignore:
Timestamp:
Aug 31, 2005, 11:36:47 AM (20 years ago)
Author:
ggaren
Message:

-rolled in fix for http://bugzilla.opendarwin.org/show_bug.cgi?id=4698
kjs does not allow named functions in function expressions

Fix by Arthur Langereis.

Reviewed by darin.

  • kjs/grammar.y:
  • kjs/nodes.cpp: (FuncExprNode::evaluate):
  • kjs/nodes.h: (KJS::FuncExprNode::FuncExprNode):

Test cases added:

  • layout-tests/fast/js/named-function-expression-expected.txt: Added.
  • layout-tests/fast/js/named-function-expression.html: Added.
Location:
trunk/JavaScriptCore/kjs
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/grammar.y

    r10352 r10416  
    668668
    669669FunctionDeclaration:
    670     FUNCTION IDENT '(' ')' FunctionBody    { $$ = new FuncDeclNode(*$2, $5); }
     670    FUNCTION '(' ')' FunctionBody  { YYABORT; }
     671  | FUNCTION '(' FormalParameterList ')' FunctionBody
     672                                   { YYABORT; }
     673  | FUNCTION IDENT '(' ')' FunctionBody
     674                                   { $$ = new FuncDeclNode(*$2, $5); }
    671675  | FUNCTION IDENT '(' FormalParameterList ')' FunctionBody
    672676                                   { $$ = new FuncDeclNode(*$2, $4, $6); }
     677;
    673678
    674679FunctionExpr:
    675     FUNCTION '(' ')' FunctionBody  { $$ = new FuncExprNode($4); }
     680    FUNCTION '(' ')' FunctionBody  { $$ = new FuncExprNode(Identifier::null(), $4); }
    676681  | FUNCTION '(' FormalParameterList ')' FunctionBody
    677                                    { $$ = new FuncExprNode($3, $5); }
    678 
     682                                   { $$ = new FuncExprNode(Identifier::null(), $3, $5); }
     683  | FUNCTION IDENT '(' ')' FunctionBody
     684                                   { $$ = new FuncExprNode(*$2, $5); }
     685  | FUNCTION IDENT '(' FormalParameterList ')' FunctionBody
     686                                   { $$ = new FuncExprNode(*$2, $4, $6); }
    679687;
    680688
     
    704712
    705713SourceElement:
    706     Statement                      { $$ = $1; }
    707   | FunctionDeclaration            { $$ = $1; }
     714    FunctionDeclaration            { $$ = $1; }
     715  | Statement                      { $$ = $1; }
    708716;
    709717
  • trunk/JavaScriptCore/kjs/nodes.cpp

    r10354 r10416  
    21182118ValueImp *FuncExprNode::evaluate(ExecState *exec)
    21192119{
    2120   FunctionImp *fimp = new DeclaredFunctionImp(exec, Identifier::null(), body.get(), exec->context().imp()->scopeChain());
     2120  ContextImp *context = exec->context().imp();
     2121  bool named = !ident.isNull();
     2122  ObjectImp *functionScopeObject = NULL;
     2123
     2124  if (named) {
     2125    // named FunctionExpressions can recursively call themselves,
     2126    // but they won't register with the current scope chain and should
     2127    // be contained as single property in an anonymous object.
     2128    functionScopeObject = new ObjectImp;
     2129    context->pushScope(functionScopeObject);
     2130  }
     2131
     2132  FunctionImp *fimp = new DeclaredFunctionImp(exec, ident, body.get(), context->scopeChain());
    21212133  ValueImp *ret(fimp);
    21222134  ValueImp *proto = exec->lexicalInterpreter()->builtinObject()->construct(exec, List::empty());
     
    21262138  for(ParameterNode *p = param.get(); p != 0L; p = p->nextParam(), plen++)
    21272139    fimp->addParameter(p->ident());
     2140
     2141  if (named) {
     2142    functionScopeObject->put(exec, ident, ret, Internal | ReadOnly | (context->codeType() == EvalCode ? 0 : DontDelete));
     2143    context->popScope();
     2144  }
    21282145
    21292146  return ret;
  • trunk/JavaScriptCore/kjs/nodes.h

    r10399 r10416  
    971971  class FuncExprNode : public Node {
    972972  public:
    973     FuncExprNode(FunctionBodyNode *b) : param(0), body(b) { }
    974     FuncExprNode(ParameterNode *p, FunctionBodyNode *b)
    975       : param(p->next), body(b) { p->next = 0; }
    976     ValueImp *evaluate(ExecState *exec);
    977     virtual void streamTo(SourceStream &s) const;
    978   private:
     973    FuncExprNode(const Identifier &i, FunctionBodyNode *b)
     974      : ident(i), param(0), body(b) { }
     975    FuncExprNode(const Identifier &i, ParameterNode *p, FunctionBodyNode *b)
     976      : ident(i), param(p->next), body(b) { p->next = 0; }
     977    ValueImp *evaluate(ExecState *exec);
     978    virtual void streamTo(SourceStream &s) const;
     979  private:
     980    Identifier ident;
    979981    KXMLCore::SharedPtr<ParameterNode> param;
    980982    KXMLCore::SharedPtr<FunctionBodyNode> body;
Note: See TracChangeset for help on using the changeset viewer.