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