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_List_inl_h
8 #define vm_List_inl_h
9 
10 #include "vm/List.h"
11 
12 #include "mozilla/Assertions.h"  // MOZ_ASSERT
13 
14 #include <stdint.h>  // uint32_t
15 
16 #include "js/RootingAPI.h"    // JS::Handle, JS::Rooted
17 #include "js/Value.h"         // JS::Value, JS::ObjectValue
18 #include "vm/JSContext.h"     // JSContext
19 #include "vm/NativeObject.h"  // js::NativeObject
20 
21 #include "vm/Compartment-inl.h"   // JS::Compartment::wrap
22 #include "vm/JSObject-inl.h"      // js::NewObjectWithGivenProto
23 #include "vm/NativeObject-inl.h"  // js::NativeObject::*
24 #include "vm/Realm-inl.h"         // js::AutoRealm
25 
create(JSContext * cx)26 inline /* static */ js::ListObject* js::ListObject::create(JSContext* cx) {
27   return NewObjectWithGivenProto<ListObject>(cx, nullptr);
28 }
29 
append(JSContext * cx,JS::Handle<JS::Value> value)30 inline bool js::ListObject::append(JSContext* cx, JS::Handle<JS::Value> value) {
31   uint32_t len = length();
32 
33   if (!ensureElements(cx, len + 1)) {
34     return false;
35   }
36 
37   ensureDenseInitializedLength(len, 1);
38   setDenseElement(len, value);
39   return true;
40 }
41 
appendValueAndSize(JSContext * cx,JS::Handle<JS::Value> value,double size)42 inline bool js::ListObject::appendValueAndSize(JSContext* cx,
43                                                JS::Handle<JS::Value> value,
44                                                double size) {
45   uint32_t len = length();
46 
47   if (!ensureElements(cx, len + 2)) {
48     return false;
49   }
50 
51   ensureDenseInitializedLength(len, 2);
52   setDenseElement(len, value);
53   setDenseElement(len + 1, JS::DoubleValue(size));
54   return true;
55 }
56 
popFirst(JSContext * cx)57 inline JS::Value js::ListObject::popFirst(JSContext* cx) {
58   uint32_t len = length();
59   MOZ_ASSERT(len > 0);
60 
61   JS::Value entry = get(0);
62   if (!tryShiftDenseElements(1)) {
63     moveDenseElements(0, 1, len - 1);
64     setDenseInitializedLength(len - 1);
65     shrinkElements(cx, len - 1);
66   }
67 
68   MOZ_ASSERT(length() == len - 1);
69   return entry;
70 }
71 
popFirstPair(JSContext * cx)72 inline void js::ListObject::popFirstPair(JSContext* cx) {
73   uint32_t len = length();
74   MOZ_ASSERT(len > 0);
75   MOZ_ASSERT((len % 2) == 0);
76 
77   if (!tryShiftDenseElements(2)) {
78     moveDenseElements(0, 2, len - 2);
79     setDenseInitializedLength(len - 2);
80     shrinkElements(cx, len - 2);
81   }
82 
83   MOZ_ASSERT(length() == len - 2);
84 }
85 
86 template <class T>
popFirstAs(JSContext * cx)87 inline T& js::ListObject::popFirstAs(JSContext* cx) {
88   return popFirst(cx).toObject().as<T>();
89 }
90 
91 namespace js {
92 
93 /**
94  * Stores an empty ListObject in the given fixed slot of |obj|.
95  */
StoreNewListInFixedSlot(JSContext * cx,JS::Handle<NativeObject * > obj,uint32_t slot)96 [[nodiscard]] inline bool StoreNewListInFixedSlot(JSContext* cx,
97                                                   JS::Handle<NativeObject*> obj,
98                                                   uint32_t slot) {
99   AutoRealm ar(cx, obj);
100   ListObject* list = ListObject::create(cx);
101   if (!list) {
102     return false;
103   }
104 
105   obj->setFixedSlot(slot, JS::ObjectValue(*list));
106   return true;
107 }
108 
109 /**
110  * Given an object |obj| whose fixed slot |slot| contains a ListObject, append
111  * |toAppend| to that list.
112  */
AppendToListInFixedSlot(JSContext * cx,JS::Handle<NativeObject * > obj,uint32_t slot,JS::Handle<JSObject * > toAppend)113 [[nodiscard]] inline bool AppendToListInFixedSlot(
114     JSContext* cx, JS::Handle<NativeObject*> obj, uint32_t slot,
115     JS::Handle<JSObject*> toAppend) {
116   JS::Rooted<ListObject*> list(
117       cx, &obj->getFixedSlot(slot).toObject().as<ListObject>());
118 
119   AutoRealm ar(cx, list);
120   JS::Rooted<JS::Value> val(cx, JS::ObjectValue(*toAppend));
121   if (!cx->compartment()->wrap(cx, &val)) {
122     return false;
123   }
124   return list->append(cx, val);
125 }
126 
127 }  // namespace js
128 
129 #endif  // vm_List_inl_h
130