]> git.saurik.com Git - apple/javascriptcore.git/blob - kjs/function_object.cpp
5af2970367d1599858ef1d54e828dfbf9f9c8d55
[apple/javascriptcore.git] / kjs / function_object.cpp
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 "function_object.h"
23
24 #include "JSGlobalObject.h"
25 #include "Parser.h"
26 #include "array_object.h"
27 #include "debugger.h"
28 #include "function.h"
29 #include "internal.h"
30 #include "lexer.h"
31 #include "nodes.h"
32 #include "object.h"
33 #include <stdio.h>
34 #include <string.h>
35 #include <wtf/Assertions.h>
36
37 namespace KJS {
38
39 // ------------------------------ FunctionPrototype -------------------------
40
41 static JSValue* functionProtoFuncToString(ExecState*, JSObject*, const List&);
42 static JSValue* functionProtoFuncApply(ExecState*, JSObject*, const List&);
43 static JSValue* functionProtoFuncCall(ExecState*, JSObject*, const List&);
44
45 FunctionPrototype::FunctionPrototype(ExecState* exec)
46 {
47 static const Identifier* applyPropertyName = new Identifier("apply");
48 static const Identifier* callPropertyName = new Identifier("call");
49
50 putDirect(exec->propertyNames().length, jsNumber(0), DontDelete | ReadOnly | DontEnum);
51
52 putDirectFunction(new PrototypeFunction(exec, this, 0, exec->propertyNames().toString, functionProtoFuncToString), DontEnum);
53 putDirectFunction(new PrototypeFunction(exec, this, 2, *applyPropertyName, functionProtoFuncApply), DontEnum);
54 putDirectFunction(new PrototypeFunction(exec, this, 1, *callPropertyName, functionProtoFuncCall), DontEnum);
55 }
56
57 // ECMA 15.3.4
58 JSValue* FunctionPrototype::callAsFunction(ExecState*, JSObject*, const List&)
59 {
60 return jsUndefined();
61 }
62
63 // Functions
64
65 JSValue* functionProtoFuncToString(ExecState* exec, JSObject* thisObj, const List&)
66 {
67 if (!thisObj || !thisObj->inherits(&InternalFunctionImp::info)) {
68 #ifndef NDEBUG
69 fprintf(stderr,"attempted toString() call on null or non-function object\n");
70 #endif
71 return throwError(exec, TypeError);
72 }
73
74 if (thisObj->inherits(&FunctionImp::info)) {
75 FunctionImp* fi = static_cast<FunctionImp*>(thisObj);
76 return jsString("function " + fi->functionName().ustring() + "(" + fi->body->paramString() + ") " + fi->body->toString());
77 }
78
79 return jsString("function " + static_cast<InternalFunctionImp*>(thisObj)->functionName().ustring() + "() {\n [native code]\n}");
80 }
81
82 JSValue* functionProtoFuncApply(ExecState* exec, JSObject* thisObj, const List& args)
83 {
84 if (!thisObj->implementsCall())
85 return throwError(exec, TypeError);
86
87 JSValue* thisArg = args[0];
88 JSValue* argArray = args[1];
89
90 JSObject* applyThis;
91 if (thisArg->isUndefinedOrNull())
92 applyThis = exec->dynamicGlobalObject();
93 else
94 applyThis = thisArg->toObject(exec);
95
96 List applyArgs;
97 if (!argArray->isUndefinedOrNull()) {
98 if (argArray->isObject() &&
99 (static_cast<JSObject*>(argArray)->inherits(&ArrayInstance::info) ||
100 static_cast<JSObject*>(argArray)->inherits(&Arguments::info))) {
101
102 JSObject* argArrayObj = static_cast<JSObject*>(argArray);
103 unsigned int length = argArrayObj->get(exec, exec->propertyNames().length)->toUInt32(exec);
104 for (unsigned int i = 0; i < length; i++)
105 applyArgs.append(argArrayObj->get(exec, i));
106 } else
107 return throwError(exec, TypeError);
108 }
109
110 return thisObj->call(exec, applyThis, applyArgs);
111 }
112
113 JSValue* functionProtoFuncCall(ExecState* exec, JSObject* thisObj, const List& args)
114 {
115 if (!thisObj->implementsCall())
116 return throwError(exec, TypeError);
117
118 JSValue* thisArg = args[0];
119
120 JSObject* callThis;
121 if (thisArg->isUndefinedOrNull())
122 callThis = exec->dynamicGlobalObject();
123 else
124 callThis = thisArg->toObject(exec);
125
126 List argsTail;
127 args.getSlice(1, argsTail);
128 return thisObj->call(exec, callThis, argsTail);
129 }
130
131 // ------------------------------ FunctionObjectImp ----------------------------
132
133 FunctionObjectImp::FunctionObjectImp(ExecState* exec, FunctionPrototype* functionPrototype)
134 : InternalFunctionImp(functionPrototype, functionPrototype->classInfo()->className)
135 {
136 putDirect(exec->propertyNames().prototype, functionPrototype, DontEnum | DontDelete | ReadOnly);
137
138 // Number of arguments for constructor
139 putDirect(exec->propertyNames().length, jsNumber(1), ReadOnly | DontDelete | DontEnum);
140 }
141
142 bool FunctionObjectImp::implementsConstruct() const
143 {
144 return true;
145 }
146
147 // ECMA 15.3.2 The Function Constructor
148 JSObject* FunctionObjectImp::construct(ExecState* exec, const List& args, const Identifier& functionName, const UString& sourceURL, int lineNumber)
149 {
150 UString p("");
151 UString body;
152 int argsSize = args.size();
153 if (argsSize == 0)
154 body = "";
155 else if (argsSize == 1)
156 body = args[0]->toString(exec);
157 else {
158 p = args[0]->toString(exec);
159 for (int k = 1; k < argsSize - 1; k++)
160 p += "," + args[k]->toString(exec);
161 body = args[argsSize - 1]->toString(exec);
162 }
163
164 // parse the source code
165 int sourceId;
166 int errLine;
167 UString errMsg;
168 RefPtr<FunctionBodyNode> functionBody = parser().parse<FunctionBodyNode>(sourceURL, lineNumber, body.data(), body.size(), &sourceId, &errLine, &errMsg);
169
170 // notify debugger that source has been parsed
171 Debugger* dbg = exec->dynamicGlobalObject()->debugger();
172 if (dbg) {
173 // send empty sourceURL to indicate constructed code
174 bool cont = dbg->sourceParsed(exec, sourceId, UString(), body, lineNumber, errLine, errMsg);
175 if (!cont) {
176 dbg->imp()->abort();
177 return new JSObject();
178 }
179 }
180
181 // No program node == syntax error - throw a syntax error
182 if (!functionBody)
183 // We can't return a Completion(Throw) here, so just set the exception
184 // and return it
185 return throwError(exec, SyntaxError, errMsg, errLine, sourceId, sourceURL);
186
187 ScopeChain scopeChain;
188 scopeChain.push(exec->lexicalGlobalObject());
189
190 FunctionImp* fimp = new FunctionImp(exec, functionName, functionBody.get(), scopeChain);
191
192 // parse parameter list. throw syntax error on illegal identifiers
193 int len = p.size();
194 const UChar* c = p.data();
195 int i = 0, params = 0;
196 UString param;
197 while (i < len) {
198 while (*c == ' ' && i < len)
199 c++, i++;
200 if (Lexer::isIdentStart(c->uc)) { // else error
201 param = UString(c, 1);
202 c++, i++;
203 while (i < len && (Lexer::isIdentPart(c->uc))) {
204 param += UString(c, 1);
205 c++, i++;
206 }
207 while (i < len && *c == ' ')
208 c++, i++;
209 if (i == len) {
210 functionBody->parameters().append(Identifier(param));
211 params++;
212 break;
213 } else if (*c == ',') {
214 functionBody->parameters().append(Identifier(param));
215 params++;
216 c++, i++;
217 continue;
218 } // else error
219 }
220 return throwError(exec, SyntaxError, "Syntax error in parameter list");
221 }
222
223 List consArgs;
224
225 JSObject* objCons = exec->lexicalGlobalObject()->objectConstructor();
226 JSObject* prototype = objCons->construct(exec, exec->emptyList());
227 prototype->putDirect(exec->propertyNames().constructor, fimp, DontEnum | DontDelete | ReadOnly);
228 fimp->putDirect(exec->propertyNames().prototype, prototype, Internal | DontDelete);
229 return fimp;
230 }
231
232 // ECMA 15.3.2 The Function Constructor
233 JSObject* FunctionObjectImp::construct(ExecState* exec, const List& args)
234 {
235 return construct(exec, args, "anonymous", UString(), 0);
236 }
237
238 // ECMA 15.3.1 The Function Constructor Called as a Function
239 JSValue* FunctionObjectImp::callAsFunction(ExecState* exec, JSObject*, const List& args)
240 {
241 return construct(exec, args);
242 }
243
244 } // namespace KJS