Skip to content

ext/dom: Return numeric results from XPath callbacks as numbers #22105

Open
afflerbach wants to merge 1 commit into
php:masterfrom
afflerbach:xpath-callback-return-numbers
Open

ext/dom: Return numeric results from XPath callbacks as numbers #22105
afflerbach wants to merge 1 commit into
php:masterfrom
afflerbach:xpath-callback-return-numbers

Conversation

@afflerbach
Copy link
Copy Markdown
Contributor

@afflerbach afflerbach requested a review from devnexen as a code owner May 21, 2026 08:15
@afflerbach afflerbach force-pushed the xpath-callback-return-numbers branch from 8388fe6 to dc37660 Compare May 21, 2026 08:20
@devnexen
Copy link
Copy Markdown
Member

nice additions, want to see a bit more in the tests area, I ll write few remarks shortly.

<?php
$dom = Dom\XMLDocument::createFromString('<root/>');
$xpath = new Dom\XPath($dom);
$xpath->registerPhpFunctionNs('urn:x', 'get-int', fn(): int => 42);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • you should also test edge cases such as NAN, PHP_INT_MIN/MAX ...
  • Using XSLTProcessor::registerPHPFunctions for returning an int/float to assert proper values even as strings.
  • demonstrating benefits of your changes through an expression, e.g. x:get-int() + 1

Comment thread ext/dom/xpath_callbacks.c
} else if (Z_TYPE(callback_retval) == IS_FALSE || Z_TYPE(callback_retval) == IS_TRUE) {
valuePush(ctxt, xmlXPathNewBoolean(Z_TYPE(callback_retval) == IS_TRUE));
} else if (Z_TYPE(callback_retval) == IS_LONG) {
valuePush(ctxt, xmlXPathNewFloat(Z_LVAL(callback_retval)));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a BC break thus need a good UPGRADING entry.

Copy link
Copy Markdown
Member

@ndossche ndossche May 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will break for numbers > 2**52 on 64-bit systems. Previously, it was represented as a string and could be coerced back to a php int without precision loss. Doubles can't reliably store integers > 2**52 so you would get precision loss.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is what tests are for ;)

@afflerbach afflerbach marked this pull request as draft May 21, 2026 10:23
@afflerbach afflerbach force-pushed the xpath-callback-return-numbers branch from dc37660 to a8ae914 Compare May 29, 2026 10:03
@afflerbach afflerbach force-pushed the xpath-callback-return-numbers branch from a8ae914 to cdc91fe Compare May 29, 2026 11:44
@afflerbach afflerbach force-pushed the xpath-callback-return-numbers branch from cdc91fe to bc32458 Compare May 29, 2026 12:28
@afflerbach afflerbach marked this pull request as ready for review May 29, 2026 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants