]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/FunctionConstructor.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / FunctionConstructor.cpp
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
81345200 3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2013 Apple Inc. All rights reserved.
9dae56ea
A
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
f9bf01c6 24#include "Debugger.h"
14957cd0 25#include "ExceptionHelpers.h"
9dae56ea
A
26#include "FunctionPrototype.h"
27#include "JSFunction.h"
28#include "JSGlobalObject.h"
29#include "JSString.h"
9dae56ea
A
30#include "Lexer.h"
31#include "Nodes.h"
81345200 32#include "JSCInlines.h"
f9bf01c6 33#include "Parser.h"
93a37866 34#include <wtf/text/StringBuilder.h>
9dae56ea
A
35
36namespace JSC {
37
81345200 38STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(FunctionConstructor);
9dae56ea 39
ed1e77d3 40const ClassInfo FunctionConstructor::s_info = { "Function", &Base::s_info, 0, CREATE_METHOD_TABLE(FunctionConstructor) };
6fe7ccc8 41
81345200
A
42FunctionConstructor::FunctionConstructor(VM& vm, Structure* structure)
43 : InternalFunction(vm, structure)
6fe7ccc8
A
44{
45}
46
81345200 47void FunctionConstructor::finishCreation(VM& vm, FunctionPrototype* functionPrototype)
9dae56ea 48{
81345200
A
49 Base::finishCreation(vm, functionPrototype->classInfo()->className);
50 putDirectWithoutTransition(vm, vm.propertyNames->prototype, functionPrototype, DontEnum | DontDelete | ReadOnly);
9dae56ea
A
51
52 // Number of arguments for constructor
81345200 53 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), ReadOnly | DontDelete | DontEnum);
9dae56ea
A
54}
55
14957cd0 56static EncodedJSValue JSC_HOST_CALL constructWithFunctionConstructor(ExecState* exec)
9dae56ea 57{
14957cd0
A
58 ArgList args(exec);
59 return JSValue::encode(constructFunction(exec, asInternalFunction(exec->callee())->globalObject(), args));
9dae56ea
A
60}
61
6fe7ccc8 62ConstructType FunctionConstructor::getConstructData(JSCell*, ConstructData& constructData)
9dae56ea
A
63{
64 constructData.native.function = constructWithFunctionConstructor;
65 return ConstructTypeHost;
66}
67
14957cd0 68static EncodedJSValue JSC_HOST_CALL callFunctionConstructor(ExecState* exec)
9dae56ea 69{
14957cd0
A
70 ArgList args(exec);
71 return JSValue::encode(constructFunction(exec, asInternalFunction(exec->callee())->globalObject(), args));
9dae56ea
A
72}
73
74// ECMA 15.3.1 The Function Constructor Called as a Function
6fe7ccc8 75CallType FunctionConstructor::getCallData(JSCell*, CallData& callData)
9dae56ea
A
76{
77 callData.native.function = callFunctionConstructor;
78 return CallTypeHost;
79}
80
9dae56ea 81// ECMA 15.3.2 The Function Constructor
93a37866 82JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, const Identifier& functionName, const String& sourceURL, const TextPosition& position)
14957cd0 83{
6fe7ccc8 84 if (!globalObject->evalEnabled())
81345200 85 return exec->vm().throwException(exec, createEvalError(exec, globalObject->evalDisabledErrorMessage()));
6fe7ccc8 86 return constructFunctionSkippingEvalEnabledCheck(exec, globalObject, args, functionName, sourceURL, position);
14957cd0
A
87}
88
ed1e77d3
A
89JSObject* constructFunctionSkippingEvalEnabledCheck(
90 ExecState* exec, JSGlobalObject* globalObject, const ArgList& args,
91 const Identifier& functionName, const String& sourceURL,
92 const TextPosition& position, int overrideLineNumber)
9dae56ea 93{
ed1e77d3
A
94 // How we stringify functions is sometimes important for web compatibility.
95 // See https://bugs.webkit.org/show_bug.cgi?id=24350.
93a37866 96 String program;
9dae56ea 97 if (args.isEmpty())
ed1e77d3 98 program = makeString("{function ", functionName.string(), "() {\n\n}}");
9dae56ea 99 else if (args.size() == 1)
ed1e77d3 100 program = makeString("{function ", functionName.string(), "() {\n", args.at(0).toString(exec)->value(exec), "\n}}");
9dae56ea 101 else {
93a37866 102 StringBuilder builder;
ed1e77d3
A
103 builder.appendLiteral("{function ");
104 builder.append(functionName.string());
105 builder.append('(');
106 builder.append(args.at(0).toString(exec)->view(exec));
f9bf01c6 107 for (size_t i = 1; i < args.size() - 1; i++) {
ed1e77d3
A
108 builder.appendLiteral(", ");
109 builder.append(args.at(i).toString(exec)->view(exec));
f9bf01c6 110 }
ed1e77d3
A
111 builder.appendLiteral(") {\n");
112 builder.append(args.at(args.size() - 1).toString(exec)->view(exec));
113 builder.appendLiteral("\n}}");
93a37866 114 program = builder.toString();
9dae56ea
A
115 }
116
6fe7ccc8 117 SourceCode source = makeSource(program, sourceURL, position);
ed1e77d3
A
118 JSObject* exception = nullptr;
119 FunctionExecutable* function = FunctionExecutable::fromGlobalCode(functionName, *exec, source, exception, overrideLineNumber);
14957cd0
A
120 if (!function) {
121 ASSERT(exception);
81345200 122 return exec->vm().throwException(exec, exception);
14957cd0 123 }
9dae56ea 124
81345200 125 return JSFunction::create(exec->vm(), function, globalObject);
9dae56ea
A
126}
127
128// ECMA 15.3.2 The Function Constructor
14957cd0 129JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args)
9dae56ea 130{
93a37866 131 return constructFunction(exec, globalObject, args, exec->propertyNames().anonymous, String(), TextPosition::minimumPosition());
9dae56ea
A
132}
133
134} // namespace JSC