1 | /*
|
---|
2 | * Copyright (C) 1999-2002 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2001 Peter Kelly ([email protected])
|
---|
4 | * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
|
---|
5 | * Copyright (C) 2007 Cameron Zwarich ([email protected])
|
---|
6 | * Copyright (C) 2007 Maks Orlovich
|
---|
7 | *
|
---|
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.
|
---|
12 | *
|
---|
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.
|
---|
17 | *
|
---|
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.
|
---|
22 | *
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "config.h"
|
---|
26 | #include "IndexToNameMap.h"
|
---|
27 |
|
---|
28 | #include "ArgList.h"
|
---|
29 | #include "JSFunction.h"
|
---|
30 | #include "identifier.h"
|
---|
31 |
|
---|
32 | namespace KJS {
|
---|
33 |
|
---|
34 | // We map indexes in the arguments array to their corresponding argument names.
|
---|
35 | // Example: function f(x, y, z): arguments[0] = x, so we map 0 to Identifier("x").
|
---|
36 |
|
---|
37 | // Once we have an argument name, we can get and set the argument's value in the
|
---|
38 | // activation object.
|
---|
39 |
|
---|
40 | // We use Identifier::null to indicate that a given argument's value
|
---|
41 | // isn't stored in the activation object.
|
---|
42 |
|
---|
43 | IndexToNameMap::IndexToNameMap(JSFunction* func, const ArgList& args)
|
---|
44 | : m_size(args.size())
|
---|
45 | , m_map(new Identifier[args.size()])
|
---|
46 | {
|
---|
47 | size_t size = args.size();
|
---|
48 | for (size_t i = 0; i < size; ++i)
|
---|
49 | m_map[i] = func->getParameterName(i); // null if there is no corresponding parameter
|
---|
50 | }
|
---|
51 |
|
---|
52 | IndexToNameMap::~IndexToNameMap()
|
---|
53 | {
|
---|
54 | delete [] m_map;
|
---|
55 | }
|
---|
56 |
|
---|
57 | bool IndexToNameMap::isMapped(const Identifier& index) const
|
---|
58 | {
|
---|
59 | bool indexIsNumber;
|
---|
60 | unsigned indexAsNumber = index.toStrictUInt32(&indexIsNumber);
|
---|
61 |
|
---|
62 | if (!indexIsNumber)
|
---|
63 | return false;
|
---|
64 |
|
---|
65 | if (indexAsNumber >= m_size)
|
---|
66 | return false;
|
---|
67 |
|
---|
68 | if (m_map[indexAsNumber].isNull())
|
---|
69 | return false;
|
---|
70 |
|
---|
71 | return true;
|
---|
72 | }
|
---|
73 |
|
---|
74 | void IndexToNameMap::unMap(ExecState* exec, const Identifier& index)
|
---|
75 | {
|
---|
76 | bool indexIsNumber;
|
---|
77 | unsigned indexAsNumber = index.toStrictUInt32(&indexIsNumber);
|
---|
78 |
|
---|
79 | ASSERT(indexIsNumber && indexAsNumber < m_size);
|
---|
80 |
|
---|
81 | m_map[indexAsNumber] = exec->propertyNames().nullIdentifier;
|
---|
82 | }
|
---|
83 |
|
---|
84 | Identifier& IndexToNameMap::operator[](const Identifier& index)
|
---|
85 | {
|
---|
86 | bool indexIsNumber;
|
---|
87 | unsigned indexAsNumber = index.toStrictUInt32(&indexIsNumber);
|
---|
88 |
|
---|
89 | ASSERT(indexIsNumber && indexAsNumber < m_size);
|
---|
90 |
|
---|
91 | return m_map[indexAsNumber];
|
---|
92 | }
|
---|
93 |
|
---|
94 | } // namespace KJS
|
---|