| 1 | #!/usr/bin/env perl | ||||||||||||||
| 2 | # XML::Axk::Sandbox - Sandbox providing a language limited access to the core. | ||||||||||||||
| 3 | # Reminder: all matchers define test($refdata)->bool. | ||||||||||||||
| 4 | # Copyright (c) 2018 cxw42. All rights reserved. Artistic 2. | ||||||||||||||
| 5 | |||||||||||||||
| 6 | package XML::Axk::Sandbox; | ||||||||||||||
| 7 | 2 | ▶ | use XML::Axk::Base; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 2 | ▶ | use Hash::Util::FieldHash; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | #our $VERBOSE = 0; | ||||||||||||||
| 11 | |||||||||||||||
| 12 | #sub _dump { | ||||||||||||||
| 13 | # local $Data::Dumper::Maxdepth = 2; | ||||||||||||||
| 14 | # Dumper @_; | ||||||||||||||
| 15 | #} #_dump() | ||||||||||||||
| 16 | |||||||||||||||
| 17 | # Constructor and inside-out fields ===================================== {{{1 | ||||||||||||||
| 18 | |||||||||||||||
| 19 | # Data storage | ||||||||||||||
| 20 | 2 | my %core_; # The core associated with this sandbox | |||||||||||||
| 21 | my %target_; # The name of the script package in which this lives | ||||||||||||||
| 22 | 2 | Hash::Util::FieldHash::fieldhashes \%core_; | |||||||||||||
| 23 | |||||||||||||||
| 24 | sub new { | ||||||||||||||
| 25 | 0 | ▶ | my $class = shift; | ||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 26 | |||||||||||||||
| 27 | 0 | my $self = bless {}, $class; | |||||||||||||
| 28 | 0 | $core_{$self} = shift; | |||||||||||||
| 29 | 0 | $target_{$self} = shift; | |||||||||||||
| 30 | |||||||||||||||
| 31 | 0 | return $self; | |||||||||||||
| 32 | } #new() | ||||||||||||||
| 33 | |||||||||||||||
| 34 | # }}}1 | ||||||||||||||
| 35 | # Variable injection ============================================= {{{1 | ||||||||||||||
| 36 | |||||||||||||||
| 37 | # Set up the tie between an SP in the script and its storage in the core. | ||||||||||||||
| 38 | sub inject_var { | ||||||||||||||
| 39 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
inject_var: not called CC=5 SCAR=30.5
Pod
undocumented
| |||||||||||||||
| 40 | 0 | ▶ | croak("Internal use only --- see XML::Axk::Language") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | unless scalar caller eq 'XML::Axk::Language'; | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 0 | ▶ | my $target = $target_{$self} or die "No target known for $self"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | |||||||||||||||
| 45 | 0 | my ($lang, $varname) = @_; | |||||||||||||
| 46 | 0 | my $basename = substr($varname, 1); | |||||||||||||
| 47 | |||||||||||||||
| 48 | 2 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 49 | |||||||||||||||
| 50 | 0 | ▶ | if(substr($varname, 0, 1) eq '$') { # scalar | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 51 | 0 | tie(${"${target}::${basename}"}, 'XML::Axk::Vars::Scalar', | |||||||||||||
| 52 | $self, $lang, $varname); | ||||||||||||||
| 53 | |||||||||||||||
| 54 | } elsif(substr($varname, 0, 1) eq '@') { # array | ||||||||||||||
| 55 | 0 | tie(@{"${target}::${basename}"}, 'XML::Axk::Vars::Array', | |||||||||||||
| 56 | $self, $lang, $varname); | ||||||||||||||
| 57 | |||||||||||||||
| 58 | } else { | ||||||||||||||
| 59 | 0 | croak "Can't inject unknown var type $varname"; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | } #_inject_var() | ||||||||||||||
| 62 | |||||||||||||||
| 63 | # }}}1 | ||||||||||||||
| 64 | # SPs =================================================================== {{{1 | ||||||||||||||
| 65 | |||||||||||||||
| 66 | # Get the SPs for a language. Returns a hashref. | ||||||||||||||
| 67 | # A sandbox is associated with one target (user script), but each target can | ||||||||||||||
| 68 | # support multiple languages. | ||||||||||||||
| 69 | # | ||||||||||||||
| 70 | # When called from a language, no params. | ||||||||||||||
| 71 | # When called from an X::A::Vars, a specific language must be provided. | ||||||||||||||
| 72 | # Returns undef if allocate_sps hasn't yet been called for that language. | ||||||||||||||
| 73 | sub sps { | ||||||||||||||
| 74 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
sps: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 75 | 0 | my $lang = caller; | |||||||||||||
| 76 | 0 | ▶ | $lang = shift if $lang =~ /^XML::Axk::Vars/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 0 | ▶ | my $core = $core_{$self} or croak("No core known for $self"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | |||||||||||||||
| 79 | 0 | my $hrSP = $core->{sp}; | |||||||||||||
| 80 | 0 | ▶ | return $hrSP->{$lang} if exists $hrSP->{$lang}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | 0 | return undef; | |||||||||||||
| 82 | } #sps() | ||||||||||||||
| 83 | |||||||||||||||
| 84 | # Allocate the SPs for a particular language. | ||||||||||||||
| 85 | # All the SPs must be given in one call. Subsequent calls are nops. | ||||||||||||||
| 86 | # Takes the list of variables to allocate, with sigils. | ||||||||||||||
| 87 | sub allocate_sps { | ||||||||||||||
| 88 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
allocate_sps: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||
| 89 | 0 | ▶ | croak("Internal use only --- see XML::Axk::Language") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | unless scalar caller eq 'XML::Axk::Language'; | ||||||||||||||
| 91 | 0 | ▶ | my $core = $core_{$self} or croak("No core known for $self"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | |||||||||||||||
| 93 | 0 | my $lang = shift; | |||||||||||||
| 94 | |||||||||||||||
| 95 | 0 | my $hrSP = $core->{sp}; | |||||||||||||
| 96 | 0 | ▶ | return if exists $hrSP->{$lang}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | |||||||||||||||
| 98 | 0 | $hrSP = $hrSP->{$lang} = {}; | |||||||||||||
| 99 | |||||||||||||||
| 100 | 0 | for my $name (@_) { | |||||||||||||
| 101 | 0 | my $sigil = substr($name, 0, 1); | |||||||||||||
| 102 | 0 | ▶ | $hrSP->{$name} = undef, next if $sigil eq '$'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 0 | ▶ | $hrSP->{$name} = [], next if $sigil eq '@'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | #say Dumper \%{$self->{sp}}; | ||||||||||||||
| 107 | } #allocate_sps() | ||||||||||||||
| 108 | |||||||||||||||
| 109 | # }}}1 | ||||||||||||||
| 110 | # Updaters ============================================================== {{{1 | ||||||||||||||
| 111 | |||||||||||||||
| 112 | sub set_updater { | ||||||||||||||
| 113 | 0 | ▶ | my ($self, $lang, $updater) = @_; | ||||||||||||
|
Subroutine
set_updater: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 114 | 0 | ▶ | croak("Internal use only --- see XML::Axk::Language") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 115 | unless scalar caller eq 'XML::Axk::Language'; | ||||||||||||||
| 116 | 0 | ▶ | my $core = $core_{$self} or croak("No core known for $self"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 117 | |||||||||||||||
| 118 | 0 | ▶ | croak("Need a coderef") unless ref $updater eq 'CODE'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 119 | 0 | $core->set_updater($lang, $updater); | |||||||||||||
| 120 | } #set_updater() | ||||||||||||||
| 121 | |||||||||||||||
| 122 | # }}}1 | ||||||||||||||
| 123 | # Events (pre, post, worklist) ========================================== {{{1 | ||||||||||||||
| 124 | |||||||||||||||
| 125 | # We provide direct access to these arrays so that languages can push/pop/&c. | ||||||||||||||
| 126 | # This does mean languages can step on each other, but has the advantage that | ||||||||||||||
| 127 | # I don't have to manually track which order which languages added what to | ||||||||||||||
| 128 | # which list. I am open to discussion of better ways. | ||||||||||||||
| 129 | |||||||||||||||
| 130 | # array($self, $core, $name) | ||||||||||||||
| 131 | my $_array = sub { | ||||||||||||||
| 132 | 0 | ▶ | my ($self, $name) = @_; | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 133 | 0 | ▶ | my $core = $core_{$self} or croak("No core known for $self"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 134 | 0 | return $core->{$name}; # an array ref | |||||||||||||
| 135 | 2 | }; | |||||||||||||
| 136 | |||||||||||||||
| 137 | 0 | ▶ | sub pre_all { push @_, 'pre_all'; goto &$_array; } | ||||||||||||
|
Subroutine
pre_all: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 138 | 0 | ▶ | sub pre_file { push @_, 'pre_file'; goto &$_array; } | ||||||||||||
|
Subroutine
pre_file: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 139 | 0 | ▶ | sub worklist { push @_, 'worklist'; goto &$_array; } | ||||||||||||
|
Subroutine
worklist: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 140 | 0 | ▶ | sub post_file { push @_, 'post_file'; goto &$_array; } | ||||||||||||
|
Subroutine
post_file: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 141 | 0 | ▶ | sub post_all { push @_, 'post_all'; goto &$_array; } | ||||||||||||
|
Subroutine
post_all: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 142 | |||||||||||||||
| 143 | # }}}1 | ||||||||||||||
| 144 | 2 | 1; | |||||||||||||
| 145 | __END__ | ||||||||||||||