| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package XS::Base; |
|
2
|
1
|
|
|
1
|
|
93043
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
32
|
|
|
3
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
108
|
|
|
4
|
|
|
|
|
|
|
our @EXPORT_OK = qw(has del def clr dump_json load_json strict_mode); |
|
5
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all => \@EXPORT_OK); |
|
6
|
|
|
|
|
|
|
our $VERSION = '1.04'; |
|
7
|
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
4
|
use Exporter 'import'; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
265
|
|
|
9
|
|
|
|
|
|
|
require XSLoader; |
|
10
|
|
|
|
|
|
|
XSLoader::load('XS::Base', $VERSION); |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# strict_mode([$on_off]) -> current_value or set+return |
|
13
|
|
|
|
|
|
|
sub strict_mode { |
|
14
|
0
|
0
|
|
0
|
0
|
|
if (@_) { |
|
15
|
0
|
|
|
|
|
|
my $v = shift; |
|
16
|
0
|
0
|
|
|
|
|
$v = $v ? 1 : 0; |
|
17
|
0
|
|
|
|
|
|
XS::Base::set_strict_mode($v); |
|
18
|
|
|
|
|
|
|
} |
|
19
|
0
|
|
|
|
|
|
return XS::Base::get_strict_mode(); |
|
20
|
|
|
|
|
|
|
} |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# dump_json: 从 XS 取到 root 的引用,序列化后释放该引用 |
|
23
|
|
|
|
|
|
|
sub dump_json { |
|
24
|
0
|
|
|
0
|
0
|
|
require JSON::XS; |
|
25
|
0
|
|
|
|
|
|
my $root = XS::Base::get_root_ref(); # 返回一个 inc'ed hashref scalar |
|
26
|
|
|
|
|
|
|
eval { |
|
27
|
0
|
|
|
|
|
|
my $json = JSON::XS->new->utf8->canonical->encode($root); |
|
28
|
0
|
|
|
|
|
|
XS::Base::_dec_sv($root); # 释放 XS 返回的引用 |
|
29
|
0
|
|
|
|
|
|
return $json; |
|
30
|
0
|
0
|
|
|
|
|
} or do { |
|
31
|
0
|
|
0
|
|
|
|
my $err = $@ || "unknown error"; |
|
32
|
0
|
|
|
|
|
|
XS::Base::_dec_sv($root); |
|
33
|
0
|
|
|
|
|
|
die $err; |
|
34
|
|
|
|
|
|
|
}; |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# load_json: decode 后要求 top-level 是 hashref,并调用 replace_root |
|
38
|
|
|
|
|
|
|
sub load_json { |
|
39
|
0
|
|
|
0
|
0
|
|
my ($json) = @_; |
|
40
|
0
|
|
|
|
|
|
require JSON::XS; |
|
41
|
0
|
|
|
|
|
|
my $perl_struct = JSON::XS->new->utf8->decode($json); |
|
42
|
0
|
0
|
|
|
|
|
unless (ref $perl_struct eq 'HASH') { |
|
43
|
0
|
|
|
|
|
|
die "load_json requires top-level JSON object (hash)"; |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
# replace_root 会在 XS 内部加写锁并 shallow-copy 到内部 root |
|
46
|
0
|
|
|
|
|
|
XS::Base::replace_root($perl_struct); |
|
47
|
0
|
|
|
|
|
|
return 1; |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
1; |
|
51
|
|
|
|
|
|
|
__END__ |