source: webkit/trunk/JavaScriptCore/kjs/identifier.cpp@ 20310

Last change on this file since 20310 was 20310, checked in by mjs, 18 years ago

JavaScriptCore:

Reviewed by Mark.


  • avoid static construction (and global variable access) in a smarter, more portable way, to later enable MUTLI_THREAD mode to work on other platforms and compilers.


  • kjs/CommonIdentifiers.cpp: Added. New class to hold all the shared identifiers. (KJS::CommonIdentifiers::CommonIdentifiers): (KJS::CommonIdentifiers::shared):
  • kjs/CommonIdentifiers.h: Added.
  • kjs/ExecState.h: (KJS::ExecState::propertyNames): Hand the CommonIdentifiers instance here for easy access. (KJS::ExecState::ExecState):
  • API/JSObjectRef.cpp: (JSObjectMakeConstructor):
  • CMakeLists.txt:
  • JavaScriptCore.exp:
  • JavaScriptCore.pri:
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • JavaScriptCoreSources.bkl:
  • bindings/runtime_array.cpp: (RuntimeArray::getOwnPropertySlot): (RuntimeArray::put):
  • bindings/runtime_method.cpp: (RuntimeMethod::getOwnPropertySlot):
  • kjs/array_object.cpp: (ArrayInstance::getOwnPropertySlot): (ArrayInstance::put): (ArrayInstance::deleteProperty): (ArrayProtoFunc::ArrayProtoFunc): (ArrayProtoFunc::callAsFunction): (ArrayObjectImp::ArrayObjectImp):
  • kjs/bool_object.cpp: (BooleanPrototype::BooleanPrototype): (BooleanProtoFunc::BooleanProtoFunc): (BooleanProtoFunc::callAsFunction): (BooleanObjectImp::BooleanObjectImp):
  • kjs/completion.h: (KJS::Completion::Completion):
  • kjs/date_object.cpp: (KJS::DateProtoFunc::DateProtoFunc): (KJS::DateObjectImp::DateObjectImp): (KJS::DateObjectFuncImp::DateObjectFuncImp):
  • kjs/error_object.cpp: (ErrorPrototype::ErrorPrototype): (ErrorProtoFunc::ErrorProtoFunc): (ErrorProtoFunc::callAsFunction): (ErrorObjectImp::ErrorObjectImp): (ErrorObjectImp::construct): (NativeErrorPrototype::NativeErrorPrototype): (NativeErrorImp::NativeErrorImp): (NativeErrorImp::construct): (NativeErrorImp::callAsFunction):
  • kjs/function.cpp: (KJS::FunctionImp::getOwnPropertySlot): (KJS::FunctionImp::put): (KJS::FunctionImp::deleteProperty): (KJS::FunctionImp::getParameterName): (KJS::DeclaredFunctionImp::construct): (KJS::IndexToNameMap::unMap): (KJS::Arguments::Arguments): (KJS::ActivationImp::getOwnPropertySlot): (KJS::ActivationImp::deleteProperty): (KJS::GlobalFuncImp::GlobalFuncImp):
  • kjs/function_object.cpp: (FunctionPrototype::FunctionPrototype): (FunctionProtoFunc::FunctionProtoFunc): (FunctionProtoFunc::callAsFunction): (FunctionObjectImp::FunctionObjectImp): (FunctionObjectImp::construct):
  • kjs/grammar.y:
  • kjs/identifier.cpp:
  • kjs/identifier.h:
  • kjs/interpreter.cpp: (KJS::Interpreter::init): (KJS::Interpreter::initGlobalObject):
  • kjs/interpreter.h:
  • kjs/lookup.h:
  • kjs/math_object.cpp: (MathFuncImp::MathFuncImp):
  • kjs/nodes.cpp: (ArrayNode::evaluate): (FuncDeclNode::processFuncDecl): (FuncExprNode::evaluate):
  • kjs/number_object.cpp: (NumberPrototype::NumberPrototype): (NumberProtoFunc::NumberProtoFunc): (NumberObjectImp::NumberObjectImp):
  • kjs/object.cpp: (KJS::JSObject::put): (KJS::JSObject::defaultValue): (KJS::JSObject::hasInstance):
  • kjs/object.h: (KJS::JSObject::getOwnPropertySlot):
  • kjs/object_object.cpp: (ObjectPrototype::ObjectPrototype): (ObjectProtoFunc::ObjectProtoFunc): (ObjectObjectImp::ObjectObjectImp):
  • kjs/regexp_object.cpp: (RegExpPrototype::RegExpPrototype): (RegExpProtoFunc::RegExpProtoFunc): (RegExpObjectImp::RegExpObjectImp):
  • kjs/string_object.cpp: (KJS::StringInstance::getOwnPropertySlot): (KJS::StringInstance::put): (KJS::StringInstance::deleteProperty): (KJS::StringPrototype::StringPrototype): (KJS::StringProtoFunc::StringProtoFunc): (KJS::StringProtoFunc::callAsFunction): (KJS::StringObjectImp::StringObjectImp): (KJS::StringObjectFuncImp::StringObjectFuncImp):
  • kjs/testkjs.cpp: (TestFunctionImp::TestFunctionImp):

