1 // -*- c-basic-offset: 2 -*-
3 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
4 * Copyright (C) 2003, 2006, 2007, 2008 Apple Inc. All rights reserved.
5 * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
6 * Copyright (C) 2007 Maks Orlovich
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 #ifndef KJS_FUNCTION_H
26 #define KJS_FUNCTION_H
28 #include "JSVariableObject.h"
29 #include "LocalStorage.h"
30 #include "SymbolTable.h"
37 class FunctionBodyNode
;
38 class FunctionPrototype
;
41 class InternalFunctionImp
: public JSObject
{
43 InternalFunctionImp();
44 InternalFunctionImp(FunctionPrototype
*, const Identifier
&);
46 virtual bool implementsCall() const;
47 virtual JSValue
* callAsFunction(ExecState
*, JSObject
* thisObjec
, const List
& args
) = 0;
48 virtual bool implementsHasInstance() const;
50 virtual const ClassInfo
* classInfo() const { return &info
; }
51 static const ClassInfo info
;
52 const Identifier
& functionName() const { return m_name
; }
58 class FunctionImp
: public InternalFunctionImp
{
59 friend class ActivationImp
;
61 FunctionImp(ExecState
*, const Identifier
& name
, FunctionBodyNode
*, const ScopeChain
&);
63 virtual bool getOwnPropertySlot(ExecState
*, const Identifier
&, PropertySlot
&);
64 virtual void put(ExecState
*, const Identifier
& propertyName
, JSValue
* value
, int attr
= None
);
65 virtual bool deleteProperty(ExecState
*, const Identifier
& propertyName
);
67 virtual bool implementsConstruct() const { return true; }
68 virtual JSObject
* construct(ExecState
*, const List
& args
);
70 virtual JSValue
* callAsFunction(ExecState
*, JSObject
* thisObj
, const List
& args
);
72 // Note: unlike body->paramName, this returns Identifier::null for parameters
73 // that will never get set, due to later param having the same name
74 Identifier
getParameterName(int index
);
76 virtual const ClassInfo
* classInfo() const { return &info
; }
77 static const ClassInfo info
;
79 RefPtr
<FunctionBodyNode
> body
;
81 void setScope(const ScopeChain
& s
) { _scope
= s
; }
82 const ScopeChain
& scope() const { return _scope
; }
89 static JSValue
* argumentsGetter(ExecState
*, JSObject
*, const Identifier
&, const PropertySlot
&);
90 static JSValue
* callerGetter(ExecState
*, JSObject
*, const Identifier
&, const PropertySlot
&);
91 static JSValue
* lengthGetter(ExecState
*, JSObject
*, const Identifier
&, const PropertySlot
&);
94 class IndexToNameMap
{
96 IndexToNameMap(FunctionImp
*, const List
& args
);
99 Identifier
& operator[](const Identifier
& index
);
100 bool isMapped(const Identifier
& index
) const;
101 void unMap(const Identifier
& index
);
108 class Arguments
: public JSObject
{
110 Arguments(ExecState
*, FunctionImp
* func
, const List
& args
, ActivationImp
* act
);
112 virtual bool getOwnPropertySlot(ExecState
*, const Identifier
&, PropertySlot
&);
113 virtual void put(ExecState
*, const Identifier
& propertyName
, JSValue
* value
, int attr
= None
);
114 virtual bool deleteProperty(ExecState
*, const Identifier
& propertyName
);
115 virtual const ClassInfo
* classInfo() const { return &info
; }
116 static const ClassInfo info
;
118 static JSValue
* mappedIndexGetter(ExecState
*, JSObject
*, const Identifier
&, const PropertySlot
& slot
);
120 ActivationImp
* _activationObject
;
121 mutable IndexToNameMap indexToNameMap
;
124 class PrototypeFunction
: public InternalFunctionImp
{
126 typedef KJS::JSValue
* (*JSMemberFunction
)(ExecState
*, JSObject
*, const List
&);
128 PrototypeFunction(ExecState
*, int len
, const Identifier
&, JSMemberFunction
);
129 PrototypeFunction(ExecState
*, FunctionPrototype
*, int len
, const Identifier
&, JSMemberFunction
);
131 virtual JSValue
* callAsFunction(ExecState
* exec
, JSObject
* thisObj
, const List
&);
134 const JSMemberFunction m_function
;
139 JSValue
* globalFuncEval(ExecState
*, JSObject
*, const List
&);
140 JSValue
* globalFuncParseInt(ExecState
*, JSObject
*, const List
&);
141 JSValue
* globalFuncParseFloat(ExecState
*, JSObject
*, const List
&);
142 JSValue
* globalFuncIsNaN(ExecState
*, JSObject
*, const List
&);
143 JSValue
* globalFuncIsFinite(ExecState
*, JSObject
*, const List
&);
144 JSValue
* globalFuncDecodeURI(ExecState
*, JSObject
*, const List
&);
145 JSValue
* globalFuncDecodeURIComponent(ExecState
*, JSObject
*, const List
&);
146 JSValue
* globalFuncEncodeURI(ExecState
*, JSObject
*, const List
&);
147 JSValue
* globalFuncEncodeURIComponent(ExecState
*, JSObject
*, const List
&);
148 JSValue
* globalFuncEscape(ExecState
*, JSObject
*, const List
&);
149 JSValue
* globalFuncUnescape(ExecState
*, JSObject
*, const List
&);
151 JSValue
* globalFuncKJSPrint(ExecState
*, JSObject
*, const List
&);
154 static const double mantissaOverflowLowerBound
= 9007199254740992.0;
155 double parseIntOverflow(const char*, int length
, int radix
);