zig-pug 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +346 -0
  3. package/binding.c +375 -0
  4. package/binding.gyp +28 -0
  5. package/common.gypi +5 -0
  6. package/include/zigpug.h +135 -0
  7. package/index.js +205 -0
  8. package/package.json +87 -0
  9. package/vendor/mujs/COPYING +16 -0
  10. package/vendor/mujs/README +50 -0
  11. package/vendor/mujs/astnames.h +92 -0
  12. package/vendor/mujs/jsarray.c +832 -0
  13. package/vendor/mujs/jsboolean.c +38 -0
  14. package/vendor/mujs/jsbuiltin.c +249 -0
  15. package/vendor/mujs/jscompile.c +1428 -0
  16. package/vendor/mujs/jsdate.c +861 -0
  17. package/vendor/mujs/jsdtoa.c +749 -0
  18. package/vendor/mujs/jserror.c +139 -0
  19. package/vendor/mujs/jsfunction.c +231 -0
  20. package/vendor/mujs/jsgc.c +284 -0
  21. package/vendor/mujs/jsi.h +870 -0
  22. package/vendor/mujs/jsintern.c +137 -0
  23. package/vendor/mujs/jslex.c +878 -0
  24. package/vendor/mujs/jsmath.c +194 -0
  25. package/vendor/mujs/jsnumber.c +198 -0
  26. package/vendor/mujs/jsobject.c +560 -0
  27. package/vendor/mujs/json.c +422 -0
  28. package/vendor/mujs/jsparse.c +1065 -0
  29. package/vendor/mujs/jsproperty.c +341 -0
  30. package/vendor/mujs/jsregexp.c +232 -0
  31. package/vendor/mujs/jsrepr.c +285 -0
  32. package/vendor/mujs/jsrun.c +2096 -0
  33. package/vendor/mujs/jsstate.c +334 -0
  34. package/vendor/mujs/jsstring.c +852 -0
  35. package/vendor/mujs/jsvalue.c +708 -0
  36. package/vendor/mujs/libmujs.a +0 -0
  37. package/vendor/mujs/main.c +396 -0
  38. package/vendor/mujs/mujs.h +253 -0
  39. package/vendor/mujs/one.c +25 -0
  40. package/vendor/mujs/opnames.h +85 -0
  41. package/vendor/mujs/pp.c +980 -0
  42. package/vendor/mujs/regexp.c +1277 -0
  43. package/vendor/mujs/regexp.h +46 -0
  44. package/vendor/mujs/utf.c +305 -0
  45. package/vendor/mujs/utf.h +52 -0
  46. package/vendor/mujs/utfdata.h +2209 -0
