|
3 | 3 | import { BLOCK_MAXSIZE } from "./rt/common";
|
4 | 4 | import { Runtime } from "shared/runtime";
|
5 | 5 | import { COMPARATOR, SORT } from "./util/sort";
|
6 |
| -import { REVERSE } from "./util/bytes"; |
| 6 | +import { REVERSE, FILL } from "./util/bytes"; |
7 | 7 | import { joinBooleanArray, joinIntegerArray, joinFloatArray, joinStringArray, joinReferenceArray } from "./util/string";
|
8 | 8 | import { idof, isArray as builtin_isArray } from "./builtins";
|
9 | 9 | import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_ILLEGALGENTYPE, E_EMPTYARRAY, E_HOLEYARRAY } from "./util/error";
|
@@ -154,56 +154,12 @@ export class Array<T> {
|
154 | 154 | return value;
|
155 | 155 | }
|
156 | 156 |
|
157 |
| - fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): this { |
158 |
| - var ptr = this.dataStart; |
159 |
| - var len = this.length_; |
160 |
| - start = start < 0 ? max(len + start, 0) : min(start, len); |
161 |
| - end = end < 0 ? max(len + end, 0) : min(end, len); |
| 157 | + fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): Array<T> { |
162 | 158 | if (isManaged<T>()) {
|
163 |
| - for (; start < end; ++start) { |
164 |
| - store<usize>(ptr + (<usize>start << alignof<T>()), changetype<usize>(value)); |
165 |
| - __link(changetype<usize>(this), changetype<usize>(value), true); |
166 |
| - } |
167 |
| - } else if (sizeof<T>() == 1) { |
168 |
| - if (start < end) { |
169 |
| - memory.fill( |
170 |
| - ptr + <usize>start, |
171 |
| - u8(value), |
172 |
| - <usize>(end - start) |
173 |
| - ); |
174 |
| - } |
| 159 | + FILL<usize>(this.dataStart, this.length_, changetype<usize>(value), start, end); |
| 160 | + __link(changetype<usize>(this), changetype<usize>(value), false); |
175 | 161 | } else {
|
176 |
| - if (ASC_SHRINK_LEVEL <= 1) { |
177 |
| - if (isInteger<T>()) { |
178 |
| - // @ts-ignore |
179 |
| - if (value == <T>0 | value == <T>-1) { |
180 |
| - if (start < end) { |
181 |
| - memory.fill( |
182 |
| - ptr + (<usize>start << alignof<T>()), |
183 |
| - u8(value), |
184 |
| - <usize>(end - start) << alignof<T>() |
185 |
| - ); |
186 |
| - } |
187 |
| - return this; |
188 |
| - } |
189 |
| - } else if (isFloat<T>()) { |
190 |
| - // for floating non-negative zeros we can use fast memory.fill |
191 |
| - if ((sizeof<T>() == 4 && reinterpret<u32>(f32(value)) == 0) || |
192 |
| - (sizeof<T>() == 8 && reinterpret<u64>(f64(value)) == 0)) { |
193 |
| - if (start < end) { |
194 |
| - memory.fill( |
195 |
| - ptr + (<usize>start << alignof<T>()), |
196 |
| - 0, |
197 |
| - <usize>(end - start) << alignof<T>() |
198 |
| - ); |
199 |
| - } |
200 |
| - return this; |
201 |
| - } |
202 |
| - } |
203 |
| - } |
204 |
| - for (; start < end; ++start) { |
205 |
| - store<T>(ptr + (<usize>start << alignof<T>()), value); |
206 |
| - } |
| 162 | + FILL<T>(this.dataStart, this.length_, value, start, end); |
207 | 163 | }
|
208 | 164 | return this;
|
209 | 165 | }
|
@@ -295,7 +251,7 @@ export class Array<T> {
|
295 | 251 | return out;
|
296 | 252 | }
|
297 | 253 |
|
298 |
| - copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): this { |
| 254 | + copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Array<T> { |
299 | 255 | var ptr = this.dataStart;
|
300 | 256 | var len = this.length_;
|
301 | 257 |
|
@@ -466,12 +422,12 @@ export class Array<T> {
|
466 | 422 | return result;
|
467 | 423 | }
|
468 | 424 |
|
469 |
| - reverse(): this { |
| 425 | + reverse(): Array<T> { |
470 | 426 | REVERSE<T>(this.dataStart, this.length_);
|
471 | 427 | return this;
|
472 | 428 | }
|
473 | 429 |
|
474 |
| - sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): this { |
| 430 | + sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): Array<T> { |
475 | 431 | SORT<T>(this.dataStart, this.length_, comparator);
|
476 | 432 | return this;
|
477 | 433 | }
|
|
0 commit comments