1 // -*- c-basic-offset: 2 -*-
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
5 * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "function_object.h"
26 #include "JSGlobalObject.h"
27 #include "array_object.h"
36 #include <wtf/Assertions.h>
40 // ------------------------------ FunctionPrototype -------------------------
42 FunctionPrototype::FunctionPrototype(ExecState *exec)
44 static const Identifier* applyPropertyName = new Identifier("apply");
45 static const Identifier* callPropertyName = new Identifier("call");
47 putDirect(exec->propertyNames().length, jsNumber(0), DontDelete | ReadOnly | DontEnum);
48 putDirectFunction(new FunctionProtoFunc(exec, this, FunctionProtoFunc::ToString, 0, exec->propertyNames().toString), DontEnum);
49 putDirectFunction(new FunctionProtoFunc(exec, this, FunctionProtoFunc::Apply, 2, *applyPropertyName), DontEnum);
50 putDirectFunction(new FunctionProtoFunc(exec, this, FunctionProtoFunc::Call, 1, *callPropertyName), DontEnum);
53 FunctionPrototype::~FunctionPrototype()
58 JSValue *FunctionPrototype::callAsFunction(ExecState*, JSObject* /*thisObj*/, const List &/*args*/)
63 // ------------------------------ FunctionProtoFunc -------------------------
65 FunctionProtoFunc::FunctionProtoFunc(ExecState* exec, FunctionPrototype* funcProto, int i, int len, const Identifier& name)
66 : InternalFunctionImp(funcProto, name)
69 putDirect(exec->propertyNames().length, len, DontDelete | ReadOnly | DontEnum);
72 JSValue* FunctionProtoFunc::callAsFunction(ExecState* exec, JSObject* thisObj, const List &args)
74 JSValue* result = NULL;
78 if (!thisObj || !thisObj->inherits(&InternalFunctionImp::info)) {
80 fprintf(stderr,"attempted toString() call on null or non-function object\n");
82 return throwError(exec, TypeError);
84 if (thisObj->inherits(&FunctionImp::info)) {
85 FunctionImp *fi = static_cast<FunctionImp*>(thisObj);
86 return jsString("function " + fi->functionName().ustring() + "(" +
87 fi->body->paramString() + ") " + fi->body->toString());
88 } else if (thisObj->inherits(&InternalFunctionImp::info) &&
89 !static_cast<InternalFunctionImp*>(thisObj)->functionName().isNull()) {
90 result = jsString("\nfunction " + static_cast<InternalFunctionImp*>(thisObj)->functionName().ustring() + "() {\n"
91 " [native code]\n}\n");
93 result = jsString("[function]");
97 JSValue *thisArg = args[0];
98 JSValue *argArray = args[1];
99 JSObject *func = thisObj;
101 if (!func->implementsCall())
102 return throwError(exec, TypeError);
105 if (thisArg->isUndefinedOrNull())
106 applyThis = exec->dynamicInterpreter()->globalObject();
108 applyThis = thisArg->toObject(exec);
111 if (!argArray->isUndefinedOrNull()) {
112 if (argArray->isObject() &&
113 (static_cast<JSObject *>(argArray)->inherits(&ArrayInstance::info) ||
114 static_cast<JSObject *>(argArray)->inherits(&Arguments::info))) {
116 JSObject *argArrayObj = static_cast<JSObject *>(argArray);
117 unsigned int length = argArrayObj->get(exec, exec->propertyNames().length)->toUInt32(exec);
118 for (unsigned int i = 0; i < length; i++)
119 applyArgs.append(argArrayObj->get(exec,i));
122 return throwError(exec, TypeError);
124 result = func->call(exec,applyThis,applyArgs);
128 JSValue *thisArg = args[0];
129 JSObject *func = thisObj;
131 if (!func->implementsCall())
132 return throwError(exec, TypeError);
135 if (thisArg->isUndefinedOrNull())
136 callThis = exec->dynamicInterpreter()->globalObject();
138 callThis = thisArg->toObject(exec);
141 args.getSlice(1, argsTail);
142 result = func->call(exec, callThis, argsTail);
150 // ------------------------------ FunctionObjectImp ----------------------------
152 FunctionObjectImp::FunctionObjectImp(ExecState* exec, FunctionPrototype* funcProto)
153 : InternalFunctionImp(funcProto)
155 putDirect(exec->propertyNames().prototype, funcProto, DontEnum|DontDelete|ReadOnly);
157 // no. of arguments for constructor
158 putDirect(exec->propertyNames().length, jsNumber(1), ReadOnly|DontDelete|DontEnum);
161 FunctionObjectImp::~FunctionObjectImp()
165 bool FunctionObjectImp::implementsConstruct() const
170 // ECMA 15.3.2 The Function Constructor
171 JSObject* FunctionObjectImp::construct(ExecState* exec, const List& args, const Identifier& functionName, const UString& sourceURL, int lineNumber)
175 int argsSize = args.size();
178 } else if (argsSize == 1) {
179 body = args[0]->toString(exec);
181 p = args[0]->toString(exec);
182 for (int k = 1; k < argsSize - 1; k++)
183 p += "," + args[k]->toString(exec);
184 body = args[argsSize-1]->toString(exec);
187 // parse the source code
191 RefPtr<ProgramNode> progNode = Parser::parse(sourceURL, lineNumber, body.data(),body.size(),&sid,&errLine,&errMsg);
193 // notify debugger that source has been parsed
194 Debugger *dbg = exec->dynamicInterpreter()->debugger();
196 // send empty sourceURL to indicate constructed code
197 bool cont = dbg->sourceParsed(exec, sid, UString(), body, lineNumber, errLine, errMsg);
200 return new JSObject();
204 // no program node == syntax error - throw a syntax error
206 // we can't return a Completion(Throw) here, so just set the exception
208 return throwError(exec, SyntaxError, errMsg, errLine, sid, sourceURL);
210 ScopeChain scopeChain;
211 scopeChain.push(exec->lexicalInterpreter()->globalObject());
212 FunctionBodyNode *bodyNode = progNode.get();
214 FunctionImp* fimp = new FunctionImp(exec, functionName, bodyNode, scopeChain);
216 // parse parameter list. throw syntax error on illegal identifiers
218 const UChar *c = p.data();
219 int i = 0, params = 0;
222 while (*c == ' ' && i < len)
224 if (Lexer::isIdentStart(c->uc)) { // else error
225 param = UString(c, 1);
227 while (i < len && (Lexer::isIdentPart(c->uc))) {
228 param += UString(c, 1);
231 while (i < len && *c == ' ')
234 bodyNode->addParam(Identifier(param));
237 } else if (*c == ',') {
238 bodyNode->addParam(Identifier(param));
244 return throwError(exec, SyntaxError, "Syntax error in parameter list");
249 JSObject* objCons = exec->lexicalInterpreter()->builtinObject();
250 JSObject* prototype = objCons->construct(exec,List::empty());
251 prototype->put(exec, exec->propertyNames().constructor, fimp, DontEnum|DontDelete|ReadOnly);
252 fimp->put(exec, exec->propertyNames().prototype, prototype, Internal|DontDelete);
256 // ECMA 15.3.2 The Function Constructor
257 JSObject* FunctionObjectImp::construct(ExecState* exec, const List& args)
259 return construct(exec, args, "anonymous", UString(), 0);
262 // ECMA 15.3.1 The Function Constructor Called as a Function
263 JSValue* FunctionObjectImp::callAsFunction(ExecState* exec, JSObject* /*thisObj*/, const List &args)
265 return construct(exec, args);