]>
Commit | Line | Data |
---|---|---|
b37bf2e1 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 "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 | |
b37bf2e1 A |
165 | int errLine; |
166 | UString errMsg; | |
b5422865 A |
167 | SourceCode source = makeSource(body, sourceURL, lineNumber); |
168 | RefPtr<FunctionBodyNode> functionBody = parser().parse<FunctionBodyNode>(source, &errLine, &errMsg); | |
169 | ||
170 | // debugger code removed | |
b37bf2e1 A |
171 | |
172 | // No program node == syntax error - throw a syntax error | |
173 | if (!functionBody) | |
174 | // We can't return a Completion(Throw) here, so just set the exception | |
175 | // and return it | |
b5422865 | 176 | return throwError(exec, SyntaxError, errMsg, errLine, source.provider()->asID(), source.provider()->url()); |
b37bf2e1 A |
177 | |
178 | ScopeChain scopeChain; | |
179 | scopeChain.push(exec->lexicalGlobalObject()); | |
180 | ||
181 | FunctionImp* fimp = new FunctionImp(exec, functionName, functionBody.get(), scopeChain); | |
182 | ||
183 | // parse parameter list. throw syntax error on illegal identifiers | |
184 | int len = p.size(); | |
185 | const UChar* c = p.data(); | |
186 | int i = 0, params = 0; | |
187 | UString param; | |
188 | while (i < len) { | |
189 | while (*c == ' ' && i < len) | |
190 | c++, i++; | |
191 | if (Lexer::isIdentStart(c->uc)) { // else error | |
192 | param = UString(c, 1); | |
193 | c++, i++; | |
194 | while (i < len && (Lexer::isIdentPart(c->uc))) { | |
195 | param += UString(c, 1); | |
196 | c++, i++; | |
197 | } | |
198 | while (i < len && *c == ' ') | |
199 | c++, i++; | |
200 | if (i == len) { | |
201 | functionBody->parameters().append(Identifier(param)); | |
202 | params++; | |
203 | break; | |
204 | } else if (*c == ',') { | |
205 | functionBody->parameters().append(Identifier(param)); | |
206 | params++; | |
207 | c++, i++; | |
208 | continue; | |
209 | } // else error | |
210 | } | |
211 | return throwError(exec, SyntaxError, "Syntax error in parameter list"); | |
212 | } | |
213 | ||
214 | List consArgs; | |
215 | ||
216 | JSObject* objCons = exec->lexicalGlobalObject()->objectConstructor(); | |
217 | JSObject* prototype = objCons->construct(exec, exec->emptyList()); | |
218 | prototype->putDirect(exec->propertyNames().constructor, fimp, DontEnum | DontDelete | ReadOnly); | |
219 | fimp->putDirect(exec->propertyNames().prototype, prototype, Internal | DontDelete); | |
220 | return fimp; | |
221 | } | |
222 | ||
223 | // ECMA 15.3.2 The Function Constructor | |
224 | JSObject* FunctionObjectImp::construct(ExecState* exec, const List& args) | |
225 | { | |
226 | return construct(exec, args, "anonymous", UString(), 0); | |
227 | } | |
228 | ||
229 | // ECMA 15.3.1 The Function Constructor Called as a Function | |
230 | JSValue* FunctionObjectImp::callAsFunction(ExecState* exec, JSObject*, const List& args) | |
231 | { | |
232 | return construct(exec, args); | |
233 | } | |
234 | ||
235 | } // namespace KJS |