blib/lib/SPVM/Global.pm

filter: Statementstmt 92.6% 493 / 532 Branchbran 69.4% 125 / 180 Conditioncond 43.7% 7 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 84.6% 44 / 52 Podpod 0.0% 0 / 18 totaltotal 82.8% 669 / 808 CC 117 SCAR 20.6
CC
117
Cov
86%
build_dynamic_libs
18.6
build_dynamic_libs
18.6
__ANON__
9.9
CRAP 7.8
« blib/lib/SPVM/Builder/LinkInfo.pm ↑ Summary blib/lib/SPVM/Builder/CompileInfo.pm »
1 package SPVM::Global;
2 674 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 674 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 674 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 674 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 674 ▶use File::Path 'rmtree';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 674 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 680 ▶use Fcntl qw(O_RDONLY);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 680 ▶use SPVM::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 680 ▶use SPVM::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 680 ▶use SPVM::Builder::CC;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 673 ▶use SPVM::Builder::Native::Env;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 673 ▶use SPVM::ExchangeAPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 673 ▶use SPVM::BlessedObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 673 ▶use SPVM::BlessedObject::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 678 ▶use SPVM::BlessedObject::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 678 ▶use SPVM::BlessedObject::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 673 ▶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 341 ▶END {
Subroutine END: called CC=4 SCAR=13.9
33
34 6 ▶ if ($API) {
Subroutine END: called CC=4 SCAR=13.9
Branch: 2 branches
BranchTrueFalseTotal
if ($API) 0 1 1
if ($API) 271 70 341
35 {
36 # Remove circular reference
37 6 my $env = delete $API->{env};
38 6 my $stack = delete $API->{stack};
39
40 6 $env->call_end_methods($stack);
41
42 6 $env->destroy_class_vars($stack);
43 }
44
45 6 $API = undef;
46
47 6 $COMPILER = undef;
48
49 6 for my $dynamic_lib_file (keys %$DYNAMIC_LIB_LIBREFS_H) {
50 6 my $dynamic_lib_libref = $DYNAMIC_LIB_LIBREFS_H->{$dynamic_lib_file};
51 6 DynaLoader::dl_unload_file($dynamic_lib_libref);
52 }
53
54 6 $BUILDER = undef;
55
56 6 ▶ if (defined $BUILD_TMP_DIR) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $SPVM::Global::BUILD_TMP_DIR) 6 0 6
if (defined $SPVM::Global::BUILD_TMP_DIR) 228 43 271
57 6 rmtree $BUILD_TMP_DIR;
58 }
59 }
60 }
61  
62 6037 ▶sub api {
Subroutine api: called CC=2 SCAR=7.5
Pod undocumented
63
64 6 ▶ unless ($API) {
Subroutine api: called CC=2 SCAR=7.5
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
unless ($API) 0 6 6
unless ($API) 0 6037 6037
65 0 &init_api();
66 }
67 6 return $API;
68 }
69  
70 530 ▶sub build_class {
Subroutine build_class: called CC=1 SCAR=0.0
Pod undocumented
71 6 ▶ my ($class_name, $file, $line) = @_;
Subroutine build_class: called CC=1 SCAR=0.0
Pod undocumented
72
73 6 my $options = {
74 class_name => $class_name,
75 file => $file,
76 line => $line,
77 };
78
79 6 &build_class_common($options);
80 }
81  
82 5 ▶sub build_anon_class {
Subroutine build_anon_class: called CC=1 SCAR=0.0
Pod undocumented
83 6 ▶ my ($source, $file, $line) = @_;
Subroutine build_anon_class: called CC=1 SCAR=0.0
Pod undocumented
84
85 6 my $options = {
86 source => $source,
87 file => $file,
88 line => $line,
89 };
90
91 6 my $anon_class_name = &build_class_common($options);
92
93 6 return $anon_class_name;
94 }
95  
96 535 ▶sub build_class_common {
Subroutine build_class_common: called CC=7 SCAR=19.5
Pod undocumented
97 6 ▶ my ($options) = @_;
Subroutine build_class_common: called CC=7 SCAR=19.5
Pod undocumented
98
99 6 ▶ $options ||= {};
Condition50%2/4
$options ||= {}
l!l
60
$options ||= {}
l!l
5350
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 6 my $class_name = $options->{class_name};
102
103 6 my $source = $options->{source};
104
105 6 my $file = $options->{file};
106
107 6 my $line = $options->{line};
108
109 6 &init_api();
110
111 # Add module informations
112 6 my $build_success;
113 6 ▶ if (defined $class_name || defined $source) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $class_name or defined $source) 9 6 15
if (defined $class_name or defined $source) 535 0 535
Condition50%3/6
defined $class_name or defined $source
l!l&&r!l&&!r
600
defined $class_name or defined $source
l!l&&r!l&&!r
53050
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 6 my $api = $API;
116 6 my $compiler = $COMPILER;
117
118 6 my $env = $api->env;
119
120 6 my $runtime = $compiler->get_runtime;
121 6 my $start_basic_types_length = $runtime->get_basic_types_length;
122
123 6 ▶ if (defined $source) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $source) { } 2 1 3
if (defined $source) { } 5 530 535
124 6 $class_name = $compiler->compile_anon_class_with_exit($source, $file, $line);
125 }
126 else {
127 6 $compiler->compile_with_exit($class_name, $file, $line);
128 }
129
130 6 my $basic_types_length = $runtime->get_basic_types_length;
131
132 6 my $class_names = [];
133 6 ▶ 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) 5 7 12
if ($basic_type_id < $basic_types_length) 10827 532 11359
134 6 my $basic_type = $runtime->get_basic_type_by_id($basic_type_id);
135 6 push @$class_names, $basic_type->get_name;
136 }
137
138 6 &build_dynamic_libs($runtime, $class_names);
139
140 6 ▶ 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) 155 5 160
if ($basic_type_id < $basic_types_length) 6653 315 6968
141 6 my $basic_type = $runtime->get_basic_type_by_id($basic_type_id);
142 6 my $class_name = $basic_type->get_name;
143
144 6 &load_dynamic_lib($runtime, $class_name);
145 }
146
147 6 my $stack = $api->stack;
148
149 6 $env->call_init_methods($stack);
150
151 6 &bind_to_perl($class_name);
152 }
153
154 1 return $class_name;
155 }
156  
157 1068 ▶sub init_api {
Subroutine init_api: called CC=7 SCAR=19.8
Pod undocumented
158 0 ▶ unless ($API) {
Subroutine init_api: called CC=7 SCAR=19.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
unless ($API) 155 5 160
unless ($API) 484 584 1068
159 0 ▶ $BUILDER //= SPVM::Builder->new(is_jit => 1);
Condition33%2/6
$BUILDER //= "SPVM::Builder"->new("is_jit", 1)
l!l&&r!l&&!r
050
$BUILDER //= "SPVM::Builder"->new("is_jit", 1)
l!l&&r!l&&!r
04840
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 6 my $compiler = SPVM::Builder::Native::Compiler->new;
162
163 6 for my $class_search_dir (@{$BUILDER->class_search_dirs}) {
164 0 $compiler->add_class_search_dir($class_search_dir);
165 }
166
167 0 my $runtime = $compiler->get_runtime;
168
169 0 my $start_basic_types_length = $runtime->get_basic_types_length;
170
171 0 $compiler->compile_with_exit(undef, __FILE__, __LINE__);
172
173 6 my $basic_types_length = $runtime->get_basic_types_length;
174
175 6 my $class_names = [];
176 6 ▶ 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) 0 5 5
if ($basic_type_id < $basic_types_length) 15004 484 15488
177 6 my $basic_type = $runtime->get_basic_type_by_id($basic_type_id);
178 6 push @$class_names, $basic_type->get_name;
179 }
180
181 6 &build_dynamic_libs($runtime, $class_names);
182
183 6 ▶ 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) 0 164 164
if ($basic_type_id < $basic_types_length) 15004 484 15488
184 6 my $basic_type = $runtime->get_basic_type_by_id($basic_type_id);
185 6 my $class_name = $basic_type->get_name;
186 6 &load_dynamic_lib($runtime, $class_name);
187 }
188
189 6 my $env = $runtime->get_env;
190
191 6 my $stack = $env->new_stack;
192
193 6 my $api = SPVM::ExchangeAPI->new(env => $env, stack => $stack);
194
195 6 $COMPILER = $compiler;
196 6 $API = $api;
197
198 0 $env->set_command_info_program_name($stack, $0);
199
200 6 $env->set_command_info_argv($stack, \@ARGV);
201 6 my $base_time = $^T + 0; # For Perl 5.8.9
202 6 $env->set_command_info_basetime($stack, $base_time);
203
204 6 $env->set_command_info_basetime($stack, $base_time);
205
206 9 ▶ my $warning = $^W ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$^W ? : 7 321 328
$^W ? : 0 484 484
207 9 $env->set_command_info_warning($stack, $warning);
208 }
209 }
210  
211 1016 ▶sub build_dynamic_libs {
Subroutine build_dynamic_libs: called CC=18 SCAR=29.2
Pod undocumented
212 6 ▶ my ($runtime, $class_names) = @_;
Subroutine build_dynamic_libs: called CC=18 SCAR=29.2
Pod undocumented
213
214 1 my $build_options = {};
215 2 $build_options->{precompile_classes} = [];
216 2 $build_options->{native_classes} = [];
217
218 2 for my $class_name (@$class_names) {
219 1 ▶ if ($class_name =~ /::anon_method::/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($class_name =~ /::anon_method::/) 7 0 7
if ($class_name =~ /::anon_method::/) 3404 22427 25831
220 1 next;
221 }
222
223 1 for my $category ('precompile', 'native') {
224 1 my $basic_type = $runtime->get_basic_type_by_name($class_name);
225 12 my $method_names = $basic_type->get_method_names_by_category($category);
226
227 5 ▶ if (@$method_names) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$method_names) 0 7 7
if (@$method_names) 2292 42562 44854
228 5 ▶ unless ($DYNAMIC_LIB_FILES_H->{$class_name}{$category}) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($DYNAMIC_LIB_FILES_H->{$class_name}{$category}) 2 5 7
unless ($DYNAMIC_LIB_FILES_H->{$class_name}{$category}) 2292 0 2292
229 5 my $class_file = $basic_type->get_class_file;
230 5 my $class_file_from_inc = SPVM::Builder::Util::search_spvm_file($class_name);
231
232 50 ▶ unless ($class_file eq $class_file_from_inc) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($class_file eq $class_file_from_inc) 2 0 2
unless ($class_file eq $class_file_from_inc) 0 2292 2292
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 5 my $link_to_class_file;
237 5 ▶ if ($category eq 'native') {
Branch: 2 branches
BranchTrueFalseTotal
if ($category eq "native") 1 1 2
if ($category eq "native") 896 1396 2292
238 5 my $config_file = SPVM::Builder::Config::Util::create_config_file_path_from_class_file_path($class_file);
239 5 ▶ if (SPVM::Builder::Config::Util::exists_config_file($config_file)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SPVM::Builder::Config::Util::exists_config_file($config_file)) 0 1 1
if (SPVM::Builder::Config::Util::exists_config_file($config_file)) 896 0 896
240 5 my $config = SPVM::Builder::Config::Util::load_config($config_file);
241 155 my $link_to_class_name = $config->link_to;
242 155 ▶ if ($link_to_class_name) {
Branch: 2 branches
BranchTrueFalseTotal
if ($link_to_class_name) 0 1 1
if ($link_to_class_name) 23 873 896
243 5 my $link_to_basic_type = $runtime->get_basic_type_by_name($link_to_class_name);
244 5 ▶ unless ($link_to_basic_type) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($link_to_basic_type) 6 1 7
unless ($link_to_basic_type) 0 23 23
245 0 Carp::confess("$link_to_class_name class is not yet loaded.");
246 }
247 155 $link_to_class_file = $link_to_basic_type->get_class_file;
248 155 my $link_to_class_file_from_inc = SPVM::Builder::Util::search_spvm_file($link_to_class_name);
249
250 5 ▶ 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 7 7
unless ($link_to_class_file eq $link_to_class_file_from_inc) 0 23 23
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 5 ▶ unless (defined $link_to_class_file) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $link_to_class_file) 0 7 7
unless (defined $link_to_class_file) 2269 23 2292
258 5 $link_to_class_file = $class_file;
259 }
260
261 5 my $dynamic_lib_file_dist = SPVM::Builder::Util::get_dynamic_lib_file_dist($link_to_class_file, $category);
262
263 5 ▶ if (-f $dynamic_lib_file_dist) {
Branch: 4 branches
BranchTrueFalseTotal
if (-f $dynamic_lib_file_dist) { } 1 1 2
elsif (my $dynamic_lib_file_jit = $DYNAMIC_LIB_FILES_H->{$class_name}{$category}) { } 0 157 157
if (-f $dynamic_lib_file_dist) { } 1790 502 2292
elsif (my $dynamic_lib_file_jit = $DYNAMIC_LIB_FILES_H->{$class_name}{$category}) { } 0 502 502
264 5 $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 5 push @{$build_options->{"${category}_classes"}}, $class_name;
271 }
272 }
273 }
274 }
275 }
276
277 5 my $builder_cc = SPVM::Builder::CC->new(builder => $BUILDER, runtime => $runtime);
278 5 my $output_files_h = $builder_cc->build_parallel($build_options);
279
280 5 for my $category (keys %{$output_files_h}) {
281 11 for my $class_name (%{$output_files_h->{$category}}) {
282 11 my $dynamic_lib_file_jit = $output_files_h->{$category}{$class_name};
283 11 ▶ if ($dynamic_lib_file_jit) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dynamic_lib_file_jit) 1 313 314
if ($dynamic_lib_file_jit) 94 94 188
284 164 $DYNAMIC_LIB_FILES_H->{$class_name}{$category} = $dynamic_lib_file_jit;
285 0 $DYNAMIC_LIB_FILE_IS_JIT_H->{$dynamic_lib_file_jit} = 1;
286 }
287 }
288 }
289
290 }
291  
292 21657 ▶sub load_dynamic_lib {
Subroutine load_dynamic_lib: called CC=8 SCAR=21.1
Pod undocumented
293 164 ▶ my ($runtime, $class_name) = @_;
Subroutine load_dynamic_lib: called CC=8 SCAR=21.1
Pod undocumented
294
295 328 my $outmost_class_name;
296 328 ▶ if ($class_name =~ /^(.*)::anon_method::/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($class_name =~ /^(.*)::anon_method::/) { } 1 0 1
if ($class_name =~ /^(.*)::anon_method::/) { } 2080 19577 21657
297 328 $outmost_class_name = $1;
298 }
299 else {
300 7 $outmost_class_name = $class_name;
301 }
302
303 7 for my $category ('precompile', 'native') {
304 7 my $basic_type = $runtime->get_basic_type_by_name($class_name);
305
306 7 my $method_names = $basic_type->get_method_names_by_category($category);
307
308 0 ▶ if (@$method_names) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$method_names) 0 4 4
if (@$method_names) 1293 42021 43314
309 7 ▶ if (-f $DYNAMIC_LIB_FILES_H->{$outmost_class_name}{$category}) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $DYNAMIC_LIB_FILES_H->{$outmost_class_name}{$category}) 4 0 4
if (-f $DYNAMIC_LIB_FILES_H->{$outmost_class_name}{$category}) 1293 0 1293
310 7 my $method_addresses = &get_method_addresses($DYNAMIC_LIB_FILES_H->{$outmost_class_name}{$category}, $class_name, $method_names, $category);
311
312 2 for my $method_name (sort keys %$method_addresses) {
313 2 my $cfunc_address = $method_addresses->{$method_name};
314 2 my $method = $basic_type->get_method_by_name($method_name);
315 2 ▶ if ($category eq 'precompile') {
Branch: 4 branches
BranchTrueFalseTotal
if ($category eq 'precompile') { } 1 0 1
elsif ($category eq 'native') { } 4 0 4
if ($category eq 'precompile') { } 10985 10850 21835
elsif ($category eq 'native') { } 10850 0 10850
316 2 $method->set_precompile_address($cfunc_address);
317 }
318 elsif ($category eq 'native') {
319 1 $method->set_native_address($cfunc_address);
320 }
321 }
322 }
323 }
324 }
325 }
326  
327 1293 ▶sub get_method_addresses {
Subroutine get_method_addresses: called CC=4 SCAR=14.5
Pod undocumented
328 1 ▶ my ($dynamic_lib_file, $class_name, $method_names, $category) = @_;
Subroutine get_method_addresses: called CC=4 SCAR=14.5
Pod undocumented
329
330 0 my $method_addresses = {};
331 1 ▶ if (@$method_names) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$method_names) 3 1 4
if (@$method_names) 1293 0 1293
332 1 my $method_infos = [];
333 1 for my $method_name (@$method_names) {
334 0 my $method_info = {};
335 7 $method_info->{class_name} = $class_name;
336 6 $method_info->{method_name} = $method_name;
337 7 push @$method_infos, $method_info;
338 }
339
340 7 for my $method_info (@$method_infos) {
341 0 my $class_name = $method_info->{class_name};
342 7 my $method_name = $method_info->{method_name};
343
344 21835 ▶ $BUILDER->global_file_lock(sub {
Subroutine __ANON__: called CC=8 SCAR=22.9
345 11 ▶ my $cfunc_address;
Subroutine __ANON__: called CC=8 SCAR=22.9
346 6 ▶ if ($dynamic_lib_file) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dynamic_lib_file) { } 0 1 1
if ($dynamic_lib_file) { } 21835 0 21835
347 6 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}) { } 0 1 1
if ($DYNAMIC_LIB_LIBREFS_H->{$dynamic_lib_file}) { } 20566 1269 21835
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 1 1
unless (defined $ENV{"SPVM_BUILD_DIR"}) 0 1269 1269
354 0 confess("[Unexpected Error]no build directory");
355 }
356
357 1 ▶ unless (-f $dynamic_lib_file) {
Branch: 2 branches
BranchTrueFalseTotal
unless (-f $dynamic_lib_file) 4 0 4
unless (-f $dynamic_lib_file) 0 1269 1269
358 0 confess("\$dynamic_lib_file does not exist.");
359 }
360
361 157 ▶ 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 4 4
unless (sysopen my $dynamic_lib_fh, $dynamic_lib_file, 0) 0 1269 1269
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 157 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 157 $DYNAMIC_LIB_LIBREFS_H->{$dynamic_lib_file} = $dynamic_lib_libref;
373 }
374
375 157 ▶ if ($dynamic_lib_libref) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dynamic_lib_libref) { } 1 0 1
if ($dynamic_lib_libref) { } 21835 0 21835
376  
377 314 my $cfunc_name = SPVM::Builder::Util::create_cfunc_name($class_name, $method_name, $category);
378 314 $cfunc_address = DynaLoader::dl_find_symbol($dynamic_lib_libref, $cfunc_name);
379 314 ▶ unless ($cfunc_address) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($cfunc_address) 0 1 1
unless ($cfunc_address) 0 21835 21835
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 0 $method_addresses->{$method_name} = $cfunc_address;
409 7 });
410 }
411 }
412
413 11 return $method_addresses;
414 }
415  
416 my $BIND_TO_PERL_CLASS_NAME_H = {};
417 315 ▶sub bind_to_perl {
Subroutine bind_to_perl: called CC=8 SCAR=21.2
Pod undocumented
418 4 ▶ my ($class_name) = @_;
Subroutine bind_to_perl: called CC=8 SCAR=21.2
Pod undocumented
419
420 1 my $env = $API->env;
421
422 1 my $runtime = $env->runtime;
423
424 1 my $basic_type = $runtime->get_basic_type_by_name($class_name);
425
426 1 my $perl_class_name_base = "SPVM::";
427 1 my $perl_class_name = "$perl_class_name_base$class_name";
428
429 4 ▶ unless ($BIND_TO_PERL_CLASS_NAME_H->{$perl_class_name}) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($BIND_TO_PERL_CLASS_NAME_H->{$perl_class_name}) 0 1 1
unless ($BIND_TO_PERL_CLASS_NAME_H->{$perl_class_name}) 314 1 315
430
431 4 my $parent_basic_type = $basic_type->get_parent;
432
433 # The inheritance
434 4 my @isa;
435 4 ▶ if (defined $parent_basic_type) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $parent_basic_type) 7 1 8
if (defined $parent_basic_type) 3 311 314
436 1 my $parent_basic_type_name = $parent_basic_type->get_name;
437 4 push @isa, "$perl_class_name_base$parent_basic_type_name";
438 }
439 4 push @isa, 'SPVM::BlessedObject::Class';
440 4 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 4 my $suppress_warning_code = "sub import {} sub CLONE_SKIP { 1 } sub CLONE {}";
446
447 4 my $code = "package $perl_class_name; $isa $suppress_warning_code";
448 0 ▶ 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: not called CC=1 SCAR=0.0
Subroutine CLONE: not called CC=1 SCAR=0.0
Subroutine CLONE: not called CC=1 SCAR=0.0
Subroutine CLONE: not called CC=1 SCAR=0.0
Subroutine CLONE: not called CC=1 SCAR=0.0
449
450 4 ▶ if (my $error = $@) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $error = $@) 0 7 7
if (my $error = $@) 0 314 314
451 0 confess($error);
452 }
453
454 1 my $methods_length = $basic_type->get_methods_length;
455
456 0 ▶ for (my $method_index = 0; $method_index < $methods_length; $method_index++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method_index < $methods_length) 0 7 7
if ($method_index < $methods_length) 3312 314 3626
457 1 my $method = $basic_type->get_method_by_index($method_index);
458
459 0 my $method_name = $method->get_name;
460
461 # Destrutor is skip
462 1 ▶ if ($method_name eq 'DESTROY') {
Branch: 4 branches
BranchTrueFalseTotal
if ($method_name eq 'DESTROY') { } 7 0 7
elsif (length $method_name == 0) { } 0 0 0
if ($method_name eq 'DESTROY') { } 3 3309 3312
elsif (length $method_name == 0) { } 0 3309 3309
463 0 next;
464 }
465 # Anon method is skip
466 elsif (length $method_name == 0) {
467 0 next;
468 }
469
470 1 my $perl_method_abs_name = "${perl_class_name}::$method_name";
471 1 my $is_class_method = $method->is_class_method;
472
473 4 ▶ if ($is_class_method) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_class_method) 0 0 0
if ($is_class_method) 3099 210 3309
474 # Define Perl method
475 673 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
476
477 # Suppress refer to objects
478 4 my $class_name_string = "$class_name";
479 4 my $method_name_string = "$method_name";
480
481 0 ▶ *{"$perl_method_abs_name"} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
482 0 ▶ my $perl_class_name = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
483
484 0 my $return_value;
485
486 0 my ($caller_class_name, $caller_file, $caller_line) = caller;
487 0 my $caller_method_abs_name = "$caller_class_name#(Unknown)";
488 4 my $caller_info = SPVM::ExchangeAPI::CallerInfo->new(method_abs_name => $caller_method_abs_name, file => $caller_file, line => $caller_line);
489
490 1 eval { $return_value = SPVM::api()->call_method($class_name_string, $method_name_string, @_, $caller_info) };
491 1 my $error = $@;
492 1 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) 0 0 0
if ($error) 11 5791 5802
493 1 confess($error);
494 }
495 1 $return_value;
496 4 };
497 }
498 }
499
500 0 $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/LinkInfo.pm ↑ Summary blib/lib/SPVM/Builder/CompileInfo.pm »