Skip to content

Commit 783dd32

Browse files
jtennerdcodeIO
authored andcommitted
Implement TypedArray#forEach (AssemblyScript#530)
1 parent 208dc2f commit 783dd32

File tree

6 files changed

+3643
-15
lines changed

6 files changed

+3643
-15
lines changed

std/assembly/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,9 @@ declare abstract class TypedArray<T> implements ArrayBufferView<T> {
11361136
/** The findIndex() method returns an index in the typed array, if an element in the typed array satisfies the provided testing function. Otherwise -1 is returned. See also the find() [not implemented] method, which returns the value of a found element in the typed array instead of its index. */
11371137
findIndex(callbackfn: (value: T, index: i32, self: this) => bool): i32;
11381138
/** The every() method tests whether all elements in the typed array pass the test implemented by the provided function. This method has the same algorithm as Array.prototype.every(). */
1139-
every(callbackfn: (value: T, index: i32, self: this) => bool): i32;
1139+
every(callbackfn: (value: T, index: i32, self: this) => bool): bool;
1140+
/** The forEach() method executes a provided function once per array element. This method has the same algorithm as Array.prototype.forEach().*/
1141+
forEach(callbackfn: (value: T, index: i32, self: this) => void): void;
11401142
}
11411143

11421144
/** An array of twos-complement 8-bit signed integers. */

std/assembly/internal/typedarray.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,16 @@ export function EVERY<TArray extends TypedArray<T>, T>(
231231
}
232232
return true;
233233
}
234+
235+
@inline
236+
export function FOREACH<TArray extends TypedArray<T>, T>(
237+
array: TArray,
238+
callbackfn: (value: T, index: i32, array: TArray) => void,
239+
): void {
240+
var length = array.length;
241+
var buffer = array.buffer;
242+
var byteOffset = array.byteOffset;
243+
for (let i = 0; i < length; i++) {
244+
callbackfn(LOAD<T>(buffer, i, byteOffset), i, array);
245+
}
246+
}

std/assembly/typedarray.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
FIND_INDEX,
1010
SOME,
1111
EVERY,
12+
FOREACH,
1213
} from "./internal/typedarray";
1314

