Bug 1867190 - Add prefs for PHC probablities r=glandium
[gecko.git] / js / src / vm / Shape-inl.h
blobae8c4bb844e17dbf524e44fb422aa3cfcb1ec9c9
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/. */
7 #ifndef vm_Shape_inl_h
8 #define vm_Shape_inl_h
10 #include "vm/Shape.h"
12 #include "vm/JSObject.h"
13 #include "vm/PropertyResult.h"
15 #include "gc/GCContext-inl.h"
16 #include "gc/Marking-inl.h"
17 #include "vm/PropMap-inl.h"
19 namespace js {
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");
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;
33 // Ensure the initial shape isn't collected under assignInitialShape, to
34 // simplify insertInitialShape.
35 Rooted<Shape*> emptyShape(cx, obj->shape());
37 // If no initial shape was assigned, do so.
38 Rooted<SharedShape*> shape(cx, ObjectSubclass::assignInitialShape(cx, obj));
39 if (!shape) {
40 return false;
42 MOZ_ASSERT(!obj->empty());
44 // Cache the initial shape, so that future instances will begin life with that
45 // shape.
46 SharedShape::insertInitialShape(cx, shape);
47 return true;
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;
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;
62 inline void Shape::purgeCache(JS::GCContext* gcx) {
63 if (cache_.isShapeSetForAdd()) {
64 gcx->delete_(this, cache_.toShapeSetForAdd(), MemoryUse::ShapeSetForAdd);
66 cache_.setNone();
69 inline void Shape::finalize(JS::GCContext* gcx) {
70 if (!cache_.isNone()) {
71 purgeCache(gcx);
73 if (isWasmGC()) {
74 asWasmGC().finalize(gcx);
78 inline void WasmGCShape::init() { recGroup_->AddRef(); }
80 inline void WasmGCShape::finalize(JS::GCContext* gcx) { recGroup_->Release(); }
82 inline SharedPropMap* SharedShape::propMapMaybeForwarded() const {
83 MOZ_ASSERT(isShared());
84 PropMap* propMap = propMap_;
85 return propMap ? MaybeForwarded(propMap)->asShared() : nullptr;
88 static inline JS::PropertyAttributes GetPropertyAttributes(
89 JSObject* obj, PropertyResult prop) {
90 MOZ_ASSERT(obj->is<NativeObject>());
92 if (prop.isDenseElement()) {
93 return obj->as<NativeObject>().getElementsHeader()->elementAttributes();
95 if (prop.isTypedArrayElement()) {
96 return {JS::PropertyAttribute::Configurable,
97 JS::PropertyAttribute::Enumerable, JS::PropertyAttribute::Writable};
100 return prop.propertyInfo().propAttributes();
103 } /* namespace js */
105 #endif /* vm_Shape_inl_h */