XML/Axk/Sandbox.pm

filter: Statementstmt 20.0% 13 / 65 Branchbran 0.0% 0 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 21.4% 3 / 14 Podpod 10.0% 1 / 10 totaltotal 14.0% 17 / 121 CC 18 SCAR 30.9
CC
18
Cov
13%
allocate_sps
56.0
inject_var
21.1
sps
20.0
CRAP 22.1
« XML/Axk/Core.pm ↑ Summary XML/Axk/Vars/Array.pm »
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
BranchTrueFalseTotal
unless scalar caller eq "XML::Axk::Language" 0 0 0
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
BranchTrueFalseTotal
my $target = $target_{$self} or die "No target known for $self" 0 0 0
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
BranchTrueFalseTotal
if (substr($varname, 0, 1) eq '$') { } 0 0 0
elsif (substr($varname, 0, 1) eq '@') { } 0 0 0
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
BranchTrueFalseTotal
if $lang =~ /^XML::Axk::Vars/u 0 0 0
77 0 ▶ my $core = $core_{$self} or croak("No core known for $self");
Branch: 1 branch
BranchTrueFalseTotal
my $core = $core_{$self} or croak("No core known for $self") 0 0 0
78  
79 0 my $hrSP = $core->{sp};
80 0 ▶ return $hrSP->{$lang} if exists $hrSP->{$lang};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hrSP->{$lang} 0 0 0
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
BranchTrueFalseTotal
unless scalar caller eq "XML::Axk::Language" 0 0 0
90 unless scalar caller eq 'XML::Axk::Language';
91 0 ▶ my $core = $core_{$self} or croak("No core known for $self");
Branch: 1 branch
BranchTrueFalseTotal
my $core = $core_{$self} or croak("No core known for $self") 0 0 0
92  
93 0 my $lang = shift;
94  
95 0 my $hrSP = $core->{sp};
96 0 ▶ return if exists $hrSP->{$lang};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hrSP->{$lang} 0 0 0
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
BranchTrueFalseTotal
if $sigil eq "\$" 0 0 0
103 0 ▶ $hrSP->{$name} = [], next if $sigil eq '@';
Branch: 1 branch
BranchTrueFalseTotal
if $sigil eq "\@" 0 0 0
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
BranchTrueFalseTotal
unless scalar caller eq "XML::Axk::Language" 0 0 0
115 unless scalar caller eq 'XML::Axk::Language';
116 0 ▶ my $core = $core_{$self} or croak("No core known for $self");
Branch: 1 branch
BranchTrueFalseTotal
my $core = $core_{$self} or croak("No core known for $self") 0 0 0
117  
118 0 ▶ croak("Need a coderef") unless ref $updater eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $updater eq "CODE" 0 0 0
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
BranchTrueFalseTotal
my $core = $core_{$self} or croak("No core known for $self") 0 0 0
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__
« XML/Axk/Core.pm ↑ Summary XML/Axk/Vars/Array.pm »