From 0169d7f1b82fdde3253437c4c23574468fb50c1e Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Thu, 19 Oct 2023 09:49:29 +0200 Subject: [PATCH] feat(embed): add embed features, add test example which run php inside it --- Cargo.toml | 1 + allowed_bindings.rs | 4 +++- src/types/string.rs | 29 +++++++++++++++++++++++++++++ src/wrapper.h | 1 + unix_build.rs | 7 +++++++ 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index eaccb4d20..385a5cc12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ zip = "0.6" [features] closure = [] +embed = [] [workspace] members = [ diff --git a/allowed_bindings.rs b/allowed_bindings.rs index a74d96552..a79ae2dac 100644 --- a/allowed_bindings.rs +++ b/allowed_bindings.rs @@ -256,5 +256,7 @@ bind! { tsrm_get_ls_cache, executor_globals_offset, zend_atomic_bool_store, - zend_interrupt_function + zend_interrupt_function, + php_embed_init, + php_embed_shutdown } diff --git a/src/types/string.rs b/src/types/string.rs index 94cc1f5ef..4279be83f 100644 --- a/src/types/string.rs +++ b/src/types/string.rs @@ -453,3 +453,32 @@ impl<'a> FromZval<'a> for &'a str { zval.str() } } + +#[cfg(test)] +#[cfg(feature = "embed")] +mod tests { + use crate::ffi::{php_embed_init, php_embed_shutdown}; + use crate::types::Zval; + use crate::zend::Function; + use std::ptr::null_mut; + + #[test] + fn test_string() { + unsafe { + php_embed_init(0, null_mut()); + } + + let func = Function::try_from_function("chr").expect("Failed to find chr function"); + let mut input = Zval::new(); + input.set_long(65); + let result = func + .try_call(vec![&input]) + .expect("Failed to call chr function"); + + assert_eq!(result.string().unwrap(), "A"); + + unsafe { + php_embed_shutdown(); + } + } +} diff --git a/src/wrapper.h b/src/wrapper.h index ed9dea629..e2409e01b 100644 --- a/src/wrapper.h +++ b/src/wrapper.h @@ -21,6 +21,7 @@ #include "zend_inheritance.h" #include "zend_interfaces.h" #include "zend_ini.h" +#include "sapi/embed/php_embed.h" zend_string *ext_php_rs_zend_string_init(const char *str, size_t len, bool persistent); void ext_php_rs_zend_string_release(zend_string *zs); diff --git a/unix_build.rs b/unix_build.rs index 9bdd446dd..8be6fd8be 100644 --- a/unix_build.rs +++ b/unix_build.rs @@ -55,4 +55,11 @@ impl<'a> PHPProvider<'a> for Provider { fn get_defines(&self) -> Result> { Ok(vec![]) } + + fn print_extra_link_args(&self) -> Result<()> { + #[cfg(feature = "embed")] + println!("cargo:rustc-link-lib=php"); + + Ok(()) + } }