fix logic
[personal-kdelibs.git] / kjs / function.h
blob689c7f93dcd5696c5aff50cd712fdead62f33a7c
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, 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
6 * Copyright (C) 2007, 2008 Maksim Orlovich <maksim@kde.org>
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 "SymbolTable.h"
29 #include "LocalStorage.h"
30 #include "JSVariableObject.h"
31 #include "object.h"
32 #include <wtf/OwnPtr.h>
34 namespace KJS {
36 class ActivationImp;
37 class FunctionPrototype;
39 class KJS_EXPORT InternalFunctionImp : public JSObject {
40 public:
41 InternalFunctionImp();
42 InternalFunctionImp(FunctionPrototype*);
43 InternalFunctionImp(FunctionPrototype*, const Identifier&);
45 virtual bool implementsCall() const;
46 virtual JSValue* callAsFunction(ExecState*, JSObject* thisObjec, const List& args) = 0;
47 virtual bool implementsHasInstance() const;
49 virtual const ClassInfo* classInfo() const { return &info; }
50 static const ClassInfo info;
51 const Identifier& functionName() const { return m_name; }
52 void setFunctionName(const Identifier& name) { m_name = name; }
54 private:
55 Identifier m_name;
56 #ifdef WIN32
57 InternalFunctionImp(const InternalFunctionImp&);
58 InternalFunctionImp& operator=(const InternalFunctionImp&);
59 #endif
62 /**
63 * @internal
65 * The initial value of Function.prototype (and thus all objects created
66 * with the Function constructor)
68 class FunctionPrototype : public InternalFunctionImp {
69 public:
70 FunctionPrototype(ExecState *exec);
71 virtual ~FunctionPrototype();
73 virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
76 class IndexToNameMap {
77 public:
78 IndexToNameMap(FunctionImp *func, const List &args);
79 ~IndexToNameMap();
81 Identifier& operator[](int index);
82 Identifier& operator[](const Identifier &indexIdentifier);
83 bool isMapped(const Identifier &index) const;
84 void unMap(const Identifier &index);
86 private:
87 IndexToNameMap(); // prevent construction w/o parameters
88 int size;
89 Identifier * _map;
92 class Arguments : public JSObject {
93 public:
94 Arguments(ExecState *exec, FunctionImp *func, const List &args, ActivationImp *act);
95 virtual void mark();
96 virtual bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot&);
97 virtual void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None);
98 virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
99 virtual const ClassInfo *classInfo() const { return &info; }
100 static const ClassInfo info;
101 private:
102 static JSValue *mappedIndexGetter(ExecState *exec, JSObject *, const Identifier &, const PropertySlot& slot);
104 ActivationImp *_activationObject;
105 mutable IndexToNameMap indexToNameMap;
108 class ActivationImp : public JSVariableObject {
109 public:
110 enum {
111 FunctionSlot = NumVarObjectSlots,
112 ArgumentsObjectSlot,
113 NumReservedSlots = ArgumentsObjectSlot + 1
116 void setup(ExecState* exec, FunctionImp *function, const List* arguments,
117 LocalStorageEntry* stackSpace);
119 // Request that this activation be torn off when the code using it stops running
120 void requestTearOff();
121 void performTearOff();
123 virtual bool getOwnPropertySlot(ExecState *exec, const Identifier &, PropertySlot&);
124 virtual void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None);
125 virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
127 bool isLocalReadOnly(int propertyID) const {
128 return (localStorage[propertyID].attributes & ReadOnly) == ReadOnly;
131 virtual const ClassInfo *classInfo() const { return &info; }
132 static const ClassInfo info;
134 virtual bool isActivation() const { return true; }
135 void setupLocals(FunctionBodyNode* fbody);
136 void setupFunctionLocals(FunctionBodyNode* fbody, ExecState *exec);
137 private:
138 JSValue*& functionSlot() {
139 return localStorage[FunctionSlot].val.valueVal;
142 JSValue*& argumentsObjectSlot() {
143 return localStorage[ArgumentsObjectSlot].val.valueVal;
146 static PropertySlot::GetValueFunc getArgumentsGetter();
147 static JSValue *argumentsGetter(ExecState *exec, JSObject *, const Identifier &, const PropertySlot& slot);
148 void createArgumentsObject(ExecState *exec);
150 int numLocals() const { return lengthSlot(); }
151 bool validLocal(int id) const { return 0 <= id && id < numLocals(); }
152 const List* arguments;
155 class GlobalFuncImp : public InternalFunctionImp {
156 public:
157 GlobalFuncImp(ExecState*, FunctionPrototype*, int i, int len, const Identifier&);
158 virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
159 enum { Eval, ParseInt, ParseFloat, IsNaN, IsFinite, Escape, UnEscape,
160 DecodeURI, DecodeURIComponent, EncodeURI, EncodeURIComponent
161 #ifndef NDEBUG
162 , KJSPrint
163 #endif
165 private:
166 int id;
169 static const double mantissaOverflowLowerBound = 9007199254740992.0;
170 double parseIntOverflow(const char* s, int length, int radix);
172 } // namespace
174 #endif