From 0fbacfc43d891bd3e739672ec768fc3f94a201cc Mon Sep 17 00:00:00 2001 From: Kyle Machulis Date: Mon, 9 Mar 2015 17:19:21 -0700 Subject: [PATCH] Bug 1159469 - Add public jsapi ES6 Map delete method; r=jorendorff --- js/src/builtin/MapObject.cpp | 28 ++++++++++++++++++++++++++-- js/src/builtin/MapObject.h | 13 +++++++------ js/src/jsapi.h | 3 +++ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/js/src/builtin/MapObject.cpp b/js/src/builtin/MapObject.cpp index 1b2308eb9152..53ae80329c2f 100644 --- a/js/src/builtin/MapObject.cpp +++ b/js/src/builtin/MapObject.cpp @@ -1469,7 +1469,23 @@ MapObject::set(JSContext* cx, unsigned argc, Value* vp) } bool -MapObject::delete_impl(JSContext* cx, CallArgs args) +MapObject::delete_(JSContext *cx, HandleObject obj, HandleValue key, bool *rval) +{ + ValueMap &map = extract(obj); + AutoHashableValueRooter k(cx); + + if (!k.setValue(cx, key)) + return false; + + if (!map.remove(k, rval)) { + ReportOutOfMemory(cx); + return false; + } + return true; +} + +bool +MapObject::delete_impl(JSContext *cx, CallArgs args) { // MapObject::mark does not mark deleted entries. Incremental GC therefore // requires that no RelocatableValue objects pointing to heap values be @@ -2160,7 +2176,15 @@ JS::MapHas(JSContext* cx, HandleObject obj, HandleValue key, bool* rval) } JS_PUBLIC_API(bool) -JS::MapSet(JSContext* cx, HandleObject obj, +JS::MapDelete(JSContext *cx, HandleObject obj, HandleValue key, bool *rval) +{ + CHECK_REQUEST(cx); + assertSameCompartment(cx, key); + return MapObject::delete_(cx, obj, key, rval); +} + +JS_PUBLIC_API(bool) +JS::MapSet(JSContext *cx, HandleObject obj, HandleValue key, HandleValue val) { CHECK_REQUEST(cx); diff --git a/js/src/builtin/MapObject.h b/js/src/builtin/MapObject.h index abf1b6ffe42d..385e21a73730 100644 --- a/js/src/builtin/MapObject.h +++ b/js/src/builtin/MapObject.h @@ -98,12 +98,13 @@ class MapObject : public NativeObject { static bool has(JSContext* cx, unsigned argc, Value* vp); static MapObject* create(JSContext* cx); - static uint32_t size(JSContext* cx, HandleObject obj); - static bool get(JSContext* cx, HandleObject obj, HandleValue key, MutableHandleValue rval); - static bool has(JSContext* cx, HandleObject obj, HandleValue key, bool* rval); - static bool set(JSContext* cx, HandleObject obj, HandleValue key, HandleValue val); - static bool clear(JSContext* cx, HandleObject obj); - static bool iterator(JSContext* cx, IteratorKind kind, HandleObject obj, MutableHandleValue iter); + static uint32_t size(JSContext *cx, HandleObject obj); + static bool get(JSContext *cx, HandleObject obj, HandleValue key, MutableHandleValue rval); + static bool has(JSContext *cx, HandleObject obj, HandleValue key, bool* rval); + static bool delete_(JSContext *cx, HandleObject obj, HandleValue key, bool* rval); + static bool set(JSContext *cx, HandleObject obj, HandleValue key, HandleValue val); + static bool clear(JSContext *cx, HandleObject obj); + static bool iterator(JSContext *cx, IteratorKind kind, HandleObject obj, MutableHandleValue iter); private: static const JSPropertySpec properties[]; diff --git a/js/src/jsapi.h b/js/src/jsapi.h index 49843e4f6420..76680cccb9d6 100644 --- a/js/src/jsapi.h +++ b/js/src/jsapi.h @@ -4709,6 +4709,9 @@ MapHas(JSContext* cx, HandleObject obj, HandleValue key, bool* rval); extern JS_PUBLIC_API(bool) MapSet(JSContext* cx, HandleObject obj, HandleValue key, HandleValue val); +extern JS_PUBLIC_API(bool) +MapDelete(JSContext *cx, HandleObject obj, HandleValue key, bool *rval); + extern JS_PUBLIC_API(bool) MapClear(JSContext* cx, HandleObject obj);