| 1 | # Copyright 2024-2026 yaml.org | ||||||||||||||||||||||||||||
| 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 YAMLStar; | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use FFI::CheckLib (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use FFI::Platypus; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Cpanel::JSON::XS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | our $VERSION = '0.1.28'; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | our $libyamlstar_version = $VERSION; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 19 | # libyamlstar FFI setup: | ||||||||||||||||||||||||||||
| 20 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Find the proper libyamlstar version: | ||||||||||||||||||||||||||||
| 23 | my $libyamlstar = find_libyamlstar(); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Set up FFI functions: | ||||||||||||||||||||||||||||
| 26 | my $ffi = FFI::Platypus->new( | ||||||||||||||||||||||||||||
| 27 | api => 2, | ||||||||||||||||||||||||||||
| 28 | lib => $libyamlstar, | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | my $graal_create_isolate = $ffi->function( | ||||||||||||||||||||||||||||
| 32 | graal_create_isolate => | ||||||||||||||||||||||||||||
| 33 | ['opaque', 'opaque*', 'opaque*'] => 'int', | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | my $graal_tear_down_isolate = $ffi->function( | ||||||||||||||||||||||||||||
| 37 | graal_tear_down_isolate => | ||||||||||||||||||||||||||||
| 38 | ['opaque'] => 'int', | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | my $yamlstar_load = $ffi->function( | ||||||||||||||||||||||||||||
| 42 | yamlstar_load => | ||||||||||||||||||||||||||||
| 43 | ['sint64', 'string', 'string'] => 'string', | ||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | my $yamlstar_load_all = $ffi->function( | ||||||||||||||||||||||||||||
| 47 | yamlstar_load_all => | ||||||||||||||||||||||||||||
| 48 | ['sint64', 'string', 'string'] => 'string', | ||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | my $yamlstar_dump = $ffi->function( | ||||||||||||||||||||||||||||
| 52 | yamlstar_dump => | ||||||||||||||||||||||||||||
| 53 | ['sint64', 'string', 'string'] => 'string', | ||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | my $yamlstar_dump_all = $ffi->function( | ||||||||||||||||||||||||||||
| 57 | yamlstar_dump_all => | ||||||||||||||||||||||||||||
| 58 | ['sint64', 'string', 'string'] => 'string', | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | my $yamlstar_version = $ffi->function( | ||||||||||||||||||||||||||||
| 62 | yamlstar_version => | ||||||||||||||||||||||||||||
| 63 | ['sint64'] => 'string', | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 67 | # YAMLStar Moo attributes: | ||||||||||||||||||||||||||||
| 68 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | has 'isolatethread' => ( | ||||||||||||||||||||||||||||
| 71 | is => 'rw', | ||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | has 'error' => ( | ||||||||||||||||||||||||||||
| 75 | is => 'rw', | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 79 | # YAMLStar object lifecycle: | ||||||||||||||||||||||||||||
| 80 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # BUILD is called after new() to create the graal isolate: | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 84 | 0 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | my ($isolatethread); | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | $graal_create_isolate->(undef, undef, \$isolatethread) == 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | or die 'Failed to create graal isolate'; | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | $self->isolatethread(\$isolatethread); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # DEMOLISH tears down the graal isolate when the object goes out of scope: | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub DEMOLISH { | ||||||||||||||||||||||||||
|
Subroutine
DEMOLISH: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 95 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if (my $isolatethread = $self->isolatethread) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | $graal_tear_down_isolate->($$isolatethread) == 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | or die "Failed to tear down graal isolate"; | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 104 | # YAMLStar API methods: | ||||||||||||||||||||||||||||
| 105 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # Load a single YAML document: | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 109 | 0 | my ($self, $yaml) = @_; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | $self->error(undef); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | my $resp = Cpanel::JSON::XS::decode_json( | ||||||||||||||||||||||||||||
| 114 | 0 | $yamlstar_load->(${$self->isolatethread}, $yaml, '{}') | |||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | return $resp->{data} if exists $resp->{data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | if ($self->error($resp->{error})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | die "libyamlstar: " . $self->error->{cause}; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | die "Unexpected response from 'libyamlstar'"; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # Load all YAML documents: | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub load_all { | ||||||||||||||||||||||||||
|
Subroutine
load_all: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 128 | 0 | my ($self, $yaml) = @_; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | $self->error(undef); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | my $resp = Cpanel::JSON::XS::decode_json( | ||||||||||||||||||||||||||||
| 133 | 0 | $yamlstar_load_all->(${$self->isolatethread}, $yaml, '{}') | |||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | return $resp->{data} if exists $resp->{data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if ($self->error($resp->{error})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | die "libyamlstar: " . $self->error->{cause}; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | die "Unexpected response from 'libyamlstar'"; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # Dump a single JSON-compatible value: | ||||||||||||||||||||||||||||
| 146 | 0 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 147 | 0 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | $self->error(undef); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | my $resp = Cpanel::JSON::XS::decode_json( | ||||||||||||||||||||||||||||
| 152 | $yamlstar_dump->( | ||||||||||||||||||||||||||||
| 153 | 0 | ${$self->isolatethread}, | |||||||||||||||||||||||||||
| 154 | Cpanel::JSON::XS::encode_json($value), | ||||||||||||||||||||||||||||
| 155 | '{}', | ||||||||||||||||||||||||||||
| 156 | ) | ||||||||||||||||||||||||||||
| 157 | ); | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | return $resp->{data} if exists $resp->{data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | if ($self->error($resp->{error})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | die "libyamlstar: " . $self->error->{cause}; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | die "Unexpected response from 'libyamlstar'"; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Dump all JSON-compatible values: | ||||||||||||||||||||||||||||
| 169 | 0 | ▶ | sub dump_all { | ||||||||||||||||||||||||||
|
Subroutine
dump_all: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 170 | 0 | my ($self, $values) = @_; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | $self->error(undef); | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | my $resp = Cpanel::JSON::XS::decode_json( | ||||||||||||||||||||||||||||
| 175 | $yamlstar_dump_all->( | ||||||||||||||||||||||||||||
| 176 | 0 | ${$self->isolatethread}, | |||||||||||||||||||||||||||
| 177 | Cpanel::JSON::XS::encode_json($values), | ||||||||||||||||||||||||||||
| 178 | '{}', | ||||||||||||||||||||||||||||
| 179 | ) | ||||||||||||||||||||||||||||
| 180 | ); | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | return $resp->{data} if exists $resp->{data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | if ($self->error($resp->{error})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 0 | die "libyamlstar: " . $self->error->{cause}; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | die "Unexpected response from 'libyamlstar'"; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # Get the YAMLStar version: | ||||||||||||||||||||||||||||
| 192 | 0 | ▶ | sub version { | ||||||||||||||||||||||||||
|
Subroutine
version: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 193 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | return $yamlstar_version->(${$self->isolatethread}); | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 199 | # Helper functions: | ||||||||||||||||||||||||||||
| 200 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | # Look for the local libyamlstar first, then look in system paths: | ||||||||||||||||||||||||||||
| 203 | 1 | ▶ | sub find_libyamlstar { | ||||||||||||||||||||||||||
|
Subroutine
find_libyamlstar: called CC=11 SCAR=27.0
| |||||||||||||||||||||||||||||
| 204 | 1 | my $vers = $libyamlstar_version; | |||||||||||||||||||||||||||
| 205 | 1 | ▶ | my $so = $^O eq 'darwin' ? 'dylib' : $^O eq 'MSWin32' ? 'dll' : 'so'; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 206 | 1 | my $name = "libyamlstar.$so.$vers"; | |||||||||||||||||||||||||||
| 207 | 1 | my @paths; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # Add relative path for development: | ||||||||||||||||||||||||||||
| 210 | 1 | ▶ | use File::Basename qw(dirname); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 211 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 212 | 1 | ▶ | use Cwd qw(abs_path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 1 | my $module_file = abs_path(__FILE__); | |||||||||||||||||||||||||||
| 215 | 1 | my $module_dir = dirname($module_file); | |||||||||||||||||||||||||||
| 216 | # Go from perl/lib to libyamlstar/lib | ||||||||||||||||||||||||||||
| 217 | 1 | my $dev_path = File::Spec->catdir($module_dir, File::Spec->updir, File::Spec->updir, 'libyamlstar', 'lib'); | |||||||||||||||||||||||||||
| 218 | 1 | ▶ | $dev_path = abs_path($dev_path) if -d $dev_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 1 | ▶ | push @paths, $dev_path if $dev_path && -d $dev_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$dev_path and -d $dev_path
MC/DC0%0/2
$dev_path and -d $dev_path
$dev_path
-d $dev_path
Truth table33%1/3
A and B
A= $dev_pathB= -d $dev_path
| |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | # Add LD_LIBRARY_PATH (Unix) or PATH (Windows): | ||||||||||||||||||||||||||||
| 222 | 1 | ▶ | if ($^O eq 'MSWin32') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 0 | ▶ | if (my $path = $ENV{PATH}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 0 | push @paths, split /;/, $path; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | } else { | ||||||||||||||||||||||||||||
| 227 | 1 | ▶ | if (my $path = $ENV{LD_LIBRARY_PATH}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 0 | push @paths, split /:/, $path; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | # Add Unix system paths: | ||||||||||||||||||||||||||||
| 231 | 1 | push @paths, qw( | |||||||||||||||||||||||||||
| 232 | /usr/local/lib | ||||||||||||||||||||||||||||
| 233 | /usr/local/lib64 | ||||||||||||||||||||||||||||
| 234 | /usr/lib | ||||||||||||||||||||||||||||
| 235 | /usr/lib64 | ||||||||||||||||||||||||||||
| 236 | ), "$ENV{HOME}/.local/lib"; | ||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 1 | for my $path (@paths) { | |||||||||||||||||||||||||||
| 240 | 5 | ▶ | if (-e "$path/$name") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 0 | return "$path/$name"; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | die <<"..." | ||||||||||||||||||||||||||||
| 246 | Shared library file $name not found | ||||||||||||||||||||||||||||
| 247 | Search paths: @paths | ||||||||||||||||||||||||||||
| 248 | Build with: cd libyamlstar && make native | ||||||||||||||||||||||||||||
| 249 | ... | ||||||||||||||||||||||||||||
| 250 | 1 | } | |||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 1; | ||||||||||||||||||||||||||||