Changeset 29447 in webkit for trunk/JavaScriptCore


Ignore:
Timestamp:
Jan 13, 2008, 3:08:39 AM (17 years ago)
Author:
[email protected]
Message:

2008-01-13 Michael Goddard <[email protected]>

Reviewed by Anders Carlsson.

Add binding language type to Instance.
Allows runtime determination of the type of an
Instance, to allow safe casting. Doesn't actually
add any safe casting yet, though.

Add a helper function to get an Instance from a JSObject*.
Given an object and the expected binding language, see if
the JSObject actually wraps an Instance of the given type
and return it. Otherwise return 0.

Move RuntimeObjectImp creations into Instance.
Make the ctor protected, and Instance a friend class, so
that all creation of RuntimeObjectImps goes through
one place.

Remove copy ctor/assignment operator for QtInstance.
Instance itself is Noncopyable, so QtInstance doesn't
need to have these.

Add caching for QtInstance and associated RuntimeObjectImps.
Push any dealings with QtLanguage bindings into QtInstance,
and cache them there, rather than in the Instance layer. Add
a QtRuntimeObjectImp to help with caching.

  • JavaScriptCore.exp:
  • bindings/c/c_instance.h:
  • bindings/jni/jni_instance.h:
  • bindings/objc/objc_instance.h:
  • bindings/qt/qt_instance.cpp: (KJS::Bindings::QtRuntimeObjectImp::QtRuntimeObjectImp): (KJS::Bindings::QtRuntimeObjectImp::~QtRuntimeObjectImp): (KJS::Bindings::QtRuntimeObjectImp::invalidate): (KJS::Bindings::QtRuntimeObjectImp::removeFromCache): (KJS::Bindings::QtInstance::QtInstance): (KJS::Bindings::QtInstance::~QtInstance): (KJS::Bindings::QtInstance::getQtInstance): (KJS::Bindings::QtInstance::getRuntimeObject):
  • bindings/qt/qt_instance.h: (KJS::Bindings::QtInstance::getBindingLanguage):
  • bindings/runtime.cpp: (KJS::Bindings::Instance::createBindingForLanguageInstance): (KJS::Bindings::Instance::createRuntimeObject): (KJS::Bindings::Instance::getInstance):
  • bindings/runtime.h:
  • bindings/runtime_object.h: (KJS::RuntimeObjectImp::getInternalInstance):

2008-01-13 Michael Goddard <[email protected]>

Reviewed by Anders Carlsson.

Move RuntimeObjectImp creations into Instance.
Make the ctor protected, and Instance a friend class, so
that all creation of RuntimeObjectImps goes through
one place.

  • bindings/js/kjs_dom.cpp: (WebCore::getRuntimeObject):
