source: webkit/trunk/JavaScriptCore/kjs/internal.cpp@ 34852

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

2008-06-27 Sam Weinig <[email protected]>

Rubber-stamped by Oliver Hunt.

Splits ArrayConstructor out of ArrayPrototype.h/cpp
Splits BooleanConstructor and BooleanPrototype out of BooleanObject.h/cpp

  • GNUmakefile.am:
  • JavaScriptCore.pri:
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • JavaScriptCoreSources.bkl:
  • VM/Machine.cpp:
  • kjs/AllInOneFile.cpp:
  • kjs/ArrayConstructor.cpp: Copied from kjs/ArrayPrototype.cpp.
  • kjs/ArrayConstructor.h: Copied from kjs/ArrayPrototype.h.
  • kjs/ArrayPrototype.cpp:
  • kjs/ArrayPrototype.h:
  • kjs/BooleanConstructor.cpp: Copied from kjs/BooleanObject.cpp.
  • kjs/BooleanConstructor.h: Copied from kjs/BooleanObject.h.
  • kjs/BooleanObject.cpp:
  • kjs/BooleanObject.h:
  • kjs/BooleanPrototype.cpp: Copied from kjs/BooleanObject.cpp.
  • kjs/BooleanPrototype.h: Copied from kjs/BooleanObject.h.
  • kjs/CommonIdentifiers.h:
  • kjs/FunctionPrototype.cpp:
  • kjs/JSArray.cpp:
  • kjs/JSGlobalObject.cpp:
  • kjs/JSImmediate.cpp:
  • kjs/Shell.cpp:
  • kjs/internal.cpp:
  • kjs/nodes.cpp:
  • kjs/string_object.cpp:
  • Property svn:eol-style set to native
