2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003-2017 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 "Completion.h"
25 #include "ExceptionHelpers.h"
26 #include "FunctionPrototype.h"
27 #include "JSAsyncFunction.h"
28 #include "JSAsyncGeneratorFunction.h"
29 #include "JSFunction.h"
30 #include "JSGeneratorFunction.h"
31 #include "JSGlobalObject.h"
32 #include "JSCInlines.h"
33 #include <wtf/text/StringBuilder.h>
37 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(FunctionConstructor);
39 const ClassInfo FunctionConstructor::s_info = { "Function", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(FunctionConstructor) };
41 static EncodedJSValue JSC_HOST_CALL constructWithFunctionConstructor(ExecState* exec)
44 return JSValue::encode(constructFunction(exec, jsCast<InternalFunction*>(exec->jsCallee())->globalObject(exec->vm()), args, FunctionConstructionMode::Function, exec->newTarget()));
47 // ECMA 15.3.1 The Function Constructor Called as a Function
48 static EncodedJSValue JSC_HOST_CALL callFunctionConstructor(ExecState* exec)
51 return JSValue::encode(constructFunction(exec, jsCast<InternalFunction*>(exec->jsCallee())->globalObject(exec->vm()), args));
54 FunctionConstructor::FunctionConstructor(VM& vm, Structure* structure)
55 : InternalFunction(vm, structure, callFunctionConstructor, constructWithFunctionConstructor)
59 void FunctionConstructor::finishCreation(VM& vm, FunctionPrototype* functionPrototype)
61 Base::finishCreation(vm, functionPrototype->classInfo()->className);
62 putDirectWithoutTransition(vm, vm.propertyNames->prototype, functionPrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
63 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
66 // ECMA 15.3.2 The Function Constructor
67 JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, const Identifier& functionName, const SourceOrigin& sourceOrigin, const String& sourceURL, const TextPosition& position, FunctionConstructionMode functionConstructionMode, JSValue newTarget)
70 auto scope = DECLARE_THROW_SCOPE(vm);
72 if (!globalObject->evalEnabled())
73 return throwException(exec, scope, createEvalError(exec, globalObject->evalDisabledErrorMessage()));
74 RELEASE_AND_RETURN(scope, constructFunctionSkippingEvalEnabledCheck(exec, globalObject, args, functionName, sourceOrigin, sourceURL, position, -1, functionConstructionMode, newTarget));
77 JSObject* constructFunctionSkippingEvalEnabledCheck(
78 ExecState* exec, JSGlobalObject* globalObject, const ArgList& args,
79 const Identifier& functionName, const SourceOrigin& sourceOrigin, const String& sourceURL,
80 const TextPosition& position, int overrideLineNumber, FunctionConstructionMode functionConstructionMode, JSValue newTarget)
83 auto scope = DECLARE_THROW_SCOPE(vm);
85 const char* prefix = nullptr;
86 switch (functionConstructionMode) {
87 case FunctionConstructionMode::Function:
90 case FunctionConstructionMode::Generator:
91 prefix = "function *";
93 case FunctionConstructionMode::Async:
94 prefix = "async function ";
96 case FunctionConstructionMode::AsyncGenerator:
97 prefix = "async function*";
101 // How we stringify functions is sometimes important for web compatibility.
102 // See https://bugs.webkit.org/show_bug.cgi?id=24350.
104 Optional<int> functionConstructorParametersEndPosition = WTF::nullopt;
106 program = makeString(prefix, functionName.string(), "() {\n\n}");
107 else if (args.size() == 1) {
108 auto body = args.at(0).toWTFString(exec);
109 RETURN_IF_EXCEPTION(scope, nullptr);
110 program = makeString(prefix, functionName.string(), "() {\n", body, "\n}");
112 StringBuilder builder(StringBuilder::OverflowHandler::RecordOverflow);
113 builder.append(prefix);
114 builder.append(functionName.string());
117 auto viewWithString = args.at(0).toString(exec)->viewWithUnderlyingString(exec);
118 RETURN_IF_EXCEPTION(scope, nullptr);
119 builder.append(viewWithString.view);
120 for (size_t i = 1; !builder.hasOverflowed() && i < args.size() - 1; i++) {
121 builder.appendLiteral(", ");
122 auto viewWithString = args.at(i).toString(exec)->viewWithUnderlyingString(exec);
123 RETURN_IF_EXCEPTION(scope, nullptr);
124 builder.append(viewWithString.view);
126 if (builder.hasOverflowed()) {
127 throwOutOfMemoryError(exec, scope);
131 functionConstructorParametersEndPosition = builder.length() + 1;
132 builder.appendLiteral(") {\n");
134 auto body = args.at(args.size() - 1).toString(exec)->viewWithUnderlyingString(exec);
135 RETURN_IF_EXCEPTION(scope, nullptr);
136 builder.append(body.view);
137 builder.appendLiteral("\n}");
138 if (builder.hasOverflowed()) {
139 throwOutOfMemoryError(exec, scope);
142 program = builder.toString();
145 SourceCode source = makeSource(program, sourceOrigin, URL({ }, sourceURL), position);
146 JSObject* exception = nullptr;
147 FunctionExecutable* function = FunctionExecutable::fromGlobalCode(functionName, *exec, source, exception, overrideLineNumber, functionConstructorParametersEndPosition);
150 return throwException(exec, scope, exception);
153 Structure* structure = nullptr;
154 switch (functionConstructionMode) {
155 case FunctionConstructionMode::Function:
156 structure = JSFunction::selectStructureForNewFuncExp(globalObject, function);
158 case FunctionConstructionMode::Generator:
159 structure = globalObject->generatorFunctionStructure();
161 case FunctionConstructionMode::Async:
162 structure = globalObject->asyncFunctionStructure();
164 case FunctionConstructionMode::AsyncGenerator:
165 structure = globalObject->asyncGeneratorFunctionStructure();
169 Structure* subclassStructure = InternalFunction::createSubclassStructure(exec, newTarget, structure);
170 RETURN_IF_EXCEPTION(scope, nullptr);
172 switch (functionConstructionMode) {
173 case FunctionConstructionMode::Function:
174 return JSFunction::create(vm, function, globalObject->globalScope(), subclassStructure);
175 case FunctionConstructionMode::Generator:
176 return JSGeneratorFunction::create(vm, function, globalObject->globalScope(), subclassStructure);
177 case FunctionConstructionMode::Async:
178 return JSAsyncFunction::create(vm, function, globalObject->globalScope(), subclassStructure);
179 case FunctionConstructionMode::AsyncGenerator:
180 return JSAsyncGeneratorFunction::create(vm, function, globalObject->globalScope(), subclassStructure);
183 ASSERT_NOT_REACHED();
187 // ECMA 15.3.2 The Function Constructor
188 JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, FunctionConstructionMode functionConstructionMode, JSValue newTarget)
191 return constructFunction(exec, globalObject, args, vm.propertyNames->anonymous, exec->callerSourceOrigin(), String(), TextPosition(), functionConstructionMode, newTarget);