Attachment #617761: Patch to be reviewed (adding the AbstractProxyHandler class) for bug #703537

View | Details | Raw Unified | Return to bug 703537
Collapse All | Expand All

(-)a/js/src/jsproxy.cpp (+77 lines)
Line     Link Here 
 Lines 385-400   ProxyHandler::finalize(JSFreeOp *fop, JS Link Here 
385
{
385
{
386
}
386
}
387
387
388
void
388
void
389
ProxyHandler::trace(JSTracer *trc, JSObject *proxy)
389
ProxyHandler::trace(JSTracer *trc, JSObject *proxy)
390
{
390
{
391
}
391
}
392
392
393
AbstractProxyHandler::AbstractProxyHandler(void *family) : ProxyHandler(family)
394
{
395
}
396
397
bool
398
AbstractProxyHandler::getPropertyDescriptor(JSContext *cx, JSObject *proxy,
399
                                            jsid id, bool set,
400
                                            PropertyDescriptor *desc)
401
{
402
    return JS_GetPropertyDescriptorById(cx, GetProxyTargetObject(proxy), id,
403
                                        JSRESOLVE_QUALIFIED, desc);
404
}
405
406
static bool
407
GetOwnPropertyDescriptor(JSContext *cx, JSObject *obj, jsid id, unsigned flags,
408
                         JSPropertyDescriptor *desc)
409
{
410
    // If obj is a proxy, we can do better than just guessing. This is
411
    // important for certain types of wrappers that wrap other wrappers.
412
    if (obj->isProxy())
413
        return Proxy::getOwnPropertyDescriptor(cx, obj, id,
414
                                               flags & JSRESOLVE_ASSIGNING,
415
                                               desc);
416
417
    if (!JS_GetPropertyDescriptorById(cx, obj, id, flags, desc))
418
        return false;
419
    if (desc->obj != obj)
420
        desc->obj = NULL;
421
    return true;
422
}
423
424
bool
425
AbstractProxyHandler::getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy,
426
                                               jsid id, bool set,
427
                                               PropertyDescriptor *desc)
428
{
429
    return GetOwnPropertyDescriptor(cx, GetProxyTargetObject(proxy), id,
430
                                    JSRESOLVE_QUALIFIED, desc);
431
}
432
433
bool
434
AbstractProxyHandler::defineProperty(JSContext *cx, JSObject *proxy, jsid id,
435
                                     PropertyDescriptor *desc)
436
{
437
    return JS_DefinePropertyById(cx, GetProxyTargetObject(proxy), id,
438
                                 desc->value, desc->getter, desc->setter,
439
                                 desc->attrs);
440
}
441
442
bool
443
AbstractProxyHandler::getOwnPropertyNames(JSContext *cx, JSObject *proxy,
444
                                          AutoIdVector &props)
445
{
446
    return GetPropertyNames(cx, GetProxyTargetObject(proxy),
447
                            JSITER_OWNONLY | JSITER_HIDDEN, &props);
448
}
449
450
bool
451
AbstractProxyHandler::delete_(JSContext *cx, JSObject *proxy, jsid id, bool *bp)
452
{
453
    Value v;
454
    if (!JS_DeletePropertyById2(cx, GetProxyTargetObject(proxy), id, &v))
455
        return false;
456
    JSBool b;
457
    if (!JS_ValueToBoolean(cx, v, &b))
458
        return false;
459
    *bp = !!b;
460
    return true;
461
}
462
463
bool
464
AbstractProxyHandler::enumerate(JSContext *cx, JSObject *proxy,
465
                                AutoIdVector &props)
466
{
467
    return GetPropertyNames(cx, GetProxyTargetObject(proxy), 0, &props);
468
}
469
393
static bool
470
static bool
394
GetTrap(JSContext *cx, JSObject *handler, JSAtom *atom, Value *fvalp)
471
GetTrap(JSContext *cx, JSObject *handler, JSAtom *atom, Value *fvalp)
395
{
472
{
396
    JS_CHECK_RECURSION(cx, return false);
473
    JS_CHECK_RECURSION(cx, return false);
397
474
398
    return handler->getGeneric(cx, ATOM_TO_JSID(atom), fvalp);
475
    return handler->getGeneric(cx, ATOM_TO_JSID(atom), fvalp);
399
}
476
}
400
477
(-)a/js/src/jsproxy.h (+25 lines)
Line     Link Here 
 Lines 95-110   class JS_FRIEND_API(ProxyHandler) { Link Here 
95
        return false;
95
        return false;
96
    }
96
    }
97
97
98
    inline void *family() {
98
    inline void *family() {
99
        return mFamily;
99
        return mFamily;
100
    }
100
    }
101
};
101
};
102
102
103
class JS_PUBLIC_API(AbstractProxyHandler) : public ProxyHandler {
104
public:
105
    explicit AbstractProxyHandler(void *family);
106
107
    /* ES5 Harmony fundamental proxy traps. */
108
    virtual bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id,
109
                                       bool set, PropertyDescriptor *desc);
110
    virtual bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy,
111
                                          jsid id, bool set,
112
                                          PropertyDescriptor *desc);
113
    virtual bool defineProperty(JSContext *cx, JSObject *proxy, jsid id,
114
                                PropertyDescriptor *desc);
115
    virtual bool getOwnPropertyNames(JSContext *cx, JSObject *proxy,
116
                                     AutoIdVector &props);
117
    virtual bool delete_(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
118
    virtual bool enumerate(JSContext *cx, JSObject *proxy, AutoIdVector &props);
119
};
120
103
/* Dispatch point for handlers that executes the appropriate C++ or scripted traps. */
121
/* Dispatch point for handlers that executes the appropriate C++ or scripted traps. */
104
class Proxy {
122
class Proxy {
105
  public:
123
  public:
106
    /* ES5 Harmony fundamental proxy traps. */
124
    /* ES5 Harmony fundamental proxy traps. */
107
    static bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
125
    static bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
108
                                      PropertyDescriptor *desc);
126
                                      PropertyDescriptor *desc);
109
    static bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set, Value *vp);
127
    static bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set, Value *vp);
110
    static bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
128
    static bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
 Lines 185-200   GetProxyHandler(const JSObject *obj) Link Here 
185
203
186
inline const Value &
204
inline const Value &
187
GetProxyPrivate(const JSObject *obj)
205
GetProxyPrivate(const JSObject *obj)
188
{
206
{
189
    JS_ASSERT(IsProxy(obj));
207
    JS_ASSERT(IsProxy(obj));
190
    return GetReservedSlot(obj, JSSLOT_PROXY_PRIVATE);
208
    return GetReservedSlot(obj, JSSLOT_PROXY_PRIVATE);
191
}
209
}
192
210
211
inline JSObject *
212
GetProxyTargetObject(const JSObject *obj)
213
{
214
    JS_ASSERT(IsProxy(obj));
215
    return GetProxyPrivate(obj).toObjectOrNull();
216
}
217
193
inline const Value &
218
inline const Value &
194
GetProxyExtra(const JSObject *obj, size_t n)
219
GetProxyExtra(const JSObject *obj, size_t n)
195
{
220
{
196
    JS_ASSERT(IsProxy(obj));
221
    JS_ASSERT(IsProxy(obj));
197
    return GetReservedSlot(obj, JSSLOT_PROXY_EXTRA + n);
222
    return GetReservedSlot(obj, JSSLOT_PROXY_EXTRA + n);
198
}
223
}
199
224
200
inline void
225
inline void

Return to bug 703537