blib/lib/SPVM/Global.pm

filter: Statementstmt 96.3% 508 / 527 Branchbran 81.1% 146 / 180 Conditioncond 43.7% 7 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 86.2% 44 / 51 Podpod 0.0% 0 / 18 totaltotal 87.9% 705 / 802 CC 117 SCAR 21.0
CC
117
Cov
91%
build_dynamic_libs
18.2
build_dynamic_libs
18.2
__ANON__
9.1
CRAP 8.2
« blib/lib/SPVM/Builder/Native/Runtime.pm ↑ Summary blib/lib/SPVM/Builder/CompileInfo.pm »
1 package SPVM::Global;
2 937 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 745 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 745 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 756 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 756 ▶use File::Path 'rmtree';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 756 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 747 ▶use Fcntl qw(O_RDONLY);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 758 ▶use SPVM::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 758 ▶use SPVM::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 747 ▶use SPVM::Builder::CC;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 849 ▶use SPVM::Builder::Native::Env;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 745 ▶use SPVM::ExchangeAPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 849 ▶use SPVM::BlessedObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 823 ▶use SPVM::BlessedObject::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 763 ▶use SPVM::BlessedObject::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 776 ▶use SPVM::BlessedObject::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 770 ▶use SPVM::ExchangeAPI::CallerInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 my $COMPILER;
23 my $API;
24 my $BUILDER;
25 my $DYNAMIC_LIB_LIBREFS_H = {};
26 my $DYNAMIC_LIB_FILES_H = {};
27 my $DYNAMIC_LIB_FILE_IS_JIT_H = {};
28  
29 # Accessed from SPVM::Builder
30 our $BUILD_TMP_DIR;
31  
32 367 ▶END {
Subroutine END: called CC=4 SCAR=13.9
33
34 20 ▶ if ($API) {
Subroutine END: called CC=4 SCAR=13.9
Branch: 2 branches
BranchTrueFalseTotal
if ($API) 12 2 14
if ($API) 298 69 367
35 {
36 # Remove circular reference
37 20 my $env = delete $API->{env};
38 20 my $stack = delete $API->{stack};
39
40 20 $env->call_end_methods($stack);
41
42 20 $env->destroy_class_vars($stack);
43 }
44
45 20 $API = undef;
46
47 20 $COMPILER = undef;
48
49 20 for my $dynamic_lib_file (keys %$DYNAMIC_LIB_LIBREFS_H) {
50 20 my $dynamic_lib_libref = $DYNAMIC_LIB_LIBREFS_H->{$dynamic_lib_file};
51 20 DynaLoader::dl_unload_file($dynamic_lib_libref);
52 }
53
54 20 $BUILDER = undef;
55
56 20 ▶ if (defined $BUILD_TMP_DIR) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $SPVM::Global::BUILD_TMP_DIR) 19 0 19
if (defined $SPVM::Global::BUILD_TMP_DIR) 211 87 298
57 20 rmtree $BUILD_TMP_DIR;
58 }
59 }
60 }
61  
62 5920 ▶sub api {
Subroutine api: called CC=2 SCAR=7.5
Pod undocumented
63
64 20 ▶ unless ($API) {
Subroutine api: called CC=2 SCAR=7.5
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
unless ($API) 0 41 41
unless ($API) 0 5920 5920
65 0 &init_api();
66 }
67 20 return $API;
68 }
69  
70 669 ▶sub build_class {
Subroutine build_class: called CC=1 SCAR=0.0
Pod undocumented
71 20 ▶ my ($class_name, $file, $line) = @_;
Subroutine build_class: called CC=1 SCAR=0.0
Pod undocumented
72
73 20 my $options = {
74 class_name => $class_name,
75 file => $file,
76 line => $line,
77 };
78
79 20 &build_class_common($options);
80 }
81  
82 4 ▶sub build_anon_class {
Subroutine build_anon_class: called CC=1 SCAR=0.0
Pod undocumented
83 20 ▶ my ($source, $file, $line) = @_;
Subroutine build_anon_class: called CC=1 SCAR=0.0
Pod undocumented
84
85 20 my $options = {
86 source => $source,
87 file => $file,
88 line => $line,
89 };
90
91 20 my $anon_class_name = &build_class_common($options);
92
93 20 return $anon_class_name;
94 }
95  
96 649 ▶sub build_class_common {
Subroutine build_class_common: called CC=7 SCAR=19.5
Pod undocumented
97 20 ▶ my ($options) = @_;
Subroutine build_class_common: called CC=7 SCAR=19.5
Pod undocumented
98
99 20 ▶ $options ||= {};
Condition50%2/4
$options ||= {}
l!l
190
$options ||= {}
l!l
6490
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 20 my $class_name = $options->{class_name};
102
103 20 my $source = $options->{source};
104
105 20 my $file = $options->{file};
106
107 20 my $line = $options->{line};
108
109 20 &init_api();
110
111 # Add module informations
112 20 my $build_success;
113 20 ▶ if (defined $class_name || defined $source) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $class_name or defined $source) 91 7 98
if (defined $class_name or defined $source) 649 0 649
Condition50%3/6
defined $class_name or defined $source
l!l&&r!l&&!r
1900
defined $class_name or defined $source
l!l&&r!l&&!r
64540
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 table33%1/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 20 my $api = $API;
116 20 my $compiler = $COMPILER;
117
118 20 my $env = $api->env;
119
120 20 my $runtime = $compiler->get_runtime;
121 20 my $start_basic_types_length = $runtime->get_basic_types_length;
122
123 20 ▶ if (defined $source) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $source) { } 3 13 16
if (defined $source) { } 4 645 649
124 20 $class_name = $compiler->compile_anon_class_with_exit($source, $file, $line);
125 }
126 else {
127 20 $compiler->compile_with_exit($class_name, $file, $line);
128 }
129
130 20 my $basic_types_length = $runtime->get_basic_types_length;
131
132 20 my $class_names = [];
133 20 ▶ 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) 82 18 100
if ($basic_type_id < $basic_types_length) 10264 647 10911
134 20 my $basic_type = $runtime->get_basic_type_by_id($basic_type_id);
135 20 push @$class_names, $basic_type->get_name;
136 }
137
138 20 &build_dynamic_libs($runtime, $class_names);
139
140 20 ▶ 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) 292 18 310
if ($basic_type_id < $basic_types_length) 6289 333 6622
141 20 my $basic_type = $runtime->get_basic_type_by_id($basic_type_id);
142 20 my $class_name = $basic_type->get_name;
143
144 20 &load_dynamic_lib($runtime, $class_name);
145 }
146
147 20 my $stack = $api->stack;
148
149 20 $env->call_init_methods($stack);
150
151 20 &bind_to_perl($class_name);
152 }
153
154 14 return $class_name;
155 }
156  
157 1302 ▶sub init_api {
Subroutine init_api: called CC=7 SCAR=19.5
Pod undocumented
158 12 ▶ unless ($API) {
Subroutine init_api: called CC=7 SCAR=19.5
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
unless ($API) 229 19 248
unless ($API) 602 700 1302
159 14 ▶ $BUILDER //= SPVM::Builder->new(is_jit => 1);
Condition33%2/6
$BUILDER //= "SPVM::Builder"->new("is_jit", 1)
l!l&&r!l&&!r
0190
$BUILDER //= "SPVM::Builder"->new("is_jit", 1)
l!l&&r!l&&!r
06020
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 19 my $compiler = SPVM::Builder::Native::Compiler->new;
162
163 19 for my $class_search_dir (@{$BUILDER->class_search_dirs}) {
164 12 $compiler->add_class_search_dir($class_search_dir);
165 }
166
167 12 my $runtime = $compiler->get_runtime;
168
169 12 my $start_basic_types_length = $runtime->get_basic_types_length;
170
171 10 $compiler->compile_with_exit(undef, __FILE__, __LINE__);
172
173 17 my $basic_types_length = $runtime->get_basic_types_length;
174
175 19 my $class_names = [];
176 19 ▶ 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) 372 19 391
if ($basic_type_id < $basic_types_length) 18662 602 19264
177 19 my $basic_type = $runtime->get_basic_type_by_id($basic_type_id);
178 41 push @$class_names, $basic_type->get_name;
179 }
180
181 7 &build_dynamic_libs($runtime, $class_names);
182
183 41 ▶ 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) 398 282 680
if ($basic_type_id < $basic_types_length) 18662 602 19264
184 19 my $basic_type = $runtime->get_basic_type_by_id($basic_type_id);
185 19 my $class_name = $basic_type->get_name;
186 19 &load_dynamic_lib($runtime, $class_name);
187 }
188
189 7 my $env = $runtime->get_env;
190
191 7 my $stack = $env->new_stack;
192
193 7 my $api = SPVM::ExchangeAPI->new(env => $env, stack => $stack);
194
195 7 $COMPILER = $compiler;
196 19 $API = $api;
197
198 12 $env->set_command_info_program_name($stack, $0);
199
200 19 $env->set_command_info_argv($stack, \@ARGV);
201 19 my $base_time = $^T + 0; # For Perl 5.8.9
202 19 $env->set_command_info_basetime($stack, $base_time);
203
204 19 $env->set_command_info_basetime($stack, $base_time);
205
206 91 ▶ my $warning = $^W ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$^W ? : 20 532 552
$^W ? : 0 602 602
207 91 $env->set_command_info_warning($stack, $warning);
208 }
209 }
210  
211 1249 ▶sub build_dynamic_libs {
Subroutine build_dynamic_libs: called CC=18 SCAR=29.0
Pod undocumented
212 19 ▶ my ($runtime, $class_names) = @_;
Subroutine build_dynamic_libs: called CC=18 SCAR=29.0
Pod undocumented
213
214 13 my $build_options = {};
215 15 $build_options->{precompile_classes} = [];
216 15 $build_options->{native_classes} = [];
217
218 15 for my $class_name (@$class_names) {
219 13 ▶ if ($class_name =~ /::anon_method::/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($class_name =~ /::anon_method::/) 38 429 467
if ($class_name =~ /::anon_method::/) 3153 25773 28926
220 13 next;
221 }
222
223 1 for my $category ('precompile', 'native') {
224 13 my $basic_type = $runtime->get_basic_type_by_name($class_name);
225 25 my $method_names = $basic_type->get_method_names_by_category($category);
226
227 18 ▶ if (@$method_names) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$method_names) 10 868 878
if (@$method_names) 2300 49246 51546
228 18 ▶ unless ($DYNAMIC_LIB_FILES_H->{$class_name}{$category}) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($DYNAMIC_LIB_FILES_H->{$class_name}{$category}) 17 13 30
unless ($DYNAMIC_LIB_FILES_H->{$class_name}{$category}) 2300 0 2300
229 82 my $class_file = $basic_type->get_class_file;
230 82 my $class_file_from_inc = SPVM::Builder::Util::search_spvm_file($class_name);
231
232 82 ▶ unless ($class_file eq $class_file_from_inc) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($class_file eq $class_file_from_inc) 7 10 17
unless ($class_file eq $class_file_from_inc) 0 2300 2300
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 82 my $link_to_class_file;
237 82 ▶ if ($category eq 'native') {
Branch: 2 branches
BranchTrueFalseTotal
if ($category eq "native") 4 13 17
if ($category eq "native") 970 1330 2300
238 82 my $config_file = SPVM::Builder::Config::Util::create_config_file_path_from_class_file_path($class_file);
239 18 ▶ if (SPVM::Builder::Config::Util::exists_config_file($config_file)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SPVM::Builder::Config::Util::exists_config_file($config_file)) 4 0 4
if (SPVM::Builder::Config::Util::exists_config_file($config_file)) 970 0 970
240 18 my $config = SPVM::Builder::Config::Util::load_config($config_file);
241 228 my $link_to_class_name = $config->link_to;
242 228 ▶ if ($link_to_class_name) {
Branch: 2 branches
BranchTrueFalseTotal
if ($link_to_class_name) 0 4 4
if ($link_to_class_name) 24 946 970
243 31 my $link_to_basic_type = $runtime->get_basic_type_by_name($link_to_class_name);
244 19 ▶ unless ($link_to_basic_type) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($link_to_basic_type) 20 0 20
unless ($link_to_basic_type) 0 24 24
245 0 Carp::confess("$link_to_class_name class is not yet loaded.");
246 }
247 229 $link_to_class_file = $link_to_basic_type->get_class_file;
248 229 my $link_to_class_file_from_inc = SPVM::Builder::Util::search_spvm_file($link_to_class_name);
249
250 256 ▶ 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) 11 9 20
unless ($link_to_class_file eq $link_to_class_file_from_inc) 0 24 24
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 19 ▶ unless (defined $link_to_class_file) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $link_to_class_file) 10 9 19
unless (defined $link_to_class_file) 2276 24 2300
258 19 $link_to_class_file = $class_file;
259 }
260
261 19 my $dynamic_lib_file_dist = SPVM::Builder::Util::get_dynamic_lib_file_dist($link_to_class_file, $category);
262
263 19 ▶ if (-f $dynamic_lib_file_dist) {
Branch: 4 branches
BranchTrueFalseTotal
if (-f $dynamic_lib_file_dist) { } 11 1 12
elsif (my $dynamic_lib_file_jit = $DYNAMIC_LIB_FILES_H->{$class_name}{$category}) { } 0 220 220
if (-f $dynamic_lib_file_dist) { } 1681 619 2300
elsif (my $dynamic_lib_file_jit = $DYNAMIC_LIB_FILES_H->{$class_name}{$category}) { } 0 619 619
264 19 $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 379 push @{$build_options->{"${category}_classes"}}, $class_name;
271 }
272 }
273 }
274 }
275 }
276
277 19 my $builder_cc = SPVM::Builder::CC->new(builder => $BUILDER, runtime => $runtime);
278 19 my $output_files_h = $builder_cc->build_parallel($build_options);
279
280 379 for my $category (keys %{$output_files_h}) {
281 386 for my $class_name (%{$output_files_h->{$category}}) {
282 26 my $dynamic_lib_file_jit = $output_files_h->{$category}{$class_name};
283 26 ▶ if ($dynamic_lib_file_jit) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dynamic_lib_file_jit) 1 439 440
if ($dynamic_lib_file_jit) 135 135 270
284 308 $DYNAMIC_LIB_FILES_H->{$class_name}{$category} = $dynamic_lib_file_jit;
285 38 $DYNAMIC_LIB_FILE_IS_JIT_H->{$dynamic_lib_file_jit} = 1;
286 }
287 }
288 }
289
290 }
291  
292 24951 ▶sub load_dynamic_lib {
Subroutine load_dynamic_lib: called CC=8 SCAR=20.8
Pod undocumented
293 282 ▶ my ($runtime, $class_name) = @_;
Subroutine load_dynamic_lib: called CC=8 SCAR=20.8
Pod undocumented
294
295 552 my $outmost_class_name;
296 552 ▶ if ($class_name =~ /^(.*)::anon_method::/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($class_name =~ /^(.*)::anon_method::/) { } 19 429 448
if ($class_name =~ /^(.*)::anon_method::/) { } 1904 23047 24951
297 552 $outmost_class_name = $1;
298 }
299 else {
300 32 $outmost_class_name = $class_name;
301 }
302
303 32 for my $category ('precompile', 'native') {
304 32 my $basic_type = $runtime->get_basic_type_by_name($class_name);
305
306 43 my $method_names = $basic_type->get_method_names_by_category($category);
307
308 23 ▶ if (@$method_names) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$method_names) 11 884 895
if (@$method_names) 1270 48632 49902
309 43 ▶ if (-f $DYNAMIC_LIB_FILES_H->{$outmost_class_name}{$category}) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $DYNAMIC_LIB_FILES_H->{$outmost_class_name}{$category}) 10 0 10
if (-f $DYNAMIC_LIB_FILES_H->{$outmost_class_name}{$category}) 1270 0 1270
310 43 my $method_addresses = &get_method_addresses($DYNAMIC_LIB_FILES_H->{$outmost_class_name}{$category}, $class_name, $method_names, $category);
311
312 30 for my $method_name (sort keys %$method_addresses) {
313 454 my $cfunc_address = $method_addresses->{$method_name};
314 25 my $method = $basic_type->get_method_by_name($method_name);
315 436 ▶ if ($category eq 'precompile') {
Branch: 4 branches
BranchTrueFalseTotal
if ($category eq 'precompile') { } 101 92 193
elsif ($category eq 'native') { } 93 0 93
if ($category eq 'precompile') { } 10410 10323 20733
elsif ($category eq 'native') { } 10323 0 10323
316 865 $method->set_precompile_address($cfunc_address);
317 }
318 elsif ($category eq 'native') {
319 858 $method->set_native_address($cfunc_address);
320 }
321 }
322 }
323 }
324 }
325 }
326  
327 1270 ▶sub get_method_addresses {
Subroutine get_method_addresses: called CC=4 SCAR=14.2
Pod undocumented
328 858 ▶ my ($dynamic_lib_file, $class_name, $method_names, $category) = @_;
Subroutine get_method_addresses: called CC=4 SCAR=14.2
Pod undocumented
329
330 10 my $method_addresses = {};
331 10 ▶ if (@$method_names) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$method_names) 10 1 11
if (@$method_names) 1270 0 1270
332 10 my $method_infos = [];
333 10 for my $method_name (@$method_names) {
334 0 my $method_info = {};
335 30 $method_info->{class_name} = $class_name;
336 30 $method_info->{method_name} = $method_name;
337 24 push @$method_infos, $method_info;
338 }
339
340 24 for my $method_info (@$method_infos) {
341 15 my $class_name = $method_info->{class_name};
342 13 my $method_name = $method_info->{method_name};
343
344 20733 ▶ $BUILDER->global_file_lock(sub {
Subroutine __ANON__: called CC=8 SCAR=22.1
345 14 ▶ my $cfunc_address;
Subroutine __ANON__: called CC=8 SCAR=22.1
346 8 ▶ if ($dynamic_lib_file) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dynamic_lib_file) { } 192 1 193
if ($dynamic_lib_file) { } 20733 0 20733
347 8 my $dynamic_lib_libref;
348 1 ▶ if ($DYNAMIC_LIB_LIBREFS_H->{$dynamic_lib_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($DYNAMIC_LIB_LIBREFS_H->{$dynamic_lib_file}) { } 182 11 193
if ($DYNAMIC_LIB_LIBREFS_H->{$dynamic_lib_file}) { } 19486 1247 20733
349 1 $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 2 ▶ unless (defined $ENV{SPVM_BUILD_DIR}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $ENV{"SPVM_BUILD_DIR"}) 0 11 11
unless (defined $ENV{"SPVM_BUILD_DIR"}) 0 1247 1247
354 0 confess("[Unexpected Error]no build directory");
355 }
356
357 11 ▶ unless (-f $dynamic_lib_file) {
Branch: 2 branches
BranchTrueFalseTotal
unless (-f $dynamic_lib_file) 1 10 11
unless (-f $dynamic_lib_file) 0 1247 1247
358 0 confess("\$dynamic_lib_file does not exist.");
359 }
360
361 230 ▶ 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 11 11
unless (sysopen my $dynamic_lib_fh, $dynamic_lib_file, 0) 0 1247 1247
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 220 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 243 $DYNAMIC_LIB_LIBREFS_H->{$dynamic_lib_file} = $dynamic_lib_libref;
373 }
374
375 243 ▶ if ($dynamic_lib_libref) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dynamic_lib_libref) { } 193 0 193
if ($dynamic_lib_libref) { } 20733 0 20733
376  
377 463 my $cfunc_name = SPVM::Builder::Util::create_cfunc_name($class_name, $method_name, $category);
378 463 $cfunc_address = DynaLoader::dl_find_symbol($dynamic_lib_libref, $cfunc_name);
379 440 ▶ unless ($cfunc_address) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($cfunc_address) 0 193 193
unless ($cfunc_address) 0 20733 20733
380 0 my $dl_error = DynaLoader::dl_error();
381 0 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 0 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 0 confess("DLL file is not specified");
406 }
407
408 448 $method_addresses->{$method_name} = $cfunc_address;
409 13 });
410 }
411 }
412
413 14 return $method_addresses;
414 }
415  
416 my $BIND_TO_PERL_CLASS_NAME_H = {};
417 333 ▶sub bind_to_perl {
Subroutine bind_to_perl: called CC=8 SCAR=20.8
Pod undocumented
418 447 ▶ my ($class_name) = @_;
Subroutine bind_to_perl: called CC=8 SCAR=20.8
Pod undocumented
419
420 19 my $env = $API->env;
421
422 430 my $runtime = $env->runtime;
423
424 448 my $basic_type = $runtime->get_basic_type_by_name($class_name);
425
426 895 my $perl_class_name_base = "SPVM::";
427 895 my $perl_class_name = "$perl_class_name_base$class_name";
428
429 895 ▶ unless ($BIND_TO_PERL_CLASS_NAME_H->{$perl_class_name}) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($BIND_TO_PERL_CLASS_NAME_H->{$perl_class_name}) 11 1 12
unless ($BIND_TO_PERL_CLASS_NAME_H->{$perl_class_name}) 332 1 333
430
431 11 my $parent_basic_type = $basic_type->get_parent;
432
433 # The inheritance
434 11 my @isa;
435 11 ▶ if (defined $parent_basic_type) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $parent_basic_type) 3 12 15
if (defined $parent_basic_type) 3 329 332
436 193 my $parent_basic_type_name = $parent_basic_type->get_name;
437 193 push @isa, "$perl_class_name_base$parent_basic_type_name";
438 }
439 193 push @isa, 'SPVM::BlessedObject::Class';
440 101 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 11 my $suppress_warning_code = "sub import {} sub CLONE_SKIP { 1 } sub CLONE {}";
446
447 11 my $code = "package $perl_class_name; $isa $suppress_warning_code";
448 11 ▶ eval $code;
Subroutine CLONE: not called CC=1 SCAR=0.0
Subroutine CLONE_SKIP: not called CC=1 SCAR=0.0
Subroutine import: called CC=1 SCAR=0.0
Subroutine CLONE: called CC=1 SCAR=0.0
Subroutine CLONE: not called CC=1 SCAR=0.0
Subroutine CLONE_SKIP: not called CC=1 SCAR=0.0
Subroutine CLONE_SKIP: not called CC=1 SCAR=0.0
Subroutine import: not called CC=1 SCAR=0.0
Subroutine import: not called CC=1 SCAR=0.0
449
450 11 ▶ if (my $error = $@) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $error = $@) 0 14 14
if (my $error = $@) 0 332 332
451 0 confess($error);
452 }
453
454 193 my $methods_length = $basic_type->get_methods_length;
455
456 192 ▶ for (my $method_index = 0; $method_index < $methods_length; $method_index++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method_index < $methods_length) 104 14 118
if ($method_index < $methods_length) 2954 332 3286
457 193 my $method = $basic_type->get_method_by_index($method_index);
458
459 192 my $method_name = $method->get_name;
460
461 # Destrutor is skip
462 11 ▶ if ($method_name eq 'DESTROY') {
Branch: 4 branches
BranchTrueFalseTotal
if ($method_name eq 'DESTROY') { } 3 104 107
elsif (length $method_name == 0) { } 0 105 105
if ($method_name eq 'DESTROY') { } 3 2951 2954
elsif (length $method_name == 0) { } 0 2951 2951
463 192 next;
464 }
465 # Anon method is skip
466 elsif (length $method_name == 0) {
467 0 next;
468 }
469
470 193 my $perl_method_abs_name = "${perl_class_name}::$method_name";
471 11 my $is_class_method = $method->is_class_method;
472
473 193 ▶ if ($is_class_method) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_class_method) 80 26 106
if ($is_class_method) 2814 137 2951
474 # Define Perl method
475 770 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
476
477 # Suppress refer to objects
478 193 my $class_name_string = "$class_name";
479 193 my $method_name_string = "$method_name";
480
481 182 ▶ *{"$perl_method_abs_name"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
482 0 ▶ my $perl_class_name = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
483
484 10 my $return_value;
485
486 0 my ($caller_class_name, $caller_file, $caller_line) = caller;
487 10 my $caller_method_abs_name = "$caller_class_name#(Unknown)";
488 1 my $caller_info = SPVM::ExchangeAPI::CallerInfo->new(method_abs_name => $caller_method_abs_name, file => $caller_file, line => $caller_line);
489
490 11 eval { $return_value = SPVM::api()->call_method($class_name_string, $method_name_string, @_, $caller_info) };
491 11 my $error = $@;
492 193 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) 2 24 26
if ($error) 11 5696 5707
493 193 confess($error);
494 }
495 193 $return_value;
496 193 };
497 }
498 }
499
500 10 $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/Native/Runtime.pm ↑ Summary blib/lib/SPVM/Builder/CompileInfo.pm »