YAMLScript.pm

filter: Statementstmt 66.6% 26 / 39 Branchbran 33.3% 6 / 18 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 71.4% 5 / 7 Podpod n/a totaltotal 51.3% 37 / 72 CC 16 SCAR 32.0
CC
16
Cov
52%
find_libys
36.7
new
6.0
DESTROY
6.0
CRAP 24.6
↑ Summary YAMLScript/Alien.pm »
1 # Copyright 2023-2026 Ingy dot Net
2 # This code is licensed under MIT license (See License for details)
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package YAMLScript;
8  
9 1 ▶use FFI::CheckLib ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use FFI::Platypus;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 my $json = eval {
13 require Cpanel::JSON::XS;
14 Cpanel::JSON::XS->new;
15 } || do {
16 require JSON::PP;
17 JSON::PP->new;
18 };
19  
20 our $VERSION = '0.3.7';
21  
22 our $libys_version = $VERSION;
23  
24  
25 #------------------------------------------------------------------------------
26 # libys FFI setup:
27 #------------------------------------------------------------------------------
28  
29 # Find the proper libys version:
30 my $libys = find_libys();
31  
32 # Set up FFI functions:
33 my $ffi = FFI::Platypus->new(
34 api => 2,
35 lib => $libys,
36 );
37  
38 my $graal_create_isolate = $ffi->function(
39 graal_create_isolate =>
40 ['opaque', 'opaque*', 'opaque*'] => 'int',
41 );
42  
43 my $graal_tear_down_isolate = $ffi->function(
44 graal_tear_down_isolate =>
45 ['opaque'] => 'int',
46 );
47  
48  
49 #------------------------------------------------------------------------------
50 # YAMLScript object constructor and destructor:
51 #------------------------------------------------------------------------------
52  
53 # YAMLScript object constuctor. Creates and saves a graal isolate thread:
54 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
55 0 my ($class, $config) = (@_, {});
56 0 my ($isolatethread);
57 0 ▶ $graal_create_isolate->(undef, undef, \$isolatethread) == 0
Branch: 1 branch
BranchTrueFalseTotal
&$graal_create_isolate(undef, undef, \$isolatethread) == 0 or die "Failed to create graal isolate" 0 0 0
58 or die 'Failed to create graal isolate';
59 0 bless {
60 isolatethread => \$isolatethread,
61 }, $class;
62 }
63  
64 # Tear down the graal isolate when the YAMLScript object goes out of scope:
65 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
66 0 my ($self) = @_;
67 0 ▶ $graal_tear_down_isolate->(${$self->{isolatethread}}) == 0
Branch: 1 branch
BranchTrueFalseTotal
&$graal_tear_down_isolate(${$$self{"isolatethread"};}) == 0 or die "Failed to tear down graal isolate" 0 0 0
68 or die "Failed to tear down graal isolate";
69 }
70  
71 #------------------------------------------------------------------------------
72 # YAMLScript API methods:
73 #------------------------------------------------------------------------------
74 sub load;
75 # "load" method wrapper for FFI.
76 # It calls the libys load_ys_to_json function.
77 $ffi->attach(
78 [load_ys_to_json => 'load'] =>
79 ['sint64', 'string'] => 'string' =>
80 sub {
81 my ($xsub, $self, $ys) = @_;
82 $self->{error} = undef;
83  
84 my $resp = $json->decode($xsub->(${$self->{isolatethread}}, $ys));
85  
86 return $resp->{data} if exists $resp->{data};
87  
88 if ($self->{error} = $resp->{error}) {
89 die "libys: $self->{error}{cause}";
90 }
91  
92 die "Unexpected response from 'libys'";
93 },
94 );
95  
96 #------------------------------------------------------------------------------
97 # Helper functions:
98 #------------------------------------------------------------------------------
99 # Look for the local libys first, then look for the Alien version:
100 1 ▶sub find_libys {
Subroutine find_libys: called CC=14 SCAR=36.0
101 1 my $vers = $libys_version;
102 1 my $win = $^O eq 'MSWin32';
103 1 ▶ my $so = $win ? 'dll' : $^O eq 'darwin' ? 'dylib' : 'so';
Branch: 2 branches
BranchTrueFalseTotal
$win ? : 0 1 1
$^O eq 'darwin' ? : 0 1 1
104 1 ▶ my @names = $win ? 'libys.dll' : "libys.$so.$vers";
Branch: 1 branch
BranchTrueFalseTotal
$win ? : 0 1 1
105 1 my @paths;
106 1 ▶ if ($win) {
Branch: 2 branches
BranchTrueFalseTotal
if ($win) { } 0 1 1
elsif (my $path = $ENV{'LD_LIBRARY_PATH'}) { } 0 1 1
107 0 ▶ @paths = split /;/, $ENV{PATH} || '';
Condition0%0/2
$ENV{'PATH'} || ''
l!l
00
MC/DC0%0/1
$ENV{'PATH'} || ''
$ENV{'PATH'}
Truth table0%0/2
A
A= $ENV{'PATH'}
Aresult
11
00
108 }
109 elsif (my $path = $ENV{LD_LIBRARY_PATH}) {
110 0 @paths = split /:/, $path;
111 }
112 1 push @paths, qw(
113 /usr/local/lib
114 /usr/local/lib64
115 /usr/lib
116 /usr/lib64
117 ), "$ENV{HOME}/.local/lib";
118 1 for my $path (@paths) {
119 5 for my $name (@names) {
120 5 ▶ if (-e "$path/$name") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$path/$name") 0 5 5
121 0 return "$path/$name";
122 }
123 }
124 }
125  
126 1 require Alien::YAMLScript;
127  
128 1 for my $path (Alien::YAMLScript->dynamic_libs) {
129 0 for my $name (@names) {
130 0 ▶ if ($path =~ /\Q$name\E$/ && -r $path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /\Q$name\E$/ and -r $path) 0 0 0
Condition0%0/3
$path =~ /\Q$name\E$/ and -r $path
!ll&&!rl&&r
000
MC/DC0%0/2
$path =~ /\Q$name\E$/ and -r $path
$path =~ /\Q$name\E$/ -r $path
Truth table0%0/3
A and B
A= $path =~ /\Q$name\E$/B= -r $path
ABresult
0X0
100
111
131 0 return $path;
132 }
133 }
134 }
135  
136 die <<"..."
137 1 Shared library file @{[join ', ', @names]} not found
138 Try: source <(curl -sL https://in-1.cc) --local libys LIBYS-VERSION=$vers
139 See: https://github.com/yaml/yamlscript/wiki/Installing-YAMLScript
140 ...
141 1 }
142  
143 1;
↑ Summary YAMLScript/Alien.pm »