@@ -0,0 +1,832 @@
1
+ #include "jsi.h"
2
+
3
+ #ifndef JS_HEAPSORT
4
+ #define JS_HEAPSORT 0
5
+ #endif
6
+
7
+ int js_getlength(js_State *J, int idx)
8
+ {
9
+ int len;
10
+ js_getproperty(J, idx, "length");
11
+ len = js_tointeger(J, -1);
12
+ js_pop(J, 1);
13
+ return len;
14
+ }
15
+
16
+ void js_setlength(js_State *J, int idx, int len)
17
+ {
18
+ js_pushnumber(J, len);
19
+ js_setproperty(J, idx < 0 ? idx - 1 : idx, "length");
20
+ }
21
+
22
+ static void jsB_new_Array(js_State *J)
23
+ {
24
+ int i, top = js_gettop(J);
25
+
26
+ js_newarray(J);
27
+
28
+ if (top == 2) {
29
+ if (js_isnumber(J, 1)) {
30
+ js_copy(J, 1);
31
+ js_setproperty(J, -2, "length");
32
+ } else {
33
+ js_copy(J, 1);
34
+ js_setindex(J, -2, 0);
35
+ }
36
+ } else {
37
+ for (i = 1; i < top; ++i) {
38
+ js_copy(J, i);
39
+ js_setindex(J, -2, i - 1);
40
+ }
41
+ }
42
+ }
43
+
44
+ static void Ap_concat(js_State *J)
45
+ {
46
+ int i, top = js_gettop(J);
47
+ int n, k, len;
48
+
49
+ js_newarray(J);
50
+ n = 0;
51
+
52
+ for (i = 0; i < top; ++i) {
53
+ js_copy(J, i);
54
+ if (js_isarray(J, -1)) {
55
+ len = js_getlength(J, -1);
56
+ for (k = 0; k < len; ++k)
57
+ if (js_hasindex(J, -1, k))
58
+ js_setindex(J, -3, n++);
59
+ js_pop(J, 1);
60
+ } else {
61
+ js_setindex(J, -2, n++);
62
+ }
63
+ }
64
+ }
65
+
66
+ static void Ap_join(js_State *J)
67
+ {
68
+ char * volatile out = NULL;
69
+ const char * volatile r = NULL;
70
+ const char *sep;
71
+ int seplen;
72
+ int k, n, len, rlen;
73
+
74
+ len = js_getlength(J, 0);
75
+
76
+ if (js_isdefined(J, 1)) {
77
+ sep = js_tostring(J, 1);
78
+ seplen = strlen(sep);
79
+ } else {
80
+ sep = ",";
81
+ seplen = 1;
82
+ }
83
+
84
+ if (len <= 0) {
85
+ js_pushliteral(J, "");
86
+ return;
87
+ }
88
+
89
+ if (js_try(J)) {
90
+ js_free(J, out);
91
+ js_throw(J);
92
+ }
93
+
94
+ n = 0;
95
+ for (k = 0; k < len; ++k) {
96
+ js_getindex(J, 0, k);
97
+ if (js_iscoercible(J, -1)) {
98
+ r = js_tostring(J, -1);
99
+ rlen = strlen(r);
100
+ } else {
101
+ rlen = 0;
102
+ }
103
+
104
+ if (k == 0) {
105
+ out = js_malloc(J, rlen + 1);
106
+ if (rlen > 0) {
107
+ memcpy(out, r, rlen);
108
+ n += rlen;
109
+ }
110
+ } else {
111
+ if (n + seplen + rlen > JS_STRLIMIT)
112
+ js_rangeerror(J, "invalid string length");
113
+ out = js_realloc(J, out, n + seplen + rlen + 1);
114
+ if (seplen > 0) {
115
+ memcpy(out + n, sep, seplen);
116
+ n += seplen;
117
+ }
118
+ if (rlen > 0) {
119
+ memcpy(out + n, r, rlen);
120
+ n += rlen;
121
+ }
122
+ }
123
+
124
+ js_pop(J, 1);
125
+ }
126
+
127
+ js_pushlstring(J, out, n);
128
+ js_endtry(J);
129
+ js_free(J, out);
130
+ }
131
+
132
+ static void Ap_pop(js_State *J)
133
+ {
134
+ int n;
135
+
136
+ n = js_getlength(J, 0);
137
+
138
+ if (n > 0) {
139
+ js_getindex(J, 0, n - 1);
140
+ js_delindex(J, 0, n - 1);
141
+ js_setlength(J, 0, n - 1);
142
+ } else {
143
+ js_setlength(J, 0, 0);
144
+ js_pushundefined(J);
145
+ }
146
+ }
147
+
148
+ static void Ap_push(js_State *J)
149
+ {
150
+ int i, top = js_gettop(J);
151
+ int n;
152
+
153
+ n = js_getlength(J, 0);
154
+
155
+ for (i = 1; i < top; ++i, ++n) {
156
+ js_copy(J, i);
157
+ js_setindex(J, 0, n);
158
+ }
159
+
160
+ js_setlength(J, 0, n);
161
+
162
+ js_pushnumber(J, n);
163
+ }
164
+
165
+ static void Ap_reverse(js_State *J)
166
+ {
167
+ int len, middle, lower;
168
+
169
+ len = js_getlength(J, 0);
170
+ middle = len / 2;
171
+ lower = 0;
172
+
173
+ while (lower != middle) {
174
+ int upper = len - lower - 1;
175
+ int haslower = js_hasindex(J, 0, lower);
176
+ int hasupper = js_hasindex(J, 0, upper);
177
+ if (haslower && hasupper) {
178
+ js_setindex(J, 0, lower);
179
+ js_setindex(J, 0, upper);
180
+ } else if (hasupper) {
181
+ js_setindex(J, 0, lower);
182
+ js_delindex(J, 0, upper);
183
+ } else if (haslower) {
184
+ js_setindex(J, 0, upper);
185
+ js_delindex(J, 0, lower);
186
+ }
187
+ ++lower;
188
+ }
189
+
190
+ js_copy(J, 0);
191
+ }
192
+
193
+ static void Ap_shift(js_State *J)
194
+ {
195
+ int k, len;
196
+
197
+ len = js_getlength(J, 0);
198
+
199
+ if (len == 0) {
200
+ js_setlength(J, 0, 0);
201
+ js_pushundefined(J);
202
+ return;
203
+ }
204
+
205
+ js_getindex(J, 0, 0);
206
+
207
+ for (k = 1; k < len; ++k) {
208
+ if (js_hasindex(J, 0, k))
209
+ js_setindex(J, 0, k - 1);
210
+ else
211
+ js_delindex(J, 0, k - 1);
212
+ }
213
+
214
+ js_delindex(J, 0, len - 1);
215
+ js_setlength(J, 0, len - 1);
216
+ }
217
+
218
+ static void Ap_slice(js_State *J)
219
+ {
220
+ int len, s, e, n;
221
+ double sv, ev;
222
+
223
+ js_newarray(J);
224
+
225
+ len = js_getlength(J, 0);
226
+ sv = js_tointeger(J, 1);
227
+ ev = js_isdefined(J, 2) ? js_tointeger(J, 2) : len;
228
+
229
+ if (sv < 0) sv = sv + len;
230
+ if (ev < 0) ev = ev + len;
231
+
232
+ s = sv < 0 ? 0 : sv > len ? len : sv;
233
+ e = ev < 0 ? 0 : ev > len ? len : ev;
234
+
235
+ for (n = 0; s < e; ++s, ++n)
236
+ if (js_hasindex(J, 0, s))
237
+ js_setindex(J, -2, n);
238
+ }
239
+
240
+ static int Ap_sort_cmp(js_State *J, int idx_a, int idx_b)
241
+ {
242
+ js_Object *obj = js_tovalue(J, 0)->u.object;
243
+ if (obj->u.a.simple) {
244
+ js_Value *val_a = &obj->u.a.array[idx_a];
245
+ js_Value *val_b = &obj->u.a.array[idx_b];
246
+ int und_a = val_a->t.type == JS_TUNDEFINED;
247
+ int und_b = val_b->t.type == JS_TUNDEFINED;
248
+ if (und_a) return und_b;
249
+ if (und_b) return -1;
250
+ if (js_iscallable(J, 1)) {
251
+ double v;
252
+ js_copy(J, 1); /* copy function */
253
+ js_pushundefined(J); /* no 'this' binding */
254
+ js_pushvalue(J, *val_a);
255
+ js_pushvalue(J, *val_b);
256
+ js_call(J, 2);
257
+ v = js_tonumber(J, -1);
258
+ js_pop(J, 1);
259
+ if (isnan(v))
260
+ return 0;
261
+ if (v == 0)
262
+ return 0;
263
+ return v < 0 ? -1 : 1;
264
+ } else {
265
+ const char *str_a, *str_b;
266
+ int c;
267
+ js_pushvalue(J, *val_a);
268
+ js_pushvalue(J, *val_b);
269
+ str_a = js_tostring(J, -2);
270
+ str_b = js_tostring(J, -1);
271
+ c = strcmp(str_a, str_b);
272
+ js_pop(J, 2);
273
+ return c;
274
+ }
275
+ } else {
276
+ int und_a, und_b;
277
+ int has_a = js_hasindex(J, 0, idx_a);
278
+ int has_b = js_hasindex(J, 0, idx_b);
279
+ if (!has_a && !has_b) {
280
+ return 0;
281
+ }
282
+ if (has_a && !has_b) {
283
+ js_pop(J, 1);
284
+ return -1;
285
+ }
286
+ if (!has_a && has_b) {
287
+ js_pop(J, 1);
288
+ return 1;
289
+ }
290
+
291
+ und_a = js_isundefined(J, -2);
292
+ und_b = js_isundefined(J, -1);
293
+ if (und_a) {
294
+ js_pop(J, 2);
295
+ return und_b;
296
+ }
297
+ if (und_b) {
298
+ js_pop(J, 2);
299
+ return -1;
300
+ }
301
+
302
+ if (js_iscallable(J, 1)) {
303
+ double v;
304
+ js_copy(J, 1); /* copy function */
305
+ js_pushundefined(J); /* no 'this' binding */
306
+ js_copy(J, -4);
307
+ js_copy(J, -4);
308
+ js_call(J, 2);
309
+ v = js_tonumber(J, -1);
310
+ js_pop(J, 3);
311
+ if (isnan(v))
312
+ return 0;
313
+ if (v == 0)
314
+ return 0;
315
+ return v < 0 ? -1 : 1;
316
+ } else {
317
+ const char *str_a = js_tostring(J, -2);
318
+ const char *str_b = js_tostring(J, -1);
319
+ int c = strcmp(str_a, str_b);
320
+ js_pop(J, 2);
321
+ return c;
322
+ }
323
+ }
324
+ }
325
+
326
+ static void Ap_sort_swap(js_State *J, int idx_a, int idx_b)
327
+ {
328
+ js_Object *obj = js_tovalue(J, 0)->u.object;
329
+ if (obj->u.a.simple) {
330
+ js_Value tmp = obj->u.a.array[idx_a];
331
+ obj->u.a.array[idx_a] = obj->u.a.array[idx_b];
332
+ obj->u.a.array[idx_b] = tmp;
333
+ } else {
334
+ int has_a = js_hasindex(J, 0, idx_a);
335
+ int has_b = js_hasindex(J, 0, idx_b);
336
+ if (has_a && has_b) {
337
+ js_setindex(J, 0, idx_a);
338
+ js_setindex(J, 0, idx_b);
339
+ } else if (has_a && !has_b) {
340
+ js_delindex(J, 0, idx_a);
341
+ js_setindex(J, 0, idx_b);
342
+ } else if (!has_a && has_b) {
343
+ js_delindex(J, 0, idx_b);
344
+ js_setindex(J, 0, idx_a);
345
+ }
346
+ }
347
+ }
348
+
349
+ /* A bottom-up/bouncing heapsort implementation */
350
+
351
+ static int Ap_sort_leaf(js_State *J, int i, int end)
352
+ {
353
+ int j = i;
354
+ int lc = (j << 1) + 1; /* left child */
355
+ int rc = (j << 1) + 2; /* right child */
356
+ while (rc < end) {
357
+ if (Ap_sort_cmp(J, rc, lc) > 0)
358
+ j = rc;
359
+ else
360
+ j = lc;
361
+ lc = (j << 1) + 1;
362
+ rc = (j << 1) + 2;
363
+ }
364
+ if (lc < end)
365
+ j = lc;
366
+ return j;
367
+ }
368
+
369
+ static void Ap_sort_sift(js_State *J, int i, int end)
370
+ {
371
+ int j = Ap_sort_leaf(J, i, end);
372
+ while (Ap_sort_cmp(J, i, j) > 0)
373
+ j = (j - 1) >> 1; /* parent */
374
+ while (j > i) {
375
+ Ap_sort_swap(J, i, j);
376
+ j = (j - 1) >> 1; /* parent */
377
+ }
378
+ }
379
+
380
+ static void Ap_sort_heapsort(js_State *J, int n)
381
+ {
382
+ int i;
383
+ for (i = n / 2 - 1; i >= 0; --i)
384
+ Ap_sort_sift(J, i, n);
385
+ for (i = n - 1; i > 0; --i) {
386
+ Ap_sort_swap(J, 0, i);
387
+ Ap_sort_sift(J, 0, i);
388
+ }
389
+ }
390
+
391
+ static void Ap_sort(js_State *J)
392
+ {
393
+ int len;
394
+
395
+ len = js_getlength(J, 0);
396
+ if (len <= 1) {
397
+ js_copy(J, 0);
398
+ return;
399
+ }
400
+
401
+ if (!js_iscallable(J, 1) && !js_isundefined(J, 1))
402
+ js_typeerror(J, "comparison function must be a function or undefined");
403
+
404
+ if (len >= INT_MAX)
405
+ js_rangeerror(J, "array is too large to sort");
406
+
407
+ Ap_sort_heapsort(J, len);
408
+
409
+ js_copy(J, 0);
410
+ }
411
+
412
+ static void Ap_splice(js_State *J)
413
+ {
414
+ int top = js_gettop(J);
415
+ int len, start, del, add, k;
416
+
417
+ len = js_getlength(J, 0);
418
+ start = js_tointeger(J, 1);
419
+ if (start < 0)
420
+ start = (len + start) > 0 ? len + start : 0;
421
+ else if (start > len)
422
+ start = len;
423
+
424
+ if (js_isdefined(J, 2))
425
+ del = js_tointeger(J, 2);
426
+ else
427
+ del = len - start;
428
+ if (del > len - start)
429
+ del = len - start;
430
+ if (del < 0)
431
+ del = 0;
432
+
433
+ js_newarray(J);
434
+
435
+ /* copy deleted items to return array */
436
+ for (k = 0; k < del; ++k)
437
+ if (js_hasindex(J, 0, start + k))
438
+ js_setindex(J, -2, k);
439
+ js_setlength(J, -1, del);
440
+
441
+ /* shift the tail to resize the hole left by deleted items */
442
+ add = top - 3;
443
+ if (add < del) {
444
+ for (k = start; k < len - del; ++k) {
445
+ if (js_hasindex(J, 0, k + del))
446
+ js_setindex(J, 0, k + add);
447
+ else
448
+ js_delindex(J, 0, k + add);
449
+ }
450
+ for (k = len; k > len - del + add; --k)
451
+ js_delindex(J, 0, k - 1);
452
+ } else if (add > del) {
453
+ for (k = len - del; k > start; --k) {
454
+ if (js_hasindex(J, 0, k + del - 1))
455
+ js_setindex(J, 0, k + add - 1);
456
+ else
457
+ js_delindex(J, 0, k + add - 1);
458
+ }
459
+ }
460
+
461
+ /* copy new items into the hole */
462
+ for (k = 0; k < add; ++k) {
463
+ js_copy(J, 3 + k);
464
+ js_setindex(J, 0, start + k);
465
+ }
466
+
467
+ js_setlength(J, 0, len - del + add);
468
+ }
469
+
470
+ static void Ap_unshift(js_State *J)
471
+ {
472
+ int i, top = js_gettop(J);
473
+ int k, len;
474
+
475
+ len = js_getlength(J, 0);
476
+
477
+ for (k = len; k > 0; --k) {
478
+ int from = k - 1;
479
+ int to = k + top - 2;
480
+ if (js_hasindex(J, 0, from))
481
+ js_setindex(J, 0, to);
482
+ else
483
+ js_delindex(J, 0, to);
484
+ }
485
+
486
+ for (i = 1; i < top; ++i) {
487
+ js_copy(J, i);
488
+ js_setindex(J, 0, i - 1);
489
+ }
490
+
491
+ js_setlength(J, 0, len + top - 1);
492
+
493
+ js_pushnumber(J, len + top - 1);
494
+ }
495
+
496
+ static void Ap_toString(js_State *J)
497
+ {
498
+ if (!js_iscoercible(J, 0))
499
+ js_typeerror(J, "'this' is not an object");
500
+ js_getproperty(J, 0, "join");
501
+ if (!js_iscallable(J, -1)) {
502
+ js_pop(J, 1);
503
+ /* TODO: call Object.prototype.toString implementation directly */
504
+ js_getglobal(J, "Object");
505
+ js_getproperty(J, -1, "prototype");
506
+ js_rot2pop1(J);
507
+ js_getproperty(J, -1, "toString");
508
+ js_rot2pop1(J);
509
+ }
510
+ js_copy(J, 0);
511
+ js_call(J, 0);
512
+ }
513
+
514
+ static void Ap_indexOf(js_State *J)
515
+ {
516
+ int k, len, from;
517
+
518
+ len = js_getlength(J, 0);
519
+ from = js_isdefined(J, 2) ? js_tointeger(J, 2) : 0;
520
+ if (from < 0) from = len + from;
521
+ if (from < 0) from = 0;
522
+
523
+ js_copy(J, 1);
524
+ for (k = from; k < len; ++k) {
525
+ if (js_hasindex(J, 0, k)) {
526
+ if (js_strictequal(J)) {
527
+ js_pushnumber(J, k);
528
+ return;
529
+ }
530
+ js_pop(J, 1);
531
+ }
532
+ }
533
+
534
+ js_pushnumber(J, -1);
535
+ }
536
+
537
+ static void Ap_lastIndexOf(js_State *J)
538
+ {
539
+ int k, len, from;
540
+
541
+ len = js_getlength(J, 0);
542
+ from = js_isdefined(J, 2) ? js_tointeger(J, 2) : len - 1;
543
+ if (from > len - 1) from = len - 1;
544
+ if (from < 0) from = len + from;
545
+
546
+ js_copy(J, 1);
547
+ for (k = from; k >= 0; --k) {
548
+ if (js_hasindex(J, 0, k)) {
549
+ if (js_strictequal(J)) {
550
+ js_pushnumber(J, k);
551
+ return;
552
+ }
553
+ js_pop(J, 1);
554
+ }
555
+ }
556
+
557
+ js_pushnumber(J, -1);
558
+ }
559
+
560
+ static void Ap_every(js_State *J)
561
+ {
562
+ int hasthis = js_gettop(J) >= 3;
563
+ int k, len;
564
+
565
+ if (!js_iscallable(J, 1))
566
+ js_typeerror(J, "callback is not a function");
567
+
568
+ len = js_getlength(J, 0);
569
+ for (k = 0; k < len; ++k) {
570
+ if (js_hasindex(J, 0, k)) {
571
+ js_copy(J, 1);
572
+ if (hasthis)
573
+ js_copy(J, 2);
574
+ else
575
+ js_pushundefined(J);
576
+ js_copy(J, -3);
577
+ js_pushnumber(J, k);
578
+ js_copy(J, 0);
579
+ js_call(J, 3);
580
+ if (!js_toboolean(J, -1))
581
+ return;
582
+ js_pop(J, 2);
583
+ }
584
+ }
585
+
586
+ js_pushboolean(J, 1);
587
+ }
588
+
589
+ static void Ap_some(js_State *J)
590
+ {
591
+ int hasthis = js_gettop(J) >= 3;
592
+ int k, len;
593
+
594
+ if (!js_iscallable(J, 1))
595
+ js_typeerror(J, "callback is not a function");
596
+
597
+ len = js_getlength(J, 0);
598
+ for (k = 0; k < len; ++k) {
599
+ if (js_hasindex(J, 0, k)) {
600
+ js_copy(J, 1);
601
+ if (hasthis)
602
+ js_copy(J, 2);
603
+ else
604
+ js_pushundefined(J);
605
+ js_copy(J, -3);
606
+ js_pushnumber(J, k);
607
+ js_copy(J, 0);
608
+ js_call(J, 3);
609
+ if (js_toboolean(J, -1))
610
+ return;
611
+ js_pop(J, 2);
612
+ }
613
+ }
614
+
615
+ js_pushboolean(J, 0);
616
+ }
617
+
618
+ static void Ap_forEach(js_State *J)
619
+ {
620
+ int hasthis = js_gettop(J) >= 3;
621
+ int k, len;
622
+
623
+ if (!js_iscallable(J, 1))
624
+ js_typeerror(J, "callback is not a function");
625
+
626
+ len = js_getlength(J, 0);
627
+ for (k = 0; k < len; ++k) {
628
+ if (js_hasindex(J, 0, k)) {
629
+ js_copy(J, 1);
630
+ if (hasthis)
631
+ js_copy(J, 2);
632
+ else
633
+ js_pushundefined(J);
634
+ js_copy(J, -3);
635
+ js_pushnumber(J, k);
636
+ js_copy(J, 0);
637
+ js_call(J, 3);
638
+ js_pop(J, 2);
639
+ }
640
+ }
641
+
642
+ js_pushundefined(J);
643
+ }
644
+
645
+ static void Ap_map(js_State *J)
646
+ {
647
+ int hasthis = js_gettop(J) >= 3;
648
+ int k, len;
649
+
650
+ if (!js_iscallable(J, 1))
651
+ js_typeerror(J, "callback is not a function");
652
+
653
+ js_newarray(J);
654
+
655
+ len = js_getlength(J, 0);
656
+ for (k = 0; k < len; ++k) {
657
+ if (js_hasindex(J, 0, k)) {
658
+ js_copy(J, 1);
659
+ if (hasthis)
660
+ js_copy(J, 2);
661
+ else
662
+ js_pushundefined(J);
663
+ js_copy(J, -3);
664
+ js_pushnumber(J, k);
665
+ js_copy(J, 0);
666
+ js_call(J, 3);
667
+ js_setindex(J, -3, k);
668
+ js_pop(J, 1);
669
+ }
670
+ }
671
+ js_setlength(J, -1, len);
672
+ }
673
+
674
+ static void Ap_filter(js_State *J)
675
+ {
676
+ int hasthis = js_gettop(J) >= 3;
677
+ int k, to, len;
678
+
679
+ if (!js_iscallable(J, 1))
680
+ js_typeerror(J, "callback is not a function");
681
+
682
+ js_newarray(J);
683
+ to = 0;
684
+
685
+ len = js_getlength(J, 0);
686
+ for (k = 0; k < len; ++k) {
687
+ if (js_hasindex(J, 0, k)) {
688
+ js_copy(J, 1);
689
+ if (hasthis)
690
+ js_copy(J, 2);
691
+ else
692
+ js_pushundefined(J);
693
+ js_copy(J, -3);
694
+ js_pushnumber(J, k);
695
+ js_copy(J, 0);
696
+ js_call(J, 3);
697
+ if (js_toboolean(J, -1)) {
698
+ js_pop(J, 1);
699
+ js_setindex(J, -2, to++);
700
+ } else {
701
+ js_pop(J, 2);
702
+ }
703
+ }
704
+ }
705
+ }
706
+
707
+ static void Ap_reduce(js_State *J)
708
+ {
709
+ int hasinitial = js_gettop(J) >= 3;
710
+ int k, len;
711
+
712
+ if (!js_iscallable(J, 1))
713
+ js_typeerror(J, "callback is not a function");
714
+
715
+ len = js_getlength(J, 0);
716
+ k = 0;
717
+
718
+ if (len == 0 && !hasinitial)
719
+ js_typeerror(J, "no initial value");
720
+
721
+ /* initial value of accumulator */
722
+ if (hasinitial)
723
+ js_copy(J, 2);
724
+ else {
725
+ while (k < len)
726
+ if (js_hasindex(J, 0, k++))
727
+ break;
728
+ if (k == len)
729
+ js_typeerror(J, "no initial value");
730
+ }
731
+
732
+ while (k < len) {
733
+ if (js_hasindex(J, 0, k)) {
734
+ js_copy(J, 1);
735
+ js_pushundefined(J);
736
+ js_rot(J, 4); /* accumulator on top */
737
+ js_rot(J, 4); /* property on top */
738
+ js_pushnumber(J, k);
739
+ js_copy(J, 0);
740
+ js_call(J, 4); /* calculate new accumulator */
741
+ }
742
+ ++k;
743
+ }
744
+
745
+ /* return accumulator */
746
+ }
747
+
748
+ static void Ap_reduceRight(js_State *J)
749
+ {
750
+ int hasinitial = js_gettop(J) >= 3;
751
+ int k, len;
752
+
753
+ if (!js_iscallable(J, 1))
754
+ js_typeerror(J, "callback is not a function");
755
+
756
+ len = js_getlength(J, 0);
757
+ k = len - 1;
758
+
759
+ if (len == 0 && !hasinitial)
760
+ js_typeerror(J, "no initial value");
761
+
762
+ /* initial value of accumulator */
763
+ if (hasinitial)
764
+ js_copy(J, 2);
765
+ else {
766
+ while (k >= 0)
767
+ if (js_hasindex(J, 0, k--))
768
+ break;
769
+ if (k < 0)
770
+ js_typeerror(J, "no initial value");
771
+ }
772
+
773
+ while (k >= 0) {
774
+ if (js_hasindex(J, 0, k)) {
775
+ js_copy(J, 1);
776
+ js_pushundefined(J);
777
+ js_rot(J, 4); /* accumulator on top */
778
+ js_rot(J, 4); /* property on top */
779
+ js_pushnumber(J, k);
780
+ js_copy(J, 0);
781
+ js_call(J, 4); /* calculate new accumulator */
782
+ }
783
+ --k;
784
+ }
785
+
786
+ /* return accumulator */
787
+ }
788
+
789
+ static void A_isArray(js_State *J)
790
+ {
791
+ if (js_isobject(J, 1)) {
792
+ js_Object *T = js_toobject(J, 1);
793
+ js_pushboolean(J, T->type == JS_CARRAY);
794
+ } else {
795
+ js_pushboolean(J, 0);
796
+ }
797
+ }
798
+
799
+ void jsB_initarray(js_State *J)
800
+ {
801
+ js_pushobject(J, J->Array_prototype);
802
+ {
803
+ jsB_propf(J, "Array.prototype.toString", Ap_toString, 0);
804
+ jsB_propf(J, "Array.prototype.concat", Ap_concat, 0); /* 1 */
805
+ jsB_propf(J, "Array.prototype.join", Ap_join, 1);
806
+ jsB_propf(J, "Array.prototype.pop", Ap_pop, 0);
807
+ jsB_propf(J, "Array.prototype.push", Ap_push, 0); /* 1 */
808
+ jsB_propf(J, "Array.prototype.reverse", Ap_reverse, 0);
809
+ jsB_propf(J, "Array.prototype.shift", Ap_shift, 0);
810
+ jsB_propf(J, "Array.prototype.slice", Ap_slice, 2);
811
+ jsB_propf(J, "Array.prototype.sort", Ap_sort, 1);
812
+ jsB_propf(J, "Array.prototype.splice", Ap_splice, 2);
813
+ jsB_propf(J, "Array.prototype.unshift", Ap_unshift, 0); /* 1 */
814
+
815
+ /* ES5 */
816
+ jsB_propf(J, "Array.prototype.indexOf", Ap_indexOf, 1);
817
+ jsB_propf(J, "Array.prototype.lastIndexOf", Ap_lastIndexOf, 1);
818
+ jsB_propf(J, "Array.prototype.every", Ap_every, 1);
819
+ jsB_propf(J, "Array.prototype.some", Ap_some, 1);
820
+ jsB_propf(J, "Array.prototype.forEach", Ap_forEach, 1);
821
+ jsB_propf(J, "Array.prototype.map", Ap_map, 1);
822
+ jsB_propf(J, "Array.prototype.filter", Ap_filter, 1);
823
+ jsB_propf(J, "Array.prototype.reduce", Ap_reduce, 1);
824
+ jsB_propf(J, "Array.prototype.reduceRight", Ap_reduceRight, 1);
825
+ }
826
+ js_newcconstructor(J, jsB_new_Array, jsB_new_Array, "Array", 0); /* 1 */
827
+ {
828
+ /* ES5 */
829
+ jsB_propf(J, "Array.isArray", A_isArray, 1);
830
+ }
831
+ js_defglobal(J, "Array", JS_DONTENUM);
832
+ }