Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2
* vim: set ts=8 sts=2 et sw=2 tw=80:
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 TryPreserveReflector(JSContext* cx, HandleObject obj) {
19
if (obj->getClass()->isDOMClass()) {
20
MOZ_ASSERT(cx->runtime()->preserveWrapperCallback);
21
if (!cx->runtime()->preserveWrapperCallback(cx, obj)) {
22
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
23
JSMSG_BAD_WEAKMAP_KEY);
24
return false;
25
}
26
}
27
return true;
28
}
29
30
static MOZ_ALWAYS_INLINE bool WeakCollectionPutEntryInternal(
31
JSContext* cx, Handle<WeakCollectionObject*> obj, HandleObject key,
32
HandleValue value) {
33
ObjectValueWeakMap* map = obj->getMap();
34
if (!map) {
35
auto newMap = cx->make_unique<ObjectValueWeakMap>(cx, obj.get());
36
if (!newMap) {
37
return false;
38
}
39
map = newMap.release();
40
InitObjectPrivate(obj, map, MemoryUse::WeakMapObject);
41
}
42
43
// Preserve wrapped native keys to prevent wrapper optimization.
44
if (!TryPreserveReflector(cx, key)) {
45
return false;
46
}
47
48
RootedObject delegate(cx, UncheckedUnwrapWithoutExpose(key));
49
if (delegate && !TryPreserveReflector(cx, delegate)) {
50
return false;
51
}
52
53
MOZ_ASSERT(key->compartment() == obj->compartment());
54
MOZ_ASSERT_IF(value.isObject(),
55
value.toObject().compartment() == obj->compartment());
56
if (!map->put(key, value)) {
57
JS_ReportOutOfMemory(cx);
58
return false;
59
}
60
return true;
61
}
62
63
} // namespace js
64
65
#endif /* builtin_WeakMapObject_inl_h */