Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Perlito5 - js - TIEARRAY, STORE, FETCH; add tie() example to html/per…
…lito5.html
  • Loading branch information
fglock committed Oct 4, 2012
1 parent 9a2d51f commit 74e2045
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 18 deletions.
32 changes: 29 additions & 3 deletions html/perlito5.html
Expand Up @@ -48,19 +48,21 @@ <h1><a href="http://www.perlito.org">"Perlito" Perl 5 Compiler</a></h1>
$time = 'Run-time';
eval $str;

package BasePrinter;
{ package BasePrinter;
sub new {
bless {}, $_[0]
}
}

package Printer;
{ package Printer;
our @ISA = qw/ BasePrinter /;
sub out {
JS::inline('document.getElementById("print-result").value') =
JS::inline('document.getElementById("print-result").value') . $_[1];
}
}

package Main;
{ package Main;

my $printer = Printer->new;
$printer->out( "Using my 'Printer' object\n" );
Expand All @@ -71,6 +73,30 @@ <h1><a href="http://www.perlito.org">"Perlito" Perl 5 Compiler</a></h1>
say "oops - it was not" if $s !~ /was/;
say "it was indeed" if $s =~ /was/;

}

{ package TheArray;
sub TIEARRAY {
my $class = shift;
bless { @_ }, $class
}
sub FETCH {
my ($self, $i) = @_;
say "# fetch";
return $self->{stuff};
}
sub STORE {
my ($self, $i, $v) = @_;
say "# store";
$self->{stuff} = $v;
}
}

my @v;
tie @v, 'TheArray';
$v[2] = 'hi tie!';
say $v[3];

</textarea><br/>
<input type="button" value="Execute" onclick="execute()"/>

Expand Down
80 changes: 73 additions & 7 deletions html/perlito5.js
Expand Up @@ -133,6 +133,10 @@ function p5method_lookup(method, class_name, seen) {
function p5call(invocant, method, list) {
list.unshift(invocant);

if (typeof invocant === "string") {
invocant = p5make_package(invocant);
}

if ( invocant.hasOwnProperty("_class_") ) {

if ( invocant._class_.hasOwnProperty(method) ) {
Expand Down Expand Up @@ -165,13 +169,6 @@ function p5call(invocant, method, list) {

}

// the invocant doesn't have a class

if (typeof invocant === "string") {
var aclass = p5make_package(invocant);
return p5call(aclass, method, list);
}

p5pkg.CORE.die(["Can't call method ", method, " on unblessed reference"]);

}
Expand Down Expand Up @@ -349,6 +346,75 @@ p5tie_array = function(v, List__) {
// DESTROY this
// UNTIE this

Object.defineProperty( v, "p5aget", {
enumerable : false,
value : function (i) {
return p5call(res, 'FETCH', [i]);
}
});
Object.defineProperty( v, "p5aset", {
enumerable : false,
value : function (i, value) {
p5call(res, 'STORE', [i, value]);
return value;
}
});
Object.defineProperty( v, "p5incr", {
enumerable : false,
value : function (i) {
var value = p5incr(p5call(res, 'FETCH', [i]));
p5call(res, 'STORE', [i, value]);
return value;
}
});
Object.defineProperty( v, "p5postincr", {
enumerable : false,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
p5call(res, 'STORE', [i, p5incr(value)]);
return value;
}
});
Object.defineProperty( v, "p5decr", {
enumerable : false,
value : function (i) {
var value = p5decr(p5call(res, 'FETCH', [i]));
p5call(res, 'STORE', [i, value]);
return value;
}
});
Object.defineProperty( v, "p5postdecr", {
enumerable : false,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
p5call(res, 'STORE', [i, p5decr(value)]);
return value;
}
});

Object.defineProperty( v, "p5aget_array", {
enumerable : false,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
if (value == null) {
value = new p5ArrayRef([]);
p5call(res, 'STORE', [i, value]);
}
return value;
}
});
Object.defineProperty( v, "p5aget_hash", {
enumerable : false,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
if (value == null) {
value = new p5HashRef({});
p5call(res, 'STORE', [i, value]);
}
return value;
}
});

return res;
};

Expand Down
2 changes: 1 addition & 1 deletion perlito5.pl

Large diffs are not rendered by default.

80 changes: 73 additions & 7 deletions src5/lib/Perlito5/Javascript2/Runtime.pm
Expand Up @@ -139,6 +139,10 @@ function p5method_lookup(method, class_name, seen) {
function p5call(invocant, method, list) {
list.unshift(invocant);
if (typeof invocant === "string") {
invocant = p5make_package(invocant);
}
if ( invocant.hasOwnProperty("_class_") ) {
if ( invocant._class_.hasOwnProperty(method) ) {
Expand Down Expand Up @@ -171,13 +175,6 @@ function p5call(invocant, method, list) {
}
// the invocant doesn't have a class
if (typeof invocant === "string") {
var aclass = p5make_package(invocant);
return p5call(aclass, method, list);
}
p5pkg.CORE.die(["Can't call method ", method, " on unblessed reference"]);
}
Expand Down Expand Up @@ -355,6 +352,75 @@ p5tie_array = function(v, List__) {
// DESTROY this
// UNTIE this
Object.defineProperty( v, "p5aget", {
enumerable : false,
value : function (i) {
return p5call(res, 'FETCH', [i]);
}
});
Object.defineProperty( v, "p5aset", {
enumerable : false,
value : function (i, value) {
p5call(res, 'STORE', [i, value]);
return value;
}
});
Object.defineProperty( v, "p5incr", {
enumerable : false,
value : function (i) {
var value = p5incr(p5call(res, 'FETCH', [i]));
p5call(res, 'STORE', [i, value]);
return value;
}
});
Object.defineProperty( v, "p5postincr", {
enumerable : false,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
p5call(res, 'STORE', [i, p5incr(value)]);
return value;
}
});
Object.defineProperty( v, "p5decr", {
enumerable : false,
value : function (i) {
var value = p5decr(p5call(res, 'FETCH', [i]));
p5call(res, 'STORE', [i, value]);
return value;
}
});
Object.defineProperty( v, "p5postdecr", {
enumerable : false,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
p5call(res, 'STORE', [i, p5decr(value)]);
return value;
}
});
Object.defineProperty( v, "p5aget_array", {
enumerable : false,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
if (value == null) {
value = new p5ArrayRef([]);
p5call(res, 'STORE', [i, value]);
}
return value;
}
});
Object.defineProperty( v, "p5aget_hash", {
enumerable : false,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
if (value == null) {
value = new p5HashRef({});
p5call(res, 'STORE', [i, value]);
}
return value;
}
});
return res;
};
Expand Down
53 changes: 53 additions & 0 deletions t5/01-perlito/30-tie-array.t
@@ -0,0 +1,53 @@
use v5;
use strict;
use feature 'say';

{
package TheArray;

sub TIEARRAY {
my $class = shift;
say "# TIEARRAY $class";
bless { @_ }, $class
}

sub FETCH {
my $self = shift;
my $i = shift;
say "# FETCH $i";
if ($i == 0) {
return $self->{'zero'};
}
}

sub STORE {
my $self = shift;
my $i = shift;
my $v = shift;
say "# STORE $i, $v";
if ($i == 0) {
$self->{'zero'} = $v;
return;
}
if ($i == 1) {
say $v;
return;
}
}
}

say '1..2';

my @list;

tie @list, 'TheArray';

$list[0] = 'first';

if ($list[0] ne 'first') {
print 'not '
};
say 'ok 1 # ', $list[0];

$list[1] = 'ok 2 # whatever';

0 comments on commit 74e2045

Please sign in to comment.