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