Skip to content
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

eval_sv() doesn't preserve caller hints/features #21415

Closed
leonerd opened this issue Aug 21, 2023 · 1 comment · Fixed by #21421
Closed

eval_sv() doesn't preserve caller hints/features #21415

leonerd opened this issue Aug 21, 2023 · 1 comment · Fixed by #21421

Comments

@leonerd
Copy link
Contributor

leonerd commented Aug 21, 2023

The Perl API function eval_sv() is conceptually the XS version of the Perl eval() function. But it doesn't really match very well, because it doesn't preserve the hints bits or enabled features of its caller; whereas the Perl function does.

eval_sv() isn't normally accessible from Perl but we can abuse XS::APItest in a built source checkout to find a callable copy of it. See the following examples.

The hints bits include the strict flags; these are not preserved:

$ ./perl -Ilib -e 'use strict; eval("\$x + \$y") or die $@'
Global symbol "$x" requires explicit package name (did you forget to declare "my $x"?) at (eval 1) line 1.
Global symbol "$y" requires explicit package name (did you forget to declare "my $y"?) at (eval 1) line 1.

$ ./perl -Ilib -MXS::APItest -e 'use strict; eval_pv("print \$x + \$y, \"DONE\"", 0)'
0DONE

The features bits include the enabled keywords; these are not preserved either:

$ ./perl -Ilib -e 'use feature "say"; eval("say q(HELLO)") or die $@'
HELLO

$ ./perl -Ilib -MXS::APItest -e 'use strict; eval_pv("say q(HELLO)", 0) or die $@'
syntax error at (eval 6) line 1, near "say q(HELLO)"
Execution of (eval 6) aborted due to compilation errors.

By comparison, the warnings bits are preserved, suggesting that at least this general testing method is valid:

$ ./perl -Ilib -MXS::APItest -e 'use warnings; eval_pv("print 123 + undef", 0) or die $@'
Use of uninitialized value in addition (+) at (eval 6) line 1.
123

$ ./perl -Ilib -MXS::APItest -e 'eval_pv("print 123 + undef", 0) or die $@'
123

Ultimately, I don't think this behaviour could be changed by default without breaking a huge amount of existing code. However, I think it would be useful to provide a new flag of some name I don't have a good suggestion for, to ask it to do this.

@rwp0
Copy link
Contributor

rwp0 commented Aug 23, 2023

Nice find 👍🏻, I wander if that'd be the same with -E


perl -Ilib -MXS::APItest -e 'use strict; eval_pv("say q(HELLO)", 0) or die $@'

I think this example should have use feature instead

perl -Ilib -MXS::APItest -e 'eval_pv("print 123 + undef", 0) or die $@'

The warnings example (of eval_pv) doesn't include use warnings too

tonycoz added a commit to tonycoz/perl5 that referenced this issue Aug 23, 2023
tonycoz added a commit to tonycoz/perl5 that referenced this issue Aug 24, 2023
tonycoz added a commit to tonycoz/perl5 that referenced this issue Aug 28, 2023
tonycoz added a commit to tonycoz/perl5 that referenced this issue Aug 29, 2023
tonycoz added a commit that referenced this issue Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants