object.h
00001 // -*- c-basic-offset: 2 -*- 00002 /* 00003 * This file is part of the KDE libraries 00004 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org) 00005 * Copyright (C) 2001 Peter Kelly (pmk@post.com) 00006 * Copyright (C) 2003 Apple Computer, Inc. 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Library General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Library General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Library General Public License 00019 * along with this library; see the file COPYING.LIB. If not, write to 00020 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00021 * Boston, MA 02110-1301, USA. 00022 * 00023 */ 00024 00025 00026 #ifndef _KJS_OBJECT_H_ 00027 #define _KJS_OBJECT_H_ 00028 00029 // Objects 00030 00031 #include "value.h" 00032 #include "types.h" 00033 #include "reference_list.h" 00034 #include "identifier.h" 00035 #include "property_map.h" 00036 #include "scope_chain.h" 00037 00038 namespace KJS { 00039 00040 class ObjectImpPrivate; 00041 class PropertyMap; 00042 class HashTable; 00043 struct HashEntry; 00044 class ListImp; 00045 00049 enum Attribute { None = 0, 00050 ReadOnly = 1 << 1, 00051 DontEnum = 1 << 2, 00052 DontDelete = 1 << 3, 00053 Internal = 1 << 4, 00054 Function = 1 << 5 }; 00055 00059 struct ClassInfo { 00063 const char* className; 00068 const ClassInfo *parentClass; 00072 const HashTable *propHashTable; 00076 void *dummy; 00077 }; 00078 00082 class KJS_EXPORT Object : public Value { 00083 public: 00084 Object() { } 00085 explicit Object(ObjectImp *v); 00086 00087 ObjectImp *imp() const; 00088 00089 const ClassInfo *classInfo() const; 00090 bool inherits(const ClassInfo *cinfo) const; 00091 00101 static Object dynamicCast(const Value &v); 00102 00111 Value prototype() const; 00112 00120 UString className() const; 00121 00134 Value get(ExecState *exec, const Identifier &propertyName) const; 00135 Value get(ExecState *exec, unsigned propertyName) const; 00136 00147 void put(ExecState *exec, const Identifier &propertyName, 00148 const Value &value, int attr = None); 00149 void put(ExecState *exec, unsigned propertyName, 00150 const Value &value, int attr = None); 00151 00162 bool canPut(ExecState *exec, const Identifier &propertyName) const; 00163 00174 bool hasProperty(ExecState *exec, const Identifier &propertyName) const; 00175 bool hasProperty(ExecState *exec, unsigned propertyName) const; 00176 00188 bool deleteProperty(ExecState *exec, const Identifier &propertyName); 00189 bool deleteProperty(ExecState *exec, unsigned propertyName); 00190 00203 Value defaultValue(ExecState *exec, Type hint) const; 00204 00213 bool implementsConstruct() const; 00214 00240 Object construct(ExecState *exec, const List &args); 00241 00250 bool implementsCall() const; 00251 00252 00270 Value call(ExecState *exec, Object &thisObj, const List &args); 00271 00280 bool implementsHasInstance() const; 00281 00291 Boolean hasInstance(ExecState *exec, const Value &value); 00292 00318 const ScopeChain &scope() const; 00319 void setScope(const ScopeChain &s); 00320 00337 ReferenceList propList(ExecState *exec, bool recursive = true); 00338 00347 Value internalValue() const; 00348 00356 void setInternalValue(const Value &v); 00357 }; 00358 00359 inline Object Value::toObject(ExecState *exec) const { return rep->dispatchToObject(exec); } 00360 00361 class KJS_EXPORT ObjectImp : public ValueImp { 00362 friend class ObjectProtoFuncImp; 00363 public: 00369 ObjectImp(const Object &proto); 00370 ObjectImp(ObjectImp *proto); 00371 00377 ObjectImp(); 00378 00379 virtual ~ObjectImp(); 00380 00381 virtual void mark(); 00382 00383 Type type() const; 00384 00422 virtual const ClassInfo *classInfo() const; 00423 00450 bool inherits(const ClassInfo *cinfo) const; 00451 00452 // internal properties (ECMA 262-3 8.6.2) 00453 00460 Value prototype() const; 00461 void setPrototype(const Value &proto); 00462 00474 virtual UString className() const; 00475 00482 // [[Get]] - must be implemented by all Objects 00483 virtual Value get(ExecState *exec, const Identifier &propertyName) const; 00484 virtual Value getPropertyByIndex(ExecState *exec, 00485 unsigned propertyName) const; 00486 00493 virtual void put(ExecState *exec, const Identifier &propertyName, 00494 const Value &value, int attr = None); 00495 virtual void putPropertyByIndex(ExecState *exec, unsigned propertyName, 00496 const Value &value, int attr = None); 00497 00504 virtual bool canPut(ExecState *exec, const Identifier &propertyName) const; 00505 00512 virtual bool hasProperty(ExecState *exec, 00513 const Identifier &propertyName) const; 00514 virtual bool hasPropertyByIndex(ExecState *exec, unsigned propertyName) const; 00515 00522 virtual bool deleteProperty(ExecState *exec, 00523 const Identifier &propertyName); 00524 virtual bool deletePropertyByIndex(ExecState *exec, unsigned propertyName); 00525 00531 void deleteAllProperties(ExecState *); 00532 00539 virtual Value defaultValue(ExecState *exec, Type hint) const; 00540 00541 virtual bool implementsConstruct() const; 00547 virtual Object construct(ExecState *exec, const List &args); 00548 00549 virtual bool implementsCall() const; 00555 virtual Value call(ExecState *exec, Object &thisObj, 00556 const List &args); 00557 00558 virtual bool implementsHasInstance() const; 00564 virtual Boolean hasInstance(ExecState *exec, const Value &value); 00565 00571 const ScopeChain &scope() const { return _scope; } 00572 void setScope(const ScopeChain &s) { _scope = s; } 00573 00574 virtual ReferenceList propList(ExecState *exec, bool recursive = true); 00575 00576 Value internalValue() const; 00577 void setInternalValue(const Value &v); 00578 void setInternalValue(ValueImp *v); 00579 00580 Value toPrimitive(ExecState *exec, 00581 Type preferredType = UnspecifiedType) const; 00582 bool toBoolean(ExecState *exec) const; 00583 double toNumber(ExecState *exec) const; 00584 UString toString(ExecState *exec) const; 00585 Object toObject(ExecState *exec) const; 00586 00587 // This get method only looks at the property map. 00588 // A bit like hasProperty(recursive=false), this doesn't go to the prototype. 00589 // This is used e.g. by lookupOrCreateFunction (to cache a function, we don't want 00590 // to look up in the prototype, it might already exist there) 00591 ValueImp *getDirect(const Identifier& propertyName) const 00592 { return _prop.get(propertyName); } 00593 void putDirect(const Identifier &propertyName, ValueImp *value, int attr = 0); 00594 void putDirect(const Identifier &propertyName, int value, int attr = 0); 00595 00600 void setFunctionName(const Identifier &propertyName); 00601 00602 protected: 00603 PropertyMap _prop; 00604 private: 00605 const HashEntry* findPropertyHashEntry( const Identifier& propertyName ) const; 00606 ObjectImpPrivate *_od; 00607 ValueImp *_proto; 00608 ValueImp *_internalValue; 00609 ScopeChain _scope; 00610 }; 00611 00616 enum ErrorType { GeneralError = 0, 00617 EvalError = 1, 00618 RangeError = 2, 00619 ReferenceError = 3, 00620 SyntaxError = 4, 00621 TypeError = 5, 00622 URIError = 6}; 00623 00627 class KJS_EXPORT Error { 00628 public: 00638 static Object create(ExecState *exec, ErrorType errtype = GeneralError, 00639 const char *message = 0, int lineno = -1, 00640 int sourceId = -1); 00641 00645 static const char * const * const errorNames; 00646 }; 00647 00648 inline Object::Object(ObjectImp *v) : Value(v) { } 00649 00650 inline ObjectImp *Object::imp() const { return static_cast<ObjectImp*>(rep); } 00651 00652 inline const ClassInfo *Object::classInfo() const 00653 { return imp()->classInfo(); } 00654 00655 inline bool Object::inherits(const ClassInfo *cinfo) const 00656 { return imp()->inherits(cinfo); } 00657 00658 inline Value Object::prototype() const 00659 { return Value(imp()->prototype()); } 00660 00661 inline UString Object::className() const 00662 { return imp()->className(); } 00663 00664 inline Value Object::get(ExecState *exec, const Identifier &propertyName) const 00665 { return imp()->get(exec,propertyName); } 00666 00667 inline Value Object::get(ExecState *exec, unsigned propertyName) const 00668 { return imp()->getPropertyByIndex(exec, propertyName); } 00669 00670 inline void Object::put(ExecState *exec, const Identifier &propertyName, const Value &value, int attr) 00671 { imp()->put(exec,propertyName,value,attr); } 00672 00673 inline void Object::put(ExecState *exec, unsigned propertyName, const Value &value, int attr) 00674 { imp()->putPropertyByIndex(exec, propertyName, value, attr); } 00675 00676 inline bool Object::canPut(ExecState *exec, const Identifier &propertyName) const 00677 { return imp()->canPut(exec,propertyName); } 00678 00679 inline bool Object::hasProperty(ExecState *exec, const Identifier &propertyName) const 00680 { return imp()->hasProperty(exec, propertyName); } 00681 00682 inline bool Object::hasProperty(ExecState *exec, unsigned propertyName) const 00683 { return imp()->hasPropertyByIndex(exec, propertyName); } 00684 00685 inline bool Object::deleteProperty(ExecState *exec, const Identifier &propertyName) 00686 { return imp()->deleteProperty(exec,propertyName); } 00687 00688 inline bool Object::deleteProperty(ExecState *exec, unsigned propertyName) 00689 { return imp()->deletePropertyByIndex(exec, propertyName); } 00690 00691 inline Value Object::defaultValue(ExecState *exec, Type hint) const 00692 { return imp()->defaultValue(exec,hint); } 00693 00694 inline bool Object::implementsConstruct() const 00695 { return imp()->implementsConstruct(); } 00696 00697 inline Object Object::construct(ExecState *exec, const List &args) 00698 { return imp()->construct(exec,args); } 00699 00700 inline bool Object::implementsCall() const 00701 { return imp()->implementsCall(); } 00702 00703 inline bool Object::implementsHasInstance() const 00704 { return imp()->implementsHasInstance(); } 00705 00706 inline Boolean Object::hasInstance(ExecState *exec, const Value &value) 00707 { return imp()->hasInstance(exec,value); } 00708 00709 inline const ScopeChain &Object::scope() const 00710 { return imp()->scope(); } 00711 00712 inline void Object::setScope(const ScopeChain &s) 00713 { imp()->setScope(s); } 00714 00715 inline ReferenceList Object::propList(ExecState *exec, bool recursive) 00716 { return imp()->propList(exec,recursive); } 00717 00718 inline Value Object::internalValue() const 00719 { return imp()->internalValue(); } 00720 00721 inline void Object::setInternalValue(const Value &v) 00722 { imp()->setInternalValue(v); } 00723 00724 } // namespace 00725 00726 #endif // _KJS_OBJECT_H_