Bug 1159469 - Add public jsapi ES6 Set convenience functions; r=jorendorff

This commit is contained in:
Kyle Machulis 2015-03-09 17:38:44 -07:00
parent 19577e721f
commit ab6d4b04ea
3 changed files with 193 additions and 17 deletions

View File

@ -1354,7 +1354,6 @@ MapObject::extract(CallReceiver call)
uint32_t
MapObject::size(JSContext* cx, HandleObject obj)
{
MOZ_ASSERT(MapObject::is(obj));
ValueMap& map = extract(obj);
static_assert(sizeof(map.count()) <= sizeof(uint32_t),
"map count must be precisely representable as a JS number");
@ -1380,8 +1379,6 @@ bool
MapObject::get(JSContext* cx, HandleObject obj,
HandleValue key, MutableHandleValue rval)
{
MOZ_ASSERT(MapObject::is(obj));
ValueMap& map = extract(obj);
AutoHashableValueRooter k(cx);
@ -1413,8 +1410,6 @@ MapObject::get(JSContext* cx, unsigned argc, Value* vp)
bool
MapObject::has(JSContext* cx, HandleObject obj, HandleValue key, bool* rval)
{
MOZ_ASSERT(MapObject::is(obj));
ValueMap& map = extract(obj);
AutoHashableValueRooter k(cx);
@ -1520,7 +1515,6 @@ bool
MapObject::iterator(JSContext* cx, IteratorKind kind,
HandleObject obj, MutableHandleValue iter)
{
MOZ_ASSERT(MapObject::is(obj));
ValueMap& map = extract(obj);
Rooted<JSObject*> iterobj(cx, MapIteratorObject::create(cx, obj, &map, kind));
return iterobj && (iter.setObject(*iterobj), true);
@ -1590,7 +1584,6 @@ MapObject::clear(JSContext* cx, unsigned argc, Value* vp)
bool
MapObject::clear(JSContext* cx, HandleObject obj)
{
MOZ_ASSERT(MapObject::is(obj));
ValueMap& map = extract(obj);
if (!map.clear()) {
ReportOutOfMemory(cx);
@ -1977,7 +1970,20 @@ SetObject::is(HandleValue v)
return v.isObject() && v.toObject().hasClass(&class_) && v.toObject().as<SetObject>().getPrivate();
}
ValueSet&
bool
SetObject::is(HandleObject o)
{
return o->hasClass(&class_) && o->as<SetObject>().getPrivate();
}
ValueSet &
SetObject::extract(HandleObject o)
{
MOZ_ASSERT(o->hasClass(&SetObject::class_));
return *o->as<SetObject>().getData();
}
ValueSet &
SetObject::extract(CallReceiver call)
{
MOZ_ASSERT(call.thisv().isObject());
@ -1985,6 +1991,16 @@ SetObject::extract(CallReceiver call)
return *static_cast<SetObject&>(call.thisv().toObject()).getData();
}
uint32_t
SetObject::size(JSContext *cx, HandleObject obj)
{
MOZ_ASSERT(SetObject::is(obj));
ValueSet &set = extract(obj);
static_assert(sizeof(set.count()) <= sizeof(uint32_t),
"set count must be precisely representable as a JS number");
return set.count();
}
bool
SetObject::size_impl(JSContext* cx, CallArgs args)
{
@ -2016,7 +2032,22 @@ SetObject::has_impl(JSContext* cx, CallArgs args)
}
bool
SetObject::has(JSContext* cx, unsigned argc, Value* vp)
SetObject::has(JSContext *cx, HandleObject obj, HandleValue key, bool *rval)
{
MOZ_ASSERT(SetObject::is(obj));
ValueSet &set = extract(obj);
AutoHashableValueRooter k(cx);
if (!k.setValue(cx, key))
return false;
*rval = set.has(k);
return true;
}
bool
SetObject::has(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
return CallNonGenericMethod<SetObject::is, SetObject::has_impl>(cx, args);
@ -2046,7 +2077,25 @@ SetObject::add(JSContext* cx, unsigned argc, Value* vp)
}
bool
SetObject::delete_impl(JSContext* cx, CallArgs args)
SetObject::delete_(JSContext *cx, HandleObject obj, HandleValue key, bool *rval)
{
MOZ_ASSERT(SetObject::is(obj));
ValueSet &set = extract(obj);
AutoHashableValueRooter k(cx);
if (!k.setValue(cx, key))
return false;
if (!set.remove(k, rval)) {
ReportOutOfMemory(cx);
return false;
}
return true;
}
bool
SetObject::delete_impl(JSContext *cx, CallArgs args)
{
MOZ_ASSERT(is(args.thisv()));
@ -2069,7 +2118,17 @@ SetObject::delete_(JSContext* cx, unsigned argc, Value* vp)
}
bool
SetObject::iterator_impl(JSContext* cx, CallArgs args, IteratorKind kind)
SetObject::iterator(JSContext *cx, IteratorKind kind,
HandleObject obj, MutableHandleValue iter)
{
MOZ_ASSERT(SetObject::is(obj));
ValueSet &set = extract(obj);
Rooted<JSObject*> iterobj(cx, SetIteratorObject::create(cx, obj, &set, kind));
return iterobj && (iter.setObject(*iterobj), true);
}
bool
SetObject::iterator_impl(JSContext *cx, CallArgs args, IteratorKind kind)
{
Rooted<SetObject*> setobj(cx, &args.thisv().toObject().as<SetObject>());
ValueSet& set = *setobj->getData();
@ -2107,7 +2166,19 @@ SetObject::entries(JSContext* cx, unsigned argc, Value* vp)
}
bool
SetObject::clear_impl(JSContext* cx, CallArgs args)
SetObject::clear(JSContext *cx, HandleObject obj)
{
MOZ_ASSERT(SetObject::is(obj));
ValueSet &set = extract(obj);
if (!set.clear()) {
ReportOutOfMemory(cx);
return false;
}
return true;
}
bool
SetObject::clear_impl(JSContext *cx, CallArgs args)
{
Rooted<SetObject*> setobj(cx, &args.thisv().toObject().as<SetObject>());
if (!setobj->getData()->clear()) {
@ -2222,3 +2293,70 @@ JS::MapEntries(JSContext* cx, HandleObject obj, MutableHandleValue rval)
assertSameCompartment(cx, rval);
return MapObject::iterator(cx, MapObject::Entries, obj, rval);
}
JS_PUBLIC_API(JSObject *)
JS::NewSetObject(JSContext *cx)
{
return SetObject::create(cx);
}
JS_PUBLIC_API(uint32_t)
JS::SetSize(JSContext *cx, HandleObject obj)
{
CHECK_REQUEST(cx);
return SetObject::size(cx, obj);
}
JS_PUBLIC_API(bool)
JS::SetHas(JSContext *cx, HandleObject obj, HandleValue key, bool *rval)
{
CHECK_REQUEST(cx);
assertSameCompartment(cx, key);
return SetObject::has(cx, obj, key, rval);
}
JS_PUBLIC_API(bool)
JS::SetDelete(JSContext *cx, HandleObject obj, HandleValue key, bool *rval)
{
CHECK_REQUEST(cx);
assertSameCompartment(cx, key);
return SetObject::delete_(cx, obj, key, rval);
}
JS_PUBLIC_API(bool)
JS::SetAdd(JSContext *cx, HandleObject obj,
HandleValue key)
{
CHECK_REQUEST(cx);
assertSameCompartment(cx, key);
return SetObject::add(cx, obj, key);
}
JS_PUBLIC_API(bool)
JS::SetClear(JSContext *cx, HandleObject obj)
{
CHECK_REQUEST(cx);
return SetObject::clear(cx, obj);
}
JS_PUBLIC_API(bool)
JS::SetKeys(JSContext *cx, HandleObject obj, MutableHandleValue rval)
{
return SetValues(cx, obj, rval);
}
JS_PUBLIC_API(bool)
JS::SetValues(JSContext *cx, HandleObject obj, MutableHandleValue rval)
{
CHECK_REQUEST(cx);
assertSameCompartment(cx, rval);
return SetObject::iterator(cx, SetObject::Values, obj, rval);
}
JS_PUBLIC_API(bool)
JS::SetEntries(JSContext *cx, HandleObject obj, MutableHandleValue rval)
{
CHECK_REQUEST(cx);
assertSameCompartment(cx, rval);
return SetObject::iterator(cx, SetObject::Entries, obj, rval);
}

View File

@ -146,23 +146,31 @@ class SetObject : public NativeObject {
static JSObject* initClass(JSContext* cx, JSObject* obj);
static const Class class_;
static bool keys(JSContext* cx, HandleObject obj, JS::AutoValueVector* keys);
static bool values(JSContext* cx, unsigned argc, Value* vp);
static bool add(JSContext* cx, HandleObject obj, HandleValue key);
static bool has(JSContext* cx, unsigned argc, Value* vp);
static SetObject* create(JSContext* cx);
static bool keys(JSContext *cx, HandleObject obj, JS::AutoValueVector *keys);
static bool values(JSContext *cx, unsigned argc, Value *vp);
static bool add(JSContext *cx, HandleObject obj, HandleValue key);
static bool has(JSContext *cx, unsigned argc, Value *vp);
static SetObject* create(JSContext *cx);
static uint32_t size(JSContext *cx, HandleObject obj);
static bool has(JSContext *cx, HandleObject obj, HandleValue key, bool* rval);
static bool clear(JSContext *cx, HandleObject obj);
static bool iterator(JSContext *cx, IteratorKind kind, HandleObject obj, MutableHandleValue iter);
static bool delete_(JSContext *cx, HandleObject obj, HandleValue key, bool *rval);
private:
static const JSPropertySpec properties[];
static const JSFunctionSpec methods[];
static const JSPropertySpec staticProperties[];
ValueSet* getData() { return static_cast<ValueSet*>(getPrivate()); }
static ValueSet & extract(HandleObject o);
static ValueSet & extract(CallReceiver call);
static void mark(JSTracer* trc, JSObject* obj);
static void finalize(FreeOp* fop, JSObject* obj);
static bool construct(JSContext* cx, unsigned argc, Value* vp);
static bool is(HandleValue v);
static bool is(HandleObject o);
static bool iterator_impl(JSContext* cx, CallArgs args, IteratorKind kind);

View File

@ -4724,6 +4724,36 @@ MapValues(JSContext* cx, HandleObject obj, MutableHandleValue rval);
extern JS_PUBLIC_API(bool)
MapEntries(JSContext* cx, HandleObject obj, MutableHandleValue rval);
/*
* Set
*/
extern JS_PUBLIC_API(JSObject *)
NewSetObject(JSContext *cx);
extern JS_PUBLIC_API(uint32_t)
SetSize(JSContext *cx, HandleObject obj);
extern JS_PUBLIC_API(bool)
SetHas(JSContext *cx, HandleObject obj, HandleValue key, bool *rval);
extern JS_PUBLIC_API(bool)
SetDelete(JSContext *cx, HandleObject obj, HandleValue key, bool *rval);
extern JS_PUBLIC_API(bool)
SetAdd(JSContext *cx, HandleObject obj, HandleValue key);
extern JS_PUBLIC_API(bool)
SetClear(JSContext *cx, HandleObject obj);
extern JS_PUBLIC_API(bool)
SetKeys(JSContext *cx, HandleObject obj, MutableHandleValue rval);
extern JS_PUBLIC_API(bool)
SetValues(JSContext *cx, HandleObject obj, MutableHandleValue rval);
extern JS_PUBLIC_API(bool)
SetEntries(JSContext *cx, HandleObject obj, MutableHandleValue rval);
} /* namespace JS */
/*