File: 06-bind-object.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 (107 lines) | stat: -rw-r--r-- 1,792 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
#!perl

package Foo;

use strict;
use warnings;

use Test::More tests => 13;

use JavaScript;

sub new { return bless {}, __PACKAGE__; }

sub bar { 
    my $self = shift; 
    return 5;
}

sub baz { 
    my $self = shift; 
    return "five"; 
}

sub getWrap {
    my ($self) = @_;
    $self->{"getter_called"} = 1;
    $self->{"wrapped"};
}

sub setWrap {
    my ($self,$value) = @_;
    $self->{"setter_called"} = 1;
    $self->{"wrapped"} = $value;
}

# Create a new runtime
my $rt1 = new JavaScript::Runtime();

# Create a new context
my $cx1 = $rt1->create_context();

$cx1->bind_class(
        name => 'Foo',
        constructor => sub { return Foo->new(); },
        methods => {
            bar => \&Foo::bar,
            baz => \&Foo::baz,
        },
        properties => {
            std => 0,
            wrapped_value => {
                flags => JS_PROP_ACCESSOR,
                setter => \&Foo::setWrap,
                getter => \&Foo::getWrap,
            },  
        },
        package => 'Foo'
);

my $foo = new Foo();

$cx1->bind_object('FooSan', $foo);

isa_ok($cx1->eval("FooSan;"), "Foo");

is($cx1->eval("FooSan.bar();"), 5);

$cx1->eval(q{
FooSan.std = 1;
});

is($foo->{std}, 1);

$foo->{std} = 3;

is($cx1->eval(q{ FooSan.std }), 3);

$cx1->eval(q!
FooSan.wrapped_value = 1;
!);

ok($foo->{"setter_called"});

ok($foo->{wrapped} == 1);

ok($cx1 && ref($cx1)); # somehow disappeared during development

$foo->{wrapped} = 2;

ok($cx1->eval(q{
    FooSan.wrapped_value
}) == 2);
ok($foo->{"getter_called"});

ok($cx1 && ref($cx1)); # somehow disappeared during development


$cx1->eval(q{
FooSan.wrapped_value = FooSan.wrapped_value + 1;
});
ok($foo->{"getter_called"});

ok($cx1 && ref($cx1)); # somehow disappeared during development

ok($foo->{wrapped} == 3);