Skip to content

Commit a9e1dad

Browse files
committed
Add bindings for php_printf
1 parent 16c456d commit a9e1dad

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

allowed_bindings.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,6 @@ bind! {
192192
zend_objects_store_del,
193193
gc_possible_root,
194194
ZEND_ACC_NOT_SERIALIZABLE,
195-
executor_globals
195+
executor_globals,
196+
php_printf
196197
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub mod prelude {
4444
pub use crate::php_function;
4545
pub use crate::php_impl;
4646
pub use crate::php_module;
47+
pub use crate::php_print;
48+
pub use crate::php_println;
4749
pub use crate::php_startup;
4850
pub use crate::types::ZendCallable;
4951
pub use crate::ZvalConvert;

src/macros.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,5 +362,51 @@ macro_rules! try_from_zval {
362362
};
363363
}
364364

365+
/// Prints to the PHP standard output, without a newline.
366+
///
367+
/// Acts exactly the same as the built-in [`print`] macro.
368+
///
369+
/// # Panics
370+
///
371+
/// Panics if the generated string could not be converted to a `CString` due to
372+
/// `NUL` characters.
373+
#[macro_export]
374+
macro_rules! php_print {
375+
($arg: tt) => {{
376+
$crate::zend::printf($arg).expect("Failed to print to PHP stdout");
377+
}};
378+
379+
($($arg: tt) *) => {{
380+
let args = format!($($arg)*);
381+
$crate::zend::printf(args.as_str()).expect("Failed to print to PHP stdout");
382+
}};
383+
}
384+
385+
/// Prints to the PHP standard output, with a newline.
386+
///
387+
/// The newline is only a newline character regardless of platform (no carriage
388+
/// return).
389+
///
390+
/// Acts exactly the same as the built-in [`println`] macro.
391+
///
392+
/// # Panics
393+
///
394+
/// Panics if the generated string could not be converted to a `CString` due to
395+
/// `NUL` characters.
396+
#[macro_export]
397+
macro_rules! php_println {
398+
() => {
399+
$crate::php_print!("\n");
400+
};
401+
402+
($fmt: tt) => {
403+
$crate::php_print!(concat!($fmt, "\n"));
404+
};
405+
406+
($fmt: tt, $($arg: tt) *) => {
407+
$crate::php_print!(concat!($fmt, "\n"), $($arg)*);
408+
};
409+
}
410+
365411
pub(crate) use into_zval;
366412
pub(crate) use try_from_zval;

src/zend/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,36 @@ mod globals;
99
mod handlers;
1010
mod module;
1111

12+
use crate::{error::Result, ffi::php_printf};
13+
use std::ffi::CString;
14+
1215
pub use _type::ZendType;
1316
pub use class::ClassEntry;
1417
pub use ex::ExecuteData;
1518
pub use function::FunctionEntry;
1619
pub use globals::ExecutorGlobals;
1720
pub use handlers::ZendObjectHandlers;
1821
pub use module::ModuleEntry;
22+
23+
// Used as the format string for `php_printf`.
24+
const FORMAT_STR: &[u8] = b"%s\0";
25+
26+
/// Prints to stdout using the `php_printf` function.
27+
///
28+
/// Also see the [`php_print`] and [`php_println`] macros.
29+
///
30+
/// # Arguments
31+
///
32+
/// * message - The message to print to stdout.
33+
///
34+
/// # Returns
35+
///
36+
/// Nothing on success, error if the message could not be converted to a
37+
/// [`CString`].
38+
pub fn printf(message: &str) -> Result<()> {
39+
let message = CString::new(message)?;
40+
unsafe {
41+
php_printf(FORMAT_STR.as_ptr().cast(), message.as_ptr());
42+
};
43+
Ok(())
44+
}

0 commit comments

Comments
 (0)