/
B.pm
273 lines (201 loc) · 4.53 KB
/
B.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package Acme::Perl::VM::B;
use strict;
use warnings;
use Exporter qw(import);
use B();
our @EXPORT = grep{ /^[A-Z]/ } @B::EXPORT_OK; # constants
push @EXPORT, qw(sv_undef svref_2object);
B->import(@EXPORT);
unless(defined &OPpPAD_STATE){
constant->import(OPpPAD_STATE => 0x00);
push @EXPORT, qw(OPpPAD_STATE);
}
unless(defined &G_WANT){
constant->import(G_WANT => G_SCALAR() | G_ARRAY() | G_VOID());
push @EXPORT, qw(G_WANT);
}
unless(defined &OPpITER_REVERSED){
constant->import(OPpITER_REVERSED => 0x00);
push @EXPORT, qw(OPpITER_REVERSED);
}
push @EXPORT, qw(NULL TRUE FALSE USE_ITHREADS sv_yes sv_no);
use constant {
NULL => bless(\do{ my $addr = 0 }, 'B::SPECIAL'),
TRUE => 1,
FALSE => 0,
USE_ITHREADS => defined(&B::regex_padav),
sv_yes => B::sv_yes,
sv_no => B::sv_no,
};
package
B::OBJECT;
use B qw(class);
sub dump{
my($obj) = @_;
require Devel::Peek;
Devel::Peek::Dump($obj->object_2svref);
return;
}
package
B::OP;
sub dump{
my($obj) = @_;
require B::Debug;
$obj->debug;
return;
}
package
B::SPECIAL;
my %special_sv = (
${ B::sv_undef() } => \(undef),
${ B::sv_yes() } => \(1 == 1),
${ B::sv_no() } => \(1 != 1),
);
unless(@B::specialsv_name){
@B::specialsv_name = qw(
Nullsv
&PL_sv_undef
&PL_sv_yes
&PL_sv_no
pWARN_ALL
pWARN_NONE
pWARN_STD
);
}
sub object_2svref{
my($obj) = @_;
return $special_sv{ $$obj } || do{
Carp::confess($obj->special_name, ' is not a normal SV object');
};
}
sub setval{
my($obj) = @_;
Acme::Perl::VM::apvm_die('Modification of read-only value (%s) attempted', $obj->special_name);
}
sub STASH(){ undef }
sub POK(){ 0 }
sub ROK(){ 0 }
sub special_name{
my($obj) = @_;
return $B::specialsv_name[$$obj] || sprintf 'SPECIAL(0x%x)', $$obj;
}
package
B::SV;
# for sv_setsv()
sub setsv{
my($dst, $src) = @_;
my $dst_ref = $dst->object_2svref;
${$dst_ref} = ${$src->object_2svref};
bless $dst, ref(B::svref_2object( $dst_ref ));
return $dst;
}
# for sv_setpv()/sv_setiv()/sv_setnv() etc.
sub setval{
my($dst, $val) = @_;
my $dst_ref = $dst->object_2svref;
${$dst_ref} = $val;
bless $dst, ref(B::svref_2object( $dst_ref ));
return $dst;
}
sub clear{
my($sv) = @_;
${$sv->object_2svref} = undef;
return;
}
sub toCV{
my($sv) = @_;
Carp::croak(sprintf 'Cannot convert %s to a CV', B::class($sv));
}
sub STASH(){ undef }
package
B::PVMG;
sub ROK{
my($obj) = @_;
my $dummy = ${ $obj->object_2svref }; # invoke mg_get()
return $obj->SUPER::ROK;
}
package
B::CV;
sub toCV{ $_[0] }
sub clear{
Carp::croak('Cannot clear a CV');
}
sub ROK(){ 0 }
package
B::GV;
sub toCV{ $_[0]->CV }
sub clear{
Carp::croak('Cannot clear a CV');
}
sub ROK(){ 0 }
package
B::AV;
sub setsv{
my($sv) = @_;
Carp::croak('Cannot call setsv() for ' . B::class($sv));
}
sub clear{
my($sv) = @_;
@{$sv->object_2svref} = ();
return;
}
unless(__PACKAGE__->can('OFF')){
# some versions of B::Debug requires this
constant->import(OFF => 0);
}
sub ROK(){ 0 }
package
B::HV;
sub ROK(){ 0 }
*setsv = \&B::AV::setsv;
sub clear{
my($sv) = @_;
%{$sv->object_2svref} = ();
return;
}
sub fetch{
my($hv, $key, $lval) = @_;
if($lval){
return B::svref_2object(\$hv->object_2svref->{$key});
}
else{
my $ref = $hv->object_2svref;
if(exists $ref->{$key}){
return B::svref_2object(\$ref->{$key});
}
else{
return Acme::Perl::VM::B::NULL;
}
}
}
sub fetch_ent{
my($hv, $keysv, $lval) = @_;
return $hv->fetch(${ $keysv->object_2svref }, $lval);
}
sub exists{
my($hv, $key) = @_;
return exists $hv->object_2svref->{$key};
}
sub exists_ent{
my($hv, $keysv) = @_;
return exists $hv->object_2svref->{${ $keysv->object_2svref}};
}
sub store{
my($hv, $key, $val) = @_;
$hv->object_2svref->{$key} = ${ $val->object_2svref };
return B::svref_2object(\$hv->object_2svref->{$key}) if defined wantarray;
}
sub store_ent{
my($hv, $keysv, $val) = @_;
$hv->object_2svref->{${ $keysv->object_2svref }} = ${ $val->object_2svref };
return B::svref_2object(\$hv->object_2svref->{${ $keysv->object_2svref }}) if defined wantarray;
}
1;
__END__
=head1 NAME
Acme::Perl::VM::B - Extra B functions and constants
=head1 SYNOPSIS
use Acme::Perl::VM;
=head1 SEE ALSO
L<Acme::Perl::VM>.
=cut