1415
import {
@@ -63,6 +64,10 @@ export class Int8Array extends TypedArray<i8> {
6364
every(callbackfn: (value: i8, index: i32, self: Int8Array) => bool): bool {
6465
return EVERY<Int8Array, i8>(this, callbackfn);
6566
}
67+
68+
forEach(callbackfn: (value: i8, index: i32, self: Int8Array) => void): void {
69+
FOREACH<Int8Array, i8>(this, callbackfn);
70+
}
6671
}
6772

6873
export class Uint8Array extends TypedArray<u8> {
@@ -109,6 +114,10 @@ export class Uint8Array extends TypedArray<u8> {
109114
every(callbackfn: (value: u8, index: i32, self: Uint8Array) => bool): bool {
110115
return EVERY<Uint8Array, u8>(this, callbackfn);
111116
}
117+
118+
forEach(callbackfn: (value: u8, index: i32, self: Uint8Array) => void): void {
119+
FOREACH<Uint8Array, u8>(this, callbackfn);
120+
}
112121
}
113122

114123
export class Uint8ClampedArray extends Uint8Array {
@@ -165,6 +174,10 @@ export class Uint8ClampedArray extends Uint8Array {
165174
every(callbackfn: (value: u8, index: i32, self: Uint8ClampedArray) => bool): bool {
166175
return EVERY<Uint8ClampedArray, u8>(this, callbackfn);
167176
}
177+
178+
forEach(callbackfn: (value: u8, index: i32, self: Uint8ClampedArray) => void): void {
179+
FOREACH<Uint8ClampedArray, u8>(this, callbackfn);
180+
}
168181
}
169182

170183
export class Int16Array extends TypedArray<i16> {
@@ -211,6 +224,10 @@ export class Int16Array extends TypedArray<i16> {
211224
every(callbackfn: (value: i16, index: i32, self: Int16Array) => bool): bool {
212225
return EVERY<Int16Array, i16>(this, callbackfn);
213226
}
227+
228+
forEach(callbackfn: (value: i16, index: i32, self: Int16Array) => void): void {
229+
FOREACH<Int16Array, i16>(this, callbackfn);
230+
}
214231
}
215232

216233
export class Uint16Array extends TypedArray<u16> {
@@ -257,6 +274,10 @@ export class Uint16Array extends TypedArray<u16> {
257274
every(callbackfn: (value: u16, index: i32, self: Uint16Array) => bool): bool {
258275
return EVERY<Uint16Array, u16>(this, callbackfn);
259276
}
277+
278+
forEach(callbackfn: (value: u16, index: i32, self: Uint16Array) => void): void {
279+
FOREACH<Uint16Array, u16>(this, callbackfn);
280+
}
260281
}
261282

262283
export class Int32Array extends TypedArray<i32> {
@@ -303,6 +324,10 @@ export class Int32Array extends TypedArray<i32> {
303324
every(callbackfn: (value: i32, index: i32, self: Int32Array) => bool): bool {
304325
return EVERY<Int32Array, i32>(this, callbackfn);
305326
}
327+
328+
forEach(callbackfn: (value: i32, index: i32, self: Int32Array) => void): void {
329+
FOREACH<Int32Array, i32>(this, callbackfn);
330+
}
306331
}
307332

308333
export class Uint32Array extends TypedArray<u32> {
@@ -349,6 +374,10 @@ export class Uint32Array extends TypedArray<u32> {
349374
every(callbackfn: (value: u32, index: i32, self: Uint32Array) => bool): bool {
350375
return EVERY<Uint32Array, u32>(this, callbackfn);
351376
}
377+
378+
forEach(callbackfn: (value: u32, index: i32, self: Uint32Array) => void): void {
379+
FOREACH<Uint32Array, u32>(this, callbackfn);
380+
}
352381
}
353382

354383
export class Int64Array extends TypedArray<i64> {
@@ -395,6 +424,10 @@ export class Int64Array extends TypedArray<i64> {
395424
every(callbackfn: (value: i64, index: i32, self: Int64Array) => bool): bool {
396425
return EVERY<Int64Array, i64>(this, callbackfn);
397426
}
427+
428+
forEach(callbackfn: (value: i64, index: i32, self: Int64Array) => void): void {
429+
FOREACH<Int64Array, i64>(this, callbackfn);
430+
}
398431
}
399432

400433
export class Uint64Array extends TypedArray<u64> {
@@ -441,6 +474,10 @@ export class Uint64Array extends TypedArray<u64> {
441474
every(callbackfn: (value: u64, index: i32, self: Uint64Array) => bool): bool {
442475
return EVERY<Uint64Array, u64>(this, callbackfn);
443476
}
477+
478+
forEach(callbackfn: (value: u64, index: i32, self: Uint64Array) => void): void {
479+
FOREACH<Uint64Array, u64>(this, callbackfn);
480+
}
444481
}
445482

446483
export class Float32Array extends TypedArray<f32> {
@@ -487,6 +524,10 @@ export class Float32Array extends TypedArray<f32> {
487524
every(callbackfn: (value: f32, index: i32, self: Float32Array) => bool): bool {
488525
return EVERY<Float32Array, f32>(this, callbackfn);
489526
}
527+
528+
forEach(callbackfn: (value: f32, index: i32, self: Float32Array) => void): void {
529+
FOREACH<Float32Array, f32>(this, callbackfn);
530+
}
490531
}
491532

492533
export class Float64Array extends TypedArray<f64> {
@@ -533,4 +574,8 @@ export class Float64Array extends TypedArray<f64> {
533574
every(callbackfn: (value: f64, index: i32, self: Float64Array) => bool): bool {
534575
return EVERY<Float64Array, f64>(this, callbackfn);
535576
}
577+
578+
forEach(callbackfn: (value: f64, index: i32, self: Float64Array) => void): void {
579+
FOREACH<Float64Array, f64>(this, callbackfn);
580+
}
536581
}

0 commit comments

Comments
 (0)