From 8577027b2ce558b47adb33183f8fb91730a63539 Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Mon, 16 Nov 2026 13:44:11 -0800 Subject: [PATCH] Stub parser: add support for methods on enums --- crates/stub-parser/src/builder.rs | 11 ++-- .../tests/cases/enum_methods.expected | 32 ++++++++++ .../tests/cases/enum_methods.stub.php | 12 ++++ example/rust-ext-demo/Cargo.lock | 1 - example/rust-ext-demo/src/demo.rs | 36 ++++++++++- example/rust-ext-demo/src/lib.rs | 4 +- example/rust-ext-demo/src/rustdemo.stub.php | 11 ++++ example/rust-ext-demo/src/rustdemo_arginfo.rs | 45 +++++++++++++ example/rust-ext-demo/tests/Colors.phpt | 64 +++++++++++++++++++ example/rust-ext-demo/tests/reflection.phpt | 41 +++++++++++- 10 files changed, 247 insertions(+), 10 deletions(-) create mode 100644 crates/stub-parser/tests/cases/enum_methods.expected create mode 100644 crates/stub-parser/tests/cases/enum_methods.stub.php create mode 100644 example/rust-ext-demo/tests/Colors.phpt diff --git a/crates/stub-parser/src/builder.rs b/crates/stub-parser/src/builder.rs index 6a67dfb..2752c53 100644 --- a/crates/stub-parser/src/builder.rs +++ b/crates/stub-parser/src/builder.rs @@ -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, @@ -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"); @@ -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 = " ", diff --git a/crates/stub-parser/tests/cases/enum_methods.expected b/crates/stub-parser/tests/cases/enum_methods.expected new file mode 100644 index 0000000..1ba5d3c --- /dev/null +++ b/crates/stub-parser/tests/cases/enum_methods.expected @@ -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 + } +} diff --git a/crates/stub-parser/tests/cases/enum_methods.stub.php b/crates/stub-parser/tests/cases/enum_methods.stub.php new file mode 100644 index 0000000..2833b67 --- /dev/null +++ b/crates/stub-parser/tests/cases/enum_methods.stub.php @@ -0,0 +1,12 @@ + 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(); diff --git a/example/rust-ext-demo/src/rustdemo.stub.php b/example/rust-ext-demo/src/rustdemo.stub.php index 87ecbf7..3344f30 100644 --- a/example/rust-ext-demo/src/rustdemo.stub.php +++ b/example/rust-ext-demo/src/rustdemo.stub.php @@ -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 {} + +} diff --git a/example/rust-ext-demo/src/rustdemo_arginfo.rs b/example/rust-ext-demo/src/rustdemo_arginfo.rs index 3de6950..e9b4660 100644 --- a/example/rust-ext-demo/src/rustdemo_arginfo.rs +++ b/example/rust-ext-demo/src/rustdemo_arginfo.rs @@ -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 + } +} diff --git a/example/rust-ext-demo/tests/Colors.phpt b/example/rust-ext-demo/tests/Colors.phpt new file mode 100644 index 0000000..fd20021 --- /dev/null +++ b/example/rust-ext-demo/tests/Colors.phpt @@ -0,0 +1,64 @@ +--TEST-- +Color enum tests +--EXTENSIONS-- +rustdemo +--FILE-- +isGreyScale()); +} + +?> +--EXPECT-- +Enum [ 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 [ static public method cases ] { + + - Parameters [0] { + } + - Return [ array ] + } + } + + - Properties [1] { + Property [ public protected(set) readonly string $name ] + } + + - Methods [1] { + Method [ 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) diff --git a/example/rust-ext-demo/tests/reflection.phpt b/example/rust-ext-demo/tests/reflection.phpt index 3262989..e8e88da 100644 --- a/example/rust-ext-demo/tests/reflection.phpt +++ b/example/rust-ext-demo/tests/reflection.phpt @@ -82,7 +82,7 @@ Extension [ extension #%d rustdemo version 0.1.0 ] { } } - - Classes [4] { + - Classes [5] { Class [ class RSDemo ] { - Constants [0] { @@ -122,6 +122,45 @@ Extension [ extension #%d rustdemo version 0.1.0 ] { } } + Enum [ 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 [ static public method cases ] { + + - Parameters [0] { + } + - Return [ array ] + } + } + + - Properties [1] { + Property [ public protected(set) readonly string $name ] + } + + - Methods [1] { + Method [ public method isGreyScale ] { + + - Parameters [0] { + } + - Return [ bool ] + } + } + } + Class [ class RustDemo\OpaqueData ] { - Constants [0] {