]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/FunctionConstructor.cpp
JavaScriptCore-576.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"
9dae56ea
A
25#include "FunctionPrototype.h"
26#include "JSFunction.h"
27#include "JSGlobalObject.h"
28#include "JSString.h"
9dae56ea
A
29#include "Lexer.h"
30#include "Nodes.h"
f9bf01c6
A
31#include "Parser.h"
32#include "StringBuilder.h"
9dae56ea
A
33
34namespace JSC {
35
36ASSERT_CLASS_FITS_IN_CELL(FunctionConstructor);
37
f9bf01c6 38FunctionConstructor::FunctionConstructor(ExecState* exec, NonNullPassRefPtr<Structure> structure, FunctionPrototype* functionPrototype)
9dae56ea
A
39 : InternalFunction(&exec->globalData(), structure, Identifier(exec, functionPrototype->classInfo()->className))
40{
41 putDirectWithoutTransition(exec->propertyNames().prototype, functionPrototype, DontEnum | DontDelete | ReadOnly);
42
43 // Number of arguments for constructor
44 putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 1), ReadOnly | DontDelete | DontEnum);
45}
46
47static JSObject* constructWithFunctionConstructor(ExecState* exec, JSObject*, const ArgList& args)
48{
49 return constructFunction(exec, args);
50}
51
52ConstructType FunctionConstructor::getConstructData(ConstructData& constructData)
53{
54 constructData.native.function = constructWithFunctionConstructor;
55 return ConstructTypeHost;
56}
57
ba379fdc 58static JSValue JSC_HOST_CALL callFunctionConstructor(ExecState* exec, JSObject*, JSValue, const ArgList& args)
9dae56ea
A
59{
60 return constructFunction(exec, args);
61}
62
63// ECMA 15.3.1 The Function Constructor Called as a Function
64CallType FunctionConstructor::getCallData(CallData& callData)
65{
66 callData.native.function = callFunctionConstructor;
67 return CallTypeHost;
68}
69
9dae56ea
A
70// ECMA 15.3.2 The Function Constructor
71JSObject* constructFunction(ExecState* exec, const ArgList& args, const Identifier& functionName, const UString& sourceURL, int lineNumber)
72{
73 // Functions need to have a space following the opening { due to for web compatibility
74 // see https://bugs.webkit.org/show_bug.cgi?id=24350
75 // We also need \n before the closing } to handle // comments at the end of the last line
76 UString program;
77 if (args.isEmpty())
78 program = "(function() { \n})";
79 else if (args.size() == 1)
f9bf01c6 80 program = makeString("(function() { ", args.at(0).toString(exec), "\n})");
9dae56ea 81 else {
f9bf01c6
A
82 StringBuilder builder;
83 builder.append("(function(");
84 builder.append(args.at(0).toString(exec));
85 for (size_t i = 1; i < args.size() - 1; i++) {
86 builder.append(",");
87 builder.append(args.at(i).toString(exec));
88 }
89 builder.append(") { ");
90 builder.append(args.at(args.size() - 1).toString(exec));
91 builder.append("\n})");
92 program = builder.release();
9dae56ea
A
93 }
94
95 int errLine;
96 UString errMsg;
97 SourceCode source = makeSource(program, sourceURL, lineNumber);
f9bf01c6
A
98 RefPtr<FunctionExecutable> function = FunctionExecutable::fromGlobalCode(functionName, exec, exec->dynamicGlobalObject()->debugger(), source, &errLine, &errMsg);
99 if (!function)
9dae56ea
A
100 return throwError(exec, SyntaxError, errMsg, errLine, source.provider()->asID(), source.provider()->url());
101
102 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
f9bf01c6
A
103 ScopeChain scopeChain(globalObject, globalObject->globalData(), globalObject, exec->globalThisValue());
104 return new (exec) JSFunction(exec, function, scopeChain.node());
9dae56ea
A
105}
106
107// ECMA 15.3.2 The Function Constructor
108JSObject* constructFunction(ExecState* exec, const ArgList& args)
109{
110 return constructFunction(exec, args, Identifier(exec, "anonymous"), UString(), 1);
111}
112
113} // namespace JSC