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