BarefootJS
view release on metacpan or search on metacpan
t/primitive_vectors.t view on Meta::CPAN
return $v;
}
my $bf = bless { c => undef, config => {}, backend => undef }, 'BarefootJS';
# One binding per op, to the BarefootJS helper compiled templates call. An
# op with no binding fails.
my %bindings = (
truthy => sub { $bf->truthy($_[0]) },
to_number => sub { $bf->to_number($_[0]) },
to_string => sub { $bf->to_string($_[0]) },
get => sub { $bf->get($_[0], $_[1]) },
strict_eq => sub { $bf->strict_eq($_[0], $_[1]) },
lt => sub { $bf->lt($_[0], $_[1]) },
le => sub { $bf->le($_[0], $_[1]) },
gt => sub { $bf->gt($_[0], $_[1]) },
ge => sub { $bf->ge($_[0], $_[1]) },
plus => sub { $bf->plus($_[0], $_[1]) },
neg => sub { $bf->neg($_[0]) },
number => sub { $bf->number($_[0]) },
);
# One binding per op the Perl adapters can spell natively, to the native
# Perl operator: a test is Perl's own truthiness (`if`, `!`, `&&`, `||`,
# `?:`); an array index read is Perl's own `->[]`; a number literal is a
# Perl numeric literal, which both template languages compile to; a
# relational operator is Perl's numeric `<` / `<=` / `>` / `>=`; a unary `+`
# over a number is the operand itself; a strict equality is `eq` over two
# strings and `==` otherwise. It runs
# over the cases whose domain the two adapters' NATIVE_SAFE tables claim
# (primitives-native-safe.json, "perl": their union, since they share these
# operators; spec/js-primitive-semantics.md, "Conformance" item 3).
my %native_bindings = (
truthy => sub { $_[0] ? !!1 : !!0 },
get => sub { $_[0]->[ $_[1] ] },
number => sub { my $v = eval $_[0]; die $@ if $@; $v },
to_number => sub { $_[0] },
lt => sub { $_[0] < $_[1] },
le => sub { $_[0] <= $_[1] },
gt => sub { $_[0] > $_[1] },
ge => sub { $_[0] >= $_[1] },
# A proven string is its own ToString; a string `+` is Perl's `.` (Kolon's
# `~`), the other operand through `to_string` (#3824 step 4).
to_string => sub { $_[0] },
plus => sub {
my ($a, $b) = @_;
my $str = sub { BarefootJS::Evaluator::_is_string($_[0]) ? $_[0] : $bf->to_string($_[0]) };
$str->($a) . $str->($b);
},
# Two proven strings compare with `eq` (Kolon's string `==`); two proven
# numbers or booleans with Perl's numeric `==` (#3824 step 8).
strict_eq => sub {
my ($a, $b) = @_;
my $both_strings = BarefootJS::Evaluator::_is_string($a) && BarefootJS::Evaluator::_is_string($b);
($both_strings ? $a eq $b : $a == $b) ? !!1 : !!0;
},
);
my $INF = 9**9**9;
my %SENTINEL = (
'NaN' => sub { $INF - $INF },
'Infinity' => sub { $INF },
'-Infinity' => sub { -$INF },
'-0' => sub { -1 / $INF },
);
# Arguments keep JSON::PP booleans: the evaluator primitives the helpers
# delegate to read them as JS booleans. `$num` sentinels become doubles.
sub decode_arg {
my ($v) = @_;
return VectorUndefined::arg() if VectorUndefined::is_sentinel($v);
return [ map { decode_arg($_) } @$v ] if ref $v eq 'ARRAY';
if (ref $v eq 'HASH') {
return $SENTINEL{ $v->{'$num'} }->() if keys %$v == 1 && exists $v->{'$num'};
return { map { $_ => decode_arg($v->{$_}) } keys %$v };
}
return $v;
}
my ($total, $passed) = (0, 0);
for my $file (@files) {
for my $case (@{ _read_json($file)->{cases} }) {
my $key = "$case->{fn}/$case->{note}";
$total++;
my ($ok, $detail) = run_case($case);
$passed++ if $ok;
ok($ok, $key) or diag("FAIL $key: $detail");
}
}
# Every native claim must give the JS answer on every case it covers.
my $claims = _read_json(File::Spec->catfile($vectors_dir, 'primitives-native-safe.json'))->{runtimes}{perl}
// die 'primitives-native-safe.json has no "perl" runtime';
my $native_ran = 0;
for my $fn (sort keys %$claims) {
my $bind = $native_bindings{$fn};
unless ($bind) {
fail("no native Perl binding for '$fn' â add it to %native_bindings");
next;
}
my %claimed = map { $_ => 1 } @{ $claims->{$fn} };
for my $case (@{ _read_json(File::Spec->catfile($vectors_dir, 'primitives', "$fn.json"))->{cases} }) {
next unless $claimed{ $case->{domain} };
$native_ran++;
my ($ok, $detail) = run_case($case, $bind);
ok($ok, "native $case->{fn}/$case->{note} (domain $case->{domain})") or diag("NATIVE-FAIL: $detail");
}
}
diag(sprintf('primitives: %d cases, %d pass; %d native claimed cases', $total, $passed, $native_ran));
done_testing;
sub run_case {
my ($case, $bind) = @_;
$bind //= $bindings{ $case->{fn} }
or return (0, "no Perl binding for '$case->{fn}' â add it to %bindings");
my @args = map { decode_arg($_) } @{ $case->{args} };
my $got = eval { $bind->(@args) };
return (0, "died: $@") if $@;
return (1, '') if match($got, $case->{expect});
return (0, 'got ' . explain_value($got) . ', want ' . explain_value($case->{expect}));
}
( run in 0.969 second using v1.01-cache-2.11-cpan-60884a8df76 )