Location:
trunk/JavaScriptCore
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r29445 r29447  
     12008-01-13  Michael Goddard  <[email protected]>
     2
     3        Reviewed by Anders Carlsson.
     4
     5        Add binding language type to Instance.
     6        Allows runtime determination of the type of an
     7        Instance, to allow safe casting.  Doesn't actually
     8        add any safe casting yet, though.
     9
     10        Add a helper function to get an Instance from a JSObject*.
     11        Given an object and the expected binding language, see if
     12        the JSObject actually wraps an Instance of the given type
     13        and return it.  Otherwise return 0.
     14
     15        Move RuntimeObjectImp creations into Instance.
     16        Make the ctor protected, and Instance a friend class, so
     17        that all creation of RuntimeObjectImps goes through
     18        one place.
     19
     20        Remove copy ctor/assignment operator for QtInstance.
     21        Instance itself is Noncopyable, so QtInstance doesn't
     22        need to have these.
     23
     24        Add caching for QtInstance and associated RuntimeObjectImps.
     25        Push any dealings with QtLanguage bindings into QtInstance,
     26        and cache them there, rather than in the Instance layer.  Add
     27        a QtRuntimeObjectImp to help with caching.
     28
     29        * JavaScriptCore.exp:
     30        * bindings/c/c_instance.h:
     31        * bindings/jni/jni_instance.h:
     32        * bindings/objc/objc_instance.h:
     33        * bindings/qt/qt_instance.cpp:
     34        (KJS::Bindings::QtRuntimeObjectImp::QtRuntimeObjectImp):
     35        (KJS::Bindings::QtRuntimeObjectImp::~QtRuntimeObjectImp):
     36        (KJS::Bindings::QtRuntimeObjectImp::invalidate):
     37        (KJS::Bindings::QtRuntimeObjectImp::removeFromCache):
     38        (KJS::Bindings::QtInstance::QtInstance):
     39        (KJS::Bindings::QtInstance::~QtInstance):
     40        (KJS::Bindings::QtInstance::getQtInstance):
     41        (KJS::Bindings::QtInstance::getRuntimeObject):
     42        * bindings/qt/qt_instance.h:
     43        (KJS::Bindings::QtInstance::getBindingLanguage):
     44        * bindings/runtime.cpp:
     45        (KJS::Bindings::Instance::createBindingForLanguageInstance):
     46        (KJS::Bindings::Instance::createRuntimeObject):
     47        (KJS::Bindings::Instance::getInstance):
     48        * bindings/runtime.h:
     49        * bindings/runtime_object.h:
     50        (KJS::RuntimeObjectImp::getInternalInstance):
     51
    1522008-01-12  Alp Toker  <[email protected]>
    253
  • trunk/JavaScriptCore/JavaScriptCore.exp

    r29396 r29447  
    159159__ZN3KJS16ParserRefCounted5derefEv
    160160__ZN3KJS16RuntimeObjectImp4infoE
    161 __ZN3KJS16RuntimeObjectImpC1EPNS_8Bindings8InstanceE
    162161__ZN3KJS17PropertyNameArray3addERKNS_10IdentifierE
    163162__ZN3KJS19InternalFunctionImp4infoE
     
    196195__ZN3KJS8Bindings24findProtectingRootObjectEPNS_8JSObjectE
    197196__ZN3KJS8Bindings8Instance18didExecuteFunctionEv
     197__ZN3KJS8Bindings8Instance19createRuntimeObjectEPS1_
    198198__ZN3KJS8Bindings8Instance21setDidExecuteFunctionEPFvPNS_9ExecStateEPNS_8JSObjectEE
    199199__ZN3KJS8Bindings8Instance32createBindingForLanguageInstanceENS1_15BindingLanguageEPvN3WTF10PassRefPtrINS0_10RootObjectEEE
  • trunk/JavaScriptCore/bindings/c/c_instance.h

    r25000 r29447  
    6565    NPObject *getObject() const { return _object; }
    6666
     67    virtual BindingLanguage getBindingLanguage() const { return CLanguage; }
     68
    6769private:
    6870    mutable CClass *_class;
  • trunk/JavaScriptCore/bindings/jni/jni_instance.h

    r27186 r29447  
    8484    JSValue *numberValue() const;
    8585    JSValue *booleanValue() const;
    86        
     86
     87    virtual BindingLanguage getBindingLanguage() const { return JavaLanguage; }
     88
    8789private:
    8890    RefPtr<JObjectWrapper> _instance;
  • trunk/JavaScriptCore/bindings/jni/jni_jsobject.cpp

    r28468 r29447  
    470470    JSLock lock;
    471471    JavaInstance* javaInstance = new JavaInstance(theObject, _rootObject);
    472     RuntimeObjectImp* newImp = new RuntimeObjectImp(javaInstance);
    473 
    474     return newImp;
     472    return KJS::Bindings::Instance::createRuntimeObject(javaInstance);
    475473}
    476474
  • trunk/JavaScriptCore/bindings/objc/objc_instance.h

    r23472 r29447  
    6565    JSValue *numberValue() const;
    6666    JSValue *booleanValue() const;
    67    
     67
     68    virtual BindingLanguage getBindingLanguage() const { return ObjectiveCLanguage; }
     69
    6870private:
    6971    RetainPtr<ObjectStructPtr> _instance;
  • trunk/JavaScriptCore/bindings/qt/qt_instance.cpp

    r26972 r29447  
    2121#include "qt_instance.h"
    2222
     23#include "list.h"
    2324#include "qt_class.h"
    2425#include "qt_runtime.h"
    25 #include "list.h"
     26#include "runtime_object.h"
    2627
    2728#include <qmetaobject.h>
    2829#include <qdebug.h>
     30#include <qhash.h>
    2931
    3032namespace KJS {
    3133namespace Bindings {
    3234
     35// Cache QtInstances
     36typedef QMultiHash<void*, QtInstance*> QObjectInstanceMap;
     37static QObjectInstanceMap cachedInstances;
     38
     39// Cache JSObjects
     40typedef QHash<QtInstance*, JSObject*> InstanceJSObjectMap;
     41static InstanceJSObjectMap cachedObjects;
     42
     43// Derived RuntimeObject
     44class QtRuntimeObjectImp : public RuntimeObjectImp {
     45    public:
     46        QtRuntimeObjectImp(Instance*);
     47        ~QtRuntimeObjectImp();
     48        virtual void invalidate();
     49    protected:
     50        void removeFromCache();
     51};
     52
     53QtRuntimeObjectImp::QtRuntimeObjectImp(Instance* instance)
     54    : RuntimeObjectImp(instance)
     55{
     56}
     57
     58QtRuntimeObjectImp::~QtRuntimeObjectImp()
     59{
     60    removeFromCache();
     61}
     62
     63void QtRuntimeObjectImp::invalidate()
     64{
     65    removeFromCache();
     66    RuntimeObjectImp::invalidate();
     67}
     68
     69void QtRuntimeObjectImp::removeFromCache()
     70{
     71    JSLock lock;
     72    QtInstance* key = cachedObjects.key(this);
     73    if (key)
     74        cachedObjects.remove(key);
     75}
     76
     77// QtInstance
    3378QtInstance::QtInstance(QObject* o, PassRefPtr<RootObject> rootObject)
    34     : Instance(rootObject),
    35       _class(0),
    36       _object(o)
    37 {
    38 }
    39 
    40 QtInstance::~QtInstance()
    41 {
    42 }
    43 
    44 QtInstance::QtInstance(const QtInstance& other)
    45     : Instance(other.rootObject()), _class(0), _object(other._object)
    46 {
    47 }
    48 
    49 QtInstance& QtInstance::operator=(const QtInstance& other)
    50 {
    51     if (this == &other)
    52         return *this;
    53 
    54     _object = other._object;
    55     _class = 0;
    56 
    57     return *this;
     79    : Instance(rootObject)
     80    , _class(0)
     81    , _object(o)
     82    , _hashkey(o)
     83{
     84}
     85
     86QtInstance::~QtInstance()
     87{
     88    JSLock lock;
     89    cachedObjects.remove(this);
     90    cachedInstances.remove(_hashkey);
     91}
     92
     93QtInstance* QtInstance::getQtInstance(QObject* o, PassRefPtr<RootObject> rootObject)
     94{
     95    JSLock lock;
     96
     97    foreach(QtInstance* instance, cachedInstances.values(o)) {
     98        if (instance->rootObject() == rootObject)
     99            return instance;
     100    }
     101
     102    QtInstance* ret = new QtInstance(o, rootObject);
     103    cachedInstances.insert(o, ret);
     104
     105    return ret;
     106}
     107
     108JSObject* QtInstance::getRuntimeObject(QtInstance* instance)
     109{
     110    JSLock lock;
     111    JSObject* ret = cachedObjects.value(instance);
     112    if (!ret) {
     113        ret = new QtRuntimeObjectImp(instance);
     114        cachedObjects.insert(instance, ret);
     115    }
     116    return ret;
    58117}
    59118
  • trunk/JavaScriptCore/bindings/qt/qt_instance.h

    r22067 r29447  
    3636{
    3737public:
    38     QtInstance(QObject* instance, PassRefPtr<RootObject>);
    39        
    4038    ~QtInstance ();
    4139   
    4240    virtual Class* getClass() const;
    43    
    44     QtInstance (const QtInstance &other);
    45 
    46     QtInstance &operator=(const QtInstance &other);
    4741   
    4842    virtual void begin();
     
    6357    QObject* getObject() const { return _object; }
    6458
     59    virtual BindingLanguage getBindingLanguage() const { return QtLanguage; }
     60
     61    static QtInstance* getQtInstance(QObject*, PassRefPtr<RootObject>);
     62    static JSObject* getRuntimeObject(QtInstance*);
     63
    6564private:
     65    QtInstance(QObject*, PassRefPtr<RootObject>); // Factory produced only..
    6666    mutable QtClass* _class;
    6767    QPointer<QObject> _object;
     68    QObject* _hashkey;
    6869};
    6970
  • trunk/JavaScriptCore/bindings/runtime.cpp

    r25000 r29447  
    106106#if PLATFORM(QT)
    107107        case Instance::QtLanguage: {
    108             newInstance = new Bindings::QtInstance((QObject *)nativeInstance, rootObject);
     108            newInstance = Bindings::QtInstance::getQtInstance((QObject *)nativeInstance, rootObject);
    109109            break;
    110110        }
     
    120120{
    121121    Instance* instance = Instance::createBindingForLanguageInstance(language, nativeInstance, rootObject);
    122    
     122
     123    return createRuntimeObject(instance);
     124}
     125
     126JSObject* Instance::createRuntimeObject(Instance* instance)
     127{
     128#if PLATFORM(QT)
     129    if (instance->getBindingLanguage() == QtLanguage)
     130        return QtInstance::getRuntimeObject(static_cast<QtInstance*>(instance));
     131#endif
    123132    JSLock lock;
    124133    return new RuntimeObjectImp(instance);
    125134}
    126135
    127 RootObject* Instance::rootObject() const
    128 {
     136Instance* Instance::getInstance(JSObject* object, BindingLanguage language)
     137{
     138    if (!object)
     139        return 0;
     140    if (!object->inherits(&RuntimeObjectImp::info))
     141        return 0;
     142    Instance* instance = (static_cast<RuntimeObjectImp*>(object))->getInternalInstance();
     143    if (!instance)
     144        return 0;
     145    if (instance->getBindingLanguage() != language)
     146        return 0;
     147    return instance;
     148}
     149
     150RootObject* Instance::rootObject() const
     151{
    129152    return _rootObject && _rootObject->isValid() ? _rootObject.get() : 0;
    130153}
  • trunk/JavaScriptCore/bindings/runtime.h

    r23512 r29447  
    102102    static Instance* createBindingForLanguageInstance(BindingLanguage, void* nativeInstance, PassRefPtr<RootObject>);
    103103    static JSObject* createRuntimeObject(BindingLanguage, void* nativeInstance, PassRefPtr<RootObject>);
     104    static JSObject* createRuntimeObject(Instance*);
     105
     106    static Instance* getInstance(JSObject*, BindingLanguage);
    104107
    105108    void ref() { _refCount++; }
     
    139142    virtual ~Instance();
    140143
     144    virtual BindingLanguage getBindingLanguage() const = 0;
     145
    141146protected:
    142147    RefPtr<RootObject> _rootObject;
  • trunk/JavaScriptCore/bindings/runtime_object.h

    r23538 r29447  
    3636class RuntimeObjectImp : public JSObject {
    3737public:
    38     RuntimeObjectImp(Bindings::Instance *i);
    3938    virtual ~RuntimeObjectImp();
    4039   
     
    5049    virtual void getPropertyNames(ExecState*, PropertyNameArray&);
    5150
    52     void invalidate();
     51    virtual void invalidate();
    5352    Bindings::Instance *getInternalInstance() const { return instance.get(); }
    5453   
     
    5655   
    5756    static const ClassInfo info;
     57
     58protected:
     59    friend class Bindings::Instance;
     60    RuntimeObjectImp(Bindings::Instance*); // Only allow Instances and derived classes to create us
    5861
    5962private:
Note: See TracChangeset for help on using the changeset viewer.