jsstring.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. #include "jsi.h"
  2. #include "utf.h"
  3. #include "regexp.h"
  4. static int js_doregexec(js_State *J, Reprog *prog, const char *string, Resub *sub, int eflags)
  5. {
  6. int result = js_regexec(prog, string, sub, eflags);
  7. if (result < 0)
  8. js_error(J, "regexec failed");
  9. return result;
  10. }
  11. static const char *checkstring(js_State *J, int idx)
  12. {
  13. if (!js_iscoercible(J, idx))
  14. js_typeerror(J, "string function called on null or undefined");
  15. return js_tostring(J, idx);
  16. }
  17. int js_runeat(js_State *J, const char *s, int i)
  18. {
  19. Rune rune = EOF;
  20. while (i >= 0) {
  21. rune = *(unsigned char*)s;
  22. if (rune < Runeself) {
  23. if (rune == 0)
  24. return EOF;
  25. ++s;
  26. --i;
  27. } else {
  28. s += chartorune(&rune, s);
  29. if (rune >= 0x10000)
  30. i -= 2;
  31. else
  32. --i;
  33. }
  34. }
  35. if (rune >= 0x10000) {
  36. /* high surrogate */
  37. if (i == -2)
  38. return 0xd800 + ((rune - 0x10000) >> 10);
  39. /* low surrogate */
  40. else
  41. return 0xdc00 + ((rune - 0x10000) & 0x3ff);
  42. }
  43. return rune;
  44. }
  45. int js_utflen(const char *s)
  46. {
  47. int c;
  48. int n;
  49. Rune rune;
  50. n = 0;
  51. for(;;) {
  52. c = *(unsigned char *)s;
  53. if (c < Runeself) {
  54. if (c == 0)
  55. return n;
  56. s++;
  57. n++;
  58. } else {
  59. s += chartorune(&rune, s);
  60. if (rune >= 0x10000)
  61. n += 2;
  62. else
  63. n++;
  64. }
  65. }
  66. }
  67. int js_utfptrtoidx(const char *s, const char *p)
  68. {
  69. Rune rune;
  70. int i = 0;
  71. while (s < p) {
  72. if (*(unsigned char *)s < Runeself)
  73. ++s;
  74. else
  75. s += chartorune(&rune, s);
  76. if (rune >= 0x10000)
  77. i += 2;
  78. else
  79. i += 1;
  80. }
  81. return i;
  82. }
  83. static void jsB_new_String(js_State *J)
  84. {
  85. js_newstring(J, js_gettop(J) > 1 ? js_tostring(J, 1) : "");
  86. }
  87. static void jsB_String(js_State *J)
  88. {
  89. js_pushstring(J, js_gettop(J) > 1 ? js_tostring(J, 1) : "");
  90. }
  91. static void Sp_toString(js_State *J)
  92. {
  93. js_Object *self = js_toobject(J, 0);
  94. if (self->type != JS_CSTRING) js_typeerror(J, "not a string");
  95. js_pushstring(J, self->u.s.string);
  96. }
  97. static void Sp_valueOf(js_State *J)
  98. {
  99. js_Object *self = js_toobject(J, 0);
  100. if (self->type != JS_CSTRING) js_typeerror(J, "not a string");
  101. js_pushstring(J, self->u.s.string);
  102. }
  103. static void Sp_charAt(js_State *J)
  104. {
  105. char buf[UTFmax + 1];
  106. const char *s = checkstring(J, 0);
  107. int pos = js_tointeger(J, 1);
  108. Rune rune = js_runeat(J, s, pos);
  109. if (rune >= 0) {
  110. buf[runetochar(buf, &rune)] = 0;
  111. js_pushstring(J, buf);
  112. } else {
  113. js_pushliteral(J, "");
  114. }
  115. }
  116. static void Sp_charCodeAt(js_State *J)
  117. {
  118. const char *s = checkstring(J, 0);
  119. int pos = js_tointeger(J, 1);
  120. Rune rune = js_runeat(J, s, pos);
  121. if (rune >= 0)
  122. js_pushnumber(J, rune);
  123. else
  124. js_pushnumber(J, NAN);
  125. }
  126. static void Sp_concat(js_State *J)
  127. {
  128. int i, top = js_gettop(J);
  129. int n;
  130. char * volatile out = NULL;
  131. const char *s;
  132. if (top == 1)
  133. return;
  134. s = checkstring(J, 0);
  135. n = 1 + strlen(s);
  136. if (js_try(J)) {
  137. js_free(J, out);
  138. js_throw(J);
  139. }
  140. if (n > JS_STRLIMIT)
  141. js_rangeerror(J, "invalid string length");
  142. out = js_malloc(J, n);
  143. strcpy(out, s);
  144. for (i = 1; i < top; ++i) {
  145. s = js_tostring(J, i);
  146. n += strlen(s);
  147. if (n > JS_STRLIMIT)
  148. js_rangeerror(J, "invalid string length");
  149. out = js_realloc(J, out, n);
  150. strcat(out, s);
  151. }
  152. js_pushstring(J, out);
  153. js_endtry(J);
  154. js_free(J, out);
  155. }
  156. static void Sp_indexOf(js_State *J)
  157. {
  158. const char *haystack = checkstring(J, 0);
  159. const char *needle = js_tostring(J, 1);
  160. int pos = js_tointeger(J, 2);
  161. int len = strlen(needle);
  162. int k = 0;
  163. Rune rune;
  164. while (*haystack) {
  165. if (k >= pos && !strncmp(haystack, needle, len)) {
  166. js_pushnumber(J, k);
  167. return;
  168. }
  169. haystack += chartorune(&rune, haystack);
  170. ++k;
  171. }
  172. js_pushnumber(J, -1);
  173. }
  174. static void Sp_lastIndexOf(js_State *J)
  175. {
  176. const char *haystack = checkstring(J, 0);
  177. const char *needle = js_tostring(J, 1);
  178. int pos = js_isdefined(J, 2) ? js_tointeger(J, 2) : (int)strlen(haystack);
  179. int len = strlen(needle);
  180. int k = 0, last = -1;
  181. Rune rune;
  182. while (*haystack && k <= pos) {
  183. if (!strncmp(haystack, needle, len))
  184. last = k;
  185. haystack += chartorune(&rune, haystack);
  186. ++k;
  187. }
  188. js_pushnumber(J, last);
  189. }
  190. static void Sp_localeCompare(js_State *J)
  191. {
  192. const char *a = checkstring(J, 0);
  193. const char *b = js_tostring(J, 1);
  194. js_pushnumber(J, strcmp(a, b));
  195. }
  196. static void Sp_substring_imp(js_State *J, const char *s, int a, int n)
  197. {
  198. Rune head_rune = 0, tail_rune = 0;
  199. const char *head, *tail;
  200. char *p;
  201. int i, k, head_len, tail_len;
  202. /* find start of substring */
  203. head = s;
  204. for (i = 0; i < a; ++i) {
  205. head += chartorune(&head_rune, head);
  206. if (head_rune >= 0x10000)
  207. ++i;
  208. }
  209. /* find end of substring */
  210. tail = head;
  211. for (k = i - a; k < n; ++k) {
  212. tail += chartorune(&tail_rune, tail);
  213. if (tail_rune >= 0x10000)
  214. ++k;
  215. }
  216. /* no surrogate pair splits! */
  217. if (i == a && k == n) {
  218. js_pushlstring(J, head, tail - head);
  219. return;
  220. }
  221. if (js_try(J)) {
  222. js_free(J, p);
  223. js_throw(J);
  224. }
  225. p = js_malloc(J, UTFmax + (tail - head));
  226. /* substring starts with low surrogate (head is just after character) */
  227. if (i > a) {
  228. head_rune = 0xdc00 + ((head_rune - 0x10000) & 0x3ff);
  229. head_len = runetochar(p, &head_rune);
  230. memcpy(p + head_len, head, tail - head);
  231. js_pushlstring(J, p, head_len + (tail - head));
  232. }
  233. /* substring ends with high surrogate (tail is just after character) */
  234. if (k > n) {
  235. tail -= runelen(tail_rune);
  236. memcpy(p, head, tail - head);
  237. tail_rune = 0xd800 + ((tail_rune - 0x10000) >> 10);
  238. tail_len = runetochar(p + (tail - head), &tail_rune);
  239. js_pushlstring(J, p, (tail - head) + tail_len);
  240. }
  241. js_endtry(J);
  242. js_free(J, p);
  243. }
  244. static void Sp_slice(js_State *J)
  245. {
  246. const char *str = checkstring(J, 0);
  247. int len = js_utflen(str);
  248. int s = js_tointeger(J, 1);
  249. int e = js_isdefined(J, 2) ? js_tointeger(J, 2) : len;
  250. s = s < 0 ? s + len : s;
  251. e = e < 0 ? e + len : e;
  252. s = s < 0 ? 0 : s > len ? len : s;
  253. e = e < 0 ? 0 : e > len ? len : e;
  254. if (s < e)
  255. Sp_substring_imp(J, str, s, e - s);
  256. else
  257. Sp_substring_imp(J, str, e, s - e);
  258. }
  259. static void Sp_substring(js_State *J)
  260. {
  261. const char *str = checkstring(J, 0);
  262. int len = js_utflen(str);
  263. int s = js_tointeger(J, 1);
  264. int e = js_isdefined(J, 2) ? js_tointeger(J, 2) : len;
  265. s = s < 0 ? 0 : s > len ? len : s;
  266. e = e < 0 ? 0 : e > len ? len : e;
  267. if (s < e)
  268. Sp_substring_imp(J, str, s, e - s);
  269. else
  270. Sp_substring_imp(J, str, e, s - e);
  271. }
  272. static void Sp_toLowerCase(js_State *J)
  273. {
  274. const char *s = checkstring(J, 0);
  275. char * volatile dst = NULL;
  276. char *d;
  277. Rune rune;
  278. if (js_try(J)) {
  279. js_free(J, dst);
  280. js_throw(J);
  281. }
  282. d = dst = js_malloc(J, UTFmax * strlen(s) + 1);
  283. while (*s) {
  284. s += chartorune(&rune, s);
  285. rune = tolowerrune(rune);
  286. d += runetochar(d, &rune);
  287. }
  288. *d = 0;
  289. js_pushstring(J, dst);
  290. js_endtry(J);
  291. js_free(J, dst);
  292. }
  293. static void Sp_toUpperCase(js_State *J)
  294. {
  295. const char *s = checkstring(J, 0);
  296. char * volatile dst = NULL;
  297. char *d;
  298. Rune rune;
  299. if (js_try(J)) {
  300. js_free(J, dst);
  301. js_throw(J);
  302. }
  303. d = dst = js_malloc(J, UTFmax * strlen(s) + 1);
  304. while (*s) {
  305. s += chartorune(&rune, s);
  306. rune = toupperrune(rune);
  307. d += runetochar(d, &rune);
  308. }
  309. *d = 0;
  310. js_pushstring(J, dst);
  311. js_endtry(J);
  312. js_free(J, dst);
  313. }
  314. static int istrim(int c)
  315. {
  316. return c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0 || c == 0xFEFF ||
  317. c == 0xA || c == 0xD || c == 0x2028 || c == 0x2029;
  318. }
  319. static void Sp_trim(js_State *J)
  320. {
  321. const char *s, *e;
  322. s = checkstring(J, 0);
  323. while (istrim(*s))
  324. ++s;
  325. e = s + strlen(s);
  326. while (e > s && istrim(e[-1]))
  327. --e;
  328. js_pushlstring(J, s, e - s);
  329. }
  330. static void S_fromCharCode(js_State *J)
  331. {
  332. int i, top = js_gettop(J);
  333. char * volatile s = NULL;
  334. char *p;
  335. Rune c;
  336. if (js_try(J)) {
  337. js_free(J, s);
  338. js_throw(J);
  339. }
  340. s = p = js_malloc(J, (top-1) * UTFmax + 1);
  341. for (i = 1; i < top; ++i) {
  342. c = js_touint32(J, i);
  343. p += runetochar(p, &c);
  344. }
  345. *p = 0;
  346. js_pushstring(J, s);
  347. js_endtry(J);
  348. js_free(J, s);
  349. }
  350. static void Sp_match(js_State *J)
  351. {
  352. js_Regexp *re;
  353. const char *text;
  354. int len;
  355. const char *a, *b, *c, *e;
  356. Resub m;
  357. text = checkstring(J, 0);
  358. if (js_isregexp(J, 1))
  359. js_copy(J, 1);
  360. else if (js_isundefined(J, 1))
  361. js_newregexp(J, "", 0);
  362. else
  363. js_newregexp(J, js_tostring(J, 1), 0);
  364. re = js_toregexp(J, -1);
  365. if (!(re->flags & JS_REGEXP_G)) {
  366. js_RegExp_prototype_exec(J, re, text);
  367. return;
  368. }
  369. re->last = 0;
  370. js_newarray(J);
  371. len = 0;
  372. a = text;
  373. e = text + strlen(text);
  374. while (a <= e) {
  375. if (js_doregexec(J, re->prog, a, &m, a > text ? REG_NOTBOL : 0))
  376. break;
  377. b = m.sub[0].sp;
  378. c = m.sub[0].ep;
  379. js_pushlstring(J, b, c - b);
  380. js_setindex(J, -2, len++);
  381. a = c;
  382. if (c - b == 0)
  383. ++a;
  384. }
  385. if (len == 0) {
  386. js_pop(J, 1);
  387. js_pushnull(J);
  388. }
  389. }
  390. static void Sp_search(js_State *J)
  391. {
  392. js_Regexp *re;
  393. const char *text;
  394. Resub m;
  395. text = checkstring(J, 0);
  396. if (js_isregexp(J, 1))
  397. js_copy(J, 1);
  398. else if (js_isundefined(J, 1))
  399. js_newregexp(J, "", 0);
  400. else
  401. js_newregexp(J, js_tostring(J, 1), 0);
  402. re = js_toregexp(J, -1);
  403. if (!js_doregexec(J, re->prog, text, &m, 0))
  404. js_pushnumber(J, js_utfptrtoidx(text, m.sub[0].sp));
  405. else
  406. js_pushnumber(J, -1);
  407. }
  408. static void Sp_replace_regexp(js_State *J)
  409. {
  410. js_Regexp *re;
  411. const char *source, *s, *r;
  412. js_Buffer *sb = NULL;
  413. int n, x;
  414. Resub m;
  415. source = checkstring(J, 0);
  416. re = js_toregexp(J, 1);
  417. if (js_doregexec(J, re->prog, source, &m, 0)) {
  418. js_copy(J, 0);
  419. return;
  420. }
  421. re->last = 0;
  422. loop:
  423. s = m.sub[0].sp;
  424. n = m.sub[0].ep - m.sub[0].sp;
  425. if (js_iscallable(J, 2)) {
  426. js_copy(J, 2);
  427. js_pushundefined(J);
  428. for (x = 0; m.sub[x].sp; ++x) /* arg 0..x: substring and subexps that matched */
  429. js_pushlstring(J, m.sub[x].sp, m.sub[x].ep - m.sub[x].sp);
  430. js_pushnumber(J, s - source); /* arg x+2: offset within search string */
  431. js_copy(J, 0); /* arg x+3: search string */
  432. js_call(J, 2 + x);
  433. r = js_tostring(J, -1);
  434. js_putm(J, &sb, source, s);
  435. js_puts(J, &sb, r);
  436. js_pop(J, 1);
  437. } else {
  438. r = js_tostring(J, 2);
  439. js_putm(J, &sb, source, s);
  440. while (*r) {
  441. if (*r == '$') {
  442. switch (*(++r)) {
  443. case 0: --r; /* end of string; back up */
  444. /* fallthrough */
  445. case '$': js_putc(J, &sb, '$'); break;
  446. case '`': js_putm(J, &sb, source, s); break;
  447. case '\'': js_puts(J, &sb, s + n); break;
  448. case '&':
  449. js_putm(J, &sb, s, s + n);
  450. break;
  451. case '0': case '1': case '2': case '3': case '4':
  452. case '5': case '6': case '7': case '8': case '9':
  453. x = *r - '0';
  454. if (r[1] >= '0' && r[1] <= '9')
  455. x = x * 10 + *(++r) - '0';
  456. if (x > 0 && x < m.nsub) {
  457. js_putm(J, &sb, m.sub[x].sp, m.sub[x].ep);
  458. } else {
  459. js_putc(J, &sb, '$');
  460. if (x > 10) {
  461. js_putc(J, &sb, '0' + x / 10);
  462. js_putc(J, &sb, '0' + x % 10);
  463. } else {
  464. js_putc(J, &sb, '0' + x);
  465. }
  466. }
  467. break;
  468. default:
  469. js_putc(J, &sb, '$');
  470. js_putc(J, &sb, *r);
  471. break;
  472. }
  473. ++r;
  474. } else {
  475. js_putc(J, &sb, *r++);
  476. }
  477. }
  478. }
  479. if (re->flags & JS_REGEXP_G) {
  480. source = m.sub[0].ep;
  481. if (n == 0) {
  482. if (*source)
  483. js_putc(J, &sb, *source++);
  484. else
  485. goto end;
  486. }
  487. if (!js_doregexec(J, re->prog, source, &m, REG_NOTBOL))
  488. goto loop;
  489. }
  490. end:
  491. js_puts(J, &sb, s + n);
  492. js_putc(J, &sb, 0);
  493. if (js_try(J)) {
  494. js_free(J, sb);
  495. js_throw(J);
  496. }
  497. js_pushstring(J, sb ? sb->s : "");
  498. js_endtry(J);
  499. js_free(J, sb);
  500. }
  501. static void Sp_replace_string(js_State *J)
  502. {
  503. const char *source, *needle, *s, *r;
  504. js_Buffer *sb = NULL;
  505. int n;
  506. source = checkstring(J, 0);
  507. needle = js_tostring(J, 1);
  508. s = strstr(source, needle);
  509. if (!s) {
  510. js_copy(J, 0);
  511. return;
  512. }
  513. n = strlen(needle);
  514. if (js_iscallable(J, 2)) {
  515. js_copy(J, 2);
  516. js_pushundefined(J);
  517. js_pushlstring(J, s, n); /* arg 1: substring that matched */
  518. js_pushnumber(J, s - source); /* arg 2: offset within search string */
  519. js_copy(J, 0); /* arg 3: search string */
  520. js_call(J, 3);
  521. r = js_tostring(J, -1);
  522. js_putm(J, &sb, source, s);
  523. js_puts(J, &sb, r);
  524. js_puts(J, &sb, s + n);
  525. js_putc(J, &sb, 0);
  526. js_pop(J, 1);
  527. } else {
  528. r = js_tostring(J, 2);
  529. js_putm(J, &sb, source, s);
  530. while (*r) {
  531. if (*r == '$') {
  532. switch (*(++r)) {
  533. case 0: --r; /* end of string; back up */
  534. /* fallthrough */
  535. case '$': js_putc(J, &sb, '$'); break;
  536. case '&': js_putm(J, &sb, s, s + n); break;
  537. case '`': js_putm(J, &sb, source, s); break;
  538. case '\'': js_puts(J, &sb, s + n); break;
  539. default: js_putc(J, &sb, '$'); js_putc(J, &sb, *r); break;
  540. }
  541. ++r;
  542. } else {
  543. js_putc(J, &sb, *r++);
  544. }
  545. }
  546. js_puts(J, &sb, s + n);
  547. js_putc(J, &sb, 0);
  548. }
  549. if (js_try(J)) {
  550. js_free(J, sb);
  551. js_throw(J);
  552. }
  553. js_pushstring(J, sb ? sb->s : "");
  554. js_endtry(J);
  555. js_free(J, sb);
  556. }
  557. static void Sp_replace(js_State *J)
  558. {
  559. if (js_isregexp(J, 1))
  560. Sp_replace_regexp(J);
  561. else
  562. Sp_replace_string(J);
  563. }
  564. static void Sp_split_regexp(js_State *J)
  565. {
  566. js_Regexp *re;
  567. const char *text;
  568. int limit, len, k;
  569. const char *p, *a, *b, *c, *e;
  570. Resub m;
  571. text = checkstring(J, 0);
  572. re = js_toregexp(J, 1);
  573. limit = js_isdefined(J, 2) ? js_tointeger(J, 2) : 1 << 30;
  574. js_newarray(J);
  575. len = 0;
  576. if (limit == 0)
  577. return;
  578. e = text + strlen(text);
  579. /* splitting the empty string */
  580. if (e == text) {
  581. if (js_doregexec(J, re->prog, text, &m, 0)) {
  582. js_pushliteral(J, "");
  583. js_setindex(J, -2, 0);
  584. }
  585. return;
  586. }
  587. p = a = text;
  588. while (a < e) {
  589. if (js_doregexec(J, re->prog, a, &m, a > text ? REG_NOTBOL : 0))
  590. break; /* no match */
  591. b = m.sub[0].sp;
  592. c = m.sub[0].ep;
  593. /* empty string at end of last match */
  594. if (b == c && b == p) {
  595. ++a;
  596. continue;
  597. }
  598. if (len == limit) return;
  599. js_pushlstring(J, p, b - p);
  600. js_setindex(J, -2, len++);
  601. for (k = 1; k < m.nsub; ++k) {
  602. if (len == limit) return;
  603. js_pushlstring(J, m.sub[k].sp, m.sub[k].ep - m.sub[k].sp);
  604. js_setindex(J, -2, len++);
  605. }
  606. a = p = c;
  607. }
  608. if (len == limit) return;
  609. js_pushstring(J, p);
  610. js_setindex(J, -2, len);
  611. }
  612. static void Sp_split_string(js_State *J)
  613. {
  614. const char *str = checkstring(J, 0);
  615. const char *sep = js_tostring(J, 1);
  616. int limit = js_isdefined(J, 2) ? js_tointeger(J, 2) : 1 << 30;
  617. int i, n;
  618. js_newarray(J);
  619. if (limit == 0)
  620. return;
  621. n = strlen(sep);
  622. /* empty string */
  623. if (n == 0) {
  624. Rune rune;
  625. for (i = 0; *str && i < limit; ++i) {
  626. n = chartorune(&rune, str);
  627. js_pushlstring(J, str, n);
  628. js_setindex(J, -2, i);
  629. str += n;
  630. }
  631. return;
  632. }
  633. for (i = 0; str && i < limit; ++i) {
  634. const char *s = strstr(str, sep);
  635. if (s) {
  636. js_pushlstring(J, str, s-str);
  637. js_setindex(J, -2, i);
  638. str = s + n;
  639. } else {
  640. js_pushstring(J, str);
  641. js_setindex(J, -2, i);
  642. str = NULL;
  643. }
  644. }
  645. }
  646. static void Sp_split(js_State *J)
  647. {
  648. if (js_isundefined(J, 1)) {
  649. js_newarray(J);
  650. js_pushstring(J, js_tostring(J, 0));
  651. js_setindex(J, -2, 0);
  652. } else if (js_isregexp(J, 1)) {
  653. Sp_split_regexp(J);
  654. } else {
  655. Sp_split_string(J);
  656. }
  657. }
  658. void jsB_initstring(js_State *J)
  659. {
  660. J->String_prototype->u.s.shrstr[0] = 0;
  661. J->String_prototype->u.s.string = J->String_prototype->u.s.shrstr;
  662. J->String_prototype->u.s.length = 0;
  663. js_pushobject(J, J->String_prototype);
  664. {
  665. jsB_propf(J, "String.prototype.toString", Sp_toString, 0);
  666. jsB_propf(J, "String.prototype.valueOf", Sp_valueOf, 0);
  667. jsB_propf(J, "String.prototype.charAt", Sp_charAt, 1);
  668. jsB_propf(J, "String.prototype.charCodeAt", Sp_charCodeAt, 1);
  669. jsB_propf(J, "String.prototype.concat", Sp_concat, 0); /* 1 */
  670. jsB_propf(J, "String.prototype.indexOf", Sp_indexOf, 1);
  671. jsB_propf(J, "String.prototype.lastIndexOf", Sp_lastIndexOf, 1);
  672. jsB_propf(J, "String.prototype.localeCompare", Sp_localeCompare, 1);
  673. jsB_propf(J, "String.prototype.match", Sp_match, 1);
  674. jsB_propf(J, "String.prototype.replace", Sp_replace, 2);
  675. jsB_propf(J, "String.prototype.search", Sp_search, 1);
  676. jsB_propf(J, "String.prototype.slice", Sp_slice, 2);
  677. jsB_propf(J, "String.prototype.split", Sp_split, 2);
  678. jsB_propf(J, "String.prototype.substring", Sp_substring, 2);
  679. jsB_propf(J, "String.prototype.toLowerCase", Sp_toLowerCase, 0);
  680. jsB_propf(J, "String.prototype.toLocaleLowerCase", Sp_toLowerCase, 0);
  681. jsB_propf(J, "String.prototype.toUpperCase", Sp_toUpperCase, 0);
  682. jsB_propf(J, "String.prototype.toLocaleUpperCase", Sp_toUpperCase, 0);
  683. /* ES5 */
  684. jsB_propf(J, "String.prototype.trim", Sp_trim, 0);
  685. }
  686. js_newcconstructor(J, jsB_String, jsB_new_String, "String", 0); /* 1 */
  687. {
  688. jsB_propf(J, "String.fromCharCode", S_fromCharCode, 0); /* 1 */
  689. }
  690. js_defglobal(J, "String", JS_DONTENUM);
  691. }