js/src/builtin/WeakMapObject.cpp
author Jeff Walden <jwalden@mit.edu>
Fri, 04 Jan 2019 16:33:01 -0600
changeset 509720 e32d3be39b84c40e5bf8dee55d3a3ae8c6773916
parent 509709 cf00fd0f080c31a4d475585e7583180a62007596
child 509937 77a8b17163210f7d4bba7e800c2fcb55b0690a24
permissions -rw-r--r--
Back out f8a29b8bb211, d1267711aef0, df1b7a886a9d, 0f6a3dd2c2f7, e6628922b7be, 0a374ac233cd, cf00fd0f080c, 6fc24c4e7798, ab2e4a2e5dd2, 2d17e4ef5e71, 4a8940073f8c, ea3ab4b83152, c7577c232591, e58454c68f0f, and 0e39815d1bce for breaking a single Android J10 and nothing else. (That J10 task must not like deck chairs...) r=bustage

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "builtin/WeakMapObject-inl.h"

#include "jsapi.h"

#include "builtin/WeakSetObject.h"
#include "gc/FreeOp.h"
#include "vm/JSContext.h"
#include "vm/SelfHosting.h"

#include "vm/Interpreter-inl.h"

using namespace js;

/* static */ MOZ_ALWAYS_INLINE bool WeakMapObject::is(HandleValue v) {
  return v.isObject() && v.toObject().is<WeakMapObject>();
}

/* static */ MOZ_ALWAYS_INLINE bool WeakMapObject::has_impl(
    JSContext* cx, const CallArgs& args) {
  MOZ_ASSERT(is(args.thisv()));

  if (!args.get(0).isObject()) {
    args.rval().setBoolean(false);
    return true;
  }

  if (ObjectValueMap* map =
          args.thisv().toObject().as<WeakMapObject>().getMap()) {
    JSObject* key = &args[0].toObject();
    if (map->has(key)) {
      args.rval().setBoolean(true);
      return true;
    }
  }

  args.rval().setBoolean(false);
  return true;
}

/* static */ bool WeakMapObject::has(JSContext* cx, unsigned argc, Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);
  return CallNonGenericMethod<WeakMapObject::is, WeakMapObject::has_impl>(cx,
                                                                          args);
}

/* static */ MOZ_ALWAYS_INLINE bool WeakMapObject::get_impl(
    JSContext* cx, const CallArgs& args) {
  MOZ_ASSERT(WeakMapObject::is(args.thisv()));

  if (!args.get(0).isObject()) {
    args.rval().setUndefined();
    return true;
  }

  if (ObjectValueMap* map =
          args.thisv().toObject().as<WeakMapObject>().getMap()) {
    JSObject* key = &args[0].toObject();
    if (ObjectValueMap::Ptr ptr = map->lookup(key)) {
      args.rval().set(ptr->value());
      return true;
    }
  }

  args.rval().setUndefined();
  return true;
}

/* static */ bool WeakMapObject::get(JSContext* cx, unsigned argc, Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);
  return CallNonGenericMethod<WeakMapObject::is, WeakMapObject::get_impl>(cx,
                                                                          args);
}

/* static */ MOZ_ALWAYS_INLINE bool WeakMapObject::delete_impl(
    JSContext* cx, const CallArgs& args) {
  MOZ_ASSERT(WeakMapObject::is(args.thisv()));

  if (!args.get(0).isObject()) {
    args.rval().setBoolean(false);
    return true;
  }

  if (ObjectValueMap* map =
          args.thisv().toObject().as<WeakMapObject>().getMap()) {
    JSObject* key = &args[0].toObject();
    if (ObjectValueMap::Ptr ptr = map->lookup(key)) {
      map->remove(ptr);
      args.rval().setBoolean(true);
      return true;
    }
  }

  args.rval().setBoolean(false);
  return true;
}

/* static */ bool WeakMapObject::delete_(JSContext* cx, unsigned argc,
                                         Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);
  return CallNonGenericMethod<WeakMapObject::is, WeakMapObject::delete_impl>(
      cx, args);
}

/* static */ MOZ_ALWAYS_INLINE bool WeakMapObject::set_impl(
    JSContext* cx, const CallArgs& args) {
  MOZ_ASSERT(WeakMapObject::is(args.thisv()));

  if (!args.get(0).isObject()) {
    ReportNotObjectWithName(cx, "WeakMap key", args.get(0));
    return false;
  }

  RootedObject key(cx, &args[0].toObject());
  Rooted<WeakMapObject*> map(cx, &args.thisv().toObject().as<WeakMapObject>());

  if (!WeakCollectionPutEntryInternal(cx, map, key, args.get(1))) {
    return false;
  }
  args.rval().set(args.thisv());
  return true;
}

/* static */ bool WeakMapObject::set(JSContext* cx, unsigned argc, Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);
  return CallNonGenericMethod<WeakMapObject::is, WeakMapObject::set_impl>(cx,
                                                                          args);
}

bool WeakCollectionObject::nondeterministicGetKeys(
    JSContext* cx, Handle<WeakCollectionObject*> obj, MutableHandleObject ret) {
  RootedObject arr(cx, NewDenseEmptyArray(cx));
  if (!arr) {
    return false;
  }
  if (ObjectValueMap* map = obj->getMap()) {
    // Prevent GC from mutating the weakmap while iterating.
    gc::AutoSuppressGC suppress(cx);
    for (ObjectValueMap::Base::Range r = map->all(); !r.empty(); r.popFront()) {
      JS::ExposeObjectToActiveJS(r.front().key());
      RootedObject key(cx, r.front().key());
      if (!cx->compartment()->wrap(cx, &key)) {
        return false;
      }
      if (!NewbornArrayPush(cx, arr, ObjectValue(*key))) {
        return false;
      }
    }
  }
  ret.set(arr);
  return true;
}

