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