Browse files

Perlito5 - js - more array tie methods

  • Loading branch information...
1 parent 1ec6ea0 commit a57f236eb553f6ce39557d9cb60c98208aaa6014 @fglock committed Oct 5, 2012
View
783 html/perlito5.js
422 additions, 361 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
30 perlito5.pl
26 additions, 4 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
34 src5/lib/Perlito5/Javascript2/CORE.pm
@@ -268,40 +268,6 @@ CORE.splice = function(List__, p5want) {
return p5want ? out : out.pop();
};
-CORE.pop = function(List__) {
- var o = List__[0];
- if (o.length == null) {
- return null;
- }
- return o.pop();
-};
-
-CORE.shift = function(List__) {
- var o = List__[0];
- if (o.length == null) {
- return null;
- }
- return o.shift();
-};
-
-CORE.push = function(List__) {
- var o = List__[0];
- var v = List__[1];
- for(var i = 0; i < v.length; i++) {
- o.push(v[i]);
- }
- return o.length;
-};
-
-CORE.unshift = function(List__) {
- var o = List__[0];
- var v = List__[1];
- for(var i = v.length-1; i >= 0; i--) {
- o.unshift(v[i]);
- }
- return o.length;
-};
-
CORE.join = function(List__) {
var s = List__[0];
var o = List__[1];
View
36 src5/lib/Perlito5/Javascript2/Emitter.pm
@@ -1783,9 +1783,41 @@ package Perlito5::AST::Apply;
my $level = shift;
my $wantarray = shift;
if ( $self->{arguments} && @{$self->{arguments}} ) {
- return Perlito5::Javascript2::pkg() . '.shift([' . join(', ', map( $_->emit_javascript2( $level ), @{$self->{arguments}} )) . '])'
+ return $self->{arguments}[0]->emit_javascript2( $level ) . '.shift()'
}
- return Perlito5::Javascript2::pkg() . '.shift([List__])'
+ return 'List__.shift()'
+ },
+
+ 'pop' => sub {
+ my $self = shift;
+ my $level = shift;
+ my $wantarray = shift;
+ if ( $self->{arguments} && @{$self->{arguments}} ) {
+ return $self->{arguments}[0]->emit_javascript2( $level ) . '.pop()'
+ }
+ return 'List__.pop()'
+ },
+
+ 'unshift' => 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'
+
+ return $v->emit_javascript2( $level ) . '.p5unshift(' . Perlito5::Javascript2::to_list(\@arguments) . ')';
+ },
+
+ 'push' => 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'
+
+ return $v->emit_javascript2( $level ) . '.p5push(' . Perlito5::Javascript2::to_list(\@arguments) . ')';
},
'tie' => sub {
View
56 src5/lib/Perlito5/Javascript2/Runtime.pm
@@ -328,6 +328,26 @@ Object.defineProperty( Array.prototype, "p5aget_hash", {
return this[i]
}
});
+Object.defineProperty( Array.prototype, "p5unshift", {
+ enumerable : false,
+ configurable : true,
+ value : function (args) {
+ for(var i = args.length-1; i >= 0; i--) {
+ this.unshift(args[i]);
+ }
+ return this.length;
+ }
+});
+Object.defineProperty( Array.prototype, "p5push", {
+ enumerable : false,
+ configurable : true,
+ value : function (args) {
+ for(var i = 0; i < args.length; i++) {
+ this.push(args[i]);
+ }
+ return this.length;
+ }
+});
p5tie_array = function(v, List__) {
var pkg_name = p5str(List__.shift());
@@ -431,8 +451,36 @@ p5tie_array = function(v, List__) {
Object.defineProperty( v, "p5untie", {
enumerable : false,
configurable : true,
- value : function (i) {
- return p5call(res, 'UNTIE', []);
+ value : function (i) { return p5call(res, 'UNTIE', []) }
+ });
+ Object.defineProperty( v, "shift", {
+ enumerable : false,
+ configurable : true,
+ value : function () { return p5call(res, 'SHIFT', []) }
+ });
+ Object.defineProperty( v, "pop", {
+ enumerable : false,
+ configurable : true,
+ value : function () { return p5call(res, 'POP', []) }
+ });
+ Object.defineProperty( v, "p5unshift", {
+ enumerable : false,
+ configurable : true,
+ value : function (args) {
+ for(var i = args.length-1; i >= 0; i--) {
+ p5call(res, 'UNSHIFT', [args[i]]);
+ }
+ return p5call(res, 'FETCHSIZE', []);
+ }
+ });
+ Object.defineProperty( v, "p5push", {
+ enumerable : false,
+ configurable : true,
+ value : function (args) {
+ for(var i = 0; i < args.length; i++) {
+ p5call(res, 'PUSH', [args[i]]);
+ }
+ return p5call(res, 'FETCHSIZE', []);
}
});
@@ -451,6 +499,10 @@ p5untie_array = function(v) {
delete v.p5aget_array;
delete v.p5aget_hash;
delete v.p5untie;
+ delete v.shift;
+ delete v.pop;
+ delete v.p5unshift;
+ delete v.p5push;
return res;
}
else {
View
16 t5/01-perlito/30-tie-array.t
@@ -35,12 +35,20 @@ use feature 'say';
}
}
+ sub SHIFT {
+ return "shift ok";
+ }
+
sub UNTIE {
say "# UNTIE";
}
}
-say '1..2';
+sub shift { say "not ok 100 # PKG::shift()" }
+sub CORE::shift { say "not ok 101 # CORE::shift()" }
+sub CORE::GLOBAL::shift { say "not ok 102 # CORE::GLOBAL::shift()" }
+
+say '1..3';
my @list;
@@ -55,5 +63,11 @@ say 'ok 1 # ', $list[0];
$list[1] = 'ok 2 # whatever';
+my $v = shift @list;
+print "not " unless $v eq 'shift ok';
+say "ok 3 # shift $v";
+
untie @list;
+shift @list;
+

0 comments on commit a57f236

Please sign in to comment.