1 // -*- c-basic-offset: 2 -*-
3 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
4 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
5 * Copyright (C) 2003, 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
6 * Copyright (C) 2007 Eric Seidel (eric@webkit.org)
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.
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.
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.
28 #include "date_object.h"
29 #include "error_object.h"
31 #include "operations.h"
32 #include "PropertyNameArray.h"
34 #include <profiler/Profiler.h>
35 #include <wtf/Assertions.h>
37 #define JAVASCRIPT_MARK_TRACING 0
41 // ------------------------------ Object ---------------------------------------
43 JSValue *JSObject::call(ExecState *exec, JSObject *thisObj, const List &args)
45 ASSERT(implementsCall());
47 #if JAVASCRIPT_PROFILING
48 Profiler::profiler()->willExecute(exec, this);
51 JSValue *ret = callAsFunction(exec,thisObj,args);
53 #if JAVASCRIPT_PROFILING
54 Profiler::profiler()->didExecute(exec, this);
60 // ------------------------------ JSObject ------------------------------------
66 #if JAVASCRIPT_MARK_TRACING
67 static int markStackDepth = 0;
69 for (int i = 0; i < markStackDepth; i++)
72 printf("%s (%p)\n", className().UTF8String().c_str(), this);
75 JSValue *proto = _proto;
81 #if JAVASCRIPT_MARK_TRACING
86 JSType JSObject::type() const
91 const ClassInfo *JSObject::classInfo() const
96 UString JSObject::className() const
98 const ClassInfo *ci = classInfo();
100 return ci->className;
104 bool JSObject::getOwnPropertySlot(ExecState *exec, unsigned propertyName, PropertySlot& slot)
106 return getOwnPropertySlot(exec, Identifier::from(propertyName), slot);
109 static void throwSetterError(ExecState *exec)
111 throwError(exec, TypeError, "setting a property that has only a getter");
115 void JSObject::put(ExecState* exec, const Identifier &propertyName, JSValue *value)
119 if (propertyName == exec->propertyNames().underscoreProto) {
120 JSObject* proto = value->getObject();
122 // Setting __proto__ to a non-object, non-null value is silently ignored to match Mozilla
123 if (!proto && value != jsNull())
128 throwError(exec, GeneralError, "cyclic __proto__ value");
131 proto = proto->prototype() ? proto->prototype()->getObject() : 0;
138 // Check if there are any setters or getters in the prototype chain
139 JSObject *obj = this;
140 bool hasGettersOrSetters = false;
142 if (obj->_prop.hasGetterSetterProperties()) {
143 hasGettersOrSetters = true;
147 if (obj->_proto == jsNull())
150 obj = static_cast<JSObject *>(obj->_proto);
153 if (hasGettersOrSetters) {
155 if (_prop.get(propertyName, attributes) && attributes & ReadOnly)
160 if (JSValue *gs = obj->_prop.get(propertyName, attributes)) {
161 if (attributes & GetterSetter) {
162 JSObject *setterFunc = static_cast<GetterSetterImp *>(gs)->getSetter();
165 throwSetterError(exec);
172 setterFunc->call(exec, this->toThisObject(exec), args);
175 // If there's an existing property on the object or one of its
176 // prototype it should be replaced, so we just break here.
181 if (!obj->_proto->isObject())
184 obj = static_cast<JSObject *>(obj->_proto);
188 _prop.put(propertyName, value, 0, true);
191 void JSObject::put(ExecState* exec, unsigned propertyName, JSValue* value)
193 put(exec, Identifier::from(propertyName), value);
196 void JSObject::putWithAttributes(ExecState*, const Identifier& propertyName, JSValue* value, unsigned attributes)
198 putDirect(propertyName, value, attributes);
201 void JSObject::putWithAttributes(ExecState* exec, unsigned propertyName, JSValue* value, unsigned attributes)
203 putWithAttributes(exec, Identifier::from(propertyName), value, attributes);
206 bool JSObject::hasProperty(ExecState *exec, const Identifier &propertyName) const
209 return const_cast<JSObject *>(this)->getPropertySlot(exec, propertyName, slot);
212 bool JSObject::hasProperty(ExecState *exec, unsigned propertyName) const
215 return const_cast<JSObject *>(this)->getPropertySlot(exec, propertyName, slot);
219 bool JSObject::deleteProperty(ExecState* exec, const Identifier &propertyName)
222 JSValue *v = _prop.get(propertyName, attributes);
224 if ((attributes & DontDelete))
226 _prop.remove(propertyName);
227 if (attributes & GetterSetter)
228 _prop.setHasGetterSetterProperties(_prop.containsGettersOrSetters());
232 // Look in the static hashtable of properties
233 const HashEntry* entry = findPropertyHashEntry(exec, propertyName);
234 if (entry && entry->attributes & DontDelete)
235 return false; // this builtin property can't be deleted
236 // FIXME: Should the code here actually do some deletion?
240 bool JSObject::hasOwnProperty(ExecState* exec, const Identifier& propertyName) const
243 return const_cast<JSObject*>(this)->getOwnPropertySlot(exec, propertyName, slot);
246 bool JSObject::deleteProperty(ExecState *exec, unsigned propertyName)
248 return deleteProperty(exec, Identifier::from(propertyName));
251 static ALWAYS_INLINE JSValue *tryGetAndCallProperty(ExecState *exec, const JSObject *object, const Identifier &propertyName) {
252 JSValue* v = object->get(exec, propertyName);
254 JSObject* o = static_cast<JSObject*>(v);
256 CallType callType = o->getCallData(data);
257 // spec says "not primitive type" but ...
258 if (callType != CallTypeNone) {
259 JSObject* thisObj = const_cast<JSObject*>(object);
260 JSValue* def = o->call(exec, thisObj->toThisObject(exec), exec->emptyList());
261 JSType defType = def->type();
262 ASSERT(defType != GetterSetterType);
263 if (defType != ObjectType)
270 bool JSObject::getPrimitiveNumber(ExecState* exec, double& number, JSValue*& result)
272 result = defaultValue(exec, NumberType);
273 number = result->toNumber(exec);
274 return !result->isString();
278 JSValue* JSObject::defaultValue(ExecState* exec, JSType hint) const
280 // We need this check to guard against the case where this object is rhs of
281 // a binary expression where lhs threw an exception in its conversion to
283 if (exec->hadException())
284 return exec->exception();
285 /* Prefer String for Date objects */
286 if ((hint == StringType) || (hint != NumberType && _proto == exec->lexicalGlobalObject()->datePrototype())) {
287 if (JSValue* v = tryGetAndCallProperty(exec, this, exec->propertyNames().toString))
289 if (JSValue* v = tryGetAndCallProperty(exec, this, exec->propertyNames().valueOf))
292 if (JSValue* v = tryGetAndCallProperty(exec, this, exec->propertyNames().valueOf))
294 if (JSValue* v = tryGetAndCallProperty(exec, this, exec->propertyNames().toString))
298 if (exec->hadException())
299 return exec->exception();
301 return throwError(exec, TypeError, "No default value");
304 const HashEntry* JSObject::findPropertyHashEntry(ExecState* exec, const Identifier& propertyName) const
306 for (const ClassInfo* info = classInfo(); info; info = info->parentClass) {
307 if (const HashTable* propHashTable = info->propHashTable(exec)) {
308 if (const HashEntry* e = propHashTable->entry(propertyName))
315 void JSObject::defineGetter(ExecState*, const Identifier& propertyName, JSObject* getterFunc)
317 JSValue *o = getDirect(propertyName);
320 if (o && o->type() == GetterSetterType) {
321 gs = static_cast<GetterSetterImp *>(o);
323 gs = new GetterSetterImp;
324 putDirect(propertyName, gs, GetterSetter);
327 _prop.setHasGetterSetterProperties(true);
328 gs->setGetter(getterFunc);
331 void JSObject::defineSetter(ExecState*, const Identifier& propertyName, JSObject* setterFunc)
333 JSValue *o = getDirect(propertyName);
336 if (o && o->type() == GetterSetterType) {
337 gs = static_cast<GetterSetterImp *>(o);
339 gs = new GetterSetterImp;
340 putDirect(propertyName, gs, GetterSetter);
343 _prop.setHasGetterSetterProperties(true);
344 gs->setSetter(setterFunc);
347 JSValue* JSObject::lookupGetter(ExecState*, const Identifier& propertyName)
349 JSObject* obj = this;
351 JSValue* v = obj->getDirect(propertyName);
353 if (v->type() != GetterSetterType)
354 return jsUndefined();
355 JSObject* funcObj = static_cast<GetterSetterImp*>(v)->getGetter();
357 return jsUndefined();
361 if (!obj->prototype() || !obj->prototype()->isObject())
362 return jsUndefined();
363 obj = static_cast<JSObject*>(obj->prototype());
367 JSValue* JSObject::lookupSetter(ExecState*, const Identifier& propertyName)
369 JSObject* obj = this;
371 JSValue* v = obj->getDirect(propertyName);
373 if (v->type() != GetterSetterType)
374 return jsUndefined();
375 JSObject* funcObj = static_cast<GetterSetterImp*>(v)->getSetter();
377 return jsUndefined();
381 if (!obj->prototype() || !obj->prototype()->isObject())
382 return jsUndefined();
383 obj = static_cast<JSObject*>(obj->prototype());
387 JSObject* JSObject::construct(ExecState*, const List& /*args*/)
393 JSObject* JSObject::construct(ExecState* exec, const List& args, const Identifier& /*functionName*/, const UString& /*sourceURL*/, int /*lineNumber*/)
395 return construct(exec, args);
398 bool JSObject::implementsCall()
401 return getCallData(callData) != CallTypeNone;
404 JSValue *JSObject::callAsFunction(ExecState* /*exec*/, JSObject* /*thisObj*/, const List &/*args*/)
410 bool JSObject::implementsHasInstance() const
415 bool JSObject::hasInstance(ExecState* exec, JSValue* value)
417 JSValue* proto = get(exec, exec->propertyNames().prototype);
418 if (!proto->isObject()) {
419 throwError(exec, TypeError, "intanceof called on an object with an invalid prototype property.");
423 if (!value->isObject())
426 JSObject* o = static_cast<JSObject*>(value);
427 while ((o = o->prototype()->getObject())) {
434 bool JSObject::propertyIsEnumerable(ExecState* exec, const Identifier& propertyName) const
438 if (!getPropertyAttributes(exec, propertyName, attributes))
441 return !(attributes & DontEnum);
444 bool JSObject::getPropertyAttributes(ExecState* exec, const Identifier& propertyName, unsigned& attributes) const
446 if (_prop.get(propertyName, attributes))
449 // Look in the static hashtable of properties
450 const HashEntry* e = findPropertyHashEntry(exec, propertyName);
452 attributes = e->attributes;
459 void JSObject::getPropertyNames(ExecState* exec, PropertyNameArray& propertyNames)
461 _prop.getEnumerablePropertyNames(propertyNames);
463 // Add properties from the static hashtables of properties
464 for (const ClassInfo* info = classInfo(); info; info = info->parentClass) {
465 const HashTable* table = info->propHashTable(exec);
469 table->createTable();
470 ASSERT(table->table);
471 int hashSizeMask = table->hashSizeMask;
472 const HashEntry* e = table->table;
473 for (int i = 0; i <= hashSizeMask; ++i, ++e) {
474 if (e->key && !(e->attributes & DontEnum))
475 propertyNames.add(e->key);
479 if (_proto->isObject())
480 static_cast<JSObject*>(_proto)->getPropertyNames(exec, propertyNames);
483 bool JSObject::toBoolean(ExecState*) const
488 double JSObject::toNumber(ExecState *exec) const
490 JSValue *prim = toPrimitive(exec,NumberType);
491 if (exec->hadException()) // should be picked up soon in nodes.cpp
493 return prim->toNumber(exec);
496 UString JSObject::toString(ExecState *exec) const
498 JSValue *prim = toPrimitive(exec,StringType);
499 if (exec->hadException()) // should be picked up soon in nodes.cpp
501 return prim->toString(exec);
504 JSObject *JSObject::toObject(ExecState*) const
506 return const_cast<JSObject*>(this);
509 JSObject* JSObject::toThisObject(ExecState*) const
511 return const_cast<JSObject*>(this);
514 JSGlobalObject* JSObject::toGlobalObject(ExecState*) const
519 void JSObject::removeDirect(const Identifier &propertyName)
521 _prop.remove(propertyName);
524 void JSObject::putDirectFunction(InternalFunctionImp* func, int attr)
526 putDirect(func->functionName(), func, attr);
529 void JSObject::fillGetterPropertySlot(PropertySlot& slot, JSValue **location)
531 GetterSetterImp *gs = static_cast<GetterSetterImp *>(*location);
532 JSObject *getterFunc = gs->getGetter();
534 slot.setGetterSlot(this->toThisObject(0), getterFunc);
536 slot.setUndefined(this);
539 // ------------------------------ Error ----------------------------------------
541 const char * const errorNamesArr[] = {
542 I18N_NOOP("Error"), // GeneralError
543 I18N_NOOP("Evaluation error"), // EvalError
544 I18N_NOOP("Range error"), // RangeError
545 I18N_NOOP("Reference error"), // ReferenceError
546 I18N_NOOP("Syntax error"), // SyntaxError
547 I18N_NOOP("Type error"), // TypeError
548 I18N_NOOP("URI error"), // URIError
551 const char * const * const Error::errorNames = errorNamesArr;
553 JSObject *Error::create(ExecState *exec, ErrorType errtype, const UString &message,
554 int lineno, int sourceId, const UString &sourceURL)
559 cons = exec->lexicalGlobalObject()->evalErrorConstructor();
562 cons = exec->lexicalGlobalObject()->rangeErrorConstructor();
565 cons = exec->lexicalGlobalObject()->referenceErrorConstructor();
568 cons = exec->lexicalGlobalObject()->syntaxErrorConstructor();
571 cons = exec->lexicalGlobalObject()->typeErrorConstructor();
574 cons = exec->lexicalGlobalObject()->URIErrorConstructor();
577 cons = exec->lexicalGlobalObject()->errorConstructor();
582 if (message.isEmpty())
583 args.append(jsString(errorNames[errtype]));
585 args.append(jsString(message));
586 JSObject *err = static_cast<JSObject *>(cons->construct(exec,args));
589 err->put(exec, "line", jsNumber(lineno));
591 err->put(exec, "sourceId", jsNumber(sourceId));
593 if(!sourceURL.isNull())
594 err->put(exec, "sourceURL", jsString(sourceURL));
599 JSObject *Error::create(ExecState *exec, ErrorType type, const char *message)
601 return create(exec, type, message, -1, -1, NULL);
604 JSObject *throwError(ExecState *exec, ErrorType type)
606 JSObject *error = Error::create(exec, type, UString(), -1, -1, NULL);
607 exec->setException(error);
611 JSObject *throwError(ExecState *exec, ErrorType type, const UString &message)
613 JSObject *error = Error::create(exec, type, message, -1, -1, NULL);
614 exec->setException(error);
618 JSObject *throwError(ExecState *exec, ErrorType type, const char *message)
620 JSObject *error = Error::create(exec, type, message, -1, -1, NULL);
621 exec->setException(error);
625 JSObject *throwError(ExecState *exec, ErrorType type, const UString &message, int line, int sourceId, const UString &sourceURL)
627 JSObject *error = Error::create(exec, type, message, line, sourceId, sourceURL);
628 exec->setException(error);