File: set_globals.t

package info (click to toggle)
libjavascript-quickjs-perl 0.21-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,180 kB
  • sloc: ansic: 72,822; javascript: 7,743; perl: 1,065; makefile: 353; sh: 108
file content (145 lines) | stat: -rw-r--r-- 2,946 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Test::Deep;
use Test::FailWarnings;
use Data::Dumper;
use Config;

use Types::Serialiser;

use JavaScript::QuickJS;

my $js = JavaScript::QuickJS->new()->set_globals(
    add1 => sub { $_[0] + 1 },
);

is( $js->eval("add1(23)"), 24, 'JS calls Perl' );

my $is_64bit = $Config{'use64bitint'};

my @roundtrip = (
    "hello",
    "\xe9",
    "\x{100}",
    "\x{101234}",
    -1,
    -1.234,
    0,
    1.234,
    0xffff_ffff,
    -0x8000_0000,

    $js->eval('Number.MAX_SAFE_INTEGER'),
    $js->eval('Number.MIN_SAFE_INTEGER'),

    ($is_64bit
        ? (map { $_, -$_ } 0xffff_ffff << 17)
        : ()
    ),

    [1, 2, 3],
    [],
    {},
    { foo => 'bar' },
    { "\x{100}" => [] },

    # Test magic:
    \%Config,
);

for my $rtval (@roundtrip) {
    my $key = 'rtval';

    $js->set_globals( $key => $rtval );

    my $got = $js->eval($key);

    # Perl’s NV stringification loses precision once we exceed
    # 10^15. To minimize the chance that that will cause a problem,
    # we add 0 to encourage Perl to convert to an IV if possible.
    #
    if ($got =~ m<\A-?[0-9]+(?:\.[0-9]+)\z>) {
        $got += 0;
    }

    local $Data::Dumper::Useqq =1;
    local $Data::Dumper::Terse = 1;
    local $Data::Dumper::Indent = 0;
    my $str = Dumper($rtval);

    my $alt_double_yn = $Config{'uselongdouble'} || $Config{'usequadmath'};

    my $allow_approx = $alt_double_yn ? ($rtval !~ tr<0-9.-><>c) : ($rtval =~ tr<.><>);

    if ($allow_approx) {
        cmp_deeply($got, num($rtval, 0.01), "gave & received: $str" );
    }
    else {
        is_deeply($got, $rtval, "gave & received: $str" );
    }
}

eval { $js->set_globals( regexp => qr/abc/ ) };
my $err = $@;

like($err, qr<abc>, 'error mentions what can’t be converted');
like($err, qr<javascript>i, 'error mentions JS');


$js->set_globals(
    mytrue => Types::Serialiser::true(),
    myfalse => Types::Serialiser::false(),
);

my $got = $js->eval(<<END);
    [ typeof mytrue, mytrue, typeof myfalse, myfalse ]
END

cmp_deeply(
    $got,
    [ 'boolean', bool(1), 'boolean', bool(0) ],
    'Types::Serialiser',
);

#----------------------------------------------------------------------

if ($is_64bit) {
    my $ivmax = "\x7f\0\0\0\0\0\0\0";

    my $ivmax_plus1 = "\x80\0\0\0\0\0\0\0";

    my $ivmin = "\x80\0\0\0\0\0\0\0";

    if ($Config{'byteorder'} == '87654321') {
        $_ = reverse $_ for ($ivmax, $ivmax_plus1, $ivmin);
    }

    my @t = (
        [ '> IV_MAX', unpack('Q', $ivmax) ],
        [ '<= IV_MAX', unpack('Q', $ivmax_plus1) ],
        [ 'negative 64-bit', unpack('q', $ivmin) ],
    );

    for my $tt (@t) {
        my ($label, $expect) = @$tt;

        $js->set_globals(
            bignum => $expect,
        );

        my $got = $js->eval('bignum');

        cmp_deeply(
            $got,
            num($expect, 100),
            $label,
        );
    }

}

done_testing;