Permalink
Browse files

Perlito5 - js - add UNTIE

  • Loading branch information...
1 parent 9536f4a commit 1ec6ea07cc3eec308810941d34dfcb7085355a8b @fglock committed Oct 4, 2012
View
@@ -27,8 +27,6 @@
- Tie array.
-- String increment.
-
* Perlito5-in-Javascript differences from "perl"
@@ -76,6 +74,8 @@
- tie() is partially implemented.
+- String increment partially implemented - array and hash lookups; not in scalars.
+
* Perlito5 compiler globals
@@ -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
View
@@ -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]);
@@ -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)]);
@@ -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]);
@@ -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)]);
@@ -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) {
@@ -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) {
@@ -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", {
@@ -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__])));
View

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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;
@@ -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]);
@@ -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)]);
@@ -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]);
@@ -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)]);
@@ -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) {
@@ -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) {
@@ -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", {
@@ -34,6 +34,10 @@ use feature 'say';
return;
}
}
+
+ sub UNTIE {
+ say "# UNTIE";
+ }
}
say '1..2';
@@ -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.