KJS

scriptfunction.h
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 1999-2000 Harri Porten ([email protected])
4  * Copyright (C) 2003, 2006 Apple Computer, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef KJS_SCRIPTFUNCTION_H
24 #define KJS_SCRIPTFUNCTION_H
25 
26 #include "function.h"
27 
28 namespace KJS
29 {
30 
31 class ActivationImp;
32 class FunctionBodyNode;
33 
34 /**
35  * @short Implementation class for internal Functions.
36  */
37 class KJS_EXPORT FunctionImp : public InternalFunctionImp
38 {
39  friend class ActivationImp;
40 public:
41  FunctionImp(ExecState *exec, const Identifier &n, FunctionBodyNode *b, const ScopeChain &sc);
42  ~FunctionImp() override;
43 
44  using KJS::JSObject::getOwnPropertySlot;
45  bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot &) override;
46  bool getOwnPropertyDescriptor(ExecState *, const Identifier &, PropertyDescriptor &) override;
47  using KJS::JSObject::put;
48  void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None) override;
49  using KJS::JSObject::deleteProperty;
50  bool deleteProperty(ExecState *exec, const Identifier &propertyName) override;
51 
52  JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args) override;
53 
54  bool implementsConstruct() const override;
55  using KJS::JSObject::construct;
56  JSObject *construct(ExecState *exec, const List &args) override;
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  const ClassInfo *classInfo() const override
66  {
67  return &info;
68  }
69  static const ClassInfo info;
70 
71  RefPtr<FunctionBodyNode> body;
72 
73  /**
74  * Returns the scope of this object. This is used when execution declared
75  * functions - the execution context for the function is initialized with
76  * extra object in its scope. An example of this is functions declared
77  * inside other functions:
78  *
79  * \code
80  * function f() {
81  *
82  * function b() {
83  * return prototype;
84  * }
85  *
86  * var x = 4;
87  * // do some stuff
88  * }
89  * f.prototype = new String();
90  * \endcode
91  *
92  * When the function f.b is executed, its scope will include properties of
93  * f. So in the example above the return value of f.b() would be the new
94  * String object that was assigned to f.prototype.
95  *
96  * @param exec The current execution state
97  * @return The function's scope
98  */
99  const ScopeChain &scope() const
100  {
101  return _scope;
102  }
103  void setScope(const ScopeChain &s)
104  {
105  _scope = s;
106  }
107 
108  void mark() override;
109 private:
110  void initialCompile(ExecState *newExec);
111 
112  ScopeChain _scope;
113 
114  static JSValue *argumentsGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
115  static JSValue *callerGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
116  static JSValue *lengthGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
117  static JSValue *nameGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
118 
119  void passInParameters(ExecState *exec, const List &);
120 };
121 
122 // For compatibility...
123 typedef FunctionImp DeclaredFunctionImp;
124 } // namespace
125 
126 #endif
Class Information.
Definition: object.h:48
This AST node corresponds to the function body or top-level code in the AST, but is used to keep trac...
Definition: nodes.h:1276
JSValue is the base type for all primitives (Undefined, Null, Boolean, String, Number) and objects in...
Definition: value.h:58
const ScopeChain & scope() const
Returns the scope of this object.
Represents the current state of script execution.
Definition: ExecState.h:53
Represents an Identifier for a Javascript object.
Definition: identifier.h:36
Native list type.
Definition: list.h:52
Implementation class for internal Functions.
virtual QVariant callAsFunction(ScriptableExtension *callerPrincipal, quint64 objId, const ArgList &args)
virtual bool put(ScriptableExtension *callerPrincipal, quint64 objId, const QString &propName, const QVariant &value)
Unicode string class.
Definition: ustring.h:153
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Sep 27 2022 04:23:39 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.