source: webkit/trunk/JavaScriptCore/kjs/JSString.cpp@ 36726

Last change on this file since 36726 was 36726, checked in by Darin Adler, 17 years ago

JavaScriptCore:

2008-09-20 Darin Adler <Darin Adler>

Reviewed by Maciej Stachowiak.

This also includes some optimizations that make the change an overall
small speedup. Without those it was a bit of a slowdown.

  • API/JSCallbackConstructor.cpp: (JSC::JSCallbackConstructor::JSCallbackConstructor): Take a structure.
  • API/JSCallbackConstructor.h: Ditto.
  • API/JSCallbackFunction.cpp: (JSC::JSCallbackFunction::JSCallbackFunction): Pass a structure.
  • API/JSCallbackObject.h: Take a structure.
  • API/JSCallbackObjectFunctions.h: (JSC::JSCallbackObject::JSCallbackObject): Ditto.
  • API/JSClassRef.cpp: (OpaqueJSClass::prototype): Pass in a structure. Call setPrototype if there's a custom prototype involved.
  • API/JSObjectRef.cpp: (JSObjectMake): Ditto. (JSObjectMakeConstructor): Pass in a structure.
  • VM/Machine.cpp: (JSC::jsLess): Added a special case for when both arguments are strings. This avoids converting both strings to with UString::toDouble. (JSC::jsLessEq): Ditto. (JSC::Machine::privateExecute): Pass in a structure. (JSC::Machine::cti_op_construct_JSConstruct): Ditto. (JSC::Machine::cti_op_new_regexp): Ditto. (JSC::Machine::cti_op_is_string): Ditto.
  • VM/Machine.h: Made isJSString public so it can be used in the CTI.
  • kjs/Arguments.cpp: (JSC::Arguments::Arguments): Pass in a structure.
  • kjs/JSCell.h: Mark constructor explicit.
  • kjs/JSGlobalObject.cpp: (JSC::markIfNeeded): Added an overload for marking structures. (JSC::JSGlobalObject::reset): Eliminate code to set data members to zero. We now do that in the constructor, and we no longer use this anywhere except in the constructor. Added code to create structures. Pass structures rather than prototypes when creating objects. (JSC::JSGlobalObject::mark): Mark the structures.
  • kjs/JSGlobalObject.h: Removed unneeded class declarations. Added initializers for raw pointers in JSGlobalObjectData so everything starts with a 0. Added structure data and accessor functions.
  • kjs/JSImmediate.cpp: (JSC::JSImmediate::nonInlineNaN): Added.
  • kjs/JSImmediate.h: (JSC::JSImmediate::toDouble): Rewrote to avoid PIC branches.
  • kjs/JSNumberCell.cpp: (JSC::jsNumberCell): Made non-inline to avoid PIC branches in functions that call this one. (JSC::jsNaN): Ditto.
  • kjs/JSNumberCell.h: Ditto.
  • kjs/JSObject.h: Removed constructor that takes a prototype. All callers now pass structures.
  • kjs/ArrayConstructor.cpp: (JSC::ArrayConstructor::ArrayConstructor): (JSC::constructArrayWithSizeQuirk):
  • kjs/ArrayConstructor.h:
  • kjs/ArrayPrototype.cpp: (JSC::ArrayPrototype::ArrayPrototype):
  • kjs/ArrayPrototype.h:
  • kjs/BooleanConstructor.cpp: (JSC::BooleanConstructor::BooleanConstructor): (JSC::constructBoolean): (JSC::constructBooleanFromImmediateBoolean):
  • kjs/BooleanConstructor.h:
  • kjs/BooleanObject.cpp: (JSC::BooleanObject::BooleanObject):
  • kjs/BooleanObject.h:
  • kjs/BooleanPrototype.cpp: (JSC::BooleanPrototype::BooleanPrototype):
  • kjs/BooleanPrototype.h:
  • kjs/DateConstructor.cpp: (JSC::DateConstructor::DateConstructor): (JSC::constructDate):
  • kjs/DateConstructor.h:
  • kjs/DateInstance.cpp: (JSC::DateInstance::DateInstance):
  • kjs/DateInstance.h:
  • kjs/DatePrototype.cpp: (JSC::DatePrototype::DatePrototype):
  • kjs/DatePrototype.h:
  • kjs/ErrorConstructor.cpp: (JSC::ErrorConstructor::ErrorConstructor): (JSC::constructError):
  • kjs/ErrorConstructor.h:
  • kjs/ErrorInstance.cpp: (JSC::ErrorInstance::ErrorInstance):
  • kjs/ErrorInstance.h:
  • kjs/ErrorPrototype.cpp: (JSC::ErrorPrototype::ErrorPrototype):
  • kjs/ErrorPrototype.h:
  • kjs/FunctionConstructor.cpp: (JSC::FunctionConstructor::FunctionConstructor):
  • kjs/FunctionConstructor.h:
  • kjs/FunctionPrototype.cpp: (JSC::FunctionPrototype::FunctionPrototype): (JSC::FunctionPrototype::addFunctionProperties):
  • kjs/FunctionPrototype.h:
  • kjs/GlobalEvalFunction.cpp: (JSC::GlobalEvalFunction::GlobalEvalFunction):
  • kjs/GlobalEvalFunction.h:
  • kjs/InternalFunction.cpp: (JSC::InternalFunction::InternalFunction):
  • kjs/InternalFunction.h: (JSC::InternalFunction::InternalFunction):
  • kjs/JSArray.cpp: (JSC::JSArray::JSArray): (JSC::constructEmptyArray): (JSC::constructArray):
  • kjs/JSArray.h:
  • kjs/JSFunction.cpp: (JSC::JSFunction::JSFunction): (JSC::JSFunction::construct):
  • kjs/JSObject.cpp: (JSC::constructEmptyObject):
  • kjs/JSString.cpp: (JSC::StringObject::create):
  • kjs/JSWrapperObject.h:
  • kjs/MathObject.cpp: (JSC::MathObject::MathObject):
  • kjs/MathObject.h:
  • kjs/NativeErrorConstructor.cpp: (JSC::NativeErrorConstructor::NativeErrorConstructor): (JSC::NativeErrorConstructor::construct):
  • kjs/NativeErrorConstructor.h:
  • kjs/NativeErrorPrototype.cpp: (JSC::NativeErrorPrototype::NativeErrorPrototype):
  • kjs/NativeErrorPrototype.h:
  • kjs/NumberConstructor.cpp: (JSC::NumberConstructor::NumberConstructor): (JSC::constructWithNumberConstructor):
  • kjs/NumberConstructor.h:
  • kjs/NumberObject.cpp: (JSC::NumberObject::NumberObject): (JSC::constructNumber): (JSC::constructNumberFromImmediateNumber):
  • kjs/NumberObject.h:
  • kjs/NumberPrototype.cpp: (JSC::NumberPrototype::NumberPrototype):
  • kjs/NumberPrototype.h:
  • kjs/ObjectConstructor.cpp: (JSC::ObjectConstructor::ObjectConstructor): (JSC::constructObject):
  • kjs/ObjectConstructor.h:
  • kjs/ObjectPrototype.cpp: (JSC::ObjectPrototype::ObjectPrototype):
  • kjs/ObjectPrototype.h:
  • kjs/PrototypeFunction.cpp: (JSC::PrototypeFunction::PrototypeFunction):
  • kjs/PrototypeFunction.h:
  • kjs/RegExpConstructor.cpp: (JSC::RegExpConstructor::RegExpConstructor): (JSC::RegExpMatchesArray::RegExpMatchesArray): (JSC::constructRegExp):
  • kjs/RegExpConstructor.h:
  • kjs/RegExpObject.cpp: (JSC::RegExpObject::RegExpObject):
  • kjs/RegExpObject.h:
  • kjs/RegExpPrototype.cpp: (JSC::RegExpPrototype::RegExpPrototype):
  • kjs/RegExpPrototype.h:
  • kjs/Shell.cpp: (GlobalObject::GlobalObject):
  • kjs/StringConstructor.cpp: (JSC::StringConstructor::StringConstructor): (JSC::constructWithStringConstructor):
  • kjs/StringConstructor.h:
  • kjs/StringObject.cpp: (JSC::StringObject::StringObject):
  • kjs/StringObject.h:
  • kjs/StringObjectThatMasqueradesAsUndefined.h: (JSC::StringObjectThatMasqueradesAsUndefined::StringObjectThatMasqueradesAsUndefined):
  • kjs/StringPrototype.cpp: (JSC::StringPrototype::StringPrototype):
  • kjs/StringPrototype.h: Take and pass structures.

