Repository navigation
Expand file tree
/
Copy pathinternal.h
More file actions
1156 lines (1069 loc) · 55.4 KB
/
Copy pathinternal.h
File metadata and controls
1156 lines (1069 loc) · 55.4 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
/**
** @file mruby/internal.h - Functions only called from within the library
**
** See Copyright Notice in mruby.h
*/
#ifndef MRUBY_INTERNAL_H
#define MRUBY_INTERNAL_H
#ifdef MRUBY_ARRAY_H
void mrb_ary_decref(mrb_state*, mrb_shared_array*);
mrb_value mrb_ary_subseq(mrb_state *mrb, mrb_value ary, mrb_int beg, mrb_int len);
#endif
/* What frame `ci` of context `c` carries as special variables, or NULL.
*
* They live beside the ci stack rather than in it (see `svars` in struct
* mrb_context): a frame carrying one is rare, and a mrb_callinfo is 48 bytes
* with nothing spare. The array is NULL until a write needs one, so a
* program that never touches a special variable answers here with one load
* and one branch, and pays no memory for the frames it never fills. */
static inline struct RBasic*
mrb_ci_svar(const struct mrb_context *c, const mrb_callinfo *ci)
{
if (!c->svars) return NULL;
return c->svars[ci - c->cibase];
}
/* Record `sv` for frame `ci`. Making the array is left to vm.c, which is
* where the state to raise from is; a clear where there is no array is
* nothing to do, since an absent array reads as NULL throughout. */
void mrb_ci_svar_set(mrb_state *mrb, struct mrb_context *c, mrb_callinfo *ci, struct RBasic *sv);
/* Make `c`'s array if the state has ever written a special variable and this
context has none yet. Called where a frame gains an env, which is where a
later collection may need somewhere to record what that frame carries: the
recording itself runs inside marking and cannot allocate. */
void mrb_svars_reserve(mrb_state *mrb, struct mrb_context *c);
#ifdef MRUBY_COMPILE_H
/* Walk the top-level local variables of a parsed program, which is how
`eval` carries the caller's variables into the block it compiles.
Written by mruby-compiler and called by mruby-eval, so the typedef its
callback is spelled with is declared here rather than in each of them:
the amalgamation puts both in one translation unit, where a second copy
of a typedef is a redefinition. */
typedef mrb_bool mrb_parser_foreach_top_variable_func(mrb_state *mrb, mrb_sym sym, void *user);
void mrb_parser_foreach_top_variable(mrb_state *mrb, struct mrb_parser_state *p, mrb_parser_foreach_top_variable_func *func, void *user);
#endif
#ifdef MRUBY_CLASS_H
struct RClass *mrb_vm_define_class(mrb_state*, mrb_value, mrb_value, mrb_sym);
struct RClass *mrb_vm_define_module(mrb_state*, mrb_value, mrb_sym);
mrb_value mrb_instance_new(mrb_state *mrb, mrb_value cv);
void mrb_class_name_class(mrb_state*, struct RClass*, struct RClass*, mrb_sym);
mrb_bool mrb_const_name_p(mrb_state*, const char*, mrb_int);
mrb_value mrb_class_find_path(mrb_state*, struct RClass*);
mrb_value mrb_mod_to_s(mrb_state *, mrb_value);
mrb_value mrb_nil_to_s(mrb_state *, mrb_value);
mrb_value mrb_true_to_s(mrb_state *, mrb_value);
mrb_value mrb_false_to_s(mrb_state *, mrb_value);
#ifndef MRB_NO_FLOAT
mrb_value mrb_flo_to_s(mrb_state *, mrb_value);
#endif
void mrb_method_added(mrb_state *mrb, struct RClass *c, mrb_sym mid);
mrb_value mrb_mod_define_method_m(mrb_state *mrb, struct RClass *c);
void mrb_mod_define_method_scoped(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_method_t m);
mrb_noreturn void mrb_method_missing(mrb_state *mrb, mrb_sym name, mrb_value self, mrb_value args);
mrb_method_t mrb_vm_find_method(mrb_state *mrb, struct RClass *c, struct RClass **cp, mrb_sym mid);
#ifdef MRB_USE_REFINEMENTS
/* refinement.c / class.c / proc.c */
struct RArray *mrb_vm_refinements(mrb_state *mrb, const mrb_callinfo *ci);
struct RArray *mrb_proc_refinements(mrb_state *mrb, const struct RProc *p);
mrb_bool mrb_proc_refined_p(mrb_state *mrb, const struct RProc *p);
struct RArray *mrb_vm_caller_refinements(mrb_state *mrb);
mrb_method_t mrb_vm_find_refined_method(mrb_state *mrb, struct RArray *scope, struct RClass *c, struct RClass **cp, mrb_sym mid, struct RClass *exclude);
mrb_method_t mrb_vm_find_method_in_scope(mrb_state *mrb, struct RArray *scope, struct RClass *c, struct RClass **cp, mrb_sym mid);
mrb_bool mrb_refined_mid_p(mrb_state *mrb, mrb_sym mid);
void mrb_refinement_method_added(mrb_state *mrb, struct RClass *refinement, mrb_sym mid);
void mrb_refinement_ancestry_changed(mrb_state *mrb, struct RClass *c, struct RClass *m);
void mrb_proc_set_refscope(mrb_state *mrb, struct RProc *p, struct RArray *scope);
struct RArray *mrb_refscope_at(mrb_state *mrb, uint32_t idx);
struct RProc *mrb_scope_proc_new(mrb_state *mrb, const mrb_irep *irep);
void mrb_gc_clear_dead_refscopes(mrb_state *mrb);
#endif
mrb_value mrb_mod_const_missing(mrb_state *mrb, mrb_value mod);
int mrb_mod_method_visibility(mrb_state *mrb, mrb_value mod);
mrb_value mrb_const_missing(mrb_state *mrb, mrb_value mod, mrb_sym sym);
size_t mrb_class_mt_memsize(mrb_state*, struct RClass*);
mrb_value mrb_obj_extend(mrb_state*, mrb_value obj);
#endif
/* builtin operator guards (class.c); see `idx_class` and `bop_redefined` in
`struct mrb_state` */
void mrb_builtin_op_init(mrb_state *mrb);
void mrb_builtin_op_update(mrb_state *mrb, mrb_sym mid);
void mrb_idx_op_rearm(mrb_state *mrb, enum mrb_idx_op_slot slot);
mrb_value mrb_obj_equal_m(mrb_state *mrb, mrb_value);
/* debug */
size_t mrb_packed_int_len(uint32_t num);
size_t mrb_packed_int_encode(uint32_t num, uint8_t *p);
uint32_t mrb_packed_int_decode(const uint8_t *p, const uint8_t *end, const uint8_t **newpos);
/* codedump */
void mrb_codedump_all(mrb_state *mrb, struct RProc *proc);
#ifndef MRB_NO_STDIO
void mrb_codedump_all_file(mrb_state *mrb, struct RProc *proc, FILE *out);
#endif
/* error */
mrb_noreturn void mrb_raise_nomemory(mrb_state *mrb);
mrb_noreturn void mrb_no_method_error_recv(mrb_state *mrb, mrb_sym id, mrb_value recv, mrb_value args, const char *fmt, ...);
mrb_noreturn void mrb_name_error_recv(mrb_state *mrb, mrb_sym id, mrb_value recv, const char *fmt, ...);
mrb_value mrb_exc_inspect(mrb_state *mrb, mrb_value exc);
mrb_value mrb_exc_backtrace(mrb_state *mrb, mrb_value exc);
mrb_noreturn void mrb_exc_raise_with_cause(mrb_state *mrb, mrb_value exc, mrb_value cause);
mrb_value mrb_get_backtrace(mrb_state *mrb);
mrb_value mrb_get_packed_backtrace(mrb_state *mrb);
mrb_value mrb_exc_packed_backtrace(mrb_state *mrb, mrb_value exc);
mrb_int mrb_backtrace_len(mrb_value bt);
mrb_bool mrb_backtrace_location(mrb_state *mrb, mrb_value bt, mrb_int i, const char **file, int32_t *lineno, mrb_sym *mid, mrb_value *str);
void mrb_exc_mesg_set(mrb_state *mrb, struct RException *exc, mrb_value mesg);
mrb_value mrb_exc_mesg_get(mrb_state *mrb, struct RException *exc);
mrb_value mrb_f_raise(mrb_state*, mrb_value);
mrb_value mrb_make_exception(mrb_state *mrb, mrb_value exc, mrb_value mesg);
mrb_value mrb_exc_get_output(mrb_state *mrb, struct RObject *exc);
struct RBacktrace {
MRB_OBJECT_HEADER;
size_t len;
struct mrb_backtrace_location *locations;
};
struct mrb_backtrace_location {
mrb_sym method_id;
int32_t idx;
const mrb_irep *irep;
};
/* gc */
size_t mrb_gc_mark_mt(mrb_state*, struct RClass*);
int mrb_equal_in_c(mrb_state*, mrb_value, mrb_value);
void mrb_gc_each_live_object(mrb_state*, int (*)(mrb_state*, struct RBasic*, void*), void*);
void mrb_gc_free_mt(mrb_state*, struct RClass*);
#ifdef MRB_USE_GC_WEAK
/* A table that holds objects weakly, defined by a gem that sets
MRB_USE_GC_WEAK: what it holds is not marked through it, and `clear` is
called once marking is complete, before the sweep, to drop each object
mrb_gc_weak_dead_p() reports. `clear` may neither allocate nor run Ruby
code. The table owns the node and links it with mrb_gc_weak_add(), and
unlinks it before the node is freed. */
struct mrb_gc_weak {
struct mrb_gc_weak *prev, *next;
void (*clear)(mrb_state *mrb, struct mrb_gc_weak *weak);
};
void mrb_gc_weak_add(mrb_state *mrb, struct mrb_gc_weak *weak);
void mrb_gc_weak_remove(mrb_state *mrb, struct mrb_gc_weak *weak);
/* Only in a `clear` callback: whether `v`, which the table holds, is to be
freed by this sweep. An immediate value never is. */
mrb_bool mrb_gc_weak_dead_p(mrb_state *mrb, mrb_value v);
#endif
/* hash */
size_t mrb_hash_memsize(mrb_value obj);
size_t mrb_gc_mark_hash(mrb_state*, struct RHash*);
void mrb_gc_free_hash(mrb_state*, struct RHash*);
mrb_value mrb_hash_first_key(mrb_state*, mrb_value);
uint32_t mrb_obj_hash_code(mrb_state *mrb, mrb_value key);
/* irep */
struct mrb_insn_data mrb_decode_insn(const mrb_code *pc);
#ifdef MRUBY_IREP_H
#define MRB_MAKE_STATIC_IREP(nlocals, nregs, iseq, syms) { \
nlocals, nregs, 0, MRB_IREP_STATIC, \
iseq, NULL, syms, NULL, NULL, NULL, \
sizeof(iseq), 0, sizeof((syms)) / sizeof((syms)[0]), 0, 0, \
}
void mrb_irep_free(mrb_state*, struct mrb_irep*);
static inline const struct mrb_irep_catch_handler *
mrb_irep_catch_handler_table(const struct mrb_irep *irep)
{
if (irep->clen > 0) {
return (const struct mrb_irep_catch_handler*)(irep->iseq + irep->ilen);
}
else {
return (const struct mrb_irep_catch_handler*)NULL;
}
}
#endif
/* numeric */
mrb_value mrb_div_int_value(mrb_state *mrb, mrb_int x, mrb_int y);
mrb_int mrb_div_int(mrb_int x, mrb_int y);
mrb_value mrb_int_add(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_value mrb_int_sub(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_value mrb_int_mul(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_noreturn void mrb_int_zerodiv(mrb_state *mrb);
mrb_noreturn void mrb_int_overflow(mrb_state *mrb, const char *reason);
#ifndef MRB_NO_FLOAT
void mrb_check_num_exact(mrb_state *mrb, mrb_float num);
mrb_int mrb_int_float_cmp(mrb_int x, mrb_float y);
#endif
#ifdef MRB_USE_COMPLEX
/* struct mrb_complex sits beside the object header where it fits in the
slot, and behind one pointer where it does not. Which of the two a build
gets is also what obj_free() must know to free that pointer, so the
condition lives here, visible to both gc.c and the gem. Without
MRB_COMPLEX_FLOAT_ONLY a part is an mrb_value, which is wider than an
mrb_float wherever a value carries its type tag beside the payload, so
an unboxed build overflows the slot that its two floats used to fit. */
#if (defined(MRB_32BIT) && !defined(MRB_USE_FLOAT32)) || \
(!defined(MRB_COMPLEX_FLOAT_ONLY) && defined(MRB_NO_BOXING))
#define MRB_COMPLEX_INDIRECT
#endif
mrb_value mrb_complex_new(mrb_state *mrb, mrb_float x, mrb_float y);
mrb_value mrb_complex_add(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_value mrb_complex_sub(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_value mrb_complex_mul(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_value mrb_complex_div(mrb_state *mrb, mrb_value x, mrb_value y);
void mrb_complex_copy(mrb_state *mrb, mrb_value x, mrb_value y);
#ifndef MRB_COMPLEX_FLOAT_ONLY
mrb_value mrb_complex_new_value(mrb_state *mrb, mrb_value real, mrb_value imaginary);
int mrb_complex_mark(mrb_state *mrb, struct RBasic *comp);
#endif
#endif
#ifdef MRB_USE_RATIONAL
mrb_value mrb_rational_new(mrb_state *mrb, mrb_int x, mrb_int y);
mrb_value mrb_rational_add(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_value mrb_rational_sub(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_value mrb_rational_mul(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_value mrb_rational_div(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_value mrb_as_rational(mrb_state *mrb, mrb_value x);
mrb_value mrb_rational_canonicalize(mrb_state *mrb, mrb_value x);
mrb_bool mrb_rational_eq(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_value mrb_rational_hash(mrb_state *mrb, mrb_value rat);
void mrb_rational_copy(mrb_state *mrb, mrb_value x, mrb_value y);
int mrb_rational_mark(mrb_state *mrb, struct RBasic *rat);
#endif
#ifdef MRB_USE_COERCE
mrb_value mrb_num_coerce_bin(mrb_state *mrb, mrb_value x, mrb_value y, mrb_sym op);
#endif
#ifdef MRB_USE_SET
size_t mrb_gc_mark_set(mrb_state *mrb, struct RBasic *set);
void mrb_gc_free_set(mrb_state *mrb, struct RBasic *set);
size_t mrb_set_memsize(mrb_value);
#endif
/* One Ruby scope's special variables (CRuby's `struct vm_svar`): a plain
* container of MRB_SVAR_MAX slots, one per key of `enum mrb_svar_index`
* below, each holding any mrb_value. The container is an internal GC
* object so a frame and the env the scope escapes into can hold the same
* one, and it is allocated lazily, on the first non-nil write into a
* scope (svar_new() in vm.c), so a scope that never touches a special
* variable never carries one. The slots live outside the object because
* sizeof(mrb_value) varies with the boxing and RVALUE is sized tightly;
* gc.c marks them through the object and frees them with it. */
struct RSvar {
MRB_OBJECT_HEADER;
mrb_value *slots;
};
/* The keys of a Ruby scope's special variables, CRuby's `enum
* vm_svar_index` with CRuby's numbering. Each scope holds one container of
* MRB_SVAR_MAX slots (allocated lazily, see mrb_vm_svar_set()), and each
* key names one slot in it. The namespace is owned by the core: a new
* special variable takes a new enumerator here, never a key minted at
* runtime, so a key means the same slot in every build and gem load order.
*
* This enum and the accessor pair below are core-internal rather than
* MRB_API: the namespace they index is not open to a key minted outside
* this file, so exposing them would freeze the container's representation
* and the scope-resolution rule without giving an out-of-tree caller
* anything a new key could not already do from `mrb_gv_define_virtual()`.
* A gem publishes a special variable by registering a virtual global whose
* get/set pair closes over its own key here, the way mruby-regexp keeps
* `$~`'s MatchData under MRB_SVAR_BACKREF. */
enum mrb_svar_index {
MRB_SVAR_LASTLINE = 0, /* $_ */
MRB_SVAR_BACKREF, /* $~ */
MRB_SVAR_MAX
};
/* Reads one special-variable slot of the owning Ruby scope, resolved like
* CRuby's svar (a C frame reads through to the Ruby frame below it, a block
* shares its defining method's container, and a scope that returned keeps
* its container in its env, so a proc outliving it still reads the value).
* The core stores and marks the slots but gives them no meaning: a key's
* semantics belong to whoever pairs these accessors with a virtual global,
* the way mruby-regexp keeps `$~`'s MatchData under MRB_SVAR_BACKREF. */
mrb_value mrb_vm_svar_get(mrb_state *mrb, enum mrb_svar_index key);
/* Writes one special-variable slot of the owning Ruby scope. The slot holds
* any mrb_value, immediates included. Any richer contract, like
* mruby-regexp's TypeError for `$~ = <not a MatchData>`, belongs to the
* caller. The scope's container is allocated on the first non-nil write; a
* nil write into a scope that has none is dropped, nil being what a missing
* slot already reads as. */
void mrb_vm_svar_set(mrb_state *mrb, enum mrb_svar_index key, mrb_value v);
#ifdef MRUBY_PROC_H
#define MRB_MAKE_STATIC_PROC_FROM_IREP(irep) { \
NULL, MRB_TT_PROC, MRB_GC_RED, MRB_OBJ_IS_FROZEN, MRB_PROC_SCOPE | MRB_PROC_STRICT | MRB_PROC_ORPHAN, \
{ &irep }, NULL, { NULL } \
}
#if !defined(__cplusplus) || __cplusplus >= 202002L
/* The parameter is not spelled `func`: a macro parameter is replaced in the
member designator too, so that name would rewrite `.func` into `..` the
argument. Naming the member is what this needs, rather than writing a
function pointer through the union's first member, which is an object
pointer: C gives that conversion no meaning, and a target whose function
pointers are wider than its object pointers (the small cores mruby is
built for among them) would lose half of it. */
#define MRB_MAKE_STATIC_PROC_FROM_FUNC(cfunc) { \
NULL, MRB_TT_PROC, MRB_GC_RED, MRB_OBJ_IS_FROZEN, MRB_PROC_CFUNC_FL | MRB_PROC_ORPHAN, \
{ .func = cfunc }, NULL, { NULL } \
}
#else
/*
* In C++ older than C++20, "designated initializer" is not available.
* Instead, use the function-based initialization method.
*
* However, this approach may require dynamic initialization at runtime even
* for variables modified with `static const`, so the following precautions
* should be taken:
* - `static const`-qualified function-local variables are initialized
* during the first function call. Checking whether they have been
* initialized also incurs overhead associated with ensuring thread
* safety.
* - `static const`-qualified global variables are included as part of the
* program’s initialization process. To minimize the impact of
* initialization order, it is strongly recommended to keep external
* references to a minimum.
*/
#define MRB_MAKE_STATIC_PROC_FROM_FUNC(func) mrb_make_static_proc_from_func(func)
static inline struct RProc
mrb_make_static_proc_from_func(mrb_func_t func)
{
struct RProc p = {
NULL, MRB_TT_PROC, MRB_GC_RED, MRB_OBJ_IS_FROZEN, MRB_PROC_CFUNC_FL | MRB_PROC_ORPHAN,
{}, NULL, { NULL }
};
p.body.func = func;
return p;
}
#endif
/* A closed env may carry one slot past its locals: the special-variable
* container of the scope the env escapes from, which mrb_env_detach()
* moves there and svar_owner() reads back, or, for a scope that holds no
* container of its own, the env of the scope below whose special variables
* it shares, which the same walk follows one hop further (CRuby's ep
* chain, and its own svar slot is polymorphic the same way).
*
* Whether the slot is there is not implied by the env being closed. struct
* REnv, MRB_ENV_CLOSE() and MRB_ENV_SET_LEN() are public, and out-of-tree
* code builds closed envs over a stack of exactly MRB_ENV_LEN() values, so
* reading one past the locals of any closed env runs off such an
* allocation. The flag below says it for the env instead, leaving three
* states:
*
* on-stack ONSTACK_P, never SVAR_P; the stack is the VM's
* closed, no slot !ONSTACK_P, !SVAR_P; len values, or none at all
* closed, with the slot !ONSTACK_P, SVAR_P; len + 1 values
*
* The middle state is the ordinary way every scope closes: mrb_env_unshare()
* never asks for the extra value, so a closure escaping a scope that never
* held a container or a forward to install pays nothing for it. It is also
* what out-of-tree code makes, and what the core's own envs fall back to
* when there is no stack left to size (mrb_env_unshare() out of memory,
* error.c's fault-time rewind). It reads as a scope holding no container,
* and grows into the third state on the one write that needs it:
* mrb_env_detach() sizes the closing allocation for the slot up front
* (env_unshare_with_svar() in vm.c) when it has a container or forward to
* install at close time; svar_env_adopt_owner() adopting one just after,
* or mrb_vm_svar_set()'s first non-nil write into a scope that outlived
* its frame, instead grow an already-closed env into it (svar_slot_ensure()
* in vm.c). So: every path that grows a stack into the slot goes through
* MRB_ENV_SVAR_STACK_SIZE() and sets the flag, every path that reads or
* writes the slot goes through MRB_ENV_SVAR_SLOT() under MRB_ENV_SVAR_P(),
* and every path that drops the stack clears the flag. All three take the
* number of locals, which is MRB_ENV_LEN() once the env carries it.
*
* Invariant, asserted where the core reads the slot: MRB_ENV_SVAR_P(e)
* implies e is closed, e->stack is non-NULL, and its allocation holds
* MRB_ENV_LEN(e) + 1 values. */
#define MRB_ENV_SVAR_BIT 15
#define MRB_ENV_SVAR_P(e) MRB_FLAG_CHECK((e)->flags, MRB_ENV_SVAR_BIT)
#define MRB_ENV_SET_SVAR(e) MRB_FLAG_ON((e)->flags, MRB_ENV_SVAR_BIT)
#define MRB_ENV_CLEAR_SVAR(e) MRB_FLAG_OFF((e)->flags, MRB_ENV_SVAR_BIT)
#define MRB_ENV_SVAR_STACK_SIZE(len) (sizeof(mrb_value) * ((size_t)(len) + 1))
#define MRB_ENV_SVAR_SLOT(stack, len) ((stack)[(len)])
struct RProc *mrb_closure_new(mrb_state*, const mrb_irep*);
void mrb_proc_copy(mrb_state *mrb, struct RProc *a, const struct RProc *b);
mrb_int mrb_proc_arity(const struct RProc *p);
struct REnv *mrb_env_new(mrb_state *mrb, struct mrb_context *c, mrb_callinfo *ci, int nstacks, mrb_value *stack, struct RClass *tc);
mrb_bool mrb_env_detach(mrb_state *mrb, struct REnv *e, struct RBasic *sv, mrb_bool noraise);
void mrb_env_detach_all(mrb_state *mrb, struct mrb_context *c, mrb_bool resolve);
struct RBasic *mrb_svar_frame_container(struct mrb_context *c, mrb_callinfo *ci);
void mrb_proc_merge_lvar(mrb_state *mrb, mrb_irep *irep, struct REnv *env, int num, const mrb_sym *lv, const mrb_value *stack);
mrb_value mrb_proc_local_variables(mrb_state *mrb, const struct RProc *proc);
/* The env of `ci`'s frame, made if the frame has none; NULL where the frame
is a cfunc's and has no locals to keep. A scope that has to outlive its
frame asks for one. */
struct REnv *mrb_vm_ci_env_reify(mrb_state *mrb, struct mrb_context *c, mrb_callinfo *ci);
const struct RProc *mrb_proc_get_caller(mrb_state *mrb, struct REnv **env);
mrb_value mrb_proc_get_self(mrb_state *mrb, const struct RProc *p, struct RClass **target_class_p);
mrb_bool mrb_proc_eql(mrb_state *mrb, mrb_value self, mrb_value other);
#endif
/* range */
#ifdef MRUBY_RANGE_H
mrb_value mrb_get_values_at(mrb_state *mrb, mrb_value obj, mrb_int olen, mrb_int argc, const mrb_value *argv, mrb_value (*func)(mrb_state*, mrb_value, mrb_int));
size_t mrb_gc_mark_range(mrb_state *mrb, struct RRange *r);
#endif
/* string */
/*
* Converts `obj` to a string.
* The difference from `mrb_obj_as_string()` is that, if there is no
* mruby's built-in function to convert the object to a string, it returns
* `mrb_undef_value()` instead of calling the `#to_s` method.
*/
mrb_value mrb_obj_as_string_nomethod(mrb_state *mrb, mrb_value obj);
/* Writing what a string's bytes are read as, and what reading them came back
with. mruby/string.h hands both fields back to anyone who asks, since what
they hold is a fact about the string and reading a fact costs the string
nothing. Writing one is the other thing: it is a claim, and a claim the
bytes do not support is caught nowhere. A wrong encoding index has the bytes
read as something they are not, and a string wrongly saying it reads whole
and sound walks straight through the check a regexp makes of its subject. So
the writes are offered where they can be answered for, which is inside the
library, rather than to whoever includes a header.
The values themselves stay in mruby/string.h: naming an answer is reading,
and what reads MRB_STR_CODERANGE_7BIT off a string has to be able to say
it. */
#ifdef MRB_UTF8_STRING
/* An answer is masked to the field's width on the way in, as an encoding index
is, so a fifth one lands wrong rather than reaching the bits beside it. Here
those bits are the encoding index rather than free ones, so an unmasked
write would not merely be a wrong answer: it would have the bytes read as
another encoding. What is written is one of the four either way, spelled
outright or read back out of another string's field, so nothing is left of
this at -O3. */
# define RSTR_CODERANGE_SET(s, cr) \
((s)->flags = ((s)->flags & ~MRB_STR_CODERANGE_MASK) | \
(((cr) & ((1 << MRB_STR_CODERANGE_BITS) - 1)) << MRB_STR_CODERANGE_SHIFT))
#else
/* A build that indexes by byte hands every byte back as a character and asks
the bytes nothing, so every string in it stands where 7BIT stands and there
is nothing to record. */
# define RSTR_CODERANGE_SET(s, cr) ((void)0)
#endif
/* The index is masked to the width of the field it goes into, so an index the
field is too narrow for lands wrong rather than reaching the bits beside it.
Widening MRB_STR_ENCODING_BITS is what a build carrying that many encodings
needs; until then this keeps the mistake where it can be seen. Both operands
are constants at every call, so nothing is left of this at -O3. */
#define RSTR_ENCODING_SET(s, e) \
((s)->flags = ((s)->flags & ~MRB_STR_ENCODING_MASK) | \
(((e) & ((1 << MRB_STR_ENCODING_BITS) - 1)) << MRB_STR_ENCODING_SHIFT))
/* A copy of a string is read the way the string it copies is, so the encoding
travels with the bytes rather than being left behind on the original. */
#define RSTR_ENC_COPY(dst, src) RSTR_ENCODING_SET(dst, RSTR_ENCODING(src))
/* A copy that ends up holding exactly the source's bytes reads them the same
way and stands exactly where the source stands, so the two answers travel
together. Splitting them apart would let a copy keep one and drop the
other, which is the way flags went missing when there was a macro per
flag.
The two fields sit side by side, so one mask spells both and the pair
crosses in a single read and a single write rather than one of each per
field. A build that indexes by byte keeps no coderange and writes those
bits nowhere, so what the mask carries across there is the zeros they
hold. */
#define MRB_STR_ENC_CR_MASK (MRB_STR_ENCODING_MASK|MRB_STR_CODERANGE_MASK)
#define RSTR_ENC_CR_COPY(dst, src) \
((dst)->flags = ((dst)->flags & ~MRB_STR_ENC_CR_MASK) | \
((src)->flags & MRB_STR_ENC_CR_MASK))
/* A subrange holds bytes of the source, so it is read the same way, but a cut
can leave a character in pieces and can also cut away the piece that spelled
none: it inherits neither soundness nor brokenness. Nothing but ASCII is
what survives being cut anywhere, so that is the one answer it carries
over. */
#define RSTR_ENC_CR_COPY_FOR_SUBSTR(dst, src) \
(RSTR_ENC_COPY(dst, src), \
RSTR_CODERANGE_SET(dst, (RSTR_CODERANGE(src) == MRB_STR_CODERANGE_7BIT) \
? MRB_STR_CODERANGE_7BIT : MRB_STR_CODERANGE_UNKNOWN))
void mrb_gc_free_str(mrb_state*, struct RString*);
uint32_t mrb_str_hash(mrb_state *mrb, mrb_value str);
mrb_value mrb_str_dump(mrb_state *mrb, mrb_value str);
mrb_value mrb_str_inspect(mrb_state *mrb, mrb_value str);
mrb_bool mrb_str_beg_len(mrb_int str_len, mrb_int *begp, mrb_int *lenp);
mrb_value mrb_str_aref(mrb_state *mrb, mrb_value str, mrb_value idx, mrb_value len);
void mrb_str_aset(mrb_state *mrb, mrb_value str, mrb_value idx, mrb_value len, mrb_value replace);
/* mrb_str_modify() for a caller whose write leaves what the bytes read as
standing: it puts ASCII where ASCII stood, or it cuts where a character
ends. The next asker is then spared the walk that would arrive at the
answer the string already carries. The promise cannot be checked from
inside, which is why this is here rather than in mruby/string.h: a caller
that cannot answer for its write wants mrb_str_modify(). */
#ifdef MRB_UTF8_STRING
/* See the definition in string.c for what a string read as broken is asked
again about. */
void mrb_str_modify_keep_cr(mrb_state *mrb, struct RString *s);
#else
/* A build that indexes by byte records nothing about the bytes, so there is
nothing here to keep and this is mrb_str_modify() itself. Naming it rather
than compiling a second body of it leaves such a build the size it was,
while the callers that reach both builds go on spelling the promise they
make. */
#define mrb_str_modify_keep_cr(mrb, s) mrb_str_modify(mrb, s)
#endif
mrb_bool mrb_strcasecmp_p(const char *s1, mrb_int len1, const char *s2, mrb_int len2);
#define MRB_STR_CASECMP_P(str, lit) \
mrb_strcasecmp_p(RSTRING_PTR(str), RSTRING_LEN(str), lit, sizeof(lit"")-1)
#ifdef _WIN32
/* The mrb_malloc() half of mrb_mbs_to_wcs()/mrb_wcs_to_mbs(), for a caller
that holds an mrb_state and frees with mrb_free(). Allocation failure is
raised rather than returned, and a collection may run inside the call; a
conversion that the code page refuses is still the -1 the public pair
answers with. */
int mrb_mbs_to_wcs_m(mrb_state *mrb, const char *mbsp, int len, wchar_t **out,
uint32_t from_cp, uint32_t flags);
int mrb_wcs_to_mbs_m(mrb_state *mrb, const wchar_t *wcsp, int len, char **out,
uint32_t to_cp, uint32_t flags);
#endif
uint32_t mrb_byte_hash(const uint8_t*, mrb_int);
uint32_t mrb_byte_hash_step(const uint8_t*, mrb_int, uint32_t);
/* Character count and character-index/byte-offset conversion, honoring the
string's own indexing (single-byte and binary strings index by byte, so
their character count is the byte length). mrb_str_char_to_byte returns
the byte length of `nchars` characters starting at byte offset `off`; when
the string ends before `nchars` characters, the remaining byte length plus
one is returned so out-of-range requests stay detectable. mrb_str_byte_to_char
returns the character index for byte offset `bi` counted from the start of
the string, or -1 when `bi` is outside the string or inside a multi-byte
character. On non-UTF-8 builds a byte is a character, so the conversions are
identity within the string, and mrb_str_byte_to_char still rejects an offset
outside it. */
mrb_int mrb_str_char_len(mrb_state *mrb, mrb_value str);
mrb_int mrb_str_char_to_byte(mrb_state *mrb, mrb_value str, mrb_int off, mrb_int nchars);
mrb_int mrb_str_byte_to_char(mrb_state *mrb, mrb_value str, mrb_int bi);
/* Whether a string's bytes read as the encoding it is taken to have: FALSE for
one holding a byte that stands for no character, TRUE for a binary string
whatever its bytes are, and TRUE throughout on a non-UTF-8 build. See the
definition in string.c for what it reads and what it leaves behind. */
mrb_bool mrb_str_valid_encoding_p(mrb_state *mrb, mrb_value str);
#ifdef MRB_UTF8_STRING
/* What RSTR_SINGLE_BYTE_P() reads, asking the bytes where the string does not
say rather than answering no for one nothing has read yet. See the
definition in string.c for what it leaves behind.
Only a build that reads its strings as characters has anything to tell a
single-byte string from, so a build indexing by byte carries no answer here
rather than one saying TRUE of every string. */
mrb_bool mrb_str_single_byte_p(mrb_state *mrb, mrb_value str);
#endif
/* Raise IndexError when `pos` lands inside a character of `str`, and return
otherwise. See the definition in string.c for which offsets are positions
the string has; a build without MRB_UTF8_STRING has one per byte, so this
is a no-op there. */
void mrb_str_check_byte_pos(mrb_state *mrb, mrb_value str, mrb_int pos);
/* Write the UTF-8 spelling of a codepoint into a buffer of at least four
bytes, and return how many it took (1-4), or 0 for a value that spells no
character. What counts as one, and why a surrogate does spell one here
while mrb_utf8len() says it does not, is in the definition in string.c.
A build that indexes by byte has one writer, mruby-regexp, which spells a
named codepoint as the bytes it matches; a byte build without the gem
carries none of it. */
#if defined(MRB_UTF8_STRING) || defined(HAVE_MRUBY_REGEXP_GEM)
mrb_int mrb_utf8_to_buf(char *buf, mrb_int cp);
#endif
/* UTF-8: what a run of bytes spells. A build that indexes strings by
character reads through this on every character, so it is compiled
wherever that build compiles. A build that indexes by byte has one
reader, unpack("U") in mruby-pack, which reads UTF-8 whatever the build
indexes by: the gem defines HAVE_MRUBY_PACK_GEM for its own files alone,
so that build compiles the scan there and nowhere else, and a byte
build without the gem compiles none of it. What a caller reading a
string whatever the build encodes it in asks is mrb_enc_* below. */
#if defined(MRB_UTF8_STRING) || defined(HAVE_MRUBY_PACK_GEM)
/* What the sequence at `p`, which has to be a byte of the string rather than
`e` itself, has the form of. Which forms spell a character is a view the
scan keeps none of: it reads the length the lead byte claims, asks that
every byte after the lead continue it, and holds the byte after the lead
inside the bounds the caller's table gives for that lead. Everything a
view has to say about a sequence shows in those two bytes, so the table
is the view, one pair {lo, hi} per lead byte from 0xC0 that admits no
byte outside the continuation bytes 0x80-0xBF, and the answer is
n > 0 the shortest spelling, over n (1-4) bytes, of the
value stored through `uv`
MRB_UTF8_MALFORMED a byte that leads nothing, or a byte inside the
claimed length that is no continuation byte
MRB_UTF8_TRUNCATED fewer bytes before `e` than the lead byte claims
MRB_UTF8_REDUNDANT a second byte below the table's lower bound, which
a table set at the floor of each length makes the
form of a value that has a shorter spelling
MRB_UTF8_EXCLUDED a second byte above the table's upper bound
MRB_UTF8_LONG a lead byte claiming five or six bytes, the lengths
RFC 3629 withdrew, which a reader that admits them
reads for itself
Nothing is stored through `uv` for a negative answer, and a caller that
wants the form alone passes NULL for it. The tables sit above: string.c
holds RFC 3629's for mrb_utf8len() below, and mruby-pack holds
unpack("U")'s. The scan is inline so that each reader takes it with its
table folded in, and one that counts characters pays nothing for the
value it never asked for. */
#define MRB_UTF8_MALFORMED (-1)
#define MRB_UTF8_TRUNCATED (-2)
#define MRB_UTF8_REDUNDANT (-3)
#define MRB_UTF8_EXCLUDED (-4)
#define MRB_UTF8_LONG (-5)
static inline mrb_int
mrb_utf8_scan(const char *p, const char *e, uint32_t *uv, const uint8_t bounds[][2])
{
/* the byte length a lead byte claims, by its top five bits; the last entry
folds 0xF8-0xFF together and they are told apart below. The table lives
in the function, as the lookup tables of boxing_word.h do, so that a
translation unit that never reads through the scan emits neither, at
any optimization level. */
static const uint8_t lead[32] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 0
};
const unsigned char *q = (const unsigned char*)p;
unsigned char c = q[0];
mrb_int n = lead[c >> 3];
if (n <= 1) {
if (n == 1) {
if (uv) *uv = c;
return 1;
}
/* a continuation byte leads nothing; 0xF8-0xFD claim five or six,
the lengths RFC 3629 withdrew, which a reader that wants them reads
for itself */
return (c < 0xF8 || c >= 0xFE) ? MRB_UTF8_MALFORMED : MRB_UTF8_LONG;
}
if (n > e - p) return MRB_UTF8_TRUNCATED;
/* Each byte after the lead has to continue it. A second byte inside its
bounds has, since the table admits nothing else, so only one outside
them walks the sequence to tell a malformed one from a bounded one. The
later checks nest in the order the bytes come, so a length costs the
compares up to its own and none past it. Read as a signed char, a
continuation byte is one below -0x40, a single compare where the mask
takes three. A caller that wants the form alone passes no `uv`, and the
value is dead code the compiler drops. */
const uint8_t *b = bounds[c - 0xC0];
if (q[1] < b[0] || q[1] > b[1]) {
for (mrb_int i = 1; i < n; i++) {
if ((signed char)q[i] >= -0x40) return MRB_UTF8_MALFORMED;
}
return q[1] < b[0] ? MRB_UTF8_REDUNDANT : MRB_UTF8_EXCLUDED;
}
uint32_t v = ((c & (0x7F >> n)) << 6) | (q[1] & 0x3F);
if (n > 2) {
if ((signed char)q[2] >= -0x40) return MRB_UTF8_MALFORMED;
v = (v << 6) | (q[2] & 0x3F);
if (n > 3) {
if ((signed char)q[3] >= -0x40) return MRB_UTF8_MALFORMED;
v = (v << 6) | (q[3] & 0x3F);
}
}
if (uv) *uv = v;
return n;
}
#endif
/* How many characters a string holds, and where each one is. Only a build
that indexes strings by character has to answer, so a build without
MRB_UTF8_STRING carries none of them. */
#ifdef MRB_UTF8_STRING
/* The byte length of the character at `str`, which has to be a byte of the
string rather than `end` itself, and 1 for a run of bytes that spells no
character. See the definition in string.c for what it rejects. */
mrb_int mrb_utf8len(const char *str, const char *end);
/* The byte the character covering `p` starts at, or `p` itself when `p` is
already a character boundary. A continuation byte that no lead byte reaches
is a boundary too. */
const char *mrb_utf8_char_head(const char *beg, const char *p, const char *end);
/* The codepoint of the character at `p`, which has to be a byte of the string
rather than `e` itself, with the byte length consumed always stored through
`lenp`. A run of bytes that spells no character comes back as its first
byte over one byte, so a value of 0x80 or above beside *lenp == 1 marks an
invalid sequence; whether that is an error is the caller's question. */
uint32_t mrb_utf8_decode(const char *p, const char *e, mrb_int *lenp);
mrb_int mrb_utf8_strlen(const char *str, mrb_int byte_len);
#endif
#ifdef HAVE_MRUBY_REGEXP_GEM
/* Whether more than one byte can spell one character in what this build
reads. The three functions below answer what a given run of bytes spells,
which is what a reader wants; this is for the few places that have to know
the shape of the answer before they have bytes to ask about, such as
whether a set of single characters can hold a named codepoint at all. */
#ifdef MRB_UTF8_STRING
# define MRB_ENC_MULTIBYTE_P 1
#else
# define MRB_ENC_MULTIBYTE_P 0
#endif
/* What a run of bytes spells, in whatever a build's strings are encoded in.
These are the three above where the build reads UTF-8, and one byte per
character where it does not, which is what a String is there. Anything that
has to read a string whatever the build indexes it by asks through these,
so that adding a codec is a change here rather than in every caller. The
spelling of a codepoint has no such answer and stays UTF-8: see
mrb_utf8_to_buf() above.
The byte-per-character answers are inline because a matcher asks them once
per byte; where the build reads bytes each call folds into the constant it
returns and the branch around it goes away. */
static inline mrb_int
mrb_enc_charlen(const char *p, const char *e)
{
#ifdef MRB_UTF8_STRING
return mrb_utf8len(p, e);
#else
(void)p; (void)e;
return 1;
#endif
}
static inline const char *
mrb_enc_char_head(const char *beg, const char *p, const char *end)
{
#ifdef MRB_UTF8_STRING
return mrb_utf8_char_head(beg, p, end);
#else
(void)beg; (void)end;
return p; /* every byte starts a character of its own */
#endif
}
static inline uint32_t
mrb_enc_decode(const char *p, const char *e, mrb_int *lenp)
{
#ifdef MRB_UTF8_STRING
return mrb_utf8_decode(p, e, lenp);
#else
(void)e;
*lenp = 1;
return (uint8_t)*p;
#endif
}
#endif /* HAVE_MRUBY_REGEXP_GEM */
/* What a case conversion makes of each character. `capitalize` asks two things
of one string, title case at the front and lower case behind it, and `swap`
asks per character, so a mode is what a method does rather than one case. */
enum mrb_case_mode {
MRB_CASE_DOWN,
MRB_CASE_UP,
MRB_CASE_CAPITALIZE,
MRB_CASE_SWAP,
/* Case folding, which is what two strings are compared under rather than
something a method hands back: it spells "ß" as "ss" so that the two
compare equal, which is no lower case of anything. */
MRB_CASE_FOLD
};
/* Convert every character of `str` in place where Unicode has something to say
about it, answering 1 if any character changed, 0 if none did, and -1 for a
string this walk is not the one to convert: nothing but ASCII, read as bytes,
or empty. A caller takes -1 as "the ASCII loop I have is the whole answer",
which is what every build without the tables answers to every string.
`swapcase` lives in mruby-string-ext and reaches the tables through this, so
they are asked about in one place.
The walk raises rather than answering in two cases: `ArgumentError` for a
run of bytes that spells no character, which is what CRuby answers for the
same input, and `FrozenError` for a frozen receiver it would convert, raised
before anything is read. A string it answers -1 for is not looked at for
freezing: the caller's own loop is the one that writes it, and the one that
refuses it. A refused conversion leaves the receiver as it was: the answer
is built beside the string and taken only at the end. */
#if defined(MRB_UTF8_STRING) && !defined(MRB_USE_ASCII_CTYPE)
int mrb_str_case_convert_unicode(mrb_state *mrb, mrb_value str, enum mrb_case_mode mode);
#else
#define mrb_str_case_convert_unicode(mrb, str, mode) (-1)
#endif
#if defined(MRB_UTF8_STRING) && !defined(MRB_USE_ASCII_CTYPE)
/* What case a character has, from the tables in unicase.c. A string is
converted through mrb_str_case_convert_unicode() above; these are for a
caller holding a codepoint rather than a string, which is mruby-regexp
under /i. A build converting case by ASCII compiles none of this, the
table under it being what it asked to leave behind, so a caller reaching
for one of these there is a compile error rather than a link one. */
/* Which table a character is looked up in. The last three hold a difference
rather than a mapping: title case against upper case, swapping against the
rule that a character with a lower case swaps down, and folding against the
lowercase mapping. */
enum mrb_case_kind {
MRB_CASE_KIND_LOWER,
MRB_CASE_KIND_UPPER,
MRB_CASE_KIND_TITLE,
MRB_CASE_KIND_SWAP,
MRB_CASE_KIND_FOLD
};
/* The buffer mrb_uni_case_map() writes into. A mapping may spell several
characters, so this is wider than one of them; unicase.c asserts that the
table it carries fits. */
#define MRB_UNI_CASE_MAX_BYTES 8
/* The `kind` mapping of `cp`, written into `buf` as UTF-8, answering how many
bytes it took, or 0 for a character that maps to itself. */
mrb_int mrb_uni_case_map(enum mrb_case_kind kind, uint32_t cp, char *buf);
#ifdef HAVE_MRUBY_REGEXP_GEM
/* The four below are the foldings /i reads off the same table, in the two
directions a pattern needs them. A build without mruby-regexp has nothing
that reads them, so a caller reaching for one there is a compile error
rather than a link one. */
/* Simple case folding: the folded codepoint, or cp itself when it folds to
nothing else. A codepoint whose folding spells several characters (U+FB00
to "ff") folds to itself here, which is what makes this the simple folding
rather than the full one mrb_uni_case_map() answers with. */
uint32_t mrb_uni_case_fold(uint32_t cp);
/* At most this many codepoints share one folded form. */
#define MRB_UNI_MAX_UNFOLD 4
/* Write every other codepoint sharing cp's folded form into out, at most max
of them, and answer how many were written. */
int mrb_uni_case_unfold(uint32_t cp, uint32_t *out, int max);
/* The same two directions over a span rather than one codepoint, reporting
what they find by calling add() with each span of it: fold_range the folds
of the sources in [lo, hi], unfold_range the sources of the folds in
[lo, hi]. Spans may repeat or overlap what the caller already holds; the
caller merges. */
void mrb_uni_case_fold_range(uint32_t lo, uint32_t hi,
void (*add)(void *, uint32_t, uint32_t), void *user);
void mrb_uni_case_unfold_range(uint32_t lo, uint32_t hi,
void (*add)(void *, uint32_t, uint32_t), void *user);
#endif /* HAVE_MRUBY_REGEXP_GEM */
#endif /* MRB_UTF8_STRING && !MRB_USE_ASCII_CTYPE */
/* attr accessor bodies (class.c); the VM compares function pointers against
these to run attr calls without a full method-call frame */
mrb_value mrb_attr_reader(mrb_state *mrb, mrb_value obj);
mrb_value mrb_attr_writer(mrb_state *mrb, mrb_value obj);
/* symbol */
/* How many symbols are preallocated, the largest of their numbers; a
dynamic symbol is numbered from there. The count is a macro of the presym
table header, which symbol.c alone includes (see there). */
mrb_sym mrb_presym_max(void);
/* variable */
mrb_value mrb_vm_special_get(mrb_state*, mrb_sym);
void mrb_vm_special_set(mrb_state*, mrb_sym, mrb_value);
mrb_value mrb_vm_cv_get(mrb_state*, mrb_sym);
void mrb_vm_cv_set(mrb_state*, mrb_sym, mrb_value);
mrb_value mrb_vm_const_get(mrb_state*, mrb_sym, mrb_bool *cachep);
mrb_bool mrb_vm_const_defined_p(mrb_state *mrb, mrb_callinfo *ci, mrb_sym sym);
mrb_value mrb_vm_const_get_noraise(mrb_state *mrb, mrb_callinfo *ci, mrb_sym sym, mrb_bool load);
mrb_value mrb_const_get_noraise(mrb_state *mrb, struct RClass *mod, mrb_sym sym, mrb_bool load);
mrb_value mrb_vm_const_get_path(mrb_state *mrb, mrb_value mod, mrb_sym sym);
/* the marks Module#private_constant and #deprecate_constant leave */
#define MRB_CONST_PRIVATE 1
#define MRB_CONST_DEPRECATED 2
int mrb_const_flags(mrb_state *mrb, struct RClass *c, mrb_sym sym);
void mrb_const_flags_set(mrb_state *mrb, struct RClass *c, mrb_sym sym, int flag, mrb_bool on);
void mrb_const_flags_copy(mrb_state *mrb, struct RClass *dst, struct RClass *src);
mrb_bool mrb_const_lookup_at(mrb_state *mrb, struct RClass *c, mrb_sym sym, mrb_value *vp, mrb_bool load);
void mrb_autoload_define(mrb_state *mrb, struct RClass *c, mrb_sym sym, mrb_value path);
mrb_value mrb_autoload_path(mrb_state *mrb, struct RClass *c, mrb_sym sym, mrb_bool inherit);
mrb_bool mrb_autoload_p(mrb_value v);
mrb_bool mrb_vm_cv_defined_p(mrb_state *mrb, const struct RProc *proc, mrb_sym sym);
struct RClass *mrb_vm_cref_class(mrb_state *mrb, mrb_callinfo *ci);
struct RClass *mrb_vm_definee_class(mrb_state *mrb, mrb_callinfo *ci);
#ifndef MRB_NO_CONST_CACHE
void mrb_const_cache_forget_irep(mrb_state *mrb, const struct mrb_irep *irep);
#else
#define mrb_const_cache_forget_irep(mrb, irep) ((void)0)
#endif
struct RProc *mrb_method_proc_new(mrb_state *mrb, const mrb_irep *irep);
mrb_bool mrb_gv_defined(mrb_state *mrb, mrb_sym sym);
#ifdef MRUBY_VARIABLE_H
void mrb_gv_foreach(mrb_state *mrb, mrb_iv_foreach_func *func, void *p);
#endif
size_t mrb_obj_iv_tbl_memsize(mrb_value);
void mrb_obj_iv_set_force(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v);
mrb_value mrb_mod_constants(mrb_state *mrb, mrb_value mod);
mrb_value mrb_mod_const_at(mrb_state *mrb, struct RClass *c, mrb_value ary);
mrb_value mrb_f_global_variables(mrb_state *mrb, mrb_value self);
mrb_value mrb_obj_instance_variables(mrb_state*, mrb_value);
mrb_value mrb_mod_class_variables(mrb_state*, mrb_value);
mrb_value mrb_mod_cv_get(mrb_state *mrb, struct RClass *c, mrb_sym sym);
mrb_bool mrb_mod_cv_defined(mrb_state *mrb, struct RClass *c, mrb_sym sym);
mrb_bool mrb_ident_p(const char *s, mrb_int len);
mrb_value mrb_exc_const_get(mrb_state *mrb, mrb_sym sym);
/* GC functions */
void mrb_gc_mark_gv(mrb_state*);
/* A String, Array, Struct, Set, Fiber, Proc or inline struct has no iv slot in
its struct, so its instance variables live in a side table keyed by the
object (variable.c). The table exists only once such an object has been
given an ivar. */
static inline mrb_bool
mrb_obj_iv_slot_p(enum mrb_vtype tt)
{
switch (tt) {
case MRB_TT_OBJECT:
case MRB_TT_CLASS:
case MRB_TT_MODULE:
case MRB_TT_SCLASS:
case MRB_TT_HASH:
case MRB_TT_CDATA:
case MRB_TT_EXCEPTION:
return TRUE;
default:
return FALSE;
}
}
/* Set on an object with an entry in the side table, so that nothing else has
to look it up. A Proc uses every flag bit, this one included (see
MRB_PROC_CASPEC_MASK), so Procs are looked up, but only once a Proc has been
given an ivar (mrb_gc.giv_proc). */
#define MRB_FL_HAS_GIV (1 << 19)
static inline mrb_bool
mrb_gc_giv_p(mrb_state *mrb, struct RBasic *obj)
{
if (!mrb->giv) return FALSE;
switch (obj->tt) {
case MRB_TT_STRING:
case MRB_TT_ARRAY:
case MRB_TT_STRUCT:
case MRB_TT_SET:
case MRB_TT_FIBER:
case MRB_TT_ISTRUCT:
return MRB_FLAG_TEST(obj, MRB_FL_HAS_GIV) != 0;
case MRB_TT_PROC:
return mrb->gc.giv_proc;
default:
return FALSE;
}
}
size_t mrb_gc_mark_giv(mrb_state *mrb, struct RBasic *obj);
void mrb_gc_free_giv(mrb_state *mrb, struct RBasic *obj);
void mrb_gc_free_giv_all(mrb_state *mrb);
void mrb_gc_free_gv(mrb_state*);
size_t mrb_gc_mark_iv(mrb_state*, struct RObject*);
void mrb_gc_free_iv(mrb_state*, struct RObject*);
/* IV shape tree */
void mrb_init_shape(mrb_state*);
void mrb_free_shape(mrb_state*);
/*
* Object Shape (Hidden Class) structures.
*
* A shape describes the IV layout of an object: which syms are stored
* at which indices. Shapes form a tree rooted at the empty root shape.
* Each child adds one IV (its "edge" sym). Objects sharing the same
* set of IVs (assigned in the same order) share the same shape,
* eliminating per-object key storage.
*
* Only MRB_TT_OBJECT instances are shaped (see MRB_OBJ_SHAPED_P); RClass,
* RHash, etc. keep a traditional iv_tbl. Defined here (not variable.c) so
* the VM can inline the shaped fast path of OP_GETIV/OP_SETIV.
*/
/* Maximum IV count before de-shaping to iv_tbl */
#define MRB_SHAPE_MAX_IVS 128
/* Shape descriptor -- shared across objects with same IV layout */
typedef struct mrb_iv_shape {
struct mrb_iv_shape *parent; /* parent shape (one fewer IV) */