source: webkit/trunk/JavaScriptCore/kjs/JSString.h@ 35203

Last change on this file since 35203 was 35203, checked in by [email protected], 17 years ago

JavaScriptCore:

2008-07-16 Geoffrey Garen <[email protected]>

Reviewed by Oliver Hunt.


First step toward putting doubles in registers: Turned Register into a
proper abstraction layer. It is no longer possible to cast a Register
to a JSValue*, or a Register& to a JSValue*&, or to access the union
inside a Register directly.


SunSpider reports no change.


In support of this change, I had to make the following mechanical changes
in a lot of places:


  1. Clients now use explicit accessors to read data out of Registers, and implicit copy constructors to write data into registers.


So, assignment that used to look like


x.u.jsValue = y;


now looks like


x = y;


And access that used to look like


x = y.u.jsValue;


now looks like


x = y.jsValue();

  1. I made generic flow control specific in opcodes that made their flow control generic by treating a Register& as a JSValue*&. This had the added benefit of removing some exception checking branches from immediate number code.
  1. I beefed up PropertySlot to support storing a Register* in a property slot. For now, only JSVariableObject's symbolTableGet and symbolTablePut use this functionality, but I expect more clients to use it in the future.


  1. I changed ArgList to be a buffer of Registers, not JSValue*'s, and I changed ArgList iterator clients to iterate Registers, not JSValue*'s.

WebCore:

2008-07-16 Geoffrey Garen <[email protected]>

Reviewed by Oliver Hunt.

Support for JavaScriptCore's first step toward putting doubles in
registers: Treat ArgList iterators as Register*'s, not JSValue*'s.

  • bindings/js/ScheduledAction.cpp: (WebCore::ScheduledAction::ScheduledAction):
  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * Copyright (C) 1999-2001 Harri Porten ([email protected])
4 * Copyright (C) 2001 Peter Kelly ([email protected])
5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
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 JSString_h
25#define JSString_h
26
27#include "CommonIdentifiers.h"
28#include "ExecState.h"
29#include "JSCell.h"
30#include "PropertySlot.h"
31#include "identifier.h"
32#include "ustring.h"
33
34namespace KJS {
35
36 class JSString : public JSCell {
37 public:
38 JSString(const UString& value)
39 : m_value(value)
40 {
41 Heap::heap(this)->reportExtraMemoryCost(value.cost());
42 }
43
44 enum HasOtherOwnerType { HasOtherOwner };
45 JSString(const UString& value, HasOtherOwnerType)
46 : m_value(value)
47 {
48 }
49
50 const UString& value() const { return m_value; }
51
52 bool getStringPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
53 bool getStringPropertySlot(unsigned propertyName, PropertySlot&);
54
55 bool canGetIndex(unsigned i) { return i < static_cast<unsigned>(m_value.size()); }
56 JSValue* getIndex(ExecState* exec, unsigned i)
57 {
58 ASSERT(canGetIndex(i));
59 return new (exec) JSString(m_value.substr(i, 1));
60 }
61
62 private:
63 virtual JSType type() const { return StringType; }
64
65 virtual JSValue* toPrimitive(ExecState*, JSType preferred = UnspecifiedType) const;
66 virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*& value);
67 virtual bool toBoolean(ExecState*) const;
68 virtual double toNumber(ExecState*) const;
69 virtual JSObject* toObject(ExecState*) const;
70 virtual UString toString(ExecState*) const;
71
72 virtual JSObject* toThisObject(ExecState*) const;
73 virtual UString toThisString(ExecState*) const;
74 virtual JSString* toThisJSString(ExecState*);
75
76 // Actually getPropertySlot, not getOwnPropertySlot (see JSCell).
77 virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
78 virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
79
80 static JSValue* lengthGetter(ExecState*, const Identifier&, const PropertySlot&);
81 static JSValue* indexGetter(ExecState*, const Identifier&, const PropertySlot&);
82 static JSValue* indexNumericPropertyGetter(ExecState*, unsigned, const PropertySlot&);
83
84 UString m_value;
85 };
86
87 JSString* jsString(ExecState*, const UString&); // returns empty string if passed null string
88 JSString* jsString(ExecState*, const char* = ""); // returns empty string if passed 0
89
90 // Should be used for strings that are owned by an object that will
91 // likely outlive the JSValue this makes, such as the parse tree or a
92 // DOM object that contains a UString
93 JSString* jsOwnedString(ExecState*, const UString&);
94
95 ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
96 {
97 if (propertyName == exec->propertyNames().length) {
98 slot.setCustom(this, lengthGetter);
99 return true;
100 }
101
102 bool isStrictUInt32;
103 unsigned i = propertyName.toStrictUInt32(&isStrictUInt32);
104 if (isStrictUInt32 && i < static_cast<unsigned>(m_value.size())) {
105 slot.setCustomIndex(this, i, indexGetter);
106 return true;
107 }
108
109 return false;
110 }
111
112 ALWAYS_INLINE bool JSString::getStringPropertySlot(unsigned propertyName, PropertySlot& slot)
113 {
114 if (propertyName < static_cast<unsigned>(m_value.size())) {
115 slot.setCustomNumeric(this, indexNumericPropertyGetter);
116 return true;
117 }
118
119 return false;
120 }
121
122 // --- JSValue inlines ----------------------------
123
124 inline JSString* JSValue::toThisJSString(ExecState* exec)
125 {
126 return JSImmediate::isImmediate(this) ? jsString(exec, JSImmediate::toString(this)) : asCell()->toThisJSString(exec);
127 }
128
129} // namespace KJS
130
131#endif // JSString_h
Note: See TracBrowser for help on using the repository browser.