Repository navigation
Expand file tree
/
Copy pathproc.c
More file actions
849 lines (758 loc) · 26.8 KB
/
Copy pathproc.c
File metadata and controls
849 lines (758 loc) · 26.8 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
/*
** proc.c - Proc class
**
** See Copyright Notice in mruby.h
*/
#include <mruby.h>
#include <mruby/class.h>
#include <mruby/proc.h>
#include <mruby/opcode.h>
#include <mruby/data.h>
#include <mruby/array.h>
#include <mruby/hash.h>
#include <mruby/internal.h>
static const mrb_code call_iseq[] = {
OP_CALL,
};
static const mrb_irep call_irep = {
0, /* nlocals */
2, /* nregs */
0, /* clen */
MRB_ISEQ_NO_FREE | MRB_IREP_NO_FREE, /* flags */
call_iseq, /* iseq */
NULL, /* pool */
NULL, /* syms */
NULL, /* reps */
NULL, /* lv */
NULL, /* debug_info */
1, /* ilen */
0, /* plen */
0, /* slen */
1, /* rlen */
0, /* refcnt */
};
mrb_alignas(8)
static const struct RProc call_proc = {
NULL, MRB_TT_PROC, MRB_GC_RED, MRB_OBJ_IS_FROZEN, MRB_PROC_SCOPE | MRB_PROC_STRICT,
{ &call_irep }, NULL, { NULL }
};
/* Whether the scope this proc closes over was given the class it runs
under. A scope of its own is where the run ends: MRB_PROC_CREF answers
from its own cref and is asked first. */
static mrb_bool
given_class_env_p(const struct RProc *p)
{
return MRB_PROC_ENV_P(p) && MRB_ENV_GIVEN_CLASS_P(MRB_PROC_ENV(p));
}
/* The class a constant written in the frame's scope is looked up from, which
is that scope's cref. NULL where the chain holds no scope at all, a
method body written in C among them: the caller says what stands in.
A scope keeps its cref on its proc, so a method written `def self.name`
keeps the class around it rather than the singleton class it was
installed in. A block is not a scope of its own and the walk passes over
it, up to the one it was written in. A proc installed as a method is on
the chain and is read that way, which is what keeps a method found in a
subclass from answering with the subclass.
A class given to a block to run under does not enter here. `class_eval`
and its kin name where a `def` goes, not where a constant is read from;
the block keeps looking constants up from the scope it was written in.
An eval string is the other case: it opens a scope of its own, and one
given a class carries that class as its cref, so it is on the chain and
this walk finds it.
A method written in a block given a class carries that class for a `def`
in its body and not as its cref, MRB_PROC_GIVEN says so, and the walk
passes over it to the scope the block was written in. */
struct RClass*
mrb_vm_cref_class(mrb_state *mrb, mrb_callinfo *ci)
{
const struct RProc *p = ci->proc;
while (p && !MRB_PROC_CFUNC_P(p) && (!MRB_PROC_CREF_P(p) || MRB_PROC_GIVEN_P(p))) p = p->upper;
return (p && !MRB_PROC_CFUNC_P(p)) ? MRB_PROC_TARGET_CLASS(p) : NULL;
}
/* The class a `def`, an `alias` or an `undef` written in the frame's scope
adds to. The cref, except where a class was given to the frame to run
under: `class_eval`, `instance_eval`, `Class.new` and `Module.new` leave
one there, and a `def` written in the block goes to it.
The given class is on the frame and nowhere on the `upper` chain, so the
env the giving scope leaves behind is marked instead: a block made there
closes over that env and answers with the class it holds, which is how a
`def` in a block inside the block reaches the same place, and how a string
evaluated over a binding taken there does. A scope of its own ends the
run: a method written in such a block carries the given class itself,
marked MRB_PROC_GIVEN, and a `def` in its body, or in a block made
there, adds to that class as CRuby's does under the cref the block
pushed. */
struct RClass*
mrb_vm_definee_class(mrb_state *mrb, mrb_callinfo *ci)
{
const struct RProc *p = ci->proc;
if (MRB_CI_GIVEN_CLASS_P(ci)) {
return mrb_vm_ci_target_class(ci);
}
while (p && !MRB_PROC_CFUNC_P(p)) {
if (MRB_PROC_CREF_P(p)) return MRB_PROC_TARGET_CLASS(p);
if (given_class_env_p(p)) return MRB_PROC_ENV(p)->c;
p = p->upper;
}
return NULL;
}
#ifdef MRB_USE_REFINEMENTS
/* The refinements active for code running in the frame: the scope the
nearest proc on the chain carries. A scope of its own (MRB_PROC_CREF)
ends the walk, so a method body sees what it copied at its definition and
no `using` written after it; a block carries none and reads on up to the
scope it was written in, so it sees a `using` written after the block, as
CRuby's does. */
struct RArray*
mrb_proc_refinements(mrb_state *mrb, const struct RProc *p)
{
while (p && !MRB_PROC_CFUNC_P(p) && p->gc_color != MRB_GC_RED) {
uint32_t idx = MRB_PROC_REFSCOPE(p);
if (idx) return mrb_refscope_at(mrb, idx);
if (MRB_PROC_CREF_P(p)) break;
p = p->upper;
}
return NULL;
}
struct RArray*
mrb_vm_refinements(mrb_state *mrb, const mrb_callinfo *ci)
{
return mrb_proc_refinements(mrb, ci->proc);
}
/* Gives `p` the refinements active in the frame it is made in, as a method
body or class body copies its scope's at creation. */
static void
proc_copy_refscope(mrb_state *mrb, struct RProc *p, mrb_callinfo *ci)
{
if (mrb->refscopes_len == 0) return;
struct RArray *scope = mrb_vm_refinements(mrb, ci);
if (scope) mrb_proc_set_refscope(mrb, p, scope);
}
#endif
struct RProc*
mrb_proc_new(mrb_state *mrb, const mrb_irep *irep)
{
struct RProc *p;
mrb_callinfo *ci = mrb->c->ci;
p = (struct RProc*)mrb_obj_alloc_core(mrb, MRB_TT_PROC, mrb->proc_class);
if (ci) {
struct RClass *tc = mrb_vm_cref_class(mrb, ci);
if (tc == NULL) {
tc = mrb_vm_ci_target_class(ci);
}
p->upper = ci->proc;
p->e.target_class = tc;
}
if (irep) {
mrb_irep_incref(mrb, (mrb_irep*)irep);
}
p->body.irep = irep;
return p;
}
/* The proc for a method body written in the running frame. Its class is
the frame's cref, the scope the body is written in, except where the
frame was given a class to run under, as a `Class.new`, `class_eval` or
`instance_eval` block is: a `def` in the body then adds to the given
class, as it does in the block, and the proc carries that class marked
MRB_PROC_GIVEN, so that the walks reading the cref pass over it to the
scope around the block. A method written `def self.name` is included:
its body keeps the scope around it, which here is the given class. */
struct RProc*
mrb_method_proc_new(mrb_state *mrb, const mrb_irep *irep)
{
struct RProc *p = mrb_proc_new(mrb, irep);
struct RClass *given = mrb_vm_definee_class(mrb, mrb->c->ci);
/* mrb_proc_new() left the cref in the proc, or the frame's class where
the chain holds no scope; the proc is new and white, so no barrier. */
p->flags |= MRB_PROC_SCOPE | MRB_PROC_CREF;
if (given && given != p->e.target_class) {
p->e.target_class = given;
p->flags |= MRB_PROC_GIVEN;
}
#ifdef MRB_USE_REFINEMENTS
proc_copy_refscope(mrb, p, mrb->c->ci);
#endif
return p;
}
#ifdef MRB_USE_REFINEMENTS
/* The proc for a class or module body: it copies the scope around it. */
struct RProc*
mrb_scope_proc_new(mrb_state *mrb, const mrb_irep *irep)
{
struct RProc *p = mrb_proc_new(mrb, irep);
proc_copy_refscope(mrb, p, mrb->c->ci);
return p;
}
#endif
struct REnv*
mrb_env_new(mrb_state *mrb, struct mrb_context *c, mrb_callinfo *ci, int nstacks, mrb_value *stack, struct RClass *tc)
{
/* A frame with an env is one a collection may have to record what it
carries for (gc_mark_children()), and that recording cannot allocate.
Asked for here, where allocating is allowed. */
mrb_svars_reserve(mrb, c);
struct REnv *e;
mrb_int bidx = 1; /* stack: [self, args..., keywords (if any), block] */
int n = ci->n;
int kw = ci->kw;
e = (struct REnv*)mrb_obj_alloc_core(mrb, MRB_TT_ENV, NULL);
e->c = tc;
MRB_ENV_SET_LEN(e, nstacks);
bidx += (n == 15) ? 1 : n;
bidx += kw;
mrb_assert(bidx >= 1 && bidx <= 16);
MRB_ENV_SET_BIDX(e, bidx);
e->mid = ci->mid;
e->stack = stack;
e->cxt = c;
MRB_ENV_COPY_FLAGS_FROM_CI(e, ci);
/* The frame was given the class it runs under, or it took one from a
scope that was: either way a `def` written here adds to that class, and
so does one written in a block made here, which closes over this env.
A scope of its own does not pass it on; it has a cref to answer with. */
if (tc && ci->proc && !MRB_PROC_CFUNC_P(ci->proc) &&
(MRB_CI_GIVEN_CLASS_P(ci) ||
(!MRB_PROC_CREF_P(ci->proc) && given_class_env_p(ci->proc)))) {
MRB_ENV_SET_GIVEN_CLASS(e);
}
return e;
}
static void
closure_setup(mrb_state *mrb, struct RProc *p)
{
mrb_callinfo *ci = mrb->c->ci;
const struct RProc *up = p->upper;
struct REnv *e = NULL;
mrb_assert(ci != NULL);
if ((e = mrb_vm_ci_env(ci)) != NULL) {
/* do nothing, because e is assigned already */
}
else if (up) {
struct RClass *tc = ci->u.target_class;
if (MRB_PROC_ALIAS_P(up)) { /* alias */
up = up->upper;
}
e = mrb_env_new(mrb, mrb->c, ci, up->body.irep->nlocals, ci->stack, tc);
ci->u.env = e;
if (MRB_PROC_ENV_P(up) && MRB_PROC_ENV(up)->cxt == NULL) {
e->mid = MRB_PROC_ENV(up)->mid;
}
}
if (e) {
p->e.env = e;
p->flags |= MRB_PROC_ENVSET;
mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)e);
}
}
struct RProc*
mrb_closure_new(mrb_state *mrb, const mrb_irep *irep)
{
struct RProc *p = mrb_proc_new(mrb, irep);
closure_setup(mrb, p);
return p;
}
/*
* Creates a new Proc object from a C function.
*
* mrb: The mruby state.
* func: The C function to create the Proc from.
*
* Returns a pointer to the new Proc object.
*/
MRB_API struct RProc*
mrb_proc_new_cfunc(mrb_state *mrb, mrb_func_t func)
{
struct RProc *p;
p = MRB_OBJ_ALLOC(mrb, MRB_TT_PROC, mrb->proc_class);
p->body.func = func;
p->flags |= MRB_PROC_CFUNC_FL;
p->upper = 0;
p->e.target_class = 0;
return p;
}
/*
* Creates a new Proc object from a C function with a new environment.
*
* mrb: The mruby state.
* func: The C function to create the Proc from.
* argc: The number of arguments to be stored in the environment.
* argv: The array of mrb_value to be stored in the environment.
* These values are copied into the environment.
*
* Returns a pointer to the new Proc object.
*/
MRB_API struct RProc*
mrb_proc_new_cfunc_with_env(mrb_state *mrb, mrb_func_t func, mrb_int argc, const mrb_value *argv)
{
struct RProc *p = mrb_proc_new_cfunc(mrb, func);
struct REnv *e;
int i;
p->e.env = e = mrb_env_new(mrb, mrb->c, mrb->c->ci, 0, NULL, NULL);
p->flags |= MRB_PROC_ENVSET;
mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)e);
MRB_ENV_CLOSE(e);
/* A C frame owns no Ruby scope, so this env never carries the
special-variable slot (MRB_ENV_SVAR_P stays off; see internal.h) and
its stack is exactly the locals. */
e->stack = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * argc);
MRB_ENV_SET_LEN(e, argc);
if (argv) {
for (i = 0; i < argc; i++) {
e->stack[i] = argv[i];
}
}
else {
for (i = 0; i < argc; i++) {
SET_NIL_VALUE(e->stack[i]);
}
}
return p;
}
/*
* Creates a new closure from a C function.
*
* This function creates a new Proc object that represents a C function
* along with an environment for a specified number of local variables.
* The local variables in the environment are initialized to nil.
*
* mrb: The mruby state.
* func: The C function to create the closure from.
* nlocals: The number of local variables to allocate space for in the
* closure's environment.
*
* Returns a pointer to the new Proc object representing the closure.
*/
MRB_API struct RProc*
mrb_closure_new_cfunc(mrb_state *mrb, mrb_func_t func, int nlocals)
{
return mrb_proc_new_cfunc_with_env(mrb, func, nlocals, NULL);
}
/*
* Retrieves a value from the environment of the currently executing C function Proc.
*
* This function is used within a C function that was wrapped into a Proc
* (e.g., using mrb_closure_new_cfunc or mrb_proc_new_cfunc_with_env)
* to access values stored in its associated environment.
*
* mrb: The mruby state.
* idx: The index of the value to retrieve from the C function's environment.
* The index must be non-negative and less than the number of
* environment entries (argc passed during creation).
*
* Returns the mrb_value stored at the specified index in the environment.
*
* Raises E_TYPE_ERROR if the currently executing Proc is not a C function
* or if it does not have an associated environment.
* Raises E_INDEX_ERROR if the provided index is out of bounds.
*/
MRB_API mrb_value
mrb_proc_cfunc_env_get(mrb_state *mrb, mrb_int idx)
{
const struct RProc *p = mrb->c->ci->proc;
struct REnv *e;
if (!p || !MRB_PROC_CFUNC_P(p)) {
mrb_raise(mrb, E_TYPE_ERROR, "Can't get cfunc env from non-cfunc proc");
}
e = MRB_PROC_ENV(p);
if (!e) {
mrb_raise(mrb, E_TYPE_ERROR, "Can't get cfunc env from cfunc Proc without REnv");
}
if (idx < 0 || MRB_ENV_LEN(e) <= idx) {
mrb_raisef(mrb, E_INDEX_ERROR, "Env index out of range: %i (expected: 0 <= index < %i)",
idx, MRB_ENV_LEN(e));
}
return e->stack[idx];
}
mrb_value
mrb_proc_get_self(mrb_state *mrb, const struct RProc *p, struct RClass **target_class_p)
{
if (MRB_PROC_CFUNC_P(p)) {
*target_class_p = mrb->object_class;
return mrb_nil_value();
}
else {
struct REnv *e = p->e.env;
if (!e || e->tt != MRB_TT_ENV) {
*target_class_p = mrb->object_class;
return mrb_top_self(mrb);
}
else if (MRB_ENV_LEN(e) < 1) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "self is lost (probably ran out of memory when the block became independent)");
}
*target_class_p = e->c;
return e->stack[0];
}
}
void
mrb_proc_copy(mrb_state *mrb, struct RProc *a, const struct RProc *b)
{
if (a->body.irep) {
/* already initialized proc */
return;
}
/* an alias keeps a method name where a proc keeps its irep */
if (!MRB_PROC_CFUNC_P(b) && !MRB_PROC_ALIAS_P(b) && b->body.irep) {
mrb_irep_incref(mrb, (mrb_irep*)b->body.irep);
}
a->flags = b->flags;
a->body = b->body;
a->upper = b->upper;
a->e.env = b->e.env;
/* a->e.target_class = a->e.target_class; */
}
static mrb_value
mrb_proc_s_new(mrb_state *mrb, mrb_value proc_class)
{
mrb_value blk;
/* Calling Proc.new without a block is not implemented yet */
mrb_get_args(mrb, "&!", &blk);
struct RProc *p = MRB_OBJ_ALLOC(mrb, MRB_TT_PROC, mrb_class_ptr(proc_class));
mrb_proc_copy(mrb, p, mrb_proc_ptr(blk));
mrb_value proc = mrb_obj_value(p);
mrb_funcall_with_block(mrb, proc, MRB_SYM(initialize), 0, NULL, proc);
if (!MRB_PROC_STRICT_P(p) &&
mrb->c->ci > mrb->c->cibase && MRB_PROC_ENV(p) == mrb->c->ci[-1].u.env) {
p->flags |= MRB_PROC_ORPHAN;
}
return proc;
}
static void
check_proc(mrb_state *mrb, mrb_value proc)
{
if (!mrb_proc_p(proc)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "not a proc");
}
}
static mrb_value
mrb_proc_init_copy(mrb_state *mrb, mrb_value self)
{
mrb_value proc = mrb_get_arg1(mrb);
check_proc(mrb, proc);
mrb_proc_copy(mrb, mrb_proc_ptr(self), mrb_proc_ptr(proc));
/* A copied Proc is always treated as an orphan block: it cannot
`break` / `return` from the original yielding method. This is
stricter than CRuby (which only marks the copy orphan once the
original becomes orphan), but matches mruby's memory-first
design — tracking the original via a back pointer would grow
RProc and the GC mark set. See limitations.md for the spec
divergence note. */
mrb_proc_ptr(self)->flags |= MRB_PROC_ORPHAN;
return self;
}
static mrb_value
proc_arity(mrb_state *mrb, mrb_value self)
{
return mrb_int_value(mrb, mrb_proc_arity(mrb_proc_ptr(self)));
}
mrb_bool
mrb_proc_eql(mrb_state *mrb, mrb_value self, mrb_value other)
{
if (mrb_type(self) != MRB_TT_PROC) return FALSE;
if (mrb_type(other) != MRB_TT_PROC) return FALSE;
const struct RProc *p1 = mrb_proc_ptr(self);
const struct RProc *p2 = mrb_proc_ptr(other);
/* Follow alias chains to get the real procs */
while (p1 && MRB_PROC_ALIAS_P(p1)) {
p1 = p1->upper;
}
while (p2 && MRB_PROC_ALIAS_P(p2)) {
p2 = p2->upper;
}
/* If either pointer is NULL after following aliases, they can't be equal */
if (!p1 || !p2) return FALSE;
if (MRB_PROC_CFUNC_P(p1)) {
if (!MRB_PROC_CFUNC_P(p2)) return FALSE;
if (p1->body.func != p2->body.func) return FALSE;
}
else if (MRB_PROC_CFUNC_P(p2)) return FALSE;
else if (p1->body.irep != p2->body.irep) return FALSE;
else if (MRB_PROC_ENV(p1) != MRB_PROC_ENV(p2)) return FALSE;
return TRUE;
}
static mrb_value
proc_eql(mrb_state *mrb, mrb_value self)
{
return mrb_bool_value(mrb_proc_eql(mrb, self, mrb_get_arg1(mrb)));
}
static mrb_value
proc_hash(mrb_state *mrb, mrb_value self)
{
const struct RProc *p = mrb_proc_ptr(self);
return mrb_int_value(mrb, (mrb_int)((intptr_t)p->body.irep^((intptr_t)MRB_PROC_ENV(p)>>2)^MRB_TT_PROC));
}
/* 15.3.1.2.6 */
/* 15.3.1.3.27 */
/*
* call-seq:
* lambda { |...| block } -> a_proc
*
* Equivalent to `Proc.new`, except the resulting Proc objects
* check the number of parameters passed when called.
*/
static mrb_value
proc_lambda(mrb_state *mrb, mrb_value self)
{
mrb_value blk;
mrb_get_args(mrb, "&", &blk);
if (mrb_nil_p(blk)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "tried to create Proc object without a block");
}
check_proc(mrb, blk);
const struct RProc *p = mrb_proc_ptr(blk);
if (!MRB_PROC_STRICT_P(p)) {
struct RProc *p2 = MRB_OBJ_ALLOC(mrb, MRB_TT_PROC, p->c);
mrb_proc_copy(mrb, p2, p);
p2->flags |= MRB_PROC_STRICT;
return mrb_obj_value(p2);
}
return blk;
}
/* the instruction at pc, past an OP_EXT1/2/3 prefix, and in ext the prefix */
static const mrb_code*
insn_ext(const mrb_code *pc, const mrb_code *end, int *ext)
{
*ext = 0;
if (pc < end && (*pc == OP_EXT1 || *pc == OP_EXT2 || *pc == OP_EXT3)) {
*ext = *pc++;
}
return pc < end ? pc : NULL;
}
/* the size of the operands of a BB instruction under the prefix ext */
static int
bb_size(int ext)
{
switch (ext) {
case OP_EXT1: case OP_EXT2: return 3;
case OP_EXT3: return 4;
default: return 2;
}
}
/*
* Whether any keyword parameter of the irep is required. OP_ENTER's aspec
* counts the keywords, not which of them are, so this reads the code that
* follows it, where the compiler sets each keyword in turn: a required one
* with OP_KARG, an optional one with OP_KEY_P, an OP_JMPIF to its OP_KARG,
* and its default between them. The optional positional parameters come
* first, with a jump table whose last OP_JMP, taken when every one of them
* is given, goes past their defaults to the keywords.
*/
static mrb_bool
irep_keyreq_p(const mrb_irep *irep, mrb_aspec aspec)
{
const mrb_code *pc = irep->iseq + 4; /* past OP_ENTER W */
const mrb_code *end = irep->iseq + irep->ilen;
int op = MRB_ASPEC_OPT(aspec);
int ext;
if (op > 0) {
pc += op * 3;
if (pc + 3 > end || *pc != OP_JMP) return FALSE;
pc += 3 + (int16_t)PEEK_S(pc+1);
}
for (int i = 0; i < MRB_ASPEC_KEY(aspec); i++) {
if (pc < irep->iseq || !(pc = insn_ext(pc, end, &ext))) return FALSE;
if (*pc == OP_KARG) return TRUE;
if (*pc != OP_KEY_P) return FALSE;
pc += 1 + bb_size(ext);
/* OP_JMPIF a S, to the keyword's OP_KARG */
if (!(pc = insn_ext(pc, end, &ext)) || *pc != OP_JMPIF) return FALSE;
pc += (ext == OP_EXT1 || ext == OP_EXT3) ? 3 : 2;
if (pc + 2 > end) return FALSE;
pc += 2 + (int16_t)PEEK_S(pc);
if (pc < irep->iseq || !(pc = insn_ext(pc, end, &ext)) || *pc != OP_KARG) return FALSE;
pc += 1 + bb_size(ext);
}
return FALSE;
}
/*
* As CRuby counts: the parameters a call must give, and one more for the
* keywords when any of them is required. That is the arity of a lambda or
* method that takes no more than these, and of a proc that does not take
* any number; otherwise it is -(that + 1).
*/
mrb_int
mrb_proc_arity(const struct RProc *p)
{
const mrb_irep *irep;
const mrb_code *pc;
mrb_aspec aspec;
int ma, op, ra, pa, kw, min;
/* Resolve alias procs first: an alias carries body.mid (a symbol), not an
irep, with `upper` pointing at the original proc. Without this the irep
branch below reads body.mid as an mrb_irep* and dereferences it -> SEGV.
Mirrors the guard already present in mrb_proc_source_location / mrb_proc_eql. */
while (p && MRB_PROC_ALIAS_P(p)) {
p = p->upper;
}
if (!p) return 0;
if (MRB_PROC_CFUNC_P(p)) {
uint32_t caspec_bits = p->flags & MRB_PROC_CASPEC_MASK;
if (caspec_bits != 0) {
aspec = mrb_proc_decompress_caspec(caspec_bits);
ma = MRB_ASPEC_REQ(aspec);
op = MRB_ASPEC_OPT(aspec);
ra = MRB_ASPEC_REST(aspec);
pa = MRB_ASPEC_POST(aspec);
return ra || op ? -(ma + pa + 1) : ma + pa;
}
if (MRB_PROC_NOARG_P(p)) {
return 0;
}
return -1;
}
irep = p->body.irep;
if (!irep) {
return 0;
}
pc = irep->iseq;
/* arity is depend on OP_ENTER */
if (*pc != OP_ENTER) {
return 0;
}
aspec = PEEK_W(pc+1);
ma = MRB_ASPEC_REQ(aspec);
op = MRB_ASPEC_OPT(aspec);
ra = MRB_ASPEC_REST(aspec);
pa = MRB_ASPEC_POST(aspec);
kw = MRB_ASPEC_KEY(aspec) > 0 || MRB_ASPEC_KDICT(aspec);
min = ma + pa;
if (MRB_ASPEC_KEY(aspec) > 0 && irep_keyreq_p(irep, aspec)) min++;
if (ra) return -(min + 1);
if (MRB_PROC_STRICT_P(p) && ma + op + pa + kw != min) return -(min + 1);
return min;
}
mrb_value
mrb_proc_local_variables(mrb_state *mrb, const struct RProc *proc)
{
if (proc == NULL || MRB_PROC_CFUNC_P(proc)) {
return mrb_ary_new(mrb);
}
mrb_value vars = mrb_hash_new(mrb);
while (proc) {
if (MRB_PROC_CFUNC_P(proc)) break;
const mrb_irep *irep = proc->body.irep;
if (irep->lv) {
for (size_t i = 0; i + 1 < irep->nlocals; i++) {
if (irep->lv[i]) {
mrb_sym sym = irep->lv[i];
const char *name = mrb_sym_name(mrb, sym);
switch (name[0]) {
case '*': case '&':
break;
default:
mrb_hash_set(mrb, vars, mrb_symbol_value(sym), mrb_true_value());
break;
}
}
}
}
if (MRB_PROC_LVAR_BOUNDARY_P(proc)) break;
proc = proc->upper;
}
return mrb_hash_keys(mrb, vars);
}
/* The env of `ci`'s frame, made if the frame has none. A frame keeps its
locals on the stack until something has to outlive it; an env is that
something, and this is where a caller that needs one asks for it. */
struct REnv*
mrb_vm_ci_env_reify(mrb_state *mrb, struct mrb_context *c, mrb_callinfo *ci)
{
struct REnv *e = mrb_vm_ci_env(ci);
const struct RProc *proc = ci->proc;
if (e) return e;
if (!proc || MRB_PROC_CFUNC_P(proc)) return NULL;
e = mrb_env_new(mrb, c, ci, proc->body.irep->nlocals, ci->stack,
mrb_vm_ci_target_class(ci));
ci->u.env = e;
return e;
}
const struct RProc *
mrb_proc_get_caller(mrb_state *mrb, struct REnv **envp)
{
struct mrb_context *c = mrb->c;
mrb_callinfo *ci = (c->ci > c->cibase) ? c->ci - 1 : c->cibase;
const struct RProc *proc = ci->proc;
if (!proc || MRB_PROC_CFUNC_P(proc)) {
if (envp) *envp = NULL;
}
else {
if (envp) *envp = mrb_vm_ci_env_reify(mrb, c, ci);
}
return proc;
}
#define IREP_LVAR_MERGE_DEFAULT 50
#define IREP_LVAR_MERGE_MINIMUM 8
#define IREP_LVAR_MERGE_MAXIMUM 240
#ifdef MRB_IREP_LVAR_MERGE_LIMIT
# define IREP_LVAR_MERGE_LIMIT \
((MRB_IREP_LVAR_MERGE_LIMIT) < IREP_LVAR_MERGE_MINIMUM ? IREP_LVAR_MERGE_MINIMUM : \
(MRB_IREP_LVAR_MERGE_LIMIT) > IREP_LVAR_MERGE_MAXIMUM ? IREP_LVAR_MERGE_MAXIMUM : \
(MRB_IREP_LVAR_MERGE_LIMIT))
#else
# define IREP_LVAR_MERGE_LIMIT IREP_LVAR_MERGE_DEFAULT
#endif
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_assert(!(irep->flags & MRB_IREP_NO_FREE));
if ((irep->nlocals + num) > IREP_LVAR_MERGE_LIMIT) {
mrb_raise(mrb, E_RUNTIME_ERROR, "too many local variables for binding (mruby limitation)");
}
if (!lv) {
mrb_raise(mrb, E_RUNTIME_ERROR, "unavailable local variable names");
}
/* Only an env carrying the special-variable slot (see internal.h) has one
to keep: the new locals take the ground it stood on, so it is read off
the old stack and put back past the new ones. An env sized without it
grows by the locals alone and stays without it. */
mrb_bool has_svar = MRB_ENV_SVAR_P(env);
size_t old_len = (size_t)irep->nlocals;
size_t new_len = old_len + (size_t)num;
mrb_value svar = mrb_nil_value();
if (has_svar) svar = MRB_ENV_SVAR_SLOT(env->stack, old_len);
irep->lv = (mrb_sym*)mrb_realloc(mrb, (mrb_sym*)irep->lv, sizeof(mrb_sym) * (irep->nlocals - 1 /* self */ + num));
env->stack = (mrb_value*)mrb_realloc(mrb, env->stack,
has_svar ? MRB_ENV_SVAR_STACK_SIZE(new_len)
: sizeof(mrb_value) * new_len);
if (has_svar) MRB_ENV_SVAR_SLOT(env->stack, new_len) = svar;
mrb_sym *destlv = (mrb_sym*)irep->lv + irep->nlocals - 1 /* self */;
mrb_value *destst = env->stack + irep->nlocals;
memmove(destlv, lv, sizeof(mrb_sym) * num);
if (stack) {
memmove(destst, stack, sizeof(mrb_value) * num);
for (int i = 0; i < num; i++) {
if (!mrb_immediate_p(stack[i])) {
mrb_field_write_barrier(mrb, (struct RBasic*)env, (struct RBasic*)mrb_obj_ptr(stack[i]));
}
}
}
else {
for (int i = num; i > 0; i--, destst++) {
*destst = mrb_nil_value();
}
}
irep->nlocals += num;
irep->nregs = irep->nlocals;
MRB_ENV_SET_LEN(env, irep->nlocals);
}
/* ---------------------------*/
static const mrb_mt_entry proc_rom_entries[] = {
MRB_MT_ENTRY(mrb_proc_init_copy, MRB_SYM(initialize_copy), MRB_ARGS_REQ(1) | MRB_MT_PRIVATE),
MRB_MT_ENTRY(proc_arity, MRB_SYM(arity), MRB_ARGS_NONE()), /* 15.2.17.4.2 */
MRB_MT_ENTRY(proc_eql, MRB_OPSYM(eq), MRB_ARGS_REQ(1)),
MRB_MT_ENTRY(proc_eql, MRB_SYM_Q(eql), MRB_ARGS_REQ(1)),
MRB_MT_ENTRY(proc_hash, MRB_SYM(hash), MRB_ARGS_NONE()), /* 15.2.17.4.2 */
};
void
mrb_init_proc(mrb_state *mrb)
{
mrb_method_t m;
struct RClass *pc = mrb->proc_class = mrb_define_class_id(mrb, MRB_SYM(Proc), mrb->object_class); /* 15.2.17 */
MRB_SET_INSTANCE_TT(pc, MRB_TT_PROC);
MRB_UNDEF_ALLOCATOR(pc);
mrb_define_class_method_id(mrb, pc, MRB_SYM(new), mrb_proc_s_new, MRB_ARGS_NONE()|MRB_ARGS_BLOCK());
MRB_MT_INIT_ROM(mrb, pc, proc_rom_entries);
MRB_METHOD_FROM_PROC(m, &call_proc);
mrb_define_method_raw(mrb, pc, MRB_SYM(call), m); /* 15.2.17.4.3 */
mrb_define_method_raw(mrb, pc, MRB_OPSYM(aref), m); /* 15.2.17.4.1 */
mrb_define_module_function_id(mrb, mrb->kernel_module, MRB_SYM(lambda), proc_lambda, MRB_ARGS_NONE()|MRB_ARGS_BLOCK()); /* 15.3.1.3.27 */
}