Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JS slices #372

Merged
merged 4 commits into from
Nov 28, 2023
Merged
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
100 changes: 54 additions & 46 deletions example/js/lib/api/diplomat-runtime.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions feature_tests/js/api/Float64Vec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions feature_tests/js/api/Foo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 54 additions & 46 deletions feature_tests/js/api/diplomat-runtime.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions feature_tests/js/docs/source/slices_ffi.rst

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions tool/src/js/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,20 @@ pub fn gen_value_js_to_rust<'env>(
// clean this up.
if let ast::TypeName::PrimitiveSlice(.., prim) = typ {
pre_logic.push(format!(
"const {param_name_buf} = diplomatRuntime.DiplomatBuf.slice(wasm, {param_name}, {align});",
align = layout::primitive_size_alignment(*prim).align()
"const {param_name_buf} = diplomatRuntime.DiplomatBuf.slice(wasm, {param_name}, {rust_type:?});",
rust_type = prim.to_string(),
));
} else if matches!(
typ,
ast::TypeName::StrReference(_, ast::StringEncoding::UnvalidatedUtf16)
) {
} else if let ast::TypeName::StrReference(_, encoding) = typ {
pre_logic.push(format!(
"const {param_name_buf} = diplomatRuntime.DiplomatBuf.str16(wasm, {param_name}, 2);",
"const {param_name_buf} = diplomatRuntime.DiplomatBuf.{}(wasm, {param_name});",
match encoding {
ast::StringEncoding::UnvalidatedUtf8 => "str8",
ast::StringEncoding::UnvalidatedUtf16 => "str16",
_ => unreachable!("unknown AST/HIR variant"),
}
));
} else {
pre_logic.push(format!(
"const {param_name_buf} = diplomatRuntime.DiplomatBuf.str8(wasm, {param_name});"
));
unreachable!("unknown AST/HIR variant");
}

invocation_params.push(format!("{param_name_buf}.ptr"));
Expand All @@ -144,6 +144,7 @@ pub fn gen_value_js_to_rust<'env>(
.as_named()
.and_then(|current| borrowed_current_to_root.get(current))
{
post_logic.push(format!("{param_name_buf}.garbageCollect();"));
entries.entry(named).or_default().push(param_name_buf);
} else if lifetime == &ast::Lifetime::Static {
post_logic.push(format!("{param_name_buf}.leak();"));
Expand Down
13 changes: 0 additions & 13 deletions tool/src/js/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,6 @@ pub fn gen_method_docs<W: fmt::Write>(
writeln!(method_indented)?;
}

for p in method
.params
.iter()
.filter(|p| matches!(p.ty, ast::TypeName::PrimitiveSlice(..)))
{
writeln!(
method_indented,
"- Note: ``{}`` should be an ArrayBuffer or TypedArray corresponding to the slice type expected by Rust.",
p.name
)?;
writeln!(method_indented)?;
}

let static_borrows = method.borrowed_params();
let static_borrows = static_borrows.static_names().collect::<Vec<_>>();
if !static_borrows.is_empty() {
Expand Down
Loading
Loading