|
5 | 5 |
|
6 | 6 | #include <array>
|
7 | 7 | #include <format>
|
| 8 | +#include <optional> |
8 | 9 | #include <span>
|
9 | 10 | #include <variant>
|
10 | 11 |
|
@@ -546,6 +547,144 @@ local.get $res
|
546 | 547 | destroy(ctx);
|
547 | 548 | }
|
548 | 549 |
|
| 550 | +TEST(component, value_enum) { |
| 551 | + static const auto check = [](const wasmtime_component_val_t &val, |
| 552 | + std::string_view text) { |
| 553 | + EXPECT_EQ(val.kind, WASMTIME_COMPONENT_ENUM); |
| 554 | + EXPECT_EQ( |
| 555 | + (std::string_view{val.of.enumeration.data, val.of.enumeration.size}), |
| 556 | + text); |
| 557 | + }; |
| 558 | + |
| 559 | + static const auto make = |
| 560 | + [](std::string_view text) -> wasmtime_component_val_t { |
| 561 | + auto ret = wasmtime_component_val_t{ |
| 562 | + .kind = WASMTIME_COMPONENT_ENUM, |
| 563 | + }; |
| 564 | + |
| 565 | + wasm_name_new(&ret.of.enumeration, text.size(), text.data()); |
| 566 | + |
| 567 | + return ret; |
| 568 | + }; |
| 569 | + |
| 570 | + auto ctx = create( |
| 571 | + R"((enum "aa" "bb"))", R"( |
| 572 | +(param $x i32) |
| 573 | +(result i32) |
| 574 | +local.get $x |
| 575 | +call $do |
| 576 | + )", |
| 577 | + "(param i32) (result i32)", |
| 578 | + +[](void *, wasmtime_context_t *, const wasmtime_component_val_t *args, |
| 579 | + size_t args_len, wasmtime_component_val_t *rets, |
| 580 | + size_t rets_len) -> wasmtime_error_t * { |
| 581 | + EXPECT_EQ(args_len, 1); |
| 582 | + check(args[0], "aa"); |
| 583 | + |
| 584 | + EXPECT_EQ(rets_len, 1); |
| 585 | + rets[0] = make("bb"); |
| 586 | + |
| 587 | + return nullptr; |
| 588 | + }); |
| 589 | + |
| 590 | + auto arg = make("aa"); |
| 591 | + auto res = wasmtime_component_val_t{}; |
| 592 | + |
| 593 | + auto err = |
| 594 | + wasmtime_component_func_call(&ctx.func, ctx.context, &arg, 1, &res, 1); |
| 595 | + CHECK_ERR(err); |
| 596 | + |
| 597 | + err = wasmtime_component_func_post_return(&ctx.func, ctx.context); |
| 598 | + CHECK_ERR(err); |
| 599 | + |
| 600 | + check(res, "bb"); |
| 601 | + |
| 602 | + wasmtime_component_val_delete(&arg); |
| 603 | + wasmtime_component_val_delete(&res); |
| 604 | + |
| 605 | + destroy(ctx); |
| 606 | +} |
| 607 | + |
| 608 | +TEST(component, value_option) { |
| 609 | + static const auto check = [](const wasmtime_component_val_t &val, |
| 610 | + std::optional<uint32_t> value) { |
| 611 | + EXPECT_EQ(val.kind, WASMTIME_COMPONENT_OPTION); |
| 612 | + |
| 613 | + if (value.has_value()) { |
| 614 | + EXPECT_NE(val.of.option, nullptr); |
| 615 | + EXPECT_EQ(val.of.option->kind, WASMTIME_COMPONENT_U32); |
| 616 | + EXPECT_EQ(val.of.option->of.u32, *value); |
| 617 | + } else { |
| 618 | + EXPECT_EQ(val.of.option, nullptr); |
| 619 | + } |
| 620 | + }; |
| 621 | + |
| 622 | + static const auto make = |
| 623 | + [](std::optional<uint32_t> value) -> wasmtime_component_val_t { |
| 624 | + auto ret = wasmtime_component_val_t{ |
| 625 | + .kind = WASMTIME_COMPONENT_OPTION, |
| 626 | + .of = {.option = nullptr}, |
| 627 | + }; |
| 628 | + |
| 629 | + if (value.has_value()) { |
| 630 | + ret.of.option = wasmtime_component_val_new(); |
| 631 | + *ret.of.option = wasmtime_component_val_t{ |
| 632 | + .kind = WASMTIME_COMPONENT_U32, |
| 633 | + .of = {.u32 = *value}, |
| 634 | + }; |
| 635 | + } |
| 636 | + |
| 637 | + return ret; |
| 638 | + }; |
| 639 | + |
| 640 | + auto ctx = create( |
| 641 | + R"((option u32))", R"( |
| 642 | +(param $x i32) |
| 643 | +(param $y i32) |
| 644 | +(result i32) |
| 645 | +(local $res i32) |
| 646 | +local.get $x |
| 647 | +local.get $y |
| 648 | +(call $realloc |
| 649 | + (i32.const 0) |
| 650 | + (i32.const 0) |
| 651 | + (i32.const 4) |
| 652 | + (i32.const 8)) |
| 653 | +local.tee $res |
| 654 | +call $do |
| 655 | +local.get $res |
| 656 | + )", |
| 657 | + "(param i32 i32 i32)", |
| 658 | + +[](void *, wasmtime_context_t *, const wasmtime_component_val_t *args, |
| 659 | + size_t args_len, wasmtime_component_val_t *rets, |
| 660 | + size_t rets_len) -> wasmtime_error_t * { |
| 661 | + EXPECT_EQ(args_len, 1); |
| 662 | + check(args[0], 123); |
| 663 | + |
| 664 | + EXPECT_EQ(rets_len, 1); |
| 665 | + rets[0] = make({}); |
| 666 | + |
| 667 | + return nullptr; |
| 668 | + }); |
| 669 | + |
| 670 | + auto arg = make(123); |
| 671 | + auto res = wasmtime_component_val_t{}; |
| 672 | + |
| 673 | + auto err = |
| 674 | + wasmtime_component_func_call(&ctx.func, ctx.context, &arg, 1, &res, 1); |
| 675 | + CHECK_ERR(err); |
| 676 | + |
| 677 | + err = wasmtime_component_func_post_return(&ctx.func, ctx.context); |
| 678 | + CHECK_ERR(err); |
| 679 | + |
| 680 | + check(res, {}); |
| 681 | + |
| 682 | + wasmtime_component_val_delete(&arg); |
| 683 | + wasmtime_component_val_delete(&res); |
| 684 | + |
| 685 | + destroy(ctx); |
| 686 | +} |
| 687 | + |
549 | 688 | TEST(component, value_list_inner) {
|
550 | 689 | {
|
551 | 690 | auto x = wasmtime_component_val_t{
|
|
0 commit comments