forked from MacRuby/MacRuby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.c
1146 lines (1036 loc) · 24.6 KB
/
kernel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MacRuby kernel. This file is compiled into LLVM bitcode and injected into
* the global module. Some of the functions defined here are inlined in the
* code MacRuby generates.
*
* This file is covered by the Ruby license. See COPYING for more details.
*
* Copyright (C) 2012, The MacRuby Team. All rights reserved.
* Copyright (C) 2011, Apple Inc. All rights reserved.
*/
#include "macruby_internal.h"
#include "ruby/node.h"
#include "vm.h"
#include "compiler.h"
#include "bridgesupport.h"
#include "id.h"
#include "array.h"
#include "hash.h"
#include "encoding.h"
#include "class.h"
#include "objc.h"
#ifndef PRIMITIVE
#define PRIMITIVE
#endif
PRIMITIVE VALUE
vm_gc_wb(VALUE *slot, VALUE val)
{
GC_WB_0(slot, val, false);
return val;
}
PRIMITIVE VALUE
vm_ivar_get(VALUE obj, ID name, void *cache_p)
{
struct icache *cache = (struct icache *)cache_p;
VALUE klass = 0;
if (!SPECIAL_CONST_P(obj)) {
klass = *(VALUE *)obj;
if (klass == cache->klass) {
if ((unsigned int)cache->slot < ROBJECT(obj)->num_slots) {
rb_object_ivar_slot_t *slot;
use_slot:
slot = &ROBJECT(obj)->slots[cache->slot];
if (slot->name == name) {
VALUE val = slot->value;
if (val == Qundef) {
val = Qnil;
}
return val;
}
}
goto find_slot;
}
}
if (cache->slot == SLOT_CACHE_VIRGIN) {
int slot;
find_slot:
slot = rb_vm_get_ivar_slot(obj, name, true);
if (slot >= 0) {
cache->klass = *(VALUE *)obj;
cache->slot = slot;
goto use_slot;
}
cache->klass = 0;
cache->slot = SLOT_CACHE_CANNOT;
}
return rb_ivar_get(obj, name);
}
PRIMITIVE void
vm_ivar_set(VALUE obj, ID name, VALUE val, void *cache_p)
{
struct icache *cache = (struct icache *)cache_p;
VALUE klass = 0;
if (!SPECIAL_CONST_P(obj)) {
klass = *(VALUE *)obj;
if (klass == cache->klass) {
if ((unsigned int)cache->slot < ROBJECT(obj)->num_slots) {
rb_object_ivar_slot_t *slot;
use_slot:
slot = &ROBJECT(obj)->slots[cache->slot];
if (slot->name == name) {
if ((ROBJECT(obj)->basic.flags & FL_FREEZE) == FL_FREEZE) {
rb_error_frozen("object");
}
GC_WB(&slot->value, val);
return;
}
}
goto find_slot;
}
}
if (cache->slot == SLOT_CACHE_VIRGIN) {
int slot;
find_slot:
slot = rb_vm_get_ivar_slot(obj, name, true);
if (slot >= 0) {
cache->klass = *(VALUE *)obj;
cache->slot = slot;
goto use_slot;
}
cache->slot = SLOT_CACHE_CANNOT;
}
rb_ivar_set(obj, name, val);
}
PRIMITIVE VALUE
vm_cvar_get(VALUE klass, ID id, unsigned char check,
unsigned char dynamic_class)
{
if (dynamic_class) {
Class k = rb_vm_get_current_class();
if (k != NULL) {
klass = (VALUE)k;
}
}
return rb_cvar_get2(klass, id, check);
}
PRIMITIVE VALUE
vm_cvar_set(VALUE klass, ID id, VALUE val, unsigned char dynamic_class)
{
if (dynamic_class) {
Class k = rb_vm_get_current_class();
if (k != NULL) {
klass = (VALUE)k;
}
}
rb_cvar_set(klass, id, val);
return val;
}
PRIMITIVE VALUE
vm_get_const(VALUE outer, void *cache_p, ID path, int flags,
void *outer_stack_p)
{
struct ccache *cache = (struct ccache *)cache_p;
rb_vm_outer_t *outer_stack = (rb_vm_outer_t *)outer_stack_p;
const bool lexical_lookup = (flags & CONST_LOOKUP_LEXICAL);
const bool dynamic_class = (flags & CONST_LOOKUP_DYNAMIC_CLASS);
if (dynamic_class && lexical_lookup) {
rb_vm_outer_t *o = outer_stack;
while (o != NULL && o->pushed_by_eval) {
o = o->outer;
}
if (o == NULL) {
outer = rb_cNSObject;
}
else {
outer = (VALUE)o->klass;
}
}
VALUE val;
if (cache->outer == outer && cache->outer_stack == outer_stack
&& cache->val != Qundef) {
val = cache->val;
}
else {
val = rb_vm_const_lookup_level(outer, path, lexical_lookup, false,
outer_stack);
cache->outer = outer;
cache->outer_stack = outer_stack;
cache->val = val;
}
return val;
}
PRIMITIVE void
vm_set_const(VALUE outer, ID id, VALUE obj, unsigned char dynamic_class, void *outer_stack_p)
{
if (dynamic_class) {
rb_vm_outer_t *outer_stack = (rb_vm_outer_t *)outer_stack_p;
if (outer_stack == NULL) {
outer = rb_cNSObject;
}
else {
outer = outer_stack->klass ? (VALUE)outer_stack->klass : Qnil;
}
}
rb_const_set(outer, id, obj);
}
static void __attribute__((noinline))
vm_resolve_args(VALUE **pargv, size_t argv_size, int *pargc, VALUE *args)
{
unsigned int i, argc = *pargc, real_argc = 0, j = 0;
VALUE *argv = *pargv;
bool splat_arg_follows = false;
for (i = 0; i < argc; i++) {
VALUE arg = args[j++];
if (arg == SPLAT_ARG_FOLLOWS) {
splat_arg_follows = true;
i--;
}
else {
if (splat_arg_follows) {
VALUE ary = rb_check_convert_type(arg, T_ARRAY, "Array",
"to_a");
if (NIL_P(ary)) {
ary = rb_ary_new4(1, &arg);
}
int count = RARRAY_LEN(ary);
if (real_argc + count >= argv_size) {
const size_t new_argv_size = real_argc + count + 100;
VALUE *new_argv = (VALUE *)xmalloc_ptrs(sizeof(VALUE)
* new_argv_size);
memcpy(new_argv, argv, sizeof(VALUE) * argv_size);
argv = new_argv;
argv_size = new_argv_size;
}
int j;
for (j = 0; j < count; j++) {
argv[real_argc++] = RARRAY_AT(ary, j);
}
splat_arg_follows = false;
}
else {
if (real_argc >= argv_size) {
const size_t new_argv_size = real_argc + 100;
VALUE *new_argv = (VALUE *)xmalloc_ptrs(sizeof(VALUE)
* new_argv_size);
memcpy(new_argv, argv, sizeof(VALUE) * argv_size);
argv = new_argv;
argv_size = new_argv_size;
}
argv[real_argc++] = arg;
}
}
}
*pargv = argv;
*pargc = real_argc;
}
static inline VALUE
vm_class_of(VALUE obj)
{
// TODO: separate the const bits of CLASS_OF to make sure they will get
// reduced by the optimizer.
return CLASS_OF(obj);
}
PRIMITIVE VALUE
vm_dispatch(VALUE top, VALUE self, void *sel, void *block, unsigned char opt,
int argc, VALUE *argv)
{
if (opt & DISPATCH_SUPER) {
if (sel == 0) {
rb_raise(rb_eNoMethodError, "super called outside of method");
}
}
VALUE buf[100];
if (opt & DISPATCH_SPLAT) {
if (argc == 1 && !SPECIAL_CONST_P(argv[1])
&& *(VALUE *)argv[1] == rb_cRubyArray) {
argc = RARY(argv[1])->len;
argv = rary_ptr(argv[1]);
}
else {
VALUE *new_argv = buf;
vm_resolve_args(&new_argv, 100, &argc, argv);
argv = new_argv;
}
if (argc == 0) {
const char *selname = sel_getName((SEL)sel);
const size_t selnamelen = strlen(selname);
if (selname[selnamelen - 1] == ':') {
// Because
// def foo; end; foo(*[])
// creates foo but dispatches foo:.
char buf[100];
strncpy(buf, selname, sizeof buf);
buf[selnamelen - 1] = '\0';
sel = sel_registerName(buf);
}
}
}
void *vm = rb_vm_current_vm();
VALUE klass = vm_class_of(self);
return rb_vm_call0(vm, top, self, (Class)klass, (SEL)sel,
(rb_vm_block_t *)block, opt, argc, argv);
}
PRIMITIVE VALUE
vm_yield_args(int argc, unsigned char opt, VALUE *argv)
{
VALUE buf[100];
if (opt & DISPATCH_SPLAT) {
if (argc == 1 && !SPECIAL_CONST_P(argv[1])
&& *(VALUE *)argv[1] == rb_cRubyArray) {
argc = RARY(argv[1])->len;
argv = rary_ptr(argv[1]);
}
else {
VALUE *new_argv = buf;
vm_resolve_args(&new_argv, 100, &argc, argv);
argv = new_argv;
}
}
void *vm = rb_vm_current_vm();
return rb_vm_yield_args(vm, argc, argv);
}
PRIMITIVE VALUE
vm_get_broken_value(void)
{
return rb_vm_get_broken_value(rb_vm_current_vm());
}
PRIMITIVE VALUE
vm_returned_from_block(int id)
{
return rb_vm_returned_from_block(rb_vm_current_vm(), id);
}
PRIMITIVE void
vm_release_ownership(VALUE obj)
{
rb_vm_release_ownership(obj);
}
PRIMITIVE void *
vm_get_block(VALUE obj)
{
if (obj == Qnil) {
return NULL;
}
VALUE proc = rb_check_convert_type(obj, T_DATA, "Proc", "to_proc");
if (NIL_P(proc)) {
rb_raise(rb_eTypeError,
"wrong argument type %s (expected Proc)",
rb_obj_classname(obj));
}
return rb_proc_get_block(proc);
}
// Only numeric immediates have their lsb at 1.
#define NUMERIC_IMM_P(x) ((x & 0x1) == 0x1)
#define IMM2DBL(x) (FIXFLOAT_P(x) ? FIXFLOAT2DBL(x) : FIX2LONG(x))
PRIMITIVE VALUE
vm_fast_plus(VALUE left, VALUE right, unsigned char overriden)
{
if (overriden == 0 && NUMERIC_IMM_P(left) && NUMERIC_IMM_P(right)) {
if (FIXNUM_P(left) && FIXNUM_P(right)) {
const long res = FIX2LONG(left) + FIX2LONG(right);
if (FIXABLE(res)) {
return LONG2FIX(res);
}
}
else {
const double res = IMM2DBL(left) + IMM2DBL(right);
return DBL2FIXFLOAT(res);
}
}
return vm_dispatch(0, left, selPLUS, NULL, 0, 1, &right);
}
PRIMITIVE VALUE
vm_fast_minus(VALUE left, VALUE right, unsigned char overriden)
{
if (overriden == 0 && NUMERIC_IMM_P(left) && NUMERIC_IMM_P(right)) {
if (FIXNUM_P(left) && FIXNUM_P(right)) {
const long res = FIX2LONG(left) - FIX2LONG(right);
if (FIXABLE(res)) {
return LONG2FIX(res);
}
}
else {
const double res = IMM2DBL(left) - IMM2DBL(right);
return DBL2FIXFLOAT(res);
}
}
return vm_dispatch(0, left, selMINUS, NULL, 0, 1, &right);
}
PRIMITIVE VALUE
vm_fast_mult(VALUE left, VALUE right, unsigned char overriden)
{
if (overriden == 0 && NUMERIC_IMM_P(left) && NUMERIC_IMM_P(right)) {
if (FIXNUM_P(left) && FIXNUM_P(right)) {
const long a = FIX2LONG(left);
if (a == 0) {
return left;
}
const long b = FIX2LONG(right);
const long res = a * b;
if (FIXABLE(res) && res / a == b) {
return LONG2FIX(res);
}
else {
return rb_big_mul(rb_int2big(a), rb_int2big(b));
}
}
else {
const double res = IMM2DBL(left) * IMM2DBL(right);
return DBL2FIXFLOAT(res);
}
}
return vm_dispatch(0, left, selMULT, NULL, 0, 1, &right);
}
PRIMITIVE VALUE
vm_fast_div(VALUE left, VALUE right, unsigned char overriden)
{
if (overriden == 0 && NUMERIC_IMM_P(left) && NUMERIC_IMM_P(right)) {
if (FIXNUM_P(left) && FIXNUM_P(right)) {
const long x = FIX2LONG(left);
const long y = FIX2LONG(right);
if (y != 0) {
long res = x / y;
if (((x < 0 && y >= 0) || (x >= 0 && y < 0))
&& (x % y) != 0) {
res--;
}
if (FIXABLE(res)) {
return LONG2FIX(res);
}
}
}
else {
const double res = IMM2DBL(left) / IMM2DBL(right);
return DBL2FIXFLOAT(res);
}
}
return vm_dispatch(0, left, selDIV, NULL, 0, 1, &right);
}
PRIMITIVE VALUE
vm_fast_mod(VALUE left, VALUE right, unsigned char overriden)
{
if (overriden == 0 && NUMERIC_IMM_P(left) && NUMERIC_IMM_P(right)) {
if (FIXNUM_P(left) && FIXNUM_P(right)) {
const long x = FIX2LONG(left);
const long y = FIX2LONG(right);
long div, mod;
if (y == 0) {
rb_num_zerodiv();
}
if (y < 0) {
if (x < 0) {
div = -x / -y;
}
else {
div = - (x / -y);
}
}
else {
if (x < 0) {
div = - (-x / y);
}
else {
div = x / y;
}
}
mod = x - div*y;
if ((mod < 0 && y > 0) || (mod > 0 && y < 0)) {
mod += y;
div -= 1;
}
return LONG2FIX(mod);
}
else if (FIXFLOAT_P(left) && FIXFLOAT_P(right)) {
const double x = IMM2DBL(left);
const double y = IMM2DBL(right);
double div, mod;
#ifdef HAVE_FMOD
mod = fmod(x, y);
#else
double z;
modf(x/y, &z);
mod = x - z * y;
#endif
div = (x - mod) / y;
if (y * mod < 0) {
mod += y;
div -= 1.0;
}
return DBL2FIXFLOAT(mod);
}
}
return vm_dispatch(0, left, selMOD, NULL, 0, 1, &right);
}
PRIMITIVE VALUE
vm_fast_lt(VALUE left, VALUE right, unsigned char overriden)
{
if (overriden == 0 && NUMERIC_IMM_P(left) && NUMERIC_IMM_P(right)) {
if (FIXNUM_P(left) && FIXNUM_P(right)) {
return FIX2LONG(left) < FIX2LONG(right) ? Qtrue : Qfalse;
}
else {
return IMM2DBL(left) < IMM2DBL(right) ? Qtrue : Qfalse;
}
}
return vm_dispatch(0, left, selLT, NULL, 0, 1, &right);
}
PRIMITIVE VALUE
vm_fast_le(VALUE left, VALUE right, unsigned char overriden)
{
if (overriden == 0 && NUMERIC_IMM_P(left) && NUMERIC_IMM_P(right)) {
if (FIXNUM_P(left) && FIXNUM_P(right)) {
return FIX2LONG(left) <= FIX2LONG(right) ? Qtrue : Qfalse;
}
else {
return IMM2DBL(left) <= IMM2DBL(right) ? Qtrue : Qfalse;
}
}
return vm_dispatch(0, left, selLE, NULL, 0, 1, &right);
}
PRIMITIVE VALUE
vm_fast_gt(VALUE left, VALUE right, unsigned char overriden)
{
if (overriden == 0 && NUMERIC_IMM_P(left) && NUMERIC_IMM_P(right)) {
if (FIXNUM_P(left) && FIXNUM_P(right)) {
return FIX2LONG(left) > FIX2LONG(right) ? Qtrue : Qfalse;
}
else {
return IMM2DBL(left) > IMM2DBL(right) ? Qtrue : Qfalse;
}
}
return vm_dispatch(0, left, selGT, NULL, 0, 1, &right);
}
PRIMITIVE VALUE
vm_fast_ge(VALUE left, VALUE right, unsigned char overriden)
{
if (overriden == 0 && NUMERIC_IMM_P(left) && NUMERIC_IMM_P(right)) {
if (FIXNUM_P(left) && FIXNUM_P(right)) {
return FIX2LONG(left) >= FIX2LONG(right) ? Qtrue : Qfalse;
}
else {
return IMM2DBL(left) >= IMM2DBL(right) ? Qtrue : Qfalse;
}
}
return vm_dispatch(0, left, selGE, NULL, 0, 1, &right);
}
PRIMITIVE VALUE
vm_fast_eq(VALUE left, VALUE right, unsigned char overriden)
{
if (overriden == 0) {
if (NUMERIC_IMM_P(left) && NUMERIC_IMM_P(right)) {
if (FIXNUM_P(left) && FIXNUM_P(right)) {
return FIX2LONG(left) == FIX2LONG(right) ? Qtrue : Qfalse;
}
else {
return IMM2DBL(left) == IMM2DBL(right) ? Qtrue : Qfalse;
}
}
if (left == Qtrue || left == Qfalse) {
return left == right ? Qtrue : Qfalse;
}
// TODO: opt for non-immediate types
}
return vm_dispatch(0, left, selEq, NULL, 0, 1, &right);
}
PRIMITIVE VALUE
vm_fast_eqq(VALUE left, VALUE right, unsigned char overriden)
{
if (overriden == 0) {
if (NUMERIC_IMM_P(left) && NUMERIC_IMM_P(right)) {
if (FIXNUM_P(left) && FIXNUM_P(right)) {
return FIX2LONG(left) == FIX2LONG(right) ? Qtrue : Qfalse;
}
else {
return IMM2DBL(left) == IMM2DBL(right) ? Qtrue : Qfalse;
}
}
if (left == Qtrue || left == Qfalse) {
return left == right ? Qtrue : Qfalse;
}
// TODO: opt for non-immediate types
}
return vm_dispatch(0, left, selEqq, NULL, 0, 1, &right);
}
PRIMITIVE VALUE
vm_fast_neq(VALUE left, VALUE right, unsigned char overriden)
{
if (overriden == 0) {
if (NUMERIC_IMM_P(left) && NUMERIC_IMM_P(right)) {
if (FIXNUM_P(left) && FIXNUM_P(right)) {
return FIX2LONG(left) != FIX2LONG(right) ? Qtrue : Qfalse;
}
else {
return IMM2DBL(left) != IMM2DBL(right) ? Qtrue : Qfalse;
}
}
if (left == Qtrue || left == Qfalse) {
return left != right ? Qtrue : Qfalse;
}
// TODO: opt for non-immediate types
}
return vm_dispatch(0, left, selNeq, NULL, 0, 1, &right);
}
PRIMITIVE VALUE
vm_fast_aref(VALUE obj, VALUE other, unsigned char overriden)
{
if (overriden == 0 && !SPECIAL_CONST_P(obj)) {
VALUE klass = *(VALUE *)obj;
if (klass == rb_cRubyArray) {
if (FIXNUM_P(other)) {
return rary_entry(obj, FIX2LONG(other));
}
}
else if (klass == rb_cRubyHash) {
return rhash_aref(obj, 0, other);
}
}
return vm_dispatch(0, obj, selAREF, NULL, 0, 1, &other);
}
PRIMITIVE VALUE
vm_fast_aset(VALUE obj, VALUE other1, VALUE other2, unsigned char overriden)
{
if (overriden == 0 && !SPECIAL_CONST_P(obj)) {
VALUE klass = *(VALUE *)obj;
if (klass == rb_cRubyArray) {
if (FIXNUM_P(other1)) {
rary_store(obj, FIX2LONG(other1), other2);
return other2;
}
}
else if (klass == rb_cRubyHash) {
return rhash_aset(obj, 0, other1, other2);
}
}
VALUE args[] = {other1, other2};
return vm_dispatch(0, obj, selASET, NULL, 0, 2, args);
}
PRIMITIVE VALUE
vm_fast_shift(VALUE obj, VALUE other, unsigned char overriden)
{
if (overriden == 0 && !SPECIAL_CONST_P(obj)) {
VALUE klass = *(VALUE *)obj;
if (klass == rb_cRubyArray) {
rary_modify(obj);
rary_push(obj, other);
return obj;
}
else if (klass == rb_cRubyString) {
return rstr_concat(obj, 0, other);
}
}
return vm_dispatch(0, obj, selLTLT, NULL, 0, 1, &other);
}
PRIMITIVE VALUE
vm_when_splat(unsigned char overriden, VALUE comparedTo, VALUE splat)
{
VALUE ary = rb_check_convert_type(splat, T_ARRAY, "Array", "to_a");
if (NIL_P(ary)) {
ary = rb_ary_new4(1, &splat);
}
long i, count = RARRAY_LEN(ary);
for (i = 0; i < count; i++) {
VALUE o = RARRAY_AT(ary, i);
if (RTEST(vm_fast_eqq(o, comparedTo, overriden))) {
return Qtrue;
}
}
return Qfalse;
}
PRIMITIVE void
vm_set_current_scope(VALUE mod, int scope)
{
rb_vm_set_current_scope(mod, (rb_vm_scope_t)scope);
}
PRIMITIVE VALUE
vm_ocval_to_rval(void *ocval)
{
return OC2RB(ocval);
}
PRIMITIVE VALUE
vm_char_to_rval(char c)
{
return INT2FIX(c);
}
PRIMITIVE VALUE
vm_uchar_to_rval(unsigned char c)
{
return INT2FIX(c);
}
PRIMITIVE VALUE
vm_short_to_rval(short c)
{
return INT2FIX(c);
}
PRIMITIVE VALUE
vm_ushort_to_rval(unsigned short c)
{
return INT2FIX(c);
}
PRIMITIVE VALUE
vm_int_to_rval(int c)
{
return INT2FIX(c);
}
PRIMITIVE VALUE
vm_uint_to_rval(unsigned int c)
{
return INT2FIX(c);
}
PRIMITIVE VALUE
vm_long_to_rval(long l)
{
return LONG2NUM(l);
}
PRIMITIVE VALUE
vm_ulong_to_rval(unsigned long l)
{
return ULONG2NUM(l);
}
PRIMITIVE VALUE
vm_long_long_to_rval(long long l)
{
return LL2NUM(l);
}
PRIMITIVE VALUE
vm_ulong_long_to_rval(unsigned long long l)
{
return ULL2NUM(l);
}
PRIMITIVE VALUE
vm_float_to_rval(float f)
{
return DOUBLE2NUM(f);
}
PRIMITIVE VALUE
vm_double_to_rval(double d)
{
return DOUBLE2NUM(d);
}
PRIMITIVE VALUE
vm_sel_to_rval(void *sel)
{
return sel == 0 ? Qnil : ID2SYM(rb_intern(sel_getName((SEL)sel)));
}
PRIMITIVE VALUE
vm_charptr_to_rval(const char *ptr)
{
return ptr == NULL ? Qnil : rb_str_new2(ptr);
}
PRIMITIVE void
vm_rval_to_ocval(VALUE rval, void **ocval)
{
*ocval = rval == Qnil ? NULL : RB2OC(rval);
}
PRIMITIVE void
vm_rval_to_bool(VALUE rval, BOOL *ocval)
{
if (rval == Qfalse || rval == Qnil) {
*ocval = NO;
}
else {
// All other types should be converted as true, to follow the Ruby
// semantics (where for example any integer is always true, even 0).
*ocval = YES;
}
}
static inline const char *
rval_to_c_str(VALUE rval)
{
if (NIL_P(rval)) {
return NULL;
}
else {
if (CLASS_OF(rval) == rb_cSymbol) {
return rb_sym2name(rval);
}
if (rb_obj_is_kind_of(rval, rb_cPointer)) {
return (const char *)rb_pointer_get_data(rval, "^c");
}
return StringValueCStr(rval);
}
}
PRIMITIVE void
vm_rval_to_sel(VALUE rval, void **ocval)
{
const char *cstr = rval_to_c_str(rval);
*(SEL *)ocval = cstr == NULL ? NULL : sel_registerName(cstr);
}
PRIMITIVE void
vm_rval_to_charptr(VALUE rval, const char **ocval)
{
*ocval = rval_to_c_str(rval);
}
static inline long
bool_to_fix(VALUE rval)
{
if (rval == Qtrue) {
return INT2FIX(1);
}
if (rval == Qfalse) {
return INT2FIX(0);
}
return rval;
}
static inline long
rval_to_long(VALUE rval)
{
return NUM2LONG(rb_Integer(bool_to_fix(rval)));
}
static inline long long
rval_to_long_long(VALUE rval)
{
return NUM2LL(rb_Integer(bool_to_fix(rval)));
}
static inline unsigned long long
rval_to_ulong_long(VALUE rval)
{
return NUM2ULL(rb_Integer(bool_to_fix(rval)));
}
static inline double
rval_to_double(VALUE rval)
{
return RFLOAT_VALUE(rb_Float(bool_to_fix(rval)));
}
PRIMITIVE void
vm_rval_to_char(VALUE rval, char *ocval)
{
if (TYPE(rval) == T_STRING && RSTRING_LEN(rval) == 1) {
*ocval = (char)RSTRING_PTR(rval)[0];
}
else {
*ocval = (char)rval_to_long(rval);
}
}
PRIMITIVE void
vm_rval_to_uchar(VALUE rval, unsigned char *ocval)
{
if (TYPE(rval) == T_STRING && RSTRING_LEN(rval) == 1) {
*ocval = (unsigned char)RSTRING_PTR(rval)[0];
}
else {
*ocval = (unsigned char)rval_to_long(rval);
}
}
PRIMITIVE void
vm_rval_to_short(VALUE rval, short *ocval)
{
*ocval = (short)rval_to_long(rval);
}
PRIMITIVE void
vm_rval_to_ushort(VALUE rval, unsigned short *ocval)
{
*ocval = (unsigned short)rval_to_long(rval);
}
PRIMITIVE void
vm_rval_to_int(VALUE rval, int *ocval)
{
*ocval = (int)rval_to_long(rval);
}
PRIMITIVE void
vm_rval_to_uint(VALUE rval, unsigned int *ocval)
{
*ocval = (unsigned int)rval_to_long(rval);
}
PRIMITIVE void
vm_rval_to_long(VALUE rval, long *ocval)
{
*ocval = (long)rval_to_long(rval);
}
PRIMITIVE void
vm_rval_to_ulong(VALUE rval, unsigned long *ocval)
{
*ocval = (unsigned long)rval_to_long(rval);
}
PRIMITIVE void
vm_rval_to_long_long(VALUE rval, long long *ocval)
{
*ocval = rval_to_long_long(rval);
}
PRIMITIVE void
vm_rval_to_ulong_long(VALUE rval, unsigned long long *ocval)
{
*ocval = rval_to_ulong_long(rval);
}
PRIMITIVE void
vm_rval_to_double(VALUE rval, double *ocval)
{
*ocval = (double)rval_to_double(rval);
}
PRIMITIVE void
vm_rval_to_float(VALUE rval, float *ocval)
{
*ocval = (float)rval_to_double(rval);
}
PRIMITIVE void *
vm_rval_to_cptr(VALUE rval, const char *type, void **cptr)
{
if (NIL_P(rval)) {
*cptr = NULL;
}
else {
if (TYPE(rval) == T_ARRAY
|| rb_boxed_is_type(CLASS_OF(rval), type + 1)) {
// A convenience helper so that the user can pass a Boxed or an
// Array object instead of a Pointer to the object.
rval = rb_pointer_new2(type + 1, rval);
}
*cptr = rb_pointer_get_data(rval, type);
}
return *cptr;
}
PRIMITIVE VALUE
vm_to_a(VALUE obj)
{
VALUE ary = rb_check_convert_type(obj, T_ARRAY, "Array", "to_a");
if (NIL_P(ary)) {
ary = rb_ary_new4(1, &obj);
}
return ary;
}
PRIMITIVE VALUE
vm_to_ary(VALUE obj)
{
VALUE ary = rb_check_convert_type(obj, T_ARRAY, "Array", "to_ary");
if (NIL_P(ary)) {
ary = rb_ary_new4(1, &obj);
}
return ary;
}
PRIMITIVE VALUE
vm_ary_cat(VALUE ary, VALUE obj)
{
VALUE ary2 = rb_check_convert_type(obj, T_ARRAY, "Array", "to_a");
if (!NIL_P(ary2)) {
rb_ary_concat(ary, ary2);
}
else {
rb_ary_push(ary, obj);
}
return ary;
}