WebCore:

Reviewed by Mark.

  • avoid static construction (and global variable access) in a smarter, more portable way, to later enable MUTLI_THREAD mode to work on other platforms and compilers.


  • bindings/js/JSCustomXPathNSResolver.cpp: (WebCore::JSCustomXPathNSResolver::lookupNamespaceURI):
  • bindings/js/JSHTMLInputElementBase.cpp: (WebCore::JSHTMLInputElementBaseFunction::JSHTMLInputElementBaseFunction):
  • bindings/js/JSHTMLOptionElementConstructor.cpp: (WebCore::JSHTMLOptionElementConstructor::JSHTMLOptionElementConstructor):
  • bindings/js/JSXMLHttpRequest.cpp: (KJS::JSXMLHttpRequestConstructorImp::JSXMLHttpRequestConstructorImp):
  • bindings/js/JSXSLTProcessor.cpp: (KJS::XSLTProcessorConstructorImp::XSLTProcessorConstructorImp):
  • bindings/js/kjs_dom.cpp: (KJS::DOMNamedNodeMap::getOwnPropertySlot): (KJS::DOMNamedNodesCollection::getOwnPropertySlot):
  • bindings/js/kjs_events.cpp: (KJS::JSAbstractEventListener::handleEvent):
  • bindings/js/kjs_html.cpp: (KJS::HTMLElementFunction::HTMLElementFunction): (KJS::JSHTMLCollection::getOwnPropertySlot):
  • bindings/js/kjs_window.cpp: (KJS::ScheduledAction::execute):
  • bindings/objc/WebScriptObject.mm:
  • bindings/scripts/CodeGeneratorJS.pm:
  • Property svn:eol-style set to native
