1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
5 * Copyright (C) 2003, 2006 Apple Computer, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library 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.
11 *
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 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#ifndef KJS_SCRIPTFUNCTION_H
25#define KJS_SCRIPTFUNCTION_H
26
27#include "function.h"
28#include "nodes.h"
29
30namespace KJS {
31
32 class ActivationImp;
33 class FunctionBodyNode;
34
35 /**
36 * @short Implementation class for internal Functions.
37 */
38 class KJS_EXPORT FunctionImp : public InternalFunctionImp {
39 friend class ActivationImp;
40 public:
41 FunctionImp(ExecState* exec, const Identifier& n, FunctionBodyNode* b, const ScopeChain &sc);
42 virtual ~FunctionImp();
43
44 using KJS::JSObject::getOwnPropertySlot;
45 virtual bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot&);
46 virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
47 using KJS::JSObject::put;
48 virtual void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None);
49 using KJS::JSObject::deleteProperty;
50 virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
51
52 virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
53
54 bool implementsConstruct() const;
55 using KJS::JSObject::construct;
56 JSObject *construct(ExecState *exec, const List &args);
57
58 // Note: implemented in nodes2string.cpp
59 UString toSource() const;
60
61 // Note: unlike body->paramName, this returns Identifier::null for parameters
62 // that will never get set, due to later param having the same name
63 Identifier getParameterName(size_t index);
64
65 virtual const ClassInfo *classInfo() const { return &info; }
66 static const ClassInfo info;
67
68 RefPtr<FunctionBodyNode> body;
69
70 /**
71 * Returns the scope of this object. This is used when execution declared
72 * functions - the execution context for the function is initialized with
73 * extra object in its scope. An example of this is functions declared
74 * inside other functions:
75 *
76 * \code
77 * function f() {
78 *
79 * function b() {
80 * return prototype;
81 * }
82 *
83 * var x = 4;
84 * // do some stuff
85 * }
86 * f.prototype = new String();
87 * \endcode
88 *
89 * When the function f.b is executed, its scope will include properties of
90 * f. So in the example above the return value of f.b() would be the new
91 * String object that was assigned to f.prototype.
92 *
93 * @param exec The current execution state
94 * @return The function's scope
95 */
96 const ScopeChain &scope() const { return _scope; }
97 void setScope(const ScopeChain &s) { _scope = s; }
98
99 virtual void mark();
100 private:
101 void initialCompile(ExecState* newExec);
102
103 ScopeChain _scope;
104
105 static JSValue *argumentsGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot&);
106 static JSValue *callerGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot&);
107 static JSValue *lengthGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot&);
108 static JSValue *nameGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot&);
109
110 void passInParameters(ExecState *exec, const List &);
111 };
112
113 // For compatibility...
114 typedef FunctionImp DeclaredFunctionImp;
115} // namespace
116
117#endif
118