Skip to content

Commit 656f741

Browse files
committed
Add support for component flags type
Flags are passed to/from Ruby as `Array<String>`, matching the Wasmtime's `Val::Flags`. Similar to enum types, the strings are not matched against valid flags given the rust layer does when lowering the flags.
1 parent be11320 commit 656f741

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

ext/src/ruby_api/component/convert.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use crate::{define_rb_intern, err, error, not_implemented};
55
use magnus::exception::type_error;
66
use magnus::rb_sys::AsRawValue;
77
use magnus::value::{IntoId, Lazy, ReprValue};
8-
use magnus::{prelude::*, value, Error, IntoValue, RArray, RClass, RHash, RString, Ruby, Value};
8+
use magnus::{
9+
prelude::*, try_convert, value, Error, IntoValue, RArray, RClass, RHash, RString, Ruby, Value,
10+
};
911
use wasmtime::component::{Type, Val};
1012

1113
define_rb_intern!(
@@ -89,7 +91,7 @@ pub(crate) fn component_val_to_rb(val: Val, _store: &StoreContextValue) -> Resul
8991
};
9092
result_class(&ruby).funcall(ruby_method, (ruby_argument,))
9193
}
92-
Val::Flags(_vec) => not_implemented!("Flags not implemented"),
94+
Val::Flags(vec) => Ok(vec.into_value()),
9395
Val::Resource(_resource_any) => not_implemented!("Resource not implemented"),
9496
}
9597
}
@@ -275,7 +277,7 @@ pub(crate) fn rb_to_component_val(
275277
}
276278
}
277279
}
278-
Type::Flags(_flags) => not_implemented!("Flags not implemented"),
280+
Type::Flags(_) => Vec::<String>::try_convert(value).map(Val::Flags),
279281
Type::Own(_resource_type) => not_implemented!("Resource not implemented"),
280282
Type::Borrow(_resource_type) => not_implemented!("Resource not implemented"),
281283
}

spec/unit/component/convert_spec.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ module Component
3232
["enum", "l"],
3333
["option", 0, nil], # option<u32>
3434
["result", Result.ok(1), Result.error(2)], # result<u32, u32>
35-
["result-unit", Result.ok(nil), Result.error(nil)]
36-
# TODO flags
35+
["result-unit", Result.ok(nil), Result.error(nil)],
36+
["flags", [], ["read"], ["read", "write", "exec"]]
3737
].each do |type, *values|
3838
values.each do |v|
3939
it "#{type} #{v.inspect}" do
@@ -89,7 +89,10 @@ module Component
8989
["enum", "no", /enum variant name `no` is not valid/],
9090
["result", nil, /undefined method `ok\?/],
9191
["result-unit", Result.ok(""), /expected nil for result<_, E> ok branch/],
92-
["result-unit", Result.error(""), /expected nil for result<O, _> error branch/]
92+
["result-unit", Result.error(""), /expected nil for result<O, _> error branch/],
93+
["flags", ["no"], /unknown flag: `no`/],
94+
["flags", [1], /no implicit conversion of Integer into String/],
95+
["flags", 1, /no implicit conversion of Integer into Array/]
9396
].each do |type, value, klass, msg|
9497
it "fails on #{type} #{value.inspect}" do
9598
expect { instance.invoke("id-#{type}", value) }.to raise_error(klass, msg)

0 commit comments

Comments
 (0)