]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/FunctionConstructor.cpp
JavaScriptCore-903.tar.gz
[apple/javascriptcore.git] / runtime / FunctionConstructor.cpp
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
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"
f9bf01c6 32#include "Parser.h"
14957cd0
A
33#include "UStringBuilder.h"
34#include "UStringConcatenate.h"
9dae56ea
A
35
36namespace JSC {
37
38ASSERT_CLASS_FITS_IN_CELL(FunctionConstructor);
39
14957cd0
A
40FunctionConstructor::FunctionConstructor(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, FunctionPrototype* functionPrototype)
41 : InternalFunction(&exec->globalData(), globalObject, structure, Identifier(exec, functionPrototype->classInfo()->className))
9dae56ea 42{
14957cd0 43 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, functionPrototype, DontEnum | DontDelete | ReadOnly);
9dae56ea
A
44
45 // Number of arguments for constructor
14957cd0 46 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontDelete | DontEnum);
9dae56ea
A
47}
48
14957cd0 49static EncodedJSValue JSC_HOST_CALL constructWithFunctionConstructor(ExecState* exec)
9dae56ea 50{
14957cd0
A
51 ArgList args(exec);
52 return JSValue::encode(constructFunction(exec, asInternalFunction(exec->callee())->globalObject(), args));
9dae56ea
A
53}
54
55ConstructType FunctionConstructor::getConstructData(ConstructData& constructData)
56{
57 constructData.native.function = constructWithFunctionConstructor;
58 return ConstructTypeHost;
59}
60
14957cd0 61static EncodedJSValue JSC_HOST_CALL callFunctionConstructor(ExecState* exec)
9dae56ea 62{
14957cd0
A
63 ArgList args(exec);
64 return JSValue::encode(constructFunction(exec, asInternalFunction(exec->callee())->globalObject(), args));
9dae56ea
A
65}
66
67// ECMA 15.3.1 The Function Constructor Called as a Function
68CallType FunctionConstructor::getCallData(CallData& callData)
69{
70 callData.native.function = callFunctionConstructor;
71 return CallTypeHost;
72}
73
9dae56ea 74// ECMA 15.3.2 The Function Constructor
14957cd0
A
75JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, const Identifier& functionName, const UString& sourceURL, int lineNumber)
76{
77 if (!globalObject->isEvalEnabled())
78 return throwError(exec, createEvalError(exec, "Function constructor is disabled"));
79 return constructFunctionSkippingEvalEnabledCheck(exec, globalObject, args, functionName, sourceURL, lineNumber);
80}
81
82JSObject* constructFunctionSkippingEvalEnabledCheck(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, const Identifier& functionName, const UString& sourceURL, int lineNumber)
9dae56ea
A
83{
84 // Functions need to have a space following the opening { due to for web compatibility
85 // see https://bugs.webkit.org/show_bug.cgi?id=24350
86 // We also need \n before the closing } to handle // comments at the end of the last line
87 UString program;
88 if (args.isEmpty())
89 program = "(function() { \n})";
90 else if (args.size() == 1)
14957cd0 91 program = makeUString("(function() { ", args.at(0).toString(exec), "\n})");
9dae56ea 92 else {
14957cd0 93 UStringBuilder builder;
f9bf01c6
A
94 builder.append("(function(");
95 builder.append(args.at(0).toString(exec));
96 for (size_t i = 1; i < args.size() - 1; i++) {
97 builder.append(",");
98 builder.append(args.at(i).toString(exec));
99 }
100 builder.append(") { ");
101 builder.append(args.at(args.size() - 1).toString(exec));
102 builder.append("\n})");
14957cd0 103 program = builder.toUString();
9dae56ea
A
104 }
105
14957cd0 106 JSGlobalData& globalData = globalObject->globalData();
9dae56ea 107 SourceCode source = makeSource(program, sourceURL, lineNumber);
14957cd0
A
108 JSObject* exception = 0;
109 FunctionExecutable* function = FunctionExecutable::fromGlobalCode(functionName, exec, exec->dynamicGlobalObject()->debugger(), source, &exception);
110 if (!function) {
111 ASSERT(exception);
112 return throwError(exec, exception);
113 }
9dae56ea 114
14957cd0
A
115 ScopeChainNode* scopeChain = new (exec) ScopeChainNode(0, globalObject, &globalData, globalObject, exec->globalThisValue());
116 return new (exec) JSFunction(exec, function, scopeChain);
9dae56ea
A
117}
118
119// ECMA 15.3.2 The Function Constructor
14957cd0 120JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args)
9dae56ea 121{
14957cd0 122 return constructFunction(exec, globalObject, args, Identifier(exec, "anonymous"), UString(), 1);
9dae56ea
A
123}
124
125} // namespace JSC