2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2013 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 "ExceptionHelpers.h"
26 #include "FunctionPrototype.h"
27 #include "JSFunction.h"
28 #include "JSGlobalObject.h"
32 #include "JSCInlines.h"
34 #include <wtf/text/StringBuilder.h>
38 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(FunctionConstructor
);
40 const ClassInfo
FunctionConstructor::s_info
= { "Function", &Base::s_info
, 0, CREATE_METHOD_TABLE(FunctionConstructor
) };
42 FunctionConstructor::FunctionConstructor(VM
& vm
, Structure
* structure
)
43 : InternalFunction(vm
, structure
)
47 void FunctionConstructor::finishCreation(VM
& vm
, FunctionPrototype
* functionPrototype
)
49 Base::finishCreation(vm
, functionPrototype
->classInfo()->className
);
50 putDirectWithoutTransition(vm
, vm
.propertyNames
->prototype
, functionPrototype
, DontEnum
| DontDelete
| ReadOnly
);
52 // Number of arguments for constructor
53 putDirectWithoutTransition(vm
, vm
.propertyNames
->length
, jsNumber(1), ReadOnly
| DontDelete
| DontEnum
);
56 static EncodedJSValue JSC_HOST_CALL
constructWithFunctionConstructor(ExecState
* exec
)
59 return JSValue::encode(constructFunction(exec
, asInternalFunction(exec
->callee())->globalObject(), args
));
62 ConstructType
FunctionConstructor::getConstructData(JSCell
*, ConstructData
& constructData
)
64 constructData
.native
.function
= constructWithFunctionConstructor
;
65 return ConstructTypeHost
;
68 static EncodedJSValue JSC_HOST_CALL
callFunctionConstructor(ExecState
* exec
)
71 return JSValue::encode(constructFunction(exec
, asInternalFunction(exec
->callee())->globalObject(), args
));
74 // ECMA 15.3.1 The Function Constructor Called as a Function
75 CallType
FunctionConstructor::getCallData(JSCell
*, CallData
& callData
)
77 callData
.native
.function
= callFunctionConstructor
;
81 // ECMA 15.3.2 The Function Constructor
82 JSObject
* constructFunction(ExecState
* exec
, JSGlobalObject
* globalObject
, const ArgList
& args
, const Identifier
& functionName
, const String
& sourceURL
, const TextPosition
& position
)
84 if (!globalObject
->evalEnabled())
85 return exec
->vm().throwException(exec
, createEvalError(exec
, globalObject
->evalDisabledErrorMessage()));
86 return constructFunctionSkippingEvalEnabledCheck(exec
, globalObject
, args
, functionName
, sourceURL
, position
);
89 JSObject
* constructFunctionSkippingEvalEnabledCheck(
90 ExecState
* exec
, JSGlobalObject
* globalObject
, const ArgList
& args
,
91 const Identifier
& functionName
, const String
& sourceURL
,
92 const TextPosition
& position
, int overrideLineNumber
)
94 // How we stringify functions is sometimes important for web compatibility.
95 // See https://bugs.webkit.org/show_bug.cgi?id=24350.
98 program
= makeString("{function ", functionName
.string(), "() {\n\n}}");
99 else if (args
.size() == 1)
100 program
= makeString("{function ", functionName
.string(), "() {\n", args
.at(0).toString(exec
)->value(exec
), "\n}}");
102 StringBuilder builder
;
103 builder
.appendLiteral("{function ");
104 builder
.append(functionName
.string());
106 builder
.append(args
.at(0).toString(exec
)->view(exec
));
107 for (size_t i
= 1; i
< args
.size() - 1; i
++) {
108 builder
.appendLiteral(", ");
109 builder
.append(args
.at(i
).toString(exec
)->view(exec
));
111 builder
.appendLiteral(") {\n");
112 builder
.append(args
.at(args
.size() - 1).toString(exec
)->view(exec
));
113 builder
.appendLiteral("\n}}");
114 program
= builder
.toString();
117 SourceCode source
= makeSource(program
, sourceURL
, position
);
118 JSObject
* exception
= nullptr;
119 FunctionExecutable
* function
= FunctionExecutable::fromGlobalCode(functionName
, *exec
, source
, exception
, overrideLineNumber
);
122 return exec
->vm().throwException(exec
, exception
);
125 return JSFunction::create(exec
->vm(), function
, globalObject
);
128 // ECMA 15.3.2 The Function Constructor
129 JSObject
* constructFunction(ExecState
* exec
, JSGlobalObject
* globalObject
, const ArgList
& args
)
131 return constructFunction(exec
, globalObject
, args
, exec
->propertyNames().anonymous
, String(), TextPosition::minimumPosition());