diff options
Diffstat (limited to 'Source/JavaScriptCore/runtime/FunctionConstructor.cpp')
-rw-r--r-- | Source/JavaScriptCore/runtime/FunctionConstructor.cpp | 120 |
1 files changed, 83 insertions, 37 deletions
diff --git a/Source/JavaScriptCore/runtime/FunctionConstructor.cpp b/Source/JavaScriptCore/runtime/FunctionConstructor.cpp index 53de63271..ced3658ea 100644 --- a/Source/JavaScriptCore/runtime/FunctionConstructor.cpp +++ b/Source/JavaScriptCore/runtime/FunctionConstructor.cpp @@ -1,6 +1,6 @@ /* * Copyright (C) 1999-2001 Harri Porten (porten@kde.org) - * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2013 Apple Inc. All rights reserved. + * Copyright (C) 2003-2008, 2013, 2016 Apple Inc. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -21,23 +21,20 @@ #include "config.h" #include "FunctionConstructor.h" -#include "Debugger.h" #include "ExceptionHelpers.h" #include "FunctionPrototype.h" +#include "JSAsyncFunction.h" #include "JSFunction.h" +#include "JSGeneratorFunction.h" #include "JSGlobalObject.h" -#include "JSString.h" -#include "Lexer.h" -#include "Nodes.h" -#include "Operations.h" -#include "Parser.h" +#include "JSCInlines.h" #include <wtf/text/StringBuilder.h> namespace JSC { STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(FunctionConstructor); -const ClassInfo FunctionConstructor::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(FunctionConstructor) }; +const ClassInfo FunctionConstructor::s_info = { "Function", &Base::s_info, 0, CREATE_METHOD_TABLE(FunctionConstructor) }; FunctionConstructor::FunctionConstructor(VM& vm, Structure* structure) : InternalFunction(vm, structure) @@ -56,75 +53,124 @@ void FunctionConstructor::finishCreation(VM& vm, FunctionPrototype* functionProt static EncodedJSValue JSC_HOST_CALL constructWithFunctionConstructor(ExecState* exec) { ArgList args(exec); - return JSValue::encode(constructFunction(exec, asInternalFunction(exec->callee())->globalObject(), args)); + return JSValue::encode(constructFunction(exec, asInternalFunction(exec->jsCallee())->globalObject(), args, FunctionConstructionMode::Function, exec->newTarget())); } ConstructType FunctionConstructor::getConstructData(JSCell*, ConstructData& constructData) { constructData.native.function = constructWithFunctionConstructor; - return ConstructTypeHost; + return ConstructType::Host; } static EncodedJSValue JSC_HOST_CALL callFunctionConstructor(ExecState* exec) { ArgList args(exec); - return JSValue::encode(constructFunction(exec, asInternalFunction(exec->callee())->globalObject(), args)); + return JSValue::encode(constructFunction(exec, asInternalFunction(exec->jsCallee())->globalObject(), args)); } // ECMA 15.3.1 The Function Constructor Called as a Function CallType FunctionConstructor::getCallData(JSCell*, CallData& callData) { callData.native.function = callFunctionConstructor; - return CallTypeHost; + return CallType::Host; } // ECMA 15.3.2 The Function Constructor -JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, const Identifier& functionName, const String& sourceURL, const TextPosition& position) +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) { + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + if (!globalObject->evalEnabled()) - return exec->vm().throwException(exec, createEvalError(exec, globalObject->evalDisabledErrorMessage())); - return constructFunctionSkippingEvalEnabledCheck(exec, globalObject, args, functionName, sourceURL, position); + return throwException(exec, scope, createEvalError(exec, globalObject->evalDisabledErrorMessage())); + scope.release(); + return constructFunctionSkippingEvalEnabledCheck(exec, globalObject, args, functionName, sourceOrigin, sourceURL, position, -1, functionConstructionMode, newTarget); } -JSObject* constructFunctionSkippingEvalEnabledCheck(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, const Identifier& functionName, const String& sourceURL, const TextPosition& position) +JSObject* constructFunctionSkippingEvalEnabledCheck( + ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, + const Identifier& functionName, const SourceOrigin& sourceOrigin, const String& sourceURL, + const TextPosition& position, int overrideLineNumber, FunctionConstructionMode functionConstructionMode, JSValue newTarget) { - // Functions need to have a space following the opening { due to for web compatibility - // see https://bugs.webkit.org/show_bug.cgi?id=24350 - // We also need \n before the closing } to handle // comments at the end of the last line + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + const char* prefix = nullptr; + Structure* structure = nullptr; + switch (functionConstructionMode) { + case FunctionConstructionMode::Function: + structure = globalObject->functionStructure(); + prefix = "{function "; + break; + case FunctionConstructionMode::Generator: + structure = globalObject->generatorFunctionStructure(); + prefix = "{function *"; + break; + case FunctionConstructionMode::Async: + structure = globalObject->asyncFunctionStructure(); + prefix = "{async function "; + break; + } + + // How we stringify functions is sometimes important for web compatibility. + // See https://bugs.webkit.org/show_bug.cgi?id=24350. String program; if (args.isEmpty()) - program = ASCIILiteral("(function() {\n})"); - else if (args.size() == 1) - program = makeString("(function() {", args.at(0).toString(exec)->value(exec), "\n})"); - else { + program = makeString(prefix, functionName.string(), "() {\n\n}}"); + else if (args.size() == 1) { + auto body = args.at(0).toWTFString(exec); + RETURN_IF_EXCEPTION(scope, nullptr); + program = makeString(prefix, functionName.string(), "() {\n", body, "\n}}"); + } else { StringBuilder builder; - builder.appendLiteral("(function("); - builder.append(args.at(0).toString(exec)->value(exec)); + builder.append(prefix); + builder.append(functionName.string()); + builder.append('('); + auto viewWithString = args.at(0).toString(exec)->viewWithUnderlyingString(*exec); + RETURN_IF_EXCEPTION(scope, nullptr); + builder.append(viewWithString.view); for (size_t i = 1; i < args.size() - 1; i++) { - builder.append(','); - builder.append(args.at(i).toString(exec)->value(exec)); + builder.appendLiteral(", "); + auto viewWithString = args.at(i).toString(exec)->viewWithUnderlyingString(*exec); + RETURN_IF_EXCEPTION(scope, nullptr); + builder.append(viewWithString.view); } - builder.appendLiteral(") {"); - builder.append(args.at(args.size() - 1).toString(exec)->value(exec)); - builder.appendLiteral("\n})"); + builder.appendLiteral(") {\n"); + viewWithString = args.at(args.size() - 1).toString(exec)->viewWithUnderlyingString(*exec); + RETURN_IF_EXCEPTION(scope, nullptr); + builder.append(viewWithString.view); + builder.appendLiteral("\n}}"); program = builder.toString(); } - SourceCode source = makeSource(program, sourceURL, position); - JSObject* exception = 0; - FunctionExecutable* function = FunctionExecutable::fromGlobalCode(functionName, exec, exec->vmEntryGlobalObject()->debugger(), source, &exception); + SourceCode source = makeSource(program, sourceOrigin, sourceURL, position); + JSObject* exception = nullptr; + FunctionExecutable* function = FunctionExecutable::fromGlobalCode(functionName, *exec, source, exception, overrideLineNumber); if (!function) { ASSERT(exception); - return exec->vm().throwException(exec, exception); + return throwException(exec, scope, exception); + } + + Structure* subclassStructure = InternalFunction::createSubclassStructure(exec, newTarget, structure); + RETURN_IF_EXCEPTION(scope, nullptr); + + switch (functionConstructionMode) { + case FunctionConstructionMode::Function: + return JSFunction::create(vm, function, globalObject->globalScope(), subclassStructure); + case FunctionConstructionMode::Generator: + return JSGeneratorFunction::create(vm, function, globalObject->globalScope(), subclassStructure); + case FunctionConstructionMode::Async: + return JSAsyncFunction::create(vm, function, globalObject->globalScope(), subclassStructure); } - return JSFunction::create(exec->vm(), function, globalObject); + ASSERT_NOT_REACHED(); + return nullptr; } // ECMA 15.3.2 The Function Constructor -JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args) +JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, FunctionConstructionMode functionConstructionMode, JSValue newTarget) { - return constructFunction(exec, globalObject, args, exec->propertyNames().anonymous, String(), TextPosition::minimumPosition()); + return constructFunction(exec, globalObject, args, exec->propertyNames().anonymous, exec->callerSourceOrigin(), String(), TextPosition(), functionConstructionMode, newTarget); } } // namespace JSC |