File size: 7.8 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 "ExecState.h"
27#include "collector.h"
28#include "date_object.h"
29#include "debugger.h"
30#include "error_object.h"
31#include "FunctionPrototype.h"
32#include "lexer.h"
33#include "MathObject.h"
34#include "nodes.h"
35#include "NumberObject.h"
36#include "JSObject.h"
37#include "object_object.h"
38#include "operations.h"
39#include "RegExpObject.h"
40#include "string_object.h"
41#include <math.h>
42#include <stdio.h>
43#include <wtf/Assertions.h>
44#include <wtf/HashMap.h>
45#include <wtf/HashSet.h>
46#include <wtf/Vector.h>
47
48namespace KJS {
49
50// ------------------------------ JSString ------------------------------------
51
52JSValue* JSString::toPrimitive(ExecState*, JSType) const
53{
54 return const_cast<JSString*>(this);
55}
56
57bool JSString::getPrimitiveNumber(ExecState*, double& number, JSValue*& value)
58{
59 value = this;
60 number = m_value.toDouble();
61 return false;
62}
63
64bool JSString::toBoolean(ExecState*) const
65{
66 return !m_value.isEmpty();
67}
68
69double JSString::toNumber(ExecState*) const
70{
71 return m_value.toDouble();
72}
73
74UString JSString::toString(ExecState*) const
75{
76 return m_value;
77}
78
79UString JSString::toThisString(ExecState*) const
80{
81 return m_value;
82}
83
84JSString* JSString::toThisJSString(ExecState*)
85{
86 return this;
87}
88
89inline StringObject* StringObject::create(ExecState* exec, JSString* string)
90{
91 return new (exec) StringObject(exec->lexicalGlobalObject()->stringPrototype(), string);
92}
93
94JSObject* JSString::toObject(ExecState* exec) const
95{
96 return StringObject::create(exec, const_cast<JSString*>(this));
97}
98
99JSObject* JSString::toThisObject(ExecState* exec) const
100{
101 return StringObject::create(exec, const_cast<JSString*>(this));
102}
103
104JSValue* JSString::lengthGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
105{
106 return jsNumber(exec, static_cast<JSString*>(slot.slotBase())->value().size());
107}
108
109JSValue* JSString::indexGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
110{
111 return jsString(exec, static_cast<JSString*>(slot.slotBase())->value().substr(slot.index(), 1));
112}
113
114JSValue* JSString::indexNumericPropertyGetter(ExecState* exec, unsigned index, const PropertySlot& slot)
115{
116 return jsString(exec, static_cast<JSString*>(slot.slotBase())->value().substr(index, 1));
117}
118
119bool JSString::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
120{
121 // The semantics here are really getPropertySlot, not getOwnPropertySlot.
122 // This function should only be called by JSValue::get.
123 if (getStringPropertySlot(exec, propertyName, slot))
124 return true;
125 slot.setBase(this);
126 JSObject* object;
127 for (JSValue* prototype = exec->lexicalGlobalObject()->stringPrototype(); prototype != jsNull(); prototype = object->prototype()) {
128 ASSERT(prototype->isObject());
129 object = static_cast<JSObject*>(prototype);
130 if (object->getOwnPropertySlot(exec, propertyName, slot))
131 return true;
132 }
133 slot.setUndefined();
134 return true;
135}
136
137bool JSString::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
138{
139 // The semantics here are really getPropertySlot, not getOwnPropertySlot.
140 // This function should only be called by JSValue::get.
141 if (getStringPropertySlot(propertyName, slot))
142 return true;
143 return JSString::getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);
144}
145
146// ------------------------------ JSNumberCell ------------------------------------
147
148JSType JSNumberCell::type() const
149{
150 return NumberType;
151}
152
153JSValue* JSNumberCell::toPrimitive(ExecState*, JSType) const
154{
155 return const_cast<JSNumberCell*>(this);
156}
157
158bool JSNumberCell::getPrimitiveNumber(ExecState*, double& number, JSValue*& value)
159{
160 number = val;
161 value = this;
162 return true;
163}
164
165bool JSNumberCell::toBoolean(ExecState *) const
166{
167 return val < 0.0 || val > 0.0; // false for NaN
168}
169
170double JSNumberCell::toNumber(ExecState *) const
171{
172 return val;
173}
174
175UString JSNumberCell::toString(ExecState*) const
176{
177 if (val == 0.0) // +0.0 or -0.0
178 return "0";
179 return UString::from(val);
180}
181
182UString JSNumberCell::toThisString(ExecState*) const
183{
184 if (val == 0.0) // +0.0 or -0.0
185 return "0";
186 return UString::from(val);
187}
188
189JSObject* JSNumberCell::toObject(ExecState* exec) const
190{
191 return constructNumber(exec, const_cast<JSNumberCell*>(this));
192}
193
194JSObject* JSNumberCell::toThisObject(ExecState* exec) const
195{
196 return constructNumber(exec, const_cast<JSNumberCell*>(this));
197}
198
199bool JSNumberCell::getUInt32(uint32_t& uint32) const
200{
201 uint32 = static_cast<uint32_t>(val);
202 return uint32 == val;
203}
204
205bool JSNumberCell::getTruncatedInt32(int32_t& int32) const
206{
207 if (!(val >= -2147483648.0 && val < 2147483648.0))
208 return false;
209 int32 = static_cast<int32_t>(val);
210 return true;
211}
212
213bool JSNumberCell::getTruncatedUInt32(uint32_t& uint32) const
214{
215 if (!(val >= 0.0 && val < 4294967296.0))
216 return false;
217 uint32 = static_cast<uint32_t>(val);
218 return true;
219}
220
221JSValue* JSNumberCell::getJSNumber()
222{
223 return this;
224}
225
226// --------------------------- GetterSetter ---------------------------------
227
228void GetterSetter::mark()
229{
230 JSCell::mark();
231
232 if (m_getter && !m_getter->marked())
233 m_getter->mark();
234 if (m_setter && !m_setter->marked())
235 m_setter->mark();
236}
237
238JSValue* GetterSetter::toPrimitive(ExecState*, JSType) const
239{
240 ASSERT_NOT_REACHED();
241 return jsNull();
242}
243
244bool GetterSetter::getPrimitiveNumber(ExecState*, double& number, JSValue*& value)
245{
246 ASSERT_NOT_REACHED();
247 number = 0;
248 value = 0;
249 return true;
250}
251
252bool GetterSetter::toBoolean(ExecState*) const
253{
254 ASSERT_NOT_REACHED();
255 return false;
256}
257
258double GetterSetter::toNumber(ExecState*) const
259{
260 ASSERT_NOT_REACHED();
261 return 0.0;
262}
263
264UString GetterSetter::toString(ExecState*) const
265{
266 ASSERT_NOT_REACHED();
267 return UString::null();
268}
269
270JSObject* GetterSetter::toObject(ExecState* exec) const
271{
272 ASSERT_NOT_REACHED();
273 return jsNull()->toObject(exec);
274}
275
276// ------------------------------ LabelStack -----------------------------------
277
278bool LabelStack::push(const Identifier &id)
279{
280 if (contains(id))
281 return false;
282
283 StackElem *newtos = new StackElem;
284 newtos->id = id;
285 newtos->prev = tos;
286 tos = newtos;
287 return true;
288}
289
290bool LabelStack::contains(const Identifier &id) const
291{
292 if (id.isEmpty())
293 return true;
294
295 for (StackElem *curr = tos; curr; curr = curr->prev)
296 if (curr->id == id)
297 return true;
298
299 return false;
300}
301
302// ------------------------------ InternalFunction --------------------------
303
304const ClassInfo InternalFunction::info = { "Function", 0, 0, 0 };
305
306InternalFunction::InternalFunction()
307{
308}
309
310InternalFunction::InternalFunction(FunctionPrototype* prototype, const Identifier& name)
311 : JSObject(prototype)
312 , m_name(name)
313{
314}
315
316bool InternalFunction::implementsHasInstance() const
317{
318 return true;
319}
320
321}
Note: See TracBrowser for help on using the repository browser.