Closed p5pRT closed 14 years ago
[As requested by Jesse\, I'm forwarding these patches to RT so they don't get lost. For the record\, the patches are against commit 23a23683.]
The attached patches make it possible for extensions to hook into perl's lexical scope mechanism at compile time. This would allow things like my SCOPECHECK and Florian's @{^COMPILE_SCOPE_CONTAINER} to be implemented as CPAN modules[1]. Usage is like this (from XS):
STATIC void my_start_hook(pTHX_ int full); STATIC void my_pre_end_hook(pTHX_ OP **o); STATIC void my_post_end_hook(pTHX_ OP **o); STATIC void my_eval_hook(pTHX_ OP *const o); STATIC BHK my_hooks;
BOOT: BhkENTRY_set(&my_hooks\, start\, my_start_hook); BhkENTRY_set(&my_hooks\, pre_end\, my_pre_end_hook); BhkENTRY_set(&my_hooks\, post_end\, my_post_end_hook); BhkENTRY_set(&my_hooks\, eval\, my_eval_hook); Perl_blockhook_register(aTHX_ &my_hooks);
This will cause
- my_start_hook to be called at the start of compiling every lexical scope\, with the 'full' parameter from Perl_block_start.
- my_pre_end_hook to be called at the end of compiling every lexical scope\, *before* the compile-time stack is unwound\, with o pointing to the root OP for the scope. It is a double pointer so the hook can substitute a different OP if it needs to.
- my_post_end_hook to be called *after* the stack is unwound.
- my_eval_hook to be called just before compiling an eval/do/require\, with o set to the OP that requested the eval.
The patches are rebased against current blead\, and the branch has been pushed to http://github.com/mauzo/perl/tree/blockhooks . I hope including a whole lot of patches in one mail like this is OK: it seemed easier than separate mails for each patch\, since they all go together.
Ben
[1] Implementing the rest of the Perl 6 blocks on CPAN would require at least one more patch\, to allow extensions to create pad entries.
will match. Anything +pushed onto the save stack by this hook will be popped just before the +scope ends (between the Cand C hooks, in fact). + +=item C + +This is called at the end of a lexical scope, just before unwinding the +stack. I is the root of the optree representing the scope; it is a +double pointer so you can replace the OP if you need to. + +=item C + +This is called at the end of a lexical scope, just after unwinding the +stack. I is as above. Note that it is possible for calls to C +and C to nest, if there is something on the save stack that +calls string eval. + +=item C + +This is called just before starting to compile an C , C , C or C
@rgs - Status changed from 'new' to 'resolved'
On 6 July 2010 12:33\, Ben Morrow \perlbug\-followup@​perl\.org wrote:
The attached patches make it possible for extensions to hook into perl's lexical scope mechanism at compile time. This would allow things like my SCOPECHECK and Florian's @{^COMPILE_SCOPE_CONTAINER} to be implemented as CPAN modules[1]. Usage is like this (from XS):
STATIC void my_start_hook(pTHX_ int full); STATIC void my_pre_end_hook(pTHX_ OP **o); STATIC void my_post_end_hook(pTHX_ OP **o); STATIC void my_eval_hook(pTHX_ OP *const o); STATIC BHK my_hooks;
BOOT: BhkENTRY_set(&my_hooks\, start\, my_start_hook); BhkENTRY_set(&my_hooks\, pre_end\, my_pre_end_hook); BhkENTRY_set(&my_hooks\, post_end\, my_post_end_hook); BhkENTRY_set(&my_hooks\, eval\, my_eval_hook); Perl_blockhook_register(aTHX_ &my_hooks);
This will cause
- my_start_hook to be called at the start of compiling every lexical scope\, with the 'full' parameter from Perl_block_start.
- my_pre_end_hook to be called at the end of compiling every lexical scope\, *before* the compile-time stack is unwound\, with o pointing to the root OP for the scope. It is a double pointer so the hook can substitute a different OP if it needs to.
- my_post_end_hook to be called *after* the stack is unwound.
- my_eval_hook to be called just before compiling an eval/do/require\, with o set to the OP that requested the eval.
The patches are rebased against current blead\, and the branch has been pushed to http://github.com/mauzo/perl/tree/blockhooks . I hope including a whole lot of patches in one mail like this is OK: it seemed easier than separate mails for each patch\, since they all go together.
Ben
[1] Implementing the rest of the Perl 6 blocks on CPAN would require at least one more patch\, to allow extensions to create pad entries.
Thanks\, applied.
A small remark. The docs state:
Once registered\, there is no mechanism to switch these hooks off\, so if that is necessary you will need to do this yourself.
but if I'm not mistaken you could always switch the corresponding flag off in the BHK structure for that.
Migrated from rt.perl.org#76390 (status was 'resolved')
Searchable as RT76390$