File indexing completed on 2024-05-12 15:43:34

0001 /*
0002  *  This file is part of the KDE libraries
0003  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
0004  *  Copyright (C) 2003, 2006 Apple Computer, Inc.
0005  *
0006  *  This library is free software; you can redistribute it and/or
0007  *  modify it under the terms of the GNU Library General Public
0008  *  License as published by the Free Software Foundation; either
0009  *  version 2 of the License, or (at your option) any later version.
0010  *
0011  *  This library is distributed in the hope that it will be useful,
0012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  *  Library General Public License for more details.
0015  *
0016  *  You should have received a copy of the GNU Library General Public License
0017  *  along with this library; see the file COPYING.LIB.  If not, write to
0018  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  *  Boston, MA 02110-1301, USA.
0020  *
0021  */
0022 
0023 #ifndef KJS_SCRIPTFUNCTION_H
0024 #define KJS_SCRIPTFUNCTION_H
0025 
0026 #include "function.h"
0027 
0028 namespace KJS
0029 {
0030 
0031 class ActivationImp;
0032 class FunctionBodyNode;
0033 
0034 /**
0035  * @short Implementation class for internal Functions.
0036  */
0037 class KJS_EXPORT FunctionImp : public InternalFunctionImp
0038 {
0039     friend class ActivationImp;
0040 public:
0041     FunctionImp(ExecState *exec, const Identifier &n, FunctionBodyNode *b, const ScopeChain &sc);
0042     ~FunctionImp() override;
0043 
0044     using KJS::JSObject::getOwnPropertySlot;
0045     bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot &) override;
0046     bool getOwnPropertyDescriptor(ExecState *, const Identifier &, PropertyDescriptor &) override;
0047     using KJS::JSObject::put;
0048     void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None) override;
0049     using KJS::JSObject::deleteProperty;
0050     bool deleteProperty(ExecState *exec, const Identifier &propertyName) override;
0051 
0052     JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args) override;
0053 
0054     bool implementsConstruct() const override;
0055     using KJS::JSObject::construct;
0056     JSObject *construct(ExecState *exec, const List &args) override;
0057 
0058     // Note: implemented in nodes2string.cpp
0059     UString toSource() const;
0060 
0061     // Note: unlike body->paramName, this returns Identifier::null for parameters
0062     // that will never get set, due to later param having the same name
0063     Identifier getParameterName(size_t index);
0064 
0065     const ClassInfo *classInfo() const override
0066     {
0067         return &info;
0068     }
0069     static const ClassInfo info;
0070 
0071     RefPtr<FunctionBodyNode> body;
0072 
0073     /**
0074      * Returns the scope of this object. This is used when execution declared
0075      * functions - the execution context for the function is initialized with
0076      * extra object in its scope. An example of this is functions declared
0077      * inside other functions:
0078      *
0079      * \code
0080      * function f() {
0081      *
0082      *   function b() {
0083      *     return prototype;
0084      *   }
0085      *
0086      *   var x = 4;
0087      *   // do some stuff
0088      * }
0089      * f.prototype = new String();
0090      * \endcode
0091      *
0092      * When the function f.b is executed, its scope will include properties of
0093      * f. So in the example above the return value of f.b() would be the new
0094      * String object that was assigned to f.prototype.
0095      *
0096      * @param exec The current execution state
0097      * @return The function's scope
0098      */
0099     const ScopeChain &scope() const
0100     {
0101         return _scope;
0102     }
0103     void setScope(const ScopeChain &s)
0104     {
0105         _scope = s;
0106     }
0107 
0108     void mark() override;
0109 private:
0110     void initialCompile(ExecState *newExec);
0111 
0112     ScopeChain _scope;
0113 
0114     static JSValue *argumentsGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
0115     static JSValue *callerGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
0116     static JSValue *lengthGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
0117     static JSValue *nameGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
0118 
0119     void passInParameters(ExecState *exec, const List &);
0120 };
0121 
0122 // For compatibility...
0123 typedef FunctionImp DeclaredFunctionImp;
0124 } // namespace
0125 
0126 #endif