Repository navigation
Expand file tree
/
Copy pathkernel.c
More file actions
1082 lines (998 loc) · 35.8 KB
/
Copy pathkernel.c
File metadata and controls
1082 lines (998 loc) · 35.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
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
/*
** kernel.c - Kernel module
**
** See Copyright Notice in mruby.h
*/
#include <mruby.h>
#include <mruby/array.h>
#include <mruby/hash.h>
#include <mruby/class.h>
#include <mruby/proc.h>
#include <mruby/string.h>
#include <mruby/variable.h>
#include <mruby/error.h>
#include <mruby/istruct.h>
#include <mruby/internal.h>
/*
* Checks if the method `mid` for object `obj` is implemented by
* the C function `func`.
*/
MRB_API mrb_bool
mrb_func_basic_p(mrb_state *mrb, mrb_value obj, mrb_sym mid, mrb_func_t func)
{
struct RClass *c = mrb_class(mrb, obj);
mrb_method_t m = mrb_method_search_vm(mrb, &c, mid);
const struct RProc *p;
if (MRB_METHOD_UNDEF_P(m)) return FALSE;
if (MRB_METHOD_FUNC_P(m))
return MRB_METHOD_FUNC(m) == func;
p = MRB_METHOD_PROC(m);
if (MRB_PROC_CFUNC_P(p) && (MRB_PROC_CFUNC(p) == func))
return TRUE;
return FALSE;
}
static mrb_bool
mrb_obj_basic_to_s_p(mrb_state *mrb, mrb_value obj)
{
return mrb_func_basic_p(mrb, obj, MRB_SYM(to_s), mrb_any_to_s);
}
struct inspect_i {
mrb_value obj, str;
};
static int
inspect_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p)
{
struct inspect_i *a = (struct inspect_i*)p;
/* An ivar whose name Ruby cannot spell (set from C without the '@') is
internal state, not part of the object's picture: instance_variables
leaves it out, and so does inspect. */
if (!mrb_iv_name_sym_p(mrb, sym)) return 0;
if (mrb_nil_p(a->str)) {
const char *cn = mrb_obj_classname(mrb, a->obj);
a->str = mrb_str_new_capa(mrb, 30);
/* Each value's inspect runs Ruby, which must not reach the string
through ObjectSpace: it could swap in a shared buffer, and the first
byte below is written in place. mrb_obj_inspect() sets the class back. */
mrb_str_ptr(a->str)->c = NULL; /* hide from ObjectSpace.each_object */
mrb_str_cat_lit(mrb, a->str, "-<");
mrb_str_cat_cstr(mrb, a->str, cn);
mrb_str_cat_lit(mrb, a->str, ":");
mrb_str_cat_str(mrb, a->str, mrb_ptr_to_str(mrb, mrb_obj_ptr(a->obj)));
if (MRB_RECURSIVE_UNARY_P(mrb, MRB_SYM(inspect), a->obj)) {
mrb_str_cat_lit(mrb, a->str, " ...");
return 1;
}
}
char *sp = RSTRING_PTR(a->str);
if (sp[0] == '-') { /* first element */
sp[0] = '#';
mrb_str_cat_lit(mrb, a->str, " ");
}
else {
mrb_str_cat_lit(mrb, a->str, ", ");
}
mrb_int len;
const char *s = mrb_sym_name_len(mrb, sym, &len);
mrb_str_cat(mrb, a->str, s, len);
mrb_str_cat_lit(mrb, a->str, "=");
mrb_value ins = mrb_inspect(mrb, v);
mrb_str_cat_str(mrb, a->str, ins);
return 0;
}
/* 15.3.1.3.17 */
/*
* call-seq:
* obj.inspect -> string
*
* Returns a string containing a human-readable representation of
* *obj*. If not overridden and no instance variables, uses the
* `to_s` method to generate the string.
* *obj*. If not overridden, uses the `to_s` method to
* generate the string.
*
* [ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
* Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
*/
MRB_API mrb_value
mrb_obj_inspect(mrb_state *mrb, mrb_value obj)
{
if (mrb_object_p(obj) && mrb_obj_basic_to_s_p(mrb, obj)) {
struct inspect_i a = { obj, mrb_nil_value() };
mrb_iv_foreach(mrb, obj, inspect_i, &a);
if (!mrb_nil_p(a.str)) {
mrb_assert(mrb_string_p(a.str));
mrb_str_cat_lit(mrb, a.str, ">");
mrb_str_ptr(a.str)->c = mrb->string_class;
return a.str;
}
}
return mrb_any_to_s(mrb, obj);
}
/* 15.3.1.3.2 */
/*
* call-seq:
* obj === other -> true or false
*
* Case Equality---For class `Object`, effectively the same
* as calling `#==`, but typically overridden by descendants
* to provide meaningful semantics in `case` statements.
*/
static mrb_value
mrb_eqq_m(mrb_state *mrb, mrb_value self)
{
mrb_value arg = mrb_get_arg1(mrb);
#ifndef MRB_NO_FLOAT
/* Case equality is `==` for an Object, and a NaN is equal to nothing at all,
itself included. `mrb_equal()` answers a value that holds what the other
one holds before it asks `==` anything, which is the answer a container
searching for the object it was handed wants, and the wrong one here. */
if (mrb_float_p(self) && isnan(mrb_float(self))) return mrb_false_value();
#endif
return mrb_bool_value(mrb_equal(mrb, self, arg));
}
/* 11.4.4 Step c) */
/*
* call-seq:
* obj !~ other -> true or false
*
* Returns true when *obj* does not match *other*, which is what `=~`
* answering a false value means. Overriding `=~` is what gives a class a
* meaning for both operators; overriding this one separately is what makes
* the two disagree, as it does in CRuby.
*/
static mrb_value
mrb_obj_not_match(mrb_state *mrb, mrb_value self)
{
mrb_value arg = mrb_get_arg1(mrb);
/* Dispatching `=~` is the meaning of this method rather than a convenience,
so the VM call belongs here. What comes back is read as a truth value and
then dropped, so nothing has to survive the arena restore, and an exception
raised by `=~` unwinds past this frame with nothing left behind to clean
up. Answering from C is also what keeps `=~` writing `$~` into the scope
that wrote `!~`, the way CRuby's `rb_obj_not_match()` does. */
int ai = mrb_gc_arena_save(mrb);
mrb_bool matched = mrb_test(mrb_funcall_argv(mrb, self, MRB_OPSYM(match), 1, &arg));
mrb_gc_arena_restore(mrb, ai);
return mrb_bool_value(!matched);
}
static mrb_value
mrb_cmp_m(mrb_state *mrb, mrb_value self)
{
mrb_value arg = mrb_get_arg1(mrb);
/* recursion check */
for (mrb_callinfo *ci=&mrb->c->ci[-1]; ci>=mrb->c->cibase; ci--) {
if (ci->mid == MRB_OPSYM(cmp) &&
mrb_obj_eq(mrb, self, ci->stack[0]) &&
mrb_obj_eq(mrb, arg, ci->stack[1])) {
/* recursive <=> calling returns `nil` */
return mrb_nil_value();
}
}
if (mrb_equal(mrb, self, arg))
return mrb_fixnum_value(0);
return mrb_nil_value();
}
MRB_API mrb_bool
mrb_recursive_method_p(mrb_state *mrb, mrb_sym mid, mrb_value obj1, mrb_value obj2)
{
for (mrb_callinfo *ci=&mrb->c->ci[-1]; ci>=mrb->c->cibase; ci--) {
if (ci->mid == mid && mrb_obj_eq(mrb, obj1, ci->stack[0])) {
/* For unary methods, only check first argument */
if (mrb_nil_p(obj2)) return TRUE;
/* For binary methods, check both arguments */
if (mrb_obj_eq(mrb, obj2, ci->stack[1])) return TRUE;
}
}
return FALSE;
}
/**
* Check if a C function call is recursive.
*
* Like mrb_recursive_method_p, but starts from ci[-2] to skip the immediate
* parent frame. Use this from C functions implementing Ruby methods that can
* be overridden with super calls.
*/
MRB_API mrb_bool
mrb_recursive_func_p(mrb_state *mrb, mrb_sym mid, mrb_value obj1, mrb_value obj2)
{
/* Start from ci[-2] to skip immediate parent frame which may be a
Ruby override calling super */
for (mrb_callinfo *ci=&mrb->c->ci[-2]; ci>=mrb->c->cibase; ci--) {
if (ci->mid == mid && mrb_obj_eq(mrb, obj1, ci->stack[0])) {
/* For unary methods, only check first argument */
if (mrb_nil_p(obj2)) return TRUE;
/* For binary methods, check both arguments */
if (mrb_obj_eq(mrb, obj2, ci->stack[1])) return TRUE;
}
}
return FALSE;
}
static mrb_value
mrb_obj_method_recursive_p(mrb_state *mrb, mrb_value obj)
{
mrb_sym mid;
mrb_value arg2 = mrb_nil_value();
mrb_int argc = mrb_get_args(mrb, "n|o", &mid, &arg2);
/* Use frame-skipping version for Ruby method calls */
for (mrb_callinfo *ci=&mrb->c->ci[-2]; ci>=mrb->c->cibase; ci--) {
if (ci->mid == mid && mrb_obj_eq(mrb, obj, ci->stack[0])) {
/* For unary methods, only check first argument */
if (argc == 1 || mrb_nil_p(arg2)) return mrb_true_value();
/* For binary methods, check both arguments */
if (mrb_obj_eq(mrb, arg2, ci->stack[1])) return mrb_true_value();
}
}
return mrb_false_value();
}
/* 15.3.1.3.3 */
/* 15.3.1.3.33 */
/*
* Document-method: __id__
* Document-method: object_id
*
* call-seq:
* obj.__id__ -> int
* obj.object_id -> int
*
* Returns an integer identifier for *obj*. The same number will
* be returned on all calls to `id` for a given object, and
* no two active objects will share an id.
* `Object#object_id` is a different concept from the
* `:name` notation, which returns the symbol id of
* `name`. Replaces the deprecated `Object#id`.
*/
mrb_value
mrb_obj_id_m(mrb_state *mrb, mrb_value self)
{
return mrb_int_value(mrb, mrb_obj_id(self));
}
static int
env_bidx(struct REnv *e)
{
int bidx;
/* use saved block arg position */
bidx = MRB_ENV_BIDX(e);
/* bidx may be useless (e.g. define_method) */
if (bidx >= MRB_ENV_LEN(e)) return -1;
return bidx;
}
/* 15.3.1.2.2 */
/* 15.3.1.2.5 */
/* 15.3.1.3.6 */
/* 15.3.1.3.25 */
/*
* call-seq:
* block_given? -> true or false
* iterator? -> true or false
*
* Returns `true` if `yield` would execute a
* block in the current context. The `iterator?` form
* is mildly deprecated.
*
* def try
* if block_given?
* yield
* else
* "no block"
* end
* end
* try #=> "no block"
* try { "hello" } #=> "hello"
* try do "hello" end #=> "hello"
*/
static mrb_value
mrb_f_block_given_p_m(mrb_state *mrb, mrb_value self)
{
mrb_callinfo *ci = &mrb->c->ci[-1];
mrb_callinfo *cibase = mrb->c->cibase;
mrb_value *bp;
int bidx;
struct REnv *e = NULL;
const struct RProc *p;
if (ci <= cibase) {
/* toplevel does not have block */
return mrb_false_value();
}
p = ci->proc;
/* search method/class/module proc */
while (p) {
if (MRB_PROC_SCOPE_P(p)) break;
e = MRB_PROC_ENV(p);
p = p->upper;
}
if (p == NULL) return mrb_false_value();
if (e) {
bidx = env_bidx(e);
if (bidx < 0) return mrb_false_value();
bp = &e->stack[bidx];
goto block_given;
}
/* search ci corresponding to proc */
while (cibase < ci) {
if (ci->proc == p) break;
ci--;
}
if (ci == cibase) {
/* proc is closure */
if (!MRB_PROC_ENV_P(p)) return mrb_false_value();
e = MRB_PROC_ENV(p);
bidx = env_bidx(e);
if (bidx < 0) return mrb_false_value();
bp = &e->stack[bidx];
}
else if ((e = mrb_vm_ci_env(ci)) != NULL) {
/* top-level does not have block slot (always false) */
if (e->stack == mrb->c->stbase) return mrb_false_value();
bidx = env_bidx(e);
/* bidx may be useless (e.g. define_method) */
if (bidx < 0) return mrb_false_value();
bp = &e->stack[bidx];
}
else {
uint8_t n = ci->n == 15 ? 1 : ci->n;
uint8_t k = ci->kw;
bidx = n + k + 1; /* self + args + kargs => bidx */
bp = &ci->stack[bidx];
}
block_given:
if (mrb_nil_p(*bp))
return mrb_false_value();
return mrb_true_value();
}
/* 15.3.1.3.7 */
/*
* call-seq:
* obj.class -> class
*
* Returns the class of *obj*. This method must always be
* called with an explicit receiver, as `class` is also a
* reserved word in Ruby.
*
* 1.class #=> Integer
* self.class #=> Object
*/
static mrb_value
mrb_obj_class_m(mrb_state *mrb, mrb_value self)
{
return mrb_obj_value(mrb_obj_class(mrb, self));
}
/*
* Freezes the object `self`, preventing further modifications.
* Immediate values cannot be frozen.
*/
MRB_API mrb_value
mrb_obj_freeze(mrb_state *mrb, mrb_value self)
{
if (!mrb_immediate_p(self)) {
struct RBasic *b = mrb_basic_ptr(self);
if (!mrb_frozen_p(b)) {
b->frozen = 1;
if (b->c->tt == MRB_TT_SCLASS) b->c->frozen = 1;
}
}
return self;
}
static mrb_value
mrb_obj_frozen(mrb_state *mrb, mrb_value self)
{
return mrb_bool_value(mrb_immediate_p(self) || mrb_frozen_p(mrb_basic_ptr(self)));
}
/* 15.3.1.3.15 */
/*
* call-seq:
* obj.hash -> int
*
* Generates a `Integer` hash value for this object. This
* function must have the property that `a.eql?(b)` implies
* `a.hash == b.hash`. The hash value is used by class
* `Hash`. Any hash value that exceeds the capacity of a
* `Integer` will be truncated before being used.
*/
static mrb_value
mrb_obj_hash(mrb_state *mrb, mrb_value self)
{
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(self)) {
return mrb_bint_hash(mrb, self);
}
#endif
return mrb_int_value(mrb, mrb_obj_id(self));
}
/* 15.3.1.3.16 */
mrb_value
mrb_obj_init_copy(mrb_state *mrb, mrb_value self)
{
mrb_value orig = mrb_get_arg1(mrb);
if (mrb_obj_equal(mrb, self, orig)) return self;
if ((mrb_type(self) != mrb_type(orig)) || (mrb_obj_class(mrb, self) != mrb_obj_class(mrb, orig))) {
mrb_raise(mrb, E_TYPE_ERROR, "initialize_copy should take same class object");
}
return self;
}
/*
* Checks if the object `obj` is an instance of the class `c`.
*/
MRB_API mrb_bool
mrb_obj_is_instance_of(mrb_state *mrb, mrb_value obj, const struct RClass* c)
{
if (mrb_obj_class(mrb, obj) == c) return TRUE;
return FALSE;
}
/* 15.3.1.3.19 */
/*
* call-seq:
* obj.instance_of?(class) -> true or false
*
* Returns `true` if *obj* is an instance of the given
* class. See also `Object#kind_of?`.
*/
static mrb_value
obj_is_instance_of(mrb_state *mrb, mrb_value self)
{
struct RClass *c;
mrb_get_args(mrb, "c", &c);
return mrb_bool_value(mrb_obj_is_instance_of(mrb, self, c));
}
/* 15.3.1.3.24 */
/* 15.3.1.3.26 */
/*
* call-seq:
* obj.is_a?(class) -> true or false
* obj.kind_of?(class) -> true or false
*
* Returns `true` if *class* is the class of
* *obj*, or if *class* is one of the superclasses of
* *obj* or modules included in *obj*.
*
* module M; end
* class A
* include M
* end
* class B < A; end
* class C < B; end
* b = B.new
* b.instance_of? A #=> false
* b.instance_of? B #=> true
* b.instance_of? C #=> false
* b.instance_of? M #=> false
* b.kind_of? A #=> true
* b.kind_of? B #=> true
* b.kind_of? C #=> false
* b.kind_of? M #=> true
*/
static mrb_value
mrb_obj_is_kind_of_m(mrb_state *mrb, mrb_value self)
{
struct RClass *c;
mrb_get_args(mrb, "c", &c);
return mrb_bool_value(mrb_obj_is_kind_of(mrb, self, c));
}
/* 15.3.1.3.32 */
/*
* call_seq:
* nil.nil? -> true
* <anything_else>.nil? -> false
*
* Only the object *nil* responds `true` to `nil?`.
*/
static mrb_value
mrb_false(mrb_state *mrb, mrb_value self)
{
return mrb_false_value();
}
/* 15.3.1.2.12 */
/* 15.3.1.3.40 */
/*
* call-seq:
* raise
* raise(string)
* raise(exception [, string])
*
* With no arguments, raises a `RuntimeError`
* With a single `String` argument, raises a
* `RuntimeError` with the string as a message. Otherwise,
* the first parameter should be the name of an `Exception`
* class (or an object that returns an `Exception` object when sent
* an `exception` message). The optional second parameter sets the
* message associated with the exception, and the third parameter is an
* array of callback information. Exceptions are caught by the
* `rescue` clause of `begin...end` blocks.
*
* raise "Failed to create socket"
* raise ArgumentError, "No parameters", caller
*
* The exception being handled (`$!`) becomes the cause of the one raised;
* `cause:` gives another, or with nil none.
*
* raise ArgumentError, "bad value", cause: original_error
*/
mrb_value
mrb_f_raise(mrb_state *mrb, mrb_value self)
{
mrb_value exc, mesg;
mrb_sym kw_names[] = { MRB_SYM(cause) };
mrb_value cause = mrb_undef_value(), opts = mrb_nil_value();
mrb_kwargs kw = { 1, 0, kw_names, &cause, &opts };
/* the keyword form allocates a Hash for the rest even when there are none */
mrb_int argc = mrb->c->ci->kw ? mrb_get_args(mrb, "|oo:", &exc, &mesg, &kw)
: mrb_get_args(mrb, "|oo", &exc, &mesg);
/* keywords other than cause: are a Hash argument, as in CRuby */
if (mrb_hash_p(opts) && !mrb_hash_empty_p(mrb, opts)) {
switch (argc) {
case 0: exc = opts; break;
case 1: mesg = opts; break;
default: mrb_argnum_error(mrb, argc + 1, 0, 2);
}
argc++;
}
mrb->c->ci->mid = 0;
switch (argc) {
case 0:
if (!mrb_undef_p(cause)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "only cause is given with no arguments");
}
/* Re-raise what a rescue clause is running, which is what `$!` names.
Outside one it is nil, and there being nothing to re-raise is itself
the RuntimeError. `!` is not a word, so MRB_GVSYM() cannot spell the
name and it is interned here, as `$~` and `$?` are. */
exc = mrb_gv_get(mrb, mrb_intern_lit(mrb, "$!"));
if (!mrb_nil_p(exc)) {
mrb_exc_raise(mrb, exc);
}
mrb_raise(mrb, E_RUNTIME_ERROR, "");
break;
case 1:
if (mrb_string_p(exc)) {
mesg = exc;
exc = mrb_obj_value(E_RUNTIME_ERROR);
}
else {
mesg = mrb_nil_value();
}
/* fall through */
default:
exc = mrb_make_exception(mrb, exc, mesg);
mrb_exc_raise_with_cause(mrb, exc, cause);
break;
}
return mrb_nil_value(); /* not reached */
}
/* 15.3.1.3.41 */
/*
* call-seq:
* obj.remove_instance_variable(symbol) -> obj
*
* Removes the named instance variable from *obj*, returning that
* variable's value.
*
* class Dummy
* attr_reader :var
* def initialize
* @var = 99
* end
* def remove
* remove_instance_variable(:@var)
* end
* end
* d = Dummy.new
* d.var #=> 99
* d.remove #=> 99
* d.var #=> nil
*/
static mrb_value
mrb_obj_remove_instance_variable(mrb_state *mrb, mrb_value self)
{
mrb_sym sym;
mrb_get_args(mrb, "n", &sym);
mrb_iv_name_sym_check(mrb, sym);
mrb_value val = mrb_iv_remove(mrb, self, sym);
if (mrb_undef_p(val)) {
mrb_name_error(mrb, sym, "instance variable %n not defined", sym);
}
return val;
}
/* 15.3.1.3.43 */
/*
* call-seq:
* obj.respond_to?(symbol, include_private=false) -> true or false
*
* Returns `true` if _obj_ responds to the given
* method. Private and protected methods are included in the search
* only if the optional second parameter evaluates to `true`.
*
* If the method is defined but unimplemented on this machine,
* as IO#pread in a build without pread(2), false is returned
* and `respond_to_missing?` is not consulted.
*
* If the method is not defined, or is defined where the second
* parameter does not reach it, `respond_to_missing?` method is
* called and the result is returned.
*/
static mrb_bool
obj_respond_to_p(mrb_state *mrb, mrb_value self, mrb_sym id, mrb_bool priv)
{
struct RClass *c = mrb_class(mrb, self);
#ifdef MRB_USE_REFINEMENTS
mrb_method_t m = mrb_vm_find_method_in_scope(mrb, mrb_vm_caller_refinements(mrb), c, &c, id);
#else
mrb_method_t m = mrb_method_search_vm(mrb, &c, id);
#endif
if (!MRB_METHOD_UNDEF_P(m)) {
/* A method that is unimplemented on this machine answers a plain false,
and leaves `respond_to_missing?` nothing to add. */
if (MRB_METHOD_NOTIMPL_P(m)) return FALSE;
/* A method the caller may not reach is answered as if it were not there,
so `respond_to_missing?` gets its say, as in CRuby. */
if (priv || !(m.flags & (MRB_METHOD_PRIVATE_FL|MRB_METHOD_PROTECTED_FL))) {
return TRUE;
}
}
/* An entry `undef_method` left behind is not a redefinition: it stops the
lookup without standing for one, so it is not asked, as in CRuby. */
mrb_sym rtm_id = MRB_SYM_Q(respond_to_missing);
if (!mrb_func_basic_p(mrb, self, rtm_id, mrb_false) && mrb_respond_to(mrb, self, rtm_id)) {
mrb_value v = mrb_funcall_argv2(mrb, self, rtm_id, mrb_symbol_value(id), mrb_bool_value(priv));
return mrb_bool(v);
}
return FALSE;
}
static mrb_value
obj_respond_to(mrb_state *mrb, mrb_value self)
{
mrb_sym id;
mrb_bool priv = FALSE;
mrb_get_args(mrb, "n|b", &id, &priv);
return mrb_bool_value(obj_respond_to_p(mrb, self, id, priv));
}
static mrb_value
mrb_obj_ceqq(mrb_state *mrb, mrb_value self)
{
mrb_value v = mrb_get_arg1(mrb);
mrb_sym eqq = MRB_OPSYM(eqq);
mrb_value ary;
mrb->c->ci->mid = 0;
if (mrb_array_p(self)) {
ary = self;
}
else if (mrb_nil_p(self)) {
return mrb_false_value();
}
else if (!mrb_respond_to(mrb, self, MRB_SYM(to_a))) {
mrb_value c = mrb_funcall_argv(mrb, self, eqq, 1, &v);
if (mrb_test(c)) return mrb_true_value();
return mrb_false_value();
}
else {
ary = mrb_funcall_argv(mrb, self, MRB_SYM(to_a), 0, NULL);
if (mrb_nil_p(ary)) {
return mrb_funcall_argv(mrb, self, eqq, 1, &v);
}
mrb_ensure_array_type(mrb, ary);
}
/* A pattern's #=== can replace `ary` with a shorter array, which moves the
buffer as well as the length, so the next index has to be checked against
what RARRAY_PTR() now points at. The saved length stays as the upper
bound: a pattern that grows the array does not extend the traversal. */
mrb_int len = RARRAY_LEN(ary);
for (mrb_int i=0; i<len && i<RARRAY_LEN(ary); i++) {
mrb_value c = mrb_funcall_argv(mrb, RARRAY_PTR(ary)[i], eqq, 1, &v);
if (mrb_test(c)) return mrb_true_value();
}
return mrb_false_value();
}
// ISO 15.3.1.2.10 Kernel.print
// ISO 15.3.1.3.35 Kernel#print
mrb_value mrb_print_m(mrb_state *mrb, mrb_value self);
#ifndef HAVE_MRUBY_IO_GEM
// ISO 15.3.1.2.9 Kernel.p
// ISO 15.3.1.3.34 Kernel#p
//
// Print human readable object description
//
static mrb_value
mrb_p_m(mrb_state *mrb, mrb_value self)
{
mrb_int argc;
mrb_value *argv;
mrb_get_args(mrb, "*", &argv, &argc);
if (argc == 0) return mrb_nil_value();
for (mrb_int i=0; i<argc; i++) {
mrb_p(mrb, argv[i]);
}
if (argc == 1) return argv[0];
return mrb_ary_new_from_values(mrb, argc, argv);
}
#endif
/* defined? runtime helpers: each returns the CRuby result string, or nil when
undefined. The compiler emits calls to these for `defined?(...)` operands
whose kind can only be resolved at run time. */
/* `defined?(meth)`, a call on self with no receiver written: the answer is
what `respond_to?(:meth, true)` says, asked of the caller's own self, the
way CRuby's `rb_obj_respond_to` decides it. A `respond_to?` the object
redefines is what gets asked, and `respond_to_missing?` where the method
is not there; a call with a receiver written is answered elsewhere, on
the terms that call would be made on. */
static mrb_value
mrb_f_defined_method(mrb_state *mrb, mrb_value self)
{
mrb_sym sym;
mrb_get_args(mrb, "n", &sym);
mrb_sym rt_id = MRB_SYM_Q(respond_to);
mrb_bool found;
if (mrb_func_basic_p(mrb, self, rt_id, obj_respond_to) || !mrb_respond_to(mrb, self, rt_id)) {
found = obj_respond_to_p(mrb, self, sym, TRUE);
}
else {
found = mrb_test(mrb_funcall_argv2(mrb, self, rt_id, mrb_symbol_value(sym), mrb_true_value()));
}
if (found) return mrb_str_new_lit_frozen(mrb, "method");
return mrb_nil_value();
}
static mrb_value
mrb_f_defined_ivar(mrb_state *mrb, mrb_value self)
{
mrb_sym sym;
mrb_get_args(mrb, "n", &sym);
if (mrb_iv_defined(mrb, self, sym)) return mrb_str_new_lit_frozen(mrb, "instance-variable");
return mrb_nil_value();
}
static mrb_value
mrb_f_defined_const(mrb_state *mrb, mrb_value self)
{
mrb_sym sym;
mrb_get_args(mrb, "n", &sym);
/* resolve in the caller's lexical scope (ci[-1]), not this helper's */
mrb_callinfo *ci = &mrb->c->ci[-1];
if (ci >= mrb->c->cibase && ci->proc &&
mrb_vm_const_defined_p(mrb, ci, sym)) {
return mrb_str_new_lit_frozen(mrb, "constant");
}
return mrb_nil_value();
}
static mrb_value
mrb_f_defined_yield(mrb_state *mrb, mrb_value self)
{
/* mrb_f_block_given_p_m inspects ci[-1], i.e. the frame that used defined? */
if (mrb_test(mrb_f_block_given_p_m(mrb, self))) return mrb_str_new_lit_frozen(mrb, "yield");
return mrb_nil_value();
}
static mrb_value
mrb_f_defined_gvar(mrb_state *mrb, mrb_value self)
{
mrb_sym sym;
mrb_get_args(mrb, "n", &sym);
if (mrb_gv_defined(mrb, sym)) return mrb_str_new_lit_frozen(mrb, "global-variable");
return mrb_nil_value();
}
static mrb_value
mrb_f_defined_cvar(mrb_state *mrb, mrb_value self)
{
mrb_sym sym;
mrb_get_args(mrb, "n", &sym);
/* class-variable scope follows the caller's lexical class (ci[-1]) */
mrb_callinfo *ci = &mrb->c->ci[-1];
if (ci >= mrb->c->cibase && ci->proc &&
mrb_vm_cv_defined_p(mrb, ci->proc, sym)) {
return mrb_str_new_lit_frozen(mrb, "class variable");
}
return mrb_nil_value();
}
static mrb_value
mrb_f_defined_super(mrb_state *mrb, mrb_value self)
{
/* "super" is defined when the caller's method has a super method */
mrb_callinfo *ci = &mrb->c->ci[-1];
if (ci < mrb->c->cibase) return mrb_nil_value();
mrb_sym mid = ci->mid;
struct RClass *tc = mrb_vm_ci_target_class(ci);
if (mid != 0 && tc != NULL && tc->super != NULL) {
struct RClass *c = tc->super;
mrb_method_t m = mrb_method_search_vm(mrb, &c, mid);
if (!MRB_METHOD_UNDEF_P(m)) return mrb_str_new_lit_frozen(mrb, "super");
}
return mrb_nil_value();
}
/* The module Kernel#autoload registers on: the caller's cref, as in CRuby */
static struct RClass*
autoload_cref_class(mrb_state *mrb)
{
mrb_callinfo *ci = &mrb->c->ci[-1];
struct RClass *c = NULL;
if (ci >= mrb->c->cibase && ci->proc) c = mrb_vm_cref_class(mrb, ci);
if (!c || c->tt == MRB_TT_SCLASS) c = mrb->object_class;
return c;
}
/*
* call-seq:
* autoload(const, path) -> nil
*
* Module#autoload on the module the call is written in, Object at the
* top level.
*/
static mrb_value
mrb_f_autoload(mrb_state *mrb, mrb_value self)
{
mrb_value *argv;
mrb_int argc;
mrb_get_args(mrb, "*", &argv, &argc);
return mrb_funcall_argv(mrb, mrb_obj_value(autoload_cref_class(mrb)), MRB_SYM(autoload), argc, argv);
}
/*
* call-seq:
* autoload?(const, inherit = true) -> String or nil
*
* Module#autoload? on the module the call is written in.
*/
static mrb_value
mrb_f_autoload_p(mrb_state *mrb, mrb_value self)
{
mrb_value *argv;
mrb_int argc;
mrb_get_args(mrb, "*", &argv, &argc);
return mrb_funcall_argv(mrb, mrb_obj_value(autoload_cref_class(mrb)), MRB_SYM_Q(autoload), argc, argv);
}
/* `path` holds the names of a constant path from its root down, and `start`
says where to look the first one up: nil for the caller's lexical scope
(`A::B::C`), or the module the path is rooted at (`Object` for `::A::B`).
Each name below the root resolves the way reading `outer::NAME` resolves
it, so a constant Object holds is out of reach through any other module,
and `const_missing` is not asked. A name that is missing, or an outer
value that is not a module, ends the walk at nil rather than raising.
A pending autoload is loaded for every name but the last, as in CRuby. */
static mrb_value
mrb_f_defined_const_path(mrb_state *mrb, mrb_value self)
{
mrb_value start, path;
mrb_get_args(mrb, "oA", &start, &path);
mrb_int len = RARRAY_LEN(path);
mrb_int i = 0;
mrb_value outer;
if (len == 0) return mrb_nil_value();
if (!mrb_symbol_p(RARRAY_PTR(path)[0])) return mrb_nil_value();
if (mrb_nil_p(start)) {
mrb_callinfo *ci = &mrb->c->ci[-1];
if (ci < mrb->c->cibase || ci->proc == NULL) return mrb_nil_value();
outer = mrb_vm_const_get_noraise(mrb, ci, mrb_symbol(RARRAY_PTR(path)[0]), len > 1);
if (mrb_undef_p(outer)) return mrb_nil_value();
i = 1;
}
else {
outer = start;
}
/* An autoload runs Ruby, which can reach `path` through ObjectSpace and
shrink it, so its length is read afresh each turn. */
for (; i < RARRAY_LEN(path); i++) {
enum mrb_vtype t = mrb_type(outer);
if (t != MRB_TT_CLASS && t != MRB_TT_MODULE && t != MRB_TT_SCLASS) {
return mrb_nil_value();
}
if (!mrb_symbol_p(RARRAY_PTR(path)[i])) return mrb_nil_value();
outer = mrb_const_get_noraise(mrb, mrb_class_ptr(outer), mrb_symbol(RARRAY_PTR(path)[i]), i < RARRAY_LEN(path) - 1);
if (mrb_undef_p(outer)) return mrb_nil_value();
}
return mrb_str_new_lit_frozen(mrb, "constant");
}
/* `defined?(recv.meth)`: the caller has evaluated the receiver and hands it
over, and `self` here is the caller's own self, which is what decides
whether a protected method is reachable. Answer as a call would: a
private method is not reachable through a receiver, and a name with no
method behind it is left to `respond_to_missing?`. */
static mrb_value
mrb_f_defined_method_on(mrb_state *mrb, mrb_value self)
{
mrb_value recv;
mrb_sym sym;
mrb_get_args(mrb, "on", &recv, &sym);
struct RClass *c = mrb_class(mrb, recv);
#ifdef MRB_USE_REFINEMENTS
mrb_method_t m = mrb_vm_find_method_in_scope(mrb, mrb_vm_caller_refinements(mrb), c, &c, sym);
#else
mrb_method_t m = mrb_method_search_vm(mrb, &c, sym);
#endif
if (MRB_METHOD_UNDEF_P(m)) {
mrb_sym rtm_id = MRB_SYM_Q(respond_to_missing);
if (!mrb_func_basic_p(mrb, recv, rtm_id, mrb_false) && mrb_respond_to(mrb, recv, rtm_id)) {
mrb_value v = mrb_funcall_argv2(mrb, recv, rtm_id,
mrb_symbol_value(sym), mrb_false_value());
if (mrb_test(v)) return mrb_str_new_lit_frozen(mrb, "method");
}
return mrb_nil_value();
}
/* a method the build does not implement answers false to `respond_to?`,
and answers nil here for the same reason */
if (MRB_METHOD_NOTIMPL_P(m)) return mrb_nil_value();
/* the visibility test the VM applies to OP_SEND, in the same order */
if (m.flags & MRB_METHOD_PRIVATE_FL) return mrb_nil_value();
#ifdef MRB_USE_REFINEMENTS
/* a protected method a refinement holds is reached from the refined class */
if (MRB_CLASS_REFINEMENT_P(c)) c = c->super;
#endif
if ((m.flags & MRB_METHOD_PROTECTED_FL) && !mrb_obj_is_kind_of(mrb, self, c)) {
return mrb_nil_value();
}
return mrb_str_new_lit_frozen(mrb, "method");
}
/* ---------------------------*/
/*
* A hash pattern reads what #deconstruct_keys answered through __pat_values,
* which only Hash carries, so anything else lands here and gets the TypeError
* CRuby raises for it.
*/
static mrb_value
obj_pat_values(mrb_state *mrb, mrb_value self)
{