Coverage Report

Created: 2025-12-03 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mupdf/thirdparty/mujs/jsvalue.c
Line
Count
Source
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
    /* js_strtol doesn't parse the sign */
210
0
    if (*s == '-')
211
0
      n = -js_strtol(s+1, &end, 10);
212
0
    else if (*s == '+')
213
0
      n = js_strtol(s+1, &end, 10);
214
0
    else
215
0
      n = js_strtol(s, &end, 10);
216
0
  }
217
0
  if (end == e) {
218
0
    *ep = (char*)e;
219
0
    return n;
220
0
  }
221
0
  *ep = (char*)s;
222
0
  return 0;
223
0
}
224
225
/* ToNumber() on a string */
226
double jsV_stringtonumber(js_State *J, const char *s)
227
0
{
228
0
  char *e;
229
0
  double n;
230
0
  while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s;
231
0
  if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && s[2] != 0)
232
0
    n = js_strtol(s + 2, &e, 16);
233
0
  else if (!strncmp(s, "Infinity", 8))
234
0
    n = INFINITY, e = (char*)s + 8;
235
0
  else if (!strncmp(s, "+Infinity", 9))
236
0
    n = INFINITY, e = (char*)s + 9;
237
0
  else if (!strncmp(s, "-Infinity", 9))
238
0
    n = -INFINITY, e = (char*)s + 9;
239
0
  else
240
0
    n = js_stringtofloat(s, &e);
241
0
  while (jsY_iswhite(*e) || jsY_isnewline(*e)) ++e;
242
0
  if (*e) return NAN;
243
0
  return n;
