Permalink
Browse files

Perlito5 - js - p5ArrayOfAlias - shift, pop

  • Loading branch information...
1 parent b50d8dc commit 1a8d3e4e8fc2906fce57ab19d70fcac0ebe87f45 @fglock committed Oct 12, 2012
Showing with 19 additions and 11 deletions.
  1. +9 −5 html/perlito5.js
  2. +1 −1 perlito5.pl
  3. +9 −5 src5/lib/Perlito5/Javascript2/Array.pm
View
14 html/perlito5.js
@@ -1061,11 +1061,15 @@ function p5ArrayOfAlias(o) {
}
return this._array_.length / 2;
}
-
- // TODO - shift()
-
- // TODO - pop()
-
+ this.shift = function () {
+ var v0 = this._array_.shift();
+ return v0[this._array_.shift()];
+ }
+ this.pop = function () {
+ var v1 = this._array_.pop();
+ var v0 = this._array_.pop();
+ return v0[v1];
+ }
}
View
2 perlito5.pl
@@ -9234,7 +9234,7 @@ package main;
undef();
package Perlito5::Javascript2::Array;
sub Perlito5::Javascript2::Array::emit_javascript2 {
- return ((('//' . chr(10) . '// lib/Perlito5/Javascript2/Runtime.js' . chr(10) . '//' . chr(10) . '// Runtime for "Perlito" Perl5-in-Javascript2' . chr(10) . '//' . chr(10) . '// AUTHORS' . chr(10) . '//' . chr(10) . '// Flavio Soibelmann Glock fglock@gmail.com' . chr(10) . '//' . chr(10) . '// COPYRIGHT' . chr(10) . '//' . chr(10) . '// Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others.' . chr(10) . '//' . chr(10) . '// This program is free software; you can redistribute it and/or modify it' . chr(10) . '// under the same terms as Perl itself.' . chr(10) . '//' . chr(10) . '// See http://www.perl.com/perl/misc/Artistic.html' . chr(10) . chr(10) . '//-------- Array ' . chr(10) . chr(10) . 'Object.defineProperty( Array.prototype, "p5aget", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' return this[i] ' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5aset", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i, v) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this[i] = v;' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . 'Object.defineProperty( Array.prototype, "p5incr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this[i] = p5incr(this[i]);' . chr(10) . ' return this[i];' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5postincr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' var v = this[i];' . chr(10) . ' this[i] = p5incr(this[i]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5decr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this[i] = p5decr(this[i]);' . chr(10) . ' return this[i];' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5postdecr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' var v = this[i];' . chr(10) . ' this[i] = p5decr(this[i]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . 'Object.defineProperty( Array.prototype, "p5aget_array", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' if (this[i] == null) { this[i] = new p5ArrayRef([]) }' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5aget_hash", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' if (this[i] == null) { this[i] = new p5HashRef({}) }' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5unshift", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = args.length-1; i >= 0; i--) {' . chr(10) . ' this.unshift(args[i]);' . chr(10) . ' }' . chr(10) . ' return this.length; ' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5push", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' this.push(args[i]);' . chr(10) . ' }' . chr(10) . ' return this.length; ' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . 'p5tie_array = function(v, List__) {' . chr(10) . ' var pkg_name = p5str(List__.shift());' . chr(10) . chr(10) . ' var res = p5call(pkg_name, ' . chr(39) . 'TIEARRAY' . chr(39) . ', List__, null);' . chr(10) . ' ' . chr(10) . ' // TODO' . chr(10) . ' ' . chr(10) . ' // A class implementing an ordinary array should have the following methods:' . chr(10) . ' // TIEARRAY pkg_name, LIST' . chr(10) . ' // FETCH this, key' . chr(10) . ' // STORE this, key, value' . chr(10) . ' // FETCHSIZE this' . chr(10) . ' // STORESIZE this, count' . chr(10) . ' // CLEAR this' . chr(10) . ' // PUSH this, LIST' . chr(10) . ' // POP this' . chr(10) . ' // SHIFT this' . chr(10) . ' // UNSHIFT this, LIST' . chr(10) . ' // SPLICE this, offset, length, LIST' . chr(10) . ' // EXTEND this, count' . chr(10) . ' // DESTROY this' . chr(10) . ' // UNTIE this' . chr(10) . ' ' . chr(10) . ' Object.defineProperty( v, "p5aget", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' return p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5aset", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i, value) {' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5incr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5incr(p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]));' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5postincr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, p5incr(value)]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5decr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5decr(p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]));' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5postdecr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, p5decr(value)]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' ' . chr(10) . ' Object.defineProperty( v, "p5aget_array", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' if (value == null) {' . chr(10) . ' value = new p5ArrayRef([]);' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' }' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5aget_hash", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' if (value == null) {' . chr(10) . ' value = new p5HashRef({});' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' }' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5untie", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) { return p5call(res, ' . chr(39) . 'UNTIE' . chr(39) . ', []) }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "shift", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function () { return p5call(res, ' . chr(39) . 'SHIFT' . chr(39) . ', []) }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "pop", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function () { return p5call(res, ' . chr(39) . 'POP' . chr(39) . ', []) }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5unshift", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = args.length-1; i >= 0; i--) {' . chr(10) . ' p5call(res, ' . chr(39) . 'UNSHIFT' . chr(39) . ', [args[i]]);' . chr(10) . ' }' . chr(10) . ' return p5call(res, ' . chr(39) . 'FETCHSIZE' . chr(39) . ', []); ' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5push", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' p5call(res, ' . chr(39) . 'PUSH' . chr(39) . ', [args[i]]);' . chr(10) . ' }' . chr(10) . ' return p5call(res, ' . chr(39) . 'FETCHSIZE' . chr(39) . ', []); ' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . chr(10) . ' return res;' . chr(10) . '};' . chr(10) . chr(10) . 'p5untie_array = function(v) {' . chr(10) . ' if (v.hasOwnProperty(' . chr(39) . 'p5untie' . chr(39) . ')) {' . chr(10) . ' var res = v.p5untie(); // call UNTIE' . chr(10) . ' delete v.p5aget;' . chr(10) . ' delete v.p5aset;' . chr(10) . ' delete v.p5incr;' . chr(10) . ' delete v.p5postincr;' . chr(10) . ' delete v.p5decr;' . chr(10) . ' delete v.p5postdecr;' . chr(10) . ' delete v.p5aget_array;' . chr(10) . ' delete v.p5aget_hash;' . chr(10) . ' delete v.p5untie;' . chr(10) . ' delete v.shift;' . chr(10) . ' delete v.pop;' . chr(10) . ' delete v.p5unshift;' . chr(10) . ' delete v.p5push;' . chr(10) . ' return res;' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' return null;' . chr(10) . ' }' . chr(10) . '};' . chr(10) . chr(10) . chr(10) . 'function p5ArrayOfAlias(o) {' . chr(10) . chr(10) . ' // this is the structure that represents @_' . chr(10) . ' // _array = [ ref, index,' . chr(10) . ' // ref, index,' . chr(10) . ' // ...' . chr(10) . ' // ]' . chr(10) . chr(10) . ' // TODO - autovivify array cells' . chr(10) . chr(10) . ' this._array_ = o;' . chr(10) . chr(10) . ' this.p5aget = function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' return this._array_[i+i][this._array_[i+i]+1]; ' . chr(10) . ' }' . chr(10) . ' this.p5aset = function (i, v) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this._array_[i+i][this._array_[i+i]+1] = v;' . chr(10) . ' return this._array_[i+i][this._array_[i+i]+1]' . chr(10) . ' }' . chr(10) . ' this.p5incr = function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this._array_[i+i][this._array_[i+i]+1] = p5incr(this._array_[i+i][this._array_[i+i]+1]);' . chr(10) . ' return this._array_[i+i][this._array_[i+i]+1];' . chr(10) . ' }' . chr(10) . ' this.p5postincr = function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' var v = this._array_[i+i][this._array_[i+i]+1];' . chr(10) . ' this._array_[i+i][this._array_[i+i]+1] = p5incr(this._array_[i+i][this._array_[i+i]+1]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . ' this.p5decr = function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this._array_[i+i][this._array_[i+i]+1] = p5decr(this._array_[i+i][this._array_[i+i]+1]);' . chr(10) . ' return this._array_[i+i][this._array_[i+i]+1];' . chr(10) . ' }' . chr(10) . ' this.p5postdecr = function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' var v = this._array_[i+i][this._array_[i+i]+1];' . chr(10) . ' this._array_[i+i][this._array_[i+i]+1] = p5decr(this._array_[i+i][this._array_[i+i]+1]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . ' this.p5aget_array = function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' if (this._array_[i+i][this._array_[i+i]+1] == null) {' . chr(10) . ' this._array_[i+i][this._array_[i+i]+1] = new p5ArrayRef([])' . chr(10) . ' }' . chr(10) . ' return this._array_[i+i][this._array_[i+i]+1]' . chr(10) . ' }' . chr(10) . ' this.p5aget_hash = function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' if (this._array_[i+i][this._array_[i+i]+1] == null) {' . chr(10) . ' this._array_[i+i][this._array_[i+i]+1] = new p5HashRef({})' . chr(10) . ' }' . chr(10) . ' return this._array_[i+i][this._array_[i+i]+1]' . chr(10) . ' }' . chr(10) . ' this.p5unshift = function (args) { ' . chr(10) . ' for(var i = args.length-1; i >= 0; i--) {' . chr(10) . ' this.unshift(0);' . chr(10) . ' this.unshift([args[i]]);' . chr(10) . ' }' . chr(10) . ' return this._array_.length / 2; ' . chr(10) . ' }' . chr(10) . ' this.p5push = function (args) { ' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' this.push([args[i]]);' . chr(10) . ' this.push(0);' . chr(10) . ' }' . chr(10) . ' return this._array_.length / 2; ' . chr(10) . ' }' . chr(10) . chr(10) . ' // TODO - shift()' . chr(10) . chr(10) . ' // TODO - pop()' . chr(10) . chr(10) . '}' . chr(10) . chr(10) . chr(10))))
+ return ((('//' . chr(10) . '// lib/Perlito5/Javascript2/Runtime.js' . chr(10) . '//' . chr(10) . '// Runtime for "Perlito" Perl5-in-Javascript2' . chr(10) . '//' . chr(10) . '// AUTHORS' . chr(10) . '//' . chr(10) . '// Flavio Soibelmann Glock fglock@gmail.com' . chr(10) . '//' . chr(10) . '// COPYRIGHT' . chr(10) . '//' . chr(10) . '// Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others.' . chr(10) . '//' . chr(10) . '// This program is free software; you can redistribute it and/or modify it' . chr(10) . '// under the same terms as Perl itself.' . chr(10) . '//' . chr(10) . '// See http://www.perl.com/perl/misc/Artistic.html' . chr(10) . chr(10) . '//-------- Array ' . chr(10) . chr(10) . 'Object.defineProperty( Array.prototype, "p5aget", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' return this[i] ' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5aset", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i, v) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this[i] = v;' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . 'Object.defineProperty( Array.prototype, "p5incr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this[i] = p5incr(this[i]);' . chr(10) . ' return this[i];' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5postincr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' var v = this[i];' . chr(10) . ' this[i] = p5incr(this[i]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5decr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this[i] = p5decr(this[i]);' . chr(10) . ' return this[i];' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5postdecr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' var v = this[i];' . chr(10) . ' this[i] = p5decr(this[i]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . 'Object.defineProperty( Array.prototype, "p5aget_array", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' if (this[i] == null) { this[i] = new p5ArrayRef([]) }' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5aget_hash", {' . chr(10) . ' enumerable : false,' . chr(10) . ' value : function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' if (this[i] == null) { this[i] = new p5HashRef({}) }' . chr(10) . ' return this[i]' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5unshift", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = args.length-1; i >= 0; i--) {' . chr(10) . ' this.unshift(args[i]);' . chr(10) . ' }' . chr(10) . ' return this.length; ' . chr(10) . ' }' . chr(10) . '});' . chr(10) . 'Object.defineProperty( Array.prototype, "p5push", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' this.push(args[i]);' . chr(10) . ' }' . chr(10) . ' return this.length; ' . chr(10) . ' }' . chr(10) . '});' . chr(10) . chr(10) . 'p5tie_array = function(v, List__) {' . chr(10) . ' var pkg_name = p5str(List__.shift());' . chr(10) . chr(10) . ' var res = p5call(pkg_name, ' . chr(39) . 'TIEARRAY' . chr(39) . ', List__, null);' . chr(10) . ' ' . chr(10) . ' // TODO' . chr(10) . ' ' . chr(10) . ' // A class implementing an ordinary array should have the following methods:' . chr(10) . ' // TIEARRAY pkg_name, LIST' . chr(10) . ' // FETCH this, key' . chr(10) . ' // STORE this, key, value' . chr(10) . ' // FETCHSIZE this' . chr(10) . ' // STORESIZE this, count' . chr(10) . ' // CLEAR this' . chr(10) . ' // PUSH this, LIST' . chr(10) . ' // POP this' . chr(10) . ' // SHIFT this' . chr(10) . ' // UNSHIFT this, LIST' . chr(10) . ' // SPLICE this, offset, length, LIST' . chr(10) . ' // EXTEND this, count' . chr(10) . ' // DESTROY this' . chr(10) . ' // UNTIE this' . chr(10) . ' ' . chr(10) . ' Object.defineProperty( v, "p5aget", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' return p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5aset", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i, value) {' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5incr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5incr(p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]));' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5postincr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, p5incr(value)]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5decr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5decr(p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]));' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5postdecr", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, p5decr(value)]);' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' ' . chr(10) . ' Object.defineProperty( v, "p5aget_array", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' if (value == null) {' . chr(10) . ' value = new p5ArrayRef([]);' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' }' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5aget_hash", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) {' . chr(10) . ' var value = p5call(res, ' . chr(39) . 'FETCH' . chr(39) . ', [i]);' . chr(10) . ' if (value == null) {' . chr(10) . ' value = new p5HashRef({});' . chr(10) . ' p5call(res, ' . chr(39) . 'STORE' . chr(39) . ', [i, value]);' . chr(10) . ' }' . chr(10) . ' return value;' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5untie", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (i) { return p5call(res, ' . chr(39) . 'UNTIE' . chr(39) . ', []) }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "shift", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function () { return p5call(res, ' . chr(39) . 'SHIFT' . chr(39) . ', []) }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "pop", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function () { return p5call(res, ' . chr(39) . 'POP' . chr(39) . ', []) }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5unshift", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = args.length-1; i >= 0; i--) {' . chr(10) . ' p5call(res, ' . chr(39) . 'UNSHIFT' . chr(39) . ', [args[i]]);' . chr(10) . ' }' . chr(10) . ' return p5call(res, ' . chr(39) . 'FETCHSIZE' . chr(39) . ', []); ' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . ' Object.defineProperty( v, "p5push", {' . chr(10) . ' enumerable : false,' . chr(10) . ' configurable : true,' . chr(10) . ' value : function (args) { ' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' p5call(res, ' . chr(39) . 'PUSH' . chr(39) . ', [args[i]]);' . chr(10) . ' }' . chr(10) . ' return p5call(res, ' . chr(39) . 'FETCHSIZE' . chr(39) . ', []); ' . chr(10) . ' }' . chr(10) . ' });' . chr(10) . chr(10) . ' return res;' . chr(10) . '};' . chr(10) . chr(10) . 'p5untie_array = function(v) {' . chr(10) . ' if (v.hasOwnProperty(' . chr(39) . 'p5untie' . chr(39) . ')) {' . chr(10) . ' var res = v.p5untie(); // call UNTIE' . chr(10) . ' delete v.p5aget;' . chr(10) . ' delete v.p5aset;' . chr(10) . ' delete v.p5incr;' . chr(10) . ' delete v.p5postincr;' . chr(10) . ' delete v.p5decr;' . chr(10) . ' delete v.p5postdecr;' . chr(10) . ' delete v.p5aget_array;' . chr(10) . ' delete v.p5aget_hash;' . chr(10) . ' delete v.p5untie;' . chr(10) . ' delete v.shift;' . chr(10) . ' delete v.pop;' . chr(10) . ' delete v.p5unshift;' . chr(10) . ' delete v.p5push;' . chr(10) . ' return res;' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' return null;' . chr(10) . ' }' . chr(10) . '};' . chr(10) . chr(10) . chr(10) . 'function p5ArrayOfAlias(o) {' . chr(10) . chr(10) . ' // this is the structure that represents @_' . chr(10) . ' // _array = [ ref, index,' . chr(10) . ' // ref, index,' . chr(10) . ' // ...' . chr(10) . ' // ]' . chr(10) . chr(10) . ' // TODO - autovivify array cells' . chr(10) . chr(10) . ' this._array_ = o;' . chr(10) . chr(10) . ' this.p5aget = function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' return this._array_[i+i][this._array_[i+i]+1]; ' . chr(10) . ' }' . chr(10) . ' this.p5aset = function (i, v) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this._array_[i+i][this._array_[i+i]+1] = v;' . chr(10) . ' return this._array_[i+i][this._array_[i+i]+1]' . chr(10) . ' }' . chr(10) . ' this.p5incr = function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this._array_[i+i][this._array_[i+i]+1] = p5incr(this._array_[i+i][this._array_[i+i]+1]);' . chr(10) . ' return this._array_[i+i][this._array_[i+i]+1];' . chr(10) . ' }' . chr(10) . ' this.p5postincr = function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' var v = this._array_[i+i][this._array_[i+i]+1];' . chr(10) . ' this._array_[i+i][this._array_[i+i]+1] = p5incr(this._array_[i+i][this._array_[i+i]+1]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . ' this.p5decr = function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' this._array_[i+i][this._array_[i+i]+1] = p5decr(this._array_[i+i][this._array_[i+i]+1]);' . chr(10) . ' return this._array_[i+i][this._array_[i+i]+1];' . chr(10) . ' }' . chr(10) . ' this.p5postdecr = function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' var v = this._array_[i+i][this._array_[i+i]+1];' . chr(10) . ' this._array_[i+i][this._array_[i+i]+1] = p5decr(this._array_[i+i][this._array_[i+i]+1]);' . chr(10) . ' return v;' . chr(10) . ' }' . chr(10) . ' this.p5aget_array = function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' if (this._array_[i+i][this._array_[i+i]+1] == null) {' . chr(10) . ' this._array_[i+i][this._array_[i+i]+1] = new p5ArrayRef([])' . chr(10) . ' }' . chr(10) . ' return this._array_[i+i][this._array_[i+i]+1]' . chr(10) . ' }' . chr(10) . ' this.p5aget_hash = function (i) {' . chr(10) . ' if (i < 0) { i = this.length + i };' . chr(10) . ' if (this._array_[i+i][this._array_[i+i]+1] == null) {' . chr(10) . ' this._array_[i+i][this._array_[i+i]+1] = new p5HashRef({})' . chr(10) . ' }' . chr(10) . ' return this._array_[i+i][this._array_[i+i]+1]' . chr(10) . ' }' . chr(10) . ' this.p5unshift = function (args) { ' . chr(10) . ' for(var i = args.length-1; i >= 0; i--) {' . chr(10) . ' this.unshift(0);' . chr(10) . ' this.unshift([args[i]]);' . chr(10) . ' }' . chr(10) . ' return this._array_.length / 2; ' . chr(10) . ' }' . chr(10) . ' this.p5push = function (args) { ' . chr(10) . ' for(var i = 0; i < args.length; i++) {' . chr(10) . ' this.push([args[i]]);' . chr(10) . ' this.push(0);' . chr(10) . ' }' . chr(10) . ' return this._array_.length / 2; ' . chr(10) . ' }' . chr(10) . ' this.shift = function () { ' . chr(10) . ' var v0 = this._array_.shift();' . chr(10) . ' return v0[this._array_.shift()];' . chr(10) . ' }' . chr(10) . ' this.pop = function () { ' . chr(10) . ' var v1 = this._array_.pop();' . chr(10) . ' var v0 = this._array_.pop();' . chr(10) . ' return v0[v1];' . chr(10) . ' }' . chr(10) . '}' . chr(10) . chr(10) . chr(10))))
};
1;
View
14 src5/lib/Perlito5/Javascript2/Array.pm
@@ -346,11 +346,15 @@ function p5ArrayOfAlias(o) {
}
return this._array_.length / 2;
}
-
- // TODO - shift()
-
- // TODO - pop()
-
+ this.shift = function () {
+ var v0 = this._array_.shift();
+ return v0[this._array_.shift()];
+ }
+ this.pop = function () {
+ var v1 = this._array_.pop();
+ var v0 = this._array_.pop();
+ return v0[v1];
+ }
}

0 comments on commit 1a8d3e4

Please sign in to comment.