forked from ccxvii/mujs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsrun.c
2063 lines (1779 loc) · 43.8 KB
/
jsrun.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "jsi.h"
#include "utf.h"
#include <assert.h>
static void jsR_run(js_State *J, js_Function *F);
/* Push values on stack */
#define STACK (J->stack)
#define TOP (J->top)
#define BOT (J->bot)
static void js_trystackoverflow(js_State *J)
{
STACK[TOP].t.type = JS_TLITSTR;
STACK[TOP].u.litstr = "exception stack overflow";
++TOP;
js_throw(J);
}
static void js_stackoverflow(js_State *J)
{
STACK[TOP].t.type = JS_TLITSTR;
STACK[TOP].u.litstr = "stack overflow";
++TOP;
js_throw(J);
}
static void js_outofmemory(js_State *J)
{
STACK[TOP].t.type = JS_TLITSTR;
STACK[TOP].u.litstr = "out of memory";
++TOP;
js_throw(J);
}
void *js_malloc(js_State *J, int size)
{
void *ptr = J->alloc(J->actx, NULL, size);
if (!ptr)
js_outofmemory(J);
return ptr;
}
void *js_realloc(js_State *J, void *ptr, int size)
{
ptr = J->alloc(J->actx, ptr, size);
if (!ptr)
js_outofmemory(J);
return ptr;
}
char *js_strdup(js_State *J, const char *s)
{
int n = strlen(s) + 1;
char *p = js_malloc(J, n);
memcpy(p, s, n);
return p;
}
void js_free(js_State *J, void *ptr)
{
J->alloc(J->actx, ptr, 0);
}
js_String *jsV_newmemstring(js_State *J, const char *s, int n)
{
js_String *v = js_malloc(J, soffsetof(js_String, p) + n + 1);
memcpy(v->p, s, n);
v->p[n] = 0;
v->gcmark = 0;
v->gcnext = J->gcstr;
J->gcstr = v;
++J->gccounter;
return v;
}
#define CHECKSTACK(n) if (TOP + n >= JS_STACKSIZE) js_stackoverflow(J)
void js_pushvalue(js_State *J, js_Value v)
{
CHECKSTACK(1);
STACK[TOP] = v;
++TOP;
}
void js_pushundefined(js_State *J)
{
CHECKSTACK(1);
STACK[TOP].t.type = JS_TUNDEFINED;
++TOP;
}
void js_pushnull(js_State *J)
{
CHECKSTACK(1);
STACK[TOP].t.type = JS_TNULL;
++TOP;
}
void js_pushboolean(js_State *J, int v)
{
CHECKSTACK(1);
STACK[TOP].t.type = JS_TBOOLEAN;
STACK[TOP].u.boolean = !!v;
++TOP;
}
void js_pushnumber(js_State *J, double v)
{
CHECKSTACK(1);
STACK[TOP].t.type = JS_TNUMBER;
STACK[TOP].u.number = v;
++TOP;
}
void js_pushstring(js_State *J, const char *v)
{
size_t n = strlen(v);
if (n > JS_STRLIMIT)
js_rangeerror(J, "invalid string length");
CHECKSTACK(1);
if (n <= soffsetof(js_Value, t.type)) {
char *s = STACK[TOP].u.shrstr;
while (n--) *s++ = *v++;
*s = 0;
STACK[TOP].t.type = JS_TSHRSTR;
} else {
STACK[TOP].t.type = JS_TMEMSTR;
STACK[TOP].u.memstr = jsV_newmemstring(J, v, n);
}
++TOP;
}
void js_pushlstring(js_State *J, const char *v, int n)
{
if (n > JS_STRLIMIT)
js_rangeerror(J, "invalid string length");
CHECKSTACK(1);
if (n <= soffsetof(js_Value, t.type)) {
char *s = STACK[TOP].u.shrstr;
while (n--) *s++ = *v++;
*s = 0;
STACK[TOP].t.type = JS_TSHRSTR;
} else {
STACK[TOP].t.type = JS_TMEMSTR;
STACK[TOP].u.memstr = jsV_newmemstring(J, v, n);
}
++TOP;
}
void js_pushliteral(js_State *J, const char *v)
{
CHECKSTACK(1);
STACK[TOP].t.type = JS_TLITSTR;
STACK[TOP].u.litstr = v;
++TOP;
}
void js_pushobject(js_State *J, js_Object *v)
{
CHECKSTACK(1);
STACK[TOP].t.type = JS_TOBJECT;
STACK[TOP].u.object = v;
++TOP;
}
void js_pushglobal(js_State *J)
{
js_pushobject(J, J->G);
}
void js_currentfunction(js_State *J)
{
CHECKSTACK(1);
if (BOT > 0)
STACK[TOP] = STACK[BOT-1];
else
STACK[TOP].t.type = JS_TUNDEFINED;
++TOP;
}
void *js_currentfunctiondata(js_State *J)
{
if (BOT > 0)
return STACK[BOT-1].u.object->u.c.data;
return NULL;
}
/* Read values from stack */
static js_Value *stackidx(js_State *J, int idx)
{
static js_Value undefined = { { {0}, JS_TUNDEFINED } };
idx = idx < 0 ? TOP + idx : BOT + idx;
if (idx < 0 || idx >= TOP)
return &undefined;
return STACK + idx;
}
js_Value *js_tovalue(js_State *J, int idx)
{
return stackidx(J, idx);
}
int js_isdefined(js_State *J, int idx) { return stackidx(J, idx)->t.type != JS_TUNDEFINED; }
int js_isundefined(js_State *J, int idx) { return stackidx(J, idx)->t.type == JS_TUNDEFINED; }
int js_isnull(js_State *J, int idx) { return stackidx(J, idx)->t.type == JS_TNULL; }
int js_isboolean(js_State *J, int idx) { return stackidx(J, idx)->t.type == JS_TBOOLEAN; }
int js_isnumber(js_State *J, int idx) { return stackidx(J, idx)->t.type == JS_TNUMBER; }
int js_isstring(js_State *J, int idx) { enum js_Type t = stackidx(J, idx)->t.type; return t == JS_TSHRSTR || t == JS_TLITSTR || t == JS_TMEMSTR; }
int js_isprimitive(js_State *J, int idx) { return stackidx(J, idx)->t.type != JS_TOBJECT; }
int js_isobject(js_State *J, int idx) { return stackidx(J, idx)->t.type == JS_TOBJECT; }
int js_iscoercible(js_State *J, int idx) { js_Value *v = stackidx(J, idx); return v->t.type != JS_TUNDEFINED && v->t.type != JS_TNULL; }
int js_iscallable(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
if (v->t.type == JS_TOBJECT)
return v->u.object->type == JS_CFUNCTION ||
v->u.object->type == JS_CSCRIPT ||
v->u.object->type == JS_CCFUNCTION;
return 0;
}
int js_isarray(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
return v->t.type == JS_TOBJECT && v->u.object->type == JS_CARRAY;
}
int js_isregexp(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
return v->t.type == JS_TOBJECT && v->u.object->type == JS_CREGEXP;
}
int js_isuserdata(js_State *J, int idx, const char *tag)
{
js_Value *v = stackidx(J, idx);
if (v->t.type == JS_TOBJECT && v->u.object->type == JS_CUSERDATA)
return !strcmp(tag, v->u.object->u.user.tag);
return 0;
}
int js_iserror(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
return v->t.type == JS_TOBJECT && v->u.object->type == JS_CERROR;
}
const char *js_typeof(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
switch (v->t.type) {
default:
case JS_TSHRSTR: return "string";
case JS_TUNDEFINED: return "undefined";
case JS_TNULL: return "object";
case JS_TBOOLEAN: return "boolean";
case JS_TNUMBER: return "number";
case JS_TLITSTR: return "string";
case JS_TMEMSTR: return "string";
case JS_TOBJECT:
if (v->u.object->type == JS_CFUNCTION || v->u.object->type == JS_CCFUNCTION)
return "function";
return "object";
}
}
int js_type(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
switch (v->t.type) {
default:
case JS_TSHRSTR: return JS_ISSTRING;
case JS_TUNDEFINED: return JS_ISUNDEFINED;
case JS_TNULL: return JS_ISNULL;
case JS_TBOOLEAN: return JS_ISBOOLEAN;
case JS_TNUMBER: return JS_ISNUMBER;
case JS_TLITSTR: return JS_ISSTRING;
case JS_TMEMSTR: return JS_ISSTRING;
case JS_TOBJECT:
if (v->u.object->type == JS_CFUNCTION || v->u.object->type == JS_CCFUNCTION)
return JS_ISFUNCTION;
return JS_ISOBJECT;
}
}
int js_toboolean(js_State *J, int idx)
{
return jsV_toboolean(J, stackidx(J, idx));
}
double js_tonumber(js_State *J, int idx)
{
return jsV_tonumber(J, stackidx(J, idx));
}
int js_tointeger(js_State *J, int idx)
{
return jsV_numbertointeger(jsV_tonumber(J, stackidx(J, idx)));
}
int js_toint32(js_State *J, int idx)
{
return jsV_numbertoint32(jsV_tonumber(J, stackidx(J, idx)));
}
unsigned int js_touint32(js_State *J, int idx)
{
return jsV_numbertouint32(jsV_tonumber(J, stackidx(J, idx)));
}
short js_toint16(js_State *J, int idx)
{
return jsV_numbertoint16(jsV_tonumber(J, stackidx(J, idx)));
}
unsigned short js_touint16(js_State *J, int idx)
{
return jsV_numbertouint16(jsV_tonumber(J, stackidx(J, idx)));
}
const char *js_tostring(js_State *J, int idx)
{
return jsV_tostring(J, stackidx(J, idx));
}
js_Object *js_toobject(js_State *J, int idx)
{
return jsV_toobject(J, stackidx(J, idx));
}
void js_toprimitive(js_State *J, int idx, int hint)
{
jsV_toprimitive(J, stackidx(J, idx), hint);
}
js_Regexp *js_toregexp(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
if (v->t.type == JS_TOBJECT && v->u.object->type == JS_CREGEXP)
return &v->u.object->u.r;
js_typeerror(J, "not a regexp");
}
void *js_touserdata(js_State *J, int idx, const char *tag)
{
js_Value *v = stackidx(J, idx);
if (v->t.type == JS_TOBJECT && v->u.object->type == JS_CUSERDATA)
if (!strcmp(tag, v->u.object->u.user.tag))
return v->u.object->u.user.data;
js_typeerror(J, "not a %s", tag);
}
static js_Object *jsR_tofunction(js_State *J, int idx)
{
js_Value *v = stackidx(J, idx);
if (v->t.type == JS_TUNDEFINED || v->t.type == JS_TNULL)
return NULL;
if (v->t.type == JS_TOBJECT)
if (v->u.object->type == JS_CFUNCTION || v->u.object->type == JS_CCFUNCTION)
return v->u.object;
js_typeerror(J, "not a function");
}
/* Stack manipulation */
int js_gettop(js_State *J)
{
return TOP - BOT;
}
void js_pop(js_State *J, int n)
{
TOP -= n;
if (TOP < BOT) {
TOP = BOT;
js_error(J, "stack underflow!");
}
}
void js_remove(js_State *J, int idx)
{
idx = idx < 0 ? TOP + idx : BOT + idx;
if (idx < BOT || idx >= TOP)
js_error(J, "stack error!");
for (;idx < TOP - 1; ++idx)
STACK[idx] = STACK[idx+1];
--TOP;
}
void js_insert(js_State *J, int idx)
{
js_error(J, "not implemented yet");
}
void js_replace(js_State* J, int idx)
{
idx = idx < 0 ? TOP + idx : BOT + idx;
if (idx < BOT || idx >= TOP)
js_error(J, "stack error!");
STACK[idx] = STACK[--TOP];
}
void js_copy(js_State *J, int idx)
{
CHECKSTACK(1);
STACK[TOP] = *stackidx(J, idx);
++TOP;
}
void js_dup(js_State *J)
{
CHECKSTACK(1);
STACK[TOP] = STACK[TOP-1];
++TOP;
}
void js_dup2(js_State *J)
{
CHECKSTACK(2);
STACK[TOP] = STACK[TOP-2];
STACK[TOP+1] = STACK[TOP-1];
TOP += 2;
}
void js_rot2(js_State *J)
{
/* A B -> B A */
js_Value tmp = STACK[TOP-1]; /* A B (B) */
STACK[TOP-1] = STACK[TOP-2]; /* A A */
STACK[TOP-2] = tmp; /* B A */
}
void js_rot3(js_State *J)
{
/* A B C -> C A B */
js_Value tmp = STACK[TOP-1]; /* A B C (C) */
STACK[TOP-1] = STACK[TOP-2]; /* A B B */
STACK[TOP-2] = STACK[TOP-3]; /* A A B */
STACK[TOP-3] = tmp; /* C A B */
}
void js_rot4(js_State *J)
{
/* A B C D -> D A B C */
js_Value tmp = STACK[TOP-1]; /* A B C D (D) */
STACK[TOP-1] = STACK[TOP-2]; /* A B C C */
STACK[TOP-2] = STACK[TOP-3]; /* A B B C */
STACK[TOP-3] = STACK[TOP-4]; /* A A B C */
STACK[TOP-4] = tmp; /* D A B C */
}
void js_rot2pop1(js_State *J)
{
/* A B -> B */
STACK[TOP-2] = STACK[TOP-1];
--TOP;
}
void js_rot3pop2(js_State *J)
{
/* A B C -> C */
STACK[TOP-3] = STACK[TOP-1];
TOP -= 2;
}
void js_rot(js_State *J, int n)
{
int i;
js_Value tmp = STACK[TOP-1];
for (i = 1; i < n; ++i)
STACK[TOP-i] = STACK[TOP-i-1];
STACK[TOP-i] = tmp;
}
/* Property access that takes care of attributes and getters/setters */
int js_isarrayindex(js_State *J, const char *p, int *idx)
{
int n = 0;
/* check for empty string */
if (p[0] == 0)
return 0;
/* check for '0' and integers with leading zero */
if (p[0] == '0')
return (p[1] == 0) ? *idx = 0, 1 : 0;
while (*p) {
int c = *p++;
if (c >= '0' && c <= '9') {
if (n >= INT_MAX / 10)
return 0;
n = n * 10 + (c - '0');
} else {
return 0;
}
}
return *idx = n, 1;
}
static void js_pushrune(js_State *J, Rune rune)
{
char buf[UTFmax + 1];
if (rune >= 0) {
buf[runetochar(buf, &rune)] = 0;
js_pushstring(J, buf);
} else {
js_pushundefined(J);
}
}
void jsR_unflattenarray(js_State *J, js_Object *obj) {
if (obj->type == JS_CARRAY && obj->u.a.simple) {
js_Property *ref;
int i;
char name[32];
if (js_try(J)) {
obj->properties = NULL;
js_throw(J);
}
for (i = 0; i < obj->u.a.flat_length; ++i) {
js_itoa(name, i);
ref = jsV_setproperty(J, obj, name);
ref->value = obj->u.a.array[i];
}
js_free(J, obj->u.a.array);
obj->u.a.simple = 0;
obj->u.a.flat_length = 0;
obj->u.a.flat_capacity = 0;
obj->u.a.array = NULL;
js_endtry(J);
}
}
static int jsR_hasproperty(js_State *J, js_Object *obj, const char *name)
{
js_Property *ref;
int k;
if (obj->type == JS_CARRAY) {
if (!strcmp(name, "length")) {
js_pushnumber(J, obj->u.a.length);
return 1;
}
if (obj->u.a.simple) {
if (js_isarrayindex(J, name, &k)) {
if (k >= 0 && k < obj->u.a.flat_length) {
js_pushvalue(J, obj->u.a.array[k]);
return 1;
}
return 0;
}
}
}
else if (obj->type == JS_CSTRING) {
if (!strcmp(name, "length")) {
js_pushnumber(J, obj->u.s.length);
return 1;
}
if (js_isarrayindex(J, name, &k)) {
if (k >= 0 && k < obj->u.s.length) {
js_pushrune(J, js_runeat(J, obj->u.s.string, k));
return 1;
}
}
}
else if (obj->type == JS_CREGEXP) {
if (!strcmp(name, "source")) {
js_pushstring(J, obj->u.r.source);
return 1;
}
if (!strcmp(name, "global")) {
js_pushboolean(J, obj->u.r.flags & JS_REGEXP_G);
return 1;
}
if (!strcmp(name, "ignoreCase")) {
js_pushboolean(J, obj->u.r.flags & JS_REGEXP_I);
return 1;
}
if (!strcmp(name, "multiline")) {
js_pushboolean(J, obj->u.r.flags & JS_REGEXP_M);
return 1;
}
if (!strcmp(name, "lastIndex")) {
js_pushnumber(J, obj->u.r.last);
return 1;
}
}
else if (obj->type == JS_CUSERDATA) {
if (obj->u.user.has && obj->u.user.has(J, obj->u.user.data, name))
return 1;
}
ref = jsV_getproperty(J, obj, name);
if (ref) {
if (ref->getter) {
js_pushobject(J, ref->getter);
js_pushobject(J, obj);
js_call(J, 0);
} else {
js_pushvalue(J, ref->value);
}
return 1;
}
return 0;
}
static void jsR_getproperty(js_State *J, js_Object *obj, const char *name)
{
if (!jsR_hasproperty(J, obj, name))
js_pushundefined(J);
}
static int jsR_hasindex(js_State *J, js_Object *obj, int k)
{
char buf[32];
if (obj->type == JS_CARRAY && obj->u.a.simple) {
if (k >= 0 && k < obj->u.a.flat_length) {
js_pushvalue(J, obj->u.a.array[k]);
return 1;
}
return 0;
}
return jsR_hasproperty(J, obj, js_itoa(buf, k));
}
static void jsR_getindex(js_State *J, js_Object *obj, int k)
{
if (!jsR_hasindex(J, obj, k))
js_pushundefined(J);
}
static void jsR_setarrayindex(js_State *J, js_Object *obj, int k, js_Value *value)
{
int newlen = k + 1;
assert(obj->u.a.simple);
assert(k >= 0);
if (newlen > JS_ARRAYLIMIT)
js_rangeerror(J, "array too large");
if (newlen > obj->u.a.flat_length) {
assert(newlen == obj->u.a.flat_length + 1);
if (newlen > obj->u.a.flat_capacity) {
int newcap = obj->u.a.flat_capacity;
if (newcap == 0)
newcap = 8;
while (newcap < newlen)
newcap <<= 1;
obj->u.a.array = js_realloc(J, obj->u.a.array, newcap * sizeof(js_Value));
obj->u.a.flat_capacity = newcap;
}
obj->u.a.flat_length = newlen;
}
if (newlen > obj->u.a.length)
obj->u.a.length = newlen;
obj->u.a.array[k] = *value;
}
static void jsR_setproperty(js_State *J, js_Object *obj, const char *name, int transient)
{
js_Value *value = stackidx(J, -1);
js_Property *ref;
int k;
int own;
if (obj->type == JS_CARRAY) {
if (!strcmp(name, "length")) {
double rawlen = jsV_tonumber(J, value);
int newlen = jsV_numbertointeger(rawlen);
if (newlen != rawlen || newlen < 0)
js_rangeerror(J, "invalid array length");
if (newlen > JS_ARRAYLIMIT)
js_rangeerror(J, "array too large");
if (obj->u.a.simple) {
obj->u.a.length = newlen;
if (newlen <= obj->u.a.flat_length)
obj->u.a.flat_length = newlen;
} else {
jsV_resizearray(J, obj, newlen);
}
return;
}
if (js_isarrayindex(J, name, &k)) {
if (obj->u.a.simple) {
if (k >= 0 && k <= obj->u.a.flat_length) {
jsR_setarrayindex(J, obj, k, value);
} else {
jsR_unflattenarray(J, obj);
if (obj->u.a.length < k + 1)
obj->u.a.length = k + 1;
}
} else {
if (obj->u.a.length < k + 1)
obj->u.a.length = k + 1;
}
}
}
else if (obj->type == JS_CSTRING) {
if (!strcmp(name, "length"))
goto readonly;
if (js_isarrayindex(J, name, &k))
if (k >= 0 && k < obj->u.s.length)
goto readonly;
}
else if (obj->type == JS_CREGEXP) {
if (!strcmp(name, "source")) goto readonly;
if (!strcmp(name, "global")) goto readonly;
if (!strcmp(name, "ignoreCase")) goto readonly;
if (!strcmp(name, "multiline")) goto readonly;
if (!strcmp(name, "lastIndex")) {
obj->u.r.last = jsV_tointeger(J, value);
return;
}
}
else if (obj->type == JS_CUSERDATA) {
if (obj->u.user.put && obj->u.user.put(J, obj->u.user.data, name))
return;
}
/* First try to find a setter in prototype chain */
ref = jsV_getpropertyx(J, obj, name, &own);
if (ref) {
if (ref->setter) {
js_pushobject(J, ref->setter);
js_pushobject(J, obj);
js_pushvalue(J, *value);
js_call(J, 1);
js_pop(J, 1);
return;
} else {
if (J->strict)
if (ref->getter)
js_typeerror(J, "setting property '%s' that only has a getter", name);
if (ref->atts & JS_READONLY)
goto readonly;
}
}
/* Property not found on this object, so create one */
if (!ref || !own) {
if (transient) {
if (J->strict)
js_typeerror(J, "cannot create property '%s' on transient object", name);
return;
}
ref = jsV_setproperty(J, obj, name);
}
if (ref) {
if (!(ref->atts & JS_READONLY))
ref->value = *value;
else
goto readonly;
}
return;
readonly:
if (J->strict)
js_typeerror(J, "'%s' is read-only", name);
}
static void jsR_setindex(js_State *J, js_Object *obj, int k, int transient)
{
char buf[32];
if (obj->type == JS_CARRAY && obj->u.a.simple && k >= 0 && k <= obj->u.a.flat_length) {
jsR_setarrayindex(J, obj, k, stackidx(J, -1));
} else {
jsR_setproperty(J, obj, js_itoa(buf, k), transient);
}
}
static void jsR_defproperty(js_State *J, js_Object *obj, const char *name,
int atts, js_Value *value, js_Object *getter, js_Object *setter,
int throw)
{
js_Property *ref;
int k;
if (obj->type == JS_CARRAY) {
if (!strcmp(name, "length"))
goto readonly;
if (obj->u.a.simple)
jsR_unflattenarray(J, obj);
}
else if (obj->type == JS_CSTRING) {
if (!strcmp(name, "length"))
goto readonly;
if (js_isarrayindex(J, name, &k))
if (k >= 0 && k < obj->u.s.length)
goto readonly;
}
else if (obj->type == JS_CREGEXP) {
if (!strcmp(name, "source")) goto readonly;
if (!strcmp(name, "global")) goto readonly;
if (!strcmp(name, "ignoreCase")) goto readonly;
if (!strcmp(name, "multiline")) goto readonly;
if (!strcmp(name, "lastIndex")) goto readonly;
}
else if (obj->type == JS_CUSERDATA) {
if (obj->u.user.put && obj->u.user.put(J, obj->u.user.data, name))
return;
}
ref = jsV_setproperty(J, obj, name);
if (ref) {
if (value) {
if (!(ref->atts & JS_READONLY))
ref->value = *value;
else if (J->strict)
js_typeerror(J, "'%s' is read-only", name);
}
if (getter) {
if (!(ref->atts & JS_DONTCONF))
ref->getter = getter;
else if (J->strict)
js_typeerror(J, "'%s' is non-configurable", name);
}
if (setter) {
if (!(ref->atts & JS_DONTCONF))
ref->setter = setter;
else if (J->strict)
js_typeerror(J, "'%s' is non-configurable", name);
}
ref->atts |= atts;
}
return;
readonly:
if (J->strict || throw)
js_typeerror(J, "'%s' is read-only or non-configurable", name);
}
static int jsR_delproperty(js_State *J, js_Object *obj, const char *name)
{
js_Property *ref;
int k;
if (obj->type == JS_CARRAY) {
if (!strcmp(name, "length"))
goto dontconf;
if (obj->u.a.simple)
jsR_unflattenarray(J, obj);
}
else if (obj->type == JS_CSTRING) {
if (!strcmp(name, "length"))
goto dontconf;
if (js_isarrayindex(J, name, &k))
if (k >= 0 && k < obj->u.s.length)
goto dontconf;
}
else if (obj->type == JS_CREGEXP) {
if (!strcmp(name, "source")) goto dontconf;
if (!strcmp(name, "global")) goto dontconf;
if (!strcmp(name, "ignoreCase")) goto dontconf;
if (!strcmp(name, "multiline")) goto dontconf;
if (!strcmp(name, "lastIndex")) goto dontconf;
}
else if (obj->type == JS_CUSERDATA) {
if (obj->u.user.delete && obj->u.user.delete(J, obj->u.user.data, name))
return 1;
}
ref = jsV_getownproperty(J, obj, name);
if (ref) {
if (ref->atts & JS_DONTCONF)
goto dontconf;
jsV_delproperty(J, obj, name);
}
return 1;
dontconf:
if (J->strict)
js_typeerror(J, "'%s' is non-configurable", name);
return 0;
}
static void jsR_delindex(js_State *J, js_Object *obj, int k)
{
char buf[32];
/* Allow deleting last element of a simple array without unflattening */
if (obj->type == JS_CARRAY && obj->u.a.simple && k == obj->u.a.flat_length - 1)
obj->u.a.flat_length = k;
else
jsR_delproperty(J, obj, js_itoa(buf, k));
}
/* Registry, global and object property accessors */
const char *js_ref(js_State *J)
{
js_Value *v = stackidx(J, -1);
const char *s;
char buf[32];
switch (v->t.type) {
case JS_TUNDEFINED: s = "_Undefined"; break;
case JS_TNULL: s = "_Null"; break;
case JS_TBOOLEAN:
s = v->u.boolean ? "_True" : "_False";
break;
case JS_TOBJECT:
sprintf(buf, "%p", (void*)v->u.object);
s = js_intern(J, buf);
break;
default:
sprintf(buf, "%d", J->nextref++);
s = js_intern(J, buf);
break;
}
js_setregistry(J, s);
return s;
}
void js_unref(js_State *J, const char *ref)
{
js_delregistry(J, ref);
}
void js_getregistry(js_State *J, const char *name)
{
jsR_getproperty(J, J->R, name);
}
void js_setregistry(js_State *J, const char *name)
{
jsR_setproperty(J, J->R, name, 0);
js_pop(J, 1);
}
void js_delregistry(js_State *J, const char *name)
{
jsR_delproperty(J, J->R, name);
}
void js_getglobal(js_State *J, const char *name)
{
jsR_getproperty(J, J->G, name);
}
void js_setglobal(js_State *J, const char *name)
{
jsR_setproperty(J, J->G, name, 0);
js_pop(J, 1);
}
void js_defglobal(js_State *J, const char *name, int atts)
{
jsR_defproperty(J, J->G, name, atts, stackidx(J, -1), NULL, NULL, 0);
js_pop(J, 1);
}
void js_delglobal(js_State *J, const char *name)
{
jsR_delproperty(J, J->G, name);
}
void js_getproperty(js_State *J, int idx, const char *name)
{
jsR_getproperty(J, js_toobject(J, idx), name);
}
void js_setproperty(js_State *J, int idx, const char *name)
{
jsR_setproperty(J, js_toobject(J, idx), name, !js_isobject(J, idx));
js_pop(J, 1);
}
void js_defproperty(js_State *J, int idx, const char *name, int atts)
{
jsR_defproperty(J, js_toobject(J, idx), name, atts, stackidx(J, -1), NULL, NULL, 1);
js_pop(J, 1);
}
void js_delproperty(js_State *J, int idx, const char *name)
{
jsR_delproperty(J, js_toobject(J, idx), name);
}
void js_defaccessor(js_State *J, int idx, const char *name, int atts)
{