Skip to content

Commit eb0ee3b

Browse files
committed
Use FfiConverterArrayBuffer not FfiConverterBytes for byte arrays
1 parent 9faabc2 commit eb0ee3b

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

src/bindings/filters.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn typescript_ffi_converter_name(typ: &impl AsType, askama_values: &dyn aska
108108
Type::Float64 => "FfiConverterFloat64".into(),
109109
Type::Boolean => "FfiConverterBool".into(),
110110
Type::String => "FfiConverterString".into(),
111-
Type::Bytes => "FfiConverterBytes".into(),
111+
Type::Bytes => "FfiConverterArrayBuffer".into(),
112112
Type::Timestamp => "FfiConverterTimestamp".into(),
113113
Type::Duration => "FfiConverterDuration".into(),
114114
Type::Enum { name, .. } | Type::Record { name, .. } | Type::Object { name, .. } => typescript_ffi_converter_struct_enum_object_name(&name, askama_values)?,
@@ -131,7 +131,7 @@ pub fn typescript_ffi_converter_name(typ: &impl AsType, askama_values: &dyn aska
131131

132132
pub fn typescript_ffi_converter_lift_with(target: String, askama_values: &dyn askama::Values, typ: &impl AsType) -> Result<String> {
133133
Ok(match typ.as_type() {
134-
Type::String | Type::Map { .. } | Type::Sequence { .. } | Type::Enum { .. } | Type::Record { .. } => {
134+
Type::String | Type::Bytes | Type::Map { .. } | Type::Sequence { .. } | Type::Enum { .. } | Type::Record { .. } => {
135135
format!("{}.lift(new UniffiRustBufferValue({target}).consumeIntoUint8Array())", typescript_ffi_converter_name(typ, askama_values)?)
136136
},
137137
Type::Optional { inner_type } => {
@@ -143,7 +143,7 @@ pub fn typescript_ffi_converter_lift_with(target: String, askama_values: &dyn as
143143

144144
pub fn typescript_ffi_converter_lower_with(target: String, askama_values: &dyn askama::Values, typ: &impl AsType) -> Result<String> {
145145
Ok(match typ.as_type() {
146-
Type::String | Type::Map { .. } | Type::Sequence { .. } | Type::Enum { .. } | Type::Record { .. } => {
146+
Type::String | Type::Bytes | Type::Map { .. } | Type::Sequence { .. } | Type::Enum { .. } | Type::Record { .. } => {
147147
format!("UniffiRustBufferValue.allocateWithBytes({}.lower({target})).toStruct()", typescript_ffi_converter_name(typ, askama_values)?)
148148
},
149149
Type::Optional { inner_type } => {

src/bindings/generator.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,51 @@ pub fn generate_node_bindings(
140140
index_ts_file_contents,
141141
})
142142
}
143+
144+
#[cfg(test)]
145+
mod test {
146+
use super::*;
147+
148+
#[test]
149+
fn bytes_roundtrip_uses_rust_buffer_serde_in_generated_bindings() {
150+
let ci = ComponentInterface::from_webidl(
151+
r#"
152+
namespace test {
153+
bytes round_trip(bytes input);
154+
};
155+
"#,
156+
"crate_name",
157+
).unwrap();
158+
159+
let bindings = generate_node_bindings(
160+
&ci,
161+
GenerateNodeBindingsOptions {
162+
sys_ts_main_file_name: "test-sys",
163+
node_ts_main_file_name: "test-node",
164+
out_dirname_api: DirnameApi::Dirname,
165+
out_lib_disable_auto_loading: false,
166+
out_import_extension: ImportExtension::None,
167+
out_node_version: "^18",
168+
out_verbose_logs: false,
169+
out_lib_path: LibPath::Omitted,
170+
},
171+
).unwrap();
172+
173+
assert!(
174+
bindings.node_ts_file_contents.contains("export function roundTrip("),
175+
"node.ts should render the bytes round-trip function from the UDL"
176+
);
177+
assert!(
178+
bindings.node_ts_file_contents.contains(
179+
"let inputArg = UniffiRustBufferValue.allocateWithBytes(FfiConverterArrayBuffer.lower(input)).toStruct();"
180+
),
181+
"node.ts should lower bytes arguments into a RustBuffer struct before the ffi call"
182+
);
183+
assert!(
184+
bindings.node_ts_file_contents.contains(
185+
"return FfiConverterArrayBuffer.lift(new UniffiRustBufferValue(returnValue).consumeIntoUint8Array());"
186+
),
187+
"node.ts should lift bytes return values from the RustBuffer returned by ffi"
188+
);
189+
}
190+
}

0 commit comments

Comments
 (0)