File: kjsprototype.cpp

package info (click to toggle)
kde4libs 4%3A4.4.5-2%2Bsqueeze3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 88,360 kB
  • ctags: 94,980
  • sloc: cpp: 721,043; xml: 146,205; ansic: 8,395; java: 4,060; perl: 3,118; yacc: 2,058; sh: 1,458; python: 1,050; ruby: 715; lex: 248; asm: 166; jsp: 128; haskell: 116; f90: 99; ml: 75; erlang: 54; awk: 38; tcl: 29; lisp: 24; makefile: 23; php: 9
file content (308 lines) | stat: -rw-r--r-- 8,690 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 2008 Harri Porten (porten@kde.org)
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#include "kjsprototype.h"
#include "kjsinterpreter.h"
#include "kjsarguments.h"
#include "kjsprivate.h"

#include "kjs/object.h"
#include "kjs/JSVariableObject.h"
#include "kjs/context.h"
#include "kjs/interpreter.h"

#include <QMap>

using namespace KJS;

class KJSCustomProperty
{
public:
    KJSCustomProperty(KJSPrototype::PropertyGetter g,
                      KJSPrototype::PropertySetter s)
        : getter(g), setter(s)
    {
    }

    JSValue* read(ExecState* exec, void* object);
    void write(ExecState* exec, void* object, JSValue* value);

private:
    KJSPrototype::PropertyGetter getter;
    KJSPrototype::PropertySetter setter;
};

class CustomObjectInfo {
public:
    CustomObjectInfo(void* v): iv(v) {}
    virtual ~CustomObjectInfo() {} 
    void* internalValue() { return iv; }
protected:
    void* iv;
};

template<class Base>
class CustomObject : public Base, public CustomObjectInfo {
public:
    CustomObject(JSValue* proto, void* v)
        : Base(proto),
          CustomObjectInfo(v)
    {}

    void put(ExecState* exec, const Identifier& id,
             JSValue *value, int attr = None);


    // rtti
    static const ClassInfo info;
    const ClassInfo* classInfo() const { return &info; }
};

template<>
const ClassInfo CustomObject<JSObject>::info = { "CustomObject", 0, 0, 0 };

template<>
const ClassInfo CustomObject<JSGlobalObject>::info = { "CustomGlobalObject", 0, 0, 0 };

class KJSCustomFunction : public JSObject {
public:
    KJSCustomFunction(ExecState* exec, KJSPrototype::FunctionCall f)
        : callback(f)
    {
        setPrototype(exec->lexicalInterpreter()->builtinObjectPrototype());
    }

    JSValue* callAsFunction(ExecState* exec, JSObject* thisObj,
                            const List &args);
    bool implementsCall() const { return true; }

private:
    KJSPrototype::FunctionCall callback;
};

JSValue* KJSCustomFunction::callAsFunction(ExecState* exec, JSObject* thisObj,
                                           const List &args)
{
    // FIXME: does not protect against mixing custom objects
    CustomObjectInfo* inf = dynamic_cast<CustomObjectInfo*>(thisObj);

    if (!inf) {
        const char* errMsg = "Attempt at calling a function with an invalid receiver";
        KJS::JSObject *err = KJS::Error::create(exec, KJS::TypeError, errMsg);
        exec->setException(err);
        return err;
    }

    void* thisValue = inf->internalValue();

    KJSContext ctx(EXECSTATE_HANDLE(exec));
    KJSArguments a(LIST_HANDLE(&args));
    KJSObject res = (*callback)(&ctx, thisValue, a);
    return JSVALUE(&res);
}

JSValue* KJSCustomProperty::read(ExecState* exec, void* object)
{
    assert(getter);
    
    KJSContext ctx(EXECSTATE_HANDLE(exec));
    KJSObject res = (*getter)(&ctx, object);
    return JSVALUE(&res);
}

void KJSCustomProperty::write(ExecState* exec, void* object, JSValue* value)
{
    KJSContext ctx(EXECSTATE_HANDLE(exec));

    if (setter) {
        KJSObject vo(JSVALUE_HANDLE(value));
        (*setter)(&ctx, object, vo);
    } else {
        JSObject *e = Error::create(exec, GeneralError,
                                    "Property is read-only");
        exec->setException(e);
    }
}

static JSValue* getPropertyValue(ExecState* exec, JSObject *originalObject,
                                 const Identifier&, const PropertySlot& sl)
{
    CustomObjectInfo* inf = dynamic_cast<CustomObjectInfo*>(originalObject);
    if (!inf)
        return jsUndefined();

    KJSCustomProperty* p =
        reinterpret_cast<KJSCustomProperty*>(sl.customValue());

    return p->read(exec, inf->internalValue());
}

