| 1 | use v5; | ||
| 2 | |||
| 3 | package Perlito5::JavaScript2::Array; | ||
| 4 | |||
| 5 | sub emit_javascript2 { | ||
| 6 | |||
| 7 | return <<'EOT'; | ||
| 8 | // | ||
| 9 | // lib/Perlito5/JavaScript2/Runtime.js | ||
| 10 | // | ||
| 11 | // Runtime for "Perlito" Perl5-in-JavaScript2 | ||
| 12 | // | ||
| 13 | // AUTHORS | ||
| 14 | // | ||
| 15 | // Flavio Soibelmann Glock fglock@gmail.com | ||
| 16 | // | ||
| 17 | // COPYRIGHT | ||
| 18 | // | ||
| 19 | // Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others. | ||
| 20 | // | ||
| 21 | // This program is free software; you can redistribute it and/or modify it | ||
| 22 | // under the same terms as Perl itself. | ||
| 23 | // | ||
| 24 | // See http://www.perl.com/perl/misc/Artistic.html | ||
| 25 | |||
| 26 | //-------- Array | ||
| 27 | |||
| 28 | Object.defineProperty( Array.prototype, "p5aget", { | ||
| 29 | enumerable : false, | ||
| 30 | value : function (i) { | ||
| 31 | if (i < 0) { i = this.length + i }; | ||
| 32 | return this[i] | ||
| 33 | } | ||
| 34 | }); | ||
| 35 | Object.defineProperty( Array.prototype, "p5aset", { | ||
| 36 | enumerable : false, | ||
| 37 | value : function (i, v) { | ||
| 38 | if (i < 0) { i = this.length + i }; | ||
| 39 | this[i] = v; | ||
| 40 | return this[i] | ||
| 41 | } | ||
| 42 | }); | ||
| 43 | |||
| 44 | Object.defineProperty( Array.prototype, "p5incr", { | ||
| 45 | enumerable : false, | ||
| 46 | value : function (i) { | ||
| 47 | if (i < 0) { i = this.length + i }; | ||
| 48 | this[i] = p5incr_(this[i]); | ||
| 49 | return this[i]; | ||
| 50 | } | ||
| 51 | }); | ||
| 52 | Object.defineProperty( Array.prototype, "p5postincr", { | ||
| 53 | enumerable : false, | ||
| 54 | value : function (i) { | ||
| 55 | if (i < 0) { i = this.length + i }; | ||
| 56 | var v = this[i]; | ||
| 57 | this[i] = p5incr_(this[i]); | ||
| 58 | return v; | ||
| 59 | } | ||
| 60 | }); | ||
| 61 | Object.defineProperty( Array.prototype, "p5decr", { | ||
| 62 | enumerable : false, | ||
| 63 | value : function (i) { | ||
| 64 | if (i < 0) { i = this.length + i }; | ||
| 65 | this[i] = p5decr_(this[i]); | ||
| 66 | return this[i]; | ||
| 67 | } | ||
| 68 | }); | ||
| 69 | Object.defineProperty( Array.prototype, "p5postdecr", { | ||
| 70 | enumerable : false, | ||
| 71 | value : function (i) { | ||
| 72 | if (i < 0) { i = this.length + i }; | ||
| 73 | var v = this[i]; | ||
| 74 | this[i] = p5decr_(this[i]); | ||
| 75 | return v; | ||
| 76 | } | ||
| 77 | }); | ||
| 78 | |||
| 79 | Object.defineProperty( Array.prototype, "p5aget_array", { | ||
| 80 | enumerable : false, | ||
| 81 | value : function (i) { | ||
| 82 | if (i < 0) { i = this.length + i }; | ||
| 83 | if (this[i] == null) { this[i] = new p5ArrayRef([]) } | ||
| 84 | return this[i] | ||
| 85 | } | ||
| 86 | }); | ||
| 87 | Object.defineProperty( Array.prototype, "p5aget_hash", { | ||
| 88 | enumerable : false, | ||
| 89 | value : function (i) { | ||
| 90 | if (i < 0) { i = this.length + i }; | ||
| 91 | if (this[i] == null) { this[i] = new p5HashRef({}) } | ||
| 92 | return this[i] | ||
| 93 | } | ||
| 94 | }); | ||
| 95 | Object.defineProperty( Array.prototype, "p5unshift", { | ||
| 96 | enumerable : false, | ||
| 97 | configurable : true, | ||
| 98 | value : function (args) { | ||
| 99 | for(var i = args.length-1; i >= 0; i--) { | ||
| 100 | this.unshift(args[i]); | ||
| 101 | } | ||
| 102 | return this.length; | ||
| 103 | } | ||
| 104 | }); | ||
| 105 | Object.defineProperty( Array.prototype, "p5push", { | ||
| 106 | enumerable : false, | ||
| 107 | configurable : true, | ||
| 108 | value : function (args) { | ||
| 109 | for(var i = 0; i < args.length; i++) { | ||
| 110 | this.push(args[i]); | ||
| 111 | } | ||
| 112 | return this.length; | ||
| 113 | } | ||
| 114 | }); | ||
| 115 | |||
| 116 | var p5tie_array = function(v, List__) { | ||
| 117 | var pkg_name = p5str(List__.shift()); | ||
| 118 | |||
| 119 | var res = p5call(pkg_name, 'TIEARRAY', List__, null); | ||
| 120 | |||
| 121 | // TODO | ||
| 122 | |||
| 123 | // A class implementing an ordinary array should have the following methods: | ||
| 124 | // TIEARRAY pkg_name, LIST | ||
| 125 | // FETCH this, key | ||
| 126 | // STORE this, key, value | ||
| 127 | // FETCHSIZE this | ||
| 128 | // STORESIZE this, count | ||
| 129 | // CLEAR this | ||
| 130 | // PUSH this, LIST | ||
| 131 | // POP this | ||
| 132 | // SHIFT this | ||
| 133 | // UNSHIFT this, LIST | ||
| 134 | // SPLICE this, offset, length, LIST | ||
| 135 | // EXTEND this, count | ||
| 136 | // DESTROY this | ||
| 137 | // UNTIE this | ||
| 138 | |||
| 139 | Object.defineProperty( v, "p5aget", { | ||
| 140 | enumerable : false, | ||
| 141 | configurable : true, | ||
| 142 | value : function (i) { | ||
| 143 | return p5call(res, 'FETCH', [i]); | ||
| 144 | } | ||
| 145 | }); | ||
| 146 | Object.defineProperty( v, "p5aset", { | ||
| 147 | enumerable : false, | ||
| 148 | configurable : true, | ||
| 149 | value : function (i, value) { | ||
| 150 | p5call(res, 'STORE', [i, value]); | ||
| 151 | return value; | ||
| 152 | } | ||
| 153 | }); | ||
| 154 | Object.defineProperty( v, "p5incr", { | ||
| 155 | enumerable : false, | ||
| 156 | configurable : true, | ||
| 157 | value : function (i) { | ||
| 158 | var value = p5incr_(p5call(res, 'FETCH', [i])); | ||
| 159 | p5call(res, 'STORE', [i, value]); | ||
| 160 | return value; | ||
| 161 | } | ||
| 162 | }); | ||
| 163 | Object.defineProperty( v, "p5postincr", { | ||
| 164 | enumerable : false, | ||
| 165 | configurable : true, | ||
| 166 | value : function (i) { | ||
| 167 | var value = p5call(res, 'FETCH', [i]); | ||
| 168 | p5call(res, 'STORE', [i, p5incr_(value)]); | ||
| 169 | return value; | ||
| 170 | } | ||
| 171 | }); | ||
| 172 | Object.defineProperty( v, "p5decr", { | ||
| 173 | enumerable : false, | ||
| 174 | configurable : true, | ||
| 175 | value : function (i) { | ||
| 176 | var value = p5decr_(p5call(res, 'FETCH', [i])); | ||
| 177 | p5call(res, 'STORE', [i, value]); | ||
| 178 | return value; | ||
| 179 | } | ||
| 180 | }); | ||
| 181 | Object.defineProperty( v, "p5postdecr", { | ||
| 182 | enumerable : false, | ||
| 183 | configurable : true, | ||
| 184 | value : function (i) { | ||
| 185 | var value = p5call(res, 'FETCH', [i]); | ||
| 186 | p5call(res, 'STORE', [i, p5decr_(value)]); | ||
| 187 | return value; | ||
| 188 | } | ||
| 189 | }); | ||
| 190 | |||
| 191 | Object.defineProperty( v, "p5aget_array", { | ||
| 192 | enumerable : false, | ||
| 193 | configurable : true, | ||
| 194 | value : function (i) { | ||
| 195 | var value = p5call(res, 'FETCH', [i]); | ||
| 196 | if (value == null) { | ||
| 197 | value = new p5ArrayRef([]); | ||
| 198 | p5call(res, 'STORE', [i, value]); | ||
| 199 | } | ||
| 200 | return value; | ||
| 201 | } | ||
| 202 | }); | ||
| 203 | Object.defineProperty( v, "p5aget_hash", { | ||
| 204 | enumerable : false, | ||
| 205 | configurable : true, | ||
| 206 | value : function (i) { | ||
| 207 | var value = p5call(res, 'FETCH', [i]); | ||
| 208 | if (value == null) { | ||
| 209 | value = new p5HashRef({}); | ||
| 210 | p5call(res, 'STORE', [i, value]); | ||
| 211 | } | ||
| 212 | return value; | ||
| 213 | } | ||
| 214 | }); | ||
| 215 | Object.defineProperty( v, "p5untie", { | ||
| 216 | enumerable : false, | ||
| 217 | configurable : true, | ||
| 218 | value : function (i) { return p5call(res, 'UNTIE', []) } | ||
| 219 | }); | ||
| 220 | Object.defineProperty( v, "shift", { | ||
| 221 | enumerable : false, | ||
| 222 | configurable : true, | ||
| 223 | value : function () { return p5call(res, 'SHIFT', []) } | ||
| 224 | }); | ||
| 225 | Object.defineProperty( v, "pop", { | ||
| 226 | enumerable : false, | ||
| 227 | configurable : true, | ||
| 228 | value : function () { return p5call(res, 'POP', []) } | ||
| 229 | }); | ||
| 230 | Object.defineProperty( v, "p5unshift", { | ||
| 231 | enumerable : false, | ||
| 232 | configurable : true, | ||
| 233 | value : function (args) { | ||
| 234 | for(var i = args.length-1; i >= 0; i--) { | ||
| 235 | p5call(res, 'UNSHIFT', [args[i]]); | ||
| 236 | } | ||
| 237 | return p5call(res, 'FETCHSIZE', []); | ||
| 238 | } | ||
| 239 | }); | ||
| 240 | Object.defineProperty( v, "p5push", { | ||
| 241 | enumerable : false, | ||
| 242 | configurable : true, | ||
| 243 | value : function (args) { | ||
| 244 | for(var i = 0; i < args.length; i++) { | ||
| 245 | p5call(res, 'PUSH', [args[i]]); | ||
| 246 | } | ||
| 247 | return p5call(res, 'FETCHSIZE', []); | ||
| 248 | } | ||
| 249 | }); | ||
| 250 | |||
| 251 | return res; | ||
| 252 | }; | ||
| 253 | |||
| 254 | var p5untie_array = function(v) { | ||
| 255 | if (v.hasOwnProperty('p5untie')) { | ||
| 256 | var res = v.p5untie(); // call UNTIE | ||
| 257 | delete v.p5aget; | ||
| 258 | delete v.p5aset; | ||
| 259 | delete v.p5incr; | ||
| 260 | delete v.p5postincr; | ||
| 261 | delete v.p5decr; | ||
| 262 | delete v.p5postdecr; | ||
| 263 | delete v.p5aget_array; | ||
| 264 | delete v.p5aget_hash; | ||
| 265 | delete v.p5untie; | ||
| 266 | delete v.shift; | ||
| 267 | delete v.pop; | ||
| 268 | delete v.p5unshift; | ||
| 269 | delete v.p5push; | ||
| 270 | return res; | ||
| 271 | } | ||
| 272 | else { | ||
| 273 | return null; | ||
| 274 | } | ||
| 275 | }; | ||
| 276 | |||
| 277 | |||
| 278 | function p5ArrayOfAlias(o) { | ||
| 279 | |||
| 280 | // this is the structure that represents @_ | ||
| 281 | // _array = [ ref, index, | ||
| 282 | // ref, index, | ||
| 283 | // ... | ||
| 284 | // ] | ||
| 285 | |||
| 286 | // TODO - autovivify array cells | ||
| 287 | |||
| 288 | this._array_ = o; | ||
| 289 | |||
| 290 | this.p5aget = function (i) { | ||
| 291 | if (i < 0) { i = this.length + i }; | ||
| 292 | return this._array_[i+i][this._array_[i+i+1]]; | ||
| 293 | } | ||
| 294 | this.p5aset = function (i, v) { | ||
| 295 | if (i < 0) { i = this.length + i }; | ||
| 296 | this._array_[i+i][this._array_[i+i+1]] = v; | ||
| 297 | return this._array_[i+i][this._array_[i+i+1]] | ||
| 298 | } | ||
| 299 | this.p5incr = function (i) { | ||
| 300 | if (i < 0) { i = this.length + i }; | ||
| 301 | this._array_[i+i][this._array_[i+i+1]] = p5incr_(this._array_[i+i][this._array_[i+i+1]]); | ||
| 302 | return this._array_[i+i][this._array_[i+i+1]]; | ||
| 303 | } | ||
| 304 | this.p5postincr = function (i) { | ||
| 305 | if (i < 0) { i = this.length + i }; | ||
| 306 | var v = this._array_[i+i][this._array_[i+i+1]]; | ||
| 307 | this._array_[i+i][this._array_[i+i+1]] = p5incr_(this._array_[i+i][this._array_[i+i+1]]); | ||
| 308 | return v; | ||
| 309 | } | ||
| 310 | this.p5decr = function (i) { | ||
| 311 | if (i < 0) { i = this.length + i }; | ||
| 312 | this._array_[i+i][this._array_[i+i+1]] = p5decr_(this._array_[i+i][this._array_[i+i+1]]); | ||
| 313 | return this._array_[i+i][this._array_[i+i+1]]; | ||
| 314 | } | ||
| 315 | this.p5postdecr = function (i) { | ||
| 316 | if (i < 0) { i = this.length + i }; | ||
| 317 | var v = this._array_[i+i][this._array_[i+i+1]]; | ||
| 318 | this._array_[i+i][this._array_[i+i+1]] = p5decr_(this._array_[i+i][this._array_[i+i+1]]); | ||
| 319 | return v; | ||
| 320 | } | ||
| 321 | this.p5aget_array = function (i) { | ||
| 322 | if (i < 0) { i = this.length + i }; | ||
| 323 | if (this._array_[i+i][this._array_[i+i+1]] == null) { | ||
| 324 | this._array_[i+i][this._array_[i+i+1]] = new p5ArrayRef([]) | ||
| 325 | } | ||
| 326 | return this._array_[i+i][this._array_[i+i+1]] | ||
| 327 | } | ||
| 328 | this.p5aget_hash = function (i) { | ||
| 329 | if (i < 0) { i = this.length + i }; | ||
| 330 | if (this._array_[i+i][this._array_[i+i+1]] == null) { | ||
| 331 | this._array_[i+i][this._array_[i+i+1]] = new p5HashRef({}) | ||
| 332 | } | ||
| 333 | return this._array_[i+i][this._array_[i+i+1]] | ||
| 334 | } | ||
| 335 | this.p5unshift = function (args) { | ||
| 336 | for(var i = args.length-1; i >= 0; i--) { | ||
| 337 | this.unshift(0); | ||
| 338 | this.unshift([args[i]]); | ||
| 339 | } | ||
| 340 | return this._array_.length / 2; | ||
| 341 | } | ||
| 342 | this.p5push = function (args) { | ||
| 343 | for(var i = 0; i < args.length; i++) { | ||
| 344 | this.push([args[i]]); | ||
| 345 | this.push(0); | ||
| 346 | } | ||
| 347 | return this._array_.length / 2; | ||
| 348 | } | ||
| 349 | this.shift = function () { | ||
| 350 | var v0 = this._array_.shift(); | ||
| 351 | return v0[this._array_.shift()]; | ||
| 352 | } | ||
| 353 | this.pop = function () { | ||
| 354 | var v1 = this._array_.pop(); | ||
| 355 | var v0 = this._array_.pop(); | ||
| 356 | return v0[v1]; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | |||
| 361 | EOT | ||
| 362 | |||
| 363 | } # end of emit_javascript2() | ||
| 364 | |||
| 365 | 1; | ||
| 366 | |||
| 367 |