diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /js/src/vm/PlainObject-inl.h | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/src/vm/PlainObject-inl.h')
-rw-r--r-- | js/src/vm/PlainObject-inl.h | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/js/src/vm/PlainObject-inl.h b/js/src/vm/PlainObject-inl.h new file mode 100644 index 0000000000..f758915182 --- /dev/null +++ b/js/src/vm/PlainObject-inl.h @@ -0,0 +1,104 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * vim: set ts=8 sts=2 et sw=2 tw=80: + * 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/. */ + +#ifndef vm_PlainObject_inl_h +#define vm_PlainObject_inl_h + +#include "vm/PlainObject.h" + +#include "mozilla/Assertions.h" // MOZ_ASSERT, MOZ_ASSERT_IF +#include "mozilla/Attributes.h" // MOZ_ALWAYS_INLINE + +#include "gc/Allocator.h" // js::gc::InitialHeap +#include "js/RootingAPI.h" // JS::Handle, JS::Rooted, JS::MutableHandle +#include "js/Value.h" // JS::Value, JS_IS_CONSTRUCTING +#include "vm/JSFunction.h" // JSFunction +#include "vm/NativeObject.h" // js::NativeObject::create +#include "vm/ObjectGroup.h" // js::ObjectGroup, js::GenericObject, js::NewObjectKind +#include "vm/Shape.h" // js::Shape + +#include "gc/ObjectKind-inl.h" // js::gc::GetGCObjectKind +#include "vm/JSObject-inl.h" // js::GetInitialHeap, js::NewBuiltinClassInstance +#include "vm/NativeObject-inl.h" // js::NativeObject::{create,setLastProperty} + +/* static */ inline JS::Result<js::PlainObject*, JS::OOM> +js::PlainObject::createWithTemplate(JSContext* cx, + JS::Handle<PlainObject*> templateObject) { + JS::Rooted<ObjectGroup*> group(cx, templateObject->group()); + MOZ_ASSERT(group->clasp() == &PlainObject::class_); + + gc::InitialHeap heap = GetInitialHeap(GenericObject, group); + + JS::Rooted<Shape*> shape(cx, templateObject->lastProperty()); + + gc::AllocKind kind = gc::GetGCObjectKind(shape->numFixedSlots()); + MOZ_ASSERT(gc::CanChangeToBackgroundAllocKind(kind, shape->getObjectClass())); + kind = gc::ForegroundToBackgroundAllocKind(kind); + + return NativeObject::create(cx, kind, heap, shape, group) + .map([](NativeObject* obj) { return &obj->as<PlainObject>(); }); +} + +inline js::gc::AllocKind js::PlainObject::allocKindForTenure() const { + gc::AllocKind kind = gc::GetGCObjectFixedSlotsKind(numFixedSlots()); + MOZ_ASSERT(!gc::IsBackgroundFinalized(kind)); + MOZ_ASSERT(gc::CanChangeToBackgroundAllocKind(kind, getClass())); + return gc::ForegroundToBackgroundAllocKind(kind); +} + +namespace js { + +// Create an object based on a template object created for either a NewObject +// bytecode op or for a constructor call. +static inline PlainObject* CopyTemplateObject( + JSContext* cx, JS::Handle<PlainObject*> baseobj, + NewObjectKind newKind = GenericObject) { + MOZ_ASSERT(!baseobj->inDictionaryMode()); + + gc::AllocKind allocKind = + gc::GetGCObjectFixedSlotsKind(baseobj->numFixedSlots()); + allocKind = gc::ForegroundToBackgroundAllocKind(allocKind); + MOZ_ASSERT_IF(baseobj->isTenured(), + allocKind == baseobj->asTenured().getAllocKind()); + RootedObject proto(cx, baseobj->staticPrototype()); + JS::Rooted<PlainObject*> obj(cx, NewObjectWithGivenProtoAndKinds<PlainObject>( + cx, proto, allocKind, newKind)); + if (!obj) { + return nullptr; + } + + if (!obj->setLastProperty(cx, baseobj->lastProperty())) { + return nullptr; + } + + return obj; +} + +static MOZ_ALWAYS_INLINE bool CreateThis(JSContext* cx, + JS::Handle<JSFunction*> callee, + JS::Handle<JSObject*> newTarget, + NewObjectKind newKind, + JS::MutableHandle<JS::Value> thisv) { + if (callee->constructorNeedsUninitializedThis()) { + thisv.setMagic(JS_UNINITIALIZED_LEXICAL); + return true; + } + + MOZ_ASSERT(thisv.isMagic(JS_IS_CONSTRUCTING)); + + PlainObject* obj = CreateThisForFunction(cx, callee, newTarget, newKind); + if (!obj) { + return false; + } + + MOZ_ASSERT(obj->nonCCWRealm() == callee->realm()); + thisv.setObject(*obj); + return true; +} + +} // namespace js + +#endif // vm_PlainObject_inl_h |