2011-04-16 21:23:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=4 sw=4 et tw=99:
|
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
|
|
|
|
* May 28, 2008.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Foundation
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Andreas Gal <gal@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jscntxt.h"
|
|
|
|
#include "jsgc.h"
|
|
|
|
#include "jshashtable.h"
|
|
|
|
#include "jsobj.h"
|
|
|
|
#include "jsgc.h"
|
2011-04-15 16:56:08 -07:00
|
|
|
#include "jsgcmark.h"
|
2011-04-16 21:23:44 -07:00
|
|
|
#include "jsweakmap.h"
|
|
|
|
|
|
|
|
#include "jsgcinlines.h"
|
|
|
|
#include "jsobjinlines.h"
|
|
|
|
|
|
|
|
using namespace js;
|
|
|
|
|
2011-06-14 19:21:47 -07:00
|
|
|
namespace js {
|
|
|
|
|
2011-04-16 21:23:44 -07:00
|
|
|
bool
|
2011-06-14 19:21:47 -07:00
|
|
|
WeakMapBase::markAllIteratively(JSTracer *tracer)
|
2011-04-16 21:23:44 -07:00
|
|
|
{
|
2011-06-14 19:21:47 -07:00
|
|
|
bool markedAny = false;
|
|
|
|
JSRuntime *rt = tracer->context->runtime;
|
|
|
|
for (WeakMapBase *m = rt->gcWeakMapList; m; m = m->next) {
|
|
|
|
if (m->markIteratively(tracer))
|
|
|
|
markedAny = true;
|
|
|
|
}
|
|
|
|
return markedAny;
|
2011-04-16 21:23:44 -07:00
|
|
|
}
|
|
|
|
|
2011-06-14 19:21:47 -07:00
|
|
|
void
|
|
|
|
WeakMapBase::sweepAll(JSTracer *tracer)
|
|
|
|
{
|
|
|
|
JSRuntime *rt = tracer->context->runtime;
|
|
|
|
for (WeakMapBase *m = rt->gcWeakMapList; m; m = m->next)
|
|
|
|
m->sweep(tracer);
|
|
|
|
}
|
2011-04-16 21:23:44 -07:00
|
|
|
|
2011-06-14 19:21:47 -07:00
|
|
|
} /* namespace js */
|
|
|
|
|
|
|
|
typedef WeakMap<JSObject *, Value> ObjectValueMap;
|
|
|
|
|
|
|
|
static ObjectValueMap *
|
|
|
|
GetObjectMap(JSObject *obj)
|
2011-04-16 21:23:44 -07:00
|
|
|
{
|
2011-06-14 19:21:47 -07:00
|
|
|
JS_ASSERT(obj->isWeakMap());
|
|
|
|
return (ObjectValueMap *)obj->getPrivate();
|
2011-04-16 21:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject *
|
|
|
|
NonNullObject(JSContext *cx, Value *vp)
|
|
|
|
{
|
|
|
|
if (vp->isPrimitive()) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NOT_NONNULL_OBJECT);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return &vp->toObject();
|
|
|
|
}
|
|
|
|
|
2011-06-14 19:21:47 -07:00
|
|
|
static JSBool
|
|
|
|
WeakMap_has(JSContext *cx, uintN argc, Value *vp)
|
2011-04-16 21:23:44 -07:00
|
|
|
{
|
|
|
|
JSObject *obj = ToObject(cx, &vp[1]);
|
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
if (!obj->isWeakMap()) {
|
2011-06-14 19:21:47 -07:00
|
|
|
ReportIncompatibleMethod(cx, vp, &WeakMapClass);
|
2011-04-16 21:23:44 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (argc < 1) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_MORE_ARGS_NEEDED,
|
|
|
|
"WeakMap.has", "0", "s");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JSObject *key = NonNullObject(cx, &vp[2]);
|
|
|
|
if (!key)
|
|
|
|
return false;
|
2011-06-14 19:21:47 -07:00
|
|
|
ObjectValueMap *map = GetObjectMap(obj);
|
|
|
|
if (map) {
|
|
|
|
ObjectValueMap::Ptr ptr = map->lookup(key);
|
2011-04-16 21:23:44 -07:00
|
|
|
if (ptr) {
|
|
|
|
*vp = BooleanValue(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*vp = BooleanValue(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-14 19:21:47 -07:00
|
|
|
static JSBool
|
|
|
|
WeakMap_get(JSContext *cx, uintN argc, Value *vp)
|
2011-04-16 21:23:44 -07:00
|
|
|
{
|
|
|
|
JSObject *obj = ToObject(cx, &vp[1]);
|
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
if (!obj->isWeakMap()) {
|
2011-06-14 19:21:47 -07:00
|
|
|
ReportIncompatibleMethod(cx, vp, &WeakMapClass);
|
2011-04-16 21:23:44 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (argc < 1) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_MORE_ARGS_NEEDED,
|
|
|
|
"WeakMap.get", "0", "s");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JSObject *key = NonNullObject(cx, &vp[2]);
|
|
|
|
if (!key)
|
|
|
|
return false;
|
2011-06-14 19:21:47 -07:00
|
|
|
ObjectValueMap *map = GetObjectMap(obj);
|
|
|
|
if (map) {
|
|
|
|
ObjectValueMap::Ptr ptr = map->lookup(key);
|
2011-04-16 21:23:44 -07:00
|
|
|
if (ptr) {
|
|
|
|
*vp = ptr->value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*vp = (argc > 1) ? vp[3] : UndefinedValue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-14 19:21:47 -07:00
|
|
|
static JSBool
|
|
|
|
WeakMap_delete(JSContext *cx, uintN argc, Value *vp)
|
2011-04-16 21:23:44 -07:00
|
|
|
{
|
|
|
|
JSObject *obj = ToObject(cx, &vp[1]);
|
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
if (!obj->isWeakMap()) {
|
2011-06-14 19:21:47 -07:00
|
|
|
ReportIncompatibleMethod(cx, vp, &WeakMapClass);
|
2011-04-16 21:23:44 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (argc < 1) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_MORE_ARGS_NEEDED,
|
|
|
|
"WeakMap.delete", "0", "s");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JSObject *key = NonNullObject(cx, &vp[2]);
|
|
|
|
if (!key)
|
|
|
|
return false;
|
2011-06-14 19:21:47 -07:00
|
|
|
ObjectValueMap *map = GetObjectMap(obj);
|
|
|
|
if (map) {
|
|
|
|
ObjectValueMap::Ptr ptr = map->lookup(key);
|
2011-04-16 21:23:44 -07:00
|
|
|
if (ptr) {
|
2011-06-14 19:21:47 -07:00
|
|
|
map->remove(ptr);
|
2011-04-16 21:23:44 -07:00
|
|
|
*vp = BooleanValue(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*vp = BooleanValue(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-14 19:21:47 -07:00
|
|
|
static JSBool
|
|
|
|
WeakMap_set(JSContext *cx, uintN argc, Value *vp)
|
2011-04-16 21:23:44 -07:00
|
|
|
{
|
|
|
|
JSObject *obj = ToObject(cx, &vp[1]);
|
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
if (!obj->isWeakMap()) {
|
2011-06-14 19:21:47 -07:00
|
|
|
ReportIncompatibleMethod(cx, vp, &WeakMapClass);
|
2011-04-16 21:23:44 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (argc < 1) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_MORE_ARGS_NEEDED,
|
|
|
|
"WeakMap.set", "0", "s");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JSObject *key = NonNullObject(cx, &vp[2]);
|
|
|
|
if (!key)
|
|
|
|
return false;
|
|
|
|
Value value = (argc > 1) ? vp[3] : UndefinedValue();
|
|
|
|
|
2011-06-14 19:21:47 -07:00
|
|
|
ObjectValueMap *map = GetObjectMap(obj);
|
|
|
|
if (!map) {
|
|
|
|
map = cx->new_<ObjectValueMap>(cx);
|
|
|
|
if (!map->init()) {
|
|
|
|
cx->delete_(map);
|
2011-04-16 21:23:44 -07:00
|
|
|
goto out_of_memory;
|
|
|
|
}
|
2011-06-14 19:21:47 -07:00
|
|
|
obj->setPrivate(map);
|
2011-04-16 21:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
*vp = UndefinedValue();
|
2011-06-14 19:21:47 -07:00
|
|
|
if (!map->put(key, value))
|
2011-06-14 19:21:47 -07:00
|
|
|
goto out_of_memory;
|
|
|
|
return true;
|
2011-04-16 21:23:44 -07:00
|
|
|
|
|
|
|
out_of_memory:
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-14 19:21:47 -07:00
|
|
|
static void
|
|
|
|
WeakMap_mark(JSTracer *trc, JSObject *obj)
|
2011-04-16 21:23:44 -07:00
|
|
|
{
|
2011-07-19 09:30:16 -07:00
|
|
|
if (ObjectValueMap *map = GetObjectMap(obj))
|
|
|
|
map->trace(trc);
|
2011-04-16 21:23:44 -07:00
|
|
|
}
|
|
|
|
|
2011-06-14 19:21:47 -07:00
|
|
|
static void
|
|
|
|
WeakMap_finalize(JSContext *cx, JSObject *obj)
|
2011-04-16 21:23:44 -07:00
|
|
|
{
|
2011-06-14 19:21:47 -07:00
|
|
|
ObjectValueMap *map = GetObjectMap(obj);
|
|
|
|
cx->delete_(map);
|
2011-04-16 21:23:44 -07:00
|
|
|
}
|
|
|
|
|
2011-06-14 19:21:47 -07:00
|
|
|
static JSBool
|
|
|
|
WeakMap_construct(JSContext *cx, uintN argc, Value *vp)
|
2011-04-16 21:23:44 -07:00
|
|
|
{
|
2011-06-14 19:21:47 -07:00
|
|
|
JSObject *obj = NewBuiltinClassInstance(cx, &WeakMapClass);
|
2011-04-16 21:23:44 -07:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
obj->setPrivate(NULL);
|
|
|
|
|
|
|
|
vp->setObject(*obj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-02 17:23:26 -07:00
|
|
|
Class js::WeakMapClass = {
|
2011-04-16 21:23:44 -07:00
|
|
|
"WeakMap",
|
|
|
|
JSCLASS_HAS_PRIVATE |
|
|
|
|
JSCLASS_HAS_CACHED_PROTO(JSProto_WeakMap),
|
|
|
|
PropertyStub, /* addProperty */
|
|
|
|
PropertyStub, /* delProperty */
|
|
|
|
PropertyStub, /* getProperty */
|
|
|
|
StrictPropertyStub, /* setProperty */
|
|
|
|
EnumerateStub,
|
|
|
|
ResolveStub,
|
|
|
|
ConvertStub,
|
2011-06-14 19:21:47 -07:00
|
|
|
WeakMap_finalize,
|
2011-04-16 21:23:44 -07:00
|
|
|
NULL, /* reserved0 */
|
|
|
|
NULL, /* checkAccess */
|
|
|
|
NULL, /* call */
|
|
|
|
NULL, /* construct */
|
|
|
|
NULL, /* xdrObject */
|
|
|
|
NULL, /* hasInstance */
|
2011-06-14 19:21:47 -07:00
|
|
|
WeakMap_mark
|
2011-04-16 21:23:44 -07:00
|
|
|
};
|
|
|
|
|
2011-06-14 19:21:47 -07:00
|
|
|
static JSFunctionSpec weak_map_methods[] = {
|
|
|
|
JS_FN("has", WeakMap_has, 1, 0),
|
|
|
|
JS_FN("get", WeakMap_get, 2, 0),
|
|
|
|
JS_FN("delete", WeakMap_delete, 1, 0),
|
|
|
|
JS_FN("set", WeakMap_set, 2, 0),
|
2011-04-16 21:23:44 -07:00
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_InitWeakMapClass(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2011-06-14 19:21:47 -07:00
|
|
|
JSObject *proto = js_InitClass(cx, obj, NULL, &WeakMapClass, WeakMap_construct, 0,
|
|
|
|
NULL, weak_map_methods, NULL, NULL);
|
2011-04-16 21:23:44 -07:00
|
|
|
if (!proto)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
proto->setPrivate(NULL);
|
|
|
|
|
|
|
|
return proto;
|
|
|
|
}
|