Debian Codegraph @

js/src/vm/Shape-inl.h

raw
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 vm_Shape_inl_h
8 #define vm_Shape_inl_h
9
10 #include "vm/Shape.h"
11
12 #include "vm/JSObject.h"
13 #include "vm/PropertyResult.h"
14
15 #include "gc/GCContext-inl.h"
16 #include "gc/Marking-inl.h"
17 #include "vm/PropMap-inl.h"
18
19 namespace js {
20
21 template <class ObjectSubclass>
22 /* static */ inline bool SharedShape::ensureInitialCustomShape(
23 JSContext* cx, Handle<ObjectSubclass*> obj) {
24 static_assert(std::is_base_of_v<JSObject, ObjectSubclass>,
25 "ObjectSubclass must be a subclass of JSObject");
26
27 // If the provided object has a non-empty shape, it was given the cached
28 // initial shape when created: nothing to do.
29 if (!obj->empty()) {
30 return true;
31 }
32
33 // Ensure the initial shape isn't collected under assignInitialShape, to
34 // simplify insertInitialShape.
35 Rooted<Shape*> emptyShape(cx, obj->shape());
36
37 // If no initial shape was assigned, do so.
38 Rooted<SharedShape*> shape(cx, ObjectSubclass::assignInitialShape(cx, obj));
39 if (!shape) {
40 return false;
41 }
42 MOZ_ASSERT(!obj->empty());
43
44 // Cache the initial shape, so that future instances will begin life with that
45 // shape.
46 SharedShape::insertInitialShape(cx, shape);
47 return true;
48 }
49
50 MOZ_ALWAYS_INLINE PropMap* NativeShape::lookup(JSContext* cx, PropertyKey key,
51 uint32_t* index) {
52 uint32_t len = propMapLength();
53 return len > 0 ? propMap_->lookup(cx, len, key, index) : nullptr;
54 }
55
56 MOZ_ALWAYS_INLINE PropMap* NativeShape::lookupPure(PropertyKey key,
57 uint32_t* index) {
58 uint32_t len = propMapLength();
59 return len > 0 ? propMap_->lookupPure(len, key, index) : nullptr;
60 }
61
62 inline void Shape::purgeCache(JS::GCContext* gcx) {
63 if (cache_.isShapeSetForAdd()) {
64 gcx->delete_(this, cache_.toShapeSetForAdd(), MemoryUse::ShapeSetForAdd);
65 }
66 cache_.setNone();
67 }
68
69 inline void Shape::finalize(JS::GCContext* gcx) {
70 if (!cache_.isNone()) {
71 purgeCache(gcx);
72 }
73 if (isWasmGC()) {
74 asWasmGC().finalize(gcx);
75 }
76 }
77
78 inline void WasmGCShape::init() { recGroup_->AddRef(); }
79
80 inline void WasmGCShape::finalize(JS::GCContext* gcx) { recGroup_->Release(); }
81
82 inline SharedPropMap* SharedShape::propMapMaybeForwarded() const {
83 MOZ_ASSERT(isShared());
84 PropMap* propMap = propMap_;
85 return propMap ? MaybeForwarded(propMap)->asShared() : nullptr;
86 }
87
88 static inline JS::PropertyAttributes GetPropertyAttributes(
89 JSObject* obj, PropertyResult prop) {
90 MOZ_ASSERT(obj->is<NativeObject>());
91
92 if (prop.isDenseElement()) {
93 return obj->as<NativeObject>().getElementsHeader()->elementAttributes();
94 }
95 if (prop.isTypedArrayElement()) {
96 return {JS::PropertyAttribute::Configurable,
97 JS::PropertyAttribute::Enumerable, JS::PropertyAttribute::Writable};
98 }
99
100 return prop.propertyInfo().propAttributes();
101 }
102
103 } /* namespace js */
104
105 #endif /* vm_Shape_inl_h */