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