Skip to content

Zend autoloader fallback once #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 15, 2022
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
15 changes: 11 additions & 4 deletions Zend/tests/autoloading/function/global_fallback002.phpt
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
--TEST--
Fallback to global function should not trigger autoloading.
Fallback to global function triggers autoloading once.
--FILE--
<?php

namespace {
function loader($name) {
echo $name, \PHP_EOL;
echo "function loader called with $name\n";
}

autoload_register_function('loader');

function foo() {
echo "I am foo in global namespace.\n";
}
}

namespace bar {
var_dump('Hello');
foo();
}

?>
--EXPECT--
string(5) "Hello"
function loader called with bar\foo
I am foo in global namespace.
27 changes: 27 additions & 0 deletions Zend/tests/autoloading/function/global_fallback003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Fallback to non-existent function triggers autoloading once in namespace, once in global.
--FILE--
<?php

namespace {
function loader($name) {
echo "function loader called with $name\n";
}

autoload_register_function('loader');
}

namespace bar {
try {
non_existent_function();
}
catch (\Error $e) {
echo "Error correctly caught: " . $e->getMessage() . "\n";
}
}

?>
--EXPECT--
function loader called with bar\non_existent_function
function loader called with non_existent_function
Error correctly caught: Call to undefined function bar\non_existent_function()
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
Fallback to global function should trigger autoloading only once per namespace.
--FILE--
<?php

namespace {
function loader($name) {
echo "function loader called with $name\n";
}

autoload_register_function('loader');

function foo() {
echo "I am foo in global namespace.\n";
}
}

namespace bar {
foo();

for ($i = 0; $i < 3; $i += 1) {
foo();
}
}
namespace bar {
foo();
}

namespace Quux {
foo();
}

?>
--EXPECT--
function loader called with bar\foo
I am foo in global namespace.
I am foo in global namespace.
I am foo in global namespace.
I am foo in global namespace.
I am foo in global namespace.
function loader called with Quux\foo
I am foo in global namespace.
7 changes: 5 additions & 2 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -3859,19 +3859,22 @@ ZEND_VM_HOT_HANDLER(69, ZEND_INIT_NS_FCALL_BY_NAME, ANY, CONST, NUM|CACHE_SLOT)
if (UNEXPECTED(fbc == NULL)) {
zval *function_name = (zval *)RT_CONSTANT(opline, opline->op2);
/* Fetch lowercase name stored in the next literal slot */
fbc = zend_lookup_function_ex(Z_STR_P(function_name), Z_STR_P(function_name+1), /* use_autoload */ false);
fbc = zend_lookup_function_ex(Z_STR_P(function_name), Z_STR_P(function_name+1), /* use_autoload */ true);
if (UNEXPECTED(fbc == NULL)) {
if (UNEXPECTED(EG(exception))) {
HANDLE_EXCEPTION();
}
/* Fallback onto global namespace, by fetching the unqualified lowercase name stored in the second literal slot */
fbc = zend_lookup_function_ex(Z_STR_P(function_name+2), Z_STR_P(function_name+2), /* use_autoload */ false);
fbc = zend_lookup_function_ex(Z_STR_P(function_name+2), Z_STR_P(function_name+2), /* use_autoload */ true);
if (fbc == NULL) {
if (UNEXPECTED(EG(exception))) {
HANDLE_EXCEPTION();
}
ZEND_VM_DISPATCH_TO_HELPER(zend_undefined_function_helper);
}
else {
do_bind_function(fbc, function_name);
}
}
if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
init_func_run_time_cache(&fbc->op_array);
Expand Down
7 changes: 5 additions & 2 deletions Zend/zend_vm_execute.h

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