Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/js/src/builtin/WeakMapObject-inl.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
 * vim: set ts=8 sts=4 et sw=4 tw=99:
3
 * This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef builtin_WeakMapObject_inl_h
8
#define builtin_WeakMapObject_inl_h
9
10
#include "builtin/WeakMapObject.h"
11
12
#include "vm/ProxyObject.h"
13
14
#include "gc/WeakMap-inl.h"
15
16
namespace js {
17
18
static bool
19
TryPreserveReflector(JSContext* cx, HandleObject obj)
20
0
{
21
0
    if (obj->getClass()->isWrappedNative() ||
22
0
        obj->getClass()->isDOMClass() ||
23
0
        (obj->is<ProxyObject>() &&
24
0
         obj->as<ProxyObject>().handler()->family() == GetDOMProxyHandlerFamily()))
25
0
    {
26
0
        MOZ_ASSERT(cx->runtime()->preserveWrapperCallback);
27
0
        if (!cx->runtime()->preserveWrapperCallback(cx, obj)) {
28
0
            JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_BAD_WEAKMAP_KEY);
29
0
            return false;
30
0
        }
31
0
    }
32
0
    return true;
33
0
}
34
35
static MOZ_ALWAYS_INLINE bool
36
WeakCollectionPutEntryInternal(JSContext* cx, Handle<WeakCollectionObject*> obj,
37
                               HandleObject key, HandleValue value)
38
0
{
39
0
    ObjectValueMap* map = obj->getMap();
40
0
    if (!map) {
41
0
        auto newMap = cx->make_unique<ObjectValueMap>(cx, obj.get());
42
0
        if (!newMap) {
43
0
            return false;
44
0
        }
45
0
        map = newMap.release();
46
0
        obj->setPrivate(map);
47
0
    }
48
0
49
0
    // Preserve wrapped native keys to prevent wrapper optimization.
50
0
    if (!TryPreserveReflector(cx, key)) {
51
0
        return false;
52
0
    }
53
0
54
0
    if (JSWeakmapKeyDelegateOp op = key->getClass()->extWeakmapKeyDelegateOp()) {
55
0
        RootedObject delegate(cx, op(key));
56
0
        if (delegate && !TryPreserveReflector(cx, delegate)) {
57
0
            return false;
58
0
        }
59
0
    }
60
0
61
0
    MOZ_ASSERT(key->compartment() == obj->compartment());
62
0
    MOZ_ASSERT_IF(value.isObject(), value.toObject().compartment() == obj->compartment());
63
0
    if (!map->put(key, value)) {
64
0
        JS_ReportOutOfMemory(cx);
65
0
        return false;
66
0
    }
67
0
    return true;
68
0
}
69
70
} // namespace js
71
72
#endif /* builtin_WeakMapObject_inl_h */