Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/bindings/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn typescript_ffi_converter_name(typ: &impl AsType, askama_values: &dyn aska
Type::Float64 => "FfiConverterFloat64".into(),
Type::Boolean => "FfiConverterBool".into(),
Type::String => "FfiConverterString".into(),
Type::Bytes => "FfiConverterBytes".into(),
Type::Bytes => "FfiConverterArrayBuffer".into(),
Type::Timestamp => "FfiConverterTimestamp".into(),
Type::Duration => "FfiConverterDuration".into(),
Type::Enum { name, .. } | Type::Record { name, .. } | Type::Object { name, .. } => typescript_ffi_converter_struct_enum_object_name(&name, askama_values)?,
Expand All @@ -131,7 +131,7 @@ pub fn typescript_ffi_converter_name(typ: &impl AsType, askama_values: &dyn aska

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

pub fn typescript_ffi_converter_lower_with(target: String, askama_values: &dyn askama::Values, typ: &impl AsType) -> Result<String> {
Ok(match typ.as_type() {
Type::String | Type::Map { .. } | Type::Sequence { .. } | Type::Enum { .. } | Type::Record { .. } => {
Type::String | Type::Bytes | Type::Map { .. } | Type::Sequence { .. } | Type::Enum { .. } | Type::Record { .. } => {
format!("UniffiRustBufferValue.allocateWithBytes({}.lower({target})).toStruct()", typescript_ffi_converter_name(typ, askama_values)?)
},
Type::Optional { inner_type } => {
Expand Down
48 changes: 48 additions & 0 deletions src/bindings/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,51 @@ pub fn generate_node_bindings(
index_ts_file_contents,
})
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn bytes_roundtrip_uses_rust_buffer_serde_in_generated_bindings() {
let ci = ComponentInterface::from_webidl(
r#"
namespace test {
bytes round_trip(bytes input);
};
"#,
"crate_name",
).unwrap();

let bindings = generate_node_bindings(
&ci,
GenerateNodeBindingsOptions {
sys_ts_main_file_name: "test-sys",
node_ts_main_file_name: "test-node",
out_dirname_api: DirnameApi::Dirname,
out_lib_disable_auto_loading: false,
out_import_extension: ImportExtension::None,
out_node_version: "^18",
out_verbose_logs: false,
out_lib_path: LibPath::Omitted,
},
).unwrap();

assert!(
bindings.node_ts_file_contents.contains("export function roundTrip("),
"node.ts should render the bytes round-trip function from the UDL"
);
assert!(
bindings.node_ts_file_contents.contains(
"let inputArg = UniffiRustBufferValue.allocateWithBytes(FfiConverterArrayBuffer.lower(input)).toStruct();"
),
"node.ts should lower bytes arguments into a RustBuffer struct before the ffi call"
);
assert!(
bindings.node_ts_file_contents.contains(
"return FfiConverterArrayBuffer.lift(new UniffiRustBufferValue(returnValue).consumeIntoUint8Array());"
),
"node.ts should lift bytes return values from the RustBuffer returned by ffi"
);
}
}
Loading