File Coverage

blib/lib/JE/Object/Error.pm
Criterion Covered Total %
statement 38 39 97.4
branch 2 2 100.0
condition 4 9 44.4
subroutine 9 10 90.0
pod 3 3 100.0
total 56 63 88.8


line stmt bran cond sub pod time code
1             package JE::Object::Error;
2              
3             our $VERSION = '0.065';
4              
5              
6 101     101   19592 use strict;
  101         128  
  101         3281  
7 101     101   389 use warnings;
  101         130  
  101         57238  
8              
9             our @ISA = 'JE::Object';
10              
11             require JE::Object;
12             require JE::String;
13              
14              
15             # ~~~ Need to add support for line number, script name, etc., or perhaps
16             # just a reference to the corresponding JE::Code object.
17              
18             =head1 NAME
19              
20             JE::Object::Error - JavaScript Error object class
21              
22             =head1 SYNOPSIS
23              
24             use JE::Object::Error;
25              
26             # Somewhere in code called by an eval{}
27             die new JE::Object::Error $global, "(Error message here)";
28              
29             # Later:
30             $@->prop('message'); # error message
31             $@->prop('name'); # 'Error'
32             "$@"; # 'Error: ' plus the error message
33              
34             =head1 DESCRIPTION
35              
36             This class implements JavaScript Error objects for JE. This is the base
37             class for all JavaScript's native error objects. (See L, below.)
38              
39             =head1 METHODS
40              
41             See L for descriptions of most of the methods. Only what
42             is specific to JE::Object::Error is explained here.
43              
44             The C method returns the string S<'Error: '> followed by the error
45             message. 'Error' will be replaced with the class name (the result of
46             calling
47             C<< ->class >>) for subclasses.
48              
49             =cut
50              
51             sub new {
52 340     340 1 765 my($class, $global, $val) = @_;
53 340         1262 my($js_class) = $class->name;
54 340   33     1395 my $self = $class->SUPER::new($global, {
55             prototype => $global->prototype_for($js_class) ||
56             $global->prop($js_class)->prop('prototype')
57             });
58              
59 340 100 66     3007 $self->prop({
60             dontenum => 1,
61             name => 'message',
62             value => JE::String->_new($global, $val),
63             }) if defined $val and ref $val ne 'JE::Undefined';
64 340         2923 $self;
65             }
66              
67 0     0 1 0 sub value { $_[0]->method('toString')->value }
68              
69 44     44 1 130 sub class { 'Error' }
70             *name = *class;
71              
72             sub _new_constructor {
73 34     34   64 my $global = shift;
74             my $con = sub {
75 8     8   34 __PACKAGE__->new(@_);
76 34         362 };
77 34         150 my $args = ['scope','args'];
78 34         351 my $f = JE'Object'Function->new({
79             name => 'Error',
80             scope => $global,
81             argnames => ['message'],
82             function => $con,
83             function_args => $args,
84             constructor => $con,
85             constructor_args => $args,
86             });
87              
88 34         209 my $proto = bless $f->prop({
89             name => 'prototype', dontenum => 1, readonly => 1
90             });
91            
92 34         141 $global->prototype_for('Error',$proto);
93             $proto->prop({
94             name => 'toString',
95             value => JE::Object::Function->new({
96             scope => $global,
97             name => 'toString',
98             length => 0,
99             function_args => ['this'],
100             function => sub {
101 29     29   45 my $self = shift;
102 29         107 JE::String->_new(
103             $$$self{global},
104             $self->prop(
105             'name'
106             ) .
107             ': ' .
108             $self->prop(
109             'message' )
110             );
111             }
112 34         346 }),
113             dontenum => 1,
114             });
115 34         276 $proto->prop({
116             name => 'name',
117             value => JE::String->_new($global, 'Error'),
118             dontenum => 1,
119             });
120 34         149 $proto->prop({
121             name => 'message',
122             value => JE::String->_new($global,
123             'Unknown error'),
124             dontenum => 1,
125             });
126              
127 34         143 weaken $global;
128 34         307 $f
129             }
130              
131             sub _new_subclass_constructor {
132 42     42   89 my($package,$global) = @_;
133              
134             my $f = JE::Object::Function->new({
135             name => my $name = $package->name,
136             scope => $global,
137             argnames => ['message'],
138 38     38   172 function =>(sub { $package->new(@_) },
139 42         187 function_args => ['scope','args'],
140             constructor => # "
141             constructor_args => # "
142             )[ 0..3,0,4,2 ],
143             });
144              
145 42         263 my $proto = $f->prop({
146             name => 'prototype',
147             dontenum => 1,
148             readonly => 1,
149             });
150 42         161 $global->prototype_for($name=>$proto);
151 42         104 bless $proto, $package;
152 42   33     116 $proto->prototype(
153             $global->prototype_for('Error')
154             || $global->prop('Error')->prop('prototype')
155             );
156 42         188 $proto->prop({
157             name => 'name',
158             value => JE::String->_new($global, $name),
159             dontenum => 1,
160             });
161 42         486 (my $msg = $name) =~ s/(?!^)([A-Z])(?![A-Z])/ \l$1/g;
162 42         216 $proto->prop({
163             name => 'message',
164             value => JE::String->_new($global, $msg),
165             dontenum => 1,
166             });
167              
168 42         127 weaken $global;
169 42         322 $f;
170             }
171              
172              
173             return "a true value";
174              
175             =head1 SEE ALSO
176              
177             =over 4
178              
179             =item L
180              
181             =item L
182              
183             =item L
184              
185             =item L
186              
187             =item L
188              
189             =item L
190              
191             =item L
192              
193             =back
194              
195             =cut
196              
197              
198              
199