From 21390884d137cad6113a4e8bf4d4755fb89d0f56 Mon Sep 17 00:00:00 2001 From: Nicolas Far Date: Fri, 17 May 2019 19:14:20 -0400 Subject: [PATCH] PHP to rust FFI example --- php-to-rust/Cargo.toml | 8 ++++++++ php-to-rust/Makefile | 14 ++++++++++++++ php-to-rust/README.md | 5 +++++ php-to-rust/src/ffi.php | 9 +++++++++ php-to-rust/src/lib.rs | 5 +++++ 5 files changed, 41 insertions(+) create mode 100644 php-to-rust/Cargo.toml create mode 100644 php-to-rust/Makefile create mode 100644 php-to-rust/README.md create mode 100644 php-to-rust/src/ffi.php create mode 100644 php-to-rust/src/lib.rs diff --git a/php-to-rust/Cargo.toml b/php-to-rust/Cargo.toml new file mode 100644 index 0000000..1ffedf0 --- /dev/null +++ b/php-to-rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "php-to-rust" +version = "0.1.0" +authors = ["Nicolas Far "] + +[lib] +name = "double_input" +crate-type = ["dylib"] diff --git a/php-to-rust/Makefile b/php-to-rust/Makefile new file mode 100644 index 0000000..294df52 --- /dev/null +++ b/php-to-rust/Makefile @@ -0,0 +1,14 @@ +ifeq ($(shell uname),Darwin) + EXT := dylib +else + EXT := so +endif + +all: target/debug/libdouble_input.$(EXT) + php src/ffi.php + +target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml + cargo build + +clean: + rm -rf target diff --git a/php-to-rust/README.md b/php-to-rust/README.md new file mode 100644 index 0000000..2ac9a99 --- /dev/null +++ b/php-to-rust/README.md @@ -0,0 +1,5 @@ +PHP 7.4 is needed + +At time of writing, that means [compiling from source](https://github.com/php/php-src#building-php-source-code). + +Make sure to pass --with-ffi as a parameter to ./configure \ No newline at end of file diff --git a/php-to-rust/src/ffi.php b/php-to-rust/src/ffi.php new file mode 100644 index 0000000..65c56fe --- /dev/null +++ b/php-to-rust/src/ffi.php @@ -0,0 +1,9 @@ +double_input(3)); +?> diff --git a/php-to-rust/src/lib.rs b/php-to-rust/src/lib.rs new file mode 100644 index 0000000..2819ff1 --- /dev/null +++ b/php-to-rust/src/lib.rs @@ -0,0 +1,5 @@ +#[no_mangle] +pub extern fn double_input(input: i32) -> i32 { + input * 2 +} +