1/*
2 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
3 *
4 *  This library is free software; you can redistribute it and/or
5 *  modify it under the terms of the GNU Lesser General Public
6 *  License as published by the Free Software Foundation; either
7 *  version 2 of the License, or (at your option) any later version.
8 *
9 *  This library is distributed in the hope that it will be useful,
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 *  Lesser General Public License for more details.
13 *
14 *  You should have received a copy of the GNU Lesser General Public
15 *  License along with this library; if not, write to the Free Software
16 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 *
18 */
19
20#ifndef qt_instance_h
21#define qt_instance_h
22
23#include "BridgeJSC.h"
24#include "JSWeakObjectMapRefPrivate.h"
25#include "Weak.h"
26#include "WeakInlines.h"
27#include "runtime_root.h"
28#include <QPointer>
29#include <qhash.h>
30#include <qset.h>
31
32namespace JSC {
33
34namespace Bindings {
35
36class QtClass;
37class QtField;
38class QtRuntimeMethod;
39
40class WeakMapImpl : public RefCounted<WeakMapImpl> {
41public:
42    WeakMapImpl(JSContextGroupRef);
43    ~WeakMapImpl();
44
45    JSGlobalContextRef m_context;
46    JSWeakObjectMapRef m_map;
47};
48
49class WeakMap {
50public:
51    ~WeakMap();
52
53    void set(JSContextRef, void* key, JSObjectRef);
54    JSObjectRef get(void* key);
55    void remove(void* key);
56
57private:
58    RefPtr<WeakMapImpl> m_impl;
59};
60
61class QtInstance : public Instance {
62public:
63    enum ValueOwnership {
64        QtOwnership,
65        ScriptOwnership,
66        AutoOwnership
67    };
68
69    ~QtInstance();
70
71    virtual Class* getClass() const;
72    virtual RuntimeObject* newRuntimeObject(ExecState*);
73
74    virtual void begin();
75    virtual void end();
76
77    virtual JSValue valueOf(ExecState*) const;
78    virtual JSValue defaultValue(ExecState*, PreferredPrimitiveType) const;
79
80    virtual JSValue getMethod(ExecState*, PropertyName);
81    virtual JSValue invokeMethod(ExecState*, RuntimeMethod*);
82
83    virtual void getPropertyNames(ExecState*, PropertyNameArray&);
84
85    JSValue stringValue(ExecState* exec) const;
86    JSValue numberValue(ExecState* exec) const;
87    JSValue booleanValue() const;
88
89    QObject* getObject() const { return m_object.data(); }
90    QObject* hashKey() const { return m_hashkey; }
91
92    static PassRefPtr<QtInstance> getQtInstance(QObject*, PassRefPtr<RootObject>, ValueOwnership);
93
94    virtual bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
95    virtual void put(JSObject*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
96
97    static QtInstance* getInstance(JSObject*);
98
99private:
100    static PassRefPtr<QtInstance> create(QObject *instance, PassRefPtr<RootObject> rootObject, ValueOwnership ownership)
101    {
102        return adoptRef(new QtInstance(instance, rootObject, ownership));
103    }
104
105    friend class QtClass;
106    friend class QtField;
107    friend class QtRuntimeMethod;
108    QtInstance(QObject*, PassRefPtr<RootObject>, ValueOwnership); // Factory produced only..
109    mutable QtClass* m_class;
110    QPointer<QObject> m_object;
111    QObject* m_hashkey;
112    mutable QHash<QByteArray, QtRuntimeMethod*> m_methods;
113    mutable QHash<QString, QtField*> m_fields;
114    ValueOwnership m_ownership;
115};
116
117} // namespace Bindings
118
119} // namespace JSC
120
121#endif
122