File: js_func_to_perl.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 (45 lines) | stat: -rw-r--r-- 858 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
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Test::FailWarnings;

use JavaScript::QuickJS;

my $js = JavaScript::QuickJS->new();

my $cb = $js->helpers->eval("(val) => val + 1");

isa_ok($cb, 'JavaScript::QuickJS::Function', 'JS callback');

is($cb->length(), 1, 'length() gives arity');
is($cb->name(), q<>, 'name()');

is(
    $cb->(2),
    3,
    'JS function called from Perl',
);

undef $cb;

pass 'Still alive (callback reaped)';

undef $js;

pass 'Still alive (JS reaped)';

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

$js = JavaScript::QuickJS->new();

my $cb2 = $js->eval("let f = function myFunc(foo, bar) {}; f");

isa_ok($cb2, 'JavaScript::QuickJS::Function', 'JS callback (declaration)');

is($cb2->length(), 2, 'length() gives arity');
is($cb2->name(), q<myFunc>, 'name()');

done_testing();