Coverage Report

Created: 2023-06-07 06:20

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