// Copyright 2019 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. namespace torque_internal { // TODO(gsps): Synthesize SizeOf in the compiler macro SizeOf(): constexpr int31; SizeOf(): constexpr int31 { return kTaggedSize; } SizeOf(): constexpr int31 { return kDoubleSize; } // Unsafe is a marker that we require to be passed when calling internal APIs // that might lead to unsoundness when used incorrectly. Unsafe markers should // therefore not be instantiated anywhere outside of this namespace. struct Unsafe {} struct Reference { const object: HeapObject; const offset: intptr; unsafeMarker: Unsafe; } macro UnsafeNewReference(object: HeapObject, offset: intptr):&T { return Reference{ object: object, offset: offset, unsafeMarker: Unsafe {} }; } struct Slice { TryAtIndex(index: intptr):&T labels OutOfBounds { if (Convert(index) < Convert(this.length)) { return UnsafeNewReference( this.object, this.offset + index * SizeOf()); } else { goto OutOfBounds; } } AtIndex(index: intptr):&T { return this.TryAtIndex(index) otherwise unreachable; } AtIndex(index: constexpr int31):&T { const i: intptr = Convert(index); return this.TryAtIndex(i) otherwise unreachable; } AtIndex(index: Smi):&T { const i: intptr = Convert(index); return this.TryAtIndex(i) otherwise unreachable; } Iterator(): SliceIterator { const end = this.offset + this.length * SizeOf(); return SliceIterator{ object: this.object, start: this.offset, end: end, unsafeMarker: Unsafe {} }; } const object: HeapObject; const offset: intptr; const length: intptr; unsafeMarker: Unsafe; } macro UnsafeNewSlice( object: HeapObject, offset: intptr, length: intptr): Slice { return Slice{ object: object, offset: offset, length: length, unsafeMarker: Unsafe {} }; } struct SliceIterator { Empty(): bool { return this.start == this.end; } Next():&T labels NoMore { if (this.Empty()) { goto NoMore; } else { const result = UnsafeNewReference(this.object, this.start); this.start += SizeOf(); return result; } } object: HeapObject; start: intptr; end: intptr; unsafeMarker: Unsafe; } } // namespace torque_internal