Skip to content
Merged
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
11 changes: 7 additions & 4 deletions crates/stub-parser/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ impl ClassLikeType {
/// the given number of methods, is in the given namespace, and the overall
/// program where the class is declared is passed to allow fetching
/// unprocessed details.
#[expect(clippy::too_many_lines, reason = "Complicated function")]
fn init_and_register_code(
&self,
scope: &mut ImportsUsed,
Expand Down Expand Up @@ -251,9 +252,11 @@ impl ClassLikeType {
)
}
ClassLikeType::PhpEnum(php_enum) => {
if method_count != 0 {
return Err("Enum methods not handled yet".to_owned());
}
let method_param = if method_count == 0 {
"std::ptr::null_mut()"
} else {
"methods.as_ptr()"
};
let get_raw_code = |span: Span| -> &str {
let start: usize = span.start.try_into().expect("Valid usize");
let end: usize = span.end.try_into().expect("Valid usize");
Expand All @@ -273,7 +276,7 @@ impl ClassLikeType {
\n{space}{space}rsphp_sys::zend_register_internal_enum(\
\n{space}{space}{space}c\"{ns_name_segment}{name}\".as_ptr(),\
\n{space}{space}{space}{backing_type_code},\
\n{space}{space}{space}std::ptr::null_mut(),\
\n{space}{space}{space}{method_param},\
\n{space}{space})\
\n{space}}};",
space = " ",
Expand Down
32 changes: 32 additions & 0 deletions crates/stub-parser/tests/cases/enum_methods.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// Module for `Color` enum.
pub(crate) mod Color {
use rsphp_macros_rs::ZEND_ARG_INFO_FOR_FUNCTION_WITH_RETURN_TYPE_MASK;
use rsphp_sys::{IS_UNDEF, MAY_BE_BOOL, zend_class_entry, zend_enum_add_case_cstr, zend_function_entry, zend_internal_arg_info};

/// Argument information for `Color::isGreyScale()`.
pub(crate) const ARGINFO_ISGREYSCALE: [zend_internal_arg_info; 1] = [
ZEND_ARG_INFO_FOR_FUNCTION_WITH_RETURN_TYPE_MASK!(0, 0, MAY_BE_BOOL, 0),
];

/// Register the `Color` enum.
pub(crate) fn register_enum(methods: &[zend_function_entry]) -> *mut zend_class_entry {
assert_eq!(2, methods.len(), "Color should have 1 methods");

let class_entry: *mut zend_class_entry = unsafe {
rsphp_sys::zend_register_internal_enum(
c"Color".as_ptr(),
IS_UNDEF,
methods.as_ptr(),
)
};
unsafe {
zend_enum_add_case_cstr(class_entry, c"White".as_ptr(), std::ptr::null_mut());
zend_enum_add_case_cstr(class_entry, c"Black".as_ptr(), std::ptr::null_mut());
zend_enum_add_case_cstr(class_entry, c"Red".as_ptr(), std::ptr::null_mut());
zend_enum_add_case_cstr(class_entry, c"Green".as_ptr(), std::ptr::null_mut());
zend_enum_add_case_cstr(class_entry, c"Blue".as_ptr(), std::ptr::null_mut());
};

class_entry
}
}
12 changes: 12 additions & 0 deletions crates/stub-parser/tests/cases/enum_methods.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

enum Color {
case White;
case Black;
case Red;
case Green;
case Blue;

public function isGreyScale(): bool {}

}
1 change: 0 additions & 1 deletion example/rust-ext-demo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 33 additions & 3 deletions example/rust-ext-demo/src/demo.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use rsphp_macros_c::{RETURN_THROWS, Z_LVAL_P, Z_OBJ_P, Z_OBJCE_P, ZEND_FE_END, ZVAL_STR};
use rsphp::extensions::ZendStringExt as _;
use rsphp_macros_c::{
RETURN_THROWS, Z_LVAL_P, Z_OBJ_P, Z_OBJCE_P, Z_STR_P, ZEND_FE_END, ZVAL_BOOL, ZVAL_STR,
};
use rsphp_macros_rs::{ZEND_ME, ZEND_PARSE_PARAMETERS_NONE, ZEND_THIS};
use rsphp_sys::{
ZEND_ACC_PUBLIC, ZEND_RESULT_CODE, zend_ce_arithmetic_error, zend_class_entry,
zend_execute_data, zend_function_entry, zend_parse_parameters, zend_read_property, zend_string,
zend_string_init, zend_throw_exception, zend_update_property_long, zval,
zend_enum_fetch_case_name, zend_execute_data, zend_function_entry, zend_object,
zend_parse_parameters, zend_read_property, zend_string, zend_string_init, zend_throw_exception,
zend_update_property_long, zval,
};
use std::ffi::c_char;

pub(crate) static mut rs_demo_ptr: *mut zend_class_entry = std::ptr::null_mut();
pub(crate) static mut color_ptr: *mut zend_class_entry = std::ptr::null_mut();

extern "C" fn RSDemoConstructor(execute_data: *mut zend_execute_data, _return_value: *mut zval) {
let mut initial_value: i64 = -1;
Expand Down Expand Up @@ -95,6 +100,21 @@ extern "C" fn RSDemoAsString(execute_data: *mut zend_execute_data, return_value:
unsafe { ZVAL_STR!(return_value, result) };
}

extern "C" fn ColorIsGreyScale(execute_data: *mut zend_execute_data, return_value: *mut zval) {
// SAFETY: execute_data is valid and has $this, this is a class method
unsafe { ZEND_PARSE_PARAMETERS_NONE!(execute_data) };

let enum_obj: *mut zend_object = unsafe { Z_OBJ_P!(ZEND_THIS!(execute_data)) };
let name_obj: *const zval = unsafe { zend_enum_fetch_case_name(enum_obj) };
let name: *const zend_string = unsafe { Z_STR_P!(name_obj) };
let name_ref = unsafe { name.as_ref() }.expect("Valid reference");
let name_str = name_ref.as_bstr();
let result = name_str == "White" || name_str == "Black";
unsafe {
ZVAL_BOOL!(return_value, result);
}
}

pub(crate) const class_RSDemo_methods: [zend_function_entry; 4] = [
ZEND_ME!(
"__construct",
Expand All @@ -116,3 +136,13 @@ pub(crate) const class_RSDemo_methods: [zend_function_entry; 4] = [
),
ZEND_FE_END!(),
];

pub(crate) const enum_Color_methods: &[zend_function_entry] = &[
ZEND_ME!(
"isGreyScale",
ColorIsGreyScale,
crate::rustdemo_arginfo::Color::ARGINFO_ISGREYSCALE,
ZEND_ACC_PUBLIC
),
ZEND_FE_END!(),
];
4 changes: 3 additions & 1 deletion example/rust-ext-demo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rsphp_sys::{
use std::ffi::{c_char, c_int};

mod demo;
use demo::{class_RSDemo_methods, rs_demo_ptr};
use demo::{class_RSDemo_methods, color_ptr, enum_Color_methods, rs_demo_ptr};

mod opaquedata;
use opaquedata::{
Expand Down Expand Up @@ -50,6 +50,8 @@ extern "C" fn my_module_info(entry: *mut zend_module_entry) {
extern "C" fn my_module_init(type_: c_int, module_number: c_int) -> zend_result {
unsafe {
rs_demo_ptr = rustdemo_arginfo::RSDemo::register_class(&class_RSDemo_methods);
color_ptr = rustdemo_arginfo::Color::register_enum(enum_Color_methods);

opaquedata_ptr =
opaquedata_arginfo::RustDemo_OpaqueData::register_class(&class_opaquedata_methods);
init_opaquedata_object_handlers();
Expand Down
11 changes: 11 additions & 0 deletions example/rust-ext-demo/src/rustdemo.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,14 @@ class PropertiesDemo {
public static mixed $is_static;
public final mixed $is_final;
}

enum Color {
case White;
case Black;
case Red;
case Green;
case Blue;

public function isGreyScale(): bool {}

}
45 changes: 45 additions & 0 deletions example/rust-ext-demo/src/rustdemo_arginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,3 +685,48 @@ pub(crate) mod PropertiesDemo {
class_entry
}
}
/// Module for `Color` enum.
pub(crate) mod Color {
use rsphp_macros_rs::ZEND_ARG_INFO_FOR_FUNCTION_WITH_RETURN_TYPE_MASK;
use rsphp_sys::{IS_UNDEF, MAY_BE_BOOL, zend_class_entry, zend_enum_add_case_cstr, zend_function_entry, zend_internal_arg_info};
#[cfg(debug_assertions)]
use rsphp_sys::ZEND_ACC_PUBLIC;
#[cfg(debug_assertions)]
use super::verify_registration;

/// Argument information for `Color::isGreyScale()`.
pub(crate) const ARGINFO_ISGREYSCALE: [zend_internal_arg_info; 1] = [
ZEND_ARG_INFO_FOR_FUNCTION_WITH_RETURN_TYPE_MASK!(0, 0, MAY_BE_BOOL, 0),
];

/// Register the `Color` enum.
pub(crate) fn register_enum(methods: &[zend_function_entry]) -> *mut zend_class_entry {
#[cfg(debug_assertions)]
verify_registration(
"Color",
&[
("isGreyScale", &ARGINFO_ISGREYSCALE, ZEND_ACC_PUBLIC),
],
methods,
);
#[cfg(not(debug_assertions))]
assert_eq!(2, methods.len(), "Color should have 1 methods");

let class_entry: *mut zend_class_entry = unsafe {
rsphp_sys::zend_register_internal_enum(
c"Color".as_ptr(),
IS_UNDEF,
methods.as_ptr(),
)
};
unsafe {
zend_enum_add_case_cstr(class_entry, c"White".as_ptr(), std::ptr::null_mut());
zend_enum_add_case_cstr(class_entry, c"Black".as_ptr(), std::ptr::null_mut());
zend_enum_add_case_cstr(class_entry, c"Red".as_ptr(), std::ptr::null_mut());
zend_enum_add_case_cstr(class_entry, c"Green".as_ptr(), std::ptr::null_mut());
zend_enum_add_case_cstr(class_entry, c"Blue".as_ptr(), std::ptr::null_mut());
};

class_entry
}
}
64 changes: 64 additions & 0 deletions example/rust-ext-demo/tests/Colors.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
--TEST--
Color enum tests
--EXTENSIONS--
rustdemo
--FILE--
<?php

$r = new ReflectionEnum(Color::class);
echo $r;

foreach (Color::cases() as $case) {
var_dump($case, $case->isGreyScale());
}

?>
--EXPECT--
Enum [ <internal:rustdemo> enum Color implements UnitEnum ] {

- Enum cases [5] {
Case White
Case Black
Case Red
Case Green
Case Blue
}

- Constants [0] {
}

- Static properties [0] {
}

- Static methods [1] {
Method [ <internal:rustdemo, prototype UnitEnum> static public method cases ] {

- Parameters [0] {
}
- Return [ array ]
}
}

- Properties [1] {
Property [ public protected(set) readonly string $name ]
}

- Methods [1] {
Method [ <internal:rustdemo> public method isGreyScale ] {

- Parameters [0] {
}
- Return [ bool ]
}
}
}
enum(Color::White)
bool(true)
enum(Color::Black)
bool(true)
enum(Color::Red)
bool(false)
enum(Color::Green)
bool(false)
enum(Color::Blue)
bool(false)
41 changes: 40 additions & 1 deletion example/rust-ext-demo/tests/reflection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Extension [ <persistent> extension #%d rustdemo version 0.1.0 ] {
}
}

- Classes [4] {
- Classes [5] {
Class [ <internal:rustdemo> class RSDemo ] {

- Constants [0] {
Expand Down Expand Up @@ -122,6 +122,45 @@ Extension [ <persistent> extension #%d rustdemo version 0.1.0 ] {
}
}

Enum [ <internal:rustdemo> enum Color implements UnitEnum ] {

- Enum cases [5] {
Case White
Case Black
Case Red
Case Green
Case Blue
}

- Constants [0] {
}

- Static properties [0] {
}

- Static methods [1] {
Method [ <internal:rustdemo, prototype UnitEnum> static public method cases ] {

- Parameters [0] {
}
- Return [ array ]
}
}

- Properties [1] {
Property [ public protected(set) readonly string $name ]
}

- Methods [1] {
Method [ <internal:rustdemo> public method isGreyScale ] {

- Parameters [0] {
}
- Return [ bool ]
}
}
}

Class [ <internal:rustdemo> class RustDemo\OpaqueData ] {

- Constants [0] {
Expand Down
Loading