Skip to content
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";
23 changes: 23 additions & 0 deletions examples/wasm/lib/Greet.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define EXPORT __attribute__ ((visibility ("default")))

EXPORT void *
_allocate(size_t size) {
return malloc(size);
}

EXPORT void
_deallocate(void* ptr) {
free(ptr);
}

EXPORT char *
_greet(const char *subject) {
int len = strlen(subject) + strlen("Hello, ") + 1;
char *greeting = malloc(len);
snprintf(greeting, len, "Hello, %s", subject);
return greeting;
}
31 changes: 31 additions & 0 deletions examples/wasm/lib/Greet.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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_offset = _allocate(length($subject) + 1);
strcpy( $memory->address + $input_offset, $subject );

my $output_offset = _greet($input_offset);
my $greeting = FFI::Platypus->new->cast('opaque', 'string', $memory->address + $output_offset);

_deallocate($input_offset);
_deallocate($output_offset);

return $greeting;
}

1;
Binary file added examples/wasm/lib/Greet.wasm
Binary file not shown.