use strict;
use warnings;
+use 5.008001;
-use vars qw(%lift);
-
-use Inline C => <<'EOC';
-#include <stdio.h>
-#include <string.h>
-#include "proto.h"
-
-/* lifted from op.c (w/linklist -> Perl_linklist) */
-
-#define LINKLIST(o) ((o)->op_next ? (o)->op_next : Perl_linklist((OP*)o))
-
-/* pointer to old PL_check entersub entry to be populated in init */
-
-STATIC OP *(*dbl_old_ck_entersub)(pTHX_ OP *op);
+our $VERSION = 0.001000;
-/* replacement PL_check entersub entry */
+use vars qw(%lift);
+use base qw(DynaLoader);
+use B::Hooks::OP::Check::EntersubForCV;
-STATIC OP *dbl_ck_entersub(pTHX_ OP *o) {
- OP *kid;
- OP *last;
- OP *curop;
- HV *stash;
- I32 type = o->op_type;
- SV *sv;
- char* stack_save;
- HV* to_lift;
- SV** to_lift_pack_ref;
- HV* to_lift_pack_hash;
- SV** to_lift_flag_ref;
+bootstrap Devel::BeginLift;
- o = dbl_old_ck_entersub(aTHX_ o); /* let the original do its job */
+sub import {
+ my ($class, @args) = @_;
+ my $target = caller;
+ $class->setup_for($target => \@args);
+}
- kid = cUNOPo->op_first;
+sub unimport {
+ my ($class) = @_;
+ my $target = caller;
+ $class->teardown_for($target);
+}
- if (kid->op_type != OP_NULL) /* pushmark for method call ... */
- return o;
+sub setup_for {
+ my ($class, $target, $args) = @_;
+ $lift{$target} ||= [];
+ push @{ $lift{$target} }, map {
+ setup_for_cv($_);
+ } map {
+ ref $_ eq 'CODE'
+ ? $_
+ : \&{ "${target}::${_}" }
+ } @{ $args };
+}
- last = kLISTOP->op_last;
+sub teardown_for {
+ my ($class, $target) = @_;
+ teardown_for_cv($_) for @{ $lift{$target} };
+ delete $lift{$target};
+}
- if (last->op_type != OP_NULL) /* not what we expected */
- return o;
+=head1 NAME
+
+Devel::BeginLift - make selected sub calls evaluate at compile time
+
+=head1 SYNOPSIS
+
+ use Devel::BeginLift qw(foo baz);
+
+ use vars qw($i);
+
+ BEGIN { $i = 0 }
+
+ sub foo { "foo: $_[0]\n"; }
+
+ sub bar { "bar: $_[0]\n"; }
+
+ for (1 .. 3) {
+ print foo($i++);
+ print bar($i++);
+ }
+
+ no Devel::BeginLift;
+
+ print foo($i++);
- kid = cUNOPx(last)->op_first;
+outputs -
- if (kid->op_type != OP_GV) /* not a GV so ignore */
- return o;
+foo: 0
+bar: 1
+foo: 0
+bar: 2
+foo: 0
+bar: 3
+foo: 4
- stash = GvSTASH(kGVOP_gv);
+=head1 DESCRIPTION
- /* printf("Calling GV %s -> %s\n",
- HvNAME(stash), GvNAME(kGVOP_gv)); */
+Devel::BeginLift 'lifts' arbitrary sub calls to running at compile time
+- sort of a souped up version of "use constant". It does this via some
+slightly insane perlguts magic.
- to_lift = get_hv("Devel::BeginLift::lift", FALSE);
+=head2 import
- if (!to_lift)
- return o;
+ use Devel::BeginLift qw(list of subs);
- to_lift_pack_ref = hv_fetch(to_lift, HvNAME(stash), strlen(HvNAME(stash)),
- FALSE);
+Calls Devel::BeginLift->setup_for(__PACKAGE__ => \@list_of_subs);
- if (!to_lift_pack_ref || !SvROK(*to_lift_pack_ref))
- return o; /* not a hashref */
+=head2 unimport
- to_lift_pack_hash = (HV*) SvRV(*to_lift_pack_ref);
+ no Devel::BeginLift;
- to_lift_flag_ref = hv_fetch(to_lift_pack_hash, GvNAME(kGVOP_gv),
- strlen(GvNAME(kGVOP_gv)), FALSE);
+Calls Devel::BeginLift->teardown_for(__PACKAGE__);
- if (!to_lift_flag_ref || !SvTRUE(*to_lift_flag_ref))
- return o;
+=head2 setup_for
- /* shamelessly lifted from fold_constants in op.c */
+ Devel::BeginLift->setup_for($package => \@subnames);
- stack_save = PL_stack_sp;
- curop = LINKLIST(o);
- o->op_next = 0;
- PL_op = curop;
- CALLRUNOPS(aTHX);
+Installs begin lifting magic (unless already installed) and registers
+"${package}::$name" for each member of @subnames to be executed when parsed
+and replaced with its output rather than left for runtime.
- if (PL_stack_sp > stack_save) { /* sub returned something */
- sv = *(PL_stack_sp--);
- if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
- pad_swipe(o->op_targ, FALSE);
- else if (SvTEMP(sv)) { /* grab mortal temp? */
- (void)SvREFCNT_inc(sv);
- SvTEMP_off(sv);
- }
- op_free(o);
- if (type == OP_RV2GV)
- return newGVOP(OP_GV, 0, (GV*)sv);
- return newSVOP(OP_CONST, 0, sv);
- } else {
- /* this bit not lifted, handles the 'sub doesn't return stuff' case
- which fold_constants can ignore */
- op_free(o);
- return newSVOP(OP_CONST, 0, &PL_sv_undef);
- }
-}
+=head2 teardown_for
-static int initialized = 0;
+ Devel::BeginLift->teardown_for($package);
-void setup() {
- if (!initialized++) {
- dbl_old_ck_entersub = PL_check[OP_ENTERSUB];
- PL_check[OP_ENTERSUB] = dbl_ck_entersub;
- }
-}
+Deregisters all subs currently registered for $package and uninstalls begin
+lifting magic is number of teardown_for calls matches number of setup_for
+calls.
-void teardown() {
- /* ensure we only uninit when number of teardown calls matches
- number of setup calls */
- if (initialized && !--initialized) {
- PL_check[OP_ENTERSUB] = dbl_old_ck_entersub;
- }
-}
-EOC
+=head1 AUTHOR
-# C code done, now for the perl.
+Matt S Trout - <mst@shadowcatsystems.co.uk>
-sub import {
- my ($class, @args) = @_;
- my $target = caller;
- $class->setup_for($target => \@args);
-}
+Company: http://www.shadowcatsystems.co.uk/
+Blog: http://chainsawblues.vox.com/
-sub unimport {
- my ($class) = @_;
- my $target = caller;
- $class->teardown_for($target);
-}
+=head1 LICENSE
-sub setup_for {
- my ($class, $target, $args) = @_;
- setup();
- $lift{$target}{$_} = 1 for @$args;
-}
+This library is free software under the same terms as perl itself
-sub teardown_for {
- my ($class, $target) = @_;
- delete $lift{$target};
- teardown();
-}
+=cut
1;