JE/Object/String.pm

filter: Statementstmt 92.6% 226 / 244 Branchbran 81.8% 108 / 132 Conditioncond 79.6% 51 / 64 MC/DCmcdc 69.2% 27 / 39 Subroutinesub 84.6% 33 / 39 Podpod 85.7% 6 / 7 totaltotal 85.9% 451 / 525 CC 91 SCAR 21.2
CC
91
Cov
88%
__ANON__
24.1
__ANON__
13.0
__ANON__
8.7
CRAP 8.3
« JE/Destroyer.pm ↑ Summary JE/Object/Date.pm »
1 package JE::Object::String;
2  
3 101 our $VERSION = '0.066';
4  
5  
6 101 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 101 ▶use warnings; no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub surrogify($);
10 sub desurrogify($);
11  
12 101 our @ISA = 'JE::Object';
13  
14 101 ▶use POSIX qw 'floor ceil';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 101 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 101 require JE::Code;
18 101 require JE::Number;
19 101 require JE::Object ;
20 101 require JE::Object::Error::TypeError;
21 101 require JE::Object::Function ;
22 101 require JE::String ;
23  
24 101 JE::String->import(qw/surrogify desurrogify/);
25 101 JE::Code->import('add_line_number');
26 sub add_line_number;
27  
28 =encoding UTF-8
29  
30 =head1 NAME
31  
32 JE::Object::String - JavaScript String object class
33  
34 =head1 SYNOPSIS
35  
36 use JE;
37 use JE::Object::String;
38  
39 $j = new JE;
40  
41 $js_str_obj = new JE::Object::String $j, "etetfyoyfoht";
42  
43 $perl_str = $js_str_obj->value;
44  
45 =head1 DESCRIPTION
46  
47 This class implements JavaScript String objects for JE. The difference
48 between this and JE::String is that that module implements
49 I<primitive> string value, while this module implements the I<objects.>
50  
51 =head1 METHODS
52  
53 See L<JE::Types> for descriptions of most of the methods. Only what
54 is specific to JE::Object::String is explained here.
55  
56 =over 4
57  
58 =cut
59  
60 34 ▶sub new {
Subroutine new: called CC=5 SCAR=16.6
Pod documented
61 34 my($class, $global, $val) = @_;
62 34 ▶ my $self = $class->SUPER::new($global, {
Condition33%1/3
$global->prototype_for('String') || $global->prop('String')->prop('prototype')
l!l&&r!l&&!r
3400
MC/DC0%0/2
$global->prototype_for('String') || $global->prop('String')->prop('prototype')
$global->prototype_for('String') $global->prop('String')->prop('prototype')
Truth table33%1/3
A || B
A= $global->prototype_for('String')B= $global->prop('String')->prop('prototype')
ABresult
1X1
011
000
63 prototype => $global->prototype_for('String')
64 || $global->prop('String')->prop('prototype')
65 });
66  
67 34 ▶ $$$self{value} = defined $val
Branch: 2 branches
BranchTrueFalseTotal
defined $val ? : 29 5 34
defined builtin::blessed($val) && $val->can('to_string') ? : 24 5 29
Condition66%2/3
defined builtin::blessed($val) and $val->can("to_string")
!ll&&!rl&&r
5024
MC/DC50%1/2
defined builtin::blessed($val) and $val->can("to_string")
defined builtin::blessed($val) $val->can("to_string")
Truth table66%2/3
A and B
A= defined builtin::blessed($val)B= $val->can("to_string")
ABresult
0X0
100
111
68 ? defined blessed $val
69 && $val->can('to_string')
70 ? $val->to_string->value16
71 : surrogify $val
72 : '';
73 34 $self;
74 }
75  
76 978 ▶sub prop {
Subroutine prop: called CC=2 SCAR=6.9
Pod documented
77 978 my $self = shift;
78 978 ▶ if($_[0] eq 'length') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq "length") 3 975 978
79 return JE::Number->new(
80 $$$self{global},length $$$self{value}
81 3 );
82 }
83 975 SUPER::prop $self @_;
84 }
85  
86 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
87 0 my $self = shift;
88 0 ▶ $_[0] eq 'length' and return !1;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq "length" and 0 0 0
89 0 SUPER::delete $self @_;
90 }
91  
92 =item value
93  
94 Returns a Perl scalar.
95  
96 =cut
97  
98 3 ▶sub value { desurrogify ${+shift}->{value} }
Subroutine value: called CC=1 SCAR=0.0
Pod documented
99  
100 =item value16
101  
102 Returns a Perl scalar containing a UTF-16 string (i.e., with surrogate
103 pairs if the string has chars outside the BMP). This is here more for
104 internal usage than anything else.
105  
106 =cut
107  
108 1 ▶sub value16 { ${+shift}->{value} }
Subroutine value16: called CC=1 SCAR=0.0
Pod documented
109  
110  
111  
112 5 ▶sub is_readonly {
Subroutine is_readonly: called CC=2 SCAR=7.1
Pod undocumented
113 5 my $self = shift;
114 5 ▶ $_[0] eq 'length' and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq "length" and return 1 0 5 5
115 5 SUPER::is_readonly $self @_;
116 }
117  
118 28 ▶sub class { 'String' }
Subroutine class: called CC=1 SCAR=0.0
Pod documented
119  
120 101 ▶no warnings 'qw';
Subroutine BEGIN: called CC=1 SCAR=0.0
121 101 our %_replace = qw/
122 $ \$
123 & ".substr($str,$-[0],$+[0]-$-[0])."
124 ` ".substr($str,0,$-[0])."
125 ' ".substr($str,$+[0])."
126 /;
127  
128 19 ▶sub _new_constructor {
Subroutine _new_constructor: called CC=1 SCAR=0.0
129 19 my $global = shift;
130 my $f = JE::Object::Function->new({
131 name => 'String',
132 scope => $global,
133 26 ▶ function => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
134 26 my $arg = shift;
135 26 ▶ defined $arg ? $arg->to_string :
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 25 1 26
136 JE::String->_new($global, '');
137 },
138 function_args => ['args'],
139 argnames => ['value'],
140 22 ▶ constructor => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
141 22 unshift @_, __PACKAGE__;
142 22 goto &new;
143 },
144 19 constructor_args => ['scope','args'],
145 });
146  
147 # E 15.5.3.2
148 $f->prop({
149 name => 'fromCharCode',
150 value => JE::Object::Function->new({
151 scope => $global,
152 name => 'fromCharCode',
153 length => 1,
154 no_proto => 1,
155 function_args => ['args'],
156 17 ▶ function => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
157 17 my $str = '';
158 17 my $num;
159 17 for (@_) {
160 # % 2**16 is buggy in perl
161 52 $num = $_->to_number->value;
162 52 ▶ $num =
Branch: 1 branch
BranchTrueFalseTotal
$num < 0 ? : 12 40 52
163 ($num < 0 ? ceil$num : floor$num)
164 % 2**16 ;
165 52 ▶ $str .= chr($num == $num && $num);
Condition100%3/3
$num == $num && $num
!ll&&!rl&&r
61135
MC/DC100%2/2
$num == $num && $num
$num == $num $num
Truth table100%3/3
A && B
A= $num == $numB= $num
ABresult
0X0
100
111
166 # change nan to 0
167 }
168 17 JE::String->_new($global, $str);
169 },
170 19 }),
171 dontenum => 1,
172 });
173  
174 19 my $proto = bless $f->prop({
175 name => 'prototype',
176 dontenum => 1,
177 readonly => 1,
178 }), __PACKAGE__;
179 19 $$$proto{value} = '';
180 19 $global->prototype_for('String', $proto);
181  
182 $proto->prop({
183 name => 'toString',
184 value => JE::Object::Function->new({
185 scope => $global,
186 name => 'toString',
187 no_proto => 1,
188 function_args => ['this'],
189 12 ▶ function => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
190 12 my $self = shift;
191 12 ▶ die JE::Object::Error::TypeError->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->class eq "String" 3 9 12
192 $global, add_line_number
193 "Argument to toString is not a " .
194 "String object"
195 ) unless $self->class eq 'String';
196  
197 9 ▶ return $self if ref $self eq 'JE::String';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self eq "JE::String" 1 8 9
198 8 JE::String->_new($global, $$$self{value});
199 },
200 19 }),
201 dontenum => 1,
202 });
203  
204 $proto->prop({
205 name => 'valueOf',
206 value => JE::Object::Function->new({
207 scope => $global,
208 name => 'valueOf',
209 no_proto => 1,
210 function_args => ['this'],
211 19 ▶ function => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
212 19 my $self = shift;
213 19 ▶ die JE::Object::Error::TypeError->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->class eq "String" 3 16 19
214 $global, add_line_number
215 "Argument to valueOf is not a " .
216 "String object"
217 ) unless $self->class eq 'String';
218  
219 # We also deal with plain strings here.
220 ref $self eq 'JE::String'
221 ? $self
222 : JE::String->_new(
223 $global, $$$self{value}
224 16 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
ref $self eq 'JE::String' ? : 1 15 16
225 },
226 19 }),
227 dontenum => 1,
228 });
229  
230 $proto->prop({
231 name => 'charAt',
232 value => JE::Object::Function->new({
233 scope => $global,
234 name => 'charAt',
235 argnames => ['pos'],
236 no_proto => 1,
237 function_args => ['this','args'],
238 12 ▶ function => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
239 12 my ($self,$pos) = @_;
240
241 12 my $str = $self->to_string->value16;
242 12 ▶ if (defined $pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) 12 0 12
243 12 $pos = int $pos->to_number->[0];
244 12 ▶ $pos = 0 unless $pos == $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless $pos == $pos 2 10 12
245 }
246  
247 12 ▶ JE::String->_new($global,
Branch: 1 branch
BranchTrueFalseTotal
$pos < 0 || $pos >= length $str ? : 2 10 12
Condition100%3/3
$pos < 0 or $pos >= length $str
l!l&&r!l&&!r
1110
MC/DC100%2/2
$pos < 0 or $pos >= length $str
$pos < 0 $pos >= length $str
Truth table100%3/3
A or B
A= $pos < 0B= $pos >= length $str
ABresult
1X1
011
000
248 $pos < 0 || $pos >= length $str
249 ? ''
250 : substr $str, $pos, 1);
251 },
252 19 }),
253 dontenum => 1,
254 });
255  
256 $proto->prop({
257 name => 'charCodeAt',
258 value => JE::Object::Function->new({
259 scope => $global,
260 name => 'charCodeAt',
261 argnames => ['pos'],
262 no_proto => 1,
263 function_args => ['this','args'],
264 12 ▶ function => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
265 12 my ($self,$pos) = @_;
266
267 12 my $str = $self->to_string->value16;
268 12 ▶ if (defined $pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) 12 0 12
269 12 $pos = int $pos->to_number->[0];
270 12 ▶ $pos = 0 unless $pos == $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless $pos == $pos 2 10 12
271 }
272  
273 12 ▶ JE::Number->new($global,
Branch: 1 branch
BranchTrueFalseTotal
$pos < 0 || $pos >= length $str ? : 2 10 12
Condition100%3/3
$pos < 0 or $pos >= length $str
l!l&&r!l&&!r
1110
MC/DC100%2/2
$pos < 0 or $pos >= length $str
$pos < 0 $pos >= length $str
Truth table100%3/3
A or B
A= $pos < 0B= $pos >= length $str
ABresult
1X1
011
000
274 $pos < 0 || $pos >= length $str
275 ? 'nan'
276 : ord substr $str, $pos, 1);
277 },
278 19 }),
279 dontenum => 1,
280 });
281  
282 $proto->prop({
283 name => 'concat',
284 value => JE::Object::Function->new({
285 scope => $global,
286 name => 'concat',
287 length => 1,
288 no_proto => 1,
289 function_args => ['this','args'],
290 5 ▶ function => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
291 5 my $str = '';
292 5 for (@_) {
293 16 $str .= $_->to_string->value16
294 }
295 5 JE::String->_new($global, $str);
296 },
297 19 }),
298 dontenum => 1,
299 });
300  
301 $proto->prop({
302 name => 'indexOf',
303 value => JE::Object::Function->new({
304 scope => $global,
305 name => 'indexOf',
306 length => 1,
307 argnames => [qw/searchString position/],
308 no_proto => 1,
309 function_args => ['this','args'],
310 18 ▶ function => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
311 18 my $str = shift->to_string->value16;
312 18 ▶ my $find = defined $_[0]
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 18 0 18
313 ? $_[0]->to_string->value16
314 : 'undefined';
315 18 ▶ my $start = defined $_[1]
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 9 9 18
316 ? $_[1]->to_number
317 ->value
318 : 0;
319 18 ▶ JE::Number->new($global,
Branch: 2 branches
BranchTrueFalseTotal
$start > length $str ? : 2 16 18
length $find ? : 1 1 2
320 # In -DDEBUGGING builds of perl (as
321 # of 5.13.2), a $start greater than
322 # the length causes a panick, so we
323 # avoid passing that to index(), as
324 # of version 0.049.
325 $start > length $str
326 ? length $find ? -1 : length $str
327 : index $str, $find, $start
328 );
329 },
330 19 }),
331 dontenum => 1,
332 });
333  
334 $proto->prop({
335 name => 'lastIndexOf',
336 value => JE::Object::Function->new({
337 scope => $global,
338 name => 'lastIndexOf',
339 length => 1,
340 argnames => [qw/searchString position/],
341 no_proto => 1,
342 function_args => ['this','args'],
343 18 ▶ function => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
344 18 my $string = shift->to_string->value16;
345 18 my $pos = length $string;
346 18 ▶ if(defined $_[1] && $_[1]->id ne 'undef') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1] and $_[1]->id ne "undef") 8 10 18
Condition100%3/3
defined $_[1] and $_[1]->id ne "undef"
!ll&&!rl&&r
918
MC/DC100%2/2
defined $_[1] and $_[1]->id ne "undef"
defined $_[1] $_[1]->id ne "undef"
Truth table100%3/3
A and B
A= defined $_[1]B= $_[1]->id ne "undef"
ABresult
0X0
100
111
347 8 my $p = $_[1]->to_number->value;
348 8 ▶ $p < $pos and $pos = $p
Branch: 1 branch
BranchTrueFalseTotal
$p < $pos and $pos = $p 6 2 8
349 }
350 18 ▶ JE::Number->new($global, rindex
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 18 0 18
351 $string,
352 defined $_[0]
353 ? $_[0]->to_string->value16
354 : 'undefined',
355 $pos
356 );
357 },
358 19 }),
359 dontenum => 1,
360 });
361  
362 $proto->prop({ # ~~~ I need to figure out how to deal with
363 # locale settings
364 name => 'localeCompare',
365 value => JE::Object::Function->new({
366 scope => $global,
367 name => 'localeCompare',
368 argnames => [qw/that/],
369 no_proto => 1,
370 function_args => ['this','args'],
371 0 ▶ function => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
372 0 my($this,$that) = @_;
373 0 ▶ JE::Number->new($global,
Branch: 1 branch
BranchTrueFalseTotal
$this->to_string->value cmp defined $that ? : 0 0 0
374 $this->to_string->value
375 cmp
376 defined $that
377 ? $that->to_string->value
378 : 'undefined'
379 );
380 },
381 19 }),
382 dontenum => 1,
383 });
384  
385 $proto->prop({
386 name => 'match',
387 value => JE::Object::Function->new({
388 scope => $global,
389 name => 'match',
390 argnames => [qw/regexp/],
391 no_proto => 1,
392 function_args => ['this','args'],
393 120 ▶ function => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
394 120 my($str, $re_obj) = @_;
395  
396 120 $str = $str->to_string;
397  
398 !defined $re_obj ||
399 119 (eval{$re_obj->class}||'') ne 'RegExp'
400 120 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
!defined($re_obj) || (eval { do { $re_obj->class } } || "") ne "RegExp" and do { require JE::Object::RegExp; $re_obj = "JE::Object::RegExp"->new($global, $re_obj) } 20 100 120
Condition100%5/5
not defined $re_obj or (eval { do { $re_obj->class } } || "") ne "RegExp"
l!l&&r!l&&!r
119100
eval { do { $re_obj->class } } || ''
l!l
1163
MC/DC100%2/2
not defined $re_obj or (eval { do { $re_obj->class } } || "") ne "RegExp"
not defined $re_obj eval { do { $re_obj->class } }
Truth table100%3/3
not(A) or B
A= not defined $re_objB= eval { do { $re_obj->class } }
ABresult
1X1
011
000
401 20 require JE::Object::RegExp;
402 20 $re_obj =
403 JE::Object::RegExp->new($global,
404 $re_obj);
405 };
406
407 120 my $re = $re_obj->value;
408  
409 # For non-global patterns and string, reg-
410 # exps, just return the fancy array result-
411 # from a call to String.prototype.exec
412  
413 120 ▶ if (not $re_obj->prop('global')->value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($re_obj->prop("global")->value) 117 3 120
414 117 return $global
415 ->prototype_for('RegExp')
416 ->prop('exec')
417 ->apply($re_obj, $str);
418 }
419  
420 # For global patterns, I just do the
421 # matching here, since it's faster.
422  
423 # ~~~ Problem: This is meant to call String
424 # .prototype.exec, according to the spec,
425 # which method can, of course, be replaced
426 # with a user-defined function. So much for
427 # this optimisation. (But, then, no one
428 # else follows the spec!)
429
430 3 $str = $str->value16;
431  
432 3 my @ary;
433 3 ▶ while($str =~ /$re/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /$re/g) 84 3 87
434 84 push @ary, JE::String->_new($global,
435 substr $str, $-[0],
436 $+[0] - $-[0]);
437 }
438
439 3 JE::Object::Array->new($global, \@ary);
440 },
441 19 }),
442 dontenum => 1,
443 });
444  
445 $proto->prop({
446 name => 'replace',
447 value => JE::Object::Function->new({
448 scope => $global,
449 name => 'replace',
450 argnames => [qw/searchValue replaceValue/],
451 no_proto => 1,
452 function_args => ['this','args'],
453 55 ▶ function => sub {
Subroutine __ANON__: called CC=13 SCAR=25.6
454 55 my($str, $foo, $bar) = @_;
455 # as in s/foo/bar/
456  
457 55 $str = $str->to_string->value16;
458  
459 55 my $g; # global?
460 55 ▶ if(defined $foo && $foo->can('class') &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $foo and $foo->can('class') and $foo->class eq 'RegExp') { } 35 20 55
Condition100%6/6
defined $foo and $foo->can("class") and $foo->class eq "RegExp"
!ll&&!rl&&r
31735
defined $foo and $foo->can("class")
!ll&&!rl&&r
1252
MC/DC100%3/3
defined $foo and $foo->can("class") and $foo->class eq "RegExp"
defined $foo $foo->can("class") $foo->class eq "RegExp"
Truth table100%4/4
A and B and C
A= defined $fooB= $foo->can("class")C= $foo->class eq "RegExp"
ABCresult
0XX0
10X0
1100
1111
461 $foo->class eq 'RegExp')
462 {
463 35 $g = $foo->prop('global')->value;
464 35 $foo = $$$foo{value};
465 }
466 else {
467 20 $g = !1;
468 20 ▶ $foo = defined $foo
Branch: 1 branch
BranchTrueFalseTotal
defined $foo ? : 19 1 20
469 ? quotemeta $foo->to_string->value16
470 : 'undefined';
471 }
472  
473 55 ▶ if (defined $bar && $bar->can('class') &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bar and $bar->can('class') and $bar->class eq 'Function') { } 21 34 55
Condition100%6/6
defined $bar and $bar->can("class") and $bar->class eq "Function"
!ll&&!rl&&r
82621
defined $bar and $bar->can("class")
!ll&&!rl&&r
2647
MC/DC100%3/3
defined $bar and $bar->can("class") and $bar->class eq "Function"
defined $bar $bar->can("class") $bar->class eq "Function"
Truth table100%4/4
A and B and C
A= defined $barB= $bar->can("class")C= $bar->class eq "Function"
ABCresult
0XX0
10X0
1100
1111
474 $bar->class eq 'Function') {
475 33 ▶ my $replace = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
476 33 $global
477 ->prototype_for('RegExp')
478 ->prop('constructor')
479 ->capture_re_vars($str);
480
481 33 $_[0]->call(
482 JE::String->_new($global,
483 substr $str, $-[0],
484 $+[0] - $-[0]),
485 map(JE::String->_new(
486 $global,
487 $JE'Object'RegExp'Match[$_]
488 ), 1..$#+),
489 JE::Number->new($global,
490 $-[0]),
491 $_[1]
492 )->to_string->value16
493 21 };
494  
495 21 my $je_str = JE::String->_new(
496 $global, $str);
497  
498 21 ▶ $g
Branch: 1 branch
BranchTrueFalseTotal
$g ? : 7 14 21
499 ? $str =~ s/$foo/
500 19 &$replace($bar, $je_str)
501 /ge
502 : $str =~ s/$foo/
503 14 &$replace($bar, $je_str)
504 /e
505 }
506 else {
507 # replacement string instead of
508 # function (a little tricky)
509  
510 # We need to use /ee and surround
511 # bar with double quotes, so that
512 # '$1,$2' becomes eval '"$1,$2"'.
513 # And so we also have to quotemeta
514 # the whole string.
515  
516 # I know the indiscriminate
517 # untainting may seem a little
518 # dangerous, but quotemeta takes
519 # care of it.
520 $bar = defined $bar
521 34 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
defined $bar ? : 32 2 34
522 32 $bar->to_string->value16
523 =~ /(.*)/s; # untaint
524 32 quotemeta $1
525 }
526 : 'undefined';
527  
528 # now $1, $&, etc have become \$1,
529 # \$\& ...
530  
531 34 $bar =~ s/\\\$(?:
532 \\([\$&`'])
533 |
534 ([1-9][0-9]?|0[0-9])
535 )/
536 30 ▶ defined $1 ? $_replace{$1}
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 13 17 30
537 : "\$JE::Object::RegExp::"
538 . "Match[$2]";
539 /gex;
540  
541 34 my $orig_str = $str;
542 101 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
543 34 ▶ $g ? $str =~ s/$foo/qq'"$bar"'/gee
Branch: 2 branches
BranchTrueFalseTotal
$g ? $str =~ s/$foo/qq["$bar"];/gee : $str =~ s/$foo/qq["$bar"];/ee and $global->prototype_for("RegExp")->prop("constructor")->capture_re_vars($orig_str) 33 1 34
$g ? : 11 23 34
544 23 : $str =~ s/$foo/qq'"$bar"'/ee
545 and
546 $global
547 ->prototype_for('RegExp')
548 ->prop('constructor')
549 ->capture_re_vars($orig_str);
550 }
551
552 55 JE::String->_new($global, $str);
553 },
554 19 }),
555 dontenum => 1,
556 });
557  
558 $proto->prop({
559 name => 'search',
560 value => JE::Object::Function->new({
561 scope => $global,
562 name => 'search',
563 argnames => [qw/regexp/],
564 no_proto => 1,
565 function_args => ['this','args'],
566 function =>
567 40 ▶sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
568 40 my($str, $re) = @_;
569  
570 39 $re = defined $re ?(eval{$re->class}||'')eq 'RegExp' ? $re->value :
571 40 ▶ do {
Branch: 2 branches
BranchTrueFalseTotal
defined $re ? : 39 1 40
(eval { do { $re->class } } || '') eq 'RegExp' ? : 24 15 39
Condition100%2/2
eval { do { $re->class } } || ''
l!l
363
MC/DC100%1/1
eval { do { $re->class } } || ''
eval { do { $re->class } }
Truth table100%2/2
A
A= eval { do { $re->class } }
Aresult
11
00
572 15 require JE::Object::RegExp;
573 15 JE::Object::RegExp->new($global, $re)->value
574 } : qr//;
575  
576 40 ▶ return JE::Number->new(
Branch: 1 branch
BranchTrueFalseTotal
($str = $str->to_string->value16) =~ /$re/ ? : 27 13 40
577 $global,
578 ($str = $str->to_string->value16) =~ $re
579 ? scalar(
580 $global->prototype_for('RegExp')
581 ->prop('constructor')
582 ->capture_re_vars($str),
583 $-[0]
584 )
585 :-1
586 );
587 }
588 19 }),
589 dontenum => 1,
590 });
591  
592 $proto->prop({
593 name => 'slice',
594 value => JE::Object::Function->new({
595 scope => $global,
596 name => 'slice',
597 argnames => [qw/start end/],
598 no_proto => 1,
599 function_args => ['this','args'],
600 24 ▶ function => sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
601 24 my($str, $start, $end) = @_;
602  
603 24 $str = $str->to_string->value16;
604 24 my $length = length $str;
605  
606 24 ▶if (defined $start) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start) { } 23 1 24
607 23 $start = int $start->to_number->value;
608 23 ▶ $start = $start == $start && $start; # change nan to 0
Condition100%3/3
$start == $start && $start
!ll&&!rl&&r
2615
MC/DC100%2/2
$start == $start && $start
$start == $start $start
Truth table100%3/3
A && B
A= $start == $startB= $start
ABresult
0X0
100
111
609  
610 23 ▶ $start >= $length and return JE::String->_new($global, '');
Branch: 1 branch
BranchTrueFalseTotal
$start >= $length and return "JE::String"->_new($global, "") 1 22 23
611 # $start < 0 and $start = 0;
612 }
613 1 else { $start = 0; }
614  
615 23 ▶if (defined $end && $end->id ne 'undef') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $end and $end->id ne 'undef') { } 15 8 23
Condition100%3/3
defined $end and $end->id ne "undef"
!ll&&!rl&&r
7115
MC/DC100%2/2
defined $end and $end->id ne "undef"
defined $end $end->id ne "undef"
Truth table100%3/3
A and B
A= defined $endB= $end->id ne "undef"
ABresult
0X0
100
111
616 15 $end = int $end->to_number->value;
617 15 ▶ $end = $end == $end && $end;
Condition100%3/3
$end == $end && $end
!ll&&!rl&&r
1113
MC/DC100%2/2
$end == $end && $end
$end == $end $end
Truth table100%3/3
A && B
A= $end == $endB= $end
ABresult
0X0
100
111
618  
619 15 ▶ $end > 0 and $end -= $start + $length * ($start < 0);
Branch: 1 branch
BranchTrueFalseTotal
$end > 0 and $end -= $start + $length * ($start < 0) 10 5 15
620 }
621 8 else { $end = $length }
622  
623 23 return JE::String->_new($global, substr $str, $start, $end);
624  
625 },
626 19 }),
627 dontenum => 1,
628 });
629  
630  
631 =begin split-notes
632  
633 If the separator is a non-empty string, we need to quotemeta it.
634  
635 If we have an empty string, we mustn’t allow a match at the end of the
636 string, so we use qr/(?!\z)/.
637  
638 If we have a regexp, then there are several issues:
639  
640 A successful zero-width match that occurs at the same position as
641 the end of the previous match needs to be turned into a failure
642 (no backtracking after the initial successful match), so as to
643 produce the aardvark result below. We could accomplish this by
644 wrapping it in an atomic group: /(?> ... )/. But then we come
645 to the second issue:
646  
647 To ensure correct (ECMAScript-compliant) capture erasure in cases
648 like 'cbazyx'.split(/(a|(b))+/) (see 15.10-regexps-objects.t), we need to
649 use @JE::Object::RegExp::Match, rather than $1, $2, etc.
650  
651 This precludes the use of perl’s split operator (at least for regexp sepa-
652 rators), so we have to implement it ourselves.
653  
654 We also need to make sure that a null match does not occur at the end of
655 a string. Since a successful match that begins at the end of a string can-
656 not but be a zero-length match, we could conceptually put (?!\z) at the
657 beginning of the match, but qr/...$a_qr_with_embedded_code/ causes weird
658 scoping issues (the embedded code, although it was compiled in a separate
659 package, somehow makes its way into the package that combined it in a
660 larger regexp); hence the somewhat complex logic below.
661  
662  
663 join ',', split /a*?/, 'aardvark' gives ',,r,d,v,,r,k'
664 'aardvark'.split(/a*?/).toString() gives 'a,a,r,d,v,a,r,k'
665  
666 -----
667 JS's 'aardvark'.split('', 3) means (split //, 'aardvark', 4)[0..2]
668  
669 =end split-notes
670  
671 =cut
672  
673  
674 $proto->prop({
675 name => 'split',
676 value => JE::Object::Function->new({
677 scope => $global,
678 name => 'split',
679 argnames => [qw/separator limit/],
680 no_proto => 1,
681 function_args => ['this','args'],
682 8 ▶ function => sub {
Subroutine __ANON__: called CC=19 SCAR=31.8
683 8 my($str, $sep, $limit) = @_;
684  
685 8 $str = (my $je_str = $str->to_string)
686 ->value16;
687  
688 8 ▶ if(!defined $limit ||
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $limit or $limit->id eq 'undef') { } 7 1 8
elsif (defined $limit) { } 1 0 1
Condition66%2/3
not defined $limit or $limit->id eq "undef"
l!l&&r!l&&!r
701
MC/DC50%1/2
not defined $limit or $limit->id eq "undef"
not defined $limit $limit->id eq "undef"
Truth table66%2/3
not(A) or B
A= not defined $limitB= $limit->id eq "undef"
ABresult
1X1
011
000
689 $limit->id eq 'undef') {
690 7 $limit = -2;
691 }
692 elsif(defined $limit) {
693 1 $limit = int($limit->to_number->value)
694 % 2 ** 32;
695 1 ▶ $limit = $limit == $limit && $limit;
Condition33%1/3
$limit == $limit && $limit
!ll&&!rl&&r
001
MC/DC0%0/2
$limit == $limit && $limit
$limit == $limit $limit
Truth table33%1/3
A && B
A= $limit == $limitB= $limit
ABresult
0X0
100
111
696 # Nan --> 0
697 }
698
699 8 ▶ if (defined $sep) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sep) { } 8 0 8
700 8 ▶ if ($sep->can('class')
Branch: 2 branches
BranchTrueFalseTotal
if ($sep->can('class') and $sep->class eq 'RegExp') { } 2 6 8
elsif ($sep->id eq 'undef') { } 0 6 6
Condition66%2/3
$sep->can("class") and $sep->class eq "RegExp"
!ll&&!rl&&r
062
MC/DC50%1/2
$sep->can("class") and $sep->class eq "RegExp"
$sep->can("class") $sep->class eq "RegExp"
Truth table66%2/3
A and B
A= $sep->can("class")B= $sep->class eq "RegExp"
ABresult
0X0
100
111
701 && $sep->class eq 'RegExp') {
702 2 $sep = $sep->value;
703 }
704 elsif($sep->id eq 'undef') {
705 0 return JE::Object::Array->new(
706 $global, $je_str
707 );
708 }
709 else {
710 6 $sep = $sep->to_string->value16;
711 }
712 }
713 else {
714 0 return JE::Object::Array->new(
715 $global, $je_str
716 );
717 }
718
719 8 my @split;
720  
721 8 ▶ if (!ref $sep) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $sep) 6 2 8
722 6 ▶ $sep = length $sep ? quotemeta $sep :
Branch: 1 branch
BranchTrueFalseTotal
length $sep ? : 6 0 6
723 qr/(?!\z)/;
724 6 @split = split $sep, $str, $limit+1;
725 6 goto returne;
726 }
727  
728 2 ▶ !length $str and
Branch: 1 branch
BranchTrueFalseTotal
length $str or "" =~ /$sep/ || (@split = ""), goto returne 2 0 2
Condition0%0/3
"" =~ /$sep/ or @split = ""
l!l&&r!l&&!r
000
MC/DC0%0/2
"" =~ /$sep/ or @split = ""
"" =~ /$sep/ @split = ""
Truth table0%0/3
A or B
A= "" =~ /$sep/B= @split = ""
ABresult
1X1
011
000
729 ''=~ $sep || (@split = ''),
730 goto returne;
731
732 2 my$pos = 0;
733 2 ▶ while($str =~ /$sep/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /$sep/cg) 65 1 66
734 65 ▶ $pos == pos $str and ++pos $str, next;
Branch: 1 branch
BranchTrueFalseTotal
$pos == pos $str and ++pos($str), next 32 33 65
735 # That ++pos won’t go past the end of
736 # the string, so it may end up being a
737 # no-op; but the ‘next’ bypasses the
738 # pos assignment below, so perl refuses
739 # to match again at the same position.
740 33 ▶ $-[0] == length $str and last;
Branch: 1 branch
BranchTrueFalseTotal
$-[0] == length $str and last 1 32 33
741 32 push @split,substr($str,$pos,$-[0]-$pos),
742 @JE::Object::RegExp::Match[
743 1..$#JE::Object::RegExp::Match
744 ];
745 32 $pos = pos $str = pos $str;
746 # Assigning pos to itself has the same
747 # effect as using an atomic group
748 # with split
749 }
750 2 push @split, substr($str, $pos);
751  
752 8 ▶ returne:
Branch: 3 branches
BranchTrueFalseTotal
$limit == -2 ? : 7 1 8
@split ? : 1 0 1
@split > $limit ? : 0 1 1
753 JE::Object::Array->new($global,
754 $limit == -2 ? @split : @split ? @split[
755 0..(@split>$limit?$limit-1:$#split)
756 ] : @split);
757  
758 },
759 19 }),
760 dontenum => 1,
761 });
762  
763 $proto->prop({
764 name => 'substring',
765 value => JE::Object::Function->new({
766 scope => $global,
767 name => 'substring',
768 argnames => [qw/start end/],
769 no_proto => 1,
770 function_args => ['this','args'],
771 81 ▶ function => sub {
Subroutine __ANON__: called CC=8 SCAR=21.6
772 81 my($str, $start, $end) = @_;
773  
774 81 $str = $str->to_string->value16;
775 81 my $length = length $str;
776  
777  
778 81 ▶if (defined $start) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start) { } 81 0 81
779 81 $start = int $start->to_number->value;
780 81 ▶ $start >= 0 or $start = 0;
Branch: 1 branch
BranchTrueFalseTotal
$start >= 0 or $start = 0 81 0 81
781 }
782 0 else { $start = 0; }
783  
784  
785 81 ▶if (!defined $end || $end->id eq 'undef') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $end or $end->id eq 'undef') { } 5 76 81
Condition66%2/3
not defined $end or $end->id eq "undef"
l!l&&r!l&&!r
5076
MC/DC50%1/2
not defined $end or $end->id eq "undef"
not defined $end $end->id eq "undef"
Truth table66%2/3
not(A) or B
A= not defined $endB= $end->id eq "undef"
ABresult
1X1
011
000
786 5 $end = $length;
787 }
788 else {
789 76 $end = int $end->to_number->value;
790 76 ▶ $end >= 0 or $end = 0;
Branch: 1 branch
BranchTrueFalseTotal
$end >= 0 or $end = 0 76 0 76
791 }
792  
793 81 ▶$start > $end and ($start,$end) = ($end,$start);
Branch: 1 branch
BranchTrueFalseTotal
$start > $end and () = ($end, $start) 0 81 81
794  
795 101 ▶no warnings 'substr'; # in case start > length
Subroutine BEGIN: called CC=1 SCAR=0.0
796 81 my $x= substr $str, $start, $end-$start;
797 81 ▶return JE::String->_new($global, defined $x ? $x : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $x ? : 81 0 81
798  
799 },
800 19 }),
801 dontenum => 1,
802 });
803  
804 $proto->prop({
805 name => 'toLowerCase',
806 value => JE::Object::Function->new({
807 scope => $global,
808 name => 'toLowerCase',
809 no_proto => 1,
810 function_args => ['this'],
811 0 ▶ function => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
812 0 my $str = shift;
813  
814 0 JE::String->_new($global,
815 lc $str->to_string->value16);
816 },
817 19 }),
818 dontenum => 1,
819 });
820  
821 $proto->prop({
822 name => 'toLocaleLowerCase',
823 value => JE::Object::Function->new({
824 scope => $global,
825 name => 'toLocaleLowerCase',
826 no_proto => 1,
827 function_args => ['this'],
828 0 ▶ function => sub { # ~~~ locale settings?
Subroutine __ANON__: not called CC=1 SCAR=6.9
829 0 my $str = shift;
830  
831 0 JE::String->_new($global,
832 lc $str->to_string->value);
833 },
834 19 }),
835 dontenum => 1,
836 });
837  
838 $proto->prop({
839 name => 'toUpperCase',
840 value => JE::Object::Function->new({
841 scope => $global,
842 name => 'toUpperCase',
843 no_proto => 1,
844 function_args => ['this'],
845 0 ▶ function => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
846 0 my $str = shift;
847  
848 0 JE::String->_new($global,
849 uc $str->to_string->value16);
850 },
851 19 }),
852 dontenum => 1,
853 });
854  
855 $proto->prop({
856 name => 'toLocaleUpperCase',
857 value => JE::Object::Function->new({
858 scope => $global,
859 name => 'toLocaleUpperCase',
860 no_proto => 1,
861 function_args => ['this'],
862 0 ▶ function => sub { # ~~~ locale settings?
Subroutine __ANON__: not called CC=1 SCAR=6.9
863 0 my $str = shift;
864  
865 0 JE::String->_new($global,
866 uc $str->to_string->value);
867 },
868 19 }),
869 dontenum => 1,
870 });
871  
872 $proto->prop({
873 name => 'substr',
874 value => JE::Object::Function->new({
875 scope => $global,
876 name => 'substr',
877 argnames => [qw/start length/],
878 no_proto => 1,
879 function_args => ['this','args'],
880 4 ▶ function => sub {
Subroutine __ANON__: called CC=5 SCAR=18.7
881 4 my($str, $start, $len) = @_;
882  
883 4 $str = $str->to_string->value16;
884  
885 4 ▶if (defined $start) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start) { } 4 0 4
886 4 $start = int $start->to_number->value;
887 }
888 0 else { $start = 0; }
889  
890  
891 4 ▶if (!defined $len || $len->id eq 'undef') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $len or $len->id eq 'undef') { } 0 4 4
Condition33%1/3
not defined $len or $len->id eq "undef"
l!l&&r!l&&!r
004
MC/DC0%0/2
not defined $len or $len->id eq "undef"
not defined $len $len->id eq "undef"
Truth table33%1/3
not(A) or B
A= not defined $lenB= $len->id eq "undef"
ABresult
1X1
011
000
892 0 $len = undef;
893 }
894 else {
895 4 $len = int $len->to_number->value;
896 }
897  
898 4 ▶return JE::String->_new($global, defined $len ?
Branch: 1 branch
BranchTrueFalseTotal
defined $len ? : 4 0 4
899 (substr $str, $start, $len) :
900 (substr $str, $start)
901 );
902  
903 },
904 19 }),
905 dontenum => 1,
906 });
907  
908 19 $f;
909 }
910  
911  
912  
913 101 return "a true value";
914  
915 =back
916  
917 =head1 SEE ALSO
918  
919 =over 4
920  
921 =item JE
922  
923 =item JE::Types
924  
925 =item JE::String
926  
927 =item JE::Object
928  
929 =back
930  
931 =cut
932  
933  
934  
935  
« JE/Destroyer.pm ↑ Summary JE/Object/Date.pm »