Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Based on https://github.com/wasmerio/php-ext-wasm/blob/master/examples/greet.rs
- //Javascript that uses this: https://pastebin.com/Pz9ZMgEm
- #![no_std]
- use core::panic::PanicInfo;
- //RLS complains about this being a "duplicate lang item",
- //perhaps because my RLS is stable and I'm compiling with nightly?
- #[panic_handler]
- fn panic(_info: &PanicInfo) -> ! {
- loop {}
- }
- const SIZE: usize=20;
- static mut IN_BUF : [u8;SIZE]=[0;SIZE];
- static mut OUT_BUF: [u8;SIZE]=[0;SIZE];
- #[no_mangle]
- pub fn get_input_ptr() -> *const u8 {
- unsafe {IN_BUF}.as_ptr()
- }
- #[no_mangle]
- pub fn greet(input_ptr: *const u8) -> *const u8 {
- //input_ptr and get_input_ptr() should both point to IN_BUF,
- //so why does only the first version work properly?
- //JS logs "Hello, ใในใ!"
- let input: &[u8]=unsafe {core::slice::from_raw_parts(input_ptr,SIZE)};
- //JS logs "Hello, "
- // let input: &[u8]=unsafe {core::slice::from_raw_parts(get_input_ptr(),SIZE)};
- //JS logs "Hello, !"
- // let input: &[u8]=unsafe {&IN_BUF};
- let output: &mut [u8]=unsafe {&mut OUT_BUF};
- b"Hello, ".iter()
- .chain(input.iter().take_while(|&&chr| chr!=0))
- .chain(b"!\0")
- .enumerate().for_each(|(i,&chr)| {
- let elem=unsafe {output.get_unchecked_mut(i)};
- *elem=chr;
- });
- output.as_ptr()
- //JS logs input instead?!
- // unsafe {OUT_BUF}.as_ptr()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement