Bug 688277, part 1 - add JS friend API to get keys of a weak map. r=jorendorff

This commit is contained in:
Andrew McCreight 2011-10-13 09:33:39 -07:00
parent 6e8818f349
commit e07b95d185
3 changed files with 32 additions and 1 deletions

View File

@ -79,6 +79,9 @@ JS_SetProtoCalled(JSContext *cx);
extern JS_FRIEND_API(size_t)
JS_GetCustomIteratorCount(JSContext *cx);
extern JS_FRIEND_API(JSBool)
JS_NondeterministicGetWeakMapKeys(JSContext *cx, JSObject *obj, JSObject **ret);
enum {
JS_TELEMETRY_GC_REASON,
JS_TELEMETRY_GC_IS_COMPARTMENTAL,

View File

@ -42,6 +42,7 @@
#include <string.h>
#include "jsapi.h"
#include "jscntxt.h"
#include "jsfriendapi.h"
#include "jsgc.h"
#include "jsobj.h"
#include "jsgc.h"
@ -232,6 +233,27 @@ WeakMap_set(JSContext *cx, uintN argc, Value *vp)
return false;
}
JS_FRIEND_API(JSBool)
JS_NondeterministicGetWeakMapKeys(JSContext *cx, JSObject *obj, JSObject **ret)
{
if (!obj || !obj->isWeakMap()) {
*ret = NULL;
return true;
}
JSObject *arr = NewDenseEmptyArray(cx);
if (!arr)
return false;
ObjectValueMap *map = GetObjectMap(obj);
if (map) {
for (ObjectValueMap::Range r = map->nondeterministicAll(); !r.empty(); r.popFront()) {
if (!js_NewbornArrayPush(cx, arr, ObjectValue(*r.front().key)))
return false;
}
}
*ret = arr;
return true;
}
static void
WeakMap_mark(JSTracer *trc, JSObject *obj)
{

View File

@ -192,13 +192,19 @@ template <class Key, class Value,
class WeakMap : public HashMap<Key, Value, HashPolicy, RuntimeAllocPolicy>, public WeakMapBase {
private:
typedef HashMap<Key, Value, HashPolicy, RuntimeAllocPolicy> Base;
typedef typename Base::Range Range;
typedef typename Base::Enum Enum;
public:
typedef typename Base::Range Range;
explicit WeakMap(JSRuntime *rt) : Base(rt) { }
explicit WeakMap(JSContext *cx) : Base(cx) { }
// Use with caution, as result can be affected by garbage collection.
Range nondeterministicAll() {
return Base::all();
}
private:
void nonMarkingTrace(JSTracer *tracer) {
MarkPolicy t(tracer);