File tree Expand file tree Collapse file tree 4 files changed +76
-1
lines changed Expand file tree Collapse file tree 4 files changed +76
-1
lines changed Original file line number Diff line number Diff line change @@ -192,5 +192,6 @@ bind! {
192
192
zend_objects_store_del,
193
193
gc_possible_root,
194
194
ZEND_ACC_NOT_SERIALIZABLE ,
195
- executor_globals
195
+ executor_globals,
196
+ php_printf
196
197
}
Original file line number Diff line number Diff line change @@ -44,6 +44,8 @@ pub mod prelude {
44
44
pub use crate :: php_function;
45
45
pub use crate :: php_impl;
46
46
pub use crate :: php_module;
47
+ pub use crate :: php_print;
48
+ pub use crate :: php_println;
47
49
pub use crate :: php_startup;
48
50
pub use crate :: types:: ZendCallable ;
49
51
pub use crate :: ZvalConvert ;
Original file line number Diff line number Diff line change @@ -362,5 +362,51 @@ macro_rules! try_from_zval {
362
362
} ;
363
363
}
364
364
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
+
365
411
pub ( crate ) use into_zval;
366
412
pub ( crate ) use try_from_zval;
Original file line number Diff line number Diff line change @@ -9,10 +9,36 @@ mod globals;
9
9
mod handlers;
10
10
mod module;
11
11
12
+ use crate :: { error:: Result , ffi:: php_printf} ;
13
+ use std:: ffi:: CString ;
14
+
12
15
pub use _type:: ZendType ;
13
16
pub use class:: ClassEntry ;
14
17
pub use ex:: ExecuteData ;
15
18
pub use function:: FunctionEntry ;
16
19
pub use globals:: ExecutorGlobals ;
17
20
pub use handlers:: ZendObjectHandlers ;
18
21
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
+ }
You can’t perform that action at this time.
0 commit comments