Skip to content

add greet exmaple: passing strings back and forth #55

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions examples/wasm/greet.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use strict;
use warnings;
use Path::Tiny qw( path );
use lib path(__FILE__)->parent->child('lib')->stringify;
use Greet;

print greet("Perl!"), "\n";
33 changes: 33 additions & 0 deletions examples/wasm/lib/Greet.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package Greet;

use strict;
use warnings;
use FFI::Platypus;
use FFI::Platypus::Memory qw( strcpy );
use base qw( Exporter );
use Wasm
-api => 0,
-self
;

our @EXPORT = qw( greet );

sub greet
{
my($subject) = @_;

my $input_size = do { use bytes; length($subject)+1 };
my $input_offset = _allocate($input_size);
strcpy( $memory->address + $input_offset, $subject );

my $output_offset = _greet($input_offset);
my $greeting = FFI::Platypus->new->cast('opaque', 'string', $memory->address + $output_offset);
my $output_size = do { use bytes; length($greeting)+1 };

_deallocate($input_offset, $input_size);
_deallocate($output_offset, $output_size);

return $greeting;
}

1;
34 changes: 34 additions & 0 deletions examples/wasm/lib/Greet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* rustc --target wasm32-unknown-unknown -O --crate-type=cdylib Greet.rs -o Greet.wasm
* chmod -x Greet.wasm
*/

use std::ffi::{CStr, CString};
use std::mem;
use std::os::raw::{c_char, c_void};

#[no_mangle]
pub extern fn _allocate(size: usize) -> *mut c_void {
let mut buffer = Vec::with_capacity(size);
let pointer = buffer.as_mut_ptr();
mem::forget(buffer);

pointer as *mut c_void
}

#[no_mangle]
pub extern fn _deallocate(pointer: *mut c_void, capacity: usize) {
unsafe {
let _ = Vec::from_raw_parts(pointer, 0, capacity);
}
}

#[no_mangle]
pub extern fn _greet(subject: *mut c_char) -> *mut c_char {
let subject = unsafe { CStr::from_ptr(subject).to_bytes().to_vec() };
let mut output = b"Hello, ".to_vec();
output.extend(&subject);
output.extend(&[b'!']);

unsafe { CString::from_vec_unchecked(output) }.into_raw()
}