244
0
}
245
246
/* ToNumber() on a value */
247
double jsV_tonumber(js_State *J, js_Value *v)
248
0
{
249
0
  switch (v->t.type) {
250
0
  default:
251
0
  case JS_TSHRSTR: return jsV_stringtonumber(J, v->u.shrstr);
252
0
  case JS_TUNDEFINED: return NAN;
253
0
  case JS_TNULL: return 0;
254
0
  case JS_TBOOLEAN: return v->u.boolean;
255
0
  case JS_TNUMBER: return v->u.number;
256
0
  case JS_TLITSTR: return jsV_stringtonumber(J, v->u.litstr);
257
0
  case JS_TMEMSTR: return jsV_stringtonumber(J, v->u.memstr->p);
258
0
  case JS_TOBJECT:
259
0
    jsV_toprimitive(J, v, JS_HNUMBER);
260
0
    return jsV_tonumber(J, v);
261
0
  }
262
0
}
263
264
double jsV_tointeger(js_State *J, js_Value *v)
265
0
{
266
0
  return jsV_numbertointeger(jsV_tonumber(J, v));
267
0
}
268
269
/* ToString() on a number */
270
const char *jsV_numbertostring(js_State *J, char buf[32], double f)
271
0
{
272
0
  char digits[32], *p = buf, *s = digits;
273
0
  int exp, ndigits, point;
274
275
0
  if (f == 0) return "0";
276
0
  if (isnan(f)) return "NaN";
277
0
  if (isinf(f)) return f < 0 ? "-Infinity" : "Infinity";
278
279
  /* Fast case for integers. This only works assuming all integers can be
280
   * exactly represented by a float. This is true for 32-bit integers and
281
   * 64-bit floats. */
282
0
  if (f >= INT_MIN && f <= INT_MAX) {
283
0
    int i = (int)f;
284
0
    if ((double)i == f)
285
0
      return js_itoa(buf, i);
286
0
  }
287
288
0
  ndigits = js_grisu2(f, digits, &exp);
289
0
  point = ndigits + exp;
290
291
0
  if (signbit(f))
292
0
    *p++ = '-';
293
294
0
  if (point < -5 || point > 21) {
295
0
    *p++ = *s++;
296
0
    if (ndigits > 1) {
297
0
      int n = ndigits - 1;
298
0
      *p++ = '.';
299
0
      while (n--)
300
0
        *p++ = *s++;
301
0
    }
302
0
    js_fmtexp(p, point - 1);
303
0
  }
304
305
0
  else if (point <= 0) {
306
0
    *p++ = '0';
307
0
    *p++ = '.';
308
0
    while (point++ < 0)
309
0
      *p++ = '0';
310
0
    while (ndigits-- > 0)
311
0
      *p++ = *s++;
312
0
    *p = 0;
313
0
  }
314
315
0
  else {
316
0
    while (ndigits-- > 0) {
317
0
      *p++ = *s++;
318
0
      if (--point == 0 && ndigits > 0)
319
0
        *p++ = '.';
320
0
    }
321
0
    while (point-- > 0)
322
0
      *p++ = '0';
323
0
    *p = 0;
324
0
  }
325
326
0
  return buf;
327
0
}
328
329
/* ToString() on a value */
330
const char *jsV_tostring(js_State *J, js_Value *v)
331
0
{
332
0
  char buf[32];
333
0
  const char *p;
334
0
  switch (v->t.type) {
335
0
  default:
336
0
  case JS_TSHRSTR: return v->u.shrstr;
337
0
  case JS_TUNDEFINED: return "undefined";
338
0
  case JS_TNULL: return "null";
339
0
  case JS_TBOOLEAN: return v->u.boolean ? "true" : "false";
340
0
  case JS_TLITSTR: return v->u.litstr;
341
0
  case JS_TMEMSTR: return v->u.memstr->p;
342
0
  case JS_TNUMBER:
343
0
    p = jsV_numbertostring(J, buf, v->u.number);
344
0
    if (p == buf) {
345
0
      int n = strlen(p);
346
0
      if (n <= soffsetof(js_Value, t.type)) {
347
0
        char *s = v->u.shrstr;
348
0
        while (n--) *s++ = *p++;
349
0
        *s = 0;
350
0
        v->t.type = JS_TSHRSTR;
351
0
        return v->u.shrstr;
352
0
      } else {
353
0
        v->u.memstr = jsV_newmemstring(J, p, n);
354
0
        v->t.type = JS_TMEMSTR;
355
0
        return v->u.memstr->p;
356
0
      }
357
0
    }
358
0
    return p;
359
0
  case JS_TOBJECT:
360
0
    jsV_toprimitive(J, v, JS_HSTRING);
361
0
    return jsV_tostring(J, v);
362
0
  }
363
0
}
364
365
/* Objects */
366
367
static js_Object *jsV_newboolean(js_State *J, int v)
368
0
{
369
0
  js_Object *obj = jsV_newobject(J, JS_CBOOLEAN, J->Boolean_prototype);
370
0
  obj->u.boolean = v;
371
0
  return obj;
372
0
}
373
374
static js_Object *jsV_newnumber(js_State *J, double v)
375
0
{
376
0
  js_Object *obj = jsV_newobject(J, JS_CNUMBER, J->Number_prototype);
377
0
  obj->u.number = v;
378
0
  return obj;
379
0
}
380
381
static js_Object *jsV_newstring(js_State *J, const char *v)
382
0
{
383
0
  js_Object *obj = jsV_newobject(J, JS_CSTRING, J->String_prototype);
384
0
  size_t n = strlen(v);
385
0
  if (n < sizeof(obj->u.s.shrstr)) {
386
0
    obj->u.s.string = obj->u.s.shrstr;
387
0
    memcpy(obj->u.s.shrstr, v, n + 1);
388
0
  } else {
389
0
    obj->u.s.string = js_strdup(J, v);
390
0
  }
391
0
  obj->u.s.length = js_utflen(v);
392
0
  return obj;
393
0
}
394
395
/* ToObject() on a value */
396
js_Object *jsV_toobject(js_State *J, js_Value *v)
397
0
{
398
0
  js_Object *o;
399
0
  switch (v->t.type) {
400
0
  default:
401
0
  case JS_TUNDEFINED: js_typeerror(J, "cannot convert undefined to object");
402
0
  case JS_TNULL: js_typeerror(J, "cannot convert null to object");
403
0
  case JS_TOBJECT: return v->u.object;
404
0
  case JS_TSHRSTR: o = jsV_newstring(J, v->u.shrstr); break;
405
0
  case JS_TLITSTR: o = jsV_newstring(J, v->u.litstr); break;
406
0
  case JS_TMEMSTR: o = jsV_newstring(J, v->u.memstr->p); break;
407
0
  case JS_TBOOLEAN: o = jsV_newboolean(J, v->u.boolean); break;
408
0
  case JS_TNUMBER: o = jsV_newnumber(J, v->u.number); break;
409
0
  }
410
0
  v->t.type = JS_TOBJECT;
411
0
  v->u.object = o;
412
0
  return o;
413
0
}
414
415
void js_newobjectx(js_State *J)
416
0
{
417
0
  js_Object *prototype = NULL;
418
0
  if (js_isobject(J, -1))
419
0
    prototype = js_toobject(J, -1);
420
0
  js_pop(J, 1);
421
0
  js_pushobject(J, jsV_newobject(J, JS_COBJECT, prototype));
422
0
}
423
424
void js_newobject(js_State *J)
425
0
{
426
0
  js_pushobject(J, jsV_newobject(J, JS_COBJECT, J->Object_prototype));
427
0
}
428
429
void js_newarguments(js_State *J)
430
0
{
431
0
  js_pushobject(J, jsV_newobject(J, JS_CARGUMENTS, J->Object_prototype));
432
0
}
433
434
void js_newarray(js_State *J)
435
0
{
436
0
  js_Object *obj = jsV_newobject(J, JS_CARRAY, J->Array_prototype);
437
0
  obj->u.a.simple = 1;
438
0
  js_pushobject(J, obj);
439
0
}
440
441
void js_newboolean(js_State *J, int v)
442
0
{
443
0
  js_pushobject(J, jsV_newboolean(J, v));
444
0
}
445
446
void js_newnumber(js_State *J, double v)
447
0
{
448
0
  js_pushobject(J, jsV_newnumber(J, v));
449
0
}
450
451
void js_newstring(js_State *J, const char *v)
452
0
{
453
0
  js_pushobject(J, jsV_newstring(J, v));
454
0
}
455
456
void js_newfunction(js_State *J, js_Function *fun, js_Environment *scope)
457
0
{
458
0
  js_Object *obj = jsV_newobject(J, JS_CFUNCTION, J->Function_prototype);
459
0
  obj->u.f.function = fun;
460
0
  obj->u.f.scope = scope;
461
0
  js_pushobject(J, obj);
462
0
  {
463
0
    js_pushnumber(J, fun->numparams);
464
0
    js_defproperty(J, -2, "length", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
465
0
    js_newobject(J);
466
0
    {
467
0
      js_copy(J, -2);
468
0
      js_defproperty(J, -2, "constructor", JS_DONTENUM);
469
0
    }
470
0
    js_defproperty(J, -2, "prototype", JS_DONTENUM | JS_DONTCONF);
471
0
  }
472
0
}
473
474
void js_newscript(js_State *J, js_Function *fun, js_Environment *scope)
475
0
{
476
0
  js_Object *obj = jsV_newobject(J, JS_CSCRIPT, NULL);
477
0
  obj->u.f.function = fun;
478
0
  obj->u.f.scope = scope;
479
0
  js_pushobject(J, obj);
480
0
}
481
482
void js_newcfunctionx(js_State *J, js_CFunction cfun, const char *name, int length, void *data, js_Finalize finalize)
483
0
{
484
0
  js_Object *obj;
485
486
0
  if (js_try(J)) {
487
0
    if (finalize)
488
0
      finalize(J, data);
489
0
    js_throw(J);
490
0
  }
491
492
0
  obj = jsV_newobject(J, JS_CCFUNCTION, J->Function_prototype);
493
0
  obj->u.c.name = name;
494
0
  obj->u.c.function = cfun;
495
0
  obj->u.c.constructor = NULL;
496
0
  obj->u.c.length = length;
497
0
  obj->u.c.data = data;
498
0
  obj->u.c.finalize = finalize;
499
500
0
  js_endtry(J);
501
502
0
  js_pushobject(J, obj);
503
0
  {
504
0
    js_pushnumber(J, length);
505
0
    js_defproperty(J, -2, "length", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
506
0
    js_newobject(J);
507
0
    {
508
0
      js_copy(J, -2);
509
0
      js_defproperty(J, -2, "constructor", JS_DONTENUM);
510
0
    }
511
0
    js_defproperty(J, -2, "prototype", JS_DONTENUM | JS_DONTCONF);
512
0
  }
513
0
}
514
515
void js_newcfunction(js_State *J, js_CFunction cfun, const char *name, int length)
516
0
{
517
0
  js_newcfunctionx(J, cfun, name, length, NULL, NULL);
518
0
}
519
520
/* prototype -- constructor */
521
void js_newcconstructor(js_State *J, js_CFunction cfun, js_CFunction ccon, const char *name, int length)
522
0
{
523
0
  js_Object *obj = jsV_newobject(J, JS_CCFUNCTION, J->Function_prototype);
524
0
  obj->u.c.name = name;
525
0
  obj->u.c.function = cfun;
526
0
  obj->u.c.constructor = ccon;
527
0
  obj->u.c.length = length;
528
0
  js_pushobject(J, obj); /* proto obj */
529
0
  {
530
0
    js_pushnumber(J, length);
531
0
    js_defproperty(J, -2, "length", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
532
0
    js_rot2(J); /* obj proto */
533
0
    js_copy(J, -2); /* obj proto obj */
534
0
    js_defproperty(J, -2, "constructor", JS_DONTENUM);
535
0
    js_defproperty(J, -2, "prototype", JS_DONTENUM | JS_DONTCONF);
536
0
  }
537
0
}
538
539
void js_newuserdatax(js_State *J, const char *tag, void *data, js_HasProperty has, js_Put put, js_Delete delete, js_Finalize finalize)
540
0
{
541
0
  js_Object *prototype = NULL;
542
0
  js_Object *obj;
543
544
0
  if (js_isobject(J, -1))
545
0
    prototype = js_toobject(J, -1);
546
0
  js_pop(J, 1);
547
548
0
  if (js_try(J)) {
549
0
    if (finalize)
550
0
      finalize(J, data);
551
0
    js_throw(J);
552
0
  }
553
554
0
  obj = jsV_newobject(J, JS_CUSERDATA, prototype);
555
0
  obj->u.user.tag = tag;
556
0
  obj->u.user.data = data;
557
0
  obj->u.user.has = has;
558
0
  obj->u.user.put = put;
559
0
  obj->u.user.delete = delete;
560
0
  obj->u.user.finalize = finalize;
561
562
0
  js_endtry(J);
563
564
0
  js_pushobject(J, obj);
565
0
}
566
567
void js_newuserdata(js_State *J, const char *tag, void *data, js_Finalize finalize)
568
0
{
569
0
  js_newuserdatax(J, tag, data, NULL, NULL, NULL, finalize);
570
0
}
571
572
/* Non-trivial operations on values. These are implemented using the stack. */
573
574
int js_instanceof(js_State *J)
575
0
{
576
0
  js_Object *O, *V;
577
578
0
  if (!js_iscallable(J, -1))
579
0
    js_typeerror(J, "instanceof: invalid operand");
580
581
0
  if (!js_isobject(J, -2))
582
0
    return 0;
583
584
0
  js_getproperty(J, -1, "prototype");
585
0
  if (!js_isobject(J, -1))
586
0
    js_typeerror(J, "instanceof: 'prototype' property is not an object");
587
0
  O = js_toobject(J, -1);
588
0
  js_pop(J, 1);
589
590
0
  V = js_toobject(J, -2);
591
0
  while (V) {
592
0
    V = V->prototype;
593
0
    if (O == V)
594
0
      return 1;
595
0
  }
596
597
0
  return 0;
598
0
}
599
600
void js_concat(js_State *J)
601
0
{
602
0
  js_toprimitive(J, -2, JS_HNONE);
603
0
  js_toprimitive(J, -1, JS_HNONE);
604
605
0
  if (js_isstring(J, -2) || js_isstring(J, -1)) {
606
0
    const char *sa = js_tostring(J, -2);
607
0
    const char *sb = js_tostring(J, -1);
608
0
    char * volatile sab = NULL;
609
    /* TODO: create js_String directly */
610
0
    if (js_try(J)) {
611
0
      js_free(J, sab);
612
0
      js_throw(J);
613
0
    }
614
0
    sab = js_malloc(J, strlen(sa) + strlen(sb) + 1);
615
0
    strcpy(sab, sa);
616
0
    strcat(sab, sb);
617
0
    js_pop(J, 2);
618
0
    js_pushstring(J, sab);
619
0
    js_endtry(J);
620
0
    js_free(J, sab);
621
0
  } else {
622
0
    double x = js_tonumber(J, -2);
623
0
    double y = js_tonumber(J, -1);
624
0
    js_pop(J, 2);
625
0
    js_pushnumber(J, x + y);
626
0
  }
627
0
}
628
629
int js_compare(js_State *J, int *okay)
630
0
{
631
0
  js_toprimitive(J, -2, JS_HNUMBER);
632
0
  js_toprimitive(J, -1, JS_HNUMBER);
633
634
0
  *okay = 1;
635
0
  if (js_isstring(J, -2) && js_isstring(J, -1)) {
636
0
    return strcmp(js_tostring(J, -2), js_tostring(J, -1));
637
0
  } else {
638
0
    double x = js_tonumber(J, -2);
639
0
    double y = js_tonumber(J, -1);
640
0
    if (isnan(x) || isnan(y))
641
0
      *okay = 0;
642
0
    return x < y ? -1 : x > y ? 1 : 0;
643
0
  }
644
0
}
645
646
int js_equal(js_State *J)
647
0
{
648
0
  js_Value *x = js_tovalue(J, -2);
649
0
  js_Value *y = js_tovalue(J, -1);
650
651
0
retry:
652
0
  if (JSV_ISSTRING(x) && JSV_ISSTRING(y))
653
0
    return !strcmp(JSV_TOSTRING(x), JSV_TOSTRING(y));
654
0
  if (x->t.type == y->t.type) {
655
0
    if (x->t.type == JS_TUNDEFINED) return 1;
656
0
    if (x->t.type == JS_TNULL) return 1;
657
0
    if (x->t.type == JS_TNUMBER) return x->u.number == y->u.number;
658
0
    if (x->t.type == JS_TBOOLEAN) return x->u.boolean == y->u.boolean;
659
0
    if (x->t.type == JS_TOBJECT) return x->u.object == y->u.object;
660
0
    return 0;
661
0
  }
662
663
0
  if (x->t.type == JS_TNULL && y->t.type == JS_TUNDEFINED) return 1;
664
0
  if (x->t.type == JS_TUNDEFINED && y->t.type == JS_TNULL) return 1;
665
666
0
  if (x->t.type == JS_TNUMBER && JSV_ISSTRING(y))
667
0
    return x->u.number == jsV_tonumber(J, y);
668
0
  if (JSV_ISSTRING(x) && y->t.type == JS_TNUMBER)
669
0
    return jsV_tonumber(J, x) == y->u.number;
670
671
0
  if (x->t.type == JS_TBOOLEAN) {
672
0
    x->t.type = JS_TNUMBER;
673
0
    x->u.number = x->u.boolean ? 1 : 0;
674
0
    goto retry;
675
0
  }
676
0
  if (y->t.type == JS_TBOOLEAN) {
677
0
    y->t.type = JS_TNUMBER;
678
0
    y->u.number = y->u.boolean ? 1 : 0;
679
0
    goto retry;
680
0
  }
681
0
  if ((JSV_ISSTRING(x) || x->t.type == JS_TNUMBER) && y->t.type == JS_TOBJECT) {
682
0
    jsV_toprimitive(J, y, JS_HNONE);
683
0
    goto retry;
684
0
  }
685
0
  if (x->t.type == JS_TOBJECT && (JSV_ISSTRING(y) || y->t.type == JS_TNUMBER)) {
686
0
    jsV_toprimitive(J, x, JS_HNONE);
687
0
    goto retry;
688
0
  }
689
690
0
  return 0;
691
0
}
692
693
int js_strictequal(js_State *J)
694
0
{
695
0
  js_Value *x = js_tovalue(J, -2);
696
0
  js_Value *y = js_tovalue(J, -1);
697
698
0
  if (JSV_ISSTRING(x) && JSV_ISSTRING(y))
699
0
    return !strcmp(JSV_TOSTRING(x), JSV_TOSTRING(y));
700
701
0
  if (x->t.type != y->t.type) return 0;
702
0
  if (x->t.type == JS_TUNDEFINED) return 1;
703
0
  if (x->t.type == JS_TNULL) return 1;
704
0
  if (x->t.type == JS_TNUMBER) return x->u.number == y->u.number;
705
0
  if (x->t.type == JS_TBOOLEAN) return x->u.boolean == y->u.boolean;
706
0
  if (x->t.type == JS_TOBJECT) return x->u.object == y->u.object;
707
0
  return 0;
708
0
}