| 1 | # Copyright 2024-2026 yaml.org | ||
| 2 | # This code is licensed under MIT license (See License for details) | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | package YAMLStar; | ||
| 8 | |||
| 9 | use Moo; | ||
| 10 | use FFI::CheckLib (); | ||
| 11 | use FFI::Platypus; | ||
| 12 | use Cpanel::JSON::XS (); | ||
| 13 | |||
| 14 | our $VERSION = '0.1.25'; | ||
| 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 | sub BUILD { | ||
| 84 | my ($self, $args) = @_; | ||
| 85 | |||
| 86 | my ($isolatethread); | ||
| 87 | $graal_create_isolate->(undef, undef, \$isolatethread) == 0 | ||
| 88 | or die 'Failed to create graal isolate'; | ||
| 89 | |||
| 90 | $self->isolatethread(\$isolatethread); | ||
| 91 | } | ||
| 92 | |||
| 93 | # DEMOLISH tears down the graal isolate when the object goes out of scope: | ||
| 94 | sub DEMOLISH { | ||
| 95 | my ($self) = @_; | ||
| 96 | |||
| 97 | if (my $isolatethread = $self->isolatethread) { | ||
| 98 | $graal_tear_down_isolate->($$isolatethread) == 0 | ||
| 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 | sub load { | ||
| 109 | my ($self, $yaml) = @_; | ||
| 110 | |||
| 111 | $self->error(undef); | ||
| 112 | |||
| 113 | my $resp = Cpanel::JSON::XS::decode_json( | ||
| 114 | $yamlstar_load->(${$self->isolatethread}, $yaml, '{}') | ||
| 115 | ); | ||
| 116 | |||
| 117 | return $resp->{data} if exists $resp->{data}; | ||
| 118 | |||
| 119 | if ($self->error($resp->{error})) { | ||
| 120 | die "libyamlstar: " . $self->error->{cause}; | ||
| 121 | } | ||
| 122 | |||
| 123 | die "Unexpected response from 'libyamlstar'"; | ||
| 124 | } | ||
| 125 | |||
| 126 | # Load all YAML documents: | ||
| 127 | sub load_all { | ||
| 128 | my ($self, $yaml) = @_; | ||
| 129 | |||
| 130 | $self->error(undef); | ||
| 131 | |||
| 132 | my $resp = Cpanel::JSON::XS::decode_json( | ||
| 133 | $yamlstar_load_all->(${$self->isolatethread}, $yaml, '{}') | ||
| 134 | ); | ||
| 135 | |||
| 136 | return $resp->{data} if exists $resp->{data}; | ||
| 137 | |||
| 138 | if ($self->error($resp->{error})) { | ||
| 139 | die "libyamlstar: " . $self->error->{cause}; | ||
| 140 | } | ||
| 141 | |||
| 142 | die "Unexpected response from 'libyamlstar'"; | ||
| 143 | } | ||
| 144 | |||
| 145 | # Dump a single JSON-compatible value: | ||
| 146 | sub dump { | ||
| 147 | my ($self, $value) = @_; | ||
| 148 | |||
| 149 | $self->error(undef); | ||
| 150 | |||
| 151 | my $resp = Cpanel::JSON::XS::decode_json( | ||
| 152 | $yamlstar_dump->( | ||
| 153 | ${$self->isolatethread}, | ||
| 154 | Cpanel::JSON::XS::encode_json($value), | ||
| 155 | '{}', | ||
| 156 | ) | ||
| 157 | ); | ||
| 158 | |||
| 159 | return $resp->{data} if exists $resp->{data}; | ||
| 160 | |||
| 161 | if ($self->error($resp->{error})) { | ||
| 162 | die "libyamlstar: " . $self->error->{cause}; | ||
| 163 | } | ||
| 164 | |||
| 165 | die "Unexpected response from 'libyamlstar'"; | ||
| 166 | } | ||
| 167 | |||
| 168 | # Dump all JSON-compatible values: | ||
| 169 | sub dump_all { | ||
| 170 | my ($self, $values) = @_; | ||
| 171 | |||
| 172 | $self->error(undef); | ||
| 173 | |||
| 174 | my $resp = Cpanel::JSON::XS::decode_json( | ||
| 175 | $yamlstar_dump_all->( | ||
| 176 | ${$self->isolatethread}, | ||
| 177 | Cpanel::JSON::XS::encode_json($values), | ||
| 178 | '{}', | ||
| 179 | ) | ||
| 180 | ); | ||
| 181 | |||
| 182 | return $resp->{data} if exists $resp->{data}; | ||
| 183 | |||
| 184 | if ($self->error($resp->{error})) { | ||
| 185 | die "libyamlstar: " . $self->error->{cause}; | ||
| 186 | } | ||
| 187 | |||
| 188 | die "Unexpected response from 'libyamlstar'"; | ||
| 189 | } | ||
| 190 | |||
| 191 | # Get the YAMLStar version: | ||
| 192 | sub version { | ||
| 193 | my ($self) = @_; | ||
| 194 | |||
| 195 | 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 | sub find_libyamlstar { | ||
| 204 | my $vers = $libyamlstar_version; | ||
| 205 | my $so = $^O eq 'darwin' ? 'dylib' : $^O eq 'MSWin32' ? 'dll' : 'so'; | ||
| 206 | my $name = "libyamlstar.$so.$vers"; | ||
| 207 | my @paths; | ||
| 208 | |||
| 209 | # Add relative path for development: | ||
| 210 | use File::Basename qw(dirname); | ||
| 211 | use File::Spec; | ||
| 212 | use Cwd qw(abs_path); | ||
| 213 | |||
| 214 | my $module_file = abs_path(__FILE__); | ||
| 215 | my $module_dir = dirname($module_file); | ||
| 216 | # Go from perl/lib to libyamlstar/lib | ||
| 217 | my $dev_path = File::Spec->catdir($module_dir, File::Spec->updir, File::Spec->updir, 'libyamlstar', 'lib'); | ||
| 218 | $dev_path = abs_path($dev_path) if -d $dev_path; | ||
| 219 | push @paths, $dev_path if $dev_path && -d $dev_path; | ||
| 220 | |||
| 221 | # Add LD_LIBRARY_PATH (Unix) or PATH (Windows): | ||
| 222 | if ($^O eq 'MSWin32') { | ||
| 223 | if (my $path = $ENV{PATH}) { | ||
| 224 | push @paths, split /;/, $path; | ||
| 225 | } | ||
| 226 | } else { | ||
| 227 | if (my $path = $ENV{LD_LIBRARY_PATH}) { | ||
| 228 | push @paths, split /:/, $path; | ||
| 229 | } | ||
| 230 | # Add Unix system paths: | ||
| 231 | 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 | for my $path (@paths) { | ||
| 240 | if (-e "$path/$name") { | ||
| 241 | 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 | } | ||
| 251 | |||
| 252 | 1; |