WebCore:

2008-09-20 Darin Adler <Darin Adler>

Reviewed by Maciej Stachowiak.

  • bindings/js/JSCSSStyleDeclarationCustom.cpp: (WebCore::JSCSSStyleDeclaration::nameGetter): Pass in a structure ID. Note that this makes a new structure every time -- we could optimize this slightly be caching and reusing a single one.
  • bridge/runtime_method.cpp: (JSC::RuntimeMethod::RuntimeMethod): Create a unique structure using getDOMStructure.
  • bridge/runtime_method.h: (JSC::RuntimeMethod::createPrototype): Added createPrototype so getDOMStructure will work.
  • bindings/js/JSDOMWindowShell.cpp: (WebCore::JSDOMWindowShell::JSDOMWindowShell): Initialize m_window to 0; needed in case garbage collection happens while creating the JSDOMWindow.
  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1/*
2 * Copyright (C) 1999-2002 Harri Porten ([email protected])
3 * Copyright (C) 2001 Peter Kelly ([email protected])
4 * Copyright (C) 2004, 2007, 2008 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "config.h"
24#include "JSString.h"
25
26#include "JSGlobalObject.h"
27#include "JSObject.h"
28#include "StringObject.h"
29#include "StringPrototype.h"
30
31namespace JSC {
32
33JSValue* JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
34{
35 return const_cast<JSString*>(this);
36}
37
38bool JSString::getPrimitiveNumber(ExecState*, double& number, JSValue*& value)
39{
40 value = this;
41 number = m_value.toDouble();
42 return false;
43}
44
45bool JSString::toBoolean(ExecState*) const
46{
47 return !m_value.isEmpty();
48}
49
50double JSString::toNumber(ExecState*) const
51{
52 return m_value.toDouble();
53}
54
55UString JSString::toString(ExecState*) const
56{
57 return m_value;
58}
59
60UString JSString::toThisString(ExecState*) const
61{
62 return m_value;
63}
64
65JSString* JSString::toThisJSString(ExecState*)
66{
67 return this;
68}
69
70inline StringObject* StringObject::create(ExecState* exec, JSString* string)
71{
72 return new (exec) StringObject(exec->lexicalGlobalObject()->stringObjectStructure(), string);
73}
74
75JSObject* JSString::toObject(ExecState* exec) const
76{
77 return StringObject::create(exec, const_cast<JSString*>(this));
78}
79
80JSObject* JSString::toThisObject(ExecState* exec) const
81{
82 return StringObject::create(exec, const_cast<JSString*>(this));
83}
84
85bool JSString::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
86{
87 // The semantics here are really getPropertySlot, not getOwnPropertySlot.
88 // This function should only be called by JSValue::get.
89 if (getStringPropertySlot(exec, propertyName, slot))
90 return true;
91 slot.setBase(this);
92 JSObject* object;
93 for (JSValue* prototype = exec->lexicalGlobalObject()->stringPrototype(); !prototype->isNull(); prototype = object->prototype()) {
94 ASSERT(prototype->isObject());
95 object = static_cast<JSObject*>(prototype);
96 if (object->getOwnPropertySlot(exec, propertyName, slot))
97 return true;
98 }
99 slot.setUndefined();
100 return true;
101}
102
103bool JSString::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
104{
105 // The semantics here are really getPropertySlot, not getOwnPropertySlot.
106 // This function should only be called by JSValue::get.
107 if (getStringPropertySlot(exec, propertyName, slot))
108 return true;
109 return JSString::getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);
110}
111
112JSString* jsString(ExecState* exec, const UString& s)
113{
114 int size = s.size();
115 if (!size)
116 return exec->globalData().smallStrings.emptyString(exec);
117 if (size == 1) {
118 UChar c = s.data()[0];
119 if (c <= 0xFF)
120 return exec->globalData().smallStrings.singleCharacterString(exec, c);
121 }
122 return new (exec) JSString(exec, s);
123}
124
125JSString* jsSubstring(ExecState* exec, const UString& s, unsigned offset, unsigned length)
126{
127 ASSERT(offset <= static_cast<unsigned>(s.size()));
128 ASSERT(length <= static_cast<unsigned>(s.size()));
129 ASSERT(offset + length <= static_cast<unsigned>(s.size()));
130 if (!length)
131 return exec->globalData().smallStrings.emptyString(exec);
132 if (length == 1) {
133 UChar c = s.data()[offset];
134 if (c <= 0xFF)
135 return exec->globalData().smallStrings.singleCharacterString(exec, c);
136 }
137 return new (exec) JSString(exec, UString::Rep::create(s.rep(), offset, length));
138}
139
140JSString* jsOwnedString(ExecState* exec, const UString& s)
141{
142 int size = s.size();
143 if (!size)
144 return exec->globalData().smallStrings.emptyString(exec);
145 if (size == 1) {
146 UChar c = s.data()[0];
147 if (c <= 0xFF)
148 return exec->globalData().smallStrings.singleCharacterString(exec, c);
149 }
150 return new (exec) JSString(exec, s, JSString::HasOtherOwner);
151}
152
153} // namespace JSC
Note: See TracBrowser for help on using the repository browser.