JS_FRIEND_API bool JS_NondeterministicGetWeakMapKeys(JSContext* cx,
                                                     HandleObject objArg,
                                                     MutableHandleObject ret) {
  RootedObject obj(cx, UncheckedUnwrap(objArg));
  if (!obj || !obj->is<WeakMapObject>()) {
    ret.set(nullptr);
    return true;
  }
  return WeakCollectionObject::nondeterministicGetKeys(
      cx, obj.as<WeakCollectionObject>(), ret);
}

static void WeakCollection_trace(JSTracer* trc, JSObject* obj) {
  if (ObjectValueMap* map = obj->as<WeakCollectionObject>().getMap()) {
    map->trace(trc);
  }
}

static void WeakCollection_finalize(FreeOp* fop, JSObject* obj) {
  MOZ_ASSERT(fop->maybeOnHelperThread());
  if (ObjectValueMap* map = obj->as<WeakCollectionObject>().getMap()) {
    fop->delete_(map);
  }
}

JS_PUBLIC_API JSObject* JS::NewWeakMapObject(JSContext* cx) {
  return NewBuiltinClassInstance<WeakMapObject>(cx);
}

JS_PUBLIC_API bool JS::IsWeakMapObject(JSObject* obj) {
  return obj->is<WeakMapObject>();
}

JS_PUBLIC_API bool JS::GetWeakMapEntry(JSContext* cx, HandleObject mapObj,
                                       HandleObject key,
                                       MutableHandleValue rval) {
  CHECK_THREAD(cx);
  cx->check(key);
  rval.setUndefined();
  ObjectValueMap* map = mapObj->as<WeakMapObject>().getMap();
  if (!map) {
    return true;
  }
  if (ObjectValueMap::Ptr ptr = map->lookup(key)) {
    // Read barrier to prevent an incorrectly gray value from escaping the
    // weak map. See the comment before UnmarkGrayChildren in gc/Marking.cpp
    ExposeValueToActiveJS(ptr->value().get());
    rval.set(ptr->value());
  }
  return true;
}

JS_PUBLIC_API bool JS::SetWeakMapEntry(JSContext* cx, HandleObject mapObj,
                                       HandleObject key, HandleValue val) {
  CHECK_THREAD(cx);
  cx->check(key, val);
  Handle<WeakMapObject*> rootedMap = mapObj.as<WeakMapObject>();
  return WeakCollectionPutEntryInternal(cx, rootedMap, key, val);
}

/* static */ bool WeakMapObject::construct(JSContext* cx, unsigned argc,
                                           Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);

  // ES6 draft rev 31 (15 Jan 2015) 23.3.1.1 step 1.
  if (!ThrowIfNotConstructing(cx, args, "WeakMap")) {
    return false;
  }

  RootedObject proto(cx);
  if (!GetPrototypeFromBuiltinConstructor(cx, args, &proto)) {
    return false;
  }

  RootedObject obj(cx, NewObjectWithClassProto<WeakMapObject>(cx, proto));
  if (!obj) {
    return false;
  }

  // Steps 5-6, 11.
  if (!args.get(0).isNullOrUndefined()) {
    FixedInvokeArgs<1> args2(cx);
    args2[0].set(args[0]);

    RootedValue thisv(cx, ObjectValue(*obj));
    if (!CallSelfHostedFunction(cx, cx->names().WeakMapConstructorInit, thisv,
                                args2, args2.rval())) {
      return false;
    }
  }

  args.rval().setObject(*obj);
  return true;
}

const ClassOps WeakCollectionObject::classOps_ = {nullptr, /* addProperty */
                                                  nullptr, /* delProperty */
                                                  nullptr, /* enumerate */
                                                  nullptr, /* newEnumerate */
                                                  nullptr, /* resolve */
                                                  nullptr, /* mayResolve */
                                                  WeakCollection_finalize,
                                                  nullptr, /* call */
                                                  nullptr, /* hasInstance */
                                                  nullptr, /* construct */
                                                  WeakCollection_trace};

const ClassSpec WeakMapObject::classSpec_ = {
    GenericCreateConstructor<WeakMapObject::construct, 0,
                             gc::AllocKind::FUNCTION>,
    GenericCreatePrototype<WeakMapObject>,
    nullptr,
    nullptr,
    WeakMapObject::methods,
    WeakMapObject::properties,
};

const Class WeakMapObject::class_ = {
    "WeakMap",
    JSCLASS_HAS_PRIVATE | JSCLASS_HAS_CACHED_PROTO(JSProto_WeakMap) |
        JSCLASS_BACKGROUND_FINALIZE,
    &WeakCollectionObject::classOps_, &WeakMapObject::classSpec_};

const Class WeakMapObject::protoClass_ = {
    js_Object_str, JSCLASS_HAS_CACHED_PROTO(JSProto_WeakMap), JS_NULL_CLASS_OPS,
    &WeakMapObject::classSpec_};

const JSPropertySpec WeakMapObject::properties[] = {
    JS_STRING_SYM_PS(toStringTag, "WeakMap", JSPROP_READONLY), JS_PS_END};

const JSFunctionSpec WeakMapObject::methods[] = {
    JS_FN("has", has, 1, 0), JS_FN("get", get, 1, 0),
    JS_FN("delete", delete_, 1, 0), JS_FN("set", set, 2, 0), JS_FS_END};