Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Perlito5 - js - add UNTIE
  • Loading branch information
fglock committed Oct 4, 2012
1 parent 9536f4a commit 1ec6ea0
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 5 deletions.
11 changes: 7 additions & 4 deletions README-perlito5-js
Expand Up @@ -27,8 +27,6 @@

- Tie array.

- String increment.



* Perlito5-in-Javascript differences from "perl"
Expand Down Expand Up @@ -76,6 +74,8 @@

- tie() is partially implemented.

- String increment partially implemented - array and hash lookups; not in scalars.


* Perlito5 compiler globals

Expand Down Expand Up @@ -123,14 +123,17 @@ subroutine call
- "this" is not used.

Hash
- native {}
- native {} with perl5-specific getters and setters

Array
- native []
- native [] with perl5-specific getters and setters

Scalar
- native value

Tied containers
- javascript object with perl5-specific getters and setters

HashRef
- native {} wrapped in a "HashRef" object

Expand Down
63 changes: 63 additions & 0 deletions html/perlito5.js
Expand Up @@ -348,19 +348,22 @@ p5tie_array = function(v, List__) {

Object.defineProperty( v, "p5aget", {
enumerable : false,
configurable : true,
value : function (i) {
return p5call(res, 'FETCH', [i]);
}
});
Object.defineProperty( v, "p5aset", {
enumerable : false,
configurable : true,
value : function (i, value) {
p5call(res, 'STORE', [i, value]);
return value;
}
});
Object.defineProperty( v, "p5incr", {
enumerable : false,
configurable : true,
value : function (i) {
var value = p5incr(p5call(res, 'FETCH', [i]));
p5call(res, 'STORE', [i, value]);
Expand All @@ -369,6 +372,7 @@ p5tie_array = function(v, List__) {
});
Object.defineProperty( v, "p5postincr", {
enumerable : false,
configurable : true,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
p5call(res, 'STORE', [i, p5incr(value)]);
Expand All @@ -377,6 +381,7 @@ p5tie_array = function(v, List__) {
});
Object.defineProperty( v, "p5decr", {
enumerable : false,
configurable : true,
value : function (i) {
var value = p5decr(p5call(res, 'FETCH', [i]));
p5call(res, 'STORE', [i, value]);
Expand All @@ -385,6 +390,7 @@ p5tie_array = function(v, List__) {
});
Object.defineProperty( v, "p5postdecr", {
enumerable : false,
configurable : true,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
p5call(res, 'STORE', [i, p5decr(value)]);
Expand All @@ -394,6 +400,7 @@ p5tie_array = function(v, List__) {

Object.defineProperty( v, "p5aget_array", {
enumerable : false,
configurable : true,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
if (value == null) {
Expand All @@ -405,6 +412,7 @@ p5tie_array = function(v, List__) {
});
Object.defineProperty( v, "p5aget_hash", {
enumerable : false,
configurable : true,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
if (value == null) {
Expand All @@ -414,10 +422,36 @@ p5tie_array = function(v, List__) {
return value;
}
});
Object.defineProperty( v, "p5untie", {
enumerable : false,
configurable : true,
value : function (i) {
return p5call(res, 'UNTIE', []);
}
});

return res;
};

p5untie_array = function(v) {
if (v.hasOwnProperty('p5untie')) {
var res = v.p5untie(); // call UNTIE
delete v.p5aget;
delete v.p5aset;
delete v.p5incr;
delete v.p5postincr;
delete v.p5decr;
delete v.p5postdecr;
delete v.p5aget_array;
delete v.p5aget_hash;
delete v.p5untie;
return res;
}
else {
return null;
}
};

//-------- Hash

Object.defineProperty( Object.prototype, "p5hget", {
Expand Down Expand Up @@ -3801,6 +3835,35 @@ var p5100 = p5pkg['main'];
};
};
return (p5context([('p5tie_' + p5str(v_meth) + '(' + p5str(p5call(v_v, "emit_javascript2", [v_level], 0)) + ', ' + p5str(p5pkg["Perlito5::Javascript2"].to_list([(new p5ArrayRef(List_arguments))], 0)) + ')')], p5want));
}, 'untie', function (List__, p5want) {
var v_self = null;
(v_self = (p5pkg["Perlito5::AST::Apply"].shift([List__])));
var v_level = null;
(v_level = (p5pkg["Perlito5::AST::Apply"].shift([List__])));
var v_wantarray = null;
(v_wantarray = (p5pkg["Perlito5::AST::Apply"].shift([List__])));
var List_arguments = [];
(List_arguments = p5list_to_a((v_self || (v_self = new p5HashRef({})))._hash_.p5hget_array('arguments')._array_));
var v_v = null;
(v_v = (p5pkg["Perlito5::AST::Apply"].shift([List_arguments])));
var v_meth = null;
if ( (p5bool(p5call(v_v, "isa", ['Perlito5::AST::Var'], 0)) && (p5str(p5call(v_v, "sigil", [], 0)) == '%')) ) {
(v_meth = ('hash'));
}
else {
if ( (p5bool(p5call(v_v, "isa", ['Perlito5::AST::Var'], 0)) && (p5str(p5call(v_v, "sigil", [], 0)) == '@')) ) {
(v_meth = ('array'));
}
else {
if ( (p5bool(p5call(v_v, "isa", ['Perlito5::AST::Var'], 0)) && (p5str(p5call(v_v, "sigil", [], 0)) == '$')) ) {
(v_meth = ('scalar'));
}
else {
p5pkg["Perlito5::AST::Apply"].die([p5list_to_a('tie ' + String.fromCharCode(39), p5pkg["Perlito5::AST::Apply"].ref([v_v], 1), String.fromCharCode(39) + ' not implemented')], null);
};
};
};
return (p5context([('p5untie_' + p5str(v_meth) + '(' + p5str(p5call(v_v, "emit_javascript2", [v_level], 0)) + ')')], p5want));
}, 'map', function (List__, p5want) {
var v_self = null;
(v_self = (p5pkg["Perlito5::AST::Apply"].shift([List__])));
Expand Down
26 changes: 25 additions & 1 deletion perlito5.pl

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions src5/lib/Perlito5/Javascript2/Emitter.pm
Expand Up @@ -1812,6 +1812,30 @@ package Perlito5::AST::Apply;
return 'p5tie_' . $meth . '(' . $v->emit_javascript2( $level ) . ', ' . Perlito5::Javascript2::to_list(\@arguments) . ')';
},

'untie' => sub {
my $self = shift;
my $level = shift;
my $wantarray = shift;

my @arguments = @{$self->{arguments}};
my $v = shift @arguments; # TODO - this argument can also be a 'Decl' instead of 'Var'

my $meth;
if ( $v->isa('Perlito5::AST::Var') && $v->sigil eq '%' ) {
$meth = 'hash';
}
elsif ( $v->isa('Perlito5::AST::Var') && $v->sigil eq '@' ) {
$meth = 'array';
}
elsif ( $v->isa('Perlito5::AST::Var') && $v->sigil eq '$' ) {
$meth = 'scalar';
}
else {
die "tie '", ref($v), "' not implemented";
}
return 'p5untie_' . $meth . '(' . $v->emit_javascript2( $level ) . ')';
},

'map' => sub {
my $self = shift;
my $level = shift;
Expand Down
34 changes: 34 additions & 0 deletions src5/lib/Perlito5/Javascript2/Runtime.pm
Expand Up @@ -354,19 +354,22 @@ p5tie_array = function(v, List__) {
Object.defineProperty( v, "p5aget", {
enumerable : false,
configurable : true,
value : function (i) {
return p5call(res, 'FETCH', [i]);
}
});
Object.defineProperty( v, "p5aset", {
enumerable : false,
configurable : true,
value : function (i, value) {
p5call(res, 'STORE', [i, value]);
return value;
}
});
Object.defineProperty( v, "p5incr", {
enumerable : false,
configurable : true,
value : function (i) {
var value = p5incr(p5call(res, 'FETCH', [i]));
p5call(res, 'STORE', [i, value]);
Expand All @@ -375,6 +378,7 @@ p5tie_array = function(v, List__) {
});
Object.defineProperty( v, "p5postincr", {
enumerable : false,
configurable : true,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
p5call(res, 'STORE', [i, p5incr(value)]);
Expand All @@ -383,6 +387,7 @@ p5tie_array = function(v, List__) {
});
Object.defineProperty( v, "p5decr", {
enumerable : false,
configurable : true,
value : function (i) {
var value = p5decr(p5call(res, 'FETCH', [i]));
p5call(res, 'STORE', [i, value]);
Expand All @@ -391,6 +396,7 @@ p5tie_array = function(v, List__) {
});
Object.defineProperty( v, "p5postdecr", {
enumerable : false,
configurable : true,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
p5call(res, 'STORE', [i, p5decr(value)]);
Expand All @@ -400,6 +406,7 @@ p5tie_array = function(v, List__) {
Object.defineProperty( v, "p5aget_array", {
enumerable : false,
configurable : true,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
if (value == null) {
Expand All @@ -411,6 +418,7 @@ p5tie_array = function(v, List__) {
});
Object.defineProperty( v, "p5aget_hash", {
enumerable : false,
configurable : true,
value : function (i) {
var value = p5call(res, 'FETCH', [i]);
if (value == null) {
Expand All @@ -420,10 +428,36 @@ p5tie_array = function(v, List__) {
return value;
}
});
Object.defineProperty( v, "p5untie", {
enumerable : false,
configurable : true,
value : function (i) {
return p5call(res, 'UNTIE', []);
}
});
return res;
};
p5untie_array = function(v) {
if (v.hasOwnProperty('p5untie')) {
var res = v.p5untie(); // call UNTIE
delete v.p5aget;
delete v.p5aset;
delete v.p5incr;
delete v.p5postincr;
delete v.p5decr;
delete v.p5postdecr;
delete v.p5aget_array;
delete v.p5aget_hash;
delete v.p5untie;
return res;
}
else {
return null;
}
};
//-------- Hash
Object.defineProperty( Object.prototype, "p5hget", {
Expand Down
6 changes: 6 additions & 0 deletions t5/01-perlito/30-tie-array.t
Expand Up @@ -34,6 +34,10 @@ use feature 'say';
return;
}
}

sub UNTIE {
say "# UNTIE";
}
}

say '1..2';
Expand All @@ -51,3 +55,5 @@ say 'ok 1 # ', $list[0];

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

untie @list;

0 comments on commit 1ec6ea0

Please sign in to comment.