authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2021-05-28 11:52:08+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2021-05-28 12:58:17+02:00
log5cbe930e36d9ccd6ac90d15a2354aced5d54f0dc
treea3141de0bdab53bffb2a19f0a1b20a5c9b556c09
parent8a81dfc9997c4ae8a109071be9754d3bb52a78f0
signaturelock-open Commit is signed but in an unrecognized format.

wasm: Add stage2 tests for error unions


2 files changed, 68 insertions(+), 2 deletions(-)

src/codegen/wasm.zig+4-2
......@@ -692,13 +692,15 @@ pub const Context = struct {
692692 .Struct => return self.fail(.{ .node_offset = 0 }, "TODO: Implement struct as return type for wasm", .{}),
693693 .Optional => return self.fail(.{ .node_offset = 0 }, "TODO: Implement optionals as return type for wasm", .{}),
694694 .ErrorUnion => {
695 try leb.writeULEB128(writer, @as(u32, 2));
696695 const val_type = try self.genValtype(
697696 .{ .node_offset = 0 },
698697 return_type.errorUnionChild(),
699698 );
700 try writer.writeByte(val_type);
699
700 // write down the amount of return values
701 try leb.writeULEB128(writer, @as(u32, 2));
701702 try writer.writeByte(wasm.valtype(.i32)); // error code is always an i32 integer.
703 try writer.writeByte(val_type);
702704 },
703705 else => |ret_type| {
704706 try leb.writeULEB128(writer, @as(u32, 1));
test/stage2/wasm.zig+64
......@@ -535,4 +535,68 @@ pub fn addCases(ctx: *TestContext) !void {
535535 \\}
536536 , "2\n");
537537 }
538
539 {
540 var case = ctx.exe("wasm error unions", wasi);
541
542 case.addCompareOutput(
543 \\pub export fn _start() void {
544 \\ var e1 = error.Foo;
545 \\ var e2 = error.Bar;
546 \\ assert(e1 != e2);
547 \\ assert(e1 == error.Foo);
548 \\ assert(e2 == error.Bar);
549 \\}
550 \\
551 \\fn assert(b: bool) void {
552 \\ if (!b) unreachable;
553 \\}
554 , "");
555
556 case.addCompareOutput(
557 \\pub export fn _start() u32 {
558 \\ var e: anyerror!u32 = 5;
559 \\ const i = e catch 10;
560 \\ return i;
561 \\}
562 , "5\n");
563
564 case.addCompareOutput(
565 \\pub export fn _start() u32 {
566 \\ var e: anyerror!u32 = error.Foo;
567 \\ const i = e catch 10;
568 \\ return i;
569 \\}
570 , "10\n");
571
572 case.addCompareOutput(
573 \\pub export fn _start() u32 {
574 \\ var e = foo();
575 \\ const i = e catch 69;
576 \\ return i;
577 \\}
578 \\
579 \\fn foo() anyerror!u32 {
580 \\ return 5;
581 \\}
582 , "5\n");
583 }
584
585 {
586 // TODO implement Type equality comparison of error unions in SEMA
587 // before we can incrementally compile functions with an error union as return type
588 var case = ctx.exe("wasm error union part 2", wasi);
589
590 case.addCompareOutput(
591 \\pub export fn _start() u32 {
592 \\ var e = foo();
593 \\ const i = e catch 69;
594 \\ return i;
595 \\}
596 \\
597 \\fn foo() anyerror!u32 {
598 \\ return error.Bruh;
599 \\}
600 , "69\n");
601 }
538602}