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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
|
/**********************************************************************
eval.c -
$Author$
$Date$
created at: Thu Jun 10 14:22:17 JST 1993
Copyright (C) 1993-2003 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#include "eval_intern.h"
VALUE proc_invoke(VALUE, VALUE, VALUE, VALUE);
VALUE rb_binding_new();
VALUE rb_f_block_given_p(void);
ID rb_frame_callee(void);
static VALUE rb_frame_self(void);
NODE *ruby_current_node;
static ID removed, singleton_removed, undefined, singleton_undefined;
static ID init, eqq, each, aref, aset, match, missing;
static ID added, singleton_added;
static ID object_id, __send, __send_bang, respond_to;
VALUE rb_eLocalJumpError;
VALUE rb_eSysStackError;
extern int ruby_nerrs;
extern VALUE ruby_top_self;
static VALUE eval(VALUE, VALUE, VALUE, char *, int);
static inline VALUE rb_yield_0(int argc, VALUE *argv);
static VALUE rb_call(VALUE, VALUE, ID, int, const VALUE *, int);
#include "eval_error.h"
#include "eval_method.h"
#include "eval_safe.h"
#include "eval_jump.h"
/* initialize ruby */
#if defined(__APPLE__)
#define environ (*_NSGetEnviron())
#elif !defined(_WIN32) && !defined(__MACOS__) || defined(_WIN32_WCE)
extern char **environ;
#endif
char **rb_origenviron;
jmp_buf function_call_may_return_twice_jmp_buf;
int function_call_may_return_twice_false = 0;
void rb_call_inits _((void));
void Init_stack _((VALUE *));
void Init_heap _((void));
void Init_ext _((void));
void Init_yarv(void);
void
ruby_init(void)
{
static int initialized = 0;
int state;
if (initialized)
return;
initialized = 1;
#ifdef __MACOS__
rb_origenviron = 0;
#else
rb_origenviron = environ;
#endif
Init_stack((void *)&state);
Init_yarv();
Init_heap();
PUSH_TAG();
if ((state = EXEC_TAG()) == 0) {
rb_call_inits();
#ifdef __MACOS__
_macruby_init();
#elif defined(__VMS)
_vmsruby_init();
#endif
ruby_prog_init();
ALLOW_INTS;
}
POP_TAG_INIT();
if (state) {
error_print();
exit(EXIT_FAILURE);
}
ruby_running = 1;
}
void
ruby_options(int argc, char **argv)
{
int state;
Init_stack((void *)&state);
PUSH_TAG();
if ((state = EXEC_TAG()) == 0) {
SAVE_ROOT_JMPBUF(GET_THREAD(), ruby_process_options(argc, argv));
}
else {
rb_clear_trace_func();
exit(error_handle(state));
}
POP_TAG();
}
static void
ruby_finalize_0(void)
{
PUSH_TAG();
if (EXEC_TAG() == 0) {
rb_trap_exit();
}
POP_TAG();
rb_exec_end_proc();
}
static void
ruby_finalize_1(void)
{
signal(SIGINT, SIG_DFL);
GET_THREAD()->errinfo = 0;
rb_clear_trace_func();
rb_gc_call_finalizer_at_exit();
}
void
ruby_finalize(void)
{
ruby_finalize_0();
ruby_finalize_1();
}
int
ruby_cleanup(int ex)
{
int state;
volatile VALUE errs[2];
rb_thread_t *th = GET_THREAD();
rb_vm_t *vm = th->vm;
int nerr;
errs[1] = th->errinfo;
th->safe_level = 0;
Init_stack((void *)&state);
ruby_finalize_0();
errs[0] = th->errinfo;
PUSH_TAG();
if ((state = EXEC_TAG()) == 0) {
SAVE_ROOT_JMPBUF(th, rb_thread_terminate_all());
}
else if (ex == 0) {
ex = state;
}
th->errinfo = errs[1];
ex = error_handle(ex);
ruby_finalize_1();
POP_TAG();
rb_thread_stop_timer_thread();
for (nerr = 0; nerr < sizeof(errs) / sizeof(errs[0]); ++nerr) {
VALUE err = errs[nerr];
if (!RTEST(err)) continue;
/* th->errinfo contains a NODE while break'ing */
if (TYPE(err) == T_NODE) continue;
if (rb_obj_is_kind_of(err, rb_eSystemExit)) {
return sysexit_status(err);
}
else if (rb_obj_is_kind_of(err, rb_eSignal)) {
VALUE sig = rb_iv_get(err, "signo");
ruby_default_signal(NUM2INT(sig));
}
}
return ex;
}
extern NODE *ruby_eval_tree;
static int
ruby_exec_internal(void)
{
int state;
VALUE val;
rb_thread_t *th = GET_THREAD();
if (!ruby_eval_tree) return 0;
PUSH_TAG();
if ((state = EXEC_TAG()) == 0) {
SAVE_ROOT_JMPBUF(th, {
th->base_block = 0;
val = yarvcore_eval_parsed(ruby_eval_tree, rb_str_new2(ruby_sourcefile));
});
}
POP_TAG();
return state;
}
int
ruby_exec(void)
{
volatile NODE *tmp;
Init_stack((void *)&tmp);
return ruby_exec_internal();
}
void
ruby_stop(int ex)
{
exit(ruby_cleanup(ex));
}
void
ruby_run(void)
{
int state;
static int ex;
if (ruby_nerrs > 0) {
exit(EXIT_FAILURE);
}
state = ruby_exec();
if (state && !ex) {
ex = state;
}
ruby_stop(ex);
}
VALUE
rb_eval_string(const char *str)
{
VALUE v;
NODE *oldsrc = ruby_current_node;
ruby_current_node = 0;
ruby_sourcefile = rb_source_filename("(eval)");
v = eval(ruby_top_self, rb_str_new2(str), Qnil, 0, 0);
ruby_current_node = oldsrc;
return v;
}
VALUE
rb_eval_string_protect(const char *str, int *state)
{
return rb_protect((VALUE (*)_((VALUE)))rb_eval_string, (VALUE)str, state);
}
VALUE
rb_eval_string_wrap(const char *str, int *state)
{
int status;
rb_thread_t *th = GET_THREAD();
VALUE self = th->top_self;
VALUE wrapper = th->top_wrapper;
VALUE val;
th->top_wrapper = rb_module_new();
th->top_self = rb_obj_clone(ruby_top_self);
rb_extend_object(th->top_self, th->top_wrapper);
val = rb_eval_string_protect(str, &status);
th->top_self = self;
th->top_wrapper = wrapper;
if (state) {
*state = status;
}
else if (status) {
JUMP_TAG(status);
}
return val;
}
VALUE
rb_eval_cmd(VALUE cmd, VALUE arg, int level)
{
int state;
VALUE val = Qnil; /* OK */
volatile int safe = rb_safe_level();
if (OBJ_TAINTED(cmd)) {
level = 4;
}
if (TYPE(cmd) != T_STRING) {
PUSH_TAG();
rb_set_safe_level_force(level);
if ((state = EXEC_TAG()) == 0) {
val =
rb_funcall2(cmd, rb_intern("call"), RARRAY_LEN(arg),
RARRAY_PTR(arg));
}
POP_TAG();
rb_set_safe_level_force(safe);
if (state)
JUMP_TAG(state);
return val;
}
PUSH_TAG();
if ((state = EXEC_TAG()) == 0) {
val = eval(ruby_top_self, cmd, Qnil, 0, 0);
}
POP_TAG();
rb_set_safe_level_force(safe);
if (state) th_jump_tag_but_local_jump(state, val);
return val;
}
/*
* call-seq:
* Module.nesting => array
*
* Returns the list of +Modules+ nested at the point of call.
*
* module M1
* module M2
* $a = Module.nesting
* end
* end
* $a #=> [M1::M2, M1]
* $a[0].name #=> "M1::M2"
*/
static VALUE
rb_mod_nesting(void)
{
VALUE ary = rb_ary_new();
NODE *cref = ruby_cref();
while (cref && cref->nd_next) {
VALUE klass = cref->nd_clss;
if (!NIL_P(klass)) {
rb_ary_push(ary, klass);
}
cref = cref->nd_next;
}
return ary;
}
/*
* call-seq:
* Module.constants => array
*
* Returns an array of the names of all constants defined in the
* system. This list includes the names of all modules and classes.
*
* p Module.constants.sort[1..5]
*
* <em>produces:</em>
*
* ["ARGV", "ArgumentError", "Array", "Bignum", "Binding"]
*/
static VALUE
rb_mod_s_constants(int argc, VALUE *argv, VALUE mod)
{
NODE *cref = ruby_cref();
VALUE klass;
VALUE cbase = 0;
void *data = 0;
if (argc > 0) {
return rb_mod_constants(argc, argv, rb_cModule);
}
while (cref) {
klass = cref->nd_clss;
if (!NIL_P(klass)) {
data = rb_mod_const_at(cref->nd_clss, data);
if (!cbase) {
cbase = klass;
}
}
cref = cref->nd_next;
}
if (cbase) {
data = rb_mod_const_of(cbase, data);
}
return rb_const_list(data);
}
void
rb_frozen_class_p(VALUE klass)
{
char *desc = "something(?!)";
if (OBJ_FROZEN(klass)) {
if (FL_TEST(klass, FL_SINGLETON))
desc = "object";
else {
switch (TYPE(klass)) {
case T_MODULE:
case T_ICLASS:
desc = "module";
break;
case T_CLASS:
desc = "class";
break;
}
}
rb_error_frozen(desc);
}
}
/*
* call-seq:
* obj.respond_to?(symbol, include_private=false) => true or false
*
* Returns +true+> if _obj_ responds to the given
* method. Private methods are included in the search only if the
* optional second parameter evaluates to +true+.
*/
static NODE *basic_respond_to = 0;
int
rb_obj_respond_to(VALUE obj, ID id, int priv)
{
VALUE klass = CLASS_OF(obj);
if (rb_method_node(klass, respond_to) == basic_respond_to) {
return rb_method_boundp(klass, id, !priv);
}
else {
VALUE args[2];
int n = 0;
args[n++] = ID2SYM(id);
if (priv)
args[n++] = Qtrue;
return rb_funcall2(obj, respond_to, n, args);
}
}
int
rb_respond_to(VALUE obj, ID id)
{
return rb_obj_respond_to(obj, id, Qfalse);
}
/*
* call-seq:
* obj.respond_to?(symbol, include_private=false) => true or false
*
* Returns +true+> if _obj_ responds to the given
* method. Private methods are included in the search only if the
* optional second parameter evaluates to +true+.
*/
static VALUE
obj_respond_to(int argc, VALUE *argv, VALUE obj)
{
VALUE mid, priv;
ID id;
rb_scan_args(argc, argv, "11", &mid, &priv);
id = rb_to_id(mid);
if (rb_method_boundp(CLASS_OF(obj), id, !RTEST(priv))) {
return Qtrue;
}
return Qfalse;
}
/*
* call-seq:
* mod.method_defined?(symbol) => true or false
*
* Returns +true+ if the named method is defined by
* _mod_ (or its included modules and, if _mod_ is a class,
* its ancestors). Public and protected methods are matched.
*
* module A
* def method1() end
* end
* class B
* def method2() end
* end
* class C < B
* include A
* def method3() end
* end
*
* A.method_defined? :method1 #=> true
* C.method_defined? "method1" #=> true
* C.method_defined? "method2" #=> true
* C.method_defined? "method3" #=> true
* C.method_defined? "method4" #=> false
*/
static VALUE
rb_mod_method_defined(mod, mid)
VALUE mod, mid;
{
return rb_method_boundp(mod, rb_to_id(mid), 1);
}
#define VISI_CHECK(x,f) (((x)&NOEX_MASK) == (f))
/*
* call-seq:
* mod.public_method_defined?(symbol) => true or false
*
* Returns +true+ if the named public method is defined by
* _mod_ (or its included modules and, if _mod_ is a class,
* its ancestors).
*
* module A
* def method1() end
* end
* class B
* protected
* def method2() end
* end
* class C < B
* include A
* def method3() end
* end
*
* A.method_defined? :method1 #=> true
* C.public_method_defined? "method1" #=> true
* C.public_method_defined? "method2" #=> false
* C.method_defined? "method2" #=> true
*/
static VALUE
rb_mod_public_method_defined(VALUE mod, VALUE mid)
{
ID id = rb_to_id(mid);
NODE *method;
method = rb_method_node(mod, id);
if (method) {
if (VISI_CHECK(method->nd_noex, NOEX_PUBLIC))
return Qtrue;
}
return Qfalse;
}
/*
* call-seq:
* mod.private_method_defined?(symbol) => true or false
*
* Returns +true+ if the named private method is defined by
* _ mod_ (or its included modules and, if _mod_ is a class,
* its ancestors).
*
* module A
* def method1() end
* end
* class B
* private
* def method2() end
* end
* class C < B
* include A
* def method3() end
* end
*
* A.method_defined? :method1 #=> true
* C.private_method_defined? "method1" #=> false
* C.private_method_defined? "method2" #=> true
* C.method_defined? "method2" #=> false
*/
static VALUE
rb_mod_private_method_defined(VALUE mod, VALUE mid)
{
ID id = rb_to_id(mid);
NODE *method;
method = rb_method_node(mod, id);
if (method) {
if (VISI_CHECK(method->nd_noex, NOEX_PRIVATE))
return Qtrue;
}
return Qfalse;
}
/*
* call-seq:
* mod.protected_method_defined?(symbol) => true or false
*
* Returns +true+ if the named protected method is defined
* by _mod_ (or its included modules and, if _mod_ is a
* class, its ancestors).
*
* module A
* def method1() end
* end
* class B
* protected
* def method2() end
* end
* class C < B
* include A
* def method3() end
* end
*
* A.method_defined? :method1 #=> true
* C.protected_method_defined? "method1" #=> false
* C.protected_method_defined? "method2" #=> true
* C.method_defined? "method2" #=> true
*/
static VALUE
rb_mod_protected_method_defined(VALUE mod, VALUE mid)
{
ID id = rb_to_id(mid);
NODE *method;
method = rb_method_node(mod, id);
if (method) {
if (VISI_CHECK(method->nd_noex, NOEX_PROTECTED))
return Qtrue;
}
return Qfalse;
}
NORETURN(void th_iter_break _((rb_thread_t *)));
void
rb_iter_break()
{
th_iter_break(GET_THREAD());
}
NORETURN(static void rb_longjmp _((int, VALUE)));
static VALUE make_backtrace _((void));
static void
rb_longjmp(int tag, VALUE mesg)
{
VALUE at;
rb_thread_t *th = GET_THREAD();
if (thread_set_raised(th)) {
th->errinfo = exception_error;
JUMP_TAG(TAG_FATAL);
}
if (NIL_P(mesg))
mesg = GET_THREAD()->errinfo;
if (NIL_P(mesg)) {
mesg = rb_exc_new(rb_eRuntimeError, 0, 0);
}
ruby_set_current_source();
if (ruby_sourcefile && !NIL_P(mesg)) {
at = get_backtrace(mesg);
if (NIL_P(at)) {
at = make_backtrace();
set_backtrace(mesg, at);
}
}
if (!NIL_P(mesg)) {
GET_THREAD()->errinfo = mesg;
}
if (RTEST(ruby_debug) && !NIL_P(GET_THREAD()->errinfo)
&& !rb_obj_is_kind_of(GET_THREAD()->errinfo, rb_eSystemExit)) {
VALUE e = GET_THREAD()->errinfo;
int status;
PUSH_TAG();
if ((status = EXEC_TAG()) == 0) {
e = rb_obj_as_string(e);
warn_printf("Exception `%s' at %s:%d - %s\n",
rb_obj_classname(GET_THREAD()->errinfo),
ruby_sourcefile, ruby_sourceline, RSTRING_PTR(e));
}
POP_TAG();
if (status == TAG_FATAL && GET_THREAD()->errinfo == exception_error) {
GET_THREAD()->errinfo = mesg;
}
else if (status) {
thread_reset_raised(th);
JUMP_TAG(status);
}
}
rb_trap_restore_mask();
if (tag != TAG_FATAL) {
EXEC_EVENT_HOOK(th, RUBY_EVENT_RAISE, th->cfp->self,
0 /* TODO: id */, 0 /* TODO: klass */);
}
thread_reset_raised(th);
JUMP_TAG(tag);
}
void
rb_exc_raise(mesg)
VALUE mesg;
{
rb_longjmp(TAG_RAISE, mesg);
}
void
rb_exc_fatal(mesg)
VALUE mesg;
{
rb_longjmp(TAG_FATAL, mesg);
}
void
rb_interrupt()
{
rb_raise(rb_eInterrupt, "");
}
/*
* call-seq:
* raise
* raise(string)
* raise(exception [, string [, array]])
* fail
* fail(string)
* fail(exception [, string [, array]])
*
* With no arguments, raises the exception in <code>$!</code> or raises
* a <code>RuntimeError</code> if <code>$!</code> is +nil+.
* 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 <code>begin...end</code> blocks.
*
* raise "Failed to create socket"
* raise ArgumentError, "No parameters", caller
*/
static VALUE get_errinfo(void);
static VALUE
rb_f_raise(int argc, VALUE *argv)
{
VALUE err;
if (argc == 0) {
err = get_errinfo();
if (!NIL_P(err)) {
argc = 1;
argv = &err;
}
}
rb_raise_jump(rb_make_exception(argc, argv));
return Qnil; /* not reached */
}
VALUE
rb_make_exception(int argc, VALUE *argv)
{
VALUE mesg;
ID exception;
int n;
mesg = Qnil;
switch (argc) {
case 0:
mesg = Qnil;
break;
case 1:
if (NIL_P(argv[0]))
break;
if (TYPE(argv[0]) == T_STRING) {
mesg = rb_exc_new3(rb_eRuntimeError, argv[0]);
break;
}
n = 0;
goto exception_call;
case 2:
case 3:
n = 1;
exception_call:
exception = rb_intern("exception");
if (!rb_respond_to(argv[0], exception)) {
rb_raise(rb_eTypeError, "exception class/object expected");
}
mesg = rb_funcall(argv[0], exception, n, argv[1]);
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments");
break;
}
if (argc > 0) {
if (!rb_obj_is_kind_of(mesg, rb_eException))
rb_raise(rb_eTypeError, "exception object expected");
if (argc > 2)
set_backtrace(mesg, argv[2]);
}
return mesg;
}
void
rb_raise_jump(mesg)
VALUE mesg;
{
/* TODO: fix me */
rb_longjmp(TAG_RAISE, mesg);
}
void
rb_jump_tag(tag)
int tag;
{
JUMP_TAG(tag);
}
int
rb_block_given_p()
{
rb_thread_t *th = GET_THREAD();
if (GC_GUARDED_PTR_REF(th->cfp->lfp[0])) {
return Qtrue;
}
else {
return Qfalse;
}
}
int
rb_iterator_p()
{
return rb_block_given_p();
}
/*
* call-seq:
* block_given? => true or false
* iterator? => true or false
*
* Returns <code>true</code> if <code>yield</code> would execute a
* block in the current context. The <code>iterator?</code> 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"
*/
VALUE
rb_f_block_given_p()
{
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *cfp = th->cfp;
cfp = th_get_ruby_level_cfp(th, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
if (GC_GUARDED_PTR_REF(cfp->lfp[0])) {
return Qtrue;
}
else {
return Qfalse;
}
}
VALUE rb_eThreadError;
void
rb_need_block()
{
if (!rb_block_given_p()) {
th_localjump_error("no block given", Qnil, 0);
}
}
static inline VALUE
rb_yield_0(int argc, VALUE *argv)
{
return th_yield(GET_THREAD(), argc, argv);
}
VALUE
rb_yield(VALUE val)
{
volatile VALUE tmp = val;
if (val == Qundef) {
tmp = rb_yield_0(0, 0);
}
else {
tmp = rb_yield_0(1, &val);
}
return tmp;
}
VALUE
rb_yield_values(int n, ...)
{
int i;
VALUE *argv;
va_list args;
if (n == 0) {
return rb_yield_0(0, 0);
}
argv = ALLOCA_N(VALUE, n);
va_init_list(args, n);
for (i=0; i<n; i++) {
argv[i] = va_arg(args, VALUE);
}
va_end(args);
return rb_yield_0(n, argv);
}
VALUE
rb_yield_splat(VALUE values)
{
VALUE tmp = rb_check_array_type(values);
volatile VALUE v;
if (NIL_P(tmp)) {
rb_raise(rb_eArgError, "not an array");
}
v = rb_yield_0(RARRAY_LEN(tmp), RARRAY_PTR(tmp));
return v;
}
/*
* call-seq:
* loop {|| block }
*
* Repeatedly executes the block.
*
* loop do
* print "Input: "
* line = gets
* break if !line or line =~ /^qQ/
* # ...
* end
*/
static VALUE
rb_f_loop(void)
{
for (;;) {
rb_yield_0(0, 0);
}
return Qnil; /* dummy */
}
#define GET_THROWOBJ_CATCH_POINT(obj) ((VALUE*)RNODE((obj))->u2.value)
VALUE
rb_iterate(VALUE (*it_proc) (VALUE), VALUE data1,
VALUE (*bl_proc) (ANYARGS), VALUE data2)
{
int state;
volatile VALUE retval = Qnil;
NODE *node = NEW_IFUNC(bl_proc, data2);
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *cfp = th->cfp;
TH_PUSH_TAG(th);
state = TH_EXEC_TAG();
if (state == 0) {
iter_retry:
{
rb_block_t *blockptr = RUBY_VM_GET_BLOCK_PTR_IN_CFP(th->cfp);
blockptr->iseq = (void *)node;
blockptr->proc = 0;
th->passed_block = blockptr;
}
retval = (*it_proc) (data1);
}
else {
VALUE err = th->errinfo;
if (state == TAG_BREAK) {
VALUE *escape_dfp = GET_THROWOBJ_CATCH_POINT(err);
VALUE *cdfp = cfp->dfp;
if (cdfp == escape_dfp) {
state = 0;
th->state = 0;
th->errinfo = Qnil;
th->cfp = cfp;
}
else{
/* SDR(); printf("%p, %p\n", cdfp, escape_dfp); */
}
}
else if (state == TAG_RETRY) {
VALUE *escape_dfp = GET_THROWOBJ_CATCH_POINT(err);
VALUE *cdfp = cfp->dfp;
if (cdfp == escape_dfp) {
state = 0;
th->state = 0;
th->errinfo = Qnil;
th->cfp = cfp;
goto iter_retry;
}
}
}
TH_POP_TAG();
switch (state) {
case 0:
break;
default:
TH_JUMP_TAG(th, state);
}
return retval;
}
struct iter_method_arg {
VALUE obj;
ID mid;
int argc;
VALUE *argv;
};
static VALUE
iterate_method(VALUE obj)
{
struct iter_method_arg *arg;
arg = (struct iter_method_arg *)obj;
return rb_call(CLASS_OF(arg->obj), arg->obj, arg->mid,
arg->argc, arg->argv, NOEX_PRIVATE);
}
VALUE
rb_block_call(VALUE obj, ID mid, int argc, VALUE *argv,
VALUE (*bl_proc) (ANYARGS), VALUE data2)
{
struct iter_method_arg arg;
arg.obj = obj;
arg.mid = mid;
arg.argc = argc;
arg.argv = argv;
return rb_iterate(iterate_method, (VALUE)&arg, bl_proc, data2);
}
VALUE
rb_each(VALUE obj)
{
return rb_call(CLASS_OF(obj), obj, rb_intern("each"), 0, 0,
NOEX_PRIVATE);
}
VALUE
rb_rescue2(VALUE (*b_proc) (ANYARGS), VALUE data1, VALUE (*r_proc) (ANYARGS),
VALUE data2, ...)
{
int state;
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *cfp = th->cfp;
volatile VALUE result;
volatile VALUE e_info = th->errinfo;
va_list args;
PUSH_TAG();
if ((state = EXEC_TAG()) == 0) {
retry_entry:
result = (*b_proc) (data1);
}
else {
th->cfp = cfp; /* restore */
if (state == TAG_RAISE) {
int handle = Qfalse;
VALUE eclass;
va_init_list(args, data2);
while (eclass = va_arg(args, VALUE)) {
if (rb_obj_is_kind_of(th->errinfo, eclass)) {
handle = Qtrue;
break;
}
}
va_end(args);
if (handle) {
if (r_proc) {
PUSH_TAG();
if ((state = EXEC_TAG()) == 0) {
result = (*r_proc) (data2, th->errinfo);
}
POP_TAG();
if (state == TAG_RETRY) {
state = 0;
th->errinfo = Qnil;
goto retry_entry;
}
}
else {
result = Qnil;
state = 0;
}
if (state == 0) {
th->errinfo = e_info;
}
}
}
}
POP_TAG();
if (state)
JUMP_TAG(state);
return result;
}
VALUE
rb_rescue(VALUE (*b_proc)(ANYARGS), VALUE data1, VALUE (*r_proc)(ANYARGS), VALUE data2)
{
return rb_rescue2(b_proc, data1, r_proc, data2, rb_eStandardError,
(VALUE)0);
}
VALUE
rb_protect(VALUE (*proc) (VALUE), VALUE data, int *state)
{
VALUE result = Qnil; /* OK */
int status;
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *cfp = th->cfp;
struct rb_vm_trap_tag trap_tag = {
th->trap_tag,
};
PUSH_TAG();
th->trap_tag = &trap_tag;
if ((status = EXEC_TAG()) == 0) {
SAVE_ROOT_JMPBUF(th, result = (*proc) (data));
}
th->trap_tag = trap_tag.prev;
POP_TAG();
if (state) {
*state = status;
}
if (status != 0) {
th->cfp = cfp;
return Qnil;
}
return result;
}
VALUE
rb_ensure(VALUE (*b_proc)(ANYARGS), VALUE data1, VALUE (*e_proc)(ANYARGS), VALUE data2)
{
int state;
volatile VALUE result = Qnil;
PUSH_TAG();
if ((state = EXEC_TAG()) == 0) {
result = (*b_proc) (data1);
}
POP_TAG();
/* TODO: fix me */
/* retval = prot_tag ? prot_tag->retval : Qnil; */ /* save retval */
(*e_proc) (data2);
if (state)
JUMP_TAG(state);
return result;
}
VALUE
rb_with_disable_interrupt(VALUE (*proc)(ANYARGS), VALUE data)
{
VALUE result = Qnil; /* OK */
int status;
DEFER_INTS;
{
int thr_critical = rb_thread_critical;
rb_thread_critical = Qtrue;
PUSH_TAG();
if ((status = EXEC_TAG()) == 0) {
result = (*proc) (data);
}
POP_TAG();
rb_thread_critical = thr_critical;
}
ENABLE_INTS;
if (status)
JUMP_TAG(status);
return result;
}
static inline void
stack_check(void)
{
static int overflowing = 0;
if (!overflowing && ruby_stack_check()) {
int state;
overflowing = 1;
PUSH_TAG();
if ((state = EXEC_TAG()) == 0) {
rb_exc_raise(sysstack_error);
}
POP_TAG();
overflowing = 0;
JUMP_TAG(state);
}
}
/*
* call-seq:
* obj.method_missing(symbol [, *args] ) => result
*
* Invoked by Ruby when <i>obj</i> is sent a message it cannot handle.
* <i>symbol</i> is the symbol for the method called, and <i>args</i>
* are any arguments that were passed to it. By default, the interpreter
* raises an error when this method is called. However, it is possible
* to override the method to provide more dynamic behavior.
* The example below creates
* a class <code>Roman</code>, which responds to methods with names
* consisting of roman numerals, returning the corresponding integer
* values.
*
* class Roman
* def romanToInt(str)
* # ...
* end
* def method_missing(methId)
* str = methId.id2name
* romanToInt(str)
* end
* end
*
* r = Roman.new
* r.iv #=> 4
* r.xxiii #=> 23
* r.mm #=> 2000
*/
static VALUE
rb_method_missing(int argc, const VALUE *argv, VALUE obj)
{
ID id;
VALUE exc = rb_eNoMethodError;
char *format = 0;
NODE *cnode = ruby_current_node;
rb_thread_t *th = GET_THREAD();
int last_call_status = th->method_missing_reason;
if (argc == 0 || !SYMBOL_P(argv[0])) {
rb_raise(rb_eArgError, "no id given");
}
stack_check();
id = SYM2ID(argv[0]);
if (last_call_status & NOEX_PRIVATE) {
format = "private method `%s' called for %s";
}
else if (last_call_status & NOEX_PROTECTED) {
format = "protected method `%s' called for %s";
}
else if (last_call_status & NOEX_VCALL) {
format = "undefined local variable or method `%s' for %s";
exc = rb_eNameError;
}
else if (last_call_status & NOEX_SUPER) {
format = "super: no superclass method `%s'";
}
if (!format) {
format = "undefined method `%s' for %s";
}
ruby_current_node = cnode;
{
int n = 0;
VALUE args[3];
args[n++] = rb_funcall(rb_const_get(exc, rb_intern("message")), '!',
3, rb_str_new2(format), obj, argv[0]);
args[n++] = argv[0];
if (exc == rb_eNoMethodError) {
args[n++] = rb_ary_new4(argc - 1, argv + 1);
}
exc = rb_class_new_instance(n, args, exc);
rb_exc_raise(exc);
}
return Qnil; /* not reached */
}
static VALUE
method_missing(VALUE obj, ID id, int argc, const VALUE *argv, int call_status)
{
VALUE *nargv;
GET_THREAD()->method_missing_reason = call_status;
if (id == missing) {
rb_method_missing(argc, argv, obj);
}
else if (id == ID_ALLOCATOR) {
rb_raise(rb_eTypeError, "allocator undefined for %s",
rb_class2name(obj));
}
nargv = ALLOCA_N(VALUE, argc + 1);
nargv[0] = ID2SYM(id);
MEMCPY(nargv + 1, argv, VALUE, argc);
return rb_funcall2(obj, missing, argc + 1, nargv);
}
static VALUE
rb_call(VALUE klass, VALUE recv, ID mid, int argc, const VALUE *argv, int scope)
{
NODE *body, *method;
int noex;
ID id = mid;
struct cache_entry *ent;
if (!klass) {
rb_raise(rb_eNotImpError,
"method `%s' called on terminated object (%p)",
rb_id2name(mid), (void *)recv);
}
/* is it in the method cache? */
ent = cache + EXPR1(klass, mid);
if (ent->mid == mid && ent->klass == klass) {
if (!ent->method)
return method_missing(recv, mid, argc, argv,
scope == 2 ? NOEX_VCALL : 0);
id = ent->mid0;
noex = ent->method->nd_noex;
klass = ent->method->nd_clss;
body = ent->method->nd_body;
}
else if ((method = rb_get_method_body(klass, id, &id)) != 0) {
noex = method->nd_noex;
klass = method->nd_clss;
body = method->nd_body;
}
else {
if (scope == 3) {
return method_missing(recv, mid, argc, argv, NOEX_SUPER);
}
return method_missing(recv, mid, argc, argv,
scope == 2 ? NOEX_VCALL : 0);
}
if (mid != missing) {
/* receiver specified form for private method */
if (((noex & NOEX_MASK) & NOEX_PRIVATE) && scope == 0) {
return method_missing(recv, mid, argc, argv, NOEX_PRIVATE);
}
/* self must be kind of a specified form for protected method */
if (((noex & NOEX_MASK) & NOEX_PROTECTED) && scope == 0) {
VALUE defined_class = klass;
if (TYPE(defined_class) == T_ICLASS) {
defined_class = RBASIC(defined_class)->klass;
}
if (!rb_obj_is_kind_of(rb_frame_self(),
rb_class_real(defined_class))) {
return method_missing(recv, mid, argc, argv, NOEX_PROTECTED);
}
}
}
{
VALUE val;
/*
//static int level;
//int i;
//for(i=0; i<level; i++){printf(" ");}
//printf("invoke %s (%s)\n", rb_id2name(mid), ruby_node_name(nd_type(body)));
//level++;
//printf("%s with %d args\n", rb_id2name(mid), argc);
*/
val =
th_call0(GET_THREAD(), klass, recv, mid, id, argc, argv, body,
noex & NOEX_NOSUPER);
/*
//level--;
//for(i=0; i<level; i++){printf(" ");}
//printf("done %s (%s)\n", rb_id2name(mid), ruby_node_name(nd_type(body)));
*/
return val;
}
}
VALUE
rb_apply(VALUE recv, ID mid, VALUE args)
{
int argc;
VALUE *argv;
argc = RARRAY_LEN(args); /* Assigns LONG, but argc is INT */
argv = ALLOCA_N(VALUE, argc);
MEMCPY(argv, RARRAY_PTR(args), VALUE, argc);
return rb_call(CLASS_OF(recv), recv, mid, argc, argv, NOEX_NOSUPER);
}
static VALUE
send_funcall(int argc, VALUE *argv, VALUE recv, int scope)
{
VALUE vid;
if (argc == 0) {
rb_raise(rb_eArgError, "no method name given");
}
vid = *argv++; argc--;
PASS_PASSED_BLOCK();
return rb_call(CLASS_OF(recv), recv, rb_to_id(vid), argc, argv, scope);
}
/*
* call-seq:
* obj.send(symbol [, args...]) => obj
* obj.__send__(symbol [, args...]) => obj
*
* Invokes the method identified by _symbol_, passing it any
* arguments specified. You can use <code>__send__</code> if the name
* +send+ clashes with an existing method in _obj_.
*
* class Klass
* def hello(*args)
* "Hello " + args.join(' ')
* end
* end
* k = Klass.new
* k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
*/
VALUE
rb_f_send(int argc, VALUE *argv, VALUE recv)
{
int scope = NOEX_PUBLIC;
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
if (SPECIAL_CONST_P(cfp->sp[0])) {
scope = NOEX_NOSUPER | NOEX_PRIVATE;
}
return send_funcall(argc, argv, recv, scope);
}
/*
* call-seq:
* obj.funcall(symbol [, args...]) => obj
* obj.__send!(symbol [, args...]) => obj
*
* Invokes the method identified by _symbol_, passing it any
* arguments specified. Unlike send, which calls private methods only
* when it is invoked in function call style, funcall always aware of
* private methods.
*
* 1.funcall(:puts, "hello") # prints "foo"
*/
VALUE
rb_f_funcall(int argc, VALUE *argv, VALUE recv)
{
return send_funcall(argc, argv, recv, NOEX_NOSUPER | NOEX_PRIVATE);
}
VALUE
rb_funcall(VALUE recv, ID mid, int n, ...)
{
VALUE *argv;
va_list ar;
va_init_list(ar, n);
if (n > 0) {
long i;
argv = ALLOCA_N(VALUE, n);
for (i = 0; i < n; i++) {
argv[i] = va_arg(ar, VALUE);
}
va_end(ar);
}
else {
argv = 0;
}
return rb_call(CLASS_OF(recv), recv, mid, n, argv,
NOEX_NOSUPER | NOEX_PRIVATE);
}
VALUE
rb_funcall2(VALUE recv, ID mid, int argc, const VALUE *argv)
{
return rb_call(CLASS_OF(recv), recv, mid, argc, argv,
NOEX_NOSUPER | NOEX_PRIVATE);
}
VALUE
rb_funcall3(VALUE recv, ID mid, int argc, const VALUE *argv)
{
return rb_call(CLASS_OF(recv), recv, mid, argc, argv, NOEX_PUBLIC);
}
static VALUE
backtrace(int lev)
{
return th_backtrace(GET_THREAD(), lev);
}
/*
* call-seq:
* caller(start=1) => array
*
* Returns the current execution stack---an array containing strings in
* the form ``<em>file:line</em>'' or ``<em>file:line: in
* `method'</em>''. The optional _start_ parameter
* determines the number of initial stack entries to omit from the
* result.
*
* def a(skip)
* caller(skip)
* end
* def b(skip)
* a(skip)
* end
* def c(skip)
* b(skip)
* end
* c(0) #=> ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10"]
* c(1) #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11"]
* c(2) #=> ["prog:8:in `c'", "prog:12"]
* c(3) #=> ["prog:13"]
*/
static VALUE
rb_f_caller(int argc, VALUE *argv)
{
VALUE level;
int lev;
rb_scan_args(argc, argv, "01", &level);
if (NIL_P(level))
lev = 1;
else
lev = NUM2INT(level);
if (lev < 0)
rb_raise(rb_eArgError, "negative level (%d)", lev);
return backtrace(lev);
}
void
rb_backtrace(void)
{
long i;
VALUE ary;
ary = backtrace(-1);
for (i = 0; i < RARRAY_LEN(ary); i++) {
printf("\tfrom %s\n", RSTRING_PTR(RARRAY_PTR(ary)[i]));
}
}
static VALUE
make_backtrace(void)
{
return backtrace(-1);
}
static ID
frame_func_id(rb_control_frame_t *cfp)
{
rb_iseq_t *iseq = cfp->iseq;
if (!iseq) {
return cfp->method_id;
}
else if (RUBY_VM_IFUNC_P(iseq)) {
return rb_intern("<ifunc>");
}
else {
return rb_intern(RSTRING_PTR(iseq->name));
}
}
ID
rb_frame_this_func(void)
{
return frame_func_id(GET_THREAD()->cfp);
}
ID
rb_frame_callee(void)
{
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
/* check if prev_cfp can be accessible */
if ((void *)(th->stack + th->stack_size) == (void *)(prev_cfp)) {
return 0;
}
return frame_func_id(prev_cfp);
}
void
rb_frame_pop(void)
{
rb_thread_t *th = GET_THREAD();
th->cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
}
static VALUE
rb_frame_self(void)
{
return GET_THREAD()->cfp->self;
}
const char *
rb_sourcefile(void)
{
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *cfp = th_get_ruby_level_cfp(th, th->cfp);
if (cfp) {
return RSTRING_PTR(cfp->iseq->filename);
}
else {
return "";
}
}
int
rb_sourceline(void)
{
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *cfp = th_get_ruby_level_cfp(th, th->cfp);
if (cfp) {
return th_get_sourceline(cfp);
}
else {
return 0;
}
}
static VALUE
eval(VALUE self, VALUE src, VALUE scope, char *file, int line)
{
int state;
VALUE result = Qundef;
VALUE envval;
rb_binding_t *bind = 0;
rb_thread_t *th = GET_THREAD();
rb_env_t *env = NULL;
NODE *stored_cref_stack = 0;
if (file == 0) {
ruby_set_current_source();
file = ruby_sourcefile;
line = ruby_sourceline;
}
PUSH_TAG();
if ((state = EXEC_TAG()) == 0) {
rb_iseq_t *iseq;
VALUE iseqval;
if (scope != Qnil) {
if (CLASS_OF(scope) == rb_cBinding) {
GetBindingPtr(scope, bind);
envval = bind->env;
stored_cref_stack = bind->cref_stack;
}
else {
rb_raise(rb_eTypeError,
"wrong argument type %s (expected Binding)",
rb_obj_classname(scope));
}
GetEnvPtr(envval, env);
th->base_block = &env->block;
}
else {
rb_control_frame_t *cfp = th_get_ruby_level_cfp(th, th->cfp);
th->base_block = RUBY_VM_GET_BLOCK_PTR_IN_CFP(cfp);
th->base_block->iseq = cfp->iseq; /* TODO */
}
/* make eval iseq */
th->parse_in_eval++;
iseqval = th_compile(th, src, rb_str_new2(file), INT2FIX(line));
th->parse_in_eval--;
th_set_eval_stack(th, iseqval);
th->base_block = 0;
if (0) { /* for debug */
extern VALUE ruby_iseq_disasm(VALUE);
printf("%s\n", RSTRING_PTR(ruby_iseq_disasm(iseqval)));
}
/* save new env */
GetISeqPtr(iseqval, iseq);
if (bind && iseq->local_size > 0) {
bind->env = th_make_env_object(th, th->cfp);
}
/* push tag */
if (stored_cref_stack) {
stored_cref_stack =
th_set_special_cref(th, env->block.lfp, stored_cref_stack);
}
/* kick */
result = th_eval_body(th);
}
POP_TAG();
if (stored_cref_stack) {
th_set_special_cref(th, env->block.lfp, stored_cref_stack);
}
if (state) {
if (state == TAG_RAISE) {
if (strcmp(file, "(eval)") == 0) {
VALUE mesg, errat;
errat = get_backtrace(GET_THREAD()->errinfo);
mesg = rb_attr_get(GET_THREAD()->errinfo, rb_intern("mesg"));
if (!NIL_P(errat) && TYPE(errat) == T_ARRAY) {
if (!NIL_P(mesg) && TYPE(mesg) == T_STRING) {
rb_str_update(mesg, 0, 0, rb_str_new2(": "));
rb_str_update(mesg, 0, 0, RARRAY_PTR(errat)[0]);
}
RARRAY_PTR(errat)[0] = RARRAY_PTR(backtrace(-2))[0];
}
}
rb_exc_raise(GET_THREAD()->errinfo);
}
JUMP_TAG(state);
}
return result;
}
/*
* call-seq:
* eval(string [, binding [, filename [,lineno]]]) => obj
*
* Evaluates the Ruby expression(s) in <em>string</em>. If
* <em>binding</em> is given, the evaluation is performed in its
* context. The binding may be a <code>Binding</code> object or a
* <code>Proc</code> object. If the optional <em>filename</em> and
* <em>lineno</em> parameters are present, they will be used when
* reporting syntax errors.
*
* def getBinding(str)
* return binding
* end
* str = "hello"
* eval "str + ' Fred'" #=> "hello Fred"
* eval "str + ' Fred'", getBinding("bye") #=> "bye Fred"
*/
VALUE
rb_f_eval(int argc, VALUE *argv, VALUE self)
{
VALUE src, scope, vfile, vline;
char *file = "(eval)";
int line = 1;
rb_scan_args(argc, argv, "13", &src, &scope, &vfile, &vline);
if (rb_safe_level() >= 4) {
StringValue(src);
if (!NIL_P(scope) && !OBJ_TAINTED(scope)) {
rb_raise(rb_eSecurityError,
"Insecure: can't modify trusted binding");
}
}
else {
SafeStringValue(src);
}
if (argc >= 3) {
StringValue(vfile);
}
if (argc >= 4) {
line = NUM2INT(vline);
}
if (!NIL_P(vfile))
file = RSTRING_PTR(vfile);
return eval(self, src, scope, file, line);
}
VALUE *th_cfp_svar(rb_control_frame_t *cfp, int idx);
/* function to call func under the specified class/module context */
static VALUE
exec_under(VALUE (*func) (VALUE), VALUE under, VALUE self, VALUE args)
{
VALUE val = Qnil; /* OK */
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *cfp = th->cfp;
rb_control_frame_t *pcfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
VALUE stored_self = pcfp->self;
NODE *stored_cref = 0;
NODE **pcref = 0;
rb_block_t block;
rb_block_t *blockptr;
int state;
/* replace environment */
pcfp->self = self;
if ((blockptr = GC_GUARDED_PTR_REF(*th->cfp->lfp)) != 0) {
/* copy block info */
/* TODO: why? */
block = *blockptr;
block.self = self;
*th->cfp->lfp = GC_GUARDED_PTR(&block);
}
while (!RUBY_VM_NORMAL_ISEQ_P(cfp->iseq)) {
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
}
pcref = (NODE **) th_cfp_svar(cfp, -1);
stored_cref = *pcref;
*pcref = th_cref_push(th, under, NOEX_PUBLIC);
PUSH_TAG();
if ((state = EXEC_TAG()) == 0) {
val = (*func) (args);
}
POP_TAG();
/* restore environment */
*pcref = stored_cref;
pcfp->self = stored_self;
if (state) {
JUMP_TAG(state);
}
return val;
}
static VALUE
yield_under_i(VALUE arg)
{
if (arg == Qundef) {
return rb_yield_0(0, 0);
}
else {
return rb_yield_0(RARRAY_LEN(arg), RARRAY_PTR(arg));
}
}
/* block eval under the class/module context */
static VALUE
yield_under(VALUE under, VALUE self, VALUE values)
{
return exec_under(yield_under_i, under, self, values);
}
static VALUE
eval_under_i(VALUE arg)
{
VALUE *args = (VALUE *)arg;
return eval(args[0], args[1], Qnil, (char *)args[2], (int)args[3]);
}
/* string eval under the class/module context */
static VALUE
eval_under(VALUE under, VALUE self, VALUE src, const char *file, int line)
{
VALUE args[4];
if (rb_safe_level() >= 4) {
StringValue(src);
}
else {
SafeStringValue(src);
}
args[0] = self;
args[1] = src;
args[2] = (VALUE)file;
args[3] = (VALUE)line;
return exec_under(eval_under_i, under, self, (VALUE)args);
}
static VALUE
specific_eval(int argc, VALUE *argv, VALUE klass, VALUE self)
{
if (rb_block_given_p()) {
if (argc > 0) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)",
argc);
}
return yield_under(klass, self, Qundef);
}
else {
char *file = "(eval)";
int line = 1;
if (argc == 0) {
rb_raise(rb_eArgError, "block not supplied");
}
else {
if (rb_safe_level() >= 4) {
StringValue(argv[0]);
}
else {
SafeStringValue(argv[0]);
}
if (argc > 3) {
const char *name = rb_id2name(rb_frame_callee());
rb_raise(rb_eArgError,
"wrong number of arguments: %s(src) or %s{..}",
name, name);
}
if (argc > 2)
line = NUM2INT(argv[2]);
if (argc > 1) {
file = StringValuePtr(argv[1]);
}
}
return eval_under(klass, self, argv[0], file, line);
}
}
/*
* call-seq:
* obj.instance_eval(string [, filename [, lineno]] ) => obj
* obj.instance_eval {| | block } => obj
*
* Evaluates a string containing Ruby source code, or the given block,
* within the context of the receiver (_obj_). In order to set the
* context, the variable +self+ is set to _obj_ while
* the code is executing, giving the code access to _obj_'s
* instance variables. In the version of <code>instance_eval</code>
* that takes a +String+, the optional second and third
* parameters supply a filename and starting line number that are used
* when reporting compilation errors.
*
* class Klass
* def initialize
* @secret = 99
* end
* end
* k = Klass.new
* k.instance_eval { @secret } #=> 99
*/
VALUE
rb_obj_instance_eval(int argc, VALUE *argv, VALUE self)
{
VALUE klass;
if (SPECIAL_CONST_P(self)) {
klass = Qnil;
}
else {
klass = rb_singleton_class(self);
}
return specific_eval(argc, argv, klass, self);
}
/*
* call-seq:
* obj.instance_exec(arg...) {|var...| block } => obj
*
* Executes the given block within the context of the receiver
* (_obj_). In order to set the context, the variable +self+ is set
* to _obj_ while the code is executing, giving the code access to
* _obj_'s instance variables. Arguments are passed as block parameters.
*
* class Klass
* def initialize
* @secret = 99
* end
* end
* k = Klass.new
* k.instance_exec(5) {|x| @secret+x } #=> 104
*/
VALUE
rb_obj_instance_exec(int argc, VALUE *argv, VALUE self)
{
VALUE klass;
if (SPECIAL_CONST_P(self)) {
klass = Qnil;
}
else {
klass = rb_singleton_class(self);
}
return yield_under(klass, self, rb_ary_new4(argc, argv));
}
/*
* call-seq:
* mod.class_eval(string [, filename [, lineno]]) => obj
* mod.module_eval {|| block } => obj
*
* Evaluates the string or block in the context of _mod_. This can
* be used to add methods to a class. <code>module_eval</code> returns
* the result of evaluating its argument. The optional _filename_
* and _lineno_ parameters set the text for error messages.
*
* class Thing
* end
* a = %q{def hello() "Hello there!" end}
* Thing.module_eval(a)
* puts Thing.new.hello()
* Thing.module_eval("invalid code", "dummy", 123)
*
* <em>produces:</em>
*
* Hello there!
* dummy:123:in `module_eval': undefined local variable
* or method `code' for Thing:Class
*/
VALUE
rb_mod_module_eval(int argc, VALUE *argv, VALUE mod)
{
return specific_eval(argc, argv, mod, mod);
}
/*
* call-seq:
* mod.module_exec(arg...) {|var...| block } => obj
* mod.class_exec(arg...) {|var...| block } => obj
*
* Evaluates the given block in the context of the class/module.
* The method defined in the block will belong to the receiver.
*
* class Thing
* end
* Thing.class_exec{
* def hello() "Hello there!" end
* }
* puts Thing.new.hello()
*
* <em>produces:</em>
*
* Hello there!
*/
VALUE
rb_mod_module_exec(int argc, VALUE *argv, VALUE mod)
{
return yield_under(mod, mod, rb_ary_new4(argc, argv));
}
static void
secure_visibility(VALUE self)
{
if (rb_safe_level() >= 4 && !OBJ_TAINTED(self)) {
rb_raise(rb_eSecurityError,
"Insecure: can't change method visibility");
}
}
static void
set_method_visibility(VALUE self, int argc, VALUE *argv, ID ex)
{
int i;
secure_visibility(self);
for (i = 0; i < argc; i++) {
rb_export_method(self, rb_to_id(argv[i]), ex);
}
rb_clear_cache_by_class(self);
}
/*
* call-seq:
* public => self
* public(symbol, ...) => self
*
* With no arguments, sets the default visibility for subsequently
* defined methods to public. With arguments, sets the named methods to
* have public visibility.
*/
static VALUE
rb_mod_public(int argc, VALUE *argv, VALUE module)
{
secure_visibility(module);
if (argc == 0) {
SCOPE_SET(NOEX_PUBLIC);
}
else {
set_method_visibility(module, argc, argv, NOEX_PUBLIC);
}
return module;
}
/*
* call-seq:
* protected => self
* protected(symbol, ...) => self
*
* With no arguments, sets the default visibility for subsequently
* defined methods to protected. With arguments, sets the named methods
* to have protected visibility.
*/
static VALUE
rb_mod_protected(int argc, VALUE *argv, VALUE module)
{
secure_visibility(module);
if (argc == 0) {
SCOPE_SET(NOEX_PROTECTED);
}
else {
set_method_visibility(module, argc, argv, NOEX_PROTECTED);
}
return module;
}
/*
* call-seq:
* private => self
* private(symbol, ...) => self
*
* With no arguments, sets the default visibility for subsequently
* defined methods to private. With arguments, sets the named methods
* to have private visibility.
*
* module Mod
* def a() end
* def b() end
* private
* def c() end
* private :a
* end
* Mod.private_instance_methods #=> ["a", "c"]
*/
static VALUE
rb_mod_private(int argc, VALUE *argv, VALUE module)
{
secure_visibility(module);
if (argc == 0) {
SCOPE_SET(NOEX_PRIVATE);
}
else {
set_method_visibility(module, argc, argv, NOEX_PRIVATE);
}
return module;
}
/*
* call-seq:
* mod.public_class_method(symbol, ...) => mod
*
* Makes a list of existing class methods public.
*/
static VALUE
rb_mod_public_method(int argc, VALUE *argv, VALUE obj)
{
set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PUBLIC);
return obj;
}
/*
* call-seq:
* mod.private_class_method(symbol, ...) => mod
*
* Makes existing class methods private. Often used to hide the default
* constructor <code>new</code>.
*
* class SimpleSingleton # Not thread safe
* private_class_method :new
* def SimpleSingleton.create(*args, &block)
* @me = new(*args, &block) if ! @me
* @me
* end
* end
*/
static VALUE
rb_mod_private_method(int argc, VALUE *argv, VALUE obj)
{
set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PRIVATE);
return obj;
}
/*
* call-seq:
* public
* public(symbol, ...)
*
* With no arguments, sets the default visibility for subsequently
* defined methods to public. With arguments, sets the named methods to
* have public visibility.
*/
static VALUE
top_public(int argc, VALUE *argv)
{
return rb_mod_public(argc, argv, rb_cObject);
}
static VALUE
top_private(int argc, VALUE *argv)
{
return rb_mod_private(argc, argv, rb_cObject);
}
/*
* call-seq:
* module_function(symbol, ...) => self
*
* Creates module functions for the named methods. These functions may
* be called with the module as a receiver, and also become available
* as instance methods to classes that mix in the module. Module
* functions are copies of the original, and so may be changed
* independently. The instance-method versions are made private. If
* used with no arguments, subsequently defined methods become module
* functions.
*
* module Mod
* def one
* "This is one"
* end
* module_function :one
* end
* class Cls
* include Mod
* def callOne
* one
* end
* end
* Mod.one #=> "This is one"
* c = Cls.new
* c.callOne #=> "This is one"
* module Mod
* def one
* "This is the new one"
* end
* end
* Mod.one #=> "This is one"
* c.callOne #=> "This is the new one"
*/
static VALUE
rb_mod_modfunc(int argc, VALUE *argv, VALUE module)
{
int i;
ID id;
NODE *fbody;
if (TYPE(module) != T_MODULE) {
rb_raise(rb_eTypeError, "module_function must be called for modules");
}
secure_visibility(module);
if (argc == 0) {
SCOPE_SET(NOEX_MODFUNC);
return module;
}
set_method_visibility(module, argc, argv, NOEX_PRIVATE);
for (i = 0; i < argc; i++) {
VALUE m = module;
id = rb_to_id(argv[i]);
for (;;) {
fbody = search_method(m, id, &m);
if (fbody == 0) {
fbody = search_method(rb_cObject, id, &m);
}
if (fbody == 0 || fbody->nd_body == 0) {
rb_bug("undefined method `%s'; can't happen", rb_id2name(id));
}
if (nd_type(fbody->nd_body->nd_body) != NODE_ZSUPER) {
break; /* normal case: need not to follow 'super' link */
}
m = RCLASS(m)->super;
if (!m)
break;
}
rb_add_method(rb_singleton_class(module), id, fbody->nd_body->nd_body,
NOEX_PUBLIC);
}
return module;
}
/*
* call-seq:
* append_features(mod) => mod
*
* When this module is included in another, Ruby calls
* <code>append_features</code> in this module, passing it the
* receiving module in _mod_. Ruby's default implementation is
* to add the constants, methods, and module variables of this module
* to _mod_ if this module has not already been added to
* _mod_ or one of its ancestors. See also <code>Module#include</code>.
*/
static VALUE
rb_mod_append_features(VALUE module, VALUE include)
{
switch (TYPE(include)) {
case T_CLASS:
case T_MODULE:
break;
default:
Check_Type(include, T_CLASS);
break;
}
rb_include_module(include, module);
return module;
}
/*
* call-seq:
* include(module, ...) => self
*
* Invokes <code>Module.append_features</code> on each parameter in turn.
*/
static VALUE
rb_mod_include(int argc, VALUE *argv, VALUE module)
{
int i;
for (i = 0; i < argc; i++)
Check_Type(argv[i], T_MODULE);
while (argc--) {
rb_funcall(argv[argc], rb_intern("append_features"), 1, module);
rb_funcall(argv[argc], rb_intern("included"), 1, module);
}
return module;
}
void
rb_obj_call_init(VALUE obj, int argc, VALUE *argv)
{
PASS_PASSED_BLOCK();
rb_funcall2(obj, init, argc, argv);
}
void
rb_extend_object(VALUE obj, VALUE module)
{
rb_include_module(rb_singleton_class(obj), module);
}
/*
* call-seq:
* extend_object(obj) => obj
*
* Extends the specified object by adding this module's constants and
* methods (which are added as singleton methods). This is the callback
* method used by <code>Object#extend</code>.
*
* module Picky
* def Picky.extend_object(o)
* if String === o
* puts "Can't add Picky to a String"
* else
* puts "Picky added to #{o.class}"
* super
* end
* end
* end
* (s = Array.new).extend Picky # Call Object.extend
* (s = "quick brown fox").extend Picky
*
* <em>produces:</em>
*
* Picky added to Array
* Can't add Picky to a String
*/
static VALUE
rb_mod_extend_object(VALUE mod, VALUE obj)
{
rb_extend_object(obj, mod);
return obj;
}
/*
* call-seq:
* obj.extend(module, ...) => obj
*
* Adds to _obj_ the instance methods from each module given as a
* parameter.
*
* module Mod
* def hello
* "Hello from Mod.\n"
* end
* end
*
* class Klass
* def hello
* "Hello from Klass.\n"
* end
* end
*
* k = Klass.new
* k.hello #=> "Hello from Klass.\n"
* k.extend(Mod) #=> #<Klass:0x401b3bc8>
* k.hello #=> "Hello from Mod.\n"
*/
static VALUE
rb_obj_extend(int argc, VALUE *argv, VALUE obj)
{
int i;
if (argc == 0) {
rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)");
}
for (i = 0; i < argc; i++)
Check_Type(argv[i], T_MODULE);
while (argc--) {
rb_funcall(argv[argc], rb_intern("extend_object"), 1, obj);
rb_funcall(argv[argc], rb_intern("extended"), 1, obj);
}
return obj;
}
/*
* call-seq:
* include(module, ...) => self
*
* Invokes <code>Module.append_features</code>
* on each parameter in turn. Effectively adds the methods and constants
* in each module to the receiver.
*/
static VALUE
top_include(int argc, VALUE *argv, VALUE self)
{
rb_thread_t *th = GET_THREAD();
rb_secure(4);
if (th->top_wrapper) {
rb_warning
("main#include in the wrapped load is effective only in wrapper module");
return rb_mod_include(argc, argv, th->top_wrapper);
}
return rb_mod_include(argc, argv, rb_cObject);
}
VALUE rb_f_trace_var();
VALUE rb_f_untrace_var();
static VALUE
get_errinfo(void)
{
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *cfp = th->cfp;
rb_control_frame_t *end_cfp = RUBY_VM_END_CONTROL_FRAME(th);
while (RUBY_VM_VALID_CONTROL_FRAME_P(cfp, end_cfp)) {
if (RUBY_VM_NORMAL_ISEQ_P(cfp->iseq)) {
if (cfp->iseq->type == ISEQ_TYPE_RESCUE) {
return cfp->dfp[-1];
}
else if (cfp->iseq->type == ISEQ_TYPE_ENSURE &&
TYPE(cfp->dfp[-1]) != T_NODE) {
return cfp->dfp[-1];
}
}
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
}
return Qnil;
}
static VALUE
errinfo_getter(ID id)
{
return get_errinfo();
}
VALUE
rb_errinfo(void)
{
return get_errinfo();
}
static void
errinfo_setter(VALUE val, ID id, VALUE *var)
{
if (!NIL_P(val) && !rb_obj_is_kind_of(val, rb_eException)) {
rb_raise(rb_eTypeError, "assigning non-exception to $!");
}
else {
GET_THREAD()->errinfo = val;
}
}
void
rb_set_errinfo(VALUE err)
{
errinfo_setter(err, 0, 0);
}
static VALUE
errat_getter(ID id)
{
VALUE err = get_errinfo();
if (!NIL_P(err)) {
return get_backtrace(err);
}
else {
return Qnil;
}
}
static void
errat_setter(VALUE val, ID id, VALUE *var)
{
VALUE err = get_errinfo();
if (NIL_P(err)) {
rb_raise(rb_eArgError, "$! not set");
}
set_backtrace(err, val);
}
/*
* call-seq:
* local_variables => array
*
* Returns the names of the current local variables.
*
* fred = 1
* for i in 1..10
* # ...
* end
* local_variables #=> ["fred", "i"]
*/
int th_collect_local_variables_in_heap(rb_thread_t *th, VALUE *dfp, VALUE ary);
static VALUE
rb_f_local_variables(void)
{
VALUE ary = rb_ary_new();
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *cfp =
th_get_ruby_level_cfp(th, RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp));
int i;
while (1) {
if (cfp->iseq) {
for (i = 0; i < cfp->iseq->local_table_size; i++) {
ID lid = cfp->iseq->local_table[i];
if (lid) {
const char *vname = rb_id2name(lid);
/* should skip temporary variable */
if (vname) {
rb_ary_push(ary, rb_str_new2(vname));
}
}
}
}
if (cfp->lfp != cfp->dfp) {
/* block */
VALUE *dfp = GC_GUARDED_PTR_REF(cfp->dfp[0]);
if (th_collect_local_variables_in_heap(th, dfp, ary)) {
break;
}
else {
while (cfp->dfp != dfp) {
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
}
}
}
else {
break;
}
}
return ary;
}
/*
* call-seq:
* __method__ => symbol
*
* Returns the name of the current method as a Symbol.
* If called from inside of an aliased method it will return the original
* nonaliased name.
* If called outside of a method, it returns <code>nil</code>.
* See also <code>\_\_callee__</code>.
*
*/
static VALUE
rb_f_method_name(void)
{
ID fname = rb_frame_callee();
if (fname) {
return ID2SYM(fname);
}
else {
return Qnil;
}
}
/*
* call-seq:
* __callee__ => symbol
*
* Returns the name of the current method as Symbol.
* If called from inside of an aliased method it will return the aliased
* name.
* If called outside of a method, it returns <code>nil</code>.
* See also <code>\_\_method__</code>.
*
*/
static VALUE
rb_f_callee_name(void)
{
/* xxx need to get callee name */
ID callee = rb_frame_callee();
if (callee) {
return ID2SYM(callee);
}
else {
return Qnil;
}
}
void
Init_eval(void)
{
/* TODO: fix position */
GET_THREAD()->vm->mark_object_ary = rb_ary_new();
init = rb_intern("initialize");
eqq = rb_intern("===");
each = rb_intern("each");
aref = rb_intern("[]");
aset = rb_intern("[]=");
match = rb_intern("=~");
missing = rb_intern("method_missing");
added = rb_intern("method_added");
singleton_added = rb_intern("singleton_method_added");
removed = rb_intern("method_removed");
singleton_removed = rb_intern("singleton_method_removed");
undefined = rb_intern("method_undefined");
singleton_undefined = rb_intern("singleton_method_undefined");
object_id = rb_intern("object_id");
__send = rb_intern("__send");
__send_bang = rb_intern("__send!");
rb_global_variable((VALUE *)&ruby_eval_tree);
rb_define_virtual_variable("$@", errat_getter, errat_setter);
rb_define_virtual_variable("$!", errinfo_getter, errinfo_setter);
rb_define_global_function("eval", rb_f_eval, -1);
rb_define_global_function("iterator?", rb_f_block_given_p, 0);
rb_define_global_function("block_given?", rb_f_block_given_p, 0);
rb_define_global_function("method_missing", rb_method_missing, -1);
rb_define_global_function("loop", rb_f_loop, 0);
rb_define_method(rb_mKernel, "respond_to?", obj_respond_to, -1);
respond_to = rb_intern("respond_to?");
basic_respond_to = rb_method_node(rb_cObject, respond_to);
rb_register_mark_object((VALUE)basic_respond_to);
rb_define_global_function("raise", rb_f_raise, -1);
rb_define_global_function("fail", rb_f_raise, -1);
rb_define_global_function("caller", rb_f_caller, -1);
rb_define_global_function("global_variables", rb_f_global_variables, 0); /* in variable.c */
rb_define_global_function("local_variables", rb_f_local_variables, 0);
rb_define_global_function("__method__", rb_f_method_name, 0);
rb_define_global_function("__callee__", rb_f_callee_name, 0);
rb_define_method(rb_cBasicObject, "send", rb_f_send, -1);
rb_define_method(rb_cBasicObject, "__send__", rb_f_send, -1);
rb_define_method(rb_cBasicObject, "__send", rb_f_send, -1);
rb_define_method(rb_cBasicObject, "funcall", rb_f_funcall, -1);
rb_define_method(rb_cBasicObject, "__send!", rb_f_funcall, -1);
rb_define_method(rb_mKernel, "instance_eval", rb_obj_instance_eval, -1);
rb_define_method(rb_mKernel, "instance_exec", rb_obj_instance_exec, -1);
rb_define_private_method(rb_cModule, "append_features", rb_mod_append_features, 1);
rb_define_private_method(rb_cModule, "extend_object", rb_mod_extend_object, 1);
rb_define_private_method(rb_cModule, "include", rb_mod_include, -1);
rb_define_private_method(rb_cModule, "public", rb_mod_public, -1);
rb_define_private_method(rb_cModule, "protected", rb_mod_protected, -1);
rb_define_private_method(rb_cModule, "private", rb_mod_private, -1);
rb_define_private_method(rb_cModule, "module_function", rb_mod_modfunc, -1);
rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, 1);
rb_define_method(rb_cModule, "public_method_defined?", rb_mod_public_method_defined, 1);
rb_define_method(rb_cModule, "private_method_defined?", rb_mod_private_method_defined, 1);
rb_define_method(rb_cModule, "protected_method_defined?", rb_mod_protected_method_defined, 1);
rb_define_method(rb_cModule, "public_class_method", rb_mod_public_method, -1);
rb_define_method(rb_cModule, "private_class_method", rb_mod_private_method, -1);
rb_define_method(rb_cModule, "module_eval", rb_mod_module_eval, -1);
rb_define_method(rb_cModule, "class_eval", rb_mod_module_eval, -1);
rb_define_method(rb_cModule, "module_exec", rb_mod_module_exec, -1);
rb_define_method(rb_cModule, "class_exec", rb_mod_module_exec, -1);
rb_undef_method(rb_cClass, "module_function");
rb_define_private_method(rb_cModule, "remove_method", rb_mod_remove_method, -1);
rb_define_private_method(rb_cModule, "undef_method", rb_mod_undef_method, -1);
rb_define_private_method(rb_cModule, "alias_method", rb_mod_alias_method, 2);
rb_define_singleton_method(rb_cModule, "nesting", rb_mod_nesting, 0);
rb_define_singleton_method(rb_cModule, "constants", rb_mod_s_constants, -1);
rb_define_singleton_method(ruby_top_self, "include", top_include, -1);
rb_define_singleton_method(ruby_top_self, "public", top_public, -1);
rb_define_singleton_method(ruby_top_self, "private", top_private, -1);
rb_define_method(rb_mKernel, "extend", rb_obj_extend, -1);
rb_define_global_function("trace_var", rb_f_trace_var, -1); /* in variable.c */
rb_define_global_function("untrace_var", rb_f_untrace_var, -1); /* in variable.c */
rb_define_virtual_variable("$SAFE", safe_getter, safe_setter);
}
/* for parser */
int
rb_dvar_defined(ID id)
{
rb_thread_t *th = GET_THREAD();
rb_iseq_t *iseq;
if (th->base_block && (iseq = th->base_block->iseq)) {
while (iseq->type == ISEQ_TYPE_BLOCK ||
iseq->type == ISEQ_TYPE_RESCUE ||
iseq->type == ISEQ_TYPE_ENSURE ||
iseq->type == ISEQ_TYPE_EVAL) {
int i;
for (i = 0; i < iseq->local_table_size; i++) {
if (iseq->local_table[i] == id) {
return 1;
}
}
iseq = iseq->parent_iseq;
}
}
return 0;
}
int
rb_local_defined(ID id)
{
rb_thread_t *th = GET_THREAD();
rb_iseq_t *iseq;
if (th->base_block && th->base_block->iseq) {
int i;
iseq = th->base_block->iseq->local_iseq;
for (i=0; i<iseq->local_table_size; i++) {
if (iseq->local_table[i] == id) {
return 1;
}
}
}
return 0;
}
int
rb_parse_in_eval(void)
{
return GET_THREAD()->parse_in_eval != 0;
}
|