source: webkit/trunk/JavaScriptCore/kjs/identifier.h@ 5356

Last change on this file since 5356 was 4792, checked in by darin, 22 years ago

Reviewed by John Sullivan.

  • fixed 3365527 -- subscripting JavaScript strings does not work (leads to hang at www.newmagna.com.au)

The JavaScript specification says nothing about this, but other browsers seem to give
read-only access to the characters in a string as if the string was an array of characters.

  • kjs/array_object.cpp: (ArrayInstanceImp::get): Update to use a public toArrayIndex function instead of our own getArrayIndex function, so we can share with string. (ArrayInstanceImp::put): Ditto. (ArrayInstanceImp::hasProperty): Ditto. (ArrayInstanceImp::setLength): Ditto.
  • kjs/ustring.h: Add toArrayIndex.
  • kjs/ustring.cpp: (UString::toArrayIndex): Added. Implements the rule from array.
  • kjs/identifier.h: Add a forwarding function so we can use toArrayIndex.
  • kjs/string_object.cpp: (StringInstanceImp::get): Return a single character string if the property name is an array index. (StringInstanceImp::hasProperty): Return true for property names that are suitable array indices.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 2003 Apple Computer, Inc
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 *
20 */
21
22#ifndef KJS_IDENTIFIER_H
23#define KJS_IDENTIFIER_H
24
25#include "ustring.h"
26
27namespace KJS {
28
29 class Identifier {
30 friend class PropertyMap;
31 public:
32 static void init();
33
34 Identifier() { }
35 Identifier(const char *s) : _ustring(add(s)) { }
36 Identifier(const UChar *s, int length) : _ustring(add(s, length)) { }
37 explicit Identifier(const UString &s) : _ustring(add(s.rep)) { }
38
39 const UString &ustring() const { return _ustring; }
40 DOM::DOMString string() const;
41 QString qstring() const;
42
43 const UChar *data() const { return _ustring.data(); }
44 int size() const { return _ustring.size(); }
45
46 const char *ascii() const { return _ustring.ascii(); }
47
48 static Identifier from(unsigned y) { return Identifier(UString::from(y)); }
49
50 bool isNull() const { return _ustring.isNull(); }
51 bool isEmpty() const { return _ustring.isEmpty(); }
52
53 unsigned long toULong(bool *ok) const { return _ustring.toULong(ok); }
54 uint32_t toUInt32(bool *ok) const { return _ustring.toUInt32(ok); }
55 uint32_t toStrictUInt32(bool *ok) const { return _ustring.toStrictUInt32(ok); }
56 unsigned toArrayIndex(bool *ok) const { return _ustring.toArrayIndex(ok); }
57 double toDouble() const { return _ustring.toDouble(); }
58
59 static const Identifier &null();
60
61 friend bool operator==(const Identifier &, const Identifier &);
62 friend bool operator!=(const Identifier &, const Identifier &);
63
64 friend bool operator==(const Identifier &, const char *);
65
66 static void remove(UString::Rep *);
67
68 private:
69 UString _ustring;
70
71 static bool equal(UString::Rep *, const char *);
72 static bool equal(UString::Rep *, const UChar *, int length);
73 static bool equal(UString::Rep *, UString::Rep *);
74
75 static bool equal(const Identifier &a, const Identifier &b)
76 { return a._ustring.rep == b._ustring.rep; }
77 static bool equal(const Identifier &a, const char *b)
78 { return equal(a._ustring.rep, b); }
79
80 static UString::Rep *add(const char *);
81 static UString::Rep *add(const UChar *, int length);
82 static UString::Rep *add(UString::Rep *);
83
84 static void insert(UString::Rep *);
85
86 static void rehash(int newTableSize);
87 static void expand();
88 static void shrink();
89
90 static UString::Rep **_table;
91 static int _tableSize;
92 static int _tableSizeMask;
93 static int _keyCount;
94 };
95
96 inline bool operator==(const Identifier &a, const Identifier &b)
97 { return Identifier::equal(a, b); }
98
99 inline bool operator!=(const Identifier &a, const Identifier &b)
100 { return !Identifier::equal(a, b); }
101
102 inline bool operator==(const Identifier &a, const char *b)
103 { return Identifier::equal(a, b); }
104
105 // List of property names, passed to a macro so we can do set them up various
106 // ways without repeating the list.
107 #define KJS_IDENTIFIER_EACH_GLOBAL(macro) \
108 macro(arguments) \
109 macro(callee) \
110 macro(constructor) \
111 macro(length) \
112 macro(message) \
113 macro(name) \
114 macro(prototype) \
115 macro(toLocaleString) \
116 macro(toString) \
117 macro(valueOf)
118
119 // Define external global variables for all property names above (and one more).
120#if !KJS_IDENTIFIER_HIDE_GLOBALS
121 #define KJS_IDENTIFIER_DECLARE_GLOBAL(name) extern const Identifier name ## PropertyName;
122 KJS_IDENTIFIER_EACH_GLOBAL(KJS_IDENTIFIER_DECLARE_GLOBAL)
123 KJS_IDENTIFIER_DECLARE_GLOBAL(specialPrototype)
124 #undef KJS_IDENTIFIER_DECLARE_GLOBAL
125#endif
126
127}
128
129#endif
Note: See TracBrowser for help on using the repository browser.