List-inl.h (1597B)
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/Value.h" // JS::Value 17 #include "vm/JSContext.h" // JSContext 18 #include "vm/NativeObject.h" // js::NativeObject 19 20 #include "vm/JSObject-inl.h" // js::NewObjectWithGivenProto 21 #include "vm/NativeObject-inl.h" // js::NativeObject::* 22 23 inline /* static */ js::ListObject* js::ListObject::create(JSContext* cx) { 24 return NewObjectWithGivenProto<ListObject>(cx, nullptr); 25 } 26 27 inline bool js::ListObject::append(JSContext* cx, Value value) { 28 uint32_t len = length(); 29 30 if (!ensureElements(cx, len + 1)) { 31 return false; 32 } 33 34 ensureDenseInitializedLength(len, 1); 35 setDenseElement(len, value); 36 return true; 37 } 38 39 inline JS::Value js::ListObject::popFirst(JSContext* cx) { 40 uint32_t len = length(); 41 MOZ_ASSERT(len > 0); 42 43 JS::Value entry = get(0); 44 if (!tryShiftDenseElements(1)) { 45 moveDenseElements(0, 1, len - 1); 46 setDenseInitializedLength(len - 1); 47 shrinkElements(cx, len - 1); 48 } 49 50 MOZ_ASSERT(length() == len - 1); 51 return entry; 52 } 53 54 template <class T> 55 inline T& js::ListObject::popFirstAs(JSContext* cx) { 56 return popFirst(cx).toObject().as<T>(); 57 } 58 59 #endif // vm_List_inl_h