File: 15-function.t

package info (click to toggle)
libjavascript-perl 1.08-1%2Blenny1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 412 kB
  • ctags: 203
  • sloc: perl: 1,686; ansic: 1,620; makefile: 55
file content (58 lines) | stat: -rw-r--r-- 1,351 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
#!perl

use Test::More tests => 7;

use strict;
use warnings;

use JavaScript;

ok( my $rt1 = JavaScript::Runtime->new(), "created runtime" );
ok( my $cx1 = $rt1->create_context(), "created context" );

{
    my $context = $cx1;
    ok(
       !$cx1->bind_function(
                            name => 'test',
                            func => sub {
                                my $rv = $_[0]->();
                                ok( $rv, $rv ); $rv }
                        ),
       "bound function"
   );
}

$cx1->bind_function(
                    name => 'debug',
                    func => sub { warn Dumper(@_) });
my $code = <<EOC;

function perl_apply() {
    var args = new Array()
    for (var i = 0; i < arguments.length; i++) {
        args.push(arguments[i]);
    }

    var func = args.shift();
    return func.apply(func,args);
}

function testFunc() {
  return "called test function from perl space okay";
}

test( testFunc );

EOC
ok( my $rv = $cx1->eval( $code ), "eval'd code" );
is( $rv, "called test function from perl space okay", "roundtrip");

SKIP: {
    eval "use List::Util";
    skip ("List::Util is not installed", 1) if $@;
    no warnings 'once';
    is ($cx1->call('perl_apply', sub { return List::Util::reduce { $a + $b } @_ },
                   1, 2, 3, 4),
        10, 'invoke perlsub from javascript');
}