1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * This file is part of the KDE libraries
|
---|
4 | * Copyright (C) 1999-2001 Harri Porten ([email protected])
|
---|
5 | * Copyright (C) 2001 Peter Kelly ([email protected])
|
---|
6 | * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc.
|
---|
7 | *
|
---|
8 | * This library is free software; you can redistribute it and/or
|
---|
9 | * modify it under the terms of the GNU Library General Public
|
---|
10 | * License as published by the Free Software Foundation; either
|
---|
11 | * version 2 of the License, or (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This library is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
16 | * Library General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU Library General Public License
|
---|
19 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
21 | * Boston, MA 02110-1301, USA.
|
---|
22 | *
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "config.h"
|
---|
26 | #include "object.h"
|
---|
27 |
|
---|
28 | #include "error_object.h"
|
---|
29 | #include "lookup.h"
|
---|
30 | #include "nodes.h"
|
---|
31 | #include "operations.h"
|
---|
32 | #include "reference_list.h"
|
---|
33 | #include <math.h>
|
---|
34 |
|
---|
35 | // maximum global call stack size. Protects against accidental or
|
---|
36 | // malicious infinite recursions. Define to -1 if you want no limit.
|
---|
37 | #if PLATFORM(DARWIN)
|
---|
38 | // Given OS X stack sizes we run out of stack at about 350 levels.
|
---|
39 | // If we improve our stack usage, we can bump this number.
|
---|
40 | #define KJS_MAX_STACK 100
|
---|
41 | #else
|
---|
42 | #define KJS_MAX_STACK 1000
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | #define JAVASCRIPT_CALL_TRACING 0
|
---|
46 |
|
---|
47 | #if JAVASCRIPT_CALL_TRACING
|
---|
48 | static bool _traceJavaScript = false;
|
---|
49 |
|
---|
50 | extern "C" {
|
---|
51 | void setTraceJavaScript(bool f)
|
---|
52 | {
|
---|
53 | _traceJavaScript = f;
|
---|
54 | }
|
---|
55 |
|
---|
56 | static bool traceJavaScript()
|
---|
57 | {
|
---|
58 | return _traceJavaScript;
|
---|
59 | }
|
---|
60 | }
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | namespace KJS {
|
---|
64 |
|
---|
65 | // ------------------------------ Object ---------------------------------------
|
---|
66 |
|
---|
67 | JSValue *JSObject::call(ExecState *exec, JSObject *thisObj, const List &args)
|
---|
68 | {
|
---|
69 | assert(implementsCall());
|
---|
70 |
|
---|
71 | #if KJS_MAX_STACK > 0
|
---|
72 | static int depth = 0; // sum of all concurrent interpreters
|
---|
73 |
|
---|
74 | #if JAVASCRIPT_CALL_TRACING
|
---|
75 | static bool tracing = false;
|
---|
76 | if (traceJavaScript() && !tracing) {
|
---|
77 | tracing = true;
|
---|
78 | for (int i = 0; i < depth; i++)
|
---|
79 | putchar (' ');
|
---|
80 | printf ("*** calling: %s\n", toString(exec).ascii());
|
---|
81 | for (int j = 0; j < args.size(); j++) {
|
---|
82 | for (int i = 0; i < depth; i++)
|
---|
83 | putchar (' ');
|
---|
84 | printf ("*** arg[%d] = %s\n", j, args[j]->toString(exec).ascii());
|
---|
85 | }
|
---|
86 | tracing = false;
|
---|
87 | }
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | if (++depth > KJS_MAX_STACK) {
|
---|
91 | --depth;
|
---|
92 | return throwError(exec, RangeError, "Maximum call stack size exceeded.");
|
---|
93 | }
|
---|
94 | #endif
|
---|
95 |
|
---|
96 | JSValue *ret = callAsFunction(exec,thisObj,args);
|
---|
97 |
|
---|
98 | #if KJS_MAX_STACK > 0
|
---|
99 | --depth;
|
---|
100 | #endif
|
---|
101 |
|
---|
102 | #if JAVASCRIPT_CALL_TRACING
|
---|
103 | if (traceJavaScript() && !tracing) {
|
---|
104 | tracing = true;
|
---|
105 | for (int i = 0; i < depth; i++)
|
---|
106 | putchar (' ');
|
---|
107 | printf ("*** returning: %s\n", ret->toString(exec).ascii());
|
---|
108 | tracing = false;
|
---|
109 | }
|
---|
110 | #endif
|
---|
111 |
|
---|
112 | return ret;
|
---|
113 | }
|
---|
114 |
|
---|
115 | // ------------------------------ JSObject ------------------------------------
|
---|
116 |
|
---|
117 | void JSObject::mark()
|
---|
118 | {
|
---|
119 | JSCell::mark();
|
---|
120 |
|
---|
121 | JSValue *proto = _proto;
|
---|
122 | if (!proto->marked())
|
---|
123 | proto->mark();
|
---|
124 |
|
---|
125 | _prop.mark();
|
---|
126 |
|
---|
127 | if (_internalValue && !_internalValue->marked())
|
---|
128 | _internalValue->mark();
|
---|
129 |
|
---|
130 | _scope.mark();
|
---|
131 | }
|
---|
132 |
|
---|
133 | JSType JSObject::type() const
|
---|
134 | {
|
---|
135 | return ObjectType;
|
---|
136 | }
|
---|
137 |
|
---|
138 | const ClassInfo *JSObject::classInfo() const
|
---|
139 | {
|
---|
140 | return 0;
|
---|
141 | }
|
---|
142 |
|
---|
143 | UString JSObject::className() const
|
---|
144 | {
|
---|
145 | const ClassInfo *ci = classInfo();
|
---|
146 | if ( ci )
|
---|
147 | return ci->className;
|
---|
148 | return "Object";
|
---|
149 | }
|
---|
150 |
|
---|
151 | JSValue *JSObject::get(ExecState *exec, const Identifier &propertyName) const
|
---|
152 | {
|
---|
153 | PropertySlot slot;
|
---|
154 |
|
---|
155 | if (const_cast<JSObject *>(this)->getPropertySlot(exec, propertyName, slot))
|
---|
156 | return slot.getValue(exec, const_cast<JSObject *>(this), propertyName);
|
---|
157 |
|
---|
158 | return jsUndefined();
|
---|
159 | }
|
---|
160 |
|
---|
161 | JSValue *JSObject::get(ExecState *exec, unsigned propertyName) const
|
---|
162 | {
|
---|
163 | PropertySlot slot;
|
---|
164 | if (const_cast<JSObject *>(this)->getPropertySlot(exec, propertyName, slot))
|
---|
165 | return slot.getValue(exec, const_cast<JSObject *>(this), propertyName);
|
---|
166 |
|
---|
167 | return jsUndefined();
|
---|
168 | }
|
---|
169 |
|
---|
170 | bool JSObject::getPropertySlot(ExecState *exec, unsigned propertyName, PropertySlot& slot)
|
---|
171 | {
|
---|
172 | JSObject *imp = this;
|
---|
173 |
|
---|
174 | while (true) {
|
---|
175 | if (imp->getOwnPropertySlot(exec, propertyName, slot))
|
---|
176 | return true;
|
---|
177 |
|
---|
178 | JSValue *proto = imp->_proto;
|
---|
179 | if (!proto->isObject())
|
---|
180 | break;
|
---|
181 |
|
---|
182 | imp = static_cast<JSObject *>(proto);
|
---|
183 | }
|
---|
184 |
|
---|
185 | return false;
|
---|
186 | }
|
---|
187 |
|
---|
188 | bool JSObject::getOwnPropertySlot(ExecState *exec, unsigned propertyName, PropertySlot& slot)
|
---|
189 | {
|
---|
190 | return getOwnPropertySlot(exec, Identifier::from(propertyName), slot);
|
---|
191 | }
|
---|
192 |
|
---|
193 | static void throwSetterError(ExecState *exec)
|
---|
194 | {
|
---|
195 | throwError(exec, TypeError, "setting a property that has only a getter");
|
---|
196 | }
|
---|
197 |
|
---|
198 | // ECMA 8.6.2.2
|
---|
199 | void JSObject::put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr)
|
---|
200 | {
|
---|
201 | assert(value);
|
---|
202 |
|
---|
203 | // non-standard netscape extension
|
---|
204 | if (propertyName == exec->dynamicInterpreter()->specialPrototypeIdentifier()) {
|
---|
205 | setPrototype(value);
|
---|
206 | return;
|
---|
207 | }
|
---|
208 |
|
---|
209 | /* TODO: check for write permissions directly w/o this call */
|
---|
210 | /* Doesn't look very easy with the PropertyMap API - David */
|
---|
211 | // putValue() is used for JS assignemnts. It passes no attribute.
|
---|
212 | // Assume that a C++ implementation knows what it is doing
|
---|
213 | // and let it override the canPut() check.
|
---|
214 | if ((attr == None || attr == DontDelete) && !canPut(exec,propertyName)) {
|
---|
215 | #ifdef KJS_VERBOSE
|
---|
216 | fprintf( stderr, "WARNING: canPut %s said NO\n", propertyName.ascii() );
|
---|
217 | #endif
|
---|
218 | return;
|
---|
219 | }
|
---|
220 |
|
---|
221 | // Check if there are any setters or getters in the prototype chain
|
---|
222 | JSObject *obj = this;
|
---|
223 | bool hasGettersOrSetters = false;
|
---|
224 | while (true) {
|
---|
225 | if (obj->_prop.hasGetterSetterProperties()) {
|
---|
226 | hasGettersOrSetters = true;
|
---|
227 | break;
|
---|
228 | }
|
---|
229 |
|
---|
230 | if (!obj->_proto->isObject())
|
---|
231 | break;
|
---|
232 |
|
---|
233 | obj = static_cast<JSObject *>(obj->_proto);
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (hasGettersOrSetters) {
|
---|
237 | obj = this;
|
---|
238 | while (true) {
|
---|
239 | int attributes;
|
---|
240 | if (JSValue *gs = obj->_prop.get(propertyName, attributes)) {
|
---|
241 | if (attributes & GetterSetter) {
|
---|
242 | JSObject *setterFunc = static_cast<GetterSetterImp *>(gs)->getSetter();
|
---|
243 |
|
---|
244 | if (!setterFunc) {
|
---|
245 | throwSetterError(exec);
|
---|
246 | return;
|
---|
247 | }
|
---|
248 |
|
---|
249 | List args;
|
---|
250 | args.append(value);
|
---|
251 |
|
---|
252 | setterFunc->call(exec, this, args);
|
---|
253 | return;
|
---|
254 | } else {
|
---|
255 | // If there's an existing property on the object or one of its
|
---|
256 | // prototype it should be replaced, so we just break here.
|
---|
257 | break;
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | if (!obj->_proto->isObject())
|
---|
262 | break;
|
---|
263 |
|
---|
264 | obj = static_cast<JSObject *>(obj->_proto);
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | _prop.put(propertyName,value,attr);
|
---|
269 | }
|
---|
270 |
|
---|
271 | void JSObject::put(ExecState *exec, unsigned propertyName,
|
---|
272 | JSValue *value, int attr)
|
---|
273 | {
|
---|
274 | put(exec, Identifier::from(propertyName), value, attr);
|
---|
275 | }
|
---|
276 |
|
---|
277 | // ECMA 8.6.2.3
|
---|
278 | bool JSObject::canPut(ExecState *, const Identifier &propertyName) const
|
---|
279 | {
|
---|
280 | int attributes;
|
---|
281 |
|
---|
282 | // Don't look in the prototype here. We can always put an override
|
---|
283 | // in the object, even if the prototype has a ReadOnly property.
|
---|
284 |
|
---|
285 | if (!getPropertyAttributes(propertyName, attributes))
|
---|
286 | return true;
|
---|
287 | else
|
---|
288 | return !(attributes & ReadOnly);
|
---|
289 | }
|
---|
290 |
|
---|
291 | // ECMA 8.6.2.4
|
---|
292 | bool JSObject::hasProperty(ExecState *exec, const Identifier &propertyName) const
|
---|
293 | {
|
---|
294 | PropertySlot slot;
|
---|
295 | return const_cast<JSObject *>(this)->getPropertySlot(exec, propertyName, slot);
|
---|
296 | }
|
---|
297 |
|
---|
298 | bool JSObject::hasProperty(ExecState *exec, unsigned propertyName) const
|
---|
299 | {
|
---|
300 | PropertySlot slot;
|
---|
301 | return const_cast<JSObject *>(this)->getPropertySlot(exec, propertyName, slot);
|
---|
302 | }
|
---|
303 |
|
---|
304 | // ECMA 8.6.2.5
|
---|
305 | bool JSObject::deleteProperty(ExecState */*exec*/, const Identifier &propertyName)
|
---|
306 | {
|
---|
307 | int attributes;
|
---|
308 | JSValue *v = _prop.get(propertyName, attributes);
|
---|
309 | if (v) {
|
---|
310 | if ((attributes & DontDelete))
|
---|
311 | return false;
|
---|
312 | _prop.remove(propertyName);
|
---|
313 | if (attributes & GetterSetter)
|
---|
314 | _prop.setHasGetterSetterProperties(_prop.containsGettersOrSetters());
|
---|
315 | return true;
|
---|
316 | }
|
---|
317 |
|
---|
318 | // Look in the static hashtable of properties
|
---|
319 | const HashEntry* entry = findPropertyHashEntry(propertyName);
|
---|
320 | if (entry && entry->attr & DontDelete)
|
---|
321 | return false; // this builtin property can't be deleted
|
---|
322 | return true;
|
---|
323 | }
|
---|
324 |
|
---|
325 | bool JSObject::deleteProperty(ExecState *exec, unsigned propertyName)
|
---|
326 | {
|
---|
327 | return deleteProperty(exec, Identifier::from(propertyName));
|
---|
328 | }
|
---|
329 |
|
---|
330 | static ALWAYS_INLINE JSValue *tryGetAndCallProperty(ExecState *exec, const JSObject *object, const Identifier &propertyName) {
|
---|
331 | JSValue *v = object->get(exec, propertyName);
|
---|
332 | if (v->isObject()) {
|
---|
333 | JSObject *o = static_cast<JSObject*>(v);
|
---|
334 | if (o->implementsCall()) { // spec says "not primitive type" but ...
|
---|
335 | JSObject *thisObj = const_cast<JSObject*>(object);
|
---|
336 | JSValue *def = o->call(exec, thisObj, List::empty());
|
---|
337 | JSType defType = def->type();
|
---|
338 | ASSERT(defType != GetterSetterType);
|
---|
339 | if (defType != ObjectType)
|
---|
340 | return def;
|
---|
341 | }
|
---|
342 | }
|
---|
343 | return NULL;
|
---|
344 | }
|
---|
345 |
|
---|
346 | // ECMA 8.6.2.6
|
---|
347 | JSValue *JSObject::defaultValue(ExecState *exec, JSType hint) const
|
---|
348 | {
|
---|
349 | Identifier firstPropertyName;
|
---|
350 | Identifier secondPropertyName;
|
---|
351 | /* Prefer String for Date objects */
|
---|
352 | if ((hint == StringType) || (hint != StringType) && (hint != NumberType) && (_proto == exec->lexicalInterpreter()->builtinDatePrototype())) {
|
---|
353 | firstPropertyName = toStringPropertyName;
|
---|
354 | secondPropertyName = valueOfPropertyName;
|
---|
355 | } else {
|
---|
356 | firstPropertyName = valueOfPropertyName;
|
---|
357 | secondPropertyName = toStringPropertyName;
|
---|
358 | }
|
---|
359 |
|
---|
360 | JSValue *v;
|
---|
361 | if ((v = tryGetAndCallProperty(exec, this, firstPropertyName)))
|
---|
362 | return v;
|
---|
363 | if ((v = tryGetAndCallProperty(exec, this, secondPropertyName)))
|
---|
364 | return v;
|
---|
365 |
|
---|
366 | if (exec->hadException())
|
---|
367 | return exec->exception();
|
---|
368 |
|
---|
369 | return throwError(exec, TypeError, "No default value");
|
---|
370 | }
|
---|
371 |
|
---|
372 | const HashEntry* JSObject::findPropertyHashEntry(const Identifier& propertyName) const
|
---|
373 | {
|
---|
374 | for (const ClassInfo *info = classInfo(); info; info = info->parentClass) {
|
---|
375 | if (const HashTable *propHashTable = info->propHashTable) {
|
---|
376 | if (const HashEntry *e = Lookup::findEntry(propHashTable, propertyName))
|
---|
377 | return e;
|
---|
378 | }
|
---|
379 | }
|
---|
380 | return 0;
|
---|
381 | }
|
---|
382 |
|
---|
383 | void JSObject::defineGetter(ExecState*, const Identifier& propertyName, JSObject* getterFunc)
|
---|
384 | {
|
---|
385 | JSValue *o = getDirect(propertyName);
|
---|
386 | GetterSetterImp *gs;
|
---|
387 |
|
---|
388 | if (o && o->type() == GetterSetterType) {
|
---|
389 | gs = static_cast<GetterSetterImp *>(o);
|
---|
390 | } else {
|
---|
391 | gs = new GetterSetterImp;
|
---|
392 | putDirect(propertyName, gs, GetterSetter);
|
---|
393 | }
|
---|
394 |
|
---|
395 | _prop.setHasGetterSetterProperties(true);
|
---|
396 | gs->setGetter(getterFunc);
|
---|
397 | }
|
---|
398 |
|
---|
399 | void JSObject::defineSetter(ExecState*, const Identifier& propertyName, JSObject* setterFunc)
|
---|
400 | {
|
---|
401 | JSValue *o = getDirect(propertyName);
|
---|
402 | GetterSetterImp *gs;
|
---|
403 |
|
---|
404 | if (o && o->type() == GetterSetterType) {
|
---|
405 | gs = static_cast<GetterSetterImp *>(o);
|
---|
406 | } else {
|
---|
407 | gs = new GetterSetterImp;
|
---|
408 | putDirect(propertyName, gs, GetterSetter);
|
---|
409 | }
|
---|
410 |
|
---|
411 | _prop.setHasGetterSetterProperties(true);
|
---|
412 | gs->setSetter(setterFunc);
|
---|
413 | }
|
---|
414 |
|
---|
415 | bool JSObject::implementsConstruct() const
|
---|
416 | {
|
---|
417 | return false;
|
---|
418 | }
|
---|
419 |
|
---|
420 | JSObject* JSObject::construct(ExecState*, const List& /*args*/)
|
---|
421 | {
|
---|
422 | assert(false);
|
---|
423 | return NULL;
|
---|
424 | }
|
---|
425 |
|
---|
426 | JSObject* JSObject::construct(ExecState* exec, const List& args, const Identifier& /*functionName*/, const UString& /*sourceURL*/, int /*lineNumber*/)
|
---|
427 | {
|
---|
428 | return construct(exec, args);
|
---|
429 | }
|
---|
430 |
|
---|
431 | bool JSObject::implementsCall() const
|
---|
432 | {
|
---|
433 | return false;
|
---|
434 | }
|
---|
435 |
|
---|
436 | JSValue *JSObject::callAsFunction(ExecState */*exec*/, JSObject */*thisObj*/, const List &/*args*/)
|
---|
437 | {
|
---|
438 | assert(false);
|
---|
439 | return NULL;
|
---|
440 | }
|
---|
441 |
|
---|
442 | bool JSObject::implementsHasInstance() const
|
---|
443 | {
|
---|
444 | return false;
|
---|
445 | }
|
---|
446 |
|
---|
447 | bool JSObject::hasInstance(ExecState *, JSValue *)
|
---|
448 | {
|
---|
449 | assert(false);
|
---|
450 | return false;
|
---|
451 | }
|
---|
452 |
|
---|
453 | bool JSObject::propertyIsEnumerable(ExecState*, const Identifier& propertyName) const
|
---|
454 | {
|
---|
455 | int attributes;
|
---|
456 |
|
---|
457 | if (!getPropertyAttributes(propertyName, attributes))
|
---|
458 | return false;
|
---|
459 | else
|
---|
460 | return !(attributes & DontEnum);
|
---|
461 | }
|
---|
462 |
|
---|
463 | bool JSObject::getPropertyAttributes(const Identifier& propertyName, int& attributes) const
|
---|
464 | {
|
---|
465 | if (_prop.get(propertyName, attributes))
|
---|
466 | return true;
|
---|
467 |
|
---|
468 | // Look in the static hashtable of properties
|
---|
469 | const HashEntry* e = findPropertyHashEntry(propertyName);
|
---|
470 | if (e) {
|
---|
471 | attributes = e->attr;
|
---|
472 | return true;
|
---|
473 | }
|
---|
474 |
|
---|
475 | return false;
|
---|
476 | }
|
---|
477 |
|
---|
478 | ReferenceList JSObject::propList(ExecState *exec, bool recursive)
|
---|
479 | {
|
---|
480 | ReferenceList list;
|
---|
481 | if (_proto->isObject() && recursive)
|
---|
482 | list = static_cast<JSObject*>(_proto)->propList(exec,recursive);
|
---|
483 |
|
---|
484 | _prop.addEnumerablesToReferenceList(list, this);
|
---|
485 |
|
---|
486 | // Add properties from the static hashtable of properties
|
---|
487 | const ClassInfo *info = classInfo();
|
---|
488 | while (info) {
|
---|
489 | if (info->propHashTable) {
|
---|
490 | int size = info->propHashTable->size;
|
---|
491 | const HashEntry *e = info->propHashTable->entries;
|
---|
492 | for (int i = 0; i < size; ++i, ++e) {
|
---|
493 | if ( e->s && !(e->attr & DontEnum) )
|
---|
494 | list.append(Reference(this, e->s)); /// ######### check for duplicates with the propertymap
|
---|
495 | }
|
---|
496 | }
|
---|
497 | info = info->parentClass;
|
---|
498 | }
|
---|
499 |
|
---|
500 | return list;
|
---|
501 | }
|
---|
502 |
|
---|
503 | bool JSObject::toBoolean(ExecState */*exec*/) const
|
---|
504 | {
|
---|
505 | return true;
|
---|
506 | }
|
---|
507 |
|
---|
508 | double JSObject::toNumber(ExecState *exec) const
|
---|
509 | {
|
---|
510 | JSValue *prim = toPrimitive(exec,NumberType);
|
---|
511 | if (exec->hadException()) // should be picked up soon in nodes.cpp
|
---|
512 | return 0.0;
|
---|
513 | return prim->toNumber(exec);
|
---|
514 | }
|
---|
515 |
|
---|
516 | UString JSObject::toString(ExecState *exec) const
|
---|
517 | {
|
---|
518 | JSValue *prim = toPrimitive(exec,StringType);
|
---|
519 | if (exec->hadException()) // should be picked up soon in nodes.cpp
|
---|
520 | return "";
|
---|
521 | return prim->toString(exec);
|
---|
522 | }
|
---|
523 |
|
---|
524 | JSObject *JSObject::toObject(ExecState */*exec*/) const
|
---|
525 | {
|
---|
526 | return const_cast<JSObject*>(this);
|
---|
527 | }
|
---|
528 |
|
---|
529 | void JSObject::putDirect(const Identifier &propertyName, JSValue *value, int attr)
|
---|
530 | {
|
---|
531 | _prop.put(propertyName, value, attr);
|
---|
532 | }
|
---|
533 |
|
---|
534 | void JSObject::putDirect(const Identifier &propertyName, int value, int attr)
|
---|
535 | {
|
---|
536 | _prop.put(propertyName, jsNumber(value), attr);
|
---|
537 | }
|
---|
538 |
|
---|
539 | void JSObject::putDirectFunction(InternalFunctionImp* func, int attr)
|
---|
540 | {
|
---|
541 | putDirect(func->functionName(), func, attr);
|
---|
542 | }
|
---|
543 |
|
---|
544 | void JSObject::fillGetterPropertySlot(PropertySlot& slot, JSValue **location)
|
---|
545 | {
|
---|
546 | GetterSetterImp *gs = static_cast<GetterSetterImp *>(*location);
|
---|
547 | JSObject *getterFunc = gs->getGetter();
|
---|
548 | if (getterFunc)
|
---|
549 | slot.setGetterSlot(this, getterFunc);
|
---|
550 | else
|
---|
551 | slot.setUndefined(this);
|
---|
552 | }
|
---|
553 |
|
---|
554 | // ------------------------------ Error ----------------------------------------
|
---|
555 |
|
---|
556 | const char * const errorNamesArr[] = {
|
---|
557 | I18N_NOOP("Error"), // GeneralError
|
---|
558 | I18N_NOOP("Evaluation error"), // EvalError
|
---|
559 | I18N_NOOP("Range error"), // RangeError
|
---|
560 | I18N_NOOP("Reference error"), // ReferenceError
|
---|
561 | I18N_NOOP("Syntax error"), // SyntaxError
|
---|
562 | I18N_NOOP("Type error"), // TypeError
|
---|
563 | I18N_NOOP("URI error"), // URIError
|
---|
564 | };
|
---|
565 |
|
---|
566 | const char * const * const Error::errorNames = errorNamesArr;
|
---|
567 |
|
---|
568 | JSObject *Error::create(ExecState *exec, ErrorType errtype, const UString &message,
|
---|
569 | int lineno, int sourceId, const UString *sourceURL)
|
---|
570 | {
|
---|
571 | JSObject *cons;
|
---|
572 | switch (errtype) {
|
---|
573 | case EvalError:
|
---|
574 | cons = exec->lexicalInterpreter()->builtinEvalError();
|
---|
575 | break;
|
---|
576 | case RangeError:
|
---|
577 | cons = exec->lexicalInterpreter()->builtinRangeError();
|
---|
578 | break;
|
---|
579 | case ReferenceError:
|
---|
580 | cons = exec->lexicalInterpreter()->builtinReferenceError();
|
---|
581 | break;
|
---|
582 | case SyntaxError:
|
---|
583 | cons = exec->lexicalInterpreter()->builtinSyntaxError();
|
---|
584 | break;
|
---|
585 | case TypeError:
|
---|
586 | cons = exec->lexicalInterpreter()->builtinTypeError();
|
---|
587 | break;
|
---|
588 | case URIError:
|
---|
589 | cons = exec->lexicalInterpreter()->builtinURIError();
|
---|
590 | break;
|
---|
591 | default:
|
---|
592 | cons = exec->lexicalInterpreter()->builtinError();
|
---|
593 | break;
|
---|
594 | }
|
---|
595 |
|
---|
596 | List args;
|
---|
597 | if (message.isEmpty())
|
---|
598 | args.append(jsString(errorNames[errtype]));
|
---|
599 | else
|
---|
600 | args.append(jsString(message));
|
---|
601 | JSObject *err = static_cast<JSObject *>(cons->construct(exec,args));
|
---|
602 |
|
---|
603 | if (lineno != -1)
|
---|
604 | err->put(exec, "line", jsNumber(lineno));
|
---|
605 | if (sourceId != -1)
|
---|
606 | err->put(exec, "sourceId", jsNumber(sourceId));
|
---|
607 |
|
---|
608 | if(sourceURL)
|
---|
609 | err->put(exec,"sourceURL", jsString(*sourceURL));
|
---|
610 |
|
---|
611 | return err;
|
---|
612 |
|
---|
613 | /*
|
---|
614 | #ifndef NDEBUG
|
---|
615 | const char *msg = err->get(messagePropertyName)->toString().value().ascii();
|
---|
616 | if (l >= 0)
|
---|
617 | fprintf(stderr, "KJS: %s at line %d. %s\n", estr, l, msg);
|
---|
618 | else
|
---|
619 | fprintf(stderr, "KJS: %s. %s\n", estr, msg);
|
---|
620 | #endif
|
---|
621 |
|
---|
622 | return err;
|
---|
623 | */
|
---|
624 | }
|
---|
625 |
|
---|
626 | JSObject *Error::create(ExecState *exec, ErrorType type, const char *message)
|
---|
627 | {
|
---|
628 | return create(exec, type, message, -1, -1, NULL);
|
---|
629 | }
|
---|
630 |
|
---|
631 | JSObject *throwError(ExecState *exec, ErrorType type)
|
---|
632 | {
|
---|
633 | JSObject *error = Error::create(exec, type, UString(), -1, -1, NULL);
|
---|
634 | exec->setException(error);
|
---|
635 | return error;
|
---|
636 | }
|
---|
637 |
|
---|
638 | JSObject *throwError(ExecState *exec, ErrorType type, const UString &message)
|
---|
639 | {
|
---|
640 | JSObject *error = Error::create(exec, type, message, -1, -1, NULL);
|
---|
641 | exec->setException(error);
|
---|
642 | return error;
|
---|
643 | }
|
---|
644 |
|
---|
645 | JSObject *throwError(ExecState *exec, ErrorType type, const char *message)
|
---|
646 | {
|
---|
647 | JSObject *error = Error::create(exec, type, message, -1, -1, NULL);
|
---|
648 | exec->setException(error);
|
---|
649 | return error;
|
---|
650 | }
|
---|
651 |
|
---|
652 | JSObject *throwError(ExecState *exec, ErrorType type, const UString &message, int line, int sourceId, const UString *sourceURL)
|
---|
653 | {
|
---|
654 | JSObject *error = Error::create(exec, type, message, line, sourceId, sourceURL);
|
---|
655 | exec->setException(error);
|
---|
656 | return error;
|
---|
657 | }
|
---|
658 |
|
---|
659 | } // namespace KJS
|
---|