From c9f72cf51b809fe907db1faa4071f0baafeab20c Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 27 Jun 2013 17:19:58 -0400 Subject: use the publisher for communicating the lexical environment --- lib/Reply/Plugin/Autocomplete/Lexicals.pm | 27 ++++++++------------------- lib/Reply/Plugin/Defaults.pm | 21 +++++++++++++-------- lib/Reply/Plugin/LexicalPersistence.pm | 12 ++++++------ lib/Reply/Plugin/ResultCache.pm | 19 ++++++++----------- 4 files changed, 35 insertions(+), 44 deletions(-) diff --git a/lib/Reply/Plugin/Autocomplete/Lexicals.pm b/lib/Reply/Plugin/Autocomplete/Lexicals.pm index 47f9a95..e7c0c71 100644 --- a/lib/Reply/Plugin/Autocomplete/Lexicals.pm +++ b/lib/Reply/Plugin/Autocomplete/Lexicals.pm @@ -32,25 +32,11 @@ sub new { return $self; } -sub compile { +sub lexical_environment { my $self = shift; - my ($next, @args) = @_; - - my ($code) = $next->(@args); - - # XXX this is just copied from LexicalPersistence, which sucks first - # because of copying and pasting code, and second because it doesn't catch - # anything that wouldn't be caught by LexicalPersistence (setting lexicals - # via Devel::StackTrace::WithLexicals (Carp::Reply), setting extra lexicals - # (ResultCache)) - # we really need a way to broadcast various bits of information among - # plugins - $self->{env} = { - %{ $self->{env} }, - %{ peek_sub($code) }, - }; - - return $code; + my ($name, $env) = @_; + + $self->{env}{$name} = $env; } sub tab_handler { @@ -62,8 +48,11 @@ sub tab_handler { my ($sigil, $name_prefix) = $var =~ /(.)(.*)/; + my $env = { map { %$_ } values %{ $self->{env} } }; + my @env = keys %$env; + my @results; - for my $env_var (keys %{ $self->{env} }) { + for my $env_var (@env) { my ($env_sigil, $env_name) = $env_var =~ /(.)(.*)/; next unless index($env_name, $name_prefix) == 0; diff --git a/lib/Reply/Plugin/Defaults.pm b/lib/Reply/Plugin/Defaults.pm index eb43ecd..40dee9b 100644 --- a/lib/Reply/Plugin/Defaults.pm +++ b/lib/Reply/Plugin/Defaults.pm @@ -20,6 +20,7 @@ sub new { my $self = $class->SUPER::new(@_); $self->{quit} = 0; + $self->{env} = {}; return $self; } @@ -46,14 +47,11 @@ sub compile { my $self = shift; my ($next, $line, %args) = @_; - my @envs = ( - ($args{environment} ? ($args{environment}) : ()), - values %{ delete $args{environments} }, - ); - - if (@envs) { - $args{environment} = { map { %$_ } @envs } - } + my $default_env = delete $self->{env}{default} || {}; + my $env = { + (map { %$_ } values %{ $self->{env} }), + %$default_env, + }; my $package = delete $args{package} || 'main'; my $prefix = "package $package;\n$PREFIX"; @@ -61,10 +59,17 @@ sub compile { return eval_closure( source => "sub {\n$prefix;\n$line\n}", terse_error => 1, + environment => $env, %args, ); } +sub lexical_environment { + my $self = shift; + my ($name, $env) = @_; + $self->{env}{$name} = $env; +} + sub execute { my $self = shift; my ($next, $code, @args) = @_; diff --git a/lib/Reply/Plugin/LexicalPersistence.pm b/lib/Reply/Plugin/LexicalPersistence.pm index 44b2ef8..99c328c 100644 --- a/lib/Reply/Plugin/LexicalPersistence.pm +++ b/lib/Reply/Plugin/LexicalPersistence.pm @@ -22,8 +22,12 @@ then use C<$x> as expected in subsequent lines. sub new { my $class = shift; + my %opts = @_; + my $self = $class->SUPER::new(@_); $self->{env} = {}; + $self->{publisher} = $opts{publisher}; + return $self; } @@ -31,12 +35,6 @@ sub compile { my $self = shift; my ($next, $line, %args) = @_; - $args{environment} ||= {}; - $args{environment} = { - %{ $args{environment} }, - %{ $self->{env} }, - }; - my ($code) = $next->($line, %args); $self->{env} = { @@ -44,6 +42,8 @@ sub compile { %{ peek_sub($code) }, }; + $self->{publisher}->('lexical_environment', default => $self->{env}); + return $code; } diff --git a/lib/Reply/Plugin/ResultCache.pm b/lib/Reply/Plugin/ResultCache.pm index 91fda37..713a7fd 100644 --- a/lib/Reply/Plugin/ResultCache.pm +++ b/lib/Reply/Plugin/ResultCache.pm @@ -28,21 +28,11 @@ sub new { my $self = $class->SUPER::new(@_); $self->{results} = []; $self->{result_name} = $opts{variable} || 'res'; + $self->{publisher} = $opts{publisher}; return $self; } -sub compile { - my $self = shift; - my ($next, $line, %args) = @_; - - $args{environments}{''.__PACKAGE__} = { - "\@$self->{result_name}" => $self->{results}, - }; - - $next->($line, %args); -} - sub execute { my $self = shift; my ($next, @args) = @_; @@ -55,6 +45,13 @@ sub execute { push @{ $self->{results} }, \@res; } + $self->{publisher}->( + 'lexical_environment', + result_cache => { + "\@$self->{result_name}" => $self->{results}, + }, + ); + return @res; } -- cgit v1.2.3-54-g00ecf