source: webkit/trunk/JavaScriptCore/kjs/function.h@ 13304

Last change on this file since 13304 was 13304, checked in by mjs, 19 years ago

Reviewed by Anders.


The memory usage of Node was reduced by 2 machine words per node:

  • sourceURL was removed and only kept on FunctionBodyNode. The source URL can only be distinct per function or top-level program node, and you always have one.


  • refcount was removed and kept in a separate hashtable when greater than 1. newNodes set represents floating nodes with refcount of 0. This helps because almost all nodes have a refcount of 1 for almost all of their lifetime.


  • bindings/runtime_method.cpp: (RuntimeMethod::RuntimeMethod): Pass null body, added FIXME.
  • kjs/Parser.cpp: (KJS::clearNewNodes): New nodes are tracked in nodes.cpp now, but still clear them at the appropriate time.
  • kjs/context.h: (KJS::ContextImp::currentBody): added; used to retrieve source URL and sid for current code. (KJS::ContextImp::pushIteration): moved here from LabelStack (KJS::ContextImp::popIteration): ditto (KJS::ContextImp::inIteration): ditto (KJS::ContextImp::pushSwitch): ditto (KJS::ContextImp::popSwitch): ditto (KJS::ContextImp::inSwitch): ditto
  • kjs/function.cpp: (KJS::FunctionImp::FunctionImp): Add FunctionBodyNode* parameter. (KJS::FunctionImp::callAsFunction): Pass body to ContextImp. (KJS::FunctionImp::argumentsGetter): _context renamed to m_context. (KJS::DeclaredFunctionImp::DeclaredFunctionImp): Pass body to superclass constructor. (KJS::GlobalFuncImp::callAsFunction): Pass progNode as body for ContextImp in eval.
  • kjs/function.h: Move body field from DeclaredFunctionImp to FunctionImp.
  • kjs/grammar.y: Change DBG; statements no longer have a sourceid.
  • kjs/internal.cpp: (KJS::ContextImp::ContextImp): Initialize new m_currentBody, m_iterationDepth and m_switchDepth data members. New FunctionBodyNode* parameter - the function body provides source URL and SourceId. (KJS::InterpreterImp::mark): Use exception() function, not _exception directly. (KJS::InterpreterImp::evaluate): Pass progNode to ContextImp constructor to use as the body.
  • kjs/internal.h: (KJS::LabelStack::LabelStack): Remove iteration depth and switch depth; statement label stacks don't need these and it bloats their size. Put them in the ContextImp instead.
  • kjs/interpreter.cpp: (KJS::ExecState::lexicalInterpreter): Renamed _context to m_context.
  • kjs/interpreter.h: (KJS::ExecState::dynamicInterpreter): Renamed _context to m_context. (KJS::ExecState::context): ditto (KJS::ExecState::setException): Renamed _exception to m_exception (KJS::ExecState::clearException): ditto (KJS::ExecState::exception): ditto (KJS::ExecState::hadException): ditto (KJS::ExecState::ExecState): ditto both above renames
  • kjs/nodes.cpp: (Node::Node): Removed initialization of line, source URL and refcount. Add to local newNodes set instead of involving parser. (Node::ref): Instead of managing refcount directly, story refcount over 1 in a HashCountedSet, and keep a separate HashSet of "floating" nodes with refcount 0. (Node::deref): ditto (Node::refcount): ditto (Node::clearNewNodes): Destroy anything left in the new nodes set. (currentSourceId): Inline helper to get sourceId from function body via context. (currentSourceURL): ditto for sourceURL. (Node::createErrorCompletion): use new helper (Node::throwError): ditto (Node::setExceptionDetailsIfNeeded): ditto (StatementNode::StatementNode): remove initialization of l0 and sid, rename l1 to m_lastLine. (StatementNode::setLoc): Set own m_lastLine and Node's m_line. (StatementNode::hitStatement): Get sid, first line, last line in the proper new ways. (StatListNode::StatListNode): updated for setLoc changes (BlockNode::BlockNode): ditto (DoWhileNode::execute): excpect iteraton counts on ContextImp, not LabelStack (WhileNode::execute): ditto (ForNode::execute): ditto (ForInNode::execute): ditto (ContinueNode::execute): excpect inIteration on ContextImp, not LabelStack (BreakNode::execute): excpect inIteration and inSwitch on ContextImp, not LabelStack (SwitchNode::execute): expect switch counts on ContextImp, not LabelStack (FunctionBodyNode::FunctionBodyNode): update for new setLoc (FunctionBodyNode::processFuncDecl): reindent (SourceElementsNode::SourceElementsNode): update for new setLoc
  • kjs/nodes.h: (KJS::Node::lineNo): Renamed _line to m_line (KJS::StatementNode::firstLine): Use lineNo() (KJS::StatementNode::lastLine): Renamed l1 to m_lastLine (KJS::FunctionBodyNode::sourceId): added (KJS::FunctionBodyNode::sourceURL): added
  • kjs/testkjs.cpp:
  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2000 Harri Porten ([email protected])
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_FUNCTION_H
25#define KJS_FUNCTION_H
26
27#include "internal.h"
28#include <kxmlcore/OwnPtr.h>
29
30namespace KJS {
31
32 class ActivationImp;
33 class FunctionBodyNode;
34 class Parameter;
35
36 /**
37 * @short Implementation class for internal Functions.
38 */
39 class FunctionImp : public InternalFunctionImp {
40 friend class ActivationImp;
41 public:
42 FunctionImp(ExecState* exec, const Identifier& n, FunctionBodyNode* b);
43 virtual ~FunctionImp();
44
45 virtual bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot&);
46 virtual void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None);
47 virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
48
49 virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
50
51 void addParameter(const Identifier &n);
52 Identifier getParameterName(int index);
53 // parameters in string representation, e.g. (a, b, c)
54 UString parameterString() const;
55 virtual CodeType codeType() const = 0;
56
57 virtual Completion execute(ExecState *exec) = 0;
58
59 virtual const ClassInfo *classInfo() const { return &info; }
60 static const ClassInfo info;
61
62 RefPtr<FunctionBodyNode> body;
63
64 protected:
65 OwnPtr<Parameter> param;
66
67 private:
68 static JSValue *argumentsGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot&);
69 static JSValue *lengthGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot&);
70
71 void processParameters(ExecState *exec, const List &);
72 virtual void processVarDecls(ExecState *exec);
73 };
74
75 class DeclaredFunctionImp : public FunctionImp {
76 public:
77 DeclaredFunctionImp(ExecState *exec, const Identifier &n,
78 FunctionBodyNode *b, const ScopeChain &sc);
79
80 bool implementsConstruct() const;
81 JSObject *construct(ExecState *exec, const List &args);
82
83 virtual Completion execute(ExecState *exec);
84 CodeType codeType() const { return FunctionCode; }
85
86 virtual const ClassInfo *classInfo() const { return &info; }
87 static const ClassInfo info;
88
89 private:
90 virtual void processVarDecls(ExecState *exec);
91 };
92
93 class IndexToNameMap {
94 public:
95 IndexToNameMap(FunctionImp *func, const List &args);
96 ~IndexToNameMap();
97
98 Identifier& operator[](int index);
99 Identifier& operator[](const Identifier &indexIdentifier);
100 bool isMapped(const Identifier &index) const;
101 void unMap(const Identifier &index);
102
103 private:
104 IndexToNameMap(); // prevent construction w/o parameters
105 int size;
106 Identifier * _map;
107 };
108
109 class Arguments : public JSObject {
110 public:
111 Arguments(ExecState *exec, FunctionImp *func, const List &args, ActivationImp *act);
112 virtual void mark();
113 virtual bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot&);
114 virtual void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None);
115 virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
116 virtual const ClassInfo *classInfo() const { return &info; }
117 static const ClassInfo info;
118 private:
119 static JSValue *mappedIndexGetter(ExecState *exec, JSObject *, const Identifier &, const PropertySlot& slot);
120
121 ActivationImp *_activationObject;
122 mutable IndexToNameMap indexToNameMap;
123 };
124
125 class ActivationImp : public JSObject {
126 public:
127 ActivationImp(FunctionImp *function, const List &arguments);
128
129 virtual bool getOwnPropertySlot(ExecState *exec, const Identifier &, PropertySlot&);
130 virtual void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None);
131 virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
132
133 virtual const ClassInfo *classInfo() const { return &info; }
134 static const ClassInfo info;
135
136 virtual void mark();
137
138 bool isActivation() { return true; }
139 private:
140 static PropertySlot::GetValueFunc getArgumentsGetter();
141 static JSValue *argumentsGetter(ExecState *exec, JSObject *, const Identifier &, const PropertySlot& slot);
142 void createArgumentsObject(ExecState *exec) const;
143
144 FunctionImp *_function;
145 List _arguments;
146 mutable Arguments *_argumentsObject;
147 };
148
149 class GlobalFuncImp : public InternalFunctionImp {
150 public:
151 GlobalFuncImp(ExecState*, FunctionPrototype*, int i, int len, const Identifier&);
152 virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
153 virtual CodeType codeType() const;
154 enum { Eval, ParseInt, ParseFloat, IsNaN, IsFinite, Escape, UnEscape,
155 DecodeURI, DecodeURIComponent, EncodeURI, EncodeURIComponent
156#ifndef NDEBUG
157 , KJSPrint
158#endif
159};
160 private:
161 int id;
162 };
163
164
165
166} // namespace
167
168#endif
Note: See TracBrowser for help on using the repository browser.