2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "FunctionConstructor.h"
24 #include "FunctionPrototype.h"
25 #include "JSFunction.h"
26 #include "JSGlobalObject.h"
35 ASSERT_CLASS_FITS_IN_CELL(FunctionConstructor
);
37 FunctionConstructor::FunctionConstructor(ExecState
* exec
, PassRefPtr
<Structure
> structure
, FunctionPrototype
* functionPrototype
)
38 : InternalFunction(&exec
->globalData(), structure
, Identifier(exec
, functionPrototype
->classInfo()->className
))
40 putDirectWithoutTransition(exec
->propertyNames().prototype
, functionPrototype
, DontEnum
| DontDelete
| ReadOnly
);
42 // Number of arguments for constructor
43 putDirectWithoutTransition(exec
->propertyNames().length
, jsNumber(exec
, 1), ReadOnly
| DontDelete
| DontEnum
);
46 static JSObject
* constructWithFunctionConstructor(ExecState
* exec
, JSObject
*, const ArgList
& args
)
48 return constructFunction(exec
, args
);
51 ConstructType
FunctionConstructor::getConstructData(ConstructData
& constructData
)
53 constructData
.native
.function
= constructWithFunctionConstructor
;
54 return ConstructTypeHost
;
57 static JSValuePtr
callFunctionConstructor(ExecState
* exec
, JSObject
*, JSValuePtr
, const ArgList
& args
)
59 return constructFunction(exec
, args
);
62 // ECMA 15.3.1 The Function Constructor Called as a Function
63 CallType
FunctionConstructor::getCallData(CallData
& callData
)
65 callData
.native
.function
= callFunctionConstructor
;
69 FunctionBodyNode
* extractFunctionBody(ProgramNode
* program
)
74 StatementVector
& children
= program
->children();
75 if (children
.size() != 1)
78 ExprStatementNode
* exprStatement
= static_cast<ExprStatementNode
*>(children
[0].get());
79 ASSERT(exprStatement
->isExprStatement());
80 if (!exprStatement
|| !exprStatement
->isExprStatement())
83 FuncExprNode
* funcExpr
= static_cast<FuncExprNode
*>(exprStatement
->expr());
84 ASSERT(funcExpr
->isFuncExprNode());
85 if (!funcExpr
|| !funcExpr
->isFuncExprNode())
88 FunctionBodyNode
* body
= funcExpr
->body();
93 // ECMA 15.3.2 The Function Constructor
94 JSObject
* constructFunction(ExecState
* exec
, const ArgList
& args
, const Identifier
& functionName
, const UString
& sourceURL
, int lineNumber
)
96 // Functions need to have a space following the opening { due to for web compatibility
97 // see https://bugs.webkit.org/show_bug.cgi?id=24350
98 // We also need \n before the closing } to handle // comments at the end of the last line
101 program
= "(function() { \n})";
102 else if (args
.size() == 1)
103 program
= "(function() { " + args
.at(exec
, 0).toString(exec
) + "\n})";
105 program
= "(function(" + args
.at(exec
, 0).toString(exec
);
106 for (size_t i
= 1; i
< args
.size() - 1; i
++)
107 program
+= "," + args
.at(exec
, i
).toString(exec
);
108 program
+= ") { " + args
.at(exec
, args
.size() - 1).toString(exec
) + "\n})";
113 SourceCode source
= makeSource(program
, sourceURL
, lineNumber
);
114 RefPtr
<ProgramNode
> programNode
= exec
->globalData().parser
->parse
<ProgramNode
>(exec
, exec
->dynamicGlobalObject()->debugger(), source
, &errLine
, &errMsg
);
116 FunctionBodyNode
* body
= extractFunctionBody(programNode
.get());
118 return throwError(exec
, SyntaxError
, errMsg
, errLine
, source
.provider()->asID(), source
.provider()->url());
120 JSGlobalObject
* globalObject
= exec
->lexicalGlobalObject();
121 ScopeChain
scopeChain(globalObject
, globalObject
->globalData(), exec
->globalThisValue());
122 return new (exec
) JSFunction(exec
, functionName
, body
, scopeChain
.node());
125 // ECMA 15.3.2 The Function Constructor
126 JSObject
* constructFunction(ExecState
* exec
, const ArgList
& args
)
128 return constructFunction(exec
, args
, Identifier(exec
, "anonymous"), UString(), 1);