Repository navigation
Expand file tree
/
Copy pathvariable.c
More file actions
2528 lines (2279 loc) · 71 KB
/
Copy pathvariable.c
File metadata and controls
2528 lines (2279 loc) · 71 KB
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
/*
** variable.c - mruby variables
**
** See Copyright Notice in mruby.h
*/
#include <mruby.h>
#include <mruby/array.h>
#include <mruby/class.h>
#include <mruby/data.h>
#include <mruby/debug.h>
#include <mruby/error.h>
#include <mruby/hash.h>
#include <mruby/proc.h>
#include <mruby/string.h>
#include <mruby/variable.h>
#include <mruby/khash.h>
#include <mruby/internal.h>
/* Instance variable table structure */
typedef struct iv_tbl {
int size, alloc;
mrb_value *ptr;
} iv_tbl;
/* Creates the instance variable table. */
static iv_tbl*
iv_new(mrb_state *mrb)
{
iv_tbl *t;
t = (iv_tbl*)mrb_malloc(mrb, sizeof(iv_tbl));
t->size = 0;
t->alloc = 0;
t->ptr = NULL;
return t;
}
static void iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val);
#define IV_INITIAL_SIZE 2
static void
iv_rehash(mrb_state *mrb, iv_tbl *t)
{
int old_alloc = t->alloc;
int new_alloc = old_alloc > 0 ? old_alloc << 1 : IV_INITIAL_SIZE;
if (old_alloc == 0) {
/* first-time init */
t->ptr = (mrb_value*)mrb_calloc(mrb, new_alloc, sizeof(mrb_value)+sizeof(mrb_sym));
t->alloc = new_alloc;
return;
}
/* realloc may extend in place, avoiding malloc+memcpy+free */
size_t new_size = (size_t)new_alloc * (sizeof(mrb_value) + sizeof(mrb_sym));
t->ptr = (mrb_value*)mrb_realloc(mrb, t->ptr, new_size);
/* move keys from old position to new position */
mrb_sym *old_keys = (mrb_sym*)&t->ptr[old_alloc];
mrb_sym *new_keys = (mrb_sym*)&t->ptr[new_alloc];
memmove(new_keys, old_keys, sizeof(mrb_sym) * t->size);
/* clear extended value region (where old keys were + new slots) */
memset(&t->ptr[old_alloc], 0, sizeof(mrb_value) * (new_alloc - old_alloc));
/* clear extended key region */
memset(&new_keys[t->size], 0, sizeof(mrb_sym) * (new_alloc - t->size));
t->alloc = new_alloc;
}
/* Branch-free binary search helper: returns the index where `target` should be inserted/found. */
static inline int
iv_bsearch_idx(mrb_sym *keys, int size, mrb_sym target) {
if (size == 0) return 0;
int n = size;
mrb_sym *p = keys;
/* While more than one element remains, halve the range each iteration */
while (n > 1) {
int half = n >> 1;
MRB_MEM_PREFETCH(p + (half >> 1));
MRB_MEM_PREFETCH(p + half + (half >> 1));
mrb_sym mid_sym = p[half];
/*
* Update pointer p without a branch:
* If mid_sym < target, move p forward by half; otherwise keep p unchanged.
* Compiler will emit a CMOV or equivalent.
*/
p = (mid_sym < target) ? p + half : p;
n -= half;
}
/* Final adjustment: if the remaining element is still less than target, advance by one */
return (int)(p - keys) + (p[0] < target);
}
/* Set (insert or update) the value for `sym` in the instance variable table using branch-free search. */
static void
iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val)
{
/* If table is uninitialized, allocate and initialize */
if (t->alloc == 0) {
iv_rehash(mrb, t);
}
/* Obtain pointers to keys and values arrays */
mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
mrb_value *vals = t->ptr;
/* Determine insertion/update index:
* If table has entries, use branch-free search; otherwise index = 0.
*/
int lo = iv_bsearch_idx(keys, t->size, sym);
/* If the key already exists, update its value and return */
if (lo < t->size && keys[lo] == sym) {
vals[lo] = val;
return;
}
/* Grow table if full, then recompute position */
if (t->size == t->alloc) {
iv_rehash(mrb, t);
keys = (mrb_sym*)&t->ptr[t->alloc];
vals = t->ptr;
lo = iv_bsearch_idx(keys, t->size, sym);
}
/* Shift existing entries right to make room at index lo */
int move_count = t->size - lo;
if (move_count > 0) {
memmove(&keys[lo + 1], &keys[lo], move_count * sizeof(mrb_sym));
memmove(&vals[lo + 1], &vals[lo], move_count * sizeof(mrb_value));
}
/* Insert the new key and value */
keys[lo] = sym;
vals[lo] = val;
t->size++;
}
/* Get a value for `sym` from the instance variable table using branch-free search. */
static int
iv_get(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
{
/* Return 0 if table is null, uninitialized, or empty */
if (t == NULL || t->alloc == 0 || t->size == 0) return 0;
mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
mrb_value *vals = t->ptr;
/* Find index in a branch-free manner */
int lo = iv_bsearch_idx(keys, t->size, sym);
/* If found, store value (if vp provided) and return 1-based position */
if (lo < t->size && keys[lo] == sym) {
if (vp) *vp = vals[lo];
return lo + 1;
}
/* Not found */
return 0;
}
/* Delete the entry for `sym` from the instance variable table using branch-free search. */
static mrb_bool
iv_del(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
{
/* Return FALSE if table is null, uninitialized, or empty */
if (t == NULL || t->alloc == 0 || t->size == 0) return FALSE;
mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
mrb_value *vals = t->ptr;
/* Find index in a branch-free manner */
int lo = iv_bsearch_idx(keys, t->size, sym);
/* If found, optionally return value and shift entries left to delete */
if (lo < t->size && keys[lo] == sym) {
if (vp) *vp = vals[lo];
int move_count = t->size - lo - 1;
if (move_count > 0) {
memmove(&keys[lo], &keys[lo + 1], move_count * sizeof(mrb_sym));
memmove(&vals[lo], &vals[lo + 1], move_count * sizeof(mrb_value));
}
t->size--;
return TRUE;
}
/* Not found */
return FALSE;
}
/* Iterates over the instance variable table. */
static void
iv_foreach(mrb_state *mrb, iv_tbl *t, mrb_iv_foreach_func *func, void *p)
{
if (t == NULL || t->alloc == 0 || t->size == 0) return;
for (int i = 0; i < t->size; i++) {
mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
mrb_value *vals = t->ptr;
if ((*func)(mrb, keys[i], vals[i], p) != 0) return;
}
}
/* Get the size of the instance variable table. */
static size_t
iv_size(mrb_state *mrb, iv_tbl *t)
{
return t ? t->size : 0;
}
/* Copy the sorted table */
static iv_tbl*
iv_copy(mrb_state *mrb, iv_tbl *t)
{
if (t == NULL || t->alloc == 0 || t->size == 0) return NULL;
/* create new table and mirror alloc/size */
iv_tbl *t2 = iv_new(mrb);
t2->alloc = t->alloc;
t2->size = t->size;
/* allocate the same block shape */
t2->ptr = (mrb_value*)mrb_calloc(mrb, t2->alloc, sizeof(mrb_value)+sizeof(mrb_sym));
/* copy values[0...size] and keys[0...size] */
memcpy(t2->ptr, t->ptr, sizeof(mrb_value)*t2->size);
memcpy(&t2->ptr[t2->alloc], &t->ptr[t->alloc], sizeof(mrb_sym)*t2->size);
return t2;
}
/* Free memory of the instance variable table. */
static void
iv_free(mrb_state *mrb, iv_tbl *t)
{
mrb_free(mrb, t->ptr);
mrb_free(mrb, t);
}
/*
* Object Shape (Hidden Class) storage. The mrb_iv_shape / mrb_shaped_iv
* structures and the mrb_shape_lookup index lookup live in
* mruby/internal.h so the VM can inline the shaped OP_GETIV/OP_SETIV
* fast path; the tree construction and the slow paths stay here.
*/
/* Create the empty root shape */
static mrb_iv_shape*
shape_root(mrb_state *mrb)
{
mrb_iv_shape *s = (mrb_iv_shape*)mrb_calloc(mrb, 1, sizeof(mrb_iv_shape));
return s;
}
/* Find a child shape with the given edge sym */
static mrb_iv_shape*
shape_find_child(mrb_iv_shape *shape, mrb_sym sym)
{
mrb_iv_shape *c = shape->children;
while (c) {
if (c->edge == sym) return c;
c = c->sibling;
}
return NULL;
}
/* Find or create a child shape for adding sym */
static mrb_iv_shape*
shape_transition(mrb_state *mrb, mrb_iv_shape *shape, mrb_sym sym)
{
mrb_iv_shape *child = shape_find_child(shape, sym);
if (child) return child;
/* create new child shape */
child = (mrb_iv_shape*)mrb_malloc(mrb, sizeof(mrb_iv_shape));
child->parent = shape;
child->children = NULL;
child->sibling = shape->children;
child->edge = sym;
child->count = shape->count + 1;
shape->children = child;
return child;
}
/* Recursively free all shapes in the tree */
static void
shape_free_tree(mrb_state *mrb, mrb_iv_shape *shape)
{
mrb_iv_shape *c = shape->children;
while (c) {
mrb_iv_shape *next = c->sibling;
shape_free_tree(mrb, c);
c = next;
}
mrb_free(mrb, shape);
}
/* Allocate a mrb_shaped_iv with room for count values */
static mrb_shaped_iv*
shaped_iv_alloc(mrb_state *mrb, mrb_iv_shape *shape)
{
size_t sz = offsetof(mrb_shaped_iv, values) +
sizeof(mrb_value) * shape->count;
mrb_shaped_iv *siv = (mrb_shaped_iv*)mrb_malloc(mrb, sz);
siv->shape = shape;
return siv;
}
/* Convert a shaped object back to traditional iv_tbl (de-shape) */
static void
shaped_to_iv_tbl(mrb_state *mrb, struct RObject *obj)
{
mrb_shaped_iv *siv = (mrb_shaped_iv*)obj->iv;
iv_tbl *t = NULL;
if (siv) {
mrb_iv_shape *shape = siv->shape;
if (shape->count > 0) {
/* reconstruct keys from parent chain */
mrb_sym keys[MRB_SHAPE_MAX_IVS];
mrb_iv_shape *s = shape;
while (s->count > 0) {
keys[s->count - 1] = s->edge;
s = s->parent;
}
t = iv_new(mrb);
for (int i = 0; i < shape->count; i++) {
if (!mrb_undef_p(siv->values[i])) {
iv_put(mrb, t, keys[i], siv->values[i]);
}
}
}
mrb_free(mrb, siv);
}
obj->iv = t;
obj->flags &= ~MRB_FL_OBJ_SHAPED;
}
/* --- Shaped IV operations --- */
static void
shaped_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
{
mrb_shaped_iv *siv = (mrb_shaped_iv*)obj->iv;
mrb_iv_shape *shape = siv ? siv->shape : mrb->root_shape;
/* check if sym already exists in current shape */
int idx = mrb_shape_lookup(mrb, shape, sym);
if (idx >= 0) {
siv->values[idx] = v;
return;
}
/* transition to new shape */
mrb_iv_shape *new_shape = shape_transition(mrb, shape, sym);
/* de-shape if too many IVs */
if (new_shape->count > MRB_SHAPE_MAX_IVS) {
shaped_to_iv_tbl(mrb, obj);
if (!obj->iv) {
obj->iv = iv_new(mrb);
}
iv_put(mrb, obj->iv, sym, v);
return;
}
/* allocate new shaped_iv with room for new shape */
mrb_shaped_iv *new_siv = shaped_iv_alloc(mrb, new_shape);
/* copy old values (they are a prefix) */
if (siv) {
memcpy(new_siv->values, siv->values,
sizeof(mrb_value) * shape->count);
mrb_free(mrb, siv);
}
/* new IV goes in the last slot */
new_siv->values[new_shape->count - 1] = v;
obj->iv = (iv_tbl*)new_siv;
}
static mrb_value
shaped_iv_get(mrb_state *mrb, struct RObject *obj, mrb_sym sym)
{
mrb_shaped_iv *siv = (mrb_shaped_iv*)obj->iv;
if (!siv) return mrb_nil_value();
int idx = mrb_shape_lookup(mrb, siv->shape, sym);
if (idx >= 0 && !mrb_undef_p(siv->values[idx]))
return siv->values[idx];
return mrb_nil_value();
}
static mrb_bool
shaped_iv_defined(mrb_state *mrb, struct RObject *obj, mrb_sym sym)
{
mrb_shaped_iv *siv = (mrb_shaped_iv*)obj->iv;
if (!siv) return FALSE;
int idx = mrb_shape_lookup(mrb, siv->shape, sym);
if (idx >= 0 && !mrb_undef_p(siv->values[idx]))
return TRUE;
return FALSE;
}
static void
shaped_iv_foreach(mrb_state *mrb, struct RObject *obj,
mrb_iv_foreach_func *func, void *p)
{
mrb_shaped_iv *siv = (mrb_shaped_iv*)obj->iv;
if (!siv) return;
mrb_iv_shape *shape = siv->shape;
int count = shape->count;
if (count == 0) return;
/* reconstruct keys from parent chain: the shape tree is never freed while
the VM is alive (mrb_free_shape() tears it all down at shutdown), and
nothing here calls back into Ruby, so this part cannot be invalidated
by what follows. */
mrb_sym keys[MRB_SHAPE_MAX_IVS];
mrb_iv_shape *s = shape;
while (s->count > 0) {
keys[s->count - 1] = s->edge;
s = s->parent;
}
/* `func` can run arbitrary Ruby: mrb_obj_inspect() dispatches to a
user-defined #inspect on each value, which can add or remove an
instance variable on `obj` itself. Either one frees this `siv` out
from under the loop, growing it (shaped_iv_set(), a wider block, the
old one freed) or de-shaping it (shaped_to_iv_tbl(), called from
mrb_iv_remove(), to a plain iv_tbl). Reading `siv->values[i]` again
for the next key is then a use-after-free (GHSA-j6fq-xj4w-877x).
`obj->iv` names whichever block is current, so unchanged since the
loop started is exactly "nothing freed this one yet": the fast path
below stays in one comparison's reach of what the loop cost before
this fix, and only a key affected by a mutating callback pays for
mrb_obj_iv_get()/_defined() re-deriving it from the object's current
representation, shaped or not. */
const iv_tbl *orig = obj->iv;
for (int i = 0; i < count; i++) {
if (obj->iv == orig) {
if (!mrb_undef_p(siv->values[i])) {
if ((*func)(mrb, keys[i], siv->values[i], p) != 0) return;
}
}
else if (mrb_obj_iv_defined(mrb, obj, keys[i])) {
mrb_value v = mrb_obj_iv_get(mrb, obj, keys[i]);
if ((*func)(mrb, keys[i], v, p) != 0) return;
}
}
}
static size_t
shaped_iv_mark(mrb_state *mrb, struct RObject *obj)
{
mrb_shaped_iv *siv = (mrb_shaped_iv*)obj->iv;
if (!siv) return 0;
mrb_iv_shape *shape = siv->shape;
for (int i = 0; i < shape->count; i++) {
if (!mrb_undef_p(siv->values[i])) {
mrb_gc_mark_value(mrb, siv->values[i]);
}
}
return shape->count;
}
static void
shaped_iv_free(mrb_state *mrb, struct RObject *obj)
{
if (obj->iv) {
mrb_free(mrb, obj->iv);
}
}
static void
shaped_iv_copy(mrb_state *mrb, struct RObject *dst, struct RObject *src)
{
mrb_shaped_iv *ssiv = (mrb_shaped_iv*)src->iv;
if (!ssiv) {
dst->iv = NULL;
return;
}
mrb_iv_shape *shape = ssiv->shape;
mrb_shaped_iv *dsiv = shaped_iv_alloc(mrb, shape);
memcpy(dsiv->values, ssiv->values, sizeof(mrb_value) * shape->count);
dst->iv = (iv_tbl*)dsiv;
}
/* Public init/free for shape tree (called from state.c) */
void
mrb_init_shape(mrb_state *mrb)
{
mrb->root_shape = shape_root(mrb);
}
void
mrb_free_shape(mrb_state *mrb)
{
if (mrb->root_shape) {
shape_free_tree(mrb, mrb->root_shape);
mrb->root_shape = NULL;
}
}
static int
iv_mark_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p)
{
mrb_gc_mark_value(mrb, v);
return 0;
}
static void
mark_tbl(mrb_state *mrb, iv_tbl *t)
{
iv_foreach(mrb, t, iv_mark_i, 0);
}
void
mrb_gc_mark_gv(mrb_state *mrb)
{
mark_tbl(mrb, mrb->globals);
}
void
mrb_gc_free_gv(mrb_state *mrb)
{
if (mrb->globals) {
iv_free(mrb, mrb->globals);
mrb->globals = NULL;
}
}
size_t
mrb_gc_mark_iv(mrb_state *mrb, struct RObject *obj)
{
if (MRB_OBJ_SHAPED_P(obj)) {
return shaped_iv_mark(mrb, obj);
}
mark_tbl(mrb, obj->iv);
return iv_size(mrb, obj->iv);
}
void
mrb_gc_free_iv(mrb_state *mrb, struct RObject *obj)
{
if (MRB_OBJ_SHAPED_P(obj)) {
shaped_iv_free(mrb, obj);
return;
}
if (obj->iv) {
iv_free(mrb, obj->iv);
}
}
mrb_value
mrb_vm_special_get(mrb_state *mrb, mrb_sym i)
{
return mrb_fixnum_value(0);
}
void
mrb_vm_special_set(mrb_state *mrb, mrb_sym i, mrb_value v)
{
}
static mrb_bool
obj_iv_p(mrb_value obj)
{
return mrb_obj_iv_slot_p(mrb_unboxed_type(obj));
}
/* Instance variables of objects whose struct has no iv slot: a String or
Array subclass with an attribute is common. Rather than an iv slot in every
such struct, a side table keyed by the object holds an iv_tbl per object
that was given one. The table comes into being with the first such ivar.
An object with an entry has MRB_FL_HAS_GIV set, so the GC looks up only
those; a Proc has no bit to spare, so every Proc is looked up once a Proc
has been given one. An entry goes with its object (mrb_gc_free_giv). Keys
are object pointers the hash never dereferences. */
#define giv_hash_func(mrb,key) mrb_int_hash_func(mrb, (intptr_t)(key) >> 4)
#define giv_hash_equal(mrb,a,b) ((a) == (b))
KHASH_DECLARE(giv, struct RBasic*, iv_tbl*, TRUE)
KHASH_DEFINE(giv, struct RBasic*, iv_tbl*, TRUE, giv_hash_func, giv_hash_equal)
static iv_tbl*
giv_new_tbl(mrb_state *mrb, iv_tbl *unused)
{
(void)unused;
return iv_new(mrb);
}
/* Gives `key` an entry holding the table `make(mrb, src)` builds.
Any allocation may run the GC (every one does under MRB_GC_STRESS), and
the GC reads this hash: the mark looks up each slotless object's entry,
and the sweep deletes the entries of the objects it frees. So the entry
is published holding NULL, which every reader takes for "no ivars",
before the table is built; and the slot is looked up again afterwards,
because a sweep's deletion can move the entries of a small table. An
allocation failure leaves the NULL entry, which goes with its object. */
static iv_tbl*
giv_store(mrb_state *mrb, struct RBasic *key, iv_tbl *(*make)(mrb_state*, iv_tbl*), iv_tbl *src)
{
kh_giv_t *h = mrb->giv;
khiter_t k = kh_put(giv, mrb, h, key);
kh_value(giv, h, k) = NULL;
if (key->tt == MRB_TT_PROC) mrb->gc.giv_proc = TRUE;
else key->flags |= MRB_FL_HAS_GIV;
iv_tbl *t = make(mrb, src);
k = kh_get(giv, mrb, h, key);
mrb_assert(k != kh_end(h));
kh_value(giv, h, k) = t;
return t;
}
static iv_tbl*
giv_tbl(mrb_state *mrb, mrb_value obj, mrb_bool create)
{
kh_giv_t *h = mrb->giv;
struct RBasic *key = mrb_basic_ptr(obj);
if (!create && !mrb_gc_giv_p(mrb, key)) return NULL;
if (!h) {
if (!create) return NULL;
h = mrb->giv = kh_init(giv, mrb);
}
khiter_t k = kh_get(giv, mrb, h, key);
if (k != kh_end(h)) return kh_value(giv, h, k);
if (!create) return NULL;
return giv_store(mrb, key, giv_new_tbl, NULL);
}
/* An object that can hold instance variables in the side table. A Range or a
number is frozen and an internal type is out of reach, so none of them need
one. */
static mrb_bool
giv_obj_p(mrb_value obj)
{
if (mrb_immediate_p(obj)) return FALSE;
switch (mrb_unboxed_type(obj)) {
case MRB_TT_STRING:
case MRB_TT_ARRAY:
case MRB_TT_STRUCT:
case MRB_TT_SET:
case MRB_TT_FIBER:
case MRB_TT_ISTRUCT:
case MRB_TT_PROC:
return TRUE;
default:
return FALSE;
}
}
size_t
mrb_gc_mark_giv(mrb_state *mrb, struct RBasic *obj)
{
iv_tbl *t = giv_tbl(mrb, mrb_obj_value(obj), FALSE);
if (!t) return 0;
mark_tbl(mrb, t);
return iv_size(mrb, t);
}
void
mrb_gc_free_giv(mrb_state *mrb, struct RBasic *obj)
{
kh_giv_t *h = mrb->giv;
khiter_t k = kh_get(giv, mrb, h, obj);
if (k == kh_end(h)) return;
if (kh_value(giv, h, k)) iv_free(mrb, kh_value(giv, h, k));
kh_del(giv, mrb, h, k);
if (obj->tt != MRB_TT_PROC) obj->flags &= ~MRB_FL_HAS_GIV;
}
void
mrb_gc_free_giv_all(mrb_state *mrb)
{
kh_giv_t *h = mrb->giv;
if (!h) return;
mrb->giv = NULL;
for (khiter_t k = kh_begin(h); k != kh_end(h); k++) {
if (kh_exist(giv, h, k) && kh_value(giv, h, k)) iv_free(mrb, kh_value(giv, h, k));
}
kh_destroy(giv, mrb, h);
}
static iv_tbl*
class_iv_ptr(struct RClass *c)
{
return c->tt == MRB_TT_ICLASS ? c->c->iv : c->iv;
}
/*
* Retrieves an instance variable from an object.
*
* Args:
* mrb: The mruby state.
* obj: The object from which to retrieve the instance variable.
* sym: The symbol representing the name of the instance variable.
*
* Returns:
* The value of the instance variable, or mrb_nil_value() if the
* instance variable is not defined.
*/
MRB_API mrb_value
mrb_obj_iv_get(mrb_state *mrb, struct RObject *obj, mrb_sym sym)
{
if (MRB_OBJ_SHAPED_P(obj)) {
return shaped_iv_get(mrb, obj, sym);
}
mrb_value v;
if (obj->iv && iv_get(mrb, obj->iv, sym, &v))
return v;
return mrb_nil_value();
}
/*
* Retrieves an instance variable from an mrb_value.
*
* This function is a wrapper around mrb_obj_iv_get. It checks if the
* given mrb_value is an object that can have instance variables before
* attempting to retrieve the variable.
*
* Args:
* mrb: The mruby state.
* obj: The mrb_value from which to retrieve the instance variable.
* sym: The symbol representing the name of the instance variable.
*
* Returns:
* The value of the instance variable, or mrb_nil_value() if the
* instance variable is not defined or if the object cannot have
* instance variables.
*/
MRB_API mrb_value
mrb_iv_get(mrb_state *mrb, mrb_value obj, mrb_sym sym)
{
if (obj_iv_p(obj)) {
return mrb_obj_iv_get(mrb, mrb_obj_ptr(obj), sym);
}
if (mrb->giv && giv_obj_p(obj)) {
iv_tbl *t = giv_tbl(mrb, obj, FALSE);
mrb_value v;
if (t && iv_get(mrb, t, sym, &v)) return v;
}
return mrb_nil_value();
}
static inline mrb_bool
namespace_p(enum mrb_vtype tt)
{
return tt == MRB_TT_CLASS || tt == MRB_TT_MODULE ? TRUE : FALSE;
}
static inline void
assign_class_name(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
{
if (namespace_p(mrb_type(v))) {
struct RObject *c = mrb_obj_ptr(v);
const char *name = mrb_sym_name_len(mrb, sym, NULL);
if (obj != c && name && ISUPPER(name[0])) {
mrb_sym id_classname = MRB_SYM(__classname__);
mrb_value o = mrb_obj_iv_get(mrb, c, id_classname);
if (mrb_nil_p(o)) {
mrb_sym id_outer = MRB_SYM(__outer__);
o = mrb_obj_iv_get(mrb, c, id_outer);
if (mrb_nil_p(o)) {
if ((struct RClass*)obj == mrb->object_class) {
mrb_obj_iv_set_force(mrb, c, id_classname, mrb_symbol_value(sym));
}
else {
mrb_obj_iv_set_force(mrb, c, id_outer, mrb_obj_value(obj));
}
}
}
}
}
}
void
mrb_obj_iv_set_force(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
{
if (namespace_p(obj->tt)) {
assign_class_name(mrb, obj, sym, v);
}
if (MRB_OBJ_SHAPED_P(obj)) {
shaped_iv_set(mrb, obj, sym, v);
mrb_field_write_barrier_value(mrb, (struct RBasic*)obj, v);
return;
}
if (!obj->iv) {
obj->iv = iv_new(mrb);
}
iv_put(mrb, obj->iv, sym, v);
mrb_field_write_barrier_value(mrb, (struct RBasic*)obj, v);
}
/*
* Sets an instance variable on an object.
*
* This function checks if the object is frozen before setting the variable.
* It then calls mrb_obj_iv_set_force to actually set the variable.
*
* Args:
* mrb: The mruby state.
* obj: The object on which to set the instance variable.
* sym: The symbol representing the name of the instance variable.
* v: The value to set for the instance variable.
*/
MRB_API void
mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
{
mrb_check_frozen(mrb, obj);
mrb_obj_iv_set_force(mrb, obj, sym, v);
}
/*
* Iterates over the instance variables of an object.
*
* This function calls the provided callback function for each instance
* variable in the object.
*
* Args:
* mrb: The mruby state.
* obj: The mrb_value whose instance variables to iterate over.
* func: The callback function to call for each instance variable.
* The function should take mrb_state*, mrb_sym, mrb_value, and void*
* as arguments and return an int. If the callback returns a non-zero
* value, iteration stops.
* p: A pointer to user data that will be passed to the callback function.
*/
MRB_API void
mrb_iv_foreach(mrb_state *mrb, mrb_value obj, mrb_iv_foreach_func *func, void *p)
{
if (!obj_iv_p(obj)) {
if (mrb->giv && giv_obj_p(obj)) {
iv_tbl *t = giv_tbl(mrb, obj, FALSE);
if (t) iv_foreach(mrb, t, func, p);
}
return;
}
if (MRB_OBJ_SHAPED_P(mrb_obj_ptr(obj))) {
shaped_iv_foreach(mrb, mrb_obj_ptr(obj), func, p);
return;
}
iv_foreach(mrb, mrb_obj_ptr(obj)->iv, func, p);
}
/*
* Sets an instance variable on an mrb_value.
*
* This function is a wrapper around mrb_obj_iv_set. It checks if the
* given mrb_value is an object that can have instance variables before
* attempting to set the variable. If the object cannot have instance
* variables, it raises an E_ARGUMENT_ERROR.
*
* Args:
* mrb: The mruby state.
* obj: The mrb_value on which to set the instance variable.
* sym: The symbol representing the name of the instance variable.
* v: The value to set for the instance variable.
*
* Raises:
* E_ARGUMENT_ERROR: If the object cannot have instance variables.
*/
MRB_API void
mrb_iv_set(mrb_state *mrb, mrb_value obj, mrb_sym sym, mrb_value v)
{
if (obj_iv_p(obj)) {
mrb_obj_iv_set(mrb, mrb_obj_ptr(obj), sym, v);
}
else if (giv_obj_p(obj)) {
mrb_check_frozen(mrb, mrb_basic_ptr(obj));
iv_put(mrb, giv_tbl(mrb, obj, TRUE), sym, v);
mrb_field_write_barrier_value(mrb, mrb_basic_ptr(obj), v);
}
else {
mrb_raise(mrb, E_ARGUMENT_ERROR, "cannot set instance variable");
}
}
/*
* Checks if an instance variable is defined on an object.
*
* Args:
* mrb: The mruby state.
* obj: The object to check.
* sym: The symbol representing the name of the instance variable.
*
* Returns:
* TRUE if the instance variable is defined, FALSE otherwise.
*/
MRB_API mrb_bool
mrb_obj_iv_defined(mrb_state *mrb, struct RObject *obj, mrb_sym sym)
{
if (MRB_OBJ_SHAPED_P(obj)) {
return shaped_iv_defined(mrb, obj, sym);
}
iv_tbl *t = obj->iv;
if (t && iv_get(mrb, t, sym, NULL)) return TRUE;
return FALSE;
}
/*
* Checks if an instance variable is defined on an mrb_value.
*
* This function is a wrapper around mrb_obj_iv_defined. It checks if the
* given mrb_value is an object that can have instance variables before
* attempting to check for the variable.
*
* Args:
* mrb: The mruby state.
* obj: The mrb_value to check.
* sym: The symbol representing the name of the instance variable.
*
* Returns:
* TRUE if the instance variable is defined and the object can have
* instance variables, FALSE otherwise.
*/
MRB_API mrb_bool
mrb_iv_defined(mrb_state *mrb, mrb_value obj, mrb_sym sym)
{
if (!obj_iv_p(obj)) {
if (mrb->giv && giv_obj_p(obj)) {
iv_tbl *t = giv_tbl(mrb, obj, FALSE);
return t && iv_get(mrb, t, sym, NULL);
}
return FALSE;
}
return mrb_obj_iv_defined(mrb, mrb_obj_ptr(obj), sym);
}
/*
* Checks if a symbol is a valid instance variable name.
*
* A valid instance variable name must:
* - Be at least 2 characters long.
* - Start with '@'.
* - Not have a digit as the second character.
* - The rest of the name must be a valid identifier.
*
* Args:
* mrb: The mruby state.
* iv_name: The symbol to check.
*
* Returns:
* TRUE if the symbol is a valid instance variable name, FALSE otherwise.
*/
MRB_API mrb_bool
mrb_iv_name_sym_p(mrb_state *mrb, mrb_sym iv_name)
{
mrb_int len;
const char *s = mrb_sym_name_len(mrb, iv_name, &len);
if (len < 2) return FALSE;
if (s[0] != '@') return FALSE;
if (ISDIGIT(s[1])) return FALSE;
return mrb_ident_p(s+1, len-1);
}
/*
* Checks if a symbol is a valid instance variable name and raises a
* name error if it's not.
*
* Args:
* mrb: The mruby state.
* iv_name: The symbol to check.
*
* Raises:
* E_NAME_ERROR: If the symbol is not a valid instance variable name.
*/
MRB_API void
mrb_iv_name_sym_check(mrb_state *mrb, mrb_sym iv_name)
{
if (!mrb_iv_name_sym_p(mrb, iv_name)) {
mrb_name_error(mrb, iv_name, "'%n' is not allowed as an instance variable name", iv_name);
}
}
/*
* Copies instance variables from one object to another.
*
* If the destination object already has instance variables, they are freed
* before copying.
*
* Args:
* mrb: The mruby state.
* dest: The destination object (mrb_value).
* src: The source object (mrb_value).
*/
MRB_API void