@@ -104,6 +104,54 @@ const Primitives = struct {
104104 pub fn _echoStringZ (_ : * const Primitives , a : [:0 ]const u8 ) []const u8 {
105105 return a ;
106106 }
107+
108+ pub fn _int8 (_ : * const Primitives , arr : []i8 ) void {
109+ for (arr ) | * a | {
110+ a .* -= @intCast (arr .len );
111+ }
112+ }
113+
114+ pub fn _uint8 (_ : * const Primitives , arr : []u8 ) void {
115+ for (arr ) | * a | {
116+ a .* += @intCast (arr .len );
117+ }
118+ }
119+
120+ pub fn _int16 (_ : * const Primitives , arr : []i16 ) void {
121+ for (arr ) | * a | {
122+ a .* -= @intCast (arr .len );
123+ }
124+ }
125+
126+ pub fn _uint16 (_ : * const Primitives , arr : []u16 ) void {
127+ for (arr ) | * a | {
128+ a .* += @intCast (arr .len );
129+ }
130+ }
131+
132+ pub fn _int32 (_ : * const Primitives , arr : []i32 ) void {
133+ for (arr ) | * a | {
134+ a .* -= @intCast (arr .len );
135+ }
136+ }
137+
138+ pub fn _uint32 (_ : * const Primitives , arr : []u32 ) void {
139+ for (arr ) | * a | {
140+ a .* += @intCast (arr .len );
141+ }
142+ }
143+
144+ pub fn _int64 (_ : * const Primitives , arr : []i64 ) void {
145+ for (arr ) | * a | {
146+ a .* -= @intCast (arr .len );
147+ }
148+ }
149+
150+ pub fn _uint64 (_ : * const Primitives , arr : []u64 ) void {
151+ for (arr ) | * a | {
152+ a .* += @intCast (arr .len );
153+ }
154+ }
107155};
108156
109157const testing = @import ("testing.zig" );
@@ -185,4 +233,55 @@ test "JS: primitive types" {
185233 .{ "p.echoString('over 9000!');" , "over 9000!" },
186234 .{ "p.echoStringZ('Teg');" , "Teg" },
187235 }, .{});
236+
237+ // typed arrays
238+ try runner .testCases (&.{
239+ .{ "let arr_i8 = new Int8Array([-10, -20, -30]);" , "undefined" },
240+ .{ "p.int8(arr_i8)" , "undefined" },
241+ .{ "arr_i8;" , "-13,-23,-33" },
242+
243+ .{ "let arr_u8 = new Uint8Array([10, 20, 30]);" , "undefined" },
244+ .{ "p.uint8(arr_u8)" , "undefined" },
245+ .{ "arr_u8;" , "13,23,33" },
246+
247+ .{ "let arr_i16 = new Int16Array([-1000, -2000, -3000]);" , "undefined" },
248+ .{ "p.int16(arr_i16)" , "undefined" },
249+ .{ "arr_i16;" , "-1003,-2003,-3003" },
250+
251+ .{ "let arr_u16 = new Uint16Array([1000, 2000, 3000]);" , "undefined" },
252+ .{ "p.uint16(arr_u16)" , "undefined" },
253+ .{ "arr_u16;" , "1003,2003,3003" },
254+
255+ .{ "let arr_i32 = new Int32Array([-1000000, -2000000, -3000000]);" , "undefined" },
256+ .{ "p.int32(arr_i32)" , "undefined" },
257+ .{ "arr_i32;" , "-1000003,-2000003,-3000003" },
258+
259+ .{ "let arr_u32 = new Uint32Array([1000000, 2000000, 3000000]);" , "undefined" },
260+ .{ "p.uint32(arr_u32)" , "undefined" },
261+ .{ "arr_u32;" , "1000003,2000003,3000003" },
262+
263+ .{ "let arr_i64 = new BigInt64Array([-1000000000n, -2000000000n, -3000000000n]);" , "undefined" },
264+ .{ "p.int64(arr_i64)" , "undefined" },
265+ .{ "arr_i64;" , "-1000000003,-2000000003,-3000000003" },
266+
267+ .{ "let arr_u64 = new BigUint64Array([1000000000n, 2000000000n, 3000000000n]);" , "undefined" },
268+ .{ "p.uint64(arr_u64)" , "undefined" },
269+ .{ "arr_u64;" , "1000000003,2000000003,3000000003" },
270+
271+ .{ "try { p.int8(arr_u8) } catch(e) { e instanceof TypeError; }" , "true" },
272+ .{ "try { p.intu8(arr_i8) } catch(e) { e instanceof TypeError; }" , "true" },
273+ .{ "try { p.intu8(arr_u32) } catch(e) { e instanceof TypeError; }" , "true" },
274+
275+ .{ "try { p.int16(arr_u8) } catch(e) { e instanceof TypeError; }" , "true" },
276+ .{ "try { p.intu16(arr_i16) } catch(e) { e instanceof TypeError; }" , "true" },
277+ .{ "try { p.int16(arr_i64) } catch(e) { e instanceof TypeError; }" , "true" },
278+
279+ .{ "try { p.int32(arr_u32) } catch(e) { e instanceof TypeError; }" , "true" },
280+ .{ "try { p.intu32(arr_i32) } catch(e) { e instanceof TypeError; }" , "true" },
281+ .{ "try { p.intu32(arr_u32) } catch(e) { e instanceof TypeError; }" , "true" },
282+
283+ .{ "try { p.int64(arr_u64) } catch(e) { e instanceof TypeError; }" , "true" },
284+ .{ "try { p.intu64(arr_i64) } catch(e) { e instanceof TypeError; }" , "true" },
285+ .{ "try { p.intu64(arr_u32) } catch(e) { e instanceof TypeError; }" , "true" },
286+ }, .{});
188287}
0 commit comments