jsvalue.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. #include "jsi.h"
  2. #include "utf.h"
  3. #define JSV_ISSTRING(v) (v->t.type==JS_TSHRSTR || v->t.type==JS_TMEMSTR || v->t.type==JS_TLITSTR)
  4. #define JSV_TOSTRING(v) (v->t.type==JS_TSHRSTR ? v->u.shrstr : v->t.type==JS_TLITSTR ? v->u.litstr : v->t.type==JS_TMEMSTR ? v->u.memstr->p : "")
  5. double js_strtol(const char *s, char **p, int base)
  6. {
  7. /* ascii -> digit value. max base is 36. */
  8. static const unsigned char table[256] = {
  9. 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
  10. 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
  11. 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
  12. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 80, 80, 80, 80, 80, 80,
  13. 80, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  14. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 80, 80, 80, 80, 80,
  15. 80, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  16. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 80, 80, 80, 80, 80,
  17. 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
  18. 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
  19. 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
  20. 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
  21. 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
  22. 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
  23. 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
  24. 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80
  25. };
  26. double x;
  27. unsigned char c;
  28. if (base == 10)
  29. for (x = 0, c = *s++; (0 <= c - '0') && (c - '0' < 10); c = *s++)
  30. x = x * 10 + (c - '0');
  31. else
  32. for (x = 0, c = *s++; table[c] < base; c = *s++)
  33. x = x * base + table[c];
  34. if (p)
  35. *p = (char*)s-1;
  36. return x;
  37. }
  38. int jsV_numbertointeger(double n)
  39. {
  40. if (n == 0) return 0;
  41. if (isnan(n)) return 0;
  42. n = (n < 0) ? -floor(-n) : floor(n);
  43. if (n < INT_MIN) return INT_MIN;
  44. if (n > INT_MAX) return INT_MAX;
  45. return (int)n;
  46. }
  47. int jsV_numbertoint32(double n)
  48. {
  49. double two32 = 4294967296.0;
  50. double two31 = 2147483648.0;
  51. if (!isfinite(n) || n == 0)
  52. return 0;
  53. n = fmod(n, two32);
  54. n = n >= 0 ? floor(n) : ceil(n) + two32;
  55. if (n >= two31)
  56. return n - two32;
  57. else
  58. return n;
  59. }
  60. unsigned int jsV_numbertouint32(double n)
  61. {
  62. return (unsigned int)jsV_numbertoint32(n);
  63. }
  64. short jsV_numbertoint16(double n)
  65. {
  66. return jsV_numbertoint32(n);
  67. }
  68. unsigned short jsV_numbertouint16(double n)
  69. {
  70. return jsV_numbertoint32(n);
  71. }
  72. /* obj.toString() */
  73. static int jsV_toString(js_State *J, js_Object *obj)
  74. {
  75. js_pushobject(J, obj);
  76. js_getproperty(J, -1, "toString");
  77. if (js_iscallable(J, -1)) {
  78. js_rot2(J);
  79. js_call(J, 0);
  80. if (js_isprimitive(J, -1))
  81. return 1;
  82. js_pop(J, 1);
  83. return 0;
  84. }
  85. js_pop(J, 2);
  86. return 0;
  87. }
  88. /* obj.valueOf() */
  89. static int jsV_valueOf(js_State *J, js_Object *obj)
  90. {
  91. js_pushobject(J, obj);
  92. js_getproperty(J, -1, "valueOf");
  93. if (js_iscallable(J, -1)) {
  94. js_rot2(J);
  95. js_call(J, 0);
  96. if (js_isprimitive(J, -1))
  97. return 1;
  98. js_pop(J, 1);
  99. return 0;
  100. }
  101. js_pop(J, 2);
  102. return 0;
  103. }
  104. /* ToPrimitive() on a value */
  105. void jsV_toprimitive(js_State *J, js_Value *v, int preferred)
  106. {
  107. js_Object *obj;
  108. if (v->t.type != JS_TOBJECT)
  109. return;
  110. obj = v->u.object;
  111. if (preferred == JS_HNONE)
  112. preferred = obj->type == JS_CDATE ? JS_HSTRING : JS_HNUMBER;
  113. if (preferred == JS_HSTRING) {
  114. if (jsV_toString(J, obj) || jsV_valueOf(J, obj)) {
  115. *v = *js_tovalue(J, -1);
  116. js_pop(J, 1);
  117. return;
  118. }
  119. } else {
  120. if (jsV_valueOf(J, obj) || jsV_toString(J, obj)) {
  121. *v = *js_tovalue(J, -1);
  122. js_pop(J, 1);
  123. return;
  124. }
  125. }
  126. if (J->strict)
  127. js_typeerror(J, "cannot convert object to primitive");
  128. v->t.type = JS_TLITSTR;
  129. v->u.litstr = "[object]";
  130. return;
  131. }
  132. /* ToBoolean() on a value */
  133. int jsV_toboolean(js_State *J, js_Value *v)
  134. {
  135. switch (v->t.type) {
  136. default:
  137. case JS_TSHRSTR: return v->u.shrstr[0] != 0;
  138. case JS_TUNDEFINED: return 0;
  139. case JS_TNULL: return 0;
  140. case JS_TBOOLEAN: return v->u.boolean;
  141. case JS_TNUMBER: return v->u.number != 0 && !isnan(v->u.number);
  142. case JS_TLITSTR: return v->u.litstr[0] != 0;
  143. case JS_TMEMSTR: return v->u.memstr->p[0] != 0;
  144. case JS_TOBJECT: return 1;
  145. }
  146. }
  147. const char *js_itoa(char *out, int v)
  148. {
  149. char buf[32], *s = out;
  150. unsigned int a;
  151. int i = 0;
  152. if (v < 0) {
  153. a = -v;
  154. *s++ = '-';
  155. } else {
  156. a = v;
  157. }
  158. while (a) {
  159. buf[i++] = (a % 10) + '0';
  160. a /= 10;
  161. }
  162. if (i == 0)
  163. buf[i++] = '0';
  164. while (i > 0)
  165. *s++ = buf[--i];
  166. *s = 0;
  167. return out;
  168. }
  169. double js_stringtofloat(const char *s, char **ep)
  170. {
  171. char *end;
  172. double n;
  173. const char *e = s;
  174. int isflt = 0;
  175. if (*e == '+' || *e == '-') ++e;
  176. while (*e >= '0' && *e <= '9') ++e;
  177. if (*e == '.') { ++e; isflt = 1; }
  178. while (*e >= '0' && *e <= '9') ++e;
  179. if (*e == 'e' || *e == 'E') {
  180. ++e;
  181. if (*e == '+' || *e == '-') ++e;
  182. while (*e >= '0' && *e <= '9') ++e;
  183. isflt = 1;
  184. }
  185. if (isflt)
  186. n = js_strtod(s, &end);
  187. else {
  188. /* js_strtol doesn't parse the sign */
  189. if (*s == '-')
  190. n = -js_strtol(s+1, &end, 10);
  191. else if (*s == '+')
  192. n = js_strtol(s+1, &end, 10);
  193. else
  194. n = js_strtol(s, &end, 10);
  195. }
  196. if (end == e) {
  197. *ep = (char*)e;
  198. return n;
  199. }
  200. *ep = (char*)s;
  201. return 0;
  202. }
  203. /* ToNumber() on a string */
  204. double jsV_stringtonumber(js_State *J, const char *s)
  205. {
  206. char *e;
  207. double n;
  208. while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s;
  209. if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && s[2] != 0)
  210. n = js_strtol(s + 2, &e, 16);
  211. else if (!strncmp(s, "Infinity", 8))
  212. n = INFINITY, e = (char*)s + 8;
  213. else if (!strncmp(s, "+Infinity", 9))
  214. n = INFINITY, e = (char*)s + 9;
  215. else if (!strncmp(s, "-Infinity", 9))
  216. n = -INFINITY, e = (char*)s + 9;
  217. else
  218. n = js_stringtofloat(s, &e);
  219. while (jsY_iswhite(*e) || jsY_isnewline(*e)) ++e;
  220. if (*e) return NAN;
  221. return n;
  222. }
  223. /* ToNumber() on a value */
  224. double jsV_tonumber(js_State *J, js_Value *v)
  225. {
  226. switch (v->t.type) {
  227. default:
  228. case JS_TSHRSTR: return jsV_stringtonumber(J, v->u.shrstr);
  229. case JS_TUNDEFINED: return NAN;
  230. case JS_TNULL: return 0;
  231. case JS_TBOOLEAN: return v->u.boolean;
  232. case JS_TNUMBER: return v->u.number;
  233. case JS_TLITSTR: return jsV_stringtonumber(J, v->u.litstr);
  234. case JS_TMEMSTR: return jsV_stringtonumber(J, v->u.memstr->p);
  235. case JS_TOBJECT:
  236. jsV_toprimitive(J, v, JS_HNUMBER);
  237. return jsV_tonumber(J, v);
  238. }
  239. }
  240. double jsV_tointeger(js_State *J, js_Value *v)
  241. {
  242. return jsV_numbertointeger(jsV_tonumber(J, v));
  243. }
  244. /* ToString() on a number */
  245. const char *jsV_numbertostring(js_State *J, char buf[32], double f)
  246. {
  247. char digits[32], *p = buf, *s = digits;
  248. int exp, ndigits, point;
  249. if (f == 0) return "0";
  250. if (isnan(f)) return "NaN";
  251. if (isinf(f)) return f < 0 ? "-Infinity" : "Infinity";
  252. /* Fast case for integers. This only works assuming all integers can be
  253. * exactly represented by a float. This is true for 32-bit integers and
  254. * 64-bit floats. */
  255. if (f >= INT_MIN && f <= INT_MAX) {
  256. int i = (int)f;
  257. if ((double)i == f)
  258. return js_itoa(buf, i);
  259. }
  260. ndigits = js_grisu2(f, digits, &exp);
  261. point = ndigits + exp;
  262. if (signbit(f))
  263. *p++ = '-';
  264. if (point < -5 || point > 21) {
  265. *p++ = *s++;
  266. if (ndigits > 1) {
  267. int n = ndigits - 1;
  268. *p++ = '.';
  269. while (n--)
  270. *p++ = *s++;
  271. }
  272. js_fmtexp(p, point - 1);
  273. }
  274. else if (point <= 0) {
  275. *p++ = '0';
  276. *p++ = '.';
  277. while (point++ < 0)
  278. *p++ = '0';
  279. while (ndigits-- > 0)
  280. *p++ = *s++;
  281. *p = 0;
  282. }
  283. else {
  284. while (ndigits-- > 0) {
  285. *p++ = *s++;
  286. if (--point == 0 && ndigits > 0)
  287. *p++ = '.';
  288. }
  289. while (point-- > 0)
  290. *p++ = '0';
  291. *p = 0;
  292. }
  293. return buf;
  294. }
  295. /* ToString() on a value */
  296. const char *jsV_tostring(js_State *J, js_Value *v)
  297. {
  298. char buf[32];
  299. const char *p;
  300. switch (v->t.type) {
  301. default:
  302. case JS_TSHRSTR: return v->u.shrstr;
  303. case JS_TUNDEFINED: return "undefined";
  304. case JS_TNULL: return "null";
  305. case JS_TBOOLEAN: return v->u.boolean ? "true" : "false";
  306. case JS_TLITSTR: return v->u.litstr;
  307. case JS_TMEMSTR: return v->u.memstr->p;
  308. case JS_TNUMBER:
  309. p = jsV_numbertostring(J, buf, v->u.number);
  310. if (p == buf) {
  311. int n = strlen(p);
  312. if (n <= soffsetof(js_Value, t.type)) {
  313. char *s = v->u.shrstr;
  314. while (n--) *s++ = *p++;
  315. *s = 0;
  316. v->t.type = JS_TSHRSTR;
  317. return v->u.shrstr;
  318. } else {
  319. v->u.memstr = jsV_newmemstring(J, p, n);
  320. v->t.type = JS_TMEMSTR;
  321. return v->u.memstr->p;
  322. }
  323. }
  324. return p;
  325. case JS_TOBJECT:
  326. jsV_toprimitive(J, v, JS_HSTRING);
  327. return jsV_tostring(J, v);
  328. }
  329. }
  330. /* Objects */
  331. static js_Object *jsV_newboolean(js_State *J, int v)
  332. {
  333. js_Object *obj = jsV_newobject(J, JS_CBOOLEAN, J->Boolean_prototype);
  334. obj->u.boolean = v;
  335. return obj;
  336. }
  337. static js_Object *jsV_newnumber(js_State *J, double v)
  338. {
  339. js_Object *obj = jsV_newobject(J, JS_CNUMBER, J->Number_prototype);
  340. obj->u.number = v;
  341. return obj;
  342. }
  343. static js_Object *jsV_newstring(js_State *J, const char *v)
  344. {
  345. js_Object *obj = jsV_newobject(J, JS_CSTRING, J->String_prototype);
  346. size_t n = strlen(v);
  347. if (n < sizeof(obj->u.s.shrstr)) {
  348. obj->u.s.string = obj->u.s.shrstr;
  349. memcpy(obj->u.s.shrstr, v, n + 1);
  350. } else {
  351. obj->u.s.string = js_strdup(J, v);
  352. }
  353. obj->u.s.length = js_utflen(v);
  354. return obj;
  355. }
  356. /* ToObject() on a value */
  357. js_Object *jsV_toobject(js_State *J, js_Value *v)
  358. {
  359. js_Object *o;
  360. switch (v->t.type) {
  361. default:
  362. case JS_TUNDEFINED: js_typeerror(J, "cannot convert undefined to object");
  363. case JS_TNULL: js_typeerror(J, "cannot convert null to object");
  364. case JS_TOBJECT: return v->u.object;
  365. case JS_TSHRSTR: o = jsV_newstring(J, v->u.shrstr); break;
  366. case JS_TLITSTR: o = jsV_newstring(J, v->u.litstr); break;
  367. case JS_TMEMSTR: o = jsV_newstring(J, v->u.memstr->p); break;
  368. case JS_TBOOLEAN: o = jsV_newboolean(J, v->u.boolean); break;
  369. case JS_TNUMBER: o = jsV_newnumber(J, v->u.number); break;
  370. }
  371. v->t.type = JS_TOBJECT;
  372. v->u.object = o;
  373. return o;
  374. }
  375. void js_newobjectx(js_State *J)
  376. {
  377. js_Object *prototype = NULL;
  378. if (js_isobject(J, -1))
  379. prototype = js_toobject(J, -1);
  380. js_pop(J, 1);
  381. js_pushobject(J, jsV_newobject(J, JS_COBJECT, prototype));
  382. }
  383. void js_newobject(js_State *J)
  384. {
  385. js_pushobject(J, jsV_newobject(J, JS_COBJECT, J->Object_prototype));
  386. }
  387. void js_newarguments(js_State *J)
  388. {
  389. js_pushobject(J, jsV_newobject(J, JS_CARGUMENTS, J->Object_prototype));
  390. }
  391. void js_newarray(js_State *J)
  392. {
  393. js_Object *obj = jsV_newobject(J, JS_CARRAY, J->Array_prototype);
  394. obj->u.a.simple = 1;
  395. js_pushobject(J, obj);
  396. }
  397. void js_newboolean(js_State *J, int v)
  398. {
  399. js_pushobject(J, jsV_newboolean(J, v));
  400. }
  401. void js_newnumber(js_State *J, double v)
  402. {
  403. js_pushobject(J, jsV_newnumber(J, v));
  404. }
  405. void js_newstring(js_State *J, const char *v)
  406. {
  407. js_pushobject(J, jsV_newstring(J, v));
  408. }
  409. void js_newfunction(js_State *J, js_Function *fun, js_Environment *scope)
  410. {
  411. js_Object *obj = jsV_newobject(J, JS_CFUNCTION, J->Function_prototype);
  412. obj->u.f.function = fun;
  413. obj->u.f.scope = scope;
  414. js_pushobject(J, obj);
  415. {
  416. js_pushnumber(J, fun->numparams);
  417. js_defproperty(J, -2, "length", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
  418. js_newobject(J);
  419. {
  420. js_copy(J, -2);
  421. js_defproperty(J, -2, "constructor", JS_DONTENUM);
  422. }
  423. js_defproperty(J, -2, "prototype", JS_DONTENUM | JS_DONTCONF);
  424. }
  425. }
  426. void js_newscript(js_State *J, js_Function *fun, js_Environment *scope)
  427. {
  428. js_Object *obj = jsV_newobject(J, JS_CSCRIPT, NULL);
  429. obj->u.f.function = fun;
  430. obj->u.f.scope = scope;
  431. js_pushobject(J, obj);
  432. }
  433. void js_newcfunctionx(js_State *J, js_CFunction cfun, const char *name, int length, void *data, js_Finalize finalize)
  434. {
  435. js_Object *obj;
  436. if (js_try(J)) {
  437. if (finalize)
  438. finalize(J, data);
  439. js_throw(J);
  440. }
  441. obj = jsV_newobject(J, JS_CCFUNCTION, J->Function_prototype);
  442. obj->u.c.name = name;
  443. obj->u.c.function = cfun;
  444. obj->u.c.constructor = NULL;
  445. obj->u.c.length = length;
  446. obj->u.c.data = data;
  447. obj->u.c.finalize = finalize;
  448. js_endtry(J);
  449. js_pushobject(J, obj);
  450. {
  451. js_pushnumber(J, length);
  452. js_defproperty(J, -2, "length", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
  453. js_newobject(J);
  454. {
  455. js_copy(J, -2);
  456. js_defproperty(J, -2, "constructor", JS_DONTENUM);
  457. }
  458. js_defproperty(J, -2, "prototype", JS_DONTENUM | JS_DONTCONF);
  459. }
  460. }
  461. void js_newcfunction(js_State *J, js_CFunction cfun, const char *name, int length)
  462. {
  463. js_newcfunctionx(J, cfun, name, length, NULL, NULL);
  464. }
  465. /* prototype -- constructor */
  466. void js_newcconstructor(js_State *J, js_CFunction cfun, js_CFunction ccon, const char *name, int length)
  467. {
  468. js_Object *obj = jsV_newobject(J, JS_CCFUNCTION, J->Function_prototype);
  469. obj->u.c.name = name;
  470. obj->u.c.function = cfun;
  471. obj->u.c.constructor = ccon;
  472. obj->u.c.length = length;
  473. js_pushobject(J, obj); /* proto obj */
  474. {
  475. js_pushnumber(J, length);
  476. js_defproperty(J, -2, "length", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
  477. js_rot2(J); /* obj proto */
  478. js_copy(J, -2); /* obj proto obj */
  479. js_defproperty(J, -2, "constructor", JS_DONTENUM);
  480. js_defproperty(J, -2, "prototype", JS_DONTENUM | JS_DONTCONF);
  481. }
  482. }
  483. void js_newuserdatax(js_State *J, const char *tag, void *data, js_HasProperty has, js_Put put, js_Delete delete, js_Finalize finalize)
  484. {
  485. js_Object *prototype = NULL;
  486. js_Object *obj;
  487. if (js_isobject(J, -1))
  488. prototype = js_toobject(J, -1);
  489. js_pop(J, 1);
  490. if (js_try(J)) {
  491. if (finalize)
  492. finalize(J, data);
  493. js_throw(J);
  494. }
  495. obj = jsV_newobject(J, JS_CUSERDATA, prototype);
  496. obj->u.user.tag = tag;
  497. obj->u.user.data = data;
  498. obj->u.user.has = has;
  499. obj->u.user.put = put;
  500. obj->u.user.delete = delete;
  501. obj->u.user.finalize = finalize;
  502. js_endtry(J);
  503. js_pushobject(J, obj);
  504. }
  505. void js_newuserdata(js_State *J, const char *tag, void *data, js_Finalize finalize)
  506. {
  507. js_newuserdatax(J, tag, data, NULL, NULL, NULL, finalize);
  508. }
  509. /* Non-trivial operations on values. These are implemented using the stack. */
  510. int js_instanceof(js_State *J)
  511. {
  512. js_Object *O, *V;
  513. if (!js_iscallable(J, -1))
  514. js_typeerror(J, "instanceof: invalid operand");
  515. if (!js_isobject(J, -2))
  516. return 0;
  517. js_getproperty(J, -1, "prototype");
  518. if (!js_isobject(J, -1))
  519. js_typeerror(J, "instanceof: 'prototype' property is not an object");
  520. O = js_toobject(J, -1);
  521. js_pop(J, 1);
  522. V = js_toobject(J, -2);
  523. while (V) {
  524. V = V->prototype;
  525. if (O == V)
  526. return 1;
  527. }
  528. return 0;
  529. }
  530. void js_concat(js_State *J)
  531. {
  532. js_toprimitive(J, -2, JS_HNONE);
  533. js_toprimitive(J, -1, JS_HNONE);
  534. if (js_isstring(J, -2) || js_isstring(J, -1)) {
  535. const char *sa = js_tostring(J, -2);
  536. const char *sb = js_tostring(J, -1);
  537. char * volatile sab = NULL;
  538. /* TODO: create js_String directly */
  539. if (js_try(J)) {
  540. js_free(J, sab);
  541. js_throw(J);
  542. }
  543. sab = js_malloc(J, strlen(sa) + strlen(sb) + 1);
  544. strcpy(sab, sa);
  545. strcat(sab, sb);
  546. js_pop(J, 2);
  547. js_pushstring(J, sab);
  548. js_endtry(J);
  549. js_free(J, sab);
  550. } else {
  551. double x = js_tonumber(J, -2);
  552. double y = js_tonumber(J, -1);
  553. js_pop(J, 2);
  554. js_pushnumber(J, x + y);
  555. }
  556. }
  557. int js_compare(js_State *J, int *okay)
  558. {
  559. js_toprimitive(J, -2, JS_HNUMBER);
  560. js_toprimitive(J, -1, JS_HNUMBER);
  561. *okay = 1;
  562. if (js_isstring(J, -2) && js_isstring(J, -1)) {
  563. return strcmp(js_tostring(J, -2), js_tostring(J, -1));
  564. } else {
  565. double x = js_tonumber(J, -2);
  566. double y = js_tonumber(J, -1);
  567. if (isnan(x) || isnan(y))
  568. *okay = 0;
  569. return x < y ? -1 : x > y ? 1 : 0;
  570. }
  571. }
  572. int js_equal(js_State *J)
  573. {
  574. js_Value *x = js_tovalue(J, -2);
  575. js_Value *y = js_tovalue(J, -1);
  576. retry:
  577. if (JSV_ISSTRING(x) && JSV_ISSTRING(y))
  578. return !strcmp(JSV_TOSTRING(x), JSV_TOSTRING(y));
  579. if (x->t.type == y->t.type) {
  580. if (x->t.type == JS_TUNDEFINED) return 1;
  581. if (x->t.type == JS_TNULL) return 1;
  582. if (x->t.type == JS_TNUMBER) return x->u.number == y->u.number;
  583. if (x->t.type == JS_TBOOLEAN) return x->u.boolean == y->u.boolean;
  584. if (x->t.type == JS_TOBJECT) return x->u.object == y->u.object;
  585. return 0;
  586. }
  587. if (x->t.type == JS_TNULL && y->t.type == JS_TUNDEFINED) return 1;
  588. if (x->t.type == JS_TUNDEFINED && y->t.type == JS_TNULL) return 1;
  589. if (x->t.type == JS_TNUMBER && JSV_ISSTRING(y))
  590. return x->u.number == jsV_tonumber(J, y);
  591. if (JSV_ISSTRING(x) && y->t.type == JS_TNUMBER)
  592. return jsV_tonumber(J, x) == y->u.number;
  593. if (x->t.type == JS_TBOOLEAN) {
  594. x->t.type = JS_TNUMBER;
  595. x->u.number = x->u.boolean ? 1 : 0;
  596. goto retry;
  597. }
  598. if (y->t.type == JS_TBOOLEAN) {
  599. y->t.type = JS_TNUMBER;
  600. y->u.number = y->u.boolean ? 1 : 0;
  601. goto retry;
  602. }
  603. if ((JSV_ISSTRING(x) || x->t.type == JS_TNUMBER) && y->t.type == JS_TOBJECT) {
  604. jsV_toprimitive(J, y, JS_HNONE);
  605. goto retry;
  606. }
  607. if (x->t.type == JS_TOBJECT && (JSV_ISSTRING(y) || y->t.type == JS_TNUMBER)) {
  608. jsV_toprimitive(J, x, JS_HNONE);
  609. goto retry;
  610. }
  611. return 0;
  612. }
  613. int js_strictequal(js_State *J)
  614. {
  615. js_Value *x = js_tovalue(J, -2);
  616. js_Value *y = js_tovalue(J, -1);
  617. if (JSV_ISSTRING(x) && JSV_ISSTRING(y))
  618. return !strcmp(JSV_TOSTRING(x), JSV_TOSTRING(y));
  619. if (x->t.type != y->t.type) return 0;
  620. if (x->t.type == JS_TUNDEFINED) return 1;
  621. if (x->t.type == JS_TNULL) return 1;
  622. if (x->t.type == JS_TNUMBER) return x->u.number == y->u.number;
  623. if (x->t.type == JS_TBOOLEAN) return x->u.boolean == y->u.boolean;
  624. if (x->t.type == JS_TOBJECT) return x->u.object == y->u.object;
  625. return 0;
  626. }