/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- * 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 "js/WeakMapPtr.h" #include "jsweakmap.h" // // Machinery for the externally-linkable JS::WeakMapPtr, which wraps js::WeakMap // for a few public data types. // using namespace js; namespace details { template struct DataType { }; template<> struct DataType { using BarrieredType = HeapPtr; using HasherType = MovableCellHasher; static JSObject* NullValue() { return nullptr; } }; template<> struct DataType { using BarrieredType = HeapPtr; static JS::Value NullValue() { return JS::UndefinedValue(); } }; template struct Utils { typedef typename DataType::BarrieredType KeyType; typedef typename DataType::HasherType HasherType; typedef typename DataType::BarrieredType ValueType; typedef WeakMap Type; typedef Type* PtrType; static PtrType cast(void* ptr) { return static_cast(ptr); } }; } /* namespace */ template void JS::WeakMapPtr::destroy() { MOZ_ASSERT(initialized()); js_delete(details::Utils::cast(ptr)); ptr = nullptr; } template bool JS::WeakMapPtr::init(JSContext* cx) { MOZ_ASSERT(!initialized()); typename details::Utils::PtrType map = cx->runtime()->new_::Type>(cx); if (!map || !map->init()) return false; ptr = map; return true; } template void JS::WeakMapPtr::trace(JSTracer* trc) { MOZ_ASSERT(initialized()); return details::Utils::cast(ptr)->trace(trc); } template V JS::WeakMapPtr::lookup(const K& key) { MOZ_ASSERT(initialized()); typename details::Utils::Type::Ptr result = details::Utils::cast(ptr)->lookup(key); if (!result) return details::DataType::NullValue(); return result->value(); } template bool JS::WeakMapPtr::put(JSContext* cx, const K& key, const V& value) { MOZ_ASSERT(initialized()); return details::Utils::cast(ptr)->put(key, value); } // // Supported specializations of JS::WeakMap: // template class JS_PUBLIC_API(JS::WeakMapPtr); #ifdef DEBUG // Nobody's using this at the moment, but we want to make sure it compiles. template class JS_PUBLIC_API(JS::WeakMapPtr); #endif