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"
25 #include "FunctionPrototype.h"
26 #include "JSFunction.h"
27 #include "JSGlobalObject.h"
32 #include "StringBuilder.h"
36 ASSERT_CLASS_FITS_IN_CELL(FunctionConstructor
);
38 FunctionConstructor::FunctionConstructor(ExecState
* exec
, NonNullPassRefPtr
<Structure
> structure
, FunctionPrototype
* functionPrototype
)
39 : InternalFunction(&exec
->globalData(), structure
, Identifier(exec
, functionPrototype
->classInfo()->className
))
41 putDirectWithoutTransition(exec
->propertyNames().prototype
, functionPrototype
, DontEnum
| DontDelete
| ReadOnly
);
43 // Number of arguments for constructor
44 putDirectWithoutTransition(exec
->propertyNames().length
, jsNumber(exec
, 1), ReadOnly
| DontDelete
| DontEnum
);
47 static JSObject
* constructWithFunctionConstructor(ExecState
* exec
, JSObject
*, const ArgList
& args
)
49 return constructFunction(exec
, args
);
52 ConstructType
FunctionConstructor::getConstructData(ConstructData
& constructData
)
54 constructData
.native
.function
= constructWithFunctionConstructor
;
55 return ConstructTypeHost
;
58 static JSValue JSC_HOST_CALL
callFunctionConstructor(ExecState
* exec
, JSObject
*, JSValue
, const ArgList
& args
)
60 return constructFunction(exec
, args
);
63 // ECMA 15.3.1 The Function Constructor Called as a Function
64 CallType
FunctionConstructor::getCallData(CallData
& callData
)
66 callData
.native
.function
= callFunctionConstructor
;
70 // ECMA 15.3.2 The Function Constructor
71 JSObject
* constructFunction(ExecState
* exec
, const ArgList
& args
, const Identifier
& functionName
, const UString
& sourceURL
, int lineNumber
)
73 // Functions need to have a space following the opening { due to for web compatibility
74 // see https://bugs.webkit.org/show_bug.cgi?id=24350
75 // We also need \n before the closing } to handle // comments at the end of the last line
78 program
= "(function() { \n})";
79 else if (args
.size() == 1)
80 program
= makeString("(function() { ", args
.at(0).toString(exec
), "\n})");
82 StringBuilder builder
;
83 builder
.append("(function(");
84 builder
.append(args
.at(0).toString(exec
));
85 for (size_t i
= 1; i
< args
.size() - 1; i
++) {
87 builder
.append(args
.at(i
).toString(exec
));
89 builder
.append(") { ");
90 builder
.append(args
.at(args
.size() - 1).toString(exec
));
91 builder
.append("\n})");
92 program
= builder
.release();
97 SourceCode source
= makeSource(program
, sourceURL
, lineNumber
);
98 RefPtr
<FunctionExecutable
> function
= FunctionExecutable::fromGlobalCode(functionName
, exec
, exec
->dynamicGlobalObject()->debugger(), source
, &errLine
, &errMsg
);
100 return throwError(exec
, SyntaxError
, errMsg
, errLine
, source
.provider()->asID(), source
.provider()->url());
102 JSGlobalObject
* globalObject
= exec
->lexicalGlobalObject();
103 ScopeChain
scopeChain(globalObject
, globalObject
->globalData(), globalObject
, exec
->globalThisValue());
104 return new (exec
) JSFunction(exec
, function
, scopeChain
.node());
107 // ECMA 15.3.2 The Function Constructor
108 JSObject
* constructFunction(ExecState
* exec
, const ArgList
& args
)
110 return constructFunction(exec
, args
, Identifier(exec
, "anonymous"), UString(), 1);