blib/lib/SPVM/Global.pm

filter: Statementstmt 95.3% 508 / 533 Branchbran 81.1% 146 / 180 Conditioncond 50.0% 8 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 91.4% 43 / 47 Podpod 0.0% 0 / 18 totaltotal 87.6% 705 / 804 CC 117 SCAR 21.3
CC
117
Cov
91%
build_dynamic_libs
19.0
build_dynamic_libs
19.0
__ANON__
8.8
CRAP 8.4
« blib/lib/SPVM/Builder/Util.pm ↑ Summary blib/lib/SPVM/Builder/Native/Runtime.pm »
1 package SPVM::Global;
2 536 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 536 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 536 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 536 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 536 ▶use File::Path 'rmtree';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 536 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 536 ▶use Fcntl qw(O_RDONLY);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 536 ▶use SPVM::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 536 ▶use SPVM::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 536 ▶use SPVM::Builder::CC;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 536 ▶use SPVM::Builder::Native::Env;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 536 ▶use SPVM::ExchangeAPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 536 ▶use SPVM::BlessedObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 536 ▶use SPVM::BlessedObject::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 536 ▶use SPVM::BlessedObject::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 536 ▶use SPVM::BlessedObject::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 536 ▶use SPVM::ExchangeAPI::CallerInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 790 my $COMPILER;
23 my $API;
24 790 my $BUILDER;
25 776 my $DYNAMIC_LIB_LIBREFS_H = {};
26 773 my $DYNAMIC_LIB_FILES_H = {};
27 780 my $DYNAMIC_LIB_FILE_IS_JIT_H = {};
28  
29 # Accessed from SPVM::Builder
30 780 our $BUILD_TMP_DIR;
31  
32 451 ▶END {
Subroutine END: called CC=4 SCAR=13.9
33
34 458 ▶ if ($API) {
Subroutine END: called CC=4 SCAR=13.9
Branch: 2 branches
BranchTrueFalseTotal
if ($API) 172 56 228
if ($API) 158 12 170
35 {
36 # Remove circular reference
37 402 my $env = delete $API->{env};
38 402 my $stack = delete $API->{stack};
39
40 402 $env->call_end_methods($stack);
41
42 402 $env->destroy_class_vars($stack);
43 }
44
45 0 $API = undef;
46
47 332 $COMPILER = undef;
48
49 316 for my $dynamic_lib_file (keys %$DYNAMIC_LIB_LIBREFS_H) {
50 826 my $dynamic_lib_libref = $DYNAMIC_LIB_LIBREFS_H->{$dynamic_lib_file};
51 826 DynaLoader::dl_unload_file($dynamic_lib_libref);
52 }
53
54 316 $BUILDER = undef;
55
56 316 ▶ if (defined $BUILD_TMP_DIR) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $SPVM::Global::BUILD_TMP_DIR) 85 77 162
if (defined $SPVM::Global::BUILD_TMP_DIR) 142 16 158
57 246 rmtree $BUILD_TMP_DIR;
58 }
59 }
60 }
61  
62 1293 ▶sub api {
Subroutine api: called CC=2 SCAR=7.0
Pod undocumented
63
64 1053 ▶ unless ($API) {
Subroutine api: called CC=2 SCAR=7.0
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
unless ($API) 3 1050 1053
unless ($API) 0 4967 4967
65 3 &init_api();
66 }
67 1053 return $API;
68 }
69  
70 694 ▶sub build_class {
Subroutine build_class: called CC=1 SCAR=0.0
Pod undocumented
71 448 ▶ my ($class_name, $file, $line) = @_;
Subroutine build_class: called CC=1 SCAR=0.0
Pod undocumented
72
73 448 my $options = {
74 class_name => $class_name,
75 file => $file,
76 line => $line,
77 };
78
79 448 &build_class_common($options);
80 }
81  
82 248 ▶sub build_anon_class {
Subroutine build_anon_class: called CC=1 SCAR=0.0
Pod undocumented
83 8 ▶ my ($source, $file, $line) = @_;
Subroutine build_anon_class: called CC=1 SCAR=0.0
Pod undocumented
84
85 8 my $options = {
86 source => $source,
87 file => $file,
88 line => $line,
89 };
90
91 8 my $anon_class_name = &build_class_common($options);
92
93 7 return $anon_class_name;
94 }
95  
96 690 ▶sub build_class_common {
Subroutine build_class_common: called CC=7 SCAR=19.5
Pod undocumented
97 453 ▶ my ($options) = @_;
Subroutine build_class_common: called CC=7 SCAR=19.5
Pod undocumented
98
99 453 ▶ $options ||= {};
Condition50%2/4
$options ||= {}
l!l
4530
$options ||= {}
l!l
2290
MC/DC0%0/1
$options ||= {}
$options
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
Truth table50%1/2
A
A= $options
Aresult
11
00
100
101 453 my $class_name = $options->{class_name};
102
103 452 my $source = $options->{source};
104
105 459 my $file = $options->{file};
106
107 450 my $line = $options->{line};
108
109 459 &init_api();
110
111 # Add module informations
112 453 my $build_success;
113 453 ▶ if (defined $class_name || defined $source) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $class_name or defined $source) 452 1 453
if (defined $class_name or defined $source) 229 0 229
Condition66%4/6
defined $class_name or defined $source
l!l&&r!l&&!r
44850
defined $class_name or defined $source
l!l&&r!l&&!r
22810
MC/DC0%0/2
defined $class_name or defined $source
defined $class_name defined $source
MC/DC0%0/2
defined $class_name or defined $source
defined $class_name defined $source
Truth table66%2/3
A or B
A= defined $class_nameB= defined $source
ABresult
1X1
011
000
Truth table66%2/3
A or B
A= defined $class_nameB= defined $source
ABresult
1X1
011
000
114
115 453 my $api = $API;
116 450 my $compiler = $COMPILER;
117
118 450 my $env = $api->env;
119
120 450 my $runtime = $compiler->get_runtime;
121 450 my $start_basic_types_length = $runtime->get_basic_types_length;
122
123 453 ▶ if (defined $source) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $source) { } 5 454 459
if (defined $source) { } 1 228 229
124 8 $class_name = $compiler->compile_anon_class_with_exit($source, $file, $line);
125 }
126 else {
127 448 $compiler->compile_with_exit($class_name, $file, $line);
128 }
129
130 452 my $basic_types_length = $runtime->get_basic_types_length;
131
132 452 my $class_names = [];
133 452 ▶ for (my $basic_type_id = $start_basic_types_length; $basic_type_id < $basic_types_length; $basic_type_id++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($basic_type_id < $basic_types_length) 6364 449 6813
if ($basic_type_id < $basic_types_length) 4507 227 4734
134 6364 my $basic_type = $runtime->get_basic_type_by_id($basic_type_id);
135 6364 push @$class_names, $basic_type->get_name;
136 }
137
138 452 &build_dynamic_libs($runtime, $class_names);
139
140 204 ▶ for (my $basic_type_id = $start_basic_types_length; $basic_type_id < $basic_types_length; $basic_type_id++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($basic_type_id < $basic_types_length) 2919 204 3123
if ($basic_type_id < $basic_types_length) 3672 153 3825
141 2922 my $basic_type = $runtime->get_basic_type_by_id($basic_type_id);
142 2922 my $class_name = $basic_type->get_name;
143
144 2922 &load_dynamic_lib($runtime, $class_name);
145 }
146
147 204 my $stack = $api->stack;
148
149 204 $env->call_init_methods($stack);
150
151 201 &bind_to_perl($class_name);
152 }
153
154 204 return $class_name;
155 }
156  
157 1129 ▶sub init_api {
Subroutine init_api: called CC=7 SCAR=19.5
Pod undocumented
158 889 ▶ unless ($API) {
Subroutine init_api: called CC=7 SCAR=19.5
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
unless ($API) 405 490 895
unless ($API) 228 239 467
159 402 ▶ $BUILDER //= SPVM::Builder->new(is_jit => 1);
Condition33%2/6
$BUILDER //= "SPVM::Builder"->new("is_jit", 1)
l!l&&r!l&&!r
04020
$BUILDER //= "SPVM::Builder"->new("is_jit", 1)
l!l&&r!l&&!r
02280
MC/DC0%0/2
$BUILDER //= "SPVM::Builder"->new("is_jit", 1)
$BUILDER "SPVM::Builder"->new("is_jit", 1)
MC/DC0%0/2
$BUILDER //= "SPVM::Builder"->new("is_jit", 1)
$BUILDER "SPVM::Builder"->new("is_jit", 1)
Truth table33%1/3
A //= B
A= $BUILDERB= "SPVM::Builder"->new("is_jit", 1)
ABresult
1X1
011
000
Truth table33%1/3
A //= B
A= $BUILDERB= "SPVM::Builder"->new("is_jit", 1)
ABresult
1X1
011
000
160
161 402 my $compiler = SPVM::Builder::Native::Compiler->new;
162
163 405 for my $class_search_dir (@{$BUILDER->class_search_dirs}) {
164 5872 $compiler->add_class_search_dir($class_search_dir);
165 }
166
167 402 my $runtime = $compiler->get_runtime;
168
169 405 my $start_basic_types_length = $runtime->get_basic_types_length;
170
171 405 $compiler->compile_with_exit(undef, __FILE__, __LINE__);
172
173 405 my $basic_types_length = $runtime->get_basic_types_length;
174
175 402 my $class_names = [];
176 402 ▶ for (my $basic_type_id = $start_basic_types_length; $basic_type_id < $basic_types_length; $basic_type_id++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($basic_type_id < $basic_types_length) 12375 402 12777
if ($basic_type_id < $basic_types_length) 7068 228 7296
177 12372 my $basic_type = $runtime->get_basic_type_by_id($basic_type_id);
178 12372 push @$class_names, $basic_type->get_name;
179 }
180
181 405 &build_dynamic_libs($runtime, $class_names);
182
183 402 ▶ for (my $basic_type_id = $start_basic_types_length; $basic_type_id < $basic_types_length; $basic_type_id++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($basic_type_id < $basic_types_length) 12372 402 12774
if ($basic_type_id < $basic_types_length) 7068 228 7296
184 12372 my $basic_type = $runtime->get_basic_type_by_id($basic_type_id);
185 12372 my $class_name = $basic_type->get_name;
186 12372 &load_dynamic_lib($runtime, $class_name);
187 }
188
189 429 my $env = $runtime->get_env;
190
191 402 my $stack = $env->new_stack;
192
193 402 my $api = SPVM::ExchangeAPI->new(env => $env, stack => $stack);
194
195 402 $COMPILER = $compiler;
196 402 $API = $api;
197
198 402 $env->set_command_info_program_name($stack, $0);
199
200 402 $env->set_command_info_argv($stack, \@ARGV);
201 492 my $base_time = $^T + 0; # For Perl 5.8.9
202 492 $env->set_command_info_basetime($stack, $base_time);
203
204 402 $env->set_command_info_basetime($stack, $base_time);
205
206 402 ▶ my $warning = $^W ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$^W ? : 93 402 495
$^W ? : 0 228 228
207 492 $env->set_command_info_warning($stack, $warning);
208 }
209 }
210  
211 1094 ▶sub build_dynamic_libs {
Subroutine build_dynamic_libs: called CC=18 SCAR=29.4
Pod undocumented
212 941 ▶ my ($runtime, $class_names) = @_;
Subroutine build_dynamic_libs: not called CC=18 SCAR=29.4
Pod undocumented
213
214 941 my $build_options = {};
215 851 $build_options->{precompile_classes} = [];
216 851 $build_options->{native_classes} = [];
217
218 851 for my $class_name (@$class_names) {
219 18733 ▶ if ($class_name =~ /::anon_method::/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($class_name =~ /::anon_method::/) 2117 16709 18826
if ($class_name =~ /::anon_method::/) 1333 10242 11575
220 0 next;
221 }
222
223 16709 for my $category ('precompile', 'native') {
224 33415 my $basic_type = $runtime->get_basic_type_by_name($class_name);
225 33415 my $method_names = $basic_type->get_method_names_by_category($category);
226
227 33415 ▶ if (@$method_names) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$method_names) 1596 31819 33415
if (@$method_names) 852 19632 20484
228 1599 ▶ unless ($DYNAMIC_LIB_FILES_H->{$class_name}{$category}) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($DYNAMIC_LIB_FILES_H->{$class_name}{$category}) 1596 99 1695
unless ($DYNAMIC_LIB_FILES_H->{$class_name}{$category}) 852 0 852
229 1599 my $class_file = $basic_type->get_class_file;
230 1599 my $class_file_from_inc = SPVM::Builder::Util::search_spvm_file($class_name);
231
232 0 ▶ unless ($class_file eq $class_file_from_inc) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($class_file eq $class_file_from_inc) 3 1791 1794
unless ($class_file eq $class_file_from_inc) 0 852 852
233 0 Carp::confess("The loaded class file is different from the file found in \@INC. \$loaded_class_file='$class_file', \$found_class_file='$class_file_from_inc'");
234 }
235
236 0 my $link_to_class_file;
237 0 ▶ if ($category eq 'native') {
Branch: 2 branches
BranchTrueFalseTotal
if ($category eq "native") 754 845 1599
if ($category eq "native") 297 555 852
238 0 my $config_file = SPVM::Builder::Config::Util::create_config_file_path_from_class_file_path($class_file);
239 0 ▶ if (SPVM::Builder::Config::Util::exists_config_file($config_file)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SPVM::Builder::Config::Util::exists_config_file($config_file)) 751 3 754
if (SPVM::Builder::Config::Util::exists_config_file($config_file)) 297 0 297
240 0 my $config = SPVM::Builder::Config::Util::load_config($config_file);
241 850 my $link_to_class_name = $config->link_to;
242 949 ▶ if ($link_to_class_name) {
Branch: 2 branches
BranchTrueFalseTotal
if ($link_to_class_name) 22 732 754
if ($link_to_class_name) 0 297 297
243 220 my $link_to_basic_type = $runtime->get_basic_type_by_name($link_to_class_name);
244 220 ▶ unless ($link_to_basic_type) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($link_to_basic_type) 0 22 22
unless ($link_to_basic_type) 0 0 0
245 3 Carp::confess("$link_to_class_name class is not yet loaded.");
246 }
247 25 $link_to_class_file = $link_to_basic_type->get_class_file;
248 25 my $link_to_class_file_from_inc = SPVM::Builder::Util::search_spvm_file($link_to_class_name);
249
250 25 ▶ unless ($link_to_class_file eq $link_to_class_file_from_inc) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($link_to_class_file eq $link_to_class_file_from_inc) 0 22 22
unless ($link_to_class_file eq $link_to_class_file_from_inc) 0 0 0
251 0 Carp::confess("The loaded class file is different from the file found in \@INC. \$loaded_class_file='$link_to_class_file', \$found_class_file='$link_to_class_file_from_inc'");
252 }
253 }
254 }
255 }
256
257 1599 ▶ unless (defined $link_to_class_file) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $link_to_class_file) 1574 22 1596
unless (defined $link_to_class_file) 852 0 852
258 1577 $link_to_class_file = $class_file;
259 }
260
261 1596 my $dynamic_lib_file_dist = SPVM::Builder::Util::get_dynamic_lib_file_dist($link_to_class_file, $category);
262
263 1596 ▶ if (-f $dynamic_lib_file_dist) {
Branch: 4 branches
BranchTrueFalseTotal
if (-f $dynamic_lib_file_dist) { } 1091 505 1596
elsif (my $dynamic_lib_file_jit = $DYNAMIC_LIB_FILES_H->{$class_name}{$category}) { } 3 505 508
if (-f $dynamic_lib_file_dist) { } 702 150 852
elsif (my $dynamic_lib_file_jit = $DYNAMIC_LIB_FILES_H->{$class_name}{$category}) { } 0 150 150
264 1091 $DYNAMIC_LIB_FILES_H->{$class_name}{$category} = $dynamic_lib_file_dist;
265 }
266 elsif (my $dynamic_lib_file_jit = $DYNAMIC_LIB_FILES_H->{$class_name}{$category}) {
267 # Do nothing
268 }
269 else {
270 505 push @{$build_options->{"${category}_classes"}}, $class_name;
271 }
272 }
273 }
274 }
275 }
276
277 848 my $builder_cc = SPVM::Builder::CC->new(builder => $BUILDER, runtime => $runtime);
278 848 my $output_files_h = $builder_cc->build_parallel($build_options);
279
280 600 for my $category (keys %{$output_files_h}) {
281 108 for my $class_name (%{$output_files_h->{$category}}) {
282 234 my $dynamic_lib_file_jit = $output_files_h->{$category}{$class_name};
283 237 ▶ if ($dynamic_lib_file_jit) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dynamic_lib_file_jit) 117 120 237
if ($dynamic_lib_file_jit) 16 16 32
284 120 $DYNAMIC_LIB_FILES_H->{$class_name}{$category} = $dynamic_lib_file_jit;
285 120 $DYNAMIC_LIB_FILE_IS_JIT_H->{$dynamic_lib_file_jit} = 1;
286 }
287 }
288 }
289
290 }
291  
292 15534 ▶sub load_dynamic_lib {
Subroutine load_dynamic_lib: called CC=8 SCAR=20.8
Pod undocumented
293 15291 ▶ my ($runtime, $class_name) = @_;
Subroutine load_dynamic_lib: not called CC=8 SCAR=20.8
Pod undocumented
294
295 15288 my $outmost_class_name;
296 15291 ▶ if ($class_name =~ /^(.*)::anon_method::/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($class_name =~ /^(.*)::anon_method::/) { } 948 14343 15291
if ($class_name =~ /^(.*)::anon_method::/) { } 1095 9645 10740
297 951 $outmost_class_name = $1;
298 }
299 else {
300 14346 $outmost_class_name = $class_name;
301 }
302
303 15294 for my $category ('precompile', 'native') {
304 30582 my $basic_type = $runtime->get_basic_type_by_name($class_name);
305
306 30582 my $method_names = $basic_type->get_method_names_by_category($category);
307
308 30578 ▶ if (@$method_names) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$method_names) 708 29874 30582
if (@$method_names) 618 20862 21480
309 707 ▶ if (-f $DYNAMIC_LIB_FILES_H->{$outmost_class_name}{$category}) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $DYNAMIC_LIB_FILES_H->{$outmost_class_name}{$category}) 705 99 804
if (-f $DYNAMIC_LIB_FILES_H->{$outmost_class_name}{$category}) 618 0 618
310 711 my $method_addresses = &get_method_addresses($DYNAMIC_LIB_FILES_H->{$outmost_class_name}{$category}, $class_name, $method_names, $category);
311
312 711 for my $method_name (sort keys %$method_addresses) {
313 10044 my $cfunc_address = $method_addresses->{$method_name};
314 10044 my $method = $basic_type->get_method_by_name($method_name);
315 10140 ▶ if ($category eq 'precompile') {
Branch: 4 branches
BranchTrueFalseTotal
if ($category eq 'precompile') { } 4763 5476 10239
elsif ($category eq 'native') { } 5284 0 5284
if ($category eq 'precompile') { } 5984 5540 11524
elsif ($category eq 'native') { } 5540 0 5540
316 4859 $method->set_precompile_address($cfunc_address);
317 }
318 elsif ($category eq 'native') {
319 5380 $method->set_native_address($cfunc_address);
320 }
321 }
322 }
323 }
324 }
325 }
326  
327 1044 ▶sub get_method_addresses {
Subroutine get_method_addresses: called CC=4 SCAR=14.2
Pod undocumented
328 705 ▶ my ($dynamic_lib_file, $class_name, $method_names, $category) = @_;
Subroutine get_method_addresses: called CC=4 SCAR=14.2
Pod undocumented
329
330 804 my $method_addresses = {};
331 804 ▶ if (@$method_names) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$method_names) 725 0 725
if (@$method_names) 618 0 618
332 903 my $method_infos = [];
333 903 for my $method_name (@$method_names) {
334 10239 my $method_info = {};
335 10044 $method_info->{class_name} = $class_name;
336 10044 $method_info->{method_name} = $method_name;
337 10044 push @$method_infos, $method_info;
338 }
339
340 725 for my $method_info (@$method_infos) {
341 10061 my $class_name = $method_info->{class_name};
342 0 my $method_name = $method_info->{method_name};
343
344 10284 ▶ $BUILDER->global_file_lock(sub {
Subroutine __ANON__: called CC=8 SCAR=21.7
345 10044 ▶ my $cfunc_address;
Subroutine __ANON__: not called CC=8 SCAR=21.7
346 0 ▶ if ($dynamic_lib_file) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dynamic_lib_file) { } 10041 0 10041
if ($dynamic_lib_file) { } 11524 0 11524
347 10044 my $dynamic_lib_libref;
348 10044 ▶ if ($DYNAMIC_LIB_LIBREFS_H->{$dynamic_lib_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($DYNAMIC_LIB_LIBREFS_H->{$dynamic_lib_file}) { } 9361 683 10044
if ($DYNAMIC_LIB_LIBREFS_H->{$dynamic_lib_file}) { } 10906 618 11524
349 9361 $dynamic_lib_libref = $DYNAMIC_LIB_LIBREFS_H->{$dynamic_lib_file};
350 }
351 else {
352 # Load dynamic library with a shared lock to prevent race conditions during parallel builds
353 703 ▶ unless (defined $ENV{SPVM_BUILD_DIR}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $ENV{"SPVM_BUILD_DIR"}) 20 683 703
unless (defined $ENV{"SPVM_BUILD_DIR"}) 0 618 618
354 20 confess("[Unexpected Error]no build directory");
355 }
356
357 703 ▶ unless (-f $dynamic_lib_file) {
Branch: 2 branches
BranchTrueFalseTotal
unless (-f $dynamic_lib_file) 17 686 703
unless (-f $dynamic_lib_file) 0 618 618
358 20 confess("\$dynamic_lib_file does not exist.");
359 }
360
361 0 ▶ unless (sysopen(my $dynamic_lib_fh, $dynamic_lib_file, O_RDONLY)) {
Branch: 2 branches
BranchTrueFalseTotal
unless (sysopen my $dynamic_lib_fh, $dynamic_lib_file, 0) 0 686 686
unless (sysopen my $dynamic_lib_fh, $dynamic_lib_file, 0) 0 618 618
362 0 confess("\$dynamic_lib_file cannot be opened O_RDONLY.");
363 }
364
365 # On Windows, using an absolute path for LoadLibrary (via DynaLoader) is crucial.
366 # Without it, the OS may trigger its standard search strategy, potentially
367 # loading an unintended DLL with the same base name from other directories.
368 0 my $dynamic_lib_file_abs = File::Spec->rel2abs($dynamic_lib_file);
369
370 0 $dynamic_lib_libref = DynaLoader::dl_load_file($dynamic_lib_file_abs);
371
372 0 $DYNAMIC_LIB_LIBREFS_H->{$dynamic_lib_file} = $dynamic_lib_libref;
373 }
374
375 0 ▶ if ($dynamic_lib_libref) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dynamic_lib_libref) { } 10041 3 10044
if ($dynamic_lib_libref) { } 11524 0 11524
376  
377 10061 my $cfunc_name = SPVM::Builder::Util::create_cfunc_name($class_name, $method_name, $category);
378 10061 $cfunc_address = DynaLoader::dl_find_symbol($dynamic_lib_libref, $cfunc_name);
379 10061 ▶ unless ($cfunc_address) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($cfunc_address) 0 10044 10044
unless ($cfunc_address) 0 11524 11524
380 17 my $dl_error = DynaLoader::dl_error();
381 3 my $error = <<"EOS";
382 Can't find native function '$cfunc_name' corresponding to ${class_name}#$method_name in '$dynamic_lib_file'
383  
384 You must write the following definition.
385 --------------------------------------------------
386 #include <spvm_native.h>
387  
388 int32_t $cfunc_name(SPVM_ENV* env, SPVM_VALUE* stack) {
389
390 return 0;
391 }
392 --------------------------------------------------
393  
394 $dl_error
395 EOS
396 0 confess($error);
397 }
398 }
399 else {
400 3 my $dl_error = DynaLoader::dl_error();
401 0 confess("DynaLoader#dl_load_file function failed:Can't load '$dynamic_lib_file' file for $category methods in $class_name class: dl_error='$dl_error'");
402 }
403 }
404 else {
405 3 confess("DLL file is not specified");
406 }
407
408 10041 $method_addresses->{$method_name} = $cfunc_address;
409 10061 });
410 }
411 }
412
413 0 return $method_addresses;
414 }
415  
416 780 my $BIND_TO_PERL_CLASS_NAME_H = {};
417 461 ▶sub bind_to_perl {
Subroutine bind_to_perl: called CC=8 SCAR=20.8
Pod undocumented
418 204 ▶ my ($class_name) = @_;
Subroutine bind_to_perl: not called CC=8 SCAR=20.8
Pod undocumented
419
420 204 my $env = $API->env;
421
422 204 my $runtime = $env->runtime;
423
424 221 my $basic_type = $runtime->get_basic_type_by_name($class_name);
425
426 221 my $perl_class_name_base = "SPVM::";
427 221 my $perl_class_name = "$perl_class_name_base$class_name";
428
429 461 ▶ unless ($BIND_TO_PERL_CLASS_NAME_H->{$perl_class_name}) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($BIND_TO_PERL_CLASS_NAME_H->{$perl_class_name}) 221 0 221
unless ($BIND_TO_PERL_CLASS_NAME_H->{$perl_class_name}) 152 1 153
430
431 441 my $parent_basic_type = $basic_type->get_parent;
432
433 # The inheritance
434 441 my @isa;
435 441 ▶ if (defined $parent_basic_type) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $parent_basic_type) 2 219 221
if (defined $parent_basic_type) 1 151 152
436 0 my $parent_basic_type_name = $parent_basic_type->get_name;
437 242 push @isa, "$perl_class_name_base$parent_basic_type_name";
438 }
439 441 push @isa, 'SPVM::BlessedObject::Class';
440 461 my $isa = "our \@ISA = (" . join(',', map { "'$_'" } @isa) . ");";
441
442 # Suppress a warning in child class
443 # While trying to resolve method call %s->%s() can not locate package "%s" yet it is mentioned in @%s::ISA (perhaps you forgot to load "%s"?)
444 # https://perldoc.perl.org/perldiag
445 444 my $suppress_warning_code = "sub import {} sub CLONE_SKIP { 1 } sub CLONE {}";
446
447 444 my $code = "package $perl_class_name; $isa $suppress_warning_code";
448 0 ▶ eval $code;
Subroutine CLONE: called CC=1 SCAR=0.2
Subroutine CLONE_SKIP: called CC=1 SCAR=0.2
Subroutine import: called CC=1 SCAR=0.2
Subroutine CLONE: called CC=1 SCAR=0.2
Subroutine CLONE_SKIP: called CC=1 SCAR=0.2
Subroutine import: called CC=1 SCAR=0.2
449
450 444 ▶ if (my $error = $@) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $error = $@) 3 201 204
if (my $error = $@) 0 152 152
451 243 confess($error);
452 }
453
454 444 my $methods_length = $basic_type->get_methods_length;
455
456 444 ▶ for (my $method_index = 0; $method_index < $methods_length; $method_index++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method_index < $methods_length) 1617 204 1821
if ($method_index < $methods_length) 1779 152 1931
457 1860 my $method = $basic_type->get_method_by_index($method_index);
458
459 1860 my $method_name = $method->get_name;
460
461 # Destrutor is skip
462 1857 ▶ if ($method_name eq 'DESTROY') {
Branch: 4 branches
BranchTrueFalseTotal
if ($method_name eq 'DESTROY') { } 0 1620 1620
elsif (length $method_name == 0) { } 17 1620 1637
if ($method_name eq 'DESTROY') { } 3 1776 1779
elsif (length $method_name == 0) { } 0 1776 1776
463 240 next;
464 }
465 # Anon method is skip
466 elsif (length $method_name == 0) {
467 243 next;
468 }
469
470 1860 my $perl_method_abs_name = "${perl_class_name}::$method_name";
471 1860 my $is_class_method = $method->is_class_method;
472
473 1860 ▶ if ($is_class_method) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_class_method) 1534 100 1634
if ($is_class_method) 1646 130 1776
474 # Define Perl method
475 536 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
476
477 # Suppress refer to objects
478 1777 my $class_name_string = "$class_name";
479 1777 my $method_name_string = "$method_name";
480
481 1774 ▶ *{"$perl_method_abs_name"} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
482 1204 my $perl_class_name = shift;
483
484 1218 my $return_value;
485
486 1218 my ($caller_class_name, $caller_file, $caller_line) = caller;
487 1218 my $caller_method_abs_name = "$caller_class_name#(Unknown)";
488 1201 my $caller_info = SPVM::ExchangeAPI::CallerInfo->new(method_abs_name => $caller_method_abs_name, file => $caller_file, line => $caller_line);
489
490 1201 eval { $return_value = SPVM::api()->call_method($class_name_string, $method_name_string, @_, $caller_info) };
491 1218 my $error = $@;
492 1218 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) 0 978 978
if ($error) 11 4816 4827
493 0 confess($error);
494 }
495 0 $return_value;
496 1777 };
497 }
498 }
499
500 444 $BIND_TO_PERL_CLASS_NAME_H->{$perl_class_name} = 1;
501 }
502 }
503  
504 =head1 Name
505  
506 SPVM::Global - SPVM Global Instance
507  
508 =head1 Copyright & License
509  
510 Copyright (c) 2023 Yuki Kimoto
511  
512 MIT License
« blib/lib/SPVM/Builder/Util.pm ↑ Summary blib/lib/SPVM/Builder/Native/Runtime.pm »