File size: 5.0 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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include "config.h"
23
24#include "identifier.h"
25
26#include "JSLock.h"
27#include <new> // for placement new
28#include <string.h> // for strlen
29#include <wtf/Assertions.h>
30#include <wtf/FastMalloc.h>
31#include <wtf/HashSet.h>
32
33namespace WTF {
34
35 template<typename T> struct DefaultHash;
36 template<typename T> struct StrHash;
37
38 template<> struct StrHash<KJS::UString::Rep *> {
39 static unsigned hash(const KJS::UString::Rep *key) { return key->hash(); }
40 static bool equal(const KJS::UString::Rep *a, const KJS::UString::Rep *b) { return KJS::Identifier::equal(a, b); }
41 };
42
43 template<> struct DefaultHash<KJS::UString::Rep *> {
44 typedef StrHash<KJS::UString::Rep *> Hash;
45 };
46
47}
48
49namespace KJS {
50
51typedef HashSet<UString::Rep *> IdentifierTable;
52static IdentifierTable *table;
53
54static inline IdentifierTable& identifierTable()
55{
56 ASSERT(JSLock::lockCount() > 0);
57
58 if (!table)
59 table = new IdentifierTable;
60 return *table;
61}
62
63
64bool Identifier::equal(const UString::Rep *r, const char *s)
65{
66 int length = r->len;
67 const UChar *d = r->data();
68 for (int i = 0; i != length; ++i)
69 if (d[i].uc != (unsigned char)s[i])
70 return false;
71 return s[length] == 0;
72}
73
74bool Identifier::equal(const UString::Rep *r, const UChar *s, int length)
75{
76 if (r->len != length)
77 return false;
78 const UChar *d = r->data();
79 for (int i = 0; i != length; ++i)
80 if (d[i].uc != s[i].uc)
81 return false;
82 return true;
83}
84
85bool Identifier::equal(const UString::Rep *r, const UString::Rep *b)
86{
87 int length = r->len;
88 if (length != b->len)
89 return false;
90 const UChar *d = r->data();
91 const UChar *s = b->data();
92 for (int i = 0; i != length; ++i)
93 if (d[i].uc != s[i].uc)
94 return false;
95 return true;
96}
97
98struct CStringTranslator
99{
100 static unsigned hash(const char *c)
101 {
102 return UString::Rep::computeHash(c);
103 }
104
105 static bool equal(UString::Rep *r, const char *s)
106 {
107 return Identifier::equal(r, s);
108 }
109
110 static void translate(UString::Rep*& location, const char *c, unsigned hash)
111 {
112 size_t length = strlen(c);
113 UChar *d = static_cast<UChar *>(fastMalloc(sizeof(UChar) * length));
114 for (size_t i = 0; i != length; i++)
115 d[i] = c[i];
116
117 UString::Rep *r = UString::Rep::create(d, static_cast<int>(length)).releaseRef();
118 r->isIdentifier = 1;
119 r->rc = 0;
120 r->_hash = hash;
121
122 location = r;
123 }
124};
125
126PassRefPtr<UString::Rep> Identifier::add(const char *c)
127{
128 if (!c)
129 return &UString::Rep::null;
130 size_t length = strlen(c);
131 if (length == 0)
132 return &UString::Rep::empty;
133
134 return *identifierTable().add<const char *, CStringTranslator>(c).first;
135}
136
137struct UCharBuffer {
138 const UChar *s;
139 unsigned int length;
140};
141
142struct UCharBufferTranslator
143{
144 static unsigned hash(const UCharBuffer& buf)
145 {
146 return UString::Rep::computeHash(buf.s, buf.length);
147 }
148
149 static bool equal(UString::Rep *str, const UCharBuffer& buf)
150 {
151 return Identifier::equal(str, buf.s, buf.length);
152 }
153
154 static void translate(UString::Rep *& location, const UCharBuffer& buf, unsigned hash)
155 {
156 UChar *d = static_cast<UChar *>(fastMalloc(sizeof(UChar) * buf.length));
157 for (unsigned i = 0; i != buf.length; i++)
158 d[i] = buf.s[i];
159
160 UString::Rep *r = UString::Rep::create(d, buf.length).releaseRef();
161 r->isIdentifier = 1;
162 r->rc = 0;
163 r->_hash = hash;
164
165 location = r;
166 }
167};
168
169PassRefPtr<UString::Rep> Identifier::add(const UChar *s, int length)
170{
171 if (length == 0)
172 return &UString::Rep::empty;
173
174 UCharBuffer buf = {s, length};
175 return *identifierTable().add<UCharBuffer, UCharBufferTranslator>(buf).first;
176}
177
178PassRefPtr<UString::Rep> Identifier::add(UString::Rep *r)
179{
180 if (r->isIdentifier)
181 return r;
182
183 if (r->len == 0)
184 return &UString::Rep::empty;
185
186 UString::Rep *result = *identifierTable().add(r).first;
187 if (result == r)
188 r->isIdentifier = true;
189 return result;
190}
191
192void Identifier::remove(UString::Rep *r)
193{
194 identifierTable().remove(r);
195}
196
197} // namespace KJS
Note: See TracBrowser for help on using the repository browser.