-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathobj.c
3075 lines (2878 loc) · 93.9 KB
/
obj.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 "obj.h"
#include "class.h"
#include "conv.h"
#include "error.h"
#include "func.h"
#include "gc.h"
#include "intrins/intrins.h"
#include "num.h"
#include "ops.h"
#include "shape.h"
#include "vm.h"
/* -- Prototype ----------------------------------- */
/* return -1 (exception) or TRUE/FALSE */
int JS_SetPrototypeInternal(JSContext *ctx, JSValueConst obj,
JSValueConst proto_val, BOOL throw_flag) {
JSObject *proto, *p, *p1;
JSShape *sh;
if (throw_flag) {
if (JS_VALUE_GET_TAG(obj) == JS_TAG_NULL ||
JS_VALUE_GET_TAG(obj) == JS_TAG_UNDEFINED)
goto not_obj;
} else {
if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
goto not_obj;
}
p = JS_VALUE_GET_OBJ(obj);
if (JS_VALUE_GET_TAG(proto_val) != JS_TAG_OBJECT) {
if (JS_VALUE_GET_TAG(proto_val) != JS_TAG_NULL) {
not_obj:
JS_ThrowTypeErrorNotAnObject(ctx);
return -1;
}
proto = NULL;
} else {
proto = JS_VALUE_GET_OBJ(proto_val);
}
if (throw_flag && JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
return TRUE;
if (unlikely(p->class_id == JS_CLASS_PROXY))
return js_proxy_setPrototypeOf(ctx, obj, proto_val, throw_flag);
sh = p->shape;
if (sh->proto == proto)
return TRUE;
if (!p->extensible) {
if (throw_flag) {
JS_ThrowTypeError(ctx, "object is not extensible");
return -1;
} else {
return FALSE;
}
}
if (proto) {
/* check if there is a cycle */
p1 = proto;
do {
if (p1 == p) {
if (throw_flag) {
JS_ThrowTypeError(ctx, "circular prototype chain");
return -1;
} else {
return FALSE;
}
}
/* Note: for Proxy objects, proto is NULL */
p1 = p1->shape->proto;
} while (p1 != NULL);
JS_DupValue(ctx, proto_val);
}
if (js_shape_prepare_update(ctx, p, NULL))
return -1;
sh = p->shape;
if (sh->proto)
JS_FreeValue(ctx, JS_MKPTR(JS_TAG_OBJECT, sh->proto));
sh->proto = proto;
return TRUE;
}
/* return -1 (exception) or TRUE/FALSE */
int JS_SetPrototype(JSContext *ctx, JSValueConst obj, JSValueConst proto_val) {
return JS_SetPrototypeInternal(ctx, obj, proto_val, TRUE);
}
/* Only works for primitive types, otherwise return JS_NULL. */
JSValueConst JS_GetPrototypePrimitive(JSContext *ctx, JSValueConst val) {
switch (JS_VALUE_GET_NORM_TAG(val)) {
#ifdef CONFIG_BIGNUM
case JS_TAG_BIG_INT:
val = ctx->class_proto[JS_CLASS_BIG_INT];
break;
case JS_TAG_BIG_FLOAT:
val = ctx->class_proto[JS_CLASS_BIG_FLOAT];
break;
case JS_TAG_BIG_DECIMAL:
val = ctx->class_proto[JS_CLASS_BIG_DECIMAL];
break;
#endif
case JS_TAG_INT:
case JS_TAG_FLOAT64:
val = ctx->class_proto[JS_CLASS_NUMBER];
break;
case JS_TAG_BOOL:
val = ctx->class_proto[JS_CLASS_BOOLEAN];
break;
case JS_TAG_STRING:
val = ctx->class_proto[JS_CLASS_STRING];
break;
case JS_TAG_SYMBOL:
val = ctx->class_proto[JS_CLASS_SYMBOL];
break;
case JS_TAG_OBJECT:
case JS_TAG_NULL:
case JS_TAG_UNDEFINED:
default:
val = JS_NULL;
break;
}
return val;
}
/* Return an Object, JS_NULL or JS_EXCEPTION in case of Proxy object. */
JSValue JS_GetPrototype(JSContext *ctx, JSValueConst obj) {
JSValue val;
if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
JSObject *p;
p = JS_VALUE_GET_OBJ(obj);
if (unlikely(p->class_id == JS_CLASS_PROXY)) {
val = js_proxy_getPrototypeOf(ctx, obj);
} else {
p = p->shape->proto;
if (!p)
val = JS_NULL;
else
val = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
}
} else {
val = JS_DupValue(ctx, JS_GetPrototypePrimitive(ctx, obj));
}
return val;
}
JSValue JS_GetPrototypeFree(JSContext *ctx, JSValue obj) {
JSValue obj1;
obj1 = JS_GetPrototype(ctx, obj);
JS_FreeValue(ctx, obj);
return obj1;
}
/* return TRUE, FALSE or (-1) in case of exception */
int JS_OrdinaryIsInstanceOf(JSContext *ctx, JSValueConst val,
JSValueConst obj) {
JSValue obj_proto;
JSObject *proto;
const JSObject *p, *proto1;
BOOL ret;
if (!JS_IsFunction(ctx, obj))
return FALSE;
p = JS_VALUE_GET_OBJ(obj);
if (p->class_id == JS_CLASS_BOUND_FUNCTION) {
JSBoundFunction *s = p->u.bound_function;
return JS_IsInstanceOf(ctx, val, s->func_obj);
}
/* Only explicitly boxed values are instances of constructors */
if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT)
return FALSE;
obj_proto = JS_GetProperty(ctx, obj, JS_ATOM_prototype);
if (JS_VALUE_GET_TAG(obj_proto) != JS_TAG_OBJECT) {
if (!JS_IsException(obj_proto))
JS_ThrowTypeError(ctx, "operand 'prototype' property is not an object");
ret = -1;
goto done;
}
proto = JS_VALUE_GET_OBJ(obj_proto);
p = JS_VALUE_GET_OBJ(val);
for (;;) {
proto1 = p->shape->proto;
if (!proto1) {
/* slow case if proxy in the prototype chain */
if (unlikely(p->class_id == JS_CLASS_PROXY)) {
JSValue obj1;
obj1 = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, (JSObject *)p));
for (;;) {
obj1 = JS_GetPrototypeFree(ctx, obj1);
if (JS_IsException(obj1)) {
ret = -1;
break;
}
if (JS_IsNull(obj1)) {
ret = FALSE;
break;
}
if (proto == JS_VALUE_GET_OBJ(obj1)) {
JS_FreeValue(ctx, obj1);
ret = TRUE;
break;
}
/* must check for timeout to avoid infinite loop */
if (js_poll_interrupts(ctx)) {
JS_FreeValue(ctx, obj1);
ret = -1;
break;
}
}
} else {
ret = FALSE;
}
break;
}
p = proto1;
if (proto == p) {
ret = TRUE;
break;
}
}
done:
JS_FreeValue(ctx, obj_proto);
return ret;
}
/* return TRUE, FALSE or (-1) in case of exception */
int JS_IsInstanceOf(JSContext *ctx, JSValueConst val, JSValueConst obj) {
JSValue method;
if (!JS_IsObject(obj))
goto fail;
method = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_hasInstance);
if (JS_IsException(method))
return -1;
if (!JS_IsNull(method) && !JS_IsUndefined(method)) {
JSValue ret;
ret = JS_CallFree(ctx, method, obj, 1, &val);
return JS_ToBoolFree(ctx, ret);
}
/* legacy case */
if (!JS_IsFunction(ctx, obj)) {
fail:
JS_ThrowTypeError(ctx, "invalid 'instanceof' right operand");
return -1;
}
return JS_OrdinaryIsInstanceOf(ctx, val, obj);
}
JSValue js_instantiate_prototype(JSContext *ctx, JSObject *p, JSAtom atom,
void *opaque) {
JSValue obj, this_val;
int ret;
this_val = JS_MKPTR(JS_TAG_OBJECT, p);
obj = JS_NewObject(ctx);
if (JS_IsException(obj))
return JS_EXCEPTION;
set_cycle_flag(ctx, obj);
set_cycle_flag(ctx, this_val);
ret = JS_DefinePropertyValue(ctx, obj, JS_ATOM_constructor,
JS_DupValue(ctx, this_val),
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
if (ret < 0) {
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
return obj;
}
/* -- Property ----------------------------------- */
JSContext *js_autoinit_get_realm(JSProperty *pr) {
return (JSContext *)(pr->u.init.realm_and_id & ~3);
}
JSAutoInitIDEnum js_autoinit_get_id(JSProperty *pr) {
return pr->u.init.realm_and_id & 3;
}
static void js_autoinit_free(JSRuntime *rt, JSProperty *pr) {
JS_FreeContext(js_autoinit_get_realm(pr));
}
void free_property(JSRuntime *rt, JSProperty *pr, int prop_flags) {
if (unlikely(prop_flags & JS_PROP_TMASK)) {
if ((prop_flags & JS_PROP_TMASK) == JS_PROP_GETSET) {
if (pr->u.getset.getter)
JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.getter));
if (pr->u.getset.setter)
JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.setter));
} else if ((prop_flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
free_var_ref(rt, pr->u.var_ref);
} else if ((prop_flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT) {
js_autoinit_free(rt, pr);
}
} else {
JS_FreeValueRT(rt, pr->u.value);
}
}
/* return the value associated to the autoinit property or an exception */
typedef JSValue JSAutoInitFunc(JSContext *ctx, JSObject *p, JSAtom atom,
void *opaque);
static JSAutoInitFunc *js_autoinit_func_table[] = {
js_instantiate_prototype, /* JS_AUTOINIT_ID_PROTOTYPE */
js_module_ns_autoinit, /* JS_AUTOINIT_ID_MODULE_NS */
JS_InstantiateFunctionListItem2, /* JS_AUTOINIT_ID_PROP */
};
/* warning: 'prs' is reallocated after it */
int JS_AutoInitProperty(JSContext *ctx, JSObject *p, JSAtom prop,
JSProperty *pr, JSShapeProperty *prs) {
JSValue val;
JSContext *realm;
JSAutoInitFunc *func;
if (js_shape_prepare_update(ctx, p, &prs))
return -1;
realm = js_autoinit_get_realm(pr);
func = js_autoinit_func_table[js_autoinit_get_id(pr)];
/* 'func' shall not modify the object properties 'pr' */
val = func(realm, p, prop, pr->u.init.opaque);
js_autoinit_free(ctx->rt, pr);
prs->flags &= ~JS_PROP_TMASK;
pr->u.value = JS_UNDEFINED;
if (JS_IsException(val))
return -1;
pr->u.value = val;
return 0;
}
/* return -1 if exception (proxy case) or TRUE/FALSE */
int JS_IsArray(JSContext *ctx, JSValueConst val) {
JSObject *p;
if (JS_VALUE_GET_TAG(val) == JS_TAG_OBJECT) {
p = JS_VALUE_GET_OBJ(val);
if (unlikely(p->class_id == JS_CLASS_PROXY))
return js_proxy_isArray(ctx, val);
else
return p->class_id == JS_CLASS_ARRAY;
} else {
return FALSE;
}
}
JSValue JS_GetPropertyInternal(JSContext *ctx, JSValueConst obj, JSAtom prop,
JSValueConst this_obj, BOOL throw_ref_error) {
JSObject *p;
JSProperty *pr;
JSShapeProperty *prs;
uint32_t tag;
tag = JS_VALUE_GET_TAG(obj);
if (unlikely(tag != JS_TAG_OBJECT)) {
switch (tag) {
case JS_TAG_NULL:
return JS_ThrowTypeErrorAtom(ctx, "cannot read property '%s' of null",
prop);
case JS_TAG_UNDEFINED:
return JS_ThrowTypeErrorAtom(
ctx, "cannot read property '%s' of undefined", prop);
case JS_TAG_EXCEPTION:
return JS_EXCEPTION;
case JS_TAG_STRING: {
JSString *p1 = JS_VALUE_GET_STRING(obj);
if (__JS_AtomIsTaggedInt(prop)) {
uint32_t idx, ch;
idx = __JS_AtomToUInt32(prop);
if (idx < p1->len) {
if (p1->is_wide_char)
ch = p1->u.str16[idx];
else
ch = p1->u.str8[idx];
return js_new_string_char(ctx, ch);
}
} else if (prop == JS_ATOM_length) {
return JS_NewInt32(ctx, p1->len);
}
} break;
default:
break;
}
/* cannot raise an exception */
p = JS_VALUE_GET_OBJ(JS_GetPrototypePrimitive(ctx, obj));
if (!p)
return JS_UNDEFINED;
} else {
p = JS_VALUE_GET_OBJ(obj);
}
for (;;) {
prs = find_own_property(&pr, p, prop);
if (prs) {
/* found */
if (unlikely(prs->flags & JS_PROP_TMASK)) {
if ((prs->flags & JS_PROP_TMASK) == JS_PROP_GETSET) {
if (unlikely(!pr->u.getset.getter)) {
return JS_UNDEFINED;
} else {
JSValue func = JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.getter);
/* Note: the field could be removed in the getter */
func = JS_DupValue(ctx, func);
return JS_CallFree(ctx, func, this_obj, 0, NULL);
}
} else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
JSValue val = *pr->u.var_ref->pvalue;
if (unlikely(JS_IsUninitialized(val)))
return JS_ThrowReferenceErrorUninitialized(ctx, prs->atom);
return JS_DupValue(ctx, val);
} else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT) {
/* Instantiate property and retry */
if (JS_AutoInitProperty(ctx, p, prop, pr, prs))
return JS_EXCEPTION;
continue;
}
} else {
return JS_DupValue(ctx, pr->u.value);
}
}
if (unlikely(p->is_exotic)) {
/* exotic behaviors */
if (p->fast_array) {
if (__JS_AtomIsTaggedInt(prop)) {
uint32_t idx = __JS_AtomToUInt32(prop);
if (idx < p->u.array.count) {
/* we avoid duplicating the code */
return JS_GetPropertyUint32(ctx, JS_MKPTR(JS_TAG_OBJECT, p), idx);
} else if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
return JS_UNDEFINED;
}
} else if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
int ret;
ret = JS_AtomIsNumericIndex(ctx, prop);
if (ret != 0) {
if (ret < 0)
return JS_EXCEPTION;
return JS_UNDEFINED;
}
}
} else {
const JSClassExoticMethods *em =
ctx->rt->class_array[p->class_id].exotic;
if (em) {
if (em->get_property) {
JSValue obj1, retval;
/* XXX: should pass throw_ref_error */
/* Note: if 'p' is a prototype, it can be
freed in the called function */
obj1 = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
retval = em->get_property(ctx, obj1, prop, this_obj);
JS_FreeValue(ctx, obj1);
return retval;
}
if (em->get_own_property) {
JSPropertyDescriptor desc;
int ret;
JSValue obj1;
/* Note: if 'p' is a prototype, it can be
freed in the called function */
obj1 = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
ret = em->get_own_property(ctx, &desc, obj1, prop);
JS_FreeValue(ctx, obj1);
if (ret < 0)
return JS_EXCEPTION;
if (ret) {
if (desc.flags & JS_PROP_GETSET) {
JS_FreeValue(ctx, desc.setter);
return JS_CallFree(ctx, desc.getter, this_obj, 0, NULL);
} else {
return desc.value;
}
}
}
}
}
}
p = p->shape->proto;
if (!p)
break;
}
if (unlikely(throw_ref_error)) {
return JS_ThrowReferenceErrorNotDefined(ctx, prop);
} else {
return JS_UNDEFINED;
}
}
static JSValue JS_ThrowTypeErrorPrivateNotFound(JSContext *ctx, JSAtom atom) {
return JS_ThrowTypeErrorAtom(ctx, "private class field '%s' does not exist",
atom);
}
/* Private fields can be added even on non extensible objects or
Proxies */
int JS_DefinePrivateField(JSContext *ctx, JSValueConst obj, JSValueConst name,
JSValue val) {
JSObject *p;
JSShapeProperty *prs;
JSProperty *pr;
JSAtom prop;
if (unlikely(JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)) {
JS_ThrowTypeErrorNotAnObject(ctx);
goto fail;
}
/* safety check */
if (unlikely(JS_VALUE_GET_TAG(name) != JS_TAG_SYMBOL)) {
JS_ThrowTypeErrorNotASymbol(ctx);
goto fail;
}
prop = js_symbol_to_atom(ctx, (JSValue)name);
p = JS_VALUE_GET_OBJ(obj);
prs = find_own_property(&pr, p, prop);
if (prs) {
JS_ThrowTypeErrorAtom(ctx, "private class field '%s' already exists", prop);
goto fail;
}
pr = add_property(ctx, p, prop, JS_PROP_C_W_E);
if (unlikely(!pr)) {
fail:
JS_FreeValue(ctx, val);
return -1;
}
pr->u.value = val;
return 0;
}
JSValue JS_GetPrivateField(JSContext *ctx, JSValueConst obj,
JSValueConst name) {
JSObject *p;
JSShapeProperty *prs;
JSProperty *pr;
JSAtom prop;
if (unlikely(JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT))
return JS_ThrowTypeErrorNotAnObject(ctx);
/* safety check */
if (unlikely(JS_VALUE_GET_TAG(name) != JS_TAG_SYMBOL))
return JS_ThrowTypeErrorNotASymbol(ctx);
prop = js_symbol_to_atom(ctx, (JSValue)name);
p = JS_VALUE_GET_OBJ(obj);
prs = find_own_property(&pr, p, prop);
if (!prs) {
JS_ThrowTypeErrorPrivateNotFound(ctx, prop);
return JS_EXCEPTION;
}
return JS_DupValue(ctx, pr->u.value);
}
int JS_SetPrivateField(JSContext *ctx, JSValueConst obj, JSValueConst name,
JSValue val) {
JSObject *p;
JSShapeProperty *prs;
JSProperty *pr;
JSAtom prop;
if (unlikely(JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)) {
JS_ThrowTypeErrorNotAnObject(ctx);
goto fail;
}
/* safety check */
if (unlikely(JS_VALUE_GET_TAG(name) != JS_TAG_SYMBOL)) {
JS_ThrowTypeErrorNotASymbol(ctx);
goto fail;
}
prop = js_symbol_to_atom(ctx, (JSValue)name);
p = JS_VALUE_GET_OBJ(obj);
prs = find_own_property(&pr, p, prop);
if (!prs) {
JS_ThrowTypeErrorPrivateNotFound(ctx, prop);
fail:
JS_FreeValue(ctx, val);
return -1;
}
set_value(ctx, &pr->u.value, val);
return 0;
}
int JS_AddBrand(JSContext *ctx, JSValueConst obj, JSValueConst home_obj) {
JSObject *p, *p1;
JSShapeProperty *prs;
JSProperty *pr;
JSValue brand;
JSAtom brand_atom;
if (unlikely(JS_VALUE_GET_TAG(home_obj) != JS_TAG_OBJECT)) {
JS_ThrowTypeErrorNotAnObject(ctx);
return -1;
}
p = JS_VALUE_GET_OBJ(home_obj);
prs = find_own_property(&pr, p, JS_ATOM_Private_brand);
if (!prs) {
brand = JS_NewSymbolFromAtom(ctx, JS_ATOM_brand, JS_ATOM_TYPE_PRIVATE);
if (JS_IsException(brand))
return -1;
/* if the brand is not present, add it */
pr = add_property(ctx, p, JS_ATOM_Private_brand, JS_PROP_C_W_E);
if (!pr) {
JS_FreeValue(ctx, brand);
return -1;
}
pr->u.value = JS_DupValue(ctx, brand);
} else {
brand = JS_DupValue(ctx, pr->u.value);
}
brand_atom = js_symbol_to_atom(ctx, brand);
if (unlikely(JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)) {
JS_ThrowTypeErrorNotAnObject(ctx);
JS_FreeAtom(ctx, brand_atom);
return -1;
}
p1 = JS_VALUE_GET_OBJ(obj);
pr = add_property(ctx, p1, brand_atom, JS_PROP_C_W_E);
JS_FreeAtom(ctx, brand_atom);
if (!pr)
return -1;
pr->u.value = JS_UNDEFINED;
return 0;
}
int JS_CheckBrand(JSContext *ctx, JSValueConst obj, JSValueConst func) {
JSObject *p, *p1, *home_obj;
JSShapeProperty *prs;
JSProperty *pr;
JSValueConst brand;
/* get the home object of 'func' */
if (unlikely(JS_VALUE_GET_TAG(func) != JS_TAG_OBJECT)) {
not_obj:
JS_ThrowTypeErrorNotAnObject(ctx);
return -1;
}
p1 = JS_VALUE_GET_OBJ(func);
if (!js_class_has_bytecode(p1->class_id))
goto not_obj;
home_obj = p1->u.func.home_object;
if (!home_obj)
goto not_obj;
prs = find_own_property(&pr, home_obj, JS_ATOM_Private_brand);
if (!prs) {
JS_ThrowTypeError(ctx, "expecting <brand> private field");
return -1;
}
brand = pr->u.value;
/* safety check */
if (unlikely(JS_VALUE_GET_TAG(brand) != JS_TAG_SYMBOL))
goto not_obj;
/* get the brand array of 'obj' */
if (unlikely(JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT))
goto not_obj;
p = JS_VALUE_GET_OBJ(obj);
prs = find_own_property(&pr, p, js_symbol_to_atom(ctx, (JSValue)brand));
if (!prs) {
JS_ThrowTypeError(ctx, "invalid brand on object");
return -1;
}
return 0;
}
static int num_keys_cmp(const void *p1, const void *p2, void *opaque) {
JSContext *ctx = opaque;
JSAtom atom1 = ((const JSPropertyEnum *)p1)->atom;
JSAtom atom2 = ((const JSPropertyEnum *)p2)->atom;
uint32_t v1, v2;
#ifndef NDEBUG
BOOL atom1_is_integer, atom2_is_integer;
atom1_is_integer = JS_AtomIsArrayIndex(ctx, &v1, atom1);
atom2_is_integer = JS_AtomIsArrayIndex(ctx, &v2, atom2);
#else
JS_AtomIsArrayIndex(ctx, &v1, atom1);
JS_AtomIsArrayIndex(ctx, &v2, atom2);
#endif
assert(atom1_is_integer && atom2_is_integer);
if (v1 < v2)
return -1;
else if (v1 == v2)
return 0;
else
return 1;
}
void js_free_prop_enum(JSContext *ctx, JSPropertyEnum *tab, uint32_t len) {
uint32_t i;
if (tab) {
for (i = 0; i < len; i++)
JS_FreeAtom(ctx, tab[i].atom);
js_free(ctx, tab);
}
}
/* return < 0 in case if exception, 0 if OK. ptab and its atoms must
be freed by the user. */
int __exception JS_GetOwnPropertyNamesInternal(JSContext *ctx,
JSPropertyEnum **ptab,
uint32_t *plen, JSObject *p,
int flags) {
int i, j;
JSShape *sh;
JSShapeProperty *prs;
JSPropertyEnum *tab_atom, *tab_exotic;
JSAtom atom;
uint32_t num_keys_count, str_keys_count, sym_keys_count, atom_count;
uint32_t num_index, str_index, sym_index, exotic_count, exotic_keys_count;
BOOL is_enumerable, num_sorted;
uint32_t num_key;
JSAtomKindEnum kind;
/* clear pointer for consistency in case of failure */
*ptab = NULL;
*plen = 0;
/* compute the number of returned properties */
num_keys_count = 0;
str_keys_count = 0;
sym_keys_count = 0;
exotic_keys_count = 0;
exotic_count = 0;
tab_exotic = NULL;
sh = p->shape;
for (i = 0, prs = get_shape_prop(sh); i < sh->prop_count; i++, prs++) {
atom = prs->atom;
if (atom != JS_ATOM_NULL) {
is_enumerable = ((prs->flags & JS_PROP_ENUMERABLE) != 0);
kind = JS_AtomGetKind(ctx, atom);
if ((!(flags & JS_GPN_ENUM_ONLY) || is_enumerable) &&
((flags >> kind) & 1) != 0) {
/* need to raise an exception in case of the module
name space (implicit GetOwnProperty) */
if (unlikely((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) &&
(flags & (JS_GPN_SET_ENUM | JS_GPN_ENUM_ONLY))) {
JSVarRef *var_ref = p->prop[i].u.var_ref;
if (unlikely(JS_IsUninitialized(*var_ref->pvalue))) {
JS_ThrowReferenceErrorUninitialized(ctx, prs->atom);
return -1;
}
}
if (JS_AtomIsArrayIndex(ctx, &num_key, atom)) {
num_keys_count++;
} else if (kind == JS_ATOM_KIND_STRING) {
str_keys_count++;
} else {
sym_keys_count++;
}
}
}
}
if (p->is_exotic) {
if (p->fast_array) {
if (flags & JS_GPN_STRING_MASK) {
num_keys_count += p->u.array.count;
}
} else if (p->class_id == JS_CLASS_STRING) {
if (flags & JS_GPN_STRING_MASK) {
num_keys_count +=
js_string_obj_get_length(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
}
} else {
const JSClassExoticMethods *em = ctx->rt->class_array[p->class_id].exotic;
if (em && em->get_own_property_names) {
if (em->get_own_property_names(ctx, &tab_exotic, &exotic_count,
JS_MKPTR(JS_TAG_OBJECT, p)))
return -1;
for (i = 0; i < exotic_count; i++) {
atom = tab_exotic[i].atom;
kind = JS_AtomGetKind(ctx, atom);
if (((flags >> kind) & 1) != 0) {
is_enumerable = FALSE;
if (flags & (JS_GPN_SET_ENUM | JS_GPN_ENUM_ONLY)) {
JSPropertyDescriptor desc;
int res;
/* set the "is_enumerable" field if necessary */
res = JS_GetOwnPropertyInternal(ctx, &desc, p, atom);
if (res < 0) {
js_free_prop_enum(ctx, tab_exotic, exotic_count);
return -1;
}
if (res) {
is_enumerable = ((desc.flags & JS_PROP_ENUMERABLE) != 0);
js_free_desc(ctx, &desc);
}
tab_exotic[i].is_enumerable = is_enumerable;
}
if (!(flags & JS_GPN_ENUM_ONLY) || is_enumerable) {
exotic_keys_count++;
}
}
}
}
}
}
/* fill them */
atom_count =
num_keys_count + str_keys_count + sym_keys_count + exotic_keys_count;
/* avoid allocating 0 bytes */
tab_atom = js_malloc(ctx, sizeof(tab_atom[0]) * max_int(atom_count, 1));
if (!tab_atom) {
js_free_prop_enum(ctx, tab_exotic, exotic_count);
return -1;
}
num_index = 0;
str_index = num_keys_count;
sym_index = str_index + str_keys_count;
num_sorted = TRUE;
sh = p->shape;
for (i = 0, prs = get_shape_prop(sh); i < sh->prop_count; i++, prs++) {
atom = prs->atom;
if (atom != JS_ATOM_NULL) {
is_enumerable = ((prs->flags & JS_PROP_ENUMERABLE) != 0);
kind = JS_AtomGetKind(ctx, atom);
if ((!(flags & JS_GPN_ENUM_ONLY) || is_enumerable) &&
((flags >> kind) & 1) != 0) {
if (JS_AtomIsArrayIndex(ctx, &num_key, atom)) {
j = num_index++;
num_sorted = FALSE;
} else if (kind == JS_ATOM_KIND_STRING) {
j = str_index++;
} else {
j = sym_index++;
}
tab_atom[j].atom = JS_DupAtom(ctx, atom);
tab_atom[j].is_enumerable = is_enumerable;
}
}
}
if (p->is_exotic) {
int len;
if (p->fast_array) {
if (flags & JS_GPN_STRING_MASK) {
len = p->u.array.count;
goto add_array_keys;
}
} else if (p->class_id == JS_CLASS_STRING) {
if (flags & JS_GPN_STRING_MASK) {
len = js_string_obj_get_length(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
add_array_keys:
for (i = 0; i < len; i++) {
tab_atom[num_index].atom = __JS_AtomFromUInt32(i);
if (tab_atom[num_index].atom == JS_ATOM_NULL) {
js_free_prop_enum(ctx, tab_atom, num_index);
return -1;
}
tab_atom[num_index].is_enumerable = TRUE;
num_index++;
}
}
} else {
/* Note: exotic keys are not reordered and comes after the object own
* properties. */
for (i = 0; i < exotic_count; i++) {
atom = tab_exotic[i].atom;
is_enumerable = tab_exotic[i].is_enumerable;
kind = JS_AtomGetKind(ctx, atom);
if ((!(flags & JS_GPN_ENUM_ONLY) || is_enumerable) &&
((flags >> kind) & 1) != 0) {
tab_atom[sym_index].atom = atom;
tab_atom[sym_index].is_enumerable = is_enumerable;
sym_index++;
} else {
JS_FreeAtom(ctx, atom);
}
}
js_free(ctx, tab_exotic);
}
}
assert(num_index == num_keys_count);
assert(str_index == num_keys_count + str_keys_count);
assert(sym_index == atom_count);
if (num_keys_count != 0 && !num_sorted) {
rqsort(tab_atom, num_keys_count, sizeof(tab_atom[0]), num_keys_cmp, ctx);
}
*ptab = tab_atom;
*plen = atom_count;
return 0;
}
int JS_GetOwnPropertyNames(JSContext *ctx, JSPropertyEnum **ptab,
uint32_t *plen, JSValueConst obj, int flags) {
if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT) {
JS_ThrowTypeErrorNotAnObject(ctx);
return -1;
}
return JS_GetOwnPropertyNamesInternal(ctx, ptab, plen, JS_VALUE_GET_OBJ(obj),
flags);
}
/* Return -1 if exception,
FALSE if the property does not exist, TRUE if it exists. If TRUE is
returned, the property descriptor 'desc' is filled present. */
int JS_GetOwnPropertyInternal(JSContext *ctx, JSPropertyDescriptor *desc,
JSObject *p, JSAtom prop) {
JSShapeProperty *prs;
JSProperty *pr;
retry:
prs = find_own_property(&pr, p, prop);
if (prs) {
if (desc) {
desc->flags = prs->flags & JS_PROP_C_W_E;
desc->getter = JS_UNDEFINED;
desc->setter = JS_UNDEFINED;
desc->value = JS_UNDEFINED;
if (unlikely(prs->flags & JS_PROP_TMASK)) {
if ((prs->flags & JS_PROP_TMASK) == JS_PROP_GETSET) {
desc->flags |= JS_PROP_GETSET;
if (pr->u.getset.getter)
desc->getter =
JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.getter));
if (pr->u.getset.setter)
desc->setter =
JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.setter));
} else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
JSValue val = *pr->u.var_ref->pvalue;
if (unlikely(JS_IsUninitialized(val))) {
JS_ThrowReferenceErrorUninitialized(ctx, prs->atom);
return -1;
}
desc->value = JS_DupValue(ctx, val);
} else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT) {
/* Instantiate property and retry */
if (JS_AutoInitProperty(ctx, p, prop, pr, prs))
return -1;
goto retry;
}
} else {
desc->value = JS_DupValue(ctx, pr->u.value);
}
} else {
/* for consistency, send the exception even if desc is NULL */
if (unlikely((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF)) {
if (unlikely(JS_IsUninitialized(*pr->u.var_ref->pvalue))) {
JS_ThrowReferenceErrorUninitialized(ctx, prs->atom);
return -1;
}
} else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT) {
/* nothing to do: delay instantiation until actual value and/or
* attributes are read */
}
}
return TRUE;
}
if (p->is_exotic) {
if (p->fast_array) {
/* specific case for fast arrays */
if (__JS_AtomIsTaggedInt(prop)) {
uint32_t idx;
idx = __JS_AtomToUInt32(prop);
if (idx < p->u.array.count) {
if (desc) {
desc->flags =
JS_PROP_WRITABLE | JS_PROP_ENUMERABLE | JS_PROP_CONFIGURABLE;
desc->getter = JS_UNDEFINED;
desc->setter = JS_UNDEFINED;
desc->value =
JS_GetPropertyUint32(ctx, JS_MKPTR(JS_TAG_OBJECT, p), idx);
}
return TRUE;
}
}
} else {
const JSClassExoticMethods *em = ctx->rt->class_array[p->class_id].exotic;
if (em && em->get_own_property) {
return em->get_own_property(ctx, desc, JS_MKPTR(JS_TAG_OBJECT, p),
prop);
}
}
}
return FALSE;
}
int JS_GetOwnProperty(JSContext *ctx, JSPropertyDescriptor *desc,
JSValueConst obj, JSAtom prop) {
if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT) {
JS_ThrowTypeErrorNotAnObject(ctx);
return -1;
}
return JS_GetOwnPropertyInternal(ctx, desc, JS_VALUE_GET_OBJ(obj), prop);
}
/* return -1 if exception (Proxy object only) or TRUE/FALSE */
int JS_IsExtensible(JSContext *ctx, JSValueConst obj) {