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