jsarray.c 17 KB

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