1package JE::Boolean;
2
3our $VERSION = '0.066';
4
5
6use strict;
7use warnings;
8
9use overload fallback => 1,
10	'""' =>  sub { qw< false true >[shift->[0]] },
11#	 cmp =>  sub { "$_[0]" cmp $_[1] },
12	bool =>  sub { shift->[0] },
13	'0+' =>  sub { shift->[0] };
14
15# ~~~ to do: make sure it numifies properly (as 0 or 1)
16
17require JE::Object::Boolean;
18require JE::Number;
19require JE::String;
20
21
22sub new {
23	my($class, $global, $val) = @_;
24	bless [!!$val, $global], $class;
25}
26
27
28sub prop {
29	if(@_ > 2) { return $_[2] } # If there is a value, just return it
30
31	my ($self, $name) = @_;
32
33	$$self[1]->prototype_for('Boolean')->prop($name);
34}
35
36sub keys {
37	my $self = shift;
38	$$self[1]->prototype_for('Boolean')->keys;
39}
40
41sub delete {1}
42
43sub method {
44	my $self = shift;
45	$$self[1]->prototype_for('Boolean')->prop(shift)->apply(
46		$self,$$self[1]->upgrade(@_)
47	);
48}
49
50
51sub value { warn caller if !ref $_[0];shift->[0] }
52sub TO_JSON { \(0+shift->[0]) }
53
54sub exists { !1 }
55
56sub typeof    { 'boolean' }
57sub class     { 'Boolean' }
58sub id        { 'bool:' . shift->value }
59sub primitive { 1 }
60
61sub to_primitive { $_[0] }
62sub to_boolean   { $_[0] }
63
64
65# $_[0][1] is the global object
66sub to_string { JE::String->_new($_[0][1], qw< false true >[shift->[0]]) }
67sub to_number { JE::Number->new($_[0][1], shift->[0]) }
68sub to_object { JE::Object::Boolean->new($_[0][1], shift) }
69
70sub global { $_[0][1] }
71
72
73
741;
75__END__
76
77=head1 NAME
78
79JE::Boolean - JavaScript boolean value
80
81=head1 SYNOPSIS
82
83  use JE;
84  use JE::Boolean;
85
86  $j = JE->new;
87
88  $js_true  = new JE::Boolean $j, 1;
89  $js_false = new JE::Boolean $j, 0;
90
91  $js_true ->value; # returns 1
92  $js_false->value; # returns ""
93
94  "$js_true"; # returns "true"
95
96  $js_true->to_object; # returns a new JE::Object::Boolean
97
98=head1 DESCRIPTION
99
100This class implements JavaScript boolean values for JE. The difference
101between this and JE::Object::Boolean is that that module implements
102boolean
103I<objects,> while this module implements the I<primitive> values.
104
105The stringification and boolean operators are overloaded.
106
107=head1 SEE ALSO
108
109=over 4
110
111=item L<JE>
112
113=item L<JE::Types>
114
115=item L<JE::Object::Boolean>
116
117=back
118