weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1999-2001 Harri Porten (porten@kde.org) |
mark.lam@apple.com | 284f456 | 2016-08-30 20:54:54 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2008, 2013, 2016 Apple Inc. All rights reserved. |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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 |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | #include "FunctionConstructor.h" |
| 23 | |
barraclough@apple.com | 9c099f9 | 2010-06-06 23:34:36 +0000 | [diff] [blame] | 24 | #include "ExceptionHelpers.h" |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 25 | #include "FunctionPrototype.h" |
caitp@igalia.com | 04d4f60 | 2016-10-28 16:37:38 +0000 | [diff] [blame] | 26 | #include "JSAsyncFunction.h" |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 27 | #include "JSFunction.h" |
utatane.tea@gmail.com | fb175b3 | 2016-10-01 23:35:17 +0000 | [diff] [blame] | 28 | #include "JSGeneratorFunction.h" |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 29 | #include "JSGlobalObject.h" |
fpizlo@apple.com | fb7eff2 | 2014-02-11 01:45:50 +0000 | [diff] [blame] | 30 | #include "JSCInlines.h" |
benjamin@webkit.org | cff06e4 | 2012-08-30 21:23:51 +0000 | [diff] [blame] | 31 | #include <wtf/text/StringBuilder.h> |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 32 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 33 | namespace JSC { |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 34 | |
andersca@apple.com | 7de5aae | 2013-09-05 20:12:23 +0000 | [diff] [blame] | 35 | STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(FunctionConstructor); |
ggaren@apple.com | fea4353 | 2008-08-17 20:23:49 +0000 | [diff] [blame] | 36 | |
akling@apple.com | 2de49b7 | 2014-07-30 22:26:22 +0000 | [diff] [blame] | 37 | const ClassInfo FunctionConstructor::s_info = { "Function", &Base::s_info, 0, CREATE_METHOD_TABLE(FunctionConstructor) }; |
mhahnenberg@apple.com | 77d198e | 2011-10-05 02:47:42 +0000 | [diff] [blame] | 38 | |
akling@apple.com | 5987552 | 2013-09-30 03:45:30 +0000 | [diff] [blame] | 39 | FunctionConstructor::FunctionConstructor(VM& vm, Structure* structure) |
| 40 | : InternalFunction(vm, structure) |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 41 | { |
mhahnenberg@apple.com | 7317a7f | 2011-09-09 21:43:14 +0000 | [diff] [blame] | 42 | } |
| 43 | |
akling@apple.com | 2255806 | 2013-09-26 21:43:49 +0000 | [diff] [blame] | 44 | void FunctionConstructor::finishCreation(VM& vm, FunctionPrototype* functionPrototype) |
mhahnenberg@apple.com | 7317a7f | 2011-09-09 21:43:14 +0000 | [diff] [blame] | 45 | { |
akling@apple.com | 2255806 | 2013-09-26 21:43:49 +0000 | [diff] [blame] | 46 | Base::finishCreation(vm, functionPrototype->classInfo()->className); |
commit-queue@webkit.org | 5fb8032 | 2014-04-15 17:46:42 +0000 | [diff] [blame] | 47 | putDirectWithoutTransition(vm, vm.propertyNames->prototype, functionPrototype, DontEnum | DontDelete | ReadOnly); |
commit-queue@webkit.org | 8ff356d | 2017-04-11 16:38:43 +0000 | [diff] [blame] | 48 | putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), ReadOnly | DontEnum); |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 49 | } |
| 50 | |
barraclough@apple.com | 11d351a | 2010-06-04 21:38:38 +0000 | [diff] [blame] | 51 | static EncodedJSValue JSC_HOST_CALL constructWithFunctionConstructor(ExecState* exec) |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 52 | { |
barraclough@apple.com | 11d351a | 2010-06-04 21:38:38 +0000 | [diff] [blame] | 53 | ArgList args(exec); |
sbarati@apple.com | 9f55abf | 2016-12-02 02:38:23 +0000 | [diff] [blame] | 54 | return JSValue::encode(constructFunction(exec, asInternalFunction(exec->jsCallee())->globalObject(), args, FunctionConstructionMode::Function, exec->newTarget())); |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 55 | } |
| 56 | |
mhahnenberg@apple.com | 79c8e6e | 2011-10-08 23:26:41 +0000 | [diff] [blame] | 57 | ConstructType FunctionConstructor::getConstructData(JSCell*, ConstructData& constructData) |
| 58 | { |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 59 | constructData.native.function = constructWithFunctionConstructor; |
utatane.tea@gmail.com | f76f1b4 | 2016-03-05 17:01:04 +0000 | [diff] [blame] | 60 | return ConstructType::Host; |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 61 | } |
| 62 | |
barraclough@apple.com | 99ff343 | 2010-06-03 20:00:18 +0000 | [diff] [blame] | 63 | static EncodedJSValue JSC_HOST_CALL callFunctionConstructor(ExecState* exec) |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 64 | { |
ggaren@apple.com | fea29f1 | 2010-05-29 06:33:05 +0000 | [diff] [blame] | 65 | ArgList args(exec); |
sbarati@apple.com | 9f55abf | 2016-12-02 02:38:23 +0000 | [diff] [blame] | 66 | return JSValue::encode(constructFunction(exec, asInternalFunction(exec->jsCallee())->globalObject(), args)); |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // ECMA 15.3.1 The Function Constructor Called as a Function |
mhahnenberg@apple.com | 2413eb8 | 2011-09-27 22:46:51 +0000 | [diff] [blame] | 70 | CallType FunctionConstructor::getCallData(JSCell*, CallData& callData) |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 71 | { |
| 72 | callData.native.function = callFunctionConstructor; |
utatane.tea@gmail.com | f76f1b4 | 2016-03-05 17:01:04 +0000 | [diff] [blame] | 73 | return CallType::Host; |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // ECMA 15.3.2 The Function Constructor |
utatane.tea@gmail.com | c4ec8e2 | 2016-12-26 06:35:07 +0000 | [diff] [blame] | 77 | 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) |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 78 | { |
mark.lam@apple.com | 284f456 | 2016-08-30 20:54:54 +0000 | [diff] [blame] | 79 | VM& vm = exec->vm(); |
| 80 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 81 | |
yurys@chromium.org | aa17fdc | 2011-05-19 11:47:02 +0000 | [diff] [blame] | 82 | if (!globalObject->evalEnabled()) |
mark.lam@apple.com | 284f456 | 2016-08-30 20:54:54 +0000 | [diff] [blame] | 83 | return throwException(exec, scope, createEvalError(exec, globalObject->evalDisabledErrorMessage())); |
mark.lam@apple.com | e029e1f | 2016-11-22 02:05:27 +0000 | [diff] [blame] | 84 | scope.release(); |
utatane.tea@gmail.com | c4ec8e2 | 2016-12-26 06:35:07 +0000 | [diff] [blame] | 85 | return constructFunctionSkippingEvalEnabledCheck(exec, globalObject, args, functionName, sourceOrigin, sourceURL, position, -1, functionConstructionMode, newTarget); |
abarth@webkit.org | 1973332 | 2011-05-09 23:05:54 +0000 | [diff] [blame] | 86 | } |
| 87 | |
ggaren@apple.com | 7a6a0f6 | 2015-03-26 23:12:39 +0000 | [diff] [blame] | 88 | JSObject* constructFunctionSkippingEvalEnabledCheck( |
| 89 | ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, |
utatane.tea@gmail.com | c4ec8e2 | 2016-12-26 06:35:07 +0000 | [diff] [blame] | 90 | const Identifier& functionName, const SourceOrigin& sourceOrigin, const String& sourceURL, |
keith_miller@apple.com | 0237313 | 2016-01-14 20:45:48 +0000 | [diff] [blame] | 91 | const TextPosition& position, int overrideLineNumber, FunctionConstructionMode functionConstructionMode, JSValue newTarget) |
abarth@webkit.org | 1973332 | 2011-05-09 23:05:54 +0000 | [diff] [blame] | 92 | { |
mark.lam@apple.com | 284f456 | 2016-08-30 20:54:54 +0000 | [diff] [blame] | 93 | VM& vm = exec->vm(); |
| 94 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 95 | |
caitp@igalia.com | 04d4f60 | 2016-10-28 16:37:38 +0000 | [diff] [blame] | 96 | const char* prefix = nullptr; |
| 97 | Structure* structure = nullptr; |
| 98 | switch (functionConstructionMode) { |
| 99 | case FunctionConstructionMode::Function: |
| 100 | structure = globalObject->functionStructure(); |
| 101 | prefix = "{function "; |
| 102 | break; |
| 103 | case FunctionConstructionMode::Generator: |
| 104 | structure = globalObject->generatorFunctionStructure(); |
| 105 | prefix = "{function *"; |
| 106 | break; |
| 107 | case FunctionConstructionMode::Async: |
| 108 | structure = globalObject->asyncFunctionStructure(); |
| 109 | prefix = "{async function "; |
| 110 | break; |
| 111 | } |
| 112 | |
ggaren@apple.com | ddc6f10 | 2015-03-20 20:12:10 +0000 | [diff] [blame] | 113 | // How we stringify functions is sometimes important for web compatibility. |
| 114 | // See https://bugs.webkit.org/show_bug.cgi?id=24350. |
benjamin@webkit.org | cff06e4 | 2012-08-30 21:23:51 +0000 | [diff] [blame] | 115 | String program; |
weinig@apple.com | 9b5bf06 | 2008-11-20 23:23:59 +0000 | [diff] [blame] | 116 | if (args.isEmpty()) |
caitp@igalia.com | 04d4f60 | 2016-10-28 16:37:38 +0000 | [diff] [blame] | 117 | program = makeString(prefix, functionName.string(), "() {\n\n}}"); |
mark.lam@apple.com | e029e1f | 2016-11-22 02:05:27 +0000 | [diff] [blame] | 118 | else if (args.size() == 1) { |
| 119 | auto body = args.at(0).toWTFString(exec); |
| 120 | RETURN_IF_EXCEPTION(scope, nullptr); |
| 121 | program = makeString(prefix, functionName.string(), "() {\n", body, "\n}}"); |
| 122 | } else { |
benjamin@webkit.org | cff06e4 | 2012-08-30 21:23:51 +0000 | [diff] [blame] | 123 | StringBuilder builder; |
caitp@igalia.com | 04d4f60 | 2016-10-28 16:37:38 +0000 | [diff] [blame] | 124 | builder.append(prefix); |
ggaren@apple.com | ddc6f10 | 2015-03-20 20:12:10 +0000 | [diff] [blame] | 125 | builder.append(functionName.string()); |
ggaren@apple.com | 5ae0655 | 2015-03-26 23:41:16 +0000 | [diff] [blame] | 126 | builder.append('('); |
mark.lam@apple.com | 110cbbd | 2016-11-16 00:19:54 +0000 | [diff] [blame] | 127 | auto viewWithString = args.at(0).toString(exec)->viewWithUnderlyingString(*exec); |
| 128 | RETURN_IF_EXCEPTION(scope, nullptr); |
| 129 | builder.append(viewWithString.view); |
barraclough@apple.com | 1f0cbd2 | 2009-12-14 00:27:07 +0000 | [diff] [blame] | 130 | for (size_t i = 1; i < args.size() - 1; i++) { |
ggaren@apple.com | 5ae0655 | 2015-03-26 23:41:16 +0000 | [diff] [blame] | 131 | builder.appendLiteral(", "); |
mark.lam@apple.com | 110cbbd | 2016-11-16 00:19:54 +0000 | [diff] [blame] | 132 | auto viewWithString = args.at(i).toString(exec)->viewWithUnderlyingString(*exec); |
| 133 | RETURN_IF_EXCEPTION(scope, nullptr); |
| 134 | builder.append(viewWithString.view); |
barraclough@apple.com | 1f0cbd2 | 2009-12-14 00:27:07 +0000 | [diff] [blame] | 135 | } |
ggaren@apple.com | ddc6f10 | 2015-03-20 20:12:10 +0000 | [diff] [blame] | 136 | builder.appendLiteral(") {\n"); |
mark.lam@apple.com | 110cbbd | 2016-11-16 00:19:54 +0000 | [diff] [blame] | 137 | viewWithString = args.at(args.size() - 1).toString(exec)->viewWithUnderlyingString(*exec); |
| 138 | RETURN_IF_EXCEPTION(scope, nullptr); |
| 139 | builder.append(viewWithString.view); |
ggaren@apple.com | ddc6f10 | 2015-03-20 20:12:10 +0000 | [diff] [blame] | 140 | builder.appendLiteral("\n}}"); |
benjamin@webkit.org | cff06e4 | 2012-08-30 21:23:51 +0000 | [diff] [blame] | 141 | program = builder.toString(); |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 142 | } |
| 143 | |
utatane.tea@gmail.com | c4ec8e2 | 2016-12-26 06:35:07 +0000 | [diff] [blame] | 144 | SourceCode source = makeSource(program, sourceOrigin, sourceURL, position); |
rniwa@webkit.org | 90bcb6c | 2015-03-07 17:38:40 +0000 | [diff] [blame] | 145 | JSObject* exception = nullptr; |
ggaren@apple.com | 8e8bac4 | 2015-03-26 23:35:47 +0000 | [diff] [blame] | 146 | FunctionExecutable* function = FunctionExecutable::fromGlobalCode(functionName, *exec, source, exception, overrideLineNumber); |
barraclough@apple.com | 40b4ad3 | 2010-07-14 00:27:13 +0000 | [diff] [blame] | 147 | if (!function) { |
| 148 | ASSERT(exception); |
mark.lam@apple.com | 284f456 | 2016-08-30 20:54:54 +0000 | [diff] [blame] | 149 | return throwException(exec, scope, exception); |
barraclough@apple.com | 40b4ad3 | 2010-07-14 00:27:13 +0000 | [diff] [blame] | 150 | } |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 151 | |
caitp@igalia.com | 04d4f60 | 2016-10-28 16:37:38 +0000 | [diff] [blame] | 152 | Structure* subclassStructure = InternalFunction::createSubclassStructure(exec, newTarget, structure); |
mark.lam@apple.com | e1ab17c | 2016-09-26 19:11:17 +0000 | [diff] [blame] | 153 | RETURN_IF_EXCEPTION(scope, nullptr); |
keith_miller@apple.com | 0237313 | 2016-01-14 20:45:48 +0000 | [diff] [blame] | 154 | |
caitp@igalia.com | 04d4f60 | 2016-10-28 16:37:38 +0000 | [diff] [blame] | 155 | switch (functionConstructionMode) { |
| 156 | case FunctionConstructionMode::Function: |
| 157 | return JSFunction::create(vm, function, globalObject->globalScope(), subclassStructure); |
| 158 | case FunctionConstructionMode::Generator: |
utatane.tea@gmail.com | fb175b3 | 2016-10-01 23:35:17 +0000 | [diff] [blame] | 159 | return JSGeneratorFunction::create(vm, function, globalObject->globalScope(), subclassStructure); |
caitp@igalia.com | 04d4f60 | 2016-10-28 16:37:38 +0000 | [diff] [blame] | 160 | case FunctionConstructionMode::Async: |
| 161 | return JSAsyncFunction::create(vm, function, globalObject->globalScope(), subclassStructure); |
| 162 | } |
| 163 | |
| 164 | ASSERT_NOT_REACHED(); |
| 165 | return nullptr; |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // ECMA 15.3.2 The Function Constructor |
keith_miller@apple.com | 0237313 | 2016-01-14 20:45:48 +0000 | [diff] [blame] | 169 | JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, FunctionConstructionMode functionConstructionMode, JSValue newTarget) |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 170 | { |
utatane.tea@gmail.com | c4ec8e2 | 2016-12-26 06:35:07 +0000 | [diff] [blame] | 171 | return constructFunction(exec, globalObject, args, exec->propertyNames().anonymous, exec->callerSourceOrigin(), String(), TextPosition(), functionConstructionMode, newTarget); |
weinig@apple.com | 2ce2a7f | 2008-06-28 21:22:01 +0000 | [diff] [blame] | 172 | } |
| 173 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 174 | } // namespace JSC |