| 1 | package JE::Scope; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 101 | our $VERSION = '0.066'; | |||||||||||||
| 4 | |||||||||||||||
| 5 | 101 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 101 | ▶ | use warnings; no warnings 'utf8'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 101 | require JE::LValue; | |||||||||||||
| 9 | |||||||||||||||
| 10 | 101 | our $AUTOLOAD; | |||||||||||||
| 11 | |||||||||||||||
| 12 | # ~~~ We need a C<can> method. | ||||||||||||||
| 13 | |||||||||||||||
| 14 | 105031 | ▶ | sub find_var { | ||||||||||||
|
Subroutine
find_var: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 15 | 105031 | my ($self,$var) = @_; | |||||||||||||
| 16 | 105031 | my $lvalue; | |||||||||||||
| 17 | |||||||||||||||
| 18 | 105031 | for(reverse @$self) { | |||||||||||||
| 19 | 107272 | my $p = $_; | |||||||||||||
| 20 | 107272 | ▶ | defined($p=$p->prototype) or next while !$p->exists($var); | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 21 | 104461 | return new JE::LValue $_, $var; | |||||||||||||
| 22 | } | ||||||||||||||
| 23 | # if we get this far, then we create an lvalue without a base obj | ||||||||||||||
| 24 | 570 | new JE::LValue \$self->[0], $var; | |||||||||||||
| 25 | } | ||||||||||||||
| 26 | |||||||||||||||
| 27 | 758 | ▶ | sub new_var { | ||||||||||||
|
Subroutine
new_var: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||
| 28 | 758 | my ($self,$var) = (shift,shift); | |||||||||||||
| 29 | 758 | my $var_obj; | |||||||||||||
| 30 | 758 | for(reverse @$self[1..$#$self]) { # Object 0 can't be a call | |||||||||||||
| 31 | # object. Omitting it should the- | ||||||||||||||
| 32 | # oretically make things margin- | ||||||||||||||
| 33 | # ally faster. | ||||||||||||||
| 34 | 337 | ▶ | ref $_ eq 'JE::Object::Function::Call' and | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | $var_obj = $_, | ||||||||||||||
| 36 | last; | ||||||||||||||
| 37 | } | ||||||||||||||
| 38 | 758 | ▶ | defined $var_obj or $var_obj = $$self[0]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | |||||||||||||||
| 40 | 758 | ▶ | if (defined $var_obj->prop($var)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | 75 | ▶ | $var_obj->prop($var, shift) if @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | } | ||||||||||||||
| 43 | else { | ||||||||||||||
| 44 | 683 | ▶ | $var_obj->prop($var, @_ ? shift : | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | $$self[0]->undefined); | ||||||||||||||
| 46 | |||||||||||||||
| 47 | # This is very naughty code, but it works. | ||||||||||||||
| 48 | 683 | ▶ | $JE::Code::Expression::_eval or $var_obj->prop({ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | name => $var, | ||||||||||||||
| 50 | dontdel => 1, | ||||||||||||||
| 51 | }); | ||||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 758 | ▶ | return new JE::LValue $var_obj, $var | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | unless not defined wantarray; | ||||||||||||||
| 56 | } | ||||||||||||||
| 57 | |||||||||||||||
| 58 | 1321 | ▶ | sub AUTOLOAD { # This delegates the method to the global object | ||||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=9.9
| |||||||||||||||
| 59 | 1321 | my($method) = $AUTOLOAD =~ /([^:]+)\z/; | |||||||||||||
| 60 | |||||||||||||||
| 61 | # deal with various ALLCAPS names | ||||||||||||||
| 62 | 1321 | ▶ | if($method =~ /^[A-Z]+\z/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 0 | substr($method,0,0) = 'SUPER::'; | |||||||||||||
| 64 | 0 | local *@; | |||||||||||||
| 65 | 0 | return eval { shift->$method(@_) }; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 1321 | shift->[0]->$method(@_); # ~~~ Maybe I should use goto | |||||||||||||
| 69 | # to remove AUTOLOAD from | ||||||||||||||
| 70 | # the call stack. | ||||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 0 | ▶ | sub DESTROY {} | ||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||
| 74 | |||||||||||||||
| 75 | 101 | 1; | |||||||||||||
| 76 | |||||||||||||||
| 77 | =head1 NAME | ||||||||||||||
| 78 | |||||||||||||||
| 79 | JE::Scope - JavaScript scope chain (what makes closures work) | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =head1 DESCRIPTION | ||||||||||||||
| 82 | |||||||||||||||
| 83 | JavaScript code runs within an execution context which has a scope chain | ||||||||||||||
| 84 | associated with it. This class implements this scope chain. When a variable | ||||||||||||||
| 85 | is accessed the objects in the scope chain are searched till the variable | ||||||||||||||
| 86 | is found. | ||||||||||||||
| 87 | |||||||||||||||
| 88 | A JE::Scope object can also be used as global (JE) object. Any methods it | ||||||||||||||
| 89 | does not understand will be delegated to the object at the bottom of the | ||||||||||||||
| 90 | stack (the far end of the chain), so that C<< $scope->null >> means the | ||||||||||||||
| 91 | same thing as C<< $scope->[0]->null >>. | ||||||||||||||
| 92 | |||||||||||||||
| 93 | Objects of this class consist of a reference to an array, the elements of | ||||||||||||||
| 94 | which are the objects in the chain (the first element | ||||||||||||||
| 95 | being the global object). (Think | ||||||||||||||
| 96 | of it as a stack.) | ||||||||||||||
| 97 | |||||||||||||||
| 98 | =head1 METHODS | ||||||||||||||
| 99 | |||||||||||||||
| 100 | =over 4 | ||||||||||||||
| 101 | |||||||||||||||
| 102 | =item find_var($name, $value) | ||||||||||||||
| 103 | |||||||||||||||
| 104 | =item find_var($name) | ||||||||||||||
| 105 | |||||||||||||||
| 106 | This method searches through | ||||||||||||||
| 107 | the scope chain, starting at the end of the array, until it | ||||||||||||||
| 108 | finds the | ||||||||||||||
| 109 | variable named by the first argument. If the second argument is | ||||||||||||||
| 110 | present, it sets the variable. It then returns an lvalue (a | ||||||||||||||
| 111 | JE::LValue object) that references the variable. | ||||||||||||||
| 112 | |||||||||||||||
| 113 | =item new_var($name, $value) | ||||||||||||||
| 114 | |||||||||||||||
| 115 | =item new_var($name) | ||||||||||||||
| 116 | |||||||||||||||
| 117 | This method creates (and optionally sets the value of) a new | ||||||||||||||
| 118 | variable in the variable object (the same thing that JavaScript's C<var> | ||||||||||||||
| 119 | keyword does) and returns an lvalue. | ||||||||||||||
| 120 | |||||||||||||||
| 121 | The variable object is the first object in the scope chain | ||||||||||||||
| 122 | (searching from the top of the | ||||||||||||||
| 123 | stack) that is a call object, or C<< $scope->[0] >> if no call object is | ||||||||||||||
| 124 | found. | ||||||||||||||
| 125 | |||||||||||||||
| 126 | =back | ||||||||||||||
| 127 | |||||||||||||||
| 128 | =head1 CONSTRUCTOR | ||||||||||||||
| 129 | |||||||||||||||
| 130 | None. Just bless an array reference. You should not need to do | ||||||||||||||
| 131 | this because it is done for you by the C<JE> and C<JE::Object::Function> | ||||||||||||||
| 132 | classes. | ||||||||||||||
| 133 | |||||||||||||||
| 134 | =head1 SEE ALSO | ||||||||||||||
| 135 | |||||||||||||||
| 136 | =over | ||||||||||||||
| 137 | |||||||||||||||
| 138 | =item L<JE> | ||||||||||||||
| 139 | |||||||||||||||
| 140 | =item L<JE::LValue> | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =item L<JE::Object::Function> | ||||||||||||||
| 143 | |||||||||||||||
| 144 | =back | ||||||||||||||
| 145 | |||||||||||||||
| 146 | =cut | ||||||||||||||
| 147 | |||||||||||||||
| 148 | |||||||||||||||
| 149 | |||||||||||||||
| 150 | |||||||||||||||