Implement proper zval to string conversion in av_value_to_string - #24
Merged
Merged
Conversation
Replace the placeholder implementation of av_value_to_string with full type-aware conversion of any PHP zval into a zend_string for use in error message templates: - null -> "null" - bool -> "true"/"false" (instead of "1"/"") - int/double -> numeric string via zend_long_to_str/zend_double_to_str - string -> quoted (single quotes wrap the value) - array -> "array" - object -> __toString() result if Stringable, else the class name - resource -> the resource type name, or "resource" - references are dereferenced before inspection Add zend_list.h and math.h includes for the resource type lookup. Co-authored-by: matapatos <matapatos@users.noreply.github.com>
Add Ceedling C unit tests covering av_value_to_string for every zval
type (null, bool, long, double, string, array, object, resource) plus
the Stringable/__toString and resource-unknown-type fallback paths.
To make the function unit-testable in isolation:
- Move av_value_to_string into its own translation unit
(src/helpers/av_value_to_string.{c,h}) so tests link only that minimal
file instead of the full av_error_messages.c dependency graph.
- Route all Zend internals it touches through mockable wrappers in
av_wrappers.{c,h}: av_string_concat3, av_string_copy, av_long_to_str,
av_double_to_str, av_is_stringable, av_instanceof_function,
av_rsrc_list_get_rsrc_type, av_call_tostring, av_zval_ptr_dtor.
- Fix a latent bug exposed by the tests: the Stringable branch compared
the return of zend_call_method_with_0_params (a zval*) to SUCCESS,
which is always false, making __toString a dead path. av_call_tostring
now reports success only when __toString returns a string.
- Register the new source file in config.m4.
- Teach CMock (project.yml) to treat zend_class_entry/zend_object/
zend_resource as void so pointer args compare by value instead of
trying to sizeof an incomplete struct.
Verification: ceedling test:all -> 114/114 pass (89 existing + 25 new);
extension builds cleanly with the new translation unit.
Co-authored-by: matapatos <matapatos@users.noreply.github.com>
matapatos
force-pushed
the
vibe/av-value-to-string-80d5c7
branch
from
September 10, 2026 15:12
f782460 to
80cae46
Compare
Including <Zend/zend_string.h> in av_wrappers.h before the core Zend headers caused PHP 8.2 build failures: ZSTR_VAL/ZSTR_LEN macros were undefined, cascading into errors in zend_hash.h and zend_operators.h. The forward declaration in zend_types.h suffices for the header's prototypes; the .c file includes zend_API.h which pulls zend_string.h in the correct transitive order. Co-authored-by: matapatos <matapatos@users.noreply.github.com>
The previous piecemeal includes of zend_string.h (in the header) and zend_operators.h/zend_types.h (in the .c) work on PHP 8.4 but break on PHP 8.2: including zend_string.h before the full zend.h chain leaves ZSTR_VAL/ZSTR_LEN and the zend_string_* inline declarations undefined when zend_hash.h and zend_operators.h expand, causing "conflicting types" and "subscripted value is neither array nor pointer" errors. Fix by following the repo convention: include Zend/zend_API.h, which pulls in zend.h and the rest of the Zend headers in the correct order. Remove the standalone zend_string.h include from the header since zend_types.h already forward-declares zend_string for the prototype. Co-authored-by: matapatos <matapatos@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
zvalto azend_stringinav_value_to_string, replacing the placeholder// TODOimplementation that delegated tozval_get_string.null→nullbool→true/false(previously1/ empty string)int/double→ numeric strings viazend_long_to_str/zend_double_to_strstring→ single-quoted valuearray→arrayobject→__toString()result whenStringable, otherwise the class nameresource→ resource type name (orresource)tests/c/test_value_to_string.c) covering all of the above plus the Stringable/__toStringand resource-unknown-type fallback paths.Testability refactor
av_value_to_stringinto its own translation unit (src/helpers/av_value_to_string.{c,h}) so tests link only that minimal file instead of the fullav_error_messages.cdependency graph. Registered inconfig.m4.av_wrappers.{c,h}:av_string_concat3,av_string_copy,av_long_to_str,av_double_to_str,av_is_stringable,av_instanceof_function,av_rsrc_list_get_rsrc_type,av_call_tostring,av_zval_ptr_dtor.zend_call_method_with_0_params(azval*) toSUCCESS, which is always false — so__toStringwas a dead path and objects always fell back to the class name.av_call_tostringnow reports success only when__toStringreturns a string.project.yml) to treatzend_class_entry/zend_object/zend_resourceas void so mock pointer args compare by value instead of trying tosizeofan incomplete struct.Verification
ceedling test:all→ 114/114 pass (89 existing + 25 new forav_value_to_string).phpize && ./configure --enable-attributes-validation && make) → Build complete, zero warnings in changed files (PHP 8.4).cc -Wall -Wextra -fsyntax-only src/helpers/av_value_to_string.c→ clean.Note: PHP/Ruby toolchains were not preinstalled in the environment; I installed
php-dev(PHP 8.4),ruby, andceedlingto compile-verify. The repo targets PHP 8.2+; the Zend APIs used are stable across PHP 8.2–8.4. The existing PHP integration tests do not exercise the{value}placeholder, so current expectations (e.g.The value must be integer or float.) are unaffected.