// FIXME: or use Identifier?
// FIXME: use values
typedef QMap<UString, KJSCustomProperty*> CustomPropertyMap;

class CustomPrototype : public JSObject {
public:
    CustomPrototype()
    {
    }
    ~CustomPrototype()
    {
        qDeleteAll(properties);
    }

    bool getOwnPropertySlot(ExecState *exec, const Identifier& id,
                            PropertySlot& sl)
    {
        CustomPropertyMap::iterator it = properties.find(id.ustring());
        if (it == properties.end())
            return JSObject::getOwnPropertySlot(exec, id, sl);
        
        sl.setCustomValue(0, *it, getPropertyValue);

        return true;
    }

    void registerProperty(const QString& name,
                          KJSPrototype::PropertyGetter g,
                          KJSPrototype::PropertySetter s)
    {
        properties.insert(toUString(name), new KJSCustomProperty(g, s));
    }

    void registerFunction(ExecState* exec,
                          const QString& name, KJSPrototype::FunctionCall f)
    {
        putDirect(toIdentifier(name), new KJSCustomFunction(exec, f));
    }

    template<typename Base>
    bool setProperty(ExecState* exec, CustomObject<Base>* obj,
                     const Identifier& id, JSValue* value)
    {
        CustomPropertyMap::iterator it = properties.find(id.ustring());
        if (it == properties.end())
            return false;

        (*it)->write(exec, obj->internalValue(), value);
        
        return true;
    }

private:
    CustomPropertyMap properties;
};

template<class Base>
void CustomObject<Base>::put(ExecState* exec, const Identifier& id,
                       JSValue* value, int attr)
{
    CustomPrototype* p = static_cast<CustomPrototype*>(this->prototype());

    if (!p->setProperty(exec, this, id, value))
        Base::put(exec, id, value, attr);
}

KJSPrototype::KJSPrototype()
{
    CustomPrototype* p = new CustomPrototype;
    gcProtect(p);

    hnd = PROTOTYPE_HANDLE(p);
}

KJSPrototype::~KJSPrototype()
{
    gcUnprotect(PROTOTYPE(this));
}

void KJSPrototype::defineConstant(const QString& name, double value)
{
    CustomPrototype* p = PROTOTYPE(this);

    p->putDirect(toIdentifier(name), jsNumber(value),
                 DontEnum|DontDelete|ReadOnly);
}

void KJSPrototype::defineConstant(const QString& name, const QString& value)
{
    CustomPrototype* p = PROTOTYPE(this);

    p->putDirect(toIdentifier(name), jsString(toUString(value)),
                 DontEnum|DontDelete|ReadOnly);
}

void KJSPrototype::defineConstant(const QString& name, const KJSObject& value)
{
    CustomPrototype* p = PROTOTYPE(this);

    p->putDirect(toIdentifier(name), JSVALUE(&value),
                 DontEnum|DontDelete|ReadOnly);
}

KJSObject KJSPrototype::constructObject(KJSContext* ctx, void *internalValue)
{
    CustomPrototype* p = PROTOTYPE(this);

    if (ctx && !p->prototype()) {
        ExecState* exec = EXECSTATE(ctx);
        KJS::Interpreter* i = exec->lexicalInterpreter();
        JSObject* objectProto = i->builtinObjectPrototype();
        p->setPrototype(objectProto);
    }

    CustomObject<JSObject>* newObj = new CustomObject<JSObject>(p, internalValue);
    return KJSObject(JSVALUE_HANDLE(newObj));
}

KJSGlobalObject KJSPrototype::constructGlobalObject(void *internalValue)
{
    CustomPrototype* p = PROTOTYPE(this);

    CustomObject<JSGlobalObject>* newObj = new CustomObject<JSGlobalObject>(p, internalValue);
    return KJSGlobalObject(JSVALUE_HANDLE(newObj));
}

void KJSPrototype::defineProperty(KJSContext* ctx,
                                  const QString& name,
                                  PropertyGetter getter,
                                  PropertySetter setter)
{
    assert(getter);

    CustomPrototype* p = PROTOTYPE(this);

    p->registerProperty(name, getter, setter);
}

void KJSPrototype::defineFunction(KJSContext* ctx,
                                  const QString& name, FunctionCall callback)
{
    assert(callback);

    CustomPrototype* p = PROTOTYPE(this);
    ExecState* exec = EXECSTATE(ctx);

    p->registerFunction(exec, name, callback);
}


// kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on;