Skip to content

Commit

Permalink
Use C-string literal for native dispatch (#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 authored Dec 6, 2024
1 parent e187ac9 commit df46801
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions crates/api-desc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Minor

- Use C-string literals to implement dispatch for native applets
- Implement `bytemuck::Pod` for generated `Params`

## 0.2.1
Expand Down
6 changes: 3 additions & 3 deletions crates/api-desc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::ffi::CString;
use std::io::Write;
use std::ops;

Expand Down Expand Up @@ -348,7 +349,7 @@ impl Fn {
let Fn { docs, name, link, params, result: _ } = self;
let name = format_ident!("{name}");
let env_link = format!("env_{link}");
let link0 = syn::LitByteStr::new(format!("{link}\0").as_bytes(), Span::call_site());
let ffi_link = syn::LitCStr::new(&CString::new(link.clone()).unwrap(), Span::call_site());
let doc = format!("Module of [`{name}`]({name}()).");
let params_doc = format!("Parameters of [`{name}`](super::{name}())");
let params: Vec<_> = params.iter().map(|x| x.wasm_rust()).collect();
Expand Down Expand Up @@ -379,8 +380,7 @@ impl Fn {
#[linkage = "weak"]
pub unsafe extern "C" fn #name(#fn_params) -> isize {
#let_params
let ffi_link = core::ffi::CStr::from_bytes_with_nul(#link0).unwrap().as_ptr();
crate::wasm::native::env_dispatch(ffi_link, ffi_params)
crate::wasm::native::env_dispatch(#ffi_link.as_ptr(), ffi_params)
}
}
}
Expand Down

0 comments on commit df46801

Please sign in to comment.