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
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
|
/**********************************************************************
eval.c -
$Author$
$Date$
created at: Thu Jun 10 14:22:17 JST 1993
Copyright (C) 1993-2002 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#include "ruby.h"
#include "node.h"
#include "env.h"
#include "util.h"
#include "rubysig.h"
#include <stdio.h>
#include <setjmp.h>
#include "st.h"
#include "dln.h"
/* Make alloca work the best possible way. */
#ifdef __GNUC__
# ifndef atarist
# ifndef alloca
# define alloca __builtin_alloca
# endif
# endif /* atarist */
#else
# if defined(HAVE_ALLOCA_H)
# include <alloca.h>
# elif !defined(alloca)
char *alloca();
# endif
#endif /* __GNUC__ */
#ifdef _AIX
#pragma alloca
#endif
#ifdef HAVE_STDARG_PROTOTYPES
#include <stdarg.h>
#define va_init_list(a,b) va_start(a,b)
#else
#include <varargs.h>
#define va_init_list(a,b) va_start(a)
#endif
#ifndef HAVE_STRING_H
char *strrchr _((const char*,const char));
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef __BEOS__
#include <net/socket.h>
#endif
#ifdef __MACOS__
#include "macruby_private.h"
#endif
#ifndef setjmp
#ifdef HAVE__SETJMP
#define setjmp(env) _setjmp(env)
#define longjmp(env,val) _longjmp(env,val)
#endif
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#else
#ifndef NT
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* and microseconds */
};
#endif /* NT */
#endif
#include <signal.h>
#include <errno.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <sys/stat.h>
VALUE rb_cProc;
static VALUE rb_cBinding;
static VALUE proc_invoke _((VALUE,VALUE,int,VALUE));
static VALUE rb_f_binding _((VALUE));
static void rb_f_END _((void));
static VALUE rb_f_block_given_p _((void));
static VALUE block_pass _((VALUE,NODE*));
static VALUE rb_cMethod;
static VALUE method_proc _((VALUE));
static VALUE method_call _((int, VALUE*, VALUE));
static VALUE rb_cUnboundMethod;
static VALUE umethod_bind _((VALUE, VALUE));
static VALUE rb_mod_define_method _((int, VALUE*, VALUE));
static int scope_vmode;
#define SCOPE_PUBLIC 0
#define SCOPE_PRIVATE 1
#define SCOPE_PROTECTED 2
#define SCOPE_MODFUNC 5
#define SCOPE_MASK 7
#define SCOPE_SET(f) do {scope_vmode=(f);} while(0)
#define SCOPE_TEST(f) (scope_vmode&(f))
int ruby_safe_level = 0;
/* safe-level:
0 - strings from streams/environment/ARGV are tainted (default)
1 - no dangerous operation by tainted value
2 - process/file operations prohibited
3 - all genetated objects are tainted
4 - no global (non-tainted) variable modification/no direct output
*/
static VALUE safe_getter _((void));
static void safe_setter _((VALUE val));
void
rb_secure(level)
int level;
{
if (level <= ruby_safe_level) {
rb_raise(rb_eSecurityError, "Insecure operation `%s' at level %d",
rb_id2name(ruby_frame->last_func), ruby_safe_level);
}
}
void
rb_check_safe_str(x)
VALUE x;
{
if (ruby_safe_level > 0 && OBJ_TAINTED(x)){
if (ruby_frame->last_func) {
rb_raise(rb_eSecurityError, "Insecure operation - %s",
rb_id2name(ruby_frame->last_func));
}
else {
rb_raise(rb_eSecurityError, "Insecure operation: -r");
}
}
rb_secure(4);
if (TYPE(x)!= T_STRING) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected String)",
rb_class2name(CLASS_OF(x)));
}
}
NORETURN(static void print_undef _((VALUE, ID)));
static void
print_undef(klass, id)
VALUE klass;
ID id;
{
rb_name_error(id, "undefined method `%s' for %s `%s'",
rb_id2name(id),
(TYPE(klass) == T_MODULE) ? "module" : "class",
rb_class2name(klass));
}
static ID removed, singleton_removed, undefined, singleton_undefined;
#define CACHE_SIZE 0x800
#define CACHE_MASK 0x7ff
#define EXPR1(c,m) ((((c)>>3)^(m))&CACHE_MASK)
struct cache_entry { /* method hash table. */
ID mid; /* method's id */
ID mid0; /* method's original id */
VALUE klass; /* receiver's class */
VALUE origin; /* where method defined */
NODE *method;
int noex;
};
static struct cache_entry cache[CACHE_SIZE];
void
rb_clear_cache()
{
struct cache_entry *ent, *end;
ent = cache; end = ent + CACHE_SIZE;
while (ent < end) {
ent->mid = 0;
ent++;
}
}
static void
rb_clear_cache_by_id(id)
ID id;
{
struct cache_entry *ent, *end;
ent = cache; end = ent + CACHE_SIZE;
while (ent < end) {
if (ent->mid == id) {
ent->mid = 0;
}
ent++;
}
}
void
rb_add_method(klass, mid, node, noex)
VALUE klass;
ID mid;
NODE *node;
int noex;
{
NODE *body;
if (NIL_P(klass)) klass = rb_cObject;
if (rb_safe_level() >= 4 && (klass == rb_cObject || !OBJ_TAINTED(klass))) {
rb_raise(rb_eSecurityError, "Insecure: can't define method");
}
if (OBJ_FROZEN(klass)) rb_error_frozen("class/module");
rb_clear_cache_by_id(mid);
body = NEW_METHOD(node, noex);
st_insert(RCLASS(klass)->m_tbl, mid, body);
}
static NODE*
search_method(klass, id, origin)
VALUE klass, *origin;
ID id;
{
NODE *body;
if (!klass) return 0;
while (!st_lookup(RCLASS(klass)->m_tbl, id, &body)) {
klass = RCLASS(klass)->super;
if (!klass) return 0;
}
if (origin) *origin = klass;
return body;
}
static NODE*
rb_get_method_body(klassp, idp, noexp)
VALUE *klassp;
ID *idp;
int *noexp;
{
ID id = *idp;
VALUE klass = *klassp;
VALUE origin;
NODE * volatile body;
struct cache_entry *ent;
if ((body = search_method(klass, id, &origin)) == 0 || !body->nd_body) {
/* store empty info in cache */
ent = cache + EXPR1(klass, id);
ent->klass = klass;
ent->origin = klass;
ent->mid = ent->mid0 = id;
ent->noex = 0;
ent->method = 0;
return 0;
}
/* store in cache */
ent = cache + EXPR1(klass, id);
ent->klass = klass;
ent->noex = body->nd_noex;
body = body->nd_body;
if (nd_type(body) == NODE_FBODY) {
ent->mid = id;
*klassp = body->nd_orig;
ent->origin = body->nd_orig;
*idp = ent->mid0 = body->nd_mid;
body = ent->method = body->nd_head;
}
else {
*klassp = origin;
ent->origin = origin;
ent->mid = ent->mid0 = id;
ent->method = body;
}
if (noexp) *noexp = ent->noex;
return body;
}
static void
remove_method(klass, mid)
VALUE klass;
ID mid;
{
NODE *body;
if (klass == rb_cObject) {
rb_secure(4);
}
if (rb_safe_level() >= 4 && !OBJ_TAINTED(klass)) {
rb_raise(rb_eSecurityError, "Insecure: can't remove method");
}
if (OBJ_FROZEN(klass)) rb_error_frozen("class/module");
if (!st_delete(RCLASS(klass)->m_tbl, &mid, &body) || !body->nd_body) {
rb_name_error(mid, "method `%s' not defined in %s",
rb_id2name(mid), rb_class2name(klass));
}
rb_clear_cache_by_id(mid);
if (FL_TEST(klass, FL_SINGLETON)) {
rb_funcall(rb_iv_get(klass, "__attached__"), singleton_removed, 1, ID2SYM(mid));
}
else {
rb_funcall(klass, removed, 1, ID2SYM(mid));
}
}
void
rb_remove_method(klass, name)
VALUE klass;
const char *name;
{
remove_method(klass, rb_intern(name));
}
static VALUE
rb_mod_remove_method(mod, name)
VALUE mod, name;
{
remove_method(mod, rb_to_id(name));
return mod;
}
void
rb_disable_super(klass, name)
VALUE klass;
const char *name;
{
VALUE origin;
NODE *body;
ID mid = rb_intern(name);
body = search_method(klass, mid, &origin);
if (!body || !body->nd_body) {
print_undef(klass, mid);
}
if (origin == klass) {
body->nd_noex |= NOEX_UNDEF;
}
else {
rb_add_method(klass, mid, 0, NOEX_UNDEF);
}
}
void
rb_enable_super(klass, name)
VALUE klass;
const char *name;
{
VALUE origin;
NODE *body;
ID mid = rb_intern(name);
body = search_method(klass, mid, &origin);
if (!body) {
print_undef(klass, mid);
}
if (!body->nd_body) {
remove_method(klass, mid);
}
else {
body->nd_noex &= ~NOEX_UNDEF;
}
}
static void
rb_export_method(klass, name, noex)
VALUE klass;
ID name;
ID noex;
{
NODE *body;
VALUE origin;
if (klass == rb_cObject) {
rb_secure(4);
}
body = search_method(klass, name, &origin);
if (!body && TYPE(klass) == T_MODULE) {
body = search_method(rb_cObject, name, &origin);
}
if (!body) {
print_undef(klass, name);
}
if (body->nd_noex != noex) {
if (klass == origin) {
body->nd_noex = noex;
}
else {
rb_add_method(klass, name, NEW_ZSUPER(), noex);
}
}
}
int
rb_method_boundp(klass, id, ex)
VALUE klass;
ID id;
int ex;
{
struct cache_entry *ent;
int noex;
/* is it in the method cache? */
ent = cache + EXPR1(klass, id);
if (ent->mid == id && ent->klass == klass) {
if (ex && (ent->noex & NOEX_PRIVATE))
return Qfalse;
if (!ent->method) return Qfalse;
return Qtrue;
}
if (rb_get_method_body(&klass, &id, &noex)) {
if (ex && (noex & NOEX_PRIVATE))
return Qfalse;
return Qtrue;
}
return Qfalse;
}
static ID init, alloc, eqq, each, aref, aset, match, to_ary, missing;
static ID added, singleton_added;
static ID __id__, __send__;
void
rb_attr(klass, id, read, write, ex)
VALUE klass;
ID id;
int read, write, ex;
{
const char *name;
char *buf;
ID attriv;
int noex;
if (!ex) noex = NOEX_PUBLIC;
else {
if (SCOPE_TEST(SCOPE_PRIVATE)) {
noex = NOEX_PRIVATE;
rb_warning((scope_vmode == SCOPE_MODFUNC) ?
"attribute accessor as module_function" :
"private attribute?");
}
else if (SCOPE_TEST(SCOPE_PROTECTED)) {
noex = NOEX_PROTECTED;
}
else {
noex = NOEX_PUBLIC;
}
}
name = rb_id2name(id);
if (!name) {
rb_raise(rb_eArgError, "argument needs to be symbol or string");
}
buf = ALLOCA_N(char,strlen(name)+2);
sprintf(buf, "@%s", name);
attriv = rb_intern(buf);
if (read) {
rb_add_method(klass, id, NEW_IVAR(attriv), noex);
rb_funcall(klass, added, 1, ID2SYM(id));
}
if (write) {
sprintf(buf, "%s=", name);
id = rb_intern(buf);
rb_add_method(klass, id, NEW_ATTRSET(attriv), noex);
rb_funcall(klass, added, 1, ID2SYM(id));
}
}
extern int ruby_in_compile;
VALUE ruby_errinfo = Qnil;
extern NODE *ruby_eval_tree_begin;
extern NODE *ruby_eval_tree;
extern int ruby_nerrs;
static VALUE rb_eLocalJumpError;
static VALUE rb_eSysStackError;
extern VALUE ruby_top_self;
struct FRAME *ruby_frame;
struct SCOPE *ruby_scope;
static struct FRAME *top_frame;
static struct SCOPE *top_scope;
#define PUSH_FRAME() { \
struct FRAME _frame; \
_frame.prev = ruby_frame; \
_frame.tmp = 0; \
_frame.file = ruby_sourcefile; \
_frame.line = ruby_sourceline; \
_frame.iter = ruby_iter->iter; \
_frame.cbase = ruby_frame->cbase; \
_frame.argc = 0; \
_frame.argv = 0; \
_frame.flags = FRAME_ALLOCA; \
ruby_frame = &_frame; \
#define POP_FRAME() \
ruby_sourcefile = _frame.file; \
ruby_sourceline = _frame.line; \
ruby_frame = _frame.prev; }
struct BLOCKTAG {
struct RBasic super;
long dst;
long flags;
};
struct BLOCK {
NODE *var;
NODE *body;
VALUE self;
struct FRAME frame;
struct SCOPE *scope;
struct BLOCKTAG *tag;
VALUE klass;
int iter;
int vmode;
int flags;
struct RVarmap *dyna_vars;
VALUE orig_thread;
VALUE wrapper;
struct BLOCK *prev;
};
#define BLOCK_D_SCOPE 1
#define BLOCK_DYNAMIC 2
#define BLOCK_ORPHAN 4
static struct BLOCK *ruby_block;
static struct BLOCKTAG*
new_blktag()
{
NEWOBJ(blktag, struct BLOCKTAG);
OBJSETUP(blktag, 0, T_BLKTAG);
blktag->dst = 0;
blktag->flags = 0;
return blktag;
}
#define PUSH_BLOCK(v,b) { \
struct BLOCK _block; \
_block.tag = new_blktag(); \
_block.var = v; \
_block.body = b; \
_block.self = self; \
_block.frame = *ruby_frame; \
_block.klass = ruby_class; \
_block.frame.file = ruby_sourcefile;\
_block.frame.line = ruby_sourceline;\
_block.scope = ruby_scope; \
_block.prev = ruby_block; \
_block.iter = ruby_iter->iter; \
_block.vmode = scope_vmode; \
_block.flags = BLOCK_D_SCOPE; \
_block.dyna_vars = ruby_dyna_vars; \
_block.wrapper = ruby_wrapper; \
ruby_block = &_block;
#define POP_BLOCK() \
if (_block.tag->flags & (BLOCK_DYNAMIC)) \
_block.tag->flags |= BLOCK_ORPHAN; \
else if (!(_block.scope->flags & SCOPE_DONT_RECYCLE)) \
rb_gc_force_recycle((VALUE)_block.tag); \
ruby_block = _block.prev; \
}
struct RVarmap *ruby_dyna_vars;
#define PUSH_VARS() { \
struct RVarmap * volatile _old; \
_old = ruby_dyna_vars; \
ruby_dyna_vars = 0;
#define POP_VARS() \
if (_old && (ruby_scope->flags & SCOPE_DONT_RECYCLE)) {\
if (RBASIC(_old)->flags) /* unless it's already recycled */ \
FL_SET(_old, DVAR_DONT_RECYCLE); \
}\
ruby_dyna_vars = _old; \
}
#define DVAR_DONT_RECYCLE FL_USER2
static struct RVarmap*
new_dvar(id, value, prev)
ID id;
VALUE value;
struct RVarmap *prev;
{
NEWOBJ(vars, struct RVarmap);
OBJSETUP(vars, 0, T_VARMAP);
vars->id = id;
vars->val = value;
vars->next = prev;
return vars;
}
VALUE
rb_dvar_defined(id)
ID id;
{
struct RVarmap *vars = ruby_dyna_vars;
while (vars) {
if (vars->id == id) return Qtrue;
vars = vars->next;
}
return Qfalse;
}
VALUE
rb_dvar_curr(id)
ID id;
{
struct RVarmap *vars = ruby_dyna_vars;
while (vars) {
if (vars->id == 0) break;
if (vars->id == id) return Qtrue;
vars = vars->next;
}
return Qfalse;
}
VALUE
rb_dvar_ref(id)
ID id;
{
struct RVarmap *vars = ruby_dyna_vars;
while (vars) {
if (vars->id == id) {
return vars->val;
}
vars = vars->next;
}
return Qnil;
}
void
rb_dvar_push(id, value)
ID id;
VALUE value;
{
ruby_dyna_vars = new_dvar(id, value, ruby_dyna_vars);
}
static void
dvar_asgn_internal(id, value, curr)
ID id;
VALUE value;
int curr;
{
int n = 0;
struct RVarmap *vars = ruby_dyna_vars;
while (vars) {
if (curr && vars->id == 0) {
/* first null is a dvar header */
n++;
if (n == 2) break;
}
if (vars->id == id) {
vars->val = value;
return;
}
vars = vars->next;
}
if (!ruby_dyna_vars) {
ruby_dyna_vars = new_dvar(id, value, 0);
}
else {
vars = new_dvar(id, value, ruby_dyna_vars->next);
ruby_dyna_vars->next = vars;
}
}
static inline void
dvar_asgn(id, value)
ID id;
VALUE value;
{
dvar_asgn_internal(id, value, 0);
}
static inline void
dvar_asgn_curr(id, value)
ID id;
VALUE value;
{
dvar_asgn_internal(id, value, 1);
}
VALUE *
rb_svar(cnt)
int cnt;
{
struct RVarmap *vars = ruby_dyna_vars;
ID id;
if (!ruby_scope->local_tbl) return NULL;
if (cnt >= ruby_scope->local_tbl[0]) return NULL;
id = ruby_scope->local_tbl[cnt+1];
while (vars) {
if (vars->id == id) return &vars->val;
vars = vars->next;
}
if (ruby_scope->local_vars == 0) return NULL;
return &ruby_scope->local_vars[cnt];
}
struct iter {
int iter;
struct iter *prev;
};
static struct iter *ruby_iter;
#define ITER_NOT 0
#define ITER_PRE 1
#define ITER_CUR 2
#define PUSH_ITER(i) { \
struct iter _iter; \
_iter.prev = ruby_iter; \
_iter.iter = (i); \
ruby_iter = &_iter; \
#define POP_ITER() \
ruby_iter = _iter.prev; \
}
struct tag {
jmp_buf buf;
struct FRAME *frame;
struct iter *iter;
ID tag;
VALUE retval;
struct SCOPE *scope;
int dst;
struct tag *prev;
};
static struct tag *prot_tag;
#define PUSH_TAG(ptag) { \
struct tag _tag; \
_tag.retval = Qnil; \
_tag.frame = ruby_frame; \
_tag.iter = ruby_iter; \
_tag.prev = prot_tag; \
_tag.scope = ruby_scope; \
_tag.tag = ptag; \
_tag.dst = 0; \
prot_tag = &_tag;
#define PROT_NONE 0
#define PROT_FUNC -1
#define PROT_THREAD -2
#define EXEC_TAG() setjmp(prot_tag->buf)
#define JUMP_TAG(st) { \
ruby_frame = prot_tag->frame; \
ruby_iter = prot_tag->iter; \
longjmp(prot_tag->buf,(st)); \
}
#define POP_TAG() \
if (_tag.prev) \
_tag.prev->retval = _tag.retval;\
prot_tag = _tag.prev; \
}
#define POP_TMPTAG() \
prot_tag = _tag.prev; \
}
#define TAG_RETURN 0x1
#define TAG_BREAK 0x2
#define TAG_NEXT 0x3
#define TAG_RETRY 0x4
#define TAG_REDO 0x5
#define TAG_RAISE 0x6
#define TAG_THROW 0x7
#define TAG_FATAL 0x8
#define TAG_MASK 0xf
VALUE ruby_class;
static VALUE ruby_wrapper; /* security wrapper */
#define PUSH_CLASS() { \
VALUE _class = ruby_class; \
#define POP_CLASS() ruby_class = _class; }
static NODE *ruby_cref = 0;
static NODE *top_cref;
#define PUSH_CREF(c) ruby_cref = rb_node_newnode(NODE_CREF,(c),0,ruby_cref)
#define POP_CREF() ruby_cref = ruby_cref->nd_next
#define PUSH_SCOPE() { \
volatile int _vmode = scope_vmode; \
struct SCOPE * volatile _old; \
NEWOBJ(_scope, struct SCOPE); \
OBJSETUP(_scope, 0, T_SCOPE); \
_scope->local_tbl = 0; \
_scope->local_vars = 0; \
_scope->flags = 0; \
_old = ruby_scope; \
ruby_scope = _scope; \
scope_vmode = SCOPE_PUBLIC;
typedef struct thread * rb_thread_t;
static rb_thread_t curr_thread = 0;
static rb_thread_t main_thread;
static void scope_dup _((struct SCOPE *));
#define POP_SCOPE() \
if (ruby_scope->flags & SCOPE_DONT_RECYCLE) {\
if (_old) scope_dup(_old); \
} \
if (!(ruby_scope->flags & SCOPE_MALLOC)) {\
ruby_scope->local_vars = 0; \
ruby_scope->local_tbl = 0; \
if (!(ruby_scope->flags & SCOPE_DONT_RECYCLE) && \
ruby_scope != top_scope) { \
rb_gc_force_recycle((VALUE)ruby_scope);\
} \
} \
ruby_scope->flags |= SCOPE_NOSTACK; \
ruby_scope = _old; \
scope_vmode = _vmode; \
}
static VALUE rb_eval _((VALUE,NODE*));
static VALUE eval _((VALUE,VALUE,VALUE,char*,int));
static NODE *compile _((VALUE, char*, int));
static VALUE rb_yield_0 _((VALUE, VALUE, VALUE, int));
static VALUE rb_call _((VALUE,VALUE,ID,int,VALUE*,int));
static VALUE module_setup _((VALUE,NODE*));
static VALUE massign _((VALUE,NODE*,VALUE,int));
static void assign _((VALUE,NODE*,VALUE,int));
static VALUE trace_func = 0;
static int tracing = 0;
static void call_trace_func _((char*,char*,int,VALUE,ID,VALUE));
static void
error_pos()
{
if (ruby_sourcefile) {
if (ruby_frame->last_func) {
fprintf(stderr, "%s:%d:in `%s'", ruby_sourcefile, ruby_sourceline,
rb_id2name(ruby_frame->last_func));
}
else if (ruby_sourceline == 0) {
fprintf(stderr, "%s", ruby_sourcefile);
}
else {
fprintf(stderr, "%s:%d", ruby_sourcefile, ruby_sourceline);
}
}
}
static VALUE
get_backtrace(info)
VALUE info;
{
if (NIL_P(info)) return Qnil;
return rb_funcall(info, rb_intern("backtrace"), 0);
}
static void
set_backtrace(info, bt)
VALUE info, bt;
{
rb_funcall(info, rb_intern("set_backtrace"), 1, bt);
}
static void
error_print()
{
VALUE errat = Qnil; /* OK */
volatile VALUE eclass;
char *einfo;
int elen;
if (NIL_P(ruby_errinfo)) return;
PUSH_TAG(PROT_NONE);
if (EXEC_TAG() == 0) {
errat = get_backtrace(ruby_errinfo);
}
else {
errat = Qnil;
}
POP_TAG();
if (NIL_P(errat)){
if (ruby_sourcefile)
fprintf(stderr, "%s:%d", ruby_sourcefile, ruby_sourceline);
else
fprintf(stderr, "%d", ruby_sourceline);
}
else if (RARRAY(errat)->len == 0) {
error_pos();
}
else {
VALUE mesg = RARRAY(errat)->ptr[0];
if (NIL_P(mesg)) error_pos();
else {
fwrite(RSTRING(mesg)->ptr, 1, RSTRING(mesg)->len, stderr);
}
}
eclass = CLASS_OF(ruby_errinfo);
PUSH_TAG(PROT_NONE);
if (EXEC_TAG() == 0) {
VALUE e = rb_obj_as_string(ruby_errinfo);
einfo = RSTRING(e)->ptr;
elen = RSTRING(e)->len;
}
else {
einfo = "";
elen = 0;
}
POP_TAG();
if (eclass == rb_eRuntimeError && elen == 0) {
fprintf(stderr, ": unhandled exception\n");
}
else {
VALUE epath;
epath = rb_class_path(eclass);
if (elen == 0) {
fprintf(stderr, ": ");
fwrite(RSTRING(epath)->ptr, 1, RSTRING(epath)->len, stderr);
putc('\n', stderr);
}
else {
char *tail = 0;
int len = elen;
if (RSTRING(epath)->ptr[0] == '#') epath = 0;
if (tail = strchr(einfo, '\n')) {
len = tail - einfo;
tail++; /* skip newline */
}
fprintf(stderr, ": ");
fwrite(einfo, 1, len, stderr);
if (epath) {
fprintf(stderr, " (");
fwrite(RSTRING(epath)->ptr, 1, RSTRING(epath)->len, stderr);
fprintf(stderr, ")\n");
}
if (tail) {
fwrite(tail, 1, elen-len-1, stderr);
putc('\n', stderr);
}
}
}
if (!NIL_P(errat)) {
int i;
struct RArray *ep = RARRAY(errat);
#define TRACE_MAX (TRACE_HEAD+TRACE_TAIL+5)
#define TRACE_HEAD 8
#define TRACE_TAIL 5
ep = RARRAY(errat);
for (i=1; i<ep->len; i++) {
if (TYPE(ep->ptr[i]) == T_STRING) {
fprintf(stderr, "\tfrom %s\n", RSTRING(ep->ptr[i])->ptr);
}
if (i == TRACE_HEAD && ep->len > TRACE_MAX) {
fprintf(stderr, "\t ... %ld levels...\n",
ep->len - TRACE_HEAD - TRACE_TAIL);
i = ep->len - TRACE_TAIL;
}
}
}
}
#if !defined(NT) && !defined(__MACOS__)
extern char **environ;
#endif
char **rb_origenviron;
void rb_call_inits _((void));
void Init_stack _((void*));
void Init_heap _((void));
void Init_ext _((void));
void
ruby_init()
{
static int initialized = 0;
static struct FRAME frame;
static struct iter iter;
int state;
if (initialized)
return;
initialized = 1;
ruby_frame = top_frame = &frame;
ruby_iter = &iter;
#ifdef __MACOS__
rb_origenviron = 0;
#else
rb_origenviron = environ;
#endif
Init_stack(0);
Init_heap();
PUSH_SCOPE();
ruby_scope->local_vars = 0;
ruby_scope->local_tbl = 0;
top_scope = ruby_scope;
/* default visibility is private at toplevel */
SCOPE_SET(SCOPE_PRIVATE);
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
rb_call_inits();
ruby_class = rb_cObject;
ruby_frame->self = ruby_top_self;
top_cref = rb_node_newnode(NODE_CREF,rb_cObject,0,0);
ruby_cref = top_cref;
ruby_frame->cbase = (VALUE)ruby_cref;
rb_define_global_const("TOPLEVEL_BINDING", rb_f_binding(ruby_top_self));
#ifdef __MACOS__
_macruby_init();
#endif
ruby_prog_init();
}
POP_TAG();
if (state) error_print();
POP_SCOPE();
ruby_scope = top_scope;
}
static VALUE
eval_node(self, node)
VALUE self;
NODE *node;
{
NODE *beg_tree = ruby_eval_tree_begin;
ruby_eval_tree_begin = 0;
if (beg_tree) {
rb_eval(self, beg_tree);
}
if (!node) return Qnil;
return rb_eval(self, node);
}
int ruby_in_eval;
static void rb_thread_cleanup _((void));
static void rb_thread_wait_other_threads _((void));
static int exit_status;
static int
error_handle(ex)
int ex;
{
switch (ex & TAG_MASK) {
case 0:
ex = 0;
break;
case TAG_RETURN:
error_pos();
fprintf(stderr, ": unexpected return\n");
ex = 1;
break;
case TAG_NEXT:
error_pos();
fprintf(stderr, ": unexpected next\n");
ex = 1;
break;
case TAG_BREAK:
error_pos();
fprintf(stderr, ": unexpected break\n");
ex = 1;
break;
case TAG_REDO:
error_pos();
fprintf(stderr, ": unexpected redo\n");
ex = 1;
break;
case TAG_RETRY:
error_pos();
fprintf(stderr, ": retry outside of rescue clause\n");
ex = 1;
break;
case TAG_RAISE:
case TAG_FATAL:
if (rb_obj_is_kind_of(ruby_errinfo, rb_eSystemExit)) {
ex = exit_status;
}
else {
error_print();
ex = 1;
}
break;
default:
rb_bug("Unknown longjmp status %d", ex);
break;
}
return ex;
}
void
ruby_options(argc, argv)
int argc;
char **argv;
{
int state;
PUSH_TAG(PROT_NONE)
if ((state = EXEC_TAG()) == 0) {
ruby_process_options(argc, argv);
}
POP_TAG();
if (state) {
trace_func = 0;
tracing = 0;
exit(error_handle(state));
}
}
void rb_exec_end_proc _((void));
void
ruby_finalize()
{
int state;
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
rb_trap_exit();
rb_exec_end_proc();
rb_gc_call_finalizer_at_exit();
}
POP_TAG();
}
void
ruby_stop(ex)
int ex;
{
int state;
PUSH_TAG(PROT_NONE);
PUSH_ITER(ITER_NOT);
if ((state = EXEC_TAG()) == 0) {
rb_thread_cleanup();
rb_thread_wait_other_threads();
}
else if (ex == 0) {
ex = state;
}
POP_ITER();
POP_TAG();
trace_func = 0;
tracing = 0;
ex = error_handle(ex);
ruby_finalize();
exit(ex);
}
void
ruby_run()
{
int state;
static int ex;
volatile NODE *tmp;
if (ruby_nerrs > 0) exit(ruby_nerrs);
Init_stack((void*)&tmp);
PUSH_TAG(PROT_NONE);
PUSH_ITER(ITER_NOT);
if ((state = EXEC_TAG()) == 0) {
eval_node(ruby_top_self, ruby_eval_tree);
}
POP_ITER();
POP_TAG();
if (state && !ex) ex = state;
ruby_stop(ex);
}
static void
compile_error(at)
const char *at;
{
VALUE str;
ruby_nerrs = 0;
str = rb_str_buf_new2("compile error");
if (at) {
rb_str_buf_cat2(str, " in ");
rb_str_buf_cat2(str, at);
}
rb_str_buf_cat(str, "\n", 1);
if (!NIL_P(ruby_errinfo)) {
rb_str_append(str, ruby_errinfo);
}
rb_exc_raise(rb_exc_new3(rb_eSyntaxError, str));
}
VALUE
rb_eval_string(str)
const char *str;
{
VALUE v;
char *oldsrc = ruby_sourcefile;
ruby_sourcefile = rb_source_filename("(eval)");
v = eval(ruby_top_self, rb_str_new2(str), Qnil, 0, 0);
ruby_sourcefile = oldsrc;
return v;
}
VALUE
rb_eval_string_protect(str, state)
const char *str;
int *state;
{
VALUE result; /* OK */
int status;
PUSH_TAG(PROT_NONE);
if ((status = EXEC_TAG()) == 0) {
result = rb_eval_string(str);
}
POP_TAG();
if (state) {
*state = status;
}
if (status != 0) {
return Qnil;
}
return result;
}
VALUE
rb_eval_string_wrap(str, state)
const char *str;
int *state;
{
int status;
VALUE self = ruby_top_self;
VALUE wrapper = ruby_wrapper;
VALUE val;
PUSH_CLASS();
ruby_class = ruby_wrapper = rb_module_new();
ruby_top_self = rb_obj_clone(ruby_top_self);
rb_extend_object(ruby_top_self, ruby_wrapper);
PUSH_FRAME();
ruby_frame->last_func = 0;
ruby_frame->last_class = 0;
ruby_frame->self = self;
ruby_frame->cbase = (VALUE)rb_node_newnode(NODE_CREF,ruby_wrapper,0,0);
PUSH_SCOPE();
val = rb_eval_string_protect(str, &status);
ruby_top_self = self;
POP_SCOPE();
POP_FRAME();
POP_CLASS();
ruby_wrapper = wrapper;
if (state) {
*state = status;
}
else if (status) {
JUMP_TAG(status);
}
return val;
}
static void
localjump_error(mesg, status)
const char *mesg;
{
VALUE exc = rb_exc_new2(rb_eLocalJumpError, mesg);
rb_iv_set(exc, "@status", status);
rb_exc_raise(exc);
}
static VALUE
localjump_exitstatus(exc)
VALUE exc;
{
return rb_iv_get(exc, "@status");
}
static void
jump_tag_but_local_jump(state)
int state;
{
switch (state) {
case 0:
break;
case TAG_RETURN:
localjump_error("unexpected return", Qnil);
break;
case TAG_NEXT:
localjump_error("unexpected next", Qnil);
break;
case TAG_BREAK:
localjump_error("unexpected break", Qnil);
break;
case TAG_REDO:
localjump_error("unexpected redo", Qnil);
break;
case TAG_RETRY:
localjump_error("retry outside of rescue clause", Qnil);
break;
default:
JUMP_TAG(state);
break;
}
}
VALUE
rb_eval_cmd(cmd, arg, tcheck)
VALUE cmd, arg;
int tcheck;
{
int state;
VALUE val; /* OK */
struct SCOPE *saved_scope;
volatile int safe = ruby_safe_level;
if (TYPE(cmd) != T_STRING) {
PUSH_ITER(ITER_NOT);
val = rb_funcall2(cmd, rb_intern("call"), RARRAY(arg)->len, RARRAY(arg)->ptr);
POP_ITER();
return val;
}
saved_scope = ruby_scope;
ruby_scope = top_scope;
PUSH_FRAME();
ruby_frame->last_func = 0;
ruby_frame->last_class = 0;
ruby_frame->self = ruby_top_self;
ruby_frame->cbase = (VALUE)rb_node_newnode(NODE_CREF,0,0,0);
RNODE(ruby_frame->cbase)->nd_clss = ruby_wrapper ? ruby_wrapper : rb_cObject;
if (tcheck && OBJ_TAINTED(cmd)) {
ruby_safe_level = 4;
}
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
val = eval(ruby_top_self, cmd, Qnil, 0, 0);
}
if (ruby_scope->flags & SCOPE_DONT_RECYCLE)
scope_dup(saved_scope);
ruby_scope = saved_scope;
ruby_safe_level = safe;
POP_TAG();
POP_FRAME();
jump_tag_but_local_jump(state);
return val;
}
static VALUE
rb_trap_eval(cmd, sig)
VALUE cmd;
int sig;
{
int state;
VALUE val; /* OK */
PUSH_TAG(PROT_NONE);
PUSH_ITER(ITER_NOT);
if ((state = EXEC_TAG()) == 0) {
val = rb_eval_cmd(cmd, rb_ary_new3(1, INT2FIX(sig)), 0);
}
POP_ITER();
POP_TAG();
if (state) {
rb_trap_immediate = 0;
JUMP_TAG(state);
}
return val;
}
static VALUE
superclass(self, node)
VALUE self;
NODE *node;
{
VALUE val; /* OK */
int state;
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
val = rb_eval(self, node);
}
POP_TAG();
if (state) {
superclass_error:
switch (nd_type(node)) {
case NODE_COLON2:
rb_raise(rb_eTypeError, "undefined superclass `%s'",
rb_id2name(node->nd_mid));
case NODE_CONST:
rb_raise(rb_eTypeError, "undefined superclass `%s'",
rb_id2name(node->nd_vid));
default:
break;
}
JUMP_TAG(state);
}
if (TYPE(val) != T_CLASS) goto superclass_error;
if (FL_TEST(val, FL_SINGLETON)) {
rb_raise(rb_eTypeError, "can't make subclass of virtual class");
}
return val;
}
#define ruby_cbase (RNODE(ruby_frame->cbase)->nd_clss)
static VALUE
ev_const_defined(cref, id, self)
NODE *cref;
ID id;
VALUE self;
{
NODE *cbase = cref;
while (cbase && cbase->nd_next) {
struct RClass *klass = RCLASS(cbase->nd_clss);
if (NIL_P(klass)) return rb_const_defined(CLASS_OF(self), id);
if (klass->iv_tbl && st_lookup(klass->iv_tbl, id, 0)) {
return Qtrue;
}
cbase = cbase->nd_next;
}
return rb_const_defined(cref->nd_clss, id);
}
static VALUE
ev_const_get(cref, id, self)
NODE *cref;
ID id;
VALUE self;
{
NODE *cbase = cref;
VALUE result;
while (cbase && cbase->nd_next) {
VALUE klass = cbase->nd_clss;
if (NIL_P(klass)) return rb_const_get(CLASS_OF(self), id);
if (RCLASS(klass)->iv_tbl && st_lookup(RCLASS(klass)->iv_tbl, id, &result)) {
return result;
}
cbase = cbase->nd_next;
}
return rb_const_get(cref->nd_clss, id);
}
static VALUE
cvar_cbase()
{
NODE *cref = RNODE(ruby_frame->cbase);
while (cref && cref->nd_next && FL_TEST(cref->nd_clss, FL_SINGLETON)) {
cref = cref->nd_next;
if (!cref->nd_next) {
rb_warn("class variable access from toplevel singleton method");
}
}
return cref->nd_clss;
}
static VALUE
rb_mod_nesting()
{
NODE *cbase = RNODE(ruby_frame->cbase);
VALUE ary = rb_ary_new();
while (cbase && cbase->nd_next) {
if (!NIL_P(cbase->nd_clss)) rb_ary_push(ary, cbase->nd_clss);
cbase = cbase->nd_next;
}
return ary;
}
static VALUE
rb_mod_s_constants()
{
NODE *cbase = RNODE(ruby_frame->cbase);
void *data = 0;
while (cbase) {
if (!NIL_P(cbase->nd_clss)) {
data = rb_mod_const_at(cbase->nd_clss, data);
}
cbase = cbase->nd_next;
}
if (!NIL_P(ruby_cbase)) {
data = rb_mod_const_of(ruby_cbase, data);
}
return rb_const_list(data);
}
void
rb_frozen_class_p(klass)
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);
}
}
void
rb_undef(klass, id)
VALUE klass;
ID id;
{
VALUE origin;
NODE *body;
if (ruby_class == rb_cObject) {
rb_secure(4);
}
if (rb_safe_level() >= 4 && !OBJ_TAINTED(klass)) {
rb_raise(rb_eSecurityError, "Insecure: can't undef");
if (id == __id__ || id == __send__ || id == init) {
rb_name_error(id, "undefining `%s' prohibited", rb_id2name(id));
}
}
rb_frozen_class_p(klass);
if (id == __id__ || id == __send__ || id == init) {
rb_warn("undefining `%s' may cause serious problem", rb_id2name(id));
}
body = search_method(ruby_class, id, &origin);
if (!body || !body->nd_body) {
char *s0 = " class";
VALUE c = klass;
if (FL_TEST(c, FL_SINGLETON)) {
VALUE obj = rb_iv_get(klass, "__attached__");
switch (TYPE(obj)) {
case T_MODULE:
case T_CLASS:
c = obj;
s0 = "";
}
}
else if (TYPE(c) == T_MODULE) {
s0 = " module";
}
rb_name_error(id, "undefined method `%s' for%s `%s'",
rb_id2name(id),s0,rb_class2name(c));
}
rb_add_method(klass, id, 0, NOEX_PUBLIC);
if (FL_TEST(klass, FL_SINGLETON)) {
rb_funcall(rb_iv_get(klass, "__attached__"),
singleton_undefined, 1, ID2SYM(id));
}
else {
rb_funcall(klass, undefined, 1, ID2SYM(id));
}
}
static VALUE
rb_mod_undef_method(mod, name)
VALUE mod, name;
{
rb_undef(mod, rb_to_id(name));
return mod;
}
void
rb_alias(klass, name, def)
VALUE klass;
ID name, def;
{
VALUE origin;
NODE *orig, *body;
rb_frozen_class_p(klass);
if (name == def) return;
if (klass == rb_cObject) {
rb_secure(4);
}
orig = search_method(klass, def, &origin);
if (!orig || !orig->nd_body) {
if (TYPE(klass) == T_MODULE) {
orig = search_method(rb_cObject, def, &origin);
}
}
if (!orig || !orig->nd_body) {
print_undef(klass, def);
}
body = orig->nd_body;
orig->nd_cnt++;
if (nd_type(body) == NODE_FBODY) { /* was alias */
def = body->nd_mid;
origin = body->nd_orig;
body = body->nd_head;
}
rb_clear_cache_by_id(name);
st_insert(RCLASS(klass)->m_tbl, name,
NEW_METHOD(NEW_FBODY(body, def, origin), orig->nd_noex));
if (FL_TEST(klass, FL_SINGLETON)) {
rb_funcall(rb_iv_get(klass, "__attached__"),
singleton_added, 1, ID2SYM(name));
}
else {
rb_funcall(klass, added, 1, ID2SYM(name));
}
}
static VALUE
rb_mod_alias_method(mod, newname, oldname)
VALUE mod, newname, oldname;
{
rb_alias(mod, rb_to_id(newname), rb_to_id(oldname));
return mod;
}
static NODE*
copy_node_scope(node, rval)
NODE *node;
VALUE rval;
{
NODE *copy = rb_node_newnode(NODE_SCOPE,0,rval,node->nd_next);
if (node->nd_tbl) {
copy->nd_tbl = ALLOC_N(ID, node->nd_tbl[0]+1);
MEMCPY(copy->nd_tbl, node->nd_tbl, ID, node->nd_tbl[0]+1);
}
else {
copy->nd_tbl = 0;
}
return copy;
}
#ifdef C_ALLOCA
# define TMP_PROTECT NODE * volatile tmp__protect_tmp=0
# define TMP_ALLOC(n) \
(tmp__protect_tmp = rb_node_newnode(NODE_ALLOCA, \
ALLOC_N(VALUE,n),tmp__protect_tmp,n), \
(void*)tmp__protect_tmp->nd_head)
#else
# define TMP_PROTECT typedef int foobazzz
# define TMP_ALLOC(n) ALLOCA_N(VALUE,n)
#endif
#define SETUP_ARGS(anode) {\
NODE *n = anode;\
if (!n) {\
argc = 0;\
argv = 0;\
}\
else if (nd_type(n) == NODE_ARRAY) {\
argc=n->nd_alen;\
if (argc > 0) {\
char *file = ruby_sourcefile;\
int line = ruby_sourceline;\
int i;\
n = anode;\
argv = TMP_ALLOC(argc);\
for (i=0;i<argc;i++) {\
argv[i] = rb_eval(self,n->nd_head);\
n=n->nd_next;\
}\
ruby_sourcefile = file;\
ruby_sourceline = line;\
}\
else {\
argc = 0;\
argv = 0;\
}\
}\
else {\
VALUE args = rb_eval(self,n);\
char *file = ruby_sourcefile;\
int line = ruby_sourceline;\
if (TYPE(args) != T_ARRAY)\
args = rb_ary_to_ary(args);\
argc = RARRAY(args)->len;\
argv = ALLOCA_N(VALUE, argc);\
MEMCPY(argv, RARRAY(args)->ptr, VALUE, argc);\
ruby_sourcefile = file;\
ruby_sourceline = line;\
}\
}
#define BEGIN_CALLARGS {\
struct BLOCK *tmp_block = ruby_block;\
if (ruby_iter->iter == ITER_PRE) {\
ruby_block = ruby_block->prev;\
}\
PUSH_ITER(ITER_NOT);
#define END_CALLARGS \
ruby_block = tmp_block;\
POP_ITER();\
}
#define MATCH_DATA *rb_svar(node->nd_cnt)
static char* is_defined _((VALUE, NODE*, char*));
static char*
arg_defined(self, node, buf, type)
VALUE self;
NODE *node;
char *buf;
char *type;
{
int argc;
int i;
if (!node) return type; /* no args */
if (nd_type(node) == NODE_ARRAY) {
argc=node->nd_alen;
if (argc > 0) {
for (i=0;i<argc;i++) {
if (!is_defined(self, node->nd_head, buf))
return 0;
node = node->nd_next;
}
}
}
else if (!is_defined(self, node, buf)) {
return 0;
}
return type;
}
static char*
is_defined(self, node, buf)
VALUE self;
NODE *node; /* OK */
char *buf;
{
VALUE val; /* OK */
int state;
if (!node) return "expression";
switch (nd_type(node)) {
case NODE_SUPER:
case NODE_ZSUPER:
if (ruby_frame->last_func == 0) return 0;
else if (ruby_frame->last_class == 0) return 0;
else if (rb_method_boundp(RCLASS(ruby_frame->last_class)->super,
ruby_frame->last_func, 0)) {
if (nd_type(node) == NODE_SUPER) {
return arg_defined(self, node->nd_args, buf, "super");
}
return "super";
}
break;
case NODE_VCALL:
case NODE_FCALL:
val = self;
goto check_bound;
case NODE_CALL:
if (!is_defined(self, node->nd_recv, buf)) return 0;
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
val = rb_eval(self, node->nd_recv);
}
POP_TAG();
if (state) {
ruby_errinfo = Qnil;
return 0;
}
check_bound:
{
int call = nd_type(node)== NODE_CALL;
val = CLASS_OF(val);
if (call) {
int noex;
ID id = node->nd_mid;
if (!rb_get_method_body(&val, &id, &noex))
break;
if ((noex & NOEX_PRIVATE))
break;
if ((noex & NOEX_PROTECTED) &&
!rb_obj_is_kind_of(self, rb_class_real(val)))
break;
}
else if (!rb_method_boundp(val, node->nd_mid, call))
break;
return arg_defined(self, node->nd_args, buf, "method");
}
break;
case NODE_MATCH2:
case NODE_MATCH3:
return "method";
case NODE_YIELD:
if (rb_block_given_p()) {
return "yield";
}
break;
case NODE_SELF:
return "self";
case NODE_NIL:
return "nil";
case NODE_TRUE:
return "true";
case NODE_FALSE:
return "false";
case NODE_ATTRSET:
case NODE_OP_ASGN1:
case NODE_OP_ASGN2:
case NODE_MASGN:
case NODE_LASGN:
case NODE_DASGN:
case NODE_DASGN_CURR:
case NODE_GASGN:
case NODE_CDECL:
case NODE_CVDECL:
case NODE_CVASGN:
return "assignment";
case NODE_LVAR:
return "local-variable";
case NODE_DVAR:
return "local-variable(in-block)";
case NODE_GVAR:
if (rb_gvar_defined(node->nd_entry)) {
return "global-variable";
}
break;
case NODE_IVAR:
if (rb_ivar_defined(self, node->nd_vid)) {
return "instance-variable";
}
break;
case NODE_CONST:
if (ev_const_defined(RNODE(ruby_frame->cbase), node->nd_vid, self)) {
return "constant";
}
break;
case NODE_CVAR:
if (rb_cvar_defined(cvar_cbase(), node->nd_vid)) {
return "class variable";
}
break;
case NODE_COLON2:
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
val = rb_eval(self, node->nd_head);
}
POP_TAG();
if (state) {
ruby_errinfo = Qnil;
return 0;
}
else {
switch (TYPE(val)) {
case T_CLASS:
case T_MODULE:
if (rb_const_defined_at(val, node->nd_mid))
return "constant";
break;
default:
if (rb_method_boundp(CLASS_OF(val), node->nd_mid, 1)) {
return "method";
}
}
}
break;
case NODE_NTH_REF:
if (RTEST(rb_reg_nth_defined(node->nd_nth, MATCH_DATA))) {
sprintf(buf, "$%d", node->nd_nth);
return buf;
}
break;
case NODE_BACK_REF:
if (RTEST(rb_reg_nth_defined(0, MATCH_DATA))) {
sprintf(buf, "$%c", node->nd_nth);
return buf;
}
break;
default:
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
rb_eval(self, node);
}
POP_TAG();
if (!state) {
return "expression";
}
ruby_errinfo = Qnil;
break;
}
return 0;
}
static int handle_rescue _((VALUE,NODE*));
static void blk_free();
static VALUE
rb_obj_is_block(block)
VALUE block;
{
if (TYPE(block) == T_DATA && RDATA(block)->dfree == (RUBY_DATA_FUNC)blk_free) {
return Qtrue;
}
return Qfalse;
}
static VALUE
rb_obj_is_proc(proc)
VALUE proc;
{
if (rb_obj_is_block(proc) && rb_obj_is_kind_of(proc, rb_cProc)) {
return Qtrue;
}
return Qfalse;
}
static VALUE
set_trace_func(obj, trace)
VALUE obj, trace;
{
if (NIL_P(trace)) {
trace_func = 0;
return Qnil;
}
if (!rb_obj_is_proc(trace)) {
rb_raise(rb_eTypeError, "trace_func needs to be Proc");
}
return trace_func = trace;
}
static void
call_trace_func(event, file, line, self, id, klass)
char *event;
char *file;
int line;
VALUE self;
ID id;
VALUE klass; /* OK */
{
int state;
struct FRAME *prev;
char *file_save = ruby_sourcefile;
int line_save = ruby_sourceline;
VALUE srcfile;
if (!trace_func) return;
if (tracing) return;
tracing = 1;
prev = ruby_frame;
PUSH_FRAME();
*ruby_frame = *prev;
ruby_frame->prev = prev;
ruby_frame->iter = 0; /* blocks not available anyway */
if (file) {
ruby_frame->line = ruby_sourceline = line;
ruby_frame->file = ruby_sourcefile = file;
}
if (klass) {
if (TYPE(klass) == T_ICLASS) {
klass = RBASIC(klass)->klass;
}
else if (FL_TEST(klass, FL_SINGLETON)) {
klass = self;
}
}
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
srcfile = rb_str_new2(ruby_sourcefile?ruby_sourcefile:"(ruby)");
proc_invoke(trace_func, rb_ary_new3(6, rb_str_new2(event),
srcfile,
INT2FIX(ruby_sourceline),
id?ID2SYM(id):Qnil,
self?rb_f_binding(self):Qnil,
klass),
Qtrue, Qundef);
}
POP_TMPTAG(); /* do not propagate retval */
POP_FRAME();
tracing = 0;
ruby_sourceline = line_save;
ruby_sourcefile = file_save;
if (state) JUMP_TAG(state);
}
static VALUE
svalue_to_avalue(v)
VALUE v;
{
if (NIL_P(v)) return rb_ary_new2(0);
if (TYPE(v) == T_ARRAY) {
if (RARRAY(v)->len > 1) return v;
return rb_ary_new3(1, v);
}
else {
v = rb_ary_to_ary(v);
}
return v;
}
static VALUE
avalue_to_svalue(v)
VALUE v;
{
if (TYPE(v) != T_ARRAY) {
v = rb_ary_to_ary(v);
}
if (RARRAY(v)->len == 0) {
return Qnil;
}
if (RARRAY(v)->len == 1) {
return RARRAY(v)->ptr[0];
}
return v;
}
static VALUE
svalue_to_mvalue(v)
VALUE v;
{
if (NIL_P(v)) return rb_ary_new2(0);
if (TYPE(v) == T_ARRAY) {
return v;
}
else {
v = rb_ary_to_ary(v);
}
return v;
}
static VALUE
mvalue_to_svalue(v)
VALUE v;
{
if (TYPE(v) != T_ARRAY) {
v = rb_ary_to_ary(v);
}
if (RARRAY(v)->len == 0) {
return Qnil;
}
if (RARRAY(v)->len == 1 && TYPE(RARRAY(v)->ptr[0]) != T_ARRAY) {
return RARRAY(v)->ptr[0];
}
return v;
}
static void return_check _((void));
#define return_value(v) prot_tag->retval = (v)
static VALUE
rb_eval(self, n)
VALUE self;
NODE *n;
{
NODE * volatile node = n;
int state;
volatile VALUE result = Qnil;
#define RETURN(v) { result = (v); goto finish; }
again:
if (!node) RETURN(Qnil);
switch (nd_type(node)) {
case NODE_BLOCK:
while (node->nd_next) {
rb_eval(self, node->nd_head);
node = node->nd_next;
}
node = node->nd_head;
goto again;
case NODE_POSTEXE:
rb_f_END();
nd_set_type(node, NODE_NIL); /* exec just once */
result = Qnil;
break;
/* begin .. end without clauses */
case NODE_BEGIN:
node = node->nd_body;
goto again;
/* nodes for speed-up(default match) */
case NODE_MATCH:
result = rb_reg_match2(node->nd_lit);
break;
/* nodes for speed-up(literal match) */
case NODE_MATCH2:
result = rb_reg_match(rb_eval(self,node->nd_recv),
rb_eval(self,node->nd_value));
break;
/* nodes for speed-up(literal match) */
case NODE_MATCH3:
{
VALUE r = rb_eval(self,node->nd_recv);
VALUE l = rb_eval(self,node->nd_value);
if (TYPE(l) == T_STRING) {
result = rb_reg_match(r, l);
}
else {
result = rb_funcall(l, match, 1, r);
}
}
break;
/* node for speed-up(top-level loop for -n/-p) */
case NODE_OPT_N:
PUSH_TAG(PROT_NONE);
switch (state = EXEC_TAG()) {
case 0:
opt_n_next:
while (!NIL_P(rb_gets())) {
opt_n_redo:
rb_eval(self, node->nd_body);
}
break;
case TAG_REDO:
state = 0;
goto opt_n_redo;
case TAG_NEXT:
state = 0;
goto opt_n_next;
case TAG_BREAK:
state = 0;
default:
break;
}
POP_TAG();
if (state) JUMP_TAG(state);
RETURN(Qnil);
case NODE_SELF:
RETURN(self);
case NODE_NIL:
RETURN(Qnil);
case NODE_TRUE:
RETURN(Qtrue);
case NODE_FALSE:
RETURN(Qfalse);
case NODE_IF:
ruby_sourceline = nd_line(node);
if (trace_func) {
call_trace_func("line", node->nd_file, ruby_sourceline, self,
ruby_frame->last_func,
ruby_frame->last_class);
}
if (RTEST(rb_eval(self, node->nd_cond))) {
node = node->nd_body;
}
else {
node = node->nd_else;
}
goto again;
case NODE_WHEN:
while (node) {
NODE *tag;
if (nd_type(node) != NODE_WHEN) goto again;
tag = node->nd_head;
while (tag) {
if (trace_func) {
call_trace_func("line", tag->nd_file, nd_line(tag), self,
ruby_frame->last_func,
ruby_frame->last_class);
}
ruby_sourcefile = tag->nd_file;
ruby_sourceline = nd_line(tag);
if (nd_type(tag->nd_head) == NODE_WHEN) {
VALUE v = rb_eval(self, tag->nd_head->nd_head);
int i;
if (TYPE(v) != T_ARRAY) v = rb_ary_to_ary(v);
for (i=0; i<RARRAY(v)->len; i++) {
if (RTEST(RARRAY(v)->ptr[i])) {
node = node->nd_body;
goto again;
}
}
tag = tag->nd_next;
continue;
}
if (RTEST(rb_eval(self, tag->nd_head))) {
node = node->nd_body;
goto again;
}
tag = tag->nd_next;
}
node = node->nd_next;
}
RETURN(Qnil);
case NODE_CASE:
{
VALUE val;
val = rb_eval(self, node->nd_head);
node = node->nd_body;
while (node) {
NODE *tag;
if (nd_type(node) != NODE_WHEN) {
goto again;
}
tag = node->nd_head;
while (tag) {
if (trace_func) {
call_trace_func("line", tag->nd_file, nd_line(tag), self,
ruby_frame->last_func,
ruby_frame->last_class);
}
ruby_sourcefile = tag->nd_file;
ruby_sourceline = nd_line(tag);
if (nd_type(tag->nd_head) == NODE_WHEN) {
VALUE v = rb_eval(self, tag->nd_head->nd_head);
int i;
if (TYPE(v) != T_ARRAY) v = rb_ary_to_ary(v);
for (i=0; i<RARRAY(v)->len; i++) {
if (RTEST(rb_funcall2(RARRAY(v)->ptr[i], eqq, 1, &val))){
node = node->nd_body;
goto again;
}
}
tag = tag->nd_next;
continue;
}
if (RTEST(rb_funcall2(rb_eval(self, tag->nd_head), eqq, 1, &val))) {
node = node->nd_body;
goto again;
}
tag = tag->nd_next;
}
node = node->nd_next;
}
}
RETURN(Qnil);
case NODE_WHILE:
PUSH_TAG(PROT_NONE);
result = Qnil;
switch (state = EXEC_TAG()) {
case 0:
ruby_sourceline = nd_line(node);
if (node->nd_state && !RTEST(rb_eval(self, node->nd_cond)))
goto while_out;
do {
while_redo:
rb_eval(self, node->nd_body);
while_next:
;
} while (RTEST(rb_eval(self, node->nd_cond)));
break;
case TAG_REDO:
state = 0;
goto while_redo;
case TAG_NEXT:
state = 0;
goto while_next;
case TAG_BREAK:
state = 0;
result = prot_tag->retval;
default:
break;
}
while_out:
POP_TAG();
if (state) JUMP_TAG(state);
RETURN(result);
case NODE_UNTIL:
PUSH_TAG(PROT_NONE);
result = Qnil;
switch (state = EXEC_TAG()) {
case 0:
ruby_sourceline = nd_line(node);
if (node->nd_state && RTEST(rb_eval(self, node->nd_cond)))
goto until_out;
do {
until_redo:
rb_eval(self, node->nd_body);
until_next:
;
} while (!RTEST(rb_eval(self, node->nd_cond)));
break;
case TAG_REDO:
state = 0;
goto until_redo;
case TAG_NEXT:
state = 0;
goto until_next;
case TAG_BREAK:
state = 0;
result = prot_tag->retval;
default:
break;
}
until_out:
POP_TAG();
if (state) JUMP_TAG(state);
RETURN(result);
case NODE_BLOCK_PASS:
result = block_pass(self, node);
break;
case NODE_ITER:
case NODE_FOR:
{
iter_retry:
PUSH_TAG(PROT_FUNC);
PUSH_BLOCK(node->nd_var, node->nd_body);
state = EXEC_TAG();
if (state == 0) {
PUSH_ITER(ITER_PRE);
if (nd_type(node) == NODE_ITER) {
result = rb_eval(self, node->nd_iter);
}
else {
VALUE recv;
char *file = ruby_sourcefile;
int line = ruby_sourceline;
_block.flags &= ~BLOCK_D_SCOPE;
BEGIN_CALLARGS;
recv = rb_eval(self, node->nd_iter);
END_CALLARGS;
ruby_sourcefile = file;
ruby_sourceline = line;
result = rb_call(CLASS_OF(recv),recv,each,0,0,0);
}
POP_ITER();
}
else if (_block.tag->dst == state) {
state &= TAG_MASK;
if (state == TAG_RETURN || state == TAG_BREAK) {
result = prot_tag->retval;
}
}
POP_BLOCK();
POP_TAG();
switch (state) {
case 0:
break;
case TAG_RETRY:
goto iter_retry;
case TAG_BREAK:
break;
case TAG_RETURN:
return_value(result);
/* fall through */
default:
JUMP_TAG(state);
}
}
break;
case NODE_BREAK:
if (node->nd_stts) {
return_value(avalue_to_svalue(rb_eval(self, node->nd_stts)));
}
else {
return_value(Qnil);
}
JUMP_TAG(TAG_BREAK);
break;
case NODE_NEXT:
CHECK_INTS;
if (node->nd_stts) {
return_value(avalue_to_svalue(rb_eval(self, node->nd_stts)));
}
else {
return_value(Qnil);
}
JUMP_TAG(TAG_NEXT);
break;
case NODE_REDO:
CHECK_INTS;
JUMP_TAG(TAG_REDO);
break;
case NODE_RETRY:
CHECK_INTS;
JUMP_TAG(TAG_RETRY);
break;
case NODE_RESTARGS:
case NODE_RESTARY:
result = rb_ary_to_ary(rb_eval(self, node->nd_head));
break;
case NODE_REXPAND:
result = avalue_to_svalue(rb_eval(self, node->nd_head));
break;
case NODE_YIELD:
if (node->nd_stts) {
result = avalue_to_svalue(rb_eval(self, node->nd_stts));
}
else {
result = Qnil;
}
result = rb_yield_0(result, 0, 0, 0);
break;
case NODE_RESCUE:
retry_entry:
{
volatile VALUE e_info = ruby_errinfo;
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
result = rb_eval(self, node->nd_head);
}
POP_TAG();
if (state == TAG_RAISE) {
NODE * volatile resq = node->nd_resq;
ruby_sourceline = nd_line(node);
while (resq) {
if (handle_rescue(self, resq)) {
state = 0;
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
result = rb_eval(self, resq->nd_body);
}
POP_TAG();
if (state == TAG_RETRY) {
state = 0;
ruby_errinfo = Qnil;
goto retry_entry;
}
if (state != TAG_RAISE) {
ruby_errinfo = e_info;
}
break;
}
resq = resq->nd_head; /* next rescue */
}
}
else if (node->nd_else) { /* else clause given */
if (!state) { /* no exception raised */
result = rb_eval(self, node->nd_else);
}
}
if (state) JUMP_TAG(state);
}
break;
case NODE_ENSURE:
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
result = rb_eval(self, node->nd_head);
}
POP_TAG();
if (node->nd_ensr) {
VALUE retval = prot_tag->retval; /* save retval */
VALUE errinfo = ruby_errinfo;
rb_eval(self, node->nd_ensr);
return_value(retval);
ruby_errinfo = errinfo;
}
if (state) JUMP_TAG(state);
break;
case NODE_AND:
result = rb_eval(self, node->nd_1st);
if (!RTEST(result)) break;
node = node->nd_2nd;
goto again;
case NODE_OR:
result = rb_eval(self, node->nd_1st);
if (RTEST(result)) break;
node = node->nd_2nd;
goto again;
case NODE_NOT:
if (RTEST(rb_eval(self, node->nd_body))) result = Qfalse;
else result = Qtrue;
break;
case NODE_DOT2:
case NODE_DOT3:
result = rb_range_new(rb_eval(self, node->nd_beg),
rb_eval(self, node->nd_end),
nd_type(node) == NODE_DOT3);
if (node->nd_state) break;
if (nd_type(node->nd_beg) == NODE_LIT && FIXNUM_P(node->nd_beg->nd_lit) &&
nd_type(node->nd_end) == NODE_LIT && FIXNUM_P(node->nd_end->nd_lit))
{
nd_set_type(node, NODE_LIT);
node->nd_lit = result;
}
else {
node->nd_state = 1;
}
break;
case NODE_FLIP2: /* like AWK */
{
VALUE *flip = rb_svar(node->nd_cnt);
if (!flip) rb_bug("unexpected local variable");
if (!RTEST(*flip)) {
if (RTEST(rb_eval(self, node->nd_beg))) {
*flip = RTEST(rb_eval(self, node->nd_end))?Qfalse:Qtrue;
result = Qtrue;
}
else {
result = Qfalse;
}
}
else {
if (RTEST(rb_eval(self, node->nd_end))) {
*flip = Qfalse;
}
result = Qtrue;
}
}
break;
case NODE_FLIP3: /* like SED */
{
VALUE *flip = rb_svar(node->nd_cnt);
if (!flip) rb_bug("unexpected local variable");
if (!RTEST(*flip)) {
result = RTEST(rb_eval(self, node->nd_beg)) ? Qtrue : Qfalse;
*flip = result;
}
else {
if (RTEST(rb_eval(self, node->nd_end))) {
*flip = Qfalse;
}
result = Qtrue;
}
}
break;
case NODE_RETURN:
if (node->nd_stts) {
return_value(avalue_to_svalue(rb_eval(self, node->nd_stts)));
}
else {
return_value(Qnil);
}
return_check();
JUMP_TAG(TAG_RETURN);
break;
case NODE_ARGSCAT:
result = rb_ary_concat(rb_eval(self, node->nd_head),
rb_eval(self, node->nd_body));
break;
case NODE_ARGSPUSH:
result = rb_ary_push(rb_ary_dup(rb_eval(self, node->nd_head)),
rb_eval(self, node->nd_body));
break;
case NODE_CALL:
{
VALUE recv;
int argc; VALUE *argv; /* used in SETUP_ARGS */
TMP_PROTECT;
BEGIN_CALLARGS;
recv = rb_eval(self, node->nd_recv);
SETUP_ARGS(node->nd_args);
END_CALLARGS;
result = rb_call(CLASS_OF(recv),recv,node->nd_mid,argc,argv,0);
}
break;
case NODE_FCALL:
{
int argc; VALUE *argv; /* used in SETUP_ARGS */
TMP_PROTECT;
BEGIN_CALLARGS;
SETUP_ARGS(node->nd_args);
END_CALLARGS;
result = rb_call(CLASS_OF(self),self,node->nd_mid,argc,argv,1);
}
break;
case NODE_VCALL:
result = rb_call(CLASS_OF(self),self,node->nd_mid,0,0,2);
break;
case NODE_SUPER:
case NODE_ZSUPER:
{
int argc; VALUE *argv; /* used in SETUP_ARGS */
TMP_PROTECT;
if (ruby_frame->last_class == 0) {
rb_name_error(ruby_frame->last_func,
"superclass method `%s' disabled",
rb_id2name(ruby_frame->last_func));
}
if (nd_type(node) == NODE_ZSUPER) {
argc = ruby_frame->argc;
argv = ruby_frame->argv;
}
else {
BEGIN_CALLARGS;
SETUP_ARGS(node->nd_args);
END_CALLARGS;
}
PUSH_ITER(ruby_iter->iter?ITER_PRE:ITER_NOT);
result = rb_call(RCLASS(ruby_frame->last_class)->super,
ruby_frame->self, ruby_frame->last_func,
argc, argv, 3);
POP_ITER();
}
break;
case NODE_SCOPE:
{
struct FRAME frame;
NODE *saved_cref = 0;
frame = *ruby_frame;
frame.tmp = ruby_frame;
ruby_frame = &frame;
PUSH_SCOPE();
PUSH_TAG(PROT_NONE);
if (node->nd_rval) {
saved_cref = ruby_cref;
ruby_cref = (NODE*)node->nd_rval;
ruby_frame->cbase = node->nd_rval;
}
if (node->nd_tbl) {
VALUE *vars = ALLOCA_N(VALUE, node->nd_tbl[0]+1);
*vars++ = (VALUE)node;
ruby_scope->local_vars = vars;
rb_mem_clear(ruby_scope->local_vars, node->nd_tbl[0]);
ruby_scope->local_tbl = node->nd_tbl;
}
else {
ruby_scope->local_vars = 0;
ruby_scope->local_tbl = 0;
}
if ((state = EXEC_TAG()) == 0) {
result = rb_eval(self, node->nd_next);
}
POP_TAG();
POP_SCOPE();
ruby_frame = frame.tmp;
if (saved_cref)
ruby_cref = saved_cref;
if (state) JUMP_TAG(state);
}
break;
case NODE_OP_ASGN1:
{
int argc; VALUE *argv; /* used in SETUP_ARGS */
VALUE recv, val;
NODE *rval;
TMP_PROTECT;
recv = rb_eval(self, node->nd_recv);
rval = node->nd_args->nd_head;
SETUP_ARGS(node->nd_args->nd_next);
val = rb_funcall2(recv, aref, argc-1, argv);
switch (node->nd_mid) {
case 0: /* OR */
if (RTEST(val)) RETURN(val);
val = rb_eval(self, rval);
break;
case 1: /* AND */
if (!RTEST(val)) RETURN(val);
val = rb_eval(self, rval);
break;
default:
val = rb_funcall(val, node->nd_mid, 1, rb_eval(self, rval));
}
argv[argc-1] = val;
rb_funcall2(recv, aset, argc, argv);
result = val;
}
break;
case NODE_OP_ASGN2:
{
ID id = node->nd_next->nd_vid;
VALUE recv, val;
recv = rb_eval(self, node->nd_recv);
val = rb_funcall(recv, id, 0);
switch (node->nd_next->nd_mid) {
case 0: /* OR */
if (RTEST(val)) RETURN(val);
val = rb_eval(self, node->nd_value);
break;
case 1: /* AND */
if (!RTEST(val)) RETURN(val);
val = rb_eval(self, node->nd_value);
break;
default:
val = rb_funcall(val, node->nd_next->nd_mid, 1,
rb_eval(self, node->nd_value));
}
rb_funcall2(recv, node->nd_next->nd_aid, 1, &val);
result = val;
}
break;
case NODE_OP_ASGN_AND:
result = rb_eval(self, node->nd_head);
if (!RTEST(result)) break;
node = node->nd_value;
goto again;
case NODE_OP_ASGN_OR:
if ((node->nd_aid && !rb_ivar_defined(self, node->nd_aid)) ||
!RTEST(result = rb_eval(self, node->nd_head))) {
node = node->nd_value;
goto again;
}
break;
case NODE_MASGN:
result = massign(self, node, rb_eval(self, node->nd_value),0);
break;
case NODE_LASGN:
if (ruby_scope->local_vars == 0)
rb_bug("unexpected local variable assignment");
result = rb_eval(self, node->nd_value);
ruby_scope->local_vars[node->nd_cnt] = result;
break;
case NODE_DASGN:
result = rb_eval(self, node->nd_value);
dvar_asgn(node->nd_vid, result);
break;
case NODE_DASGN_CURR:
result = rb_eval(self, node->nd_value);
dvar_asgn_curr(node->nd_vid, result);
break;
case NODE_GASGN:
result = rb_eval(self, node->nd_value);
rb_gvar_set(node->nd_entry, result);
break;
case NODE_IASGN:
result = rb_eval(self, node->nd_value);
rb_ivar_set(self, node->nd_vid, result);
break;
case NODE_CDECL:
if (NIL_P(ruby_class)) {
rb_raise(rb_eTypeError, "no class/module to define constant");
}
result = rb_eval(self, node->nd_value);
rb_const_set(ruby_class, node->nd_vid, result);
break;
case NODE_CVDECL:
if (NIL_P(ruby_cbase)) {
rb_raise(rb_eTypeError, "no class/module to define class variable");
}
result = rb_eval(self, node->nd_value);
rb_cvar_set(cvar_cbase(), node->nd_vid, result, Qtrue);
break;
case NODE_CVASGN:
result = rb_eval(self, node->nd_value);
rb_cvar_set(cvar_cbase(), node->nd_vid, result, Qfalse);
break;
case NODE_LVAR:
if (ruby_scope->local_vars == 0) {
rb_bug("unexpected local variable");
}
result = ruby_scope->local_vars[node->nd_cnt];
break;
case NODE_DVAR:
result = rb_dvar_ref(node->nd_vid);
break;
case NODE_GVAR:
result = rb_gvar_get(node->nd_entry);
break;
case NODE_IVAR:
result = rb_ivar_get(self, node->nd_vid);
break;
case NODE_CONST:
result = ev_const_get(RNODE(ruby_frame->cbase), node->nd_vid, self);
break;
case NODE_CVAR:
result = rb_cvar_get(cvar_cbase(), node->nd_vid);
break;
case NODE_BLOCK_ARG:
if (ruby_scope->local_vars == 0)
rb_bug("unexpected block argument");
if (rb_block_given_p()) {
result = rb_f_lambda();
ruby_scope->local_vars[node->nd_cnt] = result;
}
else {
result = Qnil;
}
break;
case NODE_COLON2:
{
VALUE klass;
klass = rb_eval(self, node->nd_head);
switch (TYPE(klass)) {
case T_CLASS:
case T_MODULE:
break;
default:
return rb_funcall(klass, node->nd_mid, 0, 0);
}
result = rb_const_get(klass, node->nd_mid);
}
break;
case NODE_COLON3:
result = rb_const_get_at(rb_cObject, node->nd_mid);
break;
case NODE_NTH_REF:
result = rb_reg_nth_match(node->nd_nth, MATCH_DATA);
break;
case NODE_BACK_REF:
switch (node->nd_nth) {
case '&':
result = rb_reg_last_match(MATCH_DATA);
break;
case '`':
result = rb_reg_match_pre(MATCH_DATA);
break;
case '\'':
result = rb_reg_match_post(MATCH_DATA);
break;
case '+':
result = rb_reg_match_last(MATCH_DATA);
break;
default:
rb_bug("unexpected back-ref");
}
break;
case NODE_HASH:
{
NODE *list;
VALUE hash = rb_hash_new();
VALUE key, val;
list = node->nd_head;
while (list) {
key = rb_eval(self, list->nd_head);
list = list->nd_next;
if (list == 0)
rb_bug("odd number list for Hash");
val = rb_eval(self, list->nd_head);
list = list->nd_next;
rb_hash_aset(hash, key, val);
}
result = hash;
}
break;
case NODE_ZARRAY: /* zero length list */
result = rb_ary_new();
break;
case NODE_ARRAY:
{
VALUE ary;
int i;
i = node->nd_alen;
ary = rb_ary_new2(i);
for (i=0;node;node=node->nd_next) {
RARRAY(ary)->ptr[i++] = rb_eval(self, node->nd_head);
RARRAY(ary)->len = i;
}
result = ary;
}
break;
case NODE_STR:
result = rb_str_new3(node->nd_lit);
break;
case NODE_DSTR:
case NODE_DXSTR:
case NODE_DREGX:
case NODE_DREGX_ONCE:
{
VALUE str, str2;
NODE *list = node->nd_next;
str = rb_str_new3(node->nd_lit);
while (list) {
if (list->nd_head) {
switch (nd_type(list->nd_head)) {
case NODE_STR:
str2 = list->nd_head->nd_lit;
break;
case NODE_EVSTR:
result = ruby_errinfo;
ruby_errinfo = Qnil;
ruby_sourceline = nd_line(node);
ruby_in_eval++;
list->nd_head = compile(list->nd_head->nd_lit,
ruby_sourcefile,
ruby_sourceline);
ruby_eval_tree = 0;
ruby_in_eval--;
if (ruby_nerrs > 0) {
compile_error("string expansion");
}
if (!NIL_P(result)) ruby_errinfo = result;
/* fall through */
default:
str2 = rb_obj_as_string(rb_eval(self, list->nd_head));
break;
}
rb_str_append(str, str2);
OBJ_INFECT(str, str2);
}
list = list->nd_next;
}
switch (nd_type(node)) {
case NODE_DREGX:
result = rb_reg_new(RSTRING(str)->ptr, RSTRING(str)->len,
node->nd_cflag);
break;
case NODE_DREGX_ONCE: /* regexp expand once */
result = rb_reg_new(RSTRING(str)->ptr, RSTRING(str)->len,
node->nd_cflag);
nd_set_type(node, NODE_LIT);
node->nd_lit = result;
break;
case NODE_DXSTR:
result = rb_funcall(self, '`', 1, str);
break;
default:
result = str;
break;
}
}
break;
case NODE_XSTR:
result = rb_funcall(self, '`', 1, node->nd_lit);
break;
case NODE_LIT:
result = node->nd_lit;
break;
case NODE_ATTRSET:
if (ruby_frame->argc != 1)
rb_raise(rb_eArgError, "wrong number of arguments(%d for 1)",
ruby_frame->argc);
result = rb_ivar_set(self, node->nd_vid, ruby_frame->argv[0]);
break;
case NODE_DEFN:
if (node->nd_defn) {
NODE *body, *defn;
VALUE origin;
int noex;
if (NIL_P(ruby_class)) {
rb_raise(rb_eTypeError, "no class/module to add method");
}
if (ruby_class == rb_cClass && node->nd_mid == alloc) {
rb_raise(rb_eNameError, "redefining Class#allocate will cause infinite loop");
}
if (ruby_class == rb_cObject && node->nd_mid == init) {
rb_warn("redefining Object#initialize may cause infinite loop");
}
if (node->nd_mid == __id__ || node->nd_mid == __send__) {
rb_warn("redefining `%s' may cause serious problem",
rb_id2name(node->nd_mid));
}
rb_frozen_class_p(ruby_class);
body = search_method(ruby_class, node->nd_mid, &origin);
if (body){
if (RTEST(ruby_verbose) && ruby_class == origin && body->nd_cnt == 0) {
rb_warning("discarding old %s", rb_id2name(node->nd_mid));
}
if (node->nd_noex) { /* toplevel */
/* should upgrade to rb_warn() if no super was called inside? */
rb_warning("overriding global function `%s'",
rb_id2name(node->nd_mid));
}
}
if (SCOPE_TEST(SCOPE_PRIVATE) || node->nd_mid == init) {
noex = NOEX_PRIVATE;
}
else if (SCOPE_TEST(SCOPE_PROTECTED)) {
noex = NOEX_PROTECTED;
}
else if (ruby_class == rb_cObject) {
noex = node->nd_noex;
}
else {
noex = NOEX_PUBLIC;
}
if (body && origin == ruby_class && body->nd_noex & NOEX_UNDEF) {
noex |= NOEX_UNDEF;
}
defn = copy_node_scope(node->nd_defn, ruby_cref);
rb_add_method(ruby_class, node->nd_mid, defn, noex);
if (scope_vmode == SCOPE_MODFUNC) {
rb_add_method(rb_singleton_class(ruby_class),
node->nd_mid, defn, NOEX_PUBLIC);
rb_funcall(ruby_class, singleton_added, 1, ID2SYM(node->nd_mid));
}
if (FL_TEST(ruby_class, FL_SINGLETON)) {
rb_funcall(rb_iv_get(ruby_class, "__attached__"),
singleton_added, 1, ID2SYM(node->nd_mid));
}
else {
rb_funcall(ruby_class, added, 1, ID2SYM(node->nd_mid));
}
result = Qnil;
}
break;
case NODE_DEFS:
if (node->nd_defn) {
VALUE recv = rb_eval(self, node->nd_recv);
VALUE klass;
NODE *body = 0, *defn;
if (rb_safe_level() >= 4 && !OBJ_TAINTED(recv)) {
rb_raise(rb_eSecurityError, "Insecure: can't define singleton method");
}
if (FIXNUM_P(recv) || SYMBOL_P(recv)) {
rb_raise(rb_eTypeError,
"can't define singleton method \"%s\" for %s",
rb_id2name(node->nd_mid),
rb_class2name(CLASS_OF(recv)));
}
if (OBJ_FROZEN(recv)) rb_error_frozen("object");
klass = rb_singleton_class(recv);
if (st_lookup(RCLASS(klass)->m_tbl, node->nd_mid, &body)) {
if (rb_safe_level() >= 4) {
rb_raise(rb_eSecurityError, "redefining method prohibited");
}
if (RTEST(ruby_verbose)) {
rb_warning("redefine %s", rb_id2name(node->nd_mid));
}
}
defn = copy_node_scope(node->nd_defn, ruby_cref);
defn->nd_rval = (VALUE)ruby_cref;
rb_add_method(klass, node->nd_mid, defn,
NOEX_PUBLIC|(body?body->nd_noex&NOEX_UNDEF:0));
rb_funcall(recv, singleton_added, 1, ID2SYM(node->nd_mid));
result = Qnil;
}
break;
case NODE_UNDEF:
if (NIL_P(ruby_class)) {
rb_raise(rb_eTypeError, "no class to undef method");
}
rb_undef(ruby_class, node->nd_mid);
result = Qnil;
break;
case NODE_ALIAS:
if (NIL_P(ruby_class)) {
rb_raise(rb_eTypeError, "no class to make alias");
}
rb_alias(ruby_class, node->nd_new, node->nd_old);
result = Qnil;
break;
case NODE_VALIAS:
rb_alias_variable(node->nd_new, node->nd_old);
result = Qnil;
break;
case NODE_CLASS:
{
VALUE super, klass, tmp;
if (NIL_P(ruby_class)) {
rb_raise(rb_eTypeError, "no outer class/module");
}
if (node->nd_super) {
super = superclass(self, node->nd_super);
}
else {
super = 0;
}
klass = 0;
if ((ruby_class == rb_cObject) && rb_autoload_defined(node->nd_cname)) {
rb_autoload_load(node->nd_cname);
}
if (rb_const_defined_at(ruby_class, node->nd_cname)) {
klass = rb_const_get(ruby_class, node->nd_cname);
}
if (klass) {
if (TYPE(klass) != T_CLASS) {
rb_raise(rb_eTypeError, "%s is not a class",
rb_id2name(node->nd_cname));
}
if (super) {
tmp = rb_class_real(RCLASS(klass)->super);
if (tmp != super) {
goto override_class;
}
}
if (rb_safe_level() >= 4) {
rb_raise(rb_eSecurityError, "extending class prohibited");
}
}
else {
override_class:
if (!super) super = rb_cObject;
klass = rb_define_class_id(node->nd_cname, super);
rb_set_class_path(klass,ruby_class,rb_id2name(node->nd_cname));
rb_class_inherited(super, klass);
rb_const_set(ruby_class, node->nd_cname, klass);
}
if (ruby_wrapper) {
rb_extend_object(klass, ruby_wrapper);
rb_include_module(klass, ruby_wrapper);
}
result = module_setup(klass, node->nd_body);
}
break;
case NODE_MODULE:
{
VALUE module;
if (NIL_P(ruby_class)) {
rb_raise(rb_eTypeError, "no outer class/module");
}
module = 0;
if ((ruby_class == rb_cObject) && rb_autoload_defined(node->nd_cname)) {
rb_autoload_load(node->nd_cname);
}
if (rb_const_defined_at(ruby_class, node->nd_cname)) {
module = rb_const_get(ruby_class, node->nd_cname);
}
if (module) {
if (TYPE(module) != T_MODULE) {
rb_raise(rb_eTypeError, "%s is not a module",
rb_id2name(node->nd_cname));
}
if (rb_safe_level() >= 4) {
rb_raise(rb_eSecurityError, "extending module prohibited");
}
}
else {
module = rb_define_module_id(node->nd_cname);
rb_const_set(ruby_class, node->nd_cname, module);
rb_set_class_path(module,ruby_class,rb_id2name(node->nd_cname));
}
if (ruby_wrapper) {
rb_extend_object(module, ruby_wrapper);
rb_include_module(module, ruby_wrapper);
}
result = module_setup(module, node->nd_body);
}
break;
case NODE_SCLASS:
{
VALUE klass;
result = rb_eval(self, node->nd_recv);
if (FIXNUM_P(result) || SYMBOL_P(result)) {
rb_raise(rb_eTypeError, "no virtual class for %s",
rb_class2name(CLASS_OF(result)));
}
if (rb_safe_level() >= 4 && !OBJ_TAINTED(result))
rb_raise(rb_eSecurityError, "Insecure: can't extend object");
klass = rb_singleton_class(result);
if (ruby_wrapper) {
rb_extend_object(klass, ruby_wrapper);
rb_include_module(klass, ruby_wrapper);
}
result = module_setup(klass, node->nd_body);
}
break;
case NODE_DEFINED:
{
char buf[20];
char *desc = is_defined(self, node->nd_head, buf);
if (desc) result = rb_str_new2(desc);
else result = Qnil;
}
break;
case NODE_NEWLINE:
ruby_sourcefile = node->nd_file;
ruby_sourceline = node->nd_nth;
if (trace_func) {
call_trace_func("line", ruby_sourcefile, ruby_sourceline, self,
ruby_frame->last_func,
ruby_frame->last_class);
}
node = node->nd_next;
goto again;
default:
rb_bug("unknown node type %d", nd_type(node));
}
finish:
CHECK_INTS;
return result;
}
static VALUE
module_setup(module, n)
VALUE module;
NODE *n;
{
NODE * volatile node = n;
int state;
struct FRAME frame;
VALUE result; /* OK */
char *file = ruby_sourcefile;
int line = ruby_sourceline;
TMP_PROTECT;
frame = *ruby_frame;
frame.tmp = ruby_frame;
ruby_frame = &frame;
PUSH_CLASS();
ruby_class = module;
PUSH_SCOPE();
PUSH_VARS();
if (node->nd_tbl) {
VALUE *vars = TMP_ALLOC(node->nd_tbl[0]+1);
*vars++ = (VALUE)node;
ruby_scope->local_vars = vars;
rb_mem_clear(ruby_scope->local_vars, node->nd_tbl[0]);
ruby_scope->local_tbl = node->nd_tbl;
}
else {
ruby_scope->local_vars = 0;
ruby_scope->local_tbl = 0;
}
PUSH_CREF(module);
ruby_frame->cbase = (VALUE)ruby_cref;
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
if (trace_func) {
call_trace_func("class", file, line, ruby_class,
ruby_frame->last_func,
ruby_frame->last_class);
}
result = rb_eval(ruby_class, node->nd_next);
}
POP_TAG();
POP_CREF();
POP_VARS();
POP_SCOPE();
POP_CLASS();
ruby_frame = frame.tmp;
if (trace_func) {
call_trace_func("end", file, line, 0,
ruby_frame->last_func, ruby_frame->last_class);
}
if (state) JUMP_TAG(state);
return result;
}
int
rb_respond_to(obj, id)
VALUE obj;
ID id;
{
if (rb_method_boundp(CLASS_OF(obj), id, 0)) {
return Qtrue;
}
return Qfalse;
}
static VALUE
rb_obj_respond_to(argc, argv, obj)
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;
}
static VALUE
rb_mod_method_defined(mod, mid)
VALUE mod, mid;
{
if (rb_method_boundp(mod, rb_to_id(mid), 1)) {
return Qtrue;
}
return Qfalse;
}
void
rb_exit(status)
int status;
{
if (prot_tag) {
VALUE exit;
exit_status = status;
exit = rb_exc_new2(rb_eSystemExit, "exit");
rb_iv_set(exit, "status", INT2NUM(status));
rb_exc_raise(exit);
}
ruby_finalize();
exit(status);
}
static VALUE
rb_f_exit(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
VALUE status;
int istatus;
rb_secure(4);
if (rb_scan_args(argc, argv, "01", &status) == 1) {
istatus = NUM2INT(status);
}
else {
istatus = 0;
}
rb_exit(istatus);
return Qnil; /* not reached */
}
static void
rb_abort()
{
if (!NIL_P(ruby_errinfo)) {
error_print();
}
rb_exit(1);
}
static VALUE
rb_f_abort(argc, argv)
int argc;
VALUE *argv;
{
rb_secure(4);
if (argc == 0) {
rb_abort();
}
else {
VALUE mesg;
rb_scan_args(argc, argv, "01", &mesg);
rb_io_puts(argc, argv, rb_stderr);
rb_exit(1);
}
return Qnil; /* not reached */
}
void
rb_iter_break()
{
JUMP_TAG(TAG_BREAK);
}
NORETURN(static void rb_longjmp _((int, VALUE)));
static VALUE make_backtrace _((void));
static void
rb_longjmp(tag, mesg)
int tag;
VALUE mesg;
{
VALUE at;
if (NIL_P(mesg)) mesg = ruby_errinfo;
if (NIL_P(mesg)) {
mesg = rb_exc_new(rb_eRuntimeError, 0, 0);
}
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)) {
ruby_errinfo = mesg;
}
if (RTEST(ruby_debug) && !NIL_P(ruby_errinfo)
&& !rb_obj_is_kind_of(ruby_errinfo, rb_eSystemExit)) {
VALUE e = ruby_errinfo;
StringValue(e);
fprintf(stderr, "Exception `%s' at %s:%d - %s\n",
rb_class2name(CLASS_OF(ruby_errinfo)),
ruby_sourcefile, ruby_sourceline,
RSTRING(e)->ptr);
}
rb_trap_restore_mask();
if (trace_func && tag != TAG_FATAL) {
call_trace_func("raise", ruby_sourcefile, ruby_sourceline,
ruby_frame->self,
ruby_frame->last_func,
ruby_frame->last_class);
}
if (!prot_tag) {
error_print();
}
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, "");
}
static VALUE
rb_f_raise(argc, argv)
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");
set_backtrace(mesg, (argc>2)?argv[2]:Qnil);
}
if (ruby_frame != top_frame) {
PUSH_FRAME(); /* fake frame */
*ruby_frame = *_frame.prev->prev;
rb_longjmp(TAG_RAISE, mesg);
POP_FRAME();
}
rb_longjmp(TAG_RAISE, mesg);
return Qnil; /* not reached */
}
void
rb_jump_tag(tag)
int tag;
{
JUMP_TAG(tag);
}
int
rb_block_given_p()
{
if (ruby_frame->iter && ruby_block)
return Qtrue;
return Qfalse;
}
int
rb_iterator_p()
{
return rb_block_given_p();
}
static VALUE
rb_f_block_given_p()
{
if (ruby_frame->prev && ruby_frame->prev->iter && ruby_block)
return Qtrue;
return Qfalse;
}
static VALUE
rb_yield_0(val, self, klass, pcall)
VALUE val, self, klass; /* OK */
int pcall;
{
NODE *node;
volatile VALUE result = Qnil;
volatile VALUE old_cref;
volatile VALUE old_wrapper;
struct BLOCK * volatile block;
struct SCOPE * volatile old_scope;
struct FRAME frame;
char *const file = ruby_sourcefile;
int line = ruby_sourceline;
int state;
static unsigned serial = 1;
if (!rb_block_given_p()) {
localjump_error("no block given", Qnil);
}
PUSH_VARS();
PUSH_CLASS();
block = ruby_block;
frame = block->frame;
frame.prev = ruby_frame;
ruby_frame = &(frame);
old_cref = (VALUE)ruby_cref;
ruby_cref = (NODE*)ruby_frame->cbase;
old_wrapper = ruby_wrapper;
ruby_wrapper = block->wrapper;
old_scope = ruby_scope;
ruby_scope = block->scope;
ruby_block = block->prev;
if (block->flags & BLOCK_D_SCOPE) {
/* put place holder for dynamic (in-block) local variables */
ruby_dyna_vars = new_dvar(0, 0, block->dyna_vars);
}
else {
/* FOR does not introduce new scope */
ruby_dyna_vars = block->dyna_vars;
}
ruby_class = klass?klass:block->klass;
if (!klass) self = block->self;
node = block->body;
if (block->var) {
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
if (block->var == (NODE*)1) {
if (pcall && RARRAY(val)->len != 0) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)",
RARRAY(val)->len);
}
}
else if (block->var == (NODE*)2) {
if (TYPE(val) == T_ARRAY && RARRAY(val)->len != 0) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)",
RARRAY(val)->len);
}
}
else {
if (nd_type(block->var) == NODE_MASGN) {
massign(self, block->var, val, pcall);
}
else {
if (pcall) val = avalue_to_svalue(val);
assign(self, block->var, val, pcall);
}
}
}
POP_TAG();
if (state) goto pop_state;
}
else if (pcall) {
val = avalue_to_svalue(val);
}
PUSH_ITER(block->iter);
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
redo:
if (!node) {
result = Qnil;
}
else if (nd_type(node) == NODE_CFUNC || nd_type(node) == NODE_IFUNC) {
result = (*node->nd_cfnc)(val, node->nd_tval, self);
}
else {
result = rb_eval(self, node);
}
}
else {
switch (state) {
case TAG_REDO:
state = 0;
CHECK_INTS;
goto redo;
case TAG_NEXT:
state = 0;
result = prot_tag->retval;
break;
case TAG_BREAK:
case TAG_RETURN:
state |= (serial++ << 8);
state |= 0x10;
block->tag->dst = state;
break;
default:
break;
}
}
POP_TAG();
pop_state:
POP_ITER();
POP_CLASS();
if (ruby_dyna_vars && (block->flags & BLOCK_D_SCOPE) &&
!FL_TEST(ruby_dyna_vars, DVAR_DONT_RECYCLE)) {
struct RVarmap *vars = ruby_dyna_vars;
if (ruby_dyna_vars->id == 0) {
vars = ruby_dyna_vars->next;
rb_gc_force_recycle((VALUE)ruby_dyna_vars);
while (vars && vars->id != 0) {
struct RVarmap *tmp = vars->next;
rb_gc_force_recycle((VALUE)vars);
vars = tmp;
}
}
}
POP_VARS();
ruby_block = block;
ruby_frame = ruby_frame->prev;
ruby_cref = (NODE*)old_cref;
ruby_wrapper = old_wrapper;
if (ruby_scope->flags & SCOPE_DONT_RECYCLE)
scope_dup(old_scope);
ruby_scope = old_scope;
ruby_sourcefile = file;
ruby_sourceline = line;
if (state) {
if (!block->tag) {
switch (state & TAG_MASK) {
case TAG_BREAK:
case TAG_RETURN:
jump_tag_but_local_jump(state & TAG_MASK);
break;
}
}
JUMP_TAG(state);
}
return result;
}
VALUE
rb_yield(val)
VALUE val;
{
return rb_yield_0(val, 0, 0, 0);
}
static VALUE
rb_f_loop()
{
for (;;) {
rb_yield_0(Qnil, 0, 0, 0);
CHECK_INTS;
}
return Qnil; /* dummy */
}
static VALUE
massign(self, node, val, pcall)
VALUE self;
NODE *node;
VALUE val;
int pcall;
{
NODE *list;
int i = 0, len;
if (!pcall) {
val = svalue_to_mvalue(val);
}
len = RARRAY(val)->len;
list = node->nd_head;
for (i=0; list && i<len; i++) {
assign(self, list->nd_head, RARRAY(val)->ptr[i], pcall);
list = list->nd_next;
}
if (pcall && list) goto arg_error;
if (node->nd_args) {
if (node->nd_args == (NODE*)-1) {
/* no check for mere `*' */
}
else if (!list && i<len) {
assign(self, node->nd_args, rb_ary_new4(len-i, RARRAY(val)->ptr+i), pcall);
}
else {
assign(self, node->nd_args, rb_ary_new2(0), pcall);
}
}
else if (pcall && i < len) {
goto arg_error;
}
while (list) {
i++;
assign(self, list->nd_head, Qnil, pcall);
list = list->nd_next;
}
return val;
arg_error:
while (list) {
i++;
list = list->nd_next;
}
rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", len, i);
}
static void
assign(self, lhs, val, pcall)
VALUE self;
NODE *lhs;
VALUE val;
int pcall;
{
if (val == Qundef) val = Qnil;
switch (nd_type(lhs)) {
case NODE_GASGN:
rb_gvar_set(lhs->nd_entry, val);
break;
case NODE_IASGN:
rb_ivar_set(self, lhs->nd_vid, val);
break;
case NODE_LASGN:
if (ruby_scope->local_vars == 0)
rb_bug("unexpected local variable assignment");
ruby_scope->local_vars[lhs->nd_cnt] = val;
break;
case NODE_DASGN:
dvar_asgn(lhs->nd_vid, val);
break;
case NODE_DASGN_CURR:
dvar_asgn_curr(lhs->nd_vid, val);
break;
case NODE_CDECL:
rb_const_set(ruby_class, lhs->nd_vid, val);
break;
case NODE_CVDECL:
if (ruby_verbose && FL_TEST(ruby_cbase, FL_SINGLETON)) {
rb_warn("declaring singleton class variable");
}
rb_cvar_set(cvar_cbase(), lhs->nd_vid, val, Qtrue);
break;
case NODE_CVASGN:
rb_cvar_set(cvar_cbase(), lhs->nd_vid, val, Qfalse);
break;
case NODE_MASGN:
massign(self, lhs, val, pcall);
break;
case NODE_CALL:
{
VALUE recv;
recv = rb_eval(self, lhs->nd_recv);
if (!lhs->nd_args) {
/* attr set */
rb_call(CLASS_OF(recv), recv, lhs->nd_mid, 1, &val, 0);
}
else {
/* array set */
VALUE args;
args = rb_eval(self, lhs->nd_args);
rb_ary_push(args, val);
rb_call(CLASS_OF(recv), recv, lhs->nd_mid,
RARRAY(args)->len, RARRAY(args)->ptr, 0);
}
}
break;
default:
rb_bug("bug in variable assignment");
break;
}
}
VALUE
rb_iterate(it_proc, data1, bl_proc, data2)
VALUE (*it_proc) _((VALUE)), (*bl_proc)(ANYARGS);
VALUE data1, data2;
{
int state;
volatile VALUE retval = Qnil;
NODE *node = NEW_IFUNC(bl_proc, data2);
VALUE self = ruby_top_self;
iter_retry:
PUSH_ITER(ITER_PRE);
PUSH_BLOCK(0, node);
PUSH_TAG(PROT_NONE);
state = EXEC_TAG();
if (state == 0) {
retval = (*it_proc)(data1);
}
if (ruby_block->tag->dst == state) {
state &= TAG_MASK;
if (state == TAG_RETURN || state == TAG_BREAK) {
retval = prot_tag->retval;
}
}
POP_TAG();
POP_BLOCK();
POP_ITER();
switch (state) {
case 0:
break;
case TAG_RETRY:
goto iter_retry;
case TAG_BREAK:
break;
case TAG_RETURN:
return_value(retval);
/* fall through */
default:
JUMP_TAG(state);
}
return retval;
}
static int
handle_rescue(self, node)
VALUE self;
NODE *node;
{
int argc; VALUE *argv; /* used in SETUP_ARGS */
TMP_PROTECT;
if (!node->nd_args) {
return rb_obj_is_kind_of(ruby_errinfo, rb_eStandardError);
}
BEGIN_CALLARGS;
SETUP_ARGS(node->nd_args);
END_CALLARGS;
while (argc--) {
if (!rb_obj_is_kind_of(argv[0], rb_cModule)) {
rb_raise(rb_eTypeError, "class or module required for rescue clause");
}
if (RTEST(rb_funcall(*argv, eqq, 1, ruby_errinfo))) return 1;
argv++;
}
return 0;
}
VALUE
#ifdef HAVE_STDARG_PROTOTYPES
rb_rescue2(VALUE (*b_proc)(ANYARGS), VALUE data1, VALUE (*r_proc)(ANYARGS), VALUE data2, ...)
#else
rb_rescue2(b_proc, data1, r_proc, data2, va_alist)
VALUE (*b_proc)(ANYARGS), (*r_proc)(ANYARGS);
VALUE data1, data2;
va_dcl
#endif
{
int state;
volatile VALUE result;
volatile VALUE e_info = ruby_errinfo;
va_list args;
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
retry_entry:
result = (*b_proc)(data1);
}
else 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(ruby_errinfo, eclass)) {
handle = Qtrue;
break;
}
}
va_end(args);
if (handle) {
if (r_proc) {
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
result = (*r_proc)(data2, ruby_errinfo);
}
POP_TAG();
if (state == TAG_RETRY) {
state = 0;
ruby_errinfo = Qnil;
goto retry_entry;
}
}
else {
result = Qnil;
state = 0;
}
if (state == 0) {
ruby_errinfo = e_info;
}
}
}
POP_TAG();
if (state) JUMP_TAG(state);
return result;
}
VALUE
rb_rescue(b_proc, data1, r_proc, data2)
VALUE (*b_proc)(), (*r_proc)();
VALUE data1, data2;
{
return rb_rescue2(b_proc, data1, r_proc, data2, rb_eStandardError, 0);
}
VALUE
rb_protect(proc, data, state)
VALUE (*proc) _((VALUE));
VALUE data;
int *state;
{
VALUE result; /* OK */
int status;
PUSH_TAG(PROT_NONE);
if ((status = EXEC_TAG()) == 0) {
result = (*proc)(data);
}
POP_TAG();
if (state) {
*state = status;
}
if (status != 0) {
return Qnil;
}
return result;
}
VALUE
rb_ensure(b_proc, data1, e_proc, data2)
VALUE (*b_proc)();
VALUE data1;
VALUE (*e_proc)();
VALUE data2;
{
int state;
volatile VALUE result = Qnil;
VALUE retval;
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
result = (*b_proc)(data1);
}
POP_TAG();
retval = prot_tag ? prot_tag->retval : Qnil; /* save retval */
(*e_proc)(data2);
if (prot_tag) return_value(retval);
if (state) JUMP_TAG(state);
return result;
}
VALUE
rb_with_disable_interrupt(proc, data)
VALUE (*proc)();
VALUE data;
{
VALUE result; /* OK */
int status;
DEFER_INTS;
PUSH_TAG(PROT_NONE);
if ((status = EXEC_TAG()) == 0) {
result = (*proc)(data);
}
POP_TAG();
ALLOW_INTS;
if (status) JUMP_TAG(status);
return result;
}
static void
stack_check()
{
static int overflowing = 0;
if (!overflowing && ruby_stack_check()) {
int state;
overflowing = 1;
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
rb_raise(rb_eSysStackError, "stack level too deep");
}
POP_TAG();
overflowing = 0;
JUMP_TAG(state);
}
}
static int last_call_status;
#define CSTAT_PRIV 1
#define CSTAT_PROT 2
#define CSTAT_VCALL 4
static VALUE
rb_f_missing(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
ID id;
VALUE exc = rb_eNoMethodError;
volatile VALUE d = 0;
char *format = 0;
char *desc = "";
char *file = ruby_sourcefile;
int line = ruby_sourceline;
if (argc == 0 || !SYMBOL_P(argv[0])) {
rb_raise(rb_eArgError, "no id given");
}
stack_check();
id = SYM2ID(argv[0]);
switch (TYPE(obj)) {
case T_NIL:
desc = "nil";
break;
case T_TRUE:
desc = "true";
break;
case T_FALSE:
desc = "false";
break;
case T_OBJECT:
d = rb_any_to_s(obj);
break;
default:
d = rb_inspect(obj);
break;
}
if (d) {
if (RSTRING(d)->len > 65) {
d = rb_any_to_s(obj);
}
desc = RSTRING(d)->ptr;
}
if (last_call_status & CSTAT_PRIV) {
format = "private method `%s' called for %s%s%s";
}
if (last_call_status & CSTAT_PROT) {
format = "protected method `%s' called for %s%s%s";
}
else if (last_call_status & CSTAT_VCALL) {
const char *mname = rb_id2name(id);
if (('a' <= mname[0] && mname[0] <= 'z') || mname[0] == '_') {
format = "undefined local variable or method `%s' for %s%s%s";
exc = rb_eNameError;
}
}
if (!format) {
format = "undefined method `%s' for %s%s%s";
}
ruby_sourcefile = file;
ruby_sourceline = line;
PUSH_FRAME(); /* fake frame */
*ruby_frame = *_frame.prev->prev;
{
char buf[BUFSIZ];
int noclass = (!d || desc[0]=='#');
snprintf(buf, BUFSIZ, format, rb_id2name(id),
desc, noclass ? "" : ":",
noclass ? "" : rb_class2name(CLASS_OF(obj)));
exc = rb_exc_new2(exc, buf);
rb_iv_set(exc, "name", argv[0]);
rb_iv_set(exc, "args", rb_ary_new4(argc-1, argv+1));
rb_exc_raise(exc);
}
POP_FRAME();
return Qnil; /* not reached */
}
static VALUE
rb_undefined(obj, id, argc, argv, call_status)
VALUE obj;
ID id;
int argc;
VALUE*argv;
int call_status;
{
VALUE *nargv;
last_call_status = call_status;
if (id == missing) {
PUSH_FRAME();
rb_f_missing(argc, argv, obj);
POP_FRAME();
}
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
call_cfunc(func, recv, len, argc, argv)
VALUE (*func)();
VALUE recv;
int len, argc;
VALUE *argv;
{
if (len >= 0 && argc != len) {
rb_raise(rb_eArgError, "wrong number of arguments(%d for %d)",
argc, len);
}
switch (len) {
case -2:
return (*func)(recv, rb_ary_new4(argc, argv));
break;
case -1:
return (*func)(argc, argv, recv);
break;
case 0:
return (*func)(recv);
break;
case 1:
return (*func)(recv, argv[0]);
break;
case 2:
return (*func)(recv, argv[0], argv[1]);
break;
case 3:
return (*func)(recv, argv[0], argv[1], argv[2]);
break;
case 4:
return (*func)(recv, argv[0], argv[1], argv[2], argv[3]);
break;
case 5:
return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4]);
break;
case 6:
return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4],
argv[5]);
break;
case 7:
return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4],
argv[5], argv[6]);
break;
case 8:
return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4],
argv[5], argv[6], argv[7]);
break;
case 9:
return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4],
argv[5], argv[6], argv[7], argv[8]);
break;
case 10:
return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4],
argv[5], argv[6], argv[7], argv[8], argv[9]);
break;
case 11:
return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4],
argv[5], argv[6], argv[7], argv[8], argv[9], argv[10]);
break;
case 12:
return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4],
argv[5], argv[6], argv[7], argv[8], argv[9],
argv[10], argv[11]);
break;
case 13:
return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4],
argv[5], argv[6], argv[7], argv[8], argv[9], argv[10],
argv[11], argv[12]);
break;
case 14:
return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4],
argv[5], argv[6], argv[7], argv[8], argv[9], argv[10],
argv[11], argv[12], argv[13]);
break;
case 15:
return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4],
argv[5], argv[6], argv[7], argv[8], argv[9], argv[10],
argv[11], argv[12], argv[13], argv[14]);
break;
default:
rb_raise(rb_eArgError, "too many arguments(%d)", len);
break;
}
return Qnil; /* not reached */
}
static VALUE
rb_call0(klass, recv, id, argc, argv, body, nosuper)
VALUE klass, recv;
ID id;
int argc; /* OK */
VALUE *argv; /* OK */
NODE *body; /* OK */
int nosuper;
{
NODE *b2; /* OK */
volatile VALUE result = Qnil;
int itr;
static int tick;
TMP_PROTECT;
switch (ruby_iter->iter) {
case ITER_PRE:
itr = ITER_CUR;
break;
case ITER_CUR:
default:
itr = ITER_NOT;
break;
}
if ((++tick & 0xff) == 0) {
CHECK_INTS; /* better than nothing */
stack_check();
}
PUSH_ITER(itr);
PUSH_FRAME();
ruby_frame->last_func = id;
ruby_frame->last_class = nosuper?0:klass;
ruby_frame->self = recv;
ruby_frame->argc = argc;
ruby_frame->argv = argv;
switch (nd_type(body)) {
case NODE_CFUNC:
{
int len = body->nd_argc;
if (len < -2) {
rb_bug("bad argc(%d) specified for `%s(%s)'",
len, rb_class2name(klass), rb_id2name(id));
}
if (trace_func) {
int state;
char *file = ruby_frame->prev->file;
int line = ruby_frame->prev->line;
if (!file) {
file = ruby_sourcefile;
line = ruby_sourceline;
}
call_trace_func("c-call", 0, 0, recv, id, klass);
PUSH_TAG(PROT_FUNC);
if ((state = EXEC_TAG()) == 0) {
result = call_cfunc(body->nd_cfnc, recv, len, argc, argv);
}
POP_TAG();
call_trace_func("c-return", 0, 0, recv, id, klass);
if (state) JUMP_TAG(state);
}
else {
result = call_cfunc(body->nd_cfnc, recv, len, argc, argv);
}
}
break;
/* for attr get/set */
case NODE_IVAR:
if (argc != 0) {
rb_raise(rb_eArgError, "wrong number of arguments(%d for 0)", argc);
}
case NODE_ATTRSET:
/* for re-scoped/renamed method */
case NODE_ZSUPER:
result = rb_eval(recv, body);
break;
case NODE_DMETHOD:
result = method_call(argc, argv, umethod_bind(body->nd_cval, recv));
break;
case NODE_BMETHOD:
result = proc_invoke(body->nd_cval, rb_ary_new4(argc, argv), Qtrue, recv);
break;
case NODE_SCOPE:
{
int state;
VALUE *local_vars; /* OK */
NODE *saved_cref = 0;
PUSH_SCOPE();
if (body->nd_rval) {
saved_cref = ruby_cref;
ruby_cref = (NODE*)body->nd_rval;
ruby_frame->cbase = body->nd_rval;
}
if (body->nd_tbl) {
local_vars = TMP_ALLOC(body->nd_tbl[0]+1);
*local_vars++ = (VALUE)body;
rb_mem_clear(local_vars, body->nd_tbl[0]);
ruby_scope->local_tbl = body->nd_tbl;
ruby_scope->local_vars = local_vars;
}
else {
local_vars = ruby_scope->local_vars = 0;
ruby_scope->local_tbl = 0;
}
b2 = body = body->nd_next;
PUSH_VARS();
PUSH_TAG(PROT_FUNC);
if ((state = EXEC_TAG()) == 0) {
NODE *node = 0;
int i;
if (nd_type(body) == NODE_ARGS) {
node = body;
body = 0;
}
else if (nd_type(body) == NODE_BLOCK) {
node = body->nd_head;
body = body->nd_next;
}
if (node) {
if (nd_type(node) != NODE_ARGS) {
rb_bug("no argument-node");
}
i = node->nd_cnt;
if (i > argc) {
rb_raise(rb_eArgError, "wrong number of arguments(%d for %d)",
argc, i);
}
if (node->nd_rest == -1) {
int opt = i;
NODE *optnode = node->nd_opt;
while (optnode) {
opt++;
optnode = optnode->nd_next;
}
if (opt < argc) {
rb_raise(rb_eArgError, "wrong number of arguments(%d for %d)",
argc, opt);
}
ruby_frame->argc = opt;
ruby_frame->argv = local_vars+2;
}
if (local_vars) {
if (i > 0) {
/* +2 for $_ and $~ */
MEMCPY(local_vars+2, argv, VALUE, i);
}
argv += i; argc -= i;
if (node->nd_opt) {
NODE *opt = node->nd_opt;
while (opt && argc) {
assign(recv, opt->nd_head, *argv, 1);
argv++; argc--;
opt = opt->nd_next;
}
if (opt) {
ruby_sourcefile = opt->nd_file;
ruby_sourceline = nd_line(opt);
rb_eval(recv, opt);
}
}
local_vars = ruby_scope->local_vars;
if (node->nd_rest >= 0) {
VALUE v;
if (argc > 0)
v = rb_ary_new4(argc,argv);
else
v = rb_ary_new2(0);
ruby_scope->local_vars[node->nd_rest] = v;
}
}
}
if (trace_func) {
call_trace_func("call", b2->nd_file, nd_line(b2),
recv, id, klass);
}
result = rb_eval(recv, body);
}
else if (state == TAG_RETURN) {
result = prot_tag->retval;
state = 0;
}
POP_TAG();
POP_VARS();
POP_SCOPE();
ruby_cref = saved_cref;
if (trace_func) {
char *file = ruby_frame->prev->file;
int line = ruby_frame->prev->line;
if (!file) {
file = ruby_sourcefile;
line = ruby_sourceline;
}
call_trace_func("return", file, line, recv, id, klass);
}
switch (state) {
case 0:
break;
case TAG_RETRY:
if (rb_block_given_p()) {
JUMP_TAG(state);
}
/* fall through */
default:
jump_tag_but_local_jump(state);
break;
}
}
break;
default:
rb_bug("unknown node type %d", nd_type(body));
break;
}
POP_FRAME();
POP_ITER();
return result;
}
static VALUE
rb_call(klass, recv, mid, argc, argv, scope)
VALUE klass, recv;
ID mid;
int argc; /* OK */
VALUE *argv; /* OK */
int scope;
{
NODE *body; /* OK */
int noex;
ID id = mid;
struct cache_entry *ent;
if (!klass) {
rb_raise(rb_eNotImpError, "method `%s' called on terminated object (0x%x)",
rb_id2name(mid), recv);
}
/* is it in the method cache? */
ent = cache + EXPR1(klass, mid);
if (ent->mid == mid && ent->klass == klass) {
if (!ent->method)
return rb_undefined(recv, mid, argc, argv, scope==2?CSTAT_VCALL:0);
klass = ent->origin;
id = ent->mid0;
noex = ent->noex;
body = ent->method;
}
else if ((body = rb_get_method_body(&klass, &id, &noex)) == 0) {
if (scope == 3) {
rb_name_error(mid, "super: no superclass method `%s'",
rb_id2name(mid));
}
return rb_undefined(recv, mid, argc, argv, scope==2?CSTAT_VCALL:0);
}
if (mid != missing) {
/* receiver specified form for private method */
if ((noex & NOEX_PRIVATE) && scope == 0)
return rb_undefined(recv, mid, argc, argv, CSTAT_PRIV);
/* self must be kind of a specified form for private method */
if ((noex & NOEX_PROTECTED)) {
VALUE defined_class = klass;
if (TYPE(defined_class) == T_ICLASS) {
defined_class = RBASIC(defined_class)->klass;
}
if (!rb_obj_is_kind_of(ruby_frame->self, rb_class_real(defined_class)))
return rb_undefined(recv, mid, argc, argv, CSTAT_PROT);
}
}
return rb_call0(klass, recv, id, argc, argv, body, noex & NOEX_UNDEF);
}
VALUE
rb_apply(recv, mid, args)
VALUE recv;
ID mid;
VALUE args;
{
int argc;
VALUE *argv;
argc = RARRAY(args)->len;
argv = ALLOCA_N(VALUE, argc);
MEMCPY(argv, RARRAY(args)->ptr, VALUE, argc);
return rb_call(CLASS_OF(recv), recv, mid, argc, argv, 1);
}
static VALUE
rb_f_send(argc, argv, recv)
int argc;
VALUE *argv;
VALUE recv;
{
VALUE vid;
if (argc == 0) rb_raise(rb_eArgError, "no method name given");
vid = *argv++; argc--;
PUSH_ITER(rb_block_given_p()?ITER_PRE:ITER_NOT);
vid = rb_call(CLASS_OF(recv), recv, rb_to_id(vid), argc, argv, 1);
POP_ITER();
return vid;
}
#ifdef HAVE_STDARG_PROTOTYPES
#include <stdarg.h>
#define va_init_list(a,b) va_start(a,b)
#else
#include <varargs.h>
#define va_init_list(a,b) va_start(a)
#endif
VALUE
#ifdef HAVE_STDARG_PROTOTYPES
rb_funcall(VALUE recv, ID mid, int n, ...)
#else
rb_funcall(recv, mid, n, va_alist)
VALUE recv;
ID mid;
int n;
va_dcl
#endif
{
va_list ar;
VALUE *argv;
if (n > 0) {
int i;
argv = ALLOCA_N(VALUE, n);
va_init_list(ar, 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, 1);
}
VALUE
rb_funcall2(recv, mid, argc, argv)
VALUE recv;
ID mid;
int argc;
VALUE *argv;
{
return rb_call(CLASS_OF(recv), recv, mid, argc, argv, 1);
}
VALUE
rb_funcall3(recv, mid, argc, argv)
VALUE recv;
ID mid;
int argc;
VALUE *argv;
{
return rb_call(CLASS_OF(recv), recv, mid, argc, argv, 0);
}
VALUE
rb_call_super(argc, argv)
int argc;
VALUE *argv;
{
VALUE result;
if (ruby_frame->last_class == 0) {
rb_name_error(ruby_frame->last_func, "superclass method `%s' must be enabled by rb_enable_super()",
rb_id2name(ruby_frame->last_func));
}
PUSH_ITER(ruby_iter->iter?ITER_PRE:ITER_NOT);
result = rb_call(RCLASS(ruby_frame->last_class)->super,
ruby_frame->self, ruby_frame->last_func,
argc, argv, 3);
POP_ITER();
return result;
}
static VALUE
backtrace(lev)
int lev;
{
struct FRAME *frame = ruby_frame;
char buf[BUFSIZ];
VALUE ary;
ary = rb_ary_new();
if (lev < 0) {
if (frame->last_func) {
snprintf(buf, BUFSIZ, "%s:%d:in `%s'",
ruby_sourcefile, ruby_sourceline,
rb_id2name(frame->last_func));
}
else if (ruby_sourceline == 0) {
snprintf(buf, BUFSIZ, "%s", ruby_sourcefile);
}
else {
snprintf(buf, BUFSIZ, "%s:%d", ruby_sourcefile, ruby_sourceline);
}
rb_ary_push(ary, rb_str_new2(buf));
}
else {
while (lev-- > 0) {
frame = frame->prev;
if (!frame) {
ary = Qnil;
break;
}
}
}
while (frame && frame->file) {
if (frame->prev && frame->prev->last_func) {
snprintf(buf, BUFSIZ, "%s:%d:in `%s'",
frame->file, frame->line,
rb_id2name(frame->prev->last_func));
}
else {
snprintf(buf, BUFSIZ, "%s:%d", frame->file, frame->line);
}
rb_ary_push(ary, rb_str_new2(buf));
frame = frame->prev;
}
return ary;
}
static VALUE
rb_f_caller(argc, argv)
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()
{
int i;
VALUE ary;
ary = backtrace(-1);
for (i=0; i<RARRAY(ary)->len; i++) {
printf("\tfrom %s\n", RSTRING(RARRAY(ary)->ptr[i])->ptr);
}
}
static VALUE
make_backtrace()
{
return backtrace(-1);
}
ID
rb_frame_last_func()
{
return ruby_frame->last_func;
}
static NODE*
compile(src, file, line)
VALUE src;
char *file;
int line;
{
NODE *node;
ruby_nerrs = 0;
Check_Type(src, T_STRING);
node = rb_compile_string(file, src, line);
if (ruby_nerrs == 0) return node;
return 0;
}
static VALUE
eval(self, src, scope, file, line)
VALUE self, src, scope;
char *file;
int line;
{
struct BLOCK *data;
volatile VALUE result = Qnil;
struct SCOPE * volatile old_scope;
struct BLOCK * volatile old_block;
struct RVarmap * volatile old_dyna_vars;
VALUE volatile old_cref;
int volatile old_vmode;
volatile VALUE old_wrapper;
struct FRAME frame;
char *filesave = ruby_sourcefile;
int linesave = ruby_sourceline;
volatile int iter = ruby_frame->iter;
int state;
if (!NIL_P(scope)) {
if (!rb_obj_is_block(scope)) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected Proc/Binding)",
rb_class2name(CLASS_OF(scope)));
}
Data_Get_Struct(scope, struct BLOCK, data);
/* PUSH BLOCK from data */
frame = data->frame;
frame.tmp = ruby_frame; /* gc protection */
ruby_frame = &(frame);
old_scope = ruby_scope;
ruby_scope = data->scope;
old_block = ruby_block;
ruby_block = data->prev;
old_dyna_vars = ruby_dyna_vars;
ruby_dyna_vars = data->dyna_vars;
old_vmode = scope_vmode;
scope_vmode = data->vmode;
old_cref = (VALUE)ruby_cref;
ruby_cref = (NODE*)ruby_frame->cbase;
old_wrapper = ruby_wrapper;
ruby_wrapper = data->wrapper;
if ((file == 0 || (line == 1 && strcmp(file, "(eval)") == 0)) &&
data->body && data->body->nd_file) {
file = data->body->nd_file;
line = nd_line(data->body);
}
self = data->self;
ruby_frame->iter = data->iter;
}
else {
if (ruby_frame->prev) {
ruby_frame->iter = ruby_frame->prev->iter;
}
}
if (file == 0) {
file = ruby_sourcefile;
line = ruby_sourceline;
}
PUSH_CLASS();
ruby_class = ruby_cbase;
ruby_in_eval++;
if (TYPE(ruby_class) == T_ICLASS) {
ruby_class = RBASIC(ruby_class)->klass;
}
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
NODE *node;
result = ruby_errinfo;
ruby_errinfo = Qnil;
node = compile(src, file, line);
if (ruby_nerrs > 0) {
compile_error(0);
}
if (!NIL_P(result)) ruby_errinfo = result;
result = eval_node(self, node);
}
POP_TAG();
POP_CLASS();
ruby_in_eval--;
if (!NIL_P(scope)) {
int dont_recycle = ruby_scope->flags & SCOPE_DONT_RECYCLE;
ruby_wrapper = old_wrapper;
ruby_cref = (NODE*)old_cref;
ruby_frame = frame.tmp;
ruby_scope = old_scope;
ruby_block = old_block;
ruby_dyna_vars = old_dyna_vars;
data->vmode = scope_vmode; /* write back visibility mode */
scope_vmode = old_vmode;
if (dont_recycle) {
struct tag *tag;
struct RVarmap *vars;
scope_dup(ruby_scope);
for (tag=prot_tag; tag; tag=tag->prev) {
scope_dup(tag->scope);
}
if (ruby_block) {
struct BLOCK *block = ruby_block;
while (block) {
block->tag->flags |= BLOCK_DYNAMIC;
block = block->prev;
}
}
for (vars = ruby_dyna_vars; vars; vars = vars->next) {
FL_SET(vars, DVAR_DONT_RECYCLE);
}
}
}
else {
ruby_frame->iter = iter;
}
ruby_sourcefile = filesave;
ruby_sourceline = linesave;
if (state) {
if (state == TAG_RAISE) {
VALUE err;
VALUE errat;
if (strcmp(file, "(eval)") == 0) {
if (ruby_sourceline > 1) {
errat = get_backtrace(ruby_errinfo);
err = RARRAY(errat)->ptr[0];
rb_str_cat2(err, ": ");
rb_str_append(err, ruby_errinfo);
}
else {
err = rb_str_dup(ruby_errinfo);
}
errat = Qnil;
rb_exc_raise(rb_funcall(ruby_errinfo, rb_intern("exception"), 1, err));
}
rb_exc_raise(ruby_errinfo);
}
JUMP_TAG(state);
}
return result;
}
static VALUE
rb_f_eval(argc, argv, self)
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 (ruby_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) {
file = StringValuePtr(vfile);
}
if (argc >= 4) {
line = NUM2INT(vline);
}
if (NIL_P(scope) && ruby_frame->prev) {
struct FRAME *prev;
VALUE val;
prev = ruby_frame;
PUSH_FRAME();
*ruby_frame = *prev->prev;
ruby_frame->prev = prev;
val = eval(self, src, scope, file, line);
POP_FRAME();
return val;
}
return eval(self, src, scope, file, line);
}
/* function to call func under the specified class/module context */
static VALUE
exec_under(func, under, args)
VALUE (*func)();
VALUE under;
void *args;
{
VALUE val; /* OK */
int state;
int mode;
PUSH_CLASS();
ruby_class = under;
PUSH_FRAME();
ruby_frame->self = _frame.prev->self;
ruby_frame->last_func = _frame.prev->last_func;
ruby_frame->last_class = _frame.prev->last_class;
ruby_frame->argc = _frame.prev->argc;
ruby_frame->argv = _frame.prev->argv;
if (ruby_cbase != under) {
ruby_frame->cbase = (VALUE)rb_node_newnode(NODE_CREF,under,0,ruby_frame->cbase);
}
PUSH_CREF(under);
mode = scope_vmode;
SCOPE_SET(SCOPE_PUBLIC);
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
val = (*func)(args);
}
POP_TAG();
POP_CREF();
SCOPE_SET(mode);
POP_FRAME();
POP_CLASS();
if (state) JUMP_TAG(state);
return val;
}
static VALUE
eval_under_i(args)
VALUE *args;
{
return eval(args[0], args[1], Qnil, (char*)args[2], (int)args[3]);
}
/* string eval under the class/module context */
static VALUE
eval_under(under, self, src, file, line)
VALUE under, self, src;
const char *file;
int line;
{
VALUE args[4];
if (ruby_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, args);
}
static VALUE
yield_under_i(self)
VALUE self;
{
if (ruby_block->flags & BLOCK_DYNAMIC) {
struct BLOCK * volatile old_block = ruby_block;
struct BLOCK block;
/* cbase should be pointed from volatile local variable */
/* to be protected from GC. */
VALUE result;
int state;
/* copy the block to avoid modifying global data. */
block = *ruby_block;
block.frame.cbase = ruby_frame->cbase;
ruby_block = █
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
result = rb_yield_0(self, self, ruby_class, 0);
}
POP_TAG();
ruby_block = old_block;
if (state) JUMP_TAG(state);
return result;
}
/* static block, no need to restore */
ruby_block->frame.cbase = ruby_frame->cbase;
return rb_yield_0(self, self, ruby_class, 0);
}
/* block eval under the class/module context */
static VALUE
yield_under(under, self)
VALUE under, self;
{
return exec_under(yield_under_i, under, self);
}
static VALUE
specific_eval(argc, argv, klass, self)
int argc;
VALUE *argv;
VALUE klass, 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);
}
else {
char *file = "(eval)";
int line = 1;
if (argc == 0) {
rb_raise(rb_eArgError, "block not supplied");
}
else {
if (ruby_safe_level >= 4) {
StringValue(argv[0]);
}
else {
SafeStringValue(argv[0]);
}
if (argc > 3) {
rb_raise(rb_eArgError, "wrong number of arguments: %s(src) or %s{..}",
rb_id2name(ruby_frame->last_func),
rb_id2name(ruby_frame->last_func));
}
if (argc > 1) {
file = StringValuePtr(argv[1]);
}
if (argc > 2) line = NUM2INT(argv[2]);
}
return eval_under(klass, self, argv[0], file, line);
}
}
VALUE
rb_obj_instance_eval(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
VALUE klass;
if (rb_special_const_p(self)) {
klass = Qnil;
}
else {
klass = rb_singleton_class(self);
}
return specific_eval(argc, argv, klass, self);
}
VALUE
rb_mod_module_eval(argc, argv, mod)
int argc;
VALUE *argv;
VALUE mod;
{
return specific_eval(argc, argv, mod, mod);
}
VALUE rb_load_path;
void
rb_load(fname, wrap)
VALUE fname;
int wrap;
{
VALUE tmp;
int state;
volatile ID last_func;
volatile VALUE wrapper = 0;
volatile VALUE self = ruby_top_self;
NODE *saved_cref = ruby_cref;
TMP_PROTECT;
if (wrap && ruby_safe_level >= 4) {
StringValue(fname);
}
else {
SafeStringValue(fname);
}
tmp = rb_find_file(fname);
if (!tmp) {
rb_raise(rb_eLoadError, "No such file to load -- %s", RSTRING(fname)->ptr);
}
fname = tmp;
ruby_errinfo = Qnil; /* ensure */
PUSH_VARS();
PUSH_CLASS();
wrapper = ruby_wrapper;
ruby_cref = top_cref;
if (!wrap) {
rb_secure(4); /* should alter global state */
ruby_class = rb_cObject;
ruby_wrapper = 0;
}
else {
/* load in anonymous module as toplevel */
ruby_class = ruby_wrapper = rb_module_new();
self = rb_obj_clone(ruby_top_self);
rb_extend_object(self, ruby_class);
PUSH_CREF(ruby_wrapper);
}
PUSH_FRAME();
ruby_frame->last_func = 0;
ruby_frame->last_class = 0;
ruby_frame->self = self;
ruby_frame->cbase = (VALUE)rb_node_newnode(NODE_CREF,ruby_class,0,0);
PUSH_SCOPE();
/* default visibility is private at loading toplevel */
SCOPE_SET(SCOPE_PRIVATE);
PUSH_TAG(PROT_NONE);
state = EXEC_TAG();
last_func = ruby_frame->last_func;
if (state == 0) {
NODE *node;
DEFER_INTS;
ruby_in_eval++;
rb_load_file(RSTRING(fname)->ptr);
ruby_in_eval--;
node = ruby_eval_tree;
ALLOW_INTS;
if (ruby_nerrs == 0) {
eval_node(self, node);
}
}
ruby_frame->last_func = last_func;
if (ruby_scope->flags == SCOPE_ALLOCA && ruby_class == rb_cObject) {
if (ruby_scope->local_tbl) /* toplevel was empty */
free(ruby_scope->local_tbl);
}
POP_TAG();
ruby_cref = saved_cref;
POP_SCOPE();
POP_FRAME();
POP_CLASS();
POP_VARS();
ruby_wrapper = wrapper;
if (ruby_nerrs > 0) {
ruby_nerrs = 0;
rb_exc_raise(ruby_errinfo);
}
if (state) jump_tag_but_local_jump(state);
if (!NIL_P(ruby_errinfo)) /* exception during load */
rb_exc_raise(ruby_errinfo);
}
void
rb_load_protect(fname, wrap, state)
VALUE fname;
int wrap;
int *state;
{
int status;
PUSH_TAG(PROT_NONE);
if ((status = EXEC_TAG()) == 0) {
rb_load(fname, wrap);
}
POP_TAG();
if (state) *state = status;
}
static VALUE
rb_f_load(argc, argv)
int argc;
VALUE *argv;
{
VALUE fname, wrap;
rb_scan_args(argc, argv, "11", &fname, &wrap);
rb_load(fname, RTEST(wrap));
return Qtrue;
}
VALUE ruby_dln_librefs;
static VALUE rb_features;
static st_table *loading_tbl;
static int
rb_feature_p(feature, wait)
const char *feature;
int wait;
{
VALUE v;
char *f;
int i, len = strlen(feature);
for (i = 0; i < RARRAY(rb_features)->len; ++i) {
v = RARRAY(rb_features)->ptr[i];
f = StringValuePtr(v);
if (strcmp(f, feature) == 0) {
goto load_wait;
}
if (strncmp(f, feature, len) == 0) {
if (strcmp(f+len, ".so") == 0) {
return Qtrue;
}
if (strcmp(f+len, ".rb") == 0) {
if (wait) goto load_wait;
return Qtrue;
}
}
}
return Qfalse;
load_wait:
if (loading_tbl) {
char *ext = strrchr(f, '.');
if (ext && strcmp(ext, ".rb") == 0) {
rb_thread_t th;
while (st_lookup(loading_tbl, f, &th)) {
if (th == curr_thread) {
return Qtrue;
}
CHECK_INTS;
rb_thread_schedule();
}
}
}
return Qtrue;
}
static const char *const loadable_ext[] = {
".rb", DLEXT,
#ifdef DLEXT2
DLEXT2,
#endif
0
};
int
rb_provided(feature)
const char *feature;
{
VALUE f = rb_str_new2(feature);
if (strrchr(feature, '.') == 0) {
if (rb_find_file_ext(&f, loadable_ext) == 0) {
return rb_feature_p(feature, Qfalse);
}
}
return rb_feature_p(RSTRING(f)->ptr, Qfalse);
}
static void
rb_provide_feature(feature)
VALUE feature;
{
rb_ary_push(rb_features, feature);
}
void
rb_provide(feature)
const char *feature;
{
rb_provide_feature(rb_str_new2(feature));
}
VALUE
rb_f_require(obj, fname)
VALUE obj, fname;
{
VALUE feature, tmp;
char *ext, *ftptr; /* OK */
volatile VALUE load;
int state;
volatile int safe = ruby_safe_level;
SafeStringValue(fname);
ext = strrchr(RSTRING(fname)->ptr, '.');
if (ext) {
if (strcmp(".rb", ext) == 0) {
feature = rb_str_dup(fname);
tmp = rb_find_file(fname);
if (tmp) {
fname = tmp;
goto load_rb;
}
}
else if (strcmp(".so", ext) == 0 || strcmp(".o", ext) == 0) {
fname = rb_str_new(RSTRING(fname)->ptr, ext-RSTRING(fname)->ptr);
#ifdef DLEXT2
tmp = fname;
if (rb_find_file_ext(&tmp, loadable_ext+1)) {
feature = tmp;
fname = rb_find_file(tmp);
goto load_dyna;
}
#else
feature = tmp = rb_str_dup(fname);
rb_str_cat2(tmp, DLEXT);
tmp = rb_find_file(tmp);
if (tmp) {
fname = tmp;
goto load_dyna;
}
#endif
}
else if (strcmp(DLEXT, ext) == 0) {
tmp = rb_find_file(fname);
if (tmp) {
feature = fname;
fname = tmp;
goto load_dyna;
}
}
#ifdef DLEXT2
else if (strcmp(DLEXT2, ext) == 0) {
tmp = rb_find_file(fname);
if (tmp) {
feature = fname;
fname = tmp;
goto load_dyna;
}
}
#endif
}
tmp = fname;
switch (rb_find_file_ext(&tmp, loadable_ext)) {
case 0:
break;
case 1:
feature = fname = tmp;
goto load_rb;
default:
feature = tmp;
fname = rb_find_file(tmp);
goto load_dyna;
}
if (rb_feature_p(RSTRING(fname)->ptr, Qfalse))
return Qfalse;
rb_raise(rb_eLoadError, "No such file to load -- %s", RSTRING(fname)->ptr);
load_dyna:
if (rb_feature_p(RSTRING(feature)->ptr, Qfalse))
return Qfalse;
rb_provide_feature(feature);
{
int volatile old_vmode = scope_vmode;
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
void *handle;
SCOPE_SET(SCOPE_PUBLIC);
handle = dln_load(RSTRING(fname)->ptr);
rb_ary_push(ruby_dln_librefs, INT2NUM((long)handle));
}
POP_TAG();
SCOPE_SET(old_vmode);
}
if (state) JUMP_TAG(state);
return Qtrue;
load_rb:
if (rb_feature_p(RSTRING(feature)->ptr, Qtrue))
return Qfalse;
ruby_safe_level = 0;
rb_provide_feature(feature);
/* loading ruby library should be serialized. */
if (!loading_tbl) {
loading_tbl = st_init_strtable();
}
/* partial state */
ftptr = ruby_strdup(RSTRING(feature)->ptr);
st_insert(loading_tbl, ftptr, curr_thread);
PUSH_TAG(PROT_NONE);
if ((state = EXEC_TAG()) == 0) {
rb_load(fname, 0);
}
POP_TAG();
st_delete(loading_tbl, &ftptr, 0); /* loading done */
free(ftptr);
ruby_safe_level = safe;
if (state) JUMP_TAG(state);
return Qtrue;
}
VALUE
rb_require(fname)
const char *fname;
{
return rb_f_require(Qnil, rb_str_new2(fname));
}
static void
secure_visibility(self)
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(self, argc, argv, ex)
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);
}
}
static VALUE
rb_mod_public(argc, argv, module)
int argc;
VALUE *argv;
VALUE module;
{
secure_visibility(module);
if (argc == 0) {
SCOPE_SET(SCOPE_PUBLIC);
}
else {
set_method_visibility(module, argc, argv, NOEX_PUBLIC);
}
return module;
}
static VALUE
rb_mod_protected(argc, argv, module)
int argc;
VALUE *argv;
VALUE module;
{
secure_visibility(module);
if (argc == 0) {
SCOPE_SET(SCOPE_PROTECTED);
}
else {
set_method_visibility(module, argc, argv, NOEX_PROTECTED);
}
return module;
}
static VALUE
rb_mod_private(argc, argv, module)
int argc;
VALUE *argv;
VALUE module;
{
secure_visibility(module);
if (argc == 0) {
SCOPE_SET(SCOPE_PRIVATE);
}
else {
set_method_visibility(module, argc, argv, NOEX_PRIVATE);
}
return module;
}
static VALUE
rb_mod_public_method(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PUBLIC);
return obj;
}
static VALUE
rb_mod_private_method(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PRIVATE);
return obj;
}
static VALUE
top_public(argc, argv)
int argc;
VALUE *argv;
{
return rb_mod_public(argc, argv, rb_cObject);
}
static VALUE
top_private(argc, argv)
int argc;
VALUE *argv;
{
return rb_mod_private(argc, argv, rb_cObject);
}
static VALUE
rb_mod_modfunc(argc, argv, module)
int argc;
VALUE *argv;
VALUE module;
{
int i;
ID id;
NODE *body;
if (TYPE(module) != T_MODULE) {
rb_raise(rb_eTypeError, "module_function must be called for modules");
}
secure_visibility(module);
if (argc == 0) {
SCOPE_SET(SCOPE_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 (;;) {
body = search_method(m, id, &m);
if (body == 0 || body->nd_body == 0) {
rb_bug("undefined method `%s'; can't happen", rb_id2name(id));
}
if (nd_type(body->nd_body) != NODE_ZSUPER) {
break; /* normal case: need not to follow 'super' link */
}
m = RCLASS(m)->super;
}
rb_add_method(rb_singleton_class(module), id, body->nd_body, NOEX_PUBLIC);
rb_funcall(module, singleton_added, 1, ID2SYM(id));
}
return module;
}
static VALUE
rb_mod_append_features(module, include)
VALUE module, 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;
}
static VALUE
rb_mod_include(argc, argv, module)
int argc;
VALUE *argv;
VALUE module;
{
while (argc--) {
VALUE m = argv[argc];
Check_Type(m, T_MODULE);
rb_funcall(m, rb_intern("append_features"), 1, module);
rb_funcall(m, rb_intern("included"), 1, module);
}
return module;
}
void
rb_obj_call_init(obj, argc, argv)
VALUE obj;
int argc;
VALUE *argv;
{
PUSH_ITER(rb_block_given_p()?ITER_PRE:ITER_NOT);
rb_funcall2(obj, init, argc, argv);
POP_ITER();
}
static VALUE
top_include(argc, argv)
int argc;
VALUE *argv;
{
rb_secure(4);
return rb_mod_include(argc, argv, rb_cObject);
}
void
rb_extend_object(obj, module)
VALUE obj, module;
{
rb_include_module(rb_singleton_class(obj), module);
}
static VALUE
rb_mod_extend_object(mod, obj)
VALUE mod, obj;
{
rb_extend_object(obj, mod);
return obj;
}
static VALUE
rb_obj_extend(argc, argv, obj)
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);
}
return obj;
}
VALUE rb_f_trace_var();
VALUE rb_f_untrace_var();
static void
errinfo_setter(val, id, var)
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 $!");
}
*var = val;
}
static VALUE
errat_getter(id)
ID id;
{
return get_backtrace(ruby_errinfo);
}
static void
errat_setter(val, id, var)
VALUE val;
ID id;
VALUE *var;
{
if (NIL_P(ruby_errinfo)) {
rb_raise(rb_eArgError, "$! not set");
}
set_backtrace(ruby_errinfo, val);
}
static VALUE
rb_f_local_variables()
{
ID *tbl;
int n, i;
VALUE ary = rb_ary_new();
struct RVarmap *vars;
tbl = ruby_scope->local_tbl;
if (tbl) {
n = *tbl++;
for (i=2; i<n; i++) { /* skip first 2 ($_ and $~) */
if (!rb_is_local_id(tbl[i])) continue; /* skip flip states */
rb_ary_push(ary, rb_str_new2(rb_id2name(tbl[i])));
}
}
vars = ruby_dyna_vars;
while (vars) {
if (vars->id) {
rb_ary_push(ary, rb_str_new2(rb_id2name(vars->id)));
}
vars = vars->next;
}
return ary;
}
static VALUE rb_f_catch _((VALUE,VALUE));
NORETURN(static VALUE rb_f_throw _((int,VALUE*)));
struct end_proc_data {
void (*func)();
VALUE data;
struct end_proc_data *next;
};
static struct end_proc_data *end_procs, *ephemeral_end_procs;
void
rb_set_end_proc(func, data)
void (*func) _((VALUE));
VALUE data;
{
struct end_proc_data *link = ALLOC(struct end_proc_data);
struct end_proc_data **list;
if (ruby_wrapper) list = &ephemeral_end_procs;
else list = &end_procs;
link->next = *list;
link->func = func;
link->data = data;
*list = link;
}
void
rb_mark_end_proc()
{
struct end_proc_data *link;
link = end_procs;
while (link) {
rb_gc_mark(link->data);
link = link->next;
}
link = ephemeral_end_procs;
while (link) {
rb_gc_mark(link->data);
link = link->next;
}
}
static void call_end_proc _((VALUE data));
static void
call_end_proc(data)
VALUE data;
{
PUSH_ITER(ITER_NOT);
PUSH_FRAME();
ruby_frame->self = ruby_frame->prev->self;
ruby_frame->last_func = 0;
ruby_frame->last_class = 0;
proc_invoke(data, rb_ary_new2(0), Qfalse, Qundef);
POP_FRAME();
POP_ITER();
}
static void
rb_f_END()
{
PUSH_FRAME();
ruby_frame->argc = 0;
rb_set_end_proc(call_end_proc, rb_f_lambda());
POP_FRAME();
}
static VALUE
rb_f_at_exit()
{
VALUE proc;
proc = rb_f_lambda();
rb_set_end_proc(call_end_proc, proc);
return proc;
}
void
rb_exec_end_proc()
{
struct end_proc_data *link, *save;
int status;
save = link = end_procs;
while (link) {
rb_protect((VALUE(*)_((VALUE)))link->func, link->data, &status);
if (status) {
error_handle(status);
}
link = link->next;
}
link = end_procs;
while (link != save) {
rb_protect((VALUE(*)_((VALUE)))link->func, link->data, &status);
if (status) {
error_handle(status);
}
link = link->next;
}
while (ephemeral_end_procs) {
link = ephemeral_end_procs;
ephemeral_end_procs = link->next;
rb_protect((VALUE(*)_((VALUE)))link->func, link->data, &status);
if (status) {
error_handle(status);
}
free(link);
}
}
void
Init_eval()
{
init = rb_intern("initialize");
alloc = rb_intern("allocate");
eqq = rb_intern("===");
each = rb_intern("each");
aref = rb_intern("[]");
aset = rb_intern("[]=");
match = rb_intern("=~");
to_ary = rb_intern("to_ary");
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");
__id__ = rb_intern("__id__");
__send__ = rb_intern("__send__");
rb_global_variable((VALUE*)&top_scope);
rb_global_variable((VALUE*)&ruby_eval_tree_begin);
rb_global_variable((VALUE*)&ruby_eval_tree);
rb_global_variable((VALUE*)&ruby_dyna_vars);
rb_define_virtual_variable("$@", errat_getter, errat_setter);
rb_define_hooked_variable("$!", &ruby_errinfo, 0, 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_f_missing, -1);
rb_define_global_function("loop", rb_f_loop, 0);
rb_define_method(rb_mKernel, "respond_to?", rb_obj_respond_to, -1);
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("exit", rb_f_exit, -1);
rb_define_global_function("abort", rb_f_abort, -1);
rb_define_global_function("at_exit", rb_f_at_exit, 0);
rb_define_global_function("catch", rb_f_catch, 1);
rb_define_global_function("throw", rb_f_throw, -1);
rb_define_global_function("global_variables", rb_f_global_variables, 0);
rb_define_global_function("local_variables", rb_f_local_variables, 0);
rb_define_method(rb_mKernel, "send", rb_f_send, -1);
rb_define_method(rb_mKernel, "__send__", rb_f_send, -1);
rb_define_method(rb_mKernel, "instance_eval", rb_obj_instance_eval, -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_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_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_private_method(rb_cModule, "define_method", rb_mod_define_method, -1);
rb_define_singleton_method(rb_cModule, "nesting", rb_mod_nesting, 0);
rb_define_singleton_method(rb_cModule, "constants", rb_mod_s_constants, 0);
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);
rb_define_global_function("untrace_var", rb_f_untrace_var, -1);
rb_define_global_function("set_trace_func", set_trace_func, 1);
rb_global_variable(&trace_func);
rb_define_virtual_variable("$SAFE", safe_getter, safe_setter);
}
VALUE rb_f_autoload();
void
Init_load()
{
rb_load_path = rb_ary_new();
rb_define_readonly_variable("$:", &rb_load_path);
rb_define_readonly_variable("$-I", &rb_load_path);
rb_define_readonly_variable("$LOAD_PATH", &rb_load_path);
rb_features = rb_ary_new();
rb_define_readonly_variable("$\"", &rb_features);
rb_define_global_function("load", rb_f_load, -1);
rb_define_global_function("require", rb_f_require, 1);
rb_define_global_function("autoload", rb_f_autoload, 2);
rb_global_variable(&ruby_wrapper);
ruby_dln_librefs = rb_ary_new();
rb_global_variable(&ruby_dln_librefs);
}
static void
scope_dup(scope)
struct SCOPE *scope;
{
ID *tbl;
VALUE *vars;
scope->flags |= SCOPE_DONT_RECYCLE;
if (scope->flags & SCOPE_MALLOC) return;
if (scope->local_tbl) {
tbl = scope->local_tbl;
vars = ALLOC_N(VALUE, tbl[0]+1);
*vars++ = scope->local_vars[-1];
MEMCPY(vars, scope->local_vars, VALUE, tbl[0]);
scope->local_vars = vars;
scope->flags |= SCOPE_MALLOC;
}
}
static void
blk_mark(data)
struct BLOCK *data;
{
while (data) {
rb_gc_mark_frame(&data->frame);
rb_gc_mark((VALUE)data->scope);
rb_gc_mark((VALUE)data->var);
rb_gc_mark((VALUE)data->body);
rb_gc_mark((VALUE)data->self);
rb_gc_mark((VALUE)data->dyna_vars);
rb_gc_mark((VALUE)data->klass);
rb_gc_mark((VALUE)data->tag);
rb_gc_mark(data->wrapper);
data = data->prev;
}
}
static void
blk_free(data)
struct BLOCK *data;
{
struct FRAME *frame;
void *tmp;
frame = data->frame.prev;
while (frame) {
if (frame->argc > 0 && (frame->flags & FRAME_MALLOC))
free(frame->argv);
tmp = frame;
frame = frame->prev;
free(tmp);
}
while (data) {
if (data->frame.argc > 0)
free(data->frame.argv);
tmp = data;
data = data->prev;
free(tmp);
}
}
static void
blk_copy_prev(block)
struct BLOCK *block;
{
struct BLOCK *tmp;
struct RVarmap* vars;
while (block->prev) {
tmp = ALLOC_N(struct BLOCK, 1);
MEMCPY(tmp, block->prev, struct BLOCK, 1);
if (tmp->frame.argc > 0) {
tmp->frame.argv = ALLOC_N(VALUE, tmp->frame.argc);
MEMCPY(tmp->frame.argv, block->prev->frame.argv, VALUE, tmp->frame.argc);
tmp->frame.flags |= FRAME_MALLOC;
}
scope_dup(tmp->scope);
tmp->tag->flags |= BLOCK_DYNAMIC;
for (vars = tmp->dyna_vars; vars; vars = vars->next) {
if (FL_TEST(vars, DVAR_DONT_RECYCLE)) break;
FL_SET(vars, DVAR_DONT_RECYCLE);
}
block->prev = tmp;
block = tmp;
}
}
static void
frame_dup(frame)
struct FRAME *frame;
{
VALUE *argv;
struct FRAME *tmp;
for (;;) {
if (frame->argc > 0) {
argv = ALLOC_N(VALUE, frame->argc);
MEMCPY(argv, frame->argv, VALUE, frame->argc);
frame->argv = argv;
frame->flags |= FRAME_MALLOC;
}
frame->tmp = 0; /* should not preserve tmp */
if (!frame->prev) break;
tmp = ALLOC(struct FRAME);
*tmp = *frame->prev;
frame->prev = tmp;
frame = tmp;
}
}
static VALUE
bind_clone(self)
VALUE self;
{
struct BLOCK *orig, *data;
VALUE bind;
Data_Get_Struct(self, struct BLOCK, orig);
bind = Data_Make_Struct(rb_cBinding,struct BLOCK,blk_mark,blk_free,data);
CLONESETUP(bind, self);
MEMCPY(data, orig, struct BLOCK, 1);
frame_dup(&data->frame);
if (data->iter) {
blk_copy_prev(data);
}
else {
data->prev = 0;
}
return bind;
}
static VALUE
rb_f_binding(self)
VALUE self;
{
struct BLOCK *data, *p;
struct RVarmap *vars;
VALUE bind;
PUSH_BLOCK(0,0);
bind = Data_Make_Struct(rb_cBinding,struct BLOCK,blk_mark,blk_free,data);
*data = *ruby_block;
data->orig_thread = rb_thread_current();
data->wrapper = ruby_wrapper;
data->iter = rb_f_block_given_p();
frame_dup(&data->frame);
if (ruby_frame->prev) {
data->frame.last_func = ruby_frame->prev->last_func;
data->frame.last_class = ruby_frame->prev->last_class;
}
if (data->iter) {
blk_copy_prev(data);
}
else {
data->prev = 0;
}
data->flags |= BLOCK_DYNAMIC;
data->tag->flags |= BLOCK_DYNAMIC;
for (p = data; p; p = p->prev) {
for (vars = p->dyna_vars; vars; vars = vars->next) {
if (FL_TEST(vars, DVAR_DONT_RECYCLE)) break;
FL_SET(vars, DVAR_DONT_RECYCLE);
}
}
scope_dup(data->scope);
POP_BLOCK();
return bind;
}
#define PROC_T3 FL_USER1
#define PROC_T4 FL_USER2
#define PROC_TMAX (FL_USER1|FL_USER2)
#define PROC_TMASK (FL_USER1|FL_USER2)
static void
proc_save_safe_level(data)
VALUE data;
{
if (OBJ_TAINTED(data)) {
switch (rb_safe_level()) {
case 3:
FL_SET(data, PROC_T3);
break;
case 4:
FL_SET(data, PROC_T4);
break;
default:
if (rb_safe_level() > 4) {
FL_SET(data, PROC_TMAX);
}
break;
}
}
}
static int
proc_get_safe_level(data)
VALUE data;
{
if (OBJ_TAINTED(data)) {
switch (RBASIC(data)->flags & PROC_TMASK) {
case PROC_T3:
return 3;
case PROC_T4:
return 4;
case PROC_TMAX:
return 5;
}
return 3;
}
return 0;
}
static void
proc_set_safe_level(data)
VALUE data;
{
if (OBJ_TAINTED(data)) {
ruby_safe_level = proc_get_safe_level(data);
}
}
static VALUE
proc_new(klass)
VALUE klass;
{
volatile VALUE proc;
struct BLOCK *data, *p;
struct RVarmap *vars;
if (!rb_block_given_p() && !rb_f_block_given_p()) {
rb_raise(rb_eArgError, "tried to create Proc object without a block");
}
proc = Data_Make_Struct(klass, struct BLOCK, blk_mark, blk_free, data);
*data = *ruby_block;
data->orig_thread = rb_thread_current();
data->wrapper = ruby_wrapper;
data->iter = data->prev?Qtrue:Qfalse;
frame_dup(&data->frame);
if (data->iter) {
blk_copy_prev(data);
}
else {
data->prev = 0;
}
data->flags |= BLOCK_DYNAMIC;
data->tag->flags |= BLOCK_DYNAMIC;
for (p = data; p; p = p->prev) {
for (vars = p->dyna_vars; vars; vars = vars->next) {
if (FL_TEST(vars, DVAR_DONT_RECYCLE)) break;
FL_SET(vars, DVAR_DONT_RECYCLE);
}
}
scope_dup(data->scope);
proc_save_safe_level(proc);
return proc;
}
static VALUE
proc_s_new(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
{
VALUE proc = proc_new(klass);
rb_obj_call_init(proc, argc, argv);
return proc;
}
VALUE
rb_f_lambda()
{
return proc_new(rb_cProc);
}
static int
blk_orphan(data)
struct BLOCK *data;
{
if (!(data->scope->flags & SCOPE_NOSTACK)) {
return 0;
}
if ((data->tag->flags & BLOCK_ORPHAN)) {
return 1;
}
if (data->orig_thread != rb_thread_current()) {
return 1;
}
return 0;
}
static VALUE
proc_invoke(proc, args, pcall, self)
VALUE proc, args; /* OK */
int pcall;
VALUE self;
{
struct BLOCK * volatile old_block;
struct BLOCK _block;
struct BLOCK *data;
volatile VALUE result = Qnil;
int state;
volatile int orphan;
volatile int safe = ruby_safe_level;
volatile VALUE old_wrapper = ruby_wrapper;
struct RVarmap * volatile old_dvars = ruby_dyna_vars;
if (rb_block_given_p() && ruby_frame->last_func) {
rb_warning("block for %s#%s is useless",
rb_class2name(CLASS_OF(proc)),
rb_id2name(ruby_frame->last_func));
}
Data_Get_Struct(proc, struct BLOCK, data);
orphan = blk_orphan(data);
ruby_wrapper = data->wrapper;
ruby_dyna_vars = data->dyna_vars;
/* PUSH BLOCK from data */
old_block = ruby_block;
_block = *data;
ruby_block = &_block;
PUSH_ITER(ITER_CUR);
ruby_frame->iter = ITER_CUR;
if (!pcall) {
args = avalue_to_svalue(args);
}
PUSH_TAG(PROT_NONE);
state = EXEC_TAG();
if (state == 0) {
proc_set_safe_level(proc);
result = rb_yield_0(args, self, self!=Qundef?CLASS_OF(self):0, pcall);
}
POP_TAG();
POP_ITER();
if (ruby_block->tag->dst == state) {
state &= TAG_MASK;
}
ruby_block = old_block;
ruby_wrapper = old_wrapper;
ruby_dyna_vars = old_dvars;
ruby_safe_level = safe;
switch (state) {
case 0:
break;
case TAG_BREAK:
if (!pcall && orphan) {
localjump_error("break from proc-closure", prot_tag->retval);
}
result = prot_tag->retval;
break;
case TAG_RETRY:
localjump_error("retry from proc-closure", Qnil);
break;
case TAG_RETURN:
if (orphan) { /* orphan procedure */
localjump_error("return from proc-closure", prot_tag->retval);
}
/* fall through */
default:
JUMP_TAG(state);
}
return result;
}
static VALUE
proc_call(proc, args)
VALUE proc, args; /* OK */
{
return proc_invoke(proc, args, Qtrue, Qundef);
}
static VALUE
proc_yield(proc, args)
VALUE proc, args; /* OK */
{
return proc_invoke(proc, args, Qfalse, Qundef);
}
static VALUE
proc_arity(proc)
VALUE proc;
{
struct BLOCK *data;
NODE *list;
int n;
Data_Get_Struct(proc, struct BLOCK, data);
if (data->var == 0) return INT2FIX(-1);
if (data->var == (NODE*)1) return INT2FIX(0);
if (data->var == (NODE*)2) return INT2FIX(0);
switch (nd_type(data->var)) {
default:
return INT2FIX(-1);
case NODE_MASGN:
list = data->var->nd_head;
n = 0;
while (list) {
n++;
list = list->nd_next;
}
if (data->var->nd_args) return INT2FIX(-n-1);
return INT2FIX(n);
}
}
static VALUE
proc_eq(self, other)
VALUE self, other;
{
struct BLOCK *data, *data2;
if (self == other) return Qtrue;
if (TYPE(other) != T_DATA) return Qfalse;
if (RDATA(other)->dmark != (RUBY_DATA_FUNC)blk_mark) return Qfalse;
if (CLASS_OF(self) != CLASS_OF(other)) return Qfalse;
Data_Get_Struct(self, struct BLOCK, data);
Data_Get_Struct(other, struct BLOCK, data2);
if (data->tag == data2->tag) return Qtrue;
return Qfalse;
}
static VALUE
proc_to_s(self, other)
VALUE self, other;
{
struct BLOCK *data;
char *cname = rb_class2name(CLASS_OF(self));
VALUE str;
Data_Get_Struct(self, struct BLOCK, data);
str = rb_str_new(0, strlen(cname)+6+16+1); /* 6:tags 16:addr 1:nul */
sprintf(RSTRING(str)->ptr, "#<%s:0x%lx>", cname, data->tag);
RSTRING(str)->len = strlen(RSTRING(str)->ptr);
if (OBJ_TAINTED(self)) OBJ_TAINT(str);
return str;
}
static VALUE
block_pass(self, node)
VALUE self;
NODE *node;
{
VALUE block = rb_eval(self, node->nd_body); /* OK */
struct BLOCK * volatile old_block;
struct BLOCK _block;
struct BLOCK *data;
volatile VALUE result = Qnil;
int state;
volatile int orphan;
volatile int safe = ruby_safe_level;
if (NIL_P(block)) {
PUSH_ITER(ITER_NOT);
result = rb_eval(self, node->nd_iter);
POP_ITER();
return result;
}
if (rb_obj_is_kind_of(block, rb_cMethod)) {
block = method_proc(block);
}
else if (!rb_obj_is_proc(block)) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected Proc)",
rb_class2name(CLASS_OF(block)));
}
if (rb_safe_level() >= 1 && OBJ_TAINTED(block)) {
if (rb_safe_level() > proc_get_safe_level(block)) {
rb_raise(rb_eSecurityError, "Insecure: tainted block value");
}
}
Data_Get_Struct(block, struct BLOCK, data);
orphan = blk_orphan(data);
retry:
/* PUSH BLOCK from data */
old_block = ruby_block;
_block = *data;
ruby_block = &_block;
PUSH_ITER(ITER_PRE);
ruby_frame->iter = ITER_PRE;
PUSH_TAG(PROT_NONE);
state = EXEC_TAG();
if (state == 0) {
proc_set_safe_level(block);
if (safe > ruby_safe_level)
ruby_safe_level = safe;
result = rb_eval(self, node->nd_iter);
}
POP_TAG();
POP_ITER();
if (_block.tag->dst == state) {
if (orphan) {
state &= TAG_MASK;
}
else {
struct BLOCK *ptr = old_block;
while (ptr) {
if (ptr->scope == _block.scope) {
ptr->tag->dst = state;
break;
}
ptr = ptr->prev;
}
if (!ptr) {
state &= TAG_MASK;
}
}
}
ruby_block = old_block;
ruby_safe_level = safe;
switch (state) {/* escape from orphan procedure */
case 0:
break;
case TAG_BREAK:
result = prot_tag->retval;
break;
case TAG_RETRY:
goto retry;
case TAG_RETURN:
if (orphan) {
localjump_error("return from proc-closure", prot_tag->retval);
}
default:
JUMP_TAG(state);
}
return result;
}
struct METHOD {
VALUE klass, rklass;
VALUE recv;
ID id, oid;
NODE *body;
};
static void
bm_mark(data)
struct METHOD *data;
{
rb_gc_mark(data->rklass);
rb_gc_mark(data->klass);
rb_gc_mark(data->recv);
rb_gc_mark((VALUE)data->body);
}
static VALUE
mnew(klass, obj, id, mklass)
VALUE klass, obj, mklass;
ID id;
{
VALUE method;
NODE *body;
int noex;
struct METHOD *data;
VALUE rklass = klass;
ID oid = id;
again:
if ((body = rb_get_method_body(&klass, &id, &noex)) == 0) {
print_undef(rklass, oid);
}
if (nd_type(body) == NODE_ZSUPER) {
klass = RCLASS(klass)->super;
goto again;
}
method = Data_Make_Struct(mklass, struct METHOD, bm_mark, free, data);
data->klass = klass;
data->recv = obj;
data->id = id;
data->body = body;
data->rklass = rklass;
data->oid = oid;
OBJ_INFECT(method, klass);
return method;
}
static VALUE
method_eq(method, other)
VALUE method, other;
{
struct METHOD *m1, *m2;
if (TYPE(other) != T_DATA || RDATA(other)->dmark != (RUBY_DATA_FUNC)bm_mark)
return Qfalse;
if (CLASS_OF(method) != CLASS_OF(other))
return Qfalse;
Data_Get_Struct(method, struct METHOD, m1);
Data_Get_Struct(other, struct METHOD, m2);
if (m1->klass != m2->klass || m1->rklass != m2->rklass ||
m1->recv != m2->recv || m1->body != m2->body)
return Qfalse;
return Qtrue;
}
static VALUE
method_unbind(obj)
VALUE obj;
{
VALUE method;
struct METHOD *orig, *data;
Data_Get_Struct(obj, struct METHOD, orig);
method = Data_Make_Struct(rb_cUnboundMethod, struct METHOD, bm_mark, free, data);
data->klass = orig->klass;
data->recv = Qundef;
data->id = orig->id;
data->body = orig->body;
data->rklass = orig->rklass;
data->oid = orig->oid;
OBJ_INFECT(method, obj);
return method;
}
static VALUE
umethod_unbind(obj)
VALUE obj;
{
return obj;
}
static VALUE
rb_obj_method(obj, vid)
VALUE obj;
VALUE vid;
{
return mnew(CLASS_OF(obj), obj, rb_to_id(vid), rb_cMethod);
}
static VALUE
rb_mod_method(mod, vid)
VALUE mod;
VALUE vid;
{
return mnew(mod, Qundef, rb_to_id(vid), rb_cUnboundMethod);
}
static VALUE
method_clone(self)
VALUE self;
{
VALUE clone;
struct METHOD *orig, *data;
Data_Get_Struct(self, struct METHOD, orig);
clone = Data_Make_Struct(CLASS_OF(self),struct METHOD, bm_mark, free, data);
CLONESETUP(clone, self);
*data = *orig;
return clone;
}
static VALUE
method_call(argc, argv, method)
int argc;
VALUE *argv;
VALUE method;
{
VALUE result;
struct METHOD *data;
int state;
volatile int safe = ruby_safe_level;
Data_Get_Struct(method, struct METHOD, data);
PUSH_ITER(rb_block_given_p()?ITER_PRE:ITER_NOT);
PUSH_TAG(PROT_NONE);
if (OBJ_TAINTED(method) && ruby_safe_level < 4) {
ruby_safe_level = 4;
}
if ((state = EXEC_TAG()) == 0) {
result = rb_call0(data->klass,data->recv,data->id,argc,argv,data->body,0);
}
POP_TAG();
POP_ITER();
ruby_safe_level = safe;
if (state) JUMP_TAG(state);
return result;
}
static VALUE
umethod_call(argc, argv, method)
int argc;
VALUE *argv;
VALUE method;
{
rb_raise(rb_eTypeError, "you cannot call unbound method; bind first");
return Qnil; /* not reached */
}
static VALUE
umethod_bind(method, recv)
VALUE method, recv;
{
struct METHOD *data, *bound;
Data_Get_Struct(method, struct METHOD, data);
if (data->rklass != CLASS_OF(recv)) {
if (FL_TEST(data->rklass, FL_SINGLETON)) {
rb_raise(rb_eTypeError, "singleton method called for a different object");
}
if (FL_TEST(CLASS_OF(recv), FL_SINGLETON) &&
st_lookup(RCLASS(CLASS_OF(recv))->m_tbl, data->oid, 0)) {
rb_raise(rb_eTypeError, "method `%s' overridden", rb_id2name(data->oid));
}
if (!((TYPE(data->rklass) == T_MODULE) ?
rb_obj_is_kind_of(recv, data->rklass) :
rb_obj_is_instance_of(recv, data->rklass))) {
rb_raise(rb_eTypeError, "bind argument must be an instance of %s",
rb_class2name(data->rklass));
}
}
method = Data_Make_Struct(rb_cMethod,struct METHOD,bm_mark,free,bound);
*bound = *data;
bound->recv = recv;
bound->rklass = CLASS_OF(recv);
return method;
}
static VALUE
method_arity(method)
VALUE method;
{
struct METHOD *data;
NODE *body;
int n;
Data_Get_Struct(method, struct METHOD, data);
body = data->body;
switch (nd_type(body)) {
case NODE_CFUNC:
if (body->nd_argc < 0) return INT2FIX(-1);
return INT2FIX(body->nd_argc);
case NODE_ZSUPER:
return INT2FIX(-1);
case NODE_ATTRSET:
return INT2FIX(1);
case NODE_IVAR:
return INT2FIX(0);
default:
body = body->nd_next; /* skip NODE_SCOPE */
if (nd_type(body) == NODE_BLOCK)
body = body->nd_head;
if (!body) return INT2FIX(0);
n = body->nd_cnt;
if (body->nd_opt || body->nd_rest != -1)
n = -n-1;
return INT2FIX(n);
}
}
static VALUE
method_inspect(method)
VALUE method;
{
struct METHOD *data;
VALUE str;
const char *s;
char *sharp = "#";
Data_Get_Struct(method, struct METHOD, data);
str = rb_str_buf_new2("#<");
s = rb_class2name(CLASS_OF(method));
rb_str_buf_cat2(str, s);
rb_str_buf_cat2(str, ": ");
if (FL_TEST(data->klass, FL_SINGLETON)) {
VALUE v = rb_iv_get(data->klass, "__attached__");
if (data->recv == Qundef) {
rb_str_buf_append(str, rb_inspect(data->klass));
}
else if (data->recv == v) {
rb_str_buf_append(str, rb_inspect(v));
sharp = ".";
}
else {
rb_str_buf_append(str, rb_inspect(data->recv));
rb_str_buf_cat2(str, "(");
rb_str_buf_append(str, rb_inspect(v));
rb_str_buf_cat2(str, ")");
sharp = ".";
}
}
else {
rb_str_buf_cat2(str, rb_class2name(data->rklass));
if (data->rklass != data->klass) {
rb_str_buf_cat2(str, "(");
rb_str_buf_cat2(str, rb_class2name(data->klass));
rb_str_buf_cat2(str, ")");
}
}
rb_str_buf_cat2(str, sharp);
rb_str_buf_cat2(str, rb_id2name(data->oid));
rb_str_buf_cat2(str, ">");
return str;
}
static VALUE
mproc()
{
VALUE proc;
/* emulate ruby's method call */
PUSH_ITER(ITER_CUR);
PUSH_FRAME();
proc = rb_f_lambda();
POP_FRAME();
POP_ITER();
return proc;
}
static VALUE
bmcall(args, method)
VALUE args, method;
{
args = svalue_to_avalue(args);
return method_call(RARRAY(args)->len, RARRAY(args)->ptr, method);
}
static VALUE
umcall(args, method)
VALUE args, method;
{
return umethod_call(0, 0, method);
}
static VALUE
method_proc(method)
VALUE method;
{
return rb_iterate((VALUE(*)_((VALUE)))mproc, 0, bmcall, method);
}
static VALUE
umethod_proc(method)
VALUE method;
{
return rb_iterate((VALUE(*)_((VALUE)))mproc, 0, umcall, method);
}
static VALUE
rb_mod_define_method(argc, argv, mod)
int argc;
VALUE *argv;
VALUE mod;
{
ID id;
VALUE body;
NODE *node;
int noex;
if (argc == 1) {
id = rb_to_id(argv[0]);
body = rb_f_lambda();
}
else if (argc == 2) {
id = rb_to_id(argv[0]);
body = argv[1];
if (!rb_obj_is_kind_of(body, rb_cMethod) && !rb_obj_is_proc(body)) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected Proc/Method)",
rb_class2name(CLASS_OF(body)));
}
}
else {
rb_raise(rb_eArgError, "wrong number of arguments(%d for 1)", argc);
}
if (RDATA(body)->dmark == (RUBY_DATA_FUNC)bm_mark) {
node = NEW_DMETHOD(method_unbind(body));
}
else if (RDATA(body)->dmark == (RUBY_DATA_FUNC)blk_mark) {
node = NEW_BMETHOD(body);
}
else {
/* type error */
rb_raise(rb_eTypeError, "wrong argument type (expected Proc/Method)");
}
if (SCOPE_TEST(SCOPE_PRIVATE)) {
noex = NOEX_PRIVATE;
}
else if (SCOPE_TEST(SCOPE_PROTECTED)) {
noex = NOEX_PROTECTED;
}
else {
noex = NOEX_PUBLIC;
}
rb_add_method(mod, id, node, noex);
if (scope_vmode == SCOPE_MODFUNC) {
rb_add_method(rb_singleton_class(mod), id, node, NOEX_PUBLIC);
rb_funcall(mod, singleton_added, 1, ID2SYM(id));
}
if (FL_TEST(mod, FL_SINGLETON)) {
rb_funcall(rb_iv_get(mod, "__attached__"), singleton_added, 1, ID2SYM(id));
}
else {
rb_funcall(mod, added, 1, ID2SYM(id));
}
return body;
}
void
Init_Proc()
{
rb_eLocalJumpError = rb_define_class("LocalJumpError", rb_eStandardError);
rb_define_method(rb_eLocalJumpError, "exitstatus", localjump_exitstatus, 0);
rb_eSysStackError = rb_define_class("SystemStackError", rb_eStandardError);
rb_cProc = rb_define_class("Proc", rb_cObject);
rb_undef_method(CLASS_OF(rb_cProc), "allocate");
rb_define_singleton_method(rb_cProc, "new", proc_s_new, -1);
rb_define_method(rb_cProc, "call", proc_call, -2);
rb_define_method(rb_cProc, "yield", proc_yield, -2);
rb_define_method(rb_cProc, "arity", proc_arity, 0);
rb_define_method(rb_cProc, "[]", proc_call, -2);
rb_define_method(rb_cProc, "==", proc_eq, 1);
rb_define_method(rb_cProc, "to_s", proc_to_s, 0);
rb_define_global_function("proc", rb_f_lambda, 0);
rb_define_global_function("lambda", rb_f_lambda, 0);
rb_define_global_function("binding", rb_f_binding, 0);
rb_cBinding = rb_define_class("Binding", rb_cObject);
rb_undef_method(CLASS_OF(rb_cBinding), "allocate");
rb_undef_method(CLASS_OF(rb_cBinding), "new");
rb_define_method(rb_cBinding, "clone", bind_clone, 0);
rb_cMethod = rb_define_class("Method", rb_cObject);
rb_undef_method(CLASS_OF(rb_cMethod), "allocate");
rb_undef_method(CLASS_OF(rb_cMethod), "new");
rb_define_method(rb_cMethod, "==", method_eq, 1);
rb_define_method(rb_cMethod, "clone", method_clone, 0);
rb_define_method(rb_cMethod, "call", method_call, -1);
rb_define_method(rb_cMethod, "[]", method_call, -1);
rb_define_method(rb_cMethod, "arity", method_arity, 0);
rb_define_method(rb_cMethod, "inspect", method_inspect, 0);
rb_define_method(rb_cMethod, "to_s", method_inspect, 0);
rb_define_method(rb_cMethod, "to_proc", method_proc, 0);
rb_define_method(rb_cMethod, "unbind", method_unbind, 0);
rb_define_method(rb_mKernel, "method", rb_obj_method, 1);
rb_cUnboundMethod = rb_define_class("UnboundMethod", rb_cMethod);
rb_define_method(rb_cUnboundMethod, "call", umethod_call, -1);
rb_define_method(rb_cUnboundMethod, "[]", umethod_call, -1);
rb_define_method(rb_cUnboundMethod, "to_proc", umethod_proc, 0);
rb_define_method(rb_cUnboundMethod, "bind", umethod_bind, 1);
rb_define_method(rb_cUnboundMethod, "unbind", umethod_unbind, 0);
rb_define_method(rb_cModule, "instance_method", rb_mod_method, 1);
}
static VALUE rb_eThreadError;
int rb_thread_pending = 0;
VALUE rb_cThread;
extern VALUE rb_last_status;
enum thread_status {
THREAD_TO_KILL,
THREAD_RUNNABLE,
THREAD_STOPPED,
THREAD_KILLED
};
#define WAIT_FD (1<<0)
#define WAIT_SELECT (1<<1)
#define WAIT_TIME (1<<2)
#define WAIT_JOIN (1<<3)
#define WAIT_PID (1<<4)
/* +infty, for this purpose */
#define DELAY_INFTY 1E30
/* typedef struct thread * rb_thread_t; */
struct thread {
struct thread *next, *prev;
jmp_buf context;
VALUE result;
int stk_len;
int stk_max;
VALUE*stk_ptr;
VALUE*stk_pos;
struct FRAME *frame;
struct SCOPE *scope;
struct RVarmap *dyna_vars;
struct BLOCK *block;
struct iter *iter;
struct tag *tag;
VALUE klass;
VALUE wrapper;
NODE *cref;
int flags; /* misc. states (vmode/rb_trap_immediate/raised) */
char *file;
int line;
int tracing;
VALUE errinfo;
VALUE last_status;
VALUE last_line;
VALUE last_match;
int safe;
enum thread_status status;
int wait_for;
int fd;
fd_set readfds;
fd_set writefds;
fd_set exceptfds;
int select_value;
double delay;
rb_thread_t join;
int abort;
int priority;
int gid;
st_table *locals;
VALUE thread;
};
#define THREAD_RAISED 0x200 /* temporary flag */
#define THREAD_TERMINATING 0x400 /* persistent flag */
#define THREAD_FLAGS_MASK 0x400 /* mask for persistent flags */
#define FOREACH_THREAD_FROM(f,x) x = f; do { x = x->next;
#define END_FOREACH_FROM(f,x) } while (x != f)
#define FOREACH_THREAD(x) FOREACH_THREAD_FROM(curr_thread,x)
#define END_FOREACH(x) END_FOREACH_FROM(curr_thread,x)
static const char *
thread_status_name(status)
enum thread_status status;
{
switch (status) {
case THREAD_RUNNABLE:
return "run";
case THREAD_STOPPED:
return "sleep";
case THREAD_TO_KILL:
return "aborting";
case THREAD_KILLED:
return "dead";
default:
return "unknown";
}
}
/* $SAFE accessor */
void
rb_set_safe_level(level)
int level;
{
if (level > ruby_safe_level) {
ruby_safe_level = level;
curr_thread->safe = level;
}
}
static VALUE
safe_getter()
{
return INT2NUM(ruby_safe_level);
}
static void
safe_setter(val)
VALUE val;
{
int level = NUM2INT(val);
if (level < ruby_safe_level) {
rb_raise(rb_eSecurityError, "tried to downgrade safe level from %d to %d",
ruby_safe_level, level);
}
ruby_safe_level = level;
curr_thread->safe = level;
}
/* Return the current time as a floating-point number */
static double
timeofday()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)tv.tv_usec * 1e-6;
}
#define STACK(addr) (th->stk_pos<(VALUE*)(addr) && (VALUE*)(addr)<th->stk_pos+th->stk_len)
#define ADJ(addr) (void*)(STACK(addr)?(((VALUE*)(addr)-th->stk_pos)+th->stk_ptr):(VALUE*)(addr))
static void
thread_mark(th)
rb_thread_t th;
{
struct FRAME *frame;
struct BLOCK *block;
rb_gc_mark(th->result);
rb_gc_mark(th->thread);
if (th->join) rb_gc_mark(th->join->thread);
rb_gc_mark(th->klass);
rb_gc_mark(th->wrapper);
rb_gc_mark((VALUE)th->cref);
rb_gc_mark((VALUE)th->scope);
rb_gc_mark((VALUE)th->dyna_vars);
rb_gc_mark(th->errinfo);
rb_gc_mark(th->last_line);
rb_gc_mark(th->last_match);
rb_mark_tbl(th->locals);
/* mark data in copied stack */
if (th == curr_thread) return;
if (th->status == THREAD_KILLED) return;
if (th->stk_len == 0) return; /* stack not active, no need to mark. */
if (th->stk_ptr) {
rb_gc_mark_locations(th->stk_ptr, th->stk_ptr+th->stk_len);
#if defined(THINK_C) || defined(__human68k__)
rb_gc_mark_locations(th->stk_ptr+2, th->stk_ptr+th->stk_len+2);
#endif
}
frame = th->frame;
while (frame && frame != top_frame) {
frame = ADJ(frame);
rb_gc_mark_frame(frame);
if (frame->tmp) {
struct FRAME *tmp = frame->tmp;
while (tmp && tmp != top_frame) {
tmp = ADJ(tmp);
rb_gc_mark_frame(tmp);
tmp = tmp->prev;
}
}
frame = frame->prev;
}
block = th->block;
while (block) {
block = ADJ(block);
rb_gc_mark_frame(&block->frame);
block = block->prev;
}
}
void
rb_gc_mark_threads()
{
rb_thread_t th;
/* static global mark */
rb_gc_mark((VALUE)ruby_cref);
if (!curr_thread) return;
FOREACH_THREAD(th) {
rb_gc_mark(th->thread);
} END_FOREACH(th);
}
static void
thread_free(th)
rb_thread_t th;
{
if (th->stk_ptr) free(th->stk_ptr);
th->stk_ptr = 0;
if (th->locals) st_free_table(th->locals);
if (th->status != THREAD_KILLED) {
if (th->prev) th->prev->next = th->next;
if (th->next) th->next->prev = th->prev;
}
if (th != main_thread) free(th);
}
static rb_thread_t
rb_thread_check(data)
VALUE data;
{
if (TYPE(data) != T_DATA || RDATA(data)->dmark != (RUBY_DATA_FUNC)thread_mark) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected Thread)",
rb_class2name(CLASS_OF(data)));
}
return (rb_thread_t)RDATA(data)->data;
}
static VALUE rb_thread_raise _((int, VALUE*, rb_thread_t));
static int th_raise_argc;
static VALUE th_raise_argv[2];
static char *th_raise_file;
static int th_raise_line;
static VALUE th_cmd;
static int th_sig;
static char *th_signm;
#define RESTORE_NORMAL 1
#define RESTORE_FATAL 2
#define RESTORE_INTERRUPT 3
#define RESTORE_TRAP 4
#define RESTORE_RAISE 5
#define RESTORE_SIGNAL 6
extern VALUE *rb_gc_stack_start;
static void
rb_thread_save_context(th)
rb_thread_t th;
{
VALUE *pos;
int len;
static VALUE tval;
len = ruby_stack_length(&pos);
th->stk_len = 0;
th->stk_pos = (rb_gc_stack_start<pos)?rb_gc_stack_start
:rb_gc_stack_start - len;
if (len > th->stk_max) {
REALLOC_N(th->stk_ptr, VALUE, len);
th->stk_max = len;
}
th->stk_len = len;
FLUSH_REGISTER_WINDOWS;
MEMCPY(th->stk_ptr, th->stk_pos, VALUE, th->stk_len);
th->frame = ruby_frame;
th->scope = ruby_scope;
th->klass = ruby_class;
th->wrapper = ruby_wrapper;
th->cref = ruby_cref;
th->dyna_vars = ruby_dyna_vars;
th->block = ruby_block;
th->flags &= THREAD_FLAGS_MASK;
th->flags |= (rb_trap_immediate<<8) | scope_vmode;
th->iter = ruby_iter;
th->tag = prot_tag;
th->tracing = tracing;
th->errinfo = ruby_errinfo;
th->last_status = rb_last_status;
tval = rb_lastline_get();
rb_lastline_set(th->last_line);
th->last_line = tval;
tval = rb_backref_get();
rb_backref_set(th->last_match);
th->last_match = tval;
th->safe = ruby_safe_level;
th->file = ruby_sourcefile;
th->line = ruby_sourceline;
}
static int
thread_switch(n)
int n;
{
switch (n) {
case 0:
return 0;
case RESTORE_FATAL:
JUMP_TAG(TAG_FATAL);
break;
case RESTORE_INTERRUPT:
rb_interrupt();
break;
case RESTORE_TRAP:
rb_trap_eval(th_cmd, th_sig);
errno = EINTR;
break;
case RESTORE_RAISE:
ruby_frame->last_func = 0;
ruby_sourcefile = th_raise_file;
ruby_sourceline = th_raise_line;
rb_f_raise(th_raise_argc, th_raise_argv);
break;
case RESTORE_SIGNAL:
rb_raise(rb_eSignal, "SIG%s", th_signm);
break;
case RESTORE_NORMAL:
default:
break;
}
return 1;
}
#define THREAD_SAVE_CONTEXT(th) \
(rb_thread_save_context(th),thread_switch(setjmp((th)->context)))
static void rb_thread_restore_context _((rb_thread_t,int));
static void
stack_extend(th, exit)
rb_thread_t th;
int exit;
{
VALUE space[1024];
memset(space, 0, 1); /* prevent array from optimization */
rb_thread_restore_context(th, exit);
}
static void
rb_thread_restore_context(th, exit)
rb_thread_t th;
int exit;
{
VALUE v;
static rb_thread_t tmp;
static int ex;
static VALUE tval;
if (!th->stk_ptr) rb_bug("unsaved context");
if (&v < rb_gc_stack_start) {
/* Stack grows downward */
if (&v > th->stk_pos) stack_extend(th, exit);
}
else {
/* Stack grows upward */
if (&v < th->stk_pos + th->stk_len) stack_extend(th, exit);
}
ruby_frame = th->frame;
ruby_scope = th->scope;
ruby_class = th->klass;
ruby_wrapper = th->wrapper;
ruby_cref = th->cref;
ruby_dyna_vars = th->dyna_vars;
ruby_block = th->block;
scope_vmode = th->flags&SCOPE_MASK;
rb_trap_immediate = (th->flags&0x100)?1:0;
ruby_iter = th->iter;
prot_tag = th->tag;
tracing = th->tracing;
ruby_errinfo = th->errinfo;
rb_last_status = th->last_status;
ruby_safe_level = th->safe;
ruby_sourcefile = th->file;
ruby_sourceline = th->line;
tmp = th;
ex = exit;
FLUSH_REGISTER_WINDOWS;
MEMCPY(tmp->stk_pos, tmp->stk_ptr, VALUE, tmp->stk_len);
tval = rb_lastline_get();
rb_lastline_set(tmp->last_line);
tmp->last_line = tval;
tval = rb_backref_get();
rb_backref_set(tmp->last_match);
tmp->last_match = tval;
longjmp(tmp->context, ex);
}
static void
rb_thread_ready(th)
rb_thread_t th;
{
th->wait_for = 0;
th->status = THREAD_RUNNABLE;
}
static void
rb_thread_remove(th)
rb_thread_t th;
{
if (th->status == THREAD_KILLED) return;
rb_thread_ready(th);
th->status = THREAD_KILLED;
th->prev->next = th->next;
th->next->prev = th->prev;
}
static int
rb_thread_dead(th)
rb_thread_t th;
{
return th->status == THREAD_KILLED;
}
void
rb_thread_fd_close(fd)
int fd;
{
rb_thread_t th;
FOREACH_THREAD(th) {
if ((th->wait_for & WAIT_FD) && fd == th->fd) {
VALUE exc = rb_exc_new2(rb_eIOError, "stream closed");
rb_thread_raise(1, &exc, th);
}
}
END_FOREACH(th);
}
static void
rb_thread_deadlock()
{
if (curr_thread == main_thread) {
rb_raise(rb_eFatal, "Thread: deadlock");
}
curr_thread = main_thread;
th_raise_argc = 1;
th_raise_argv[0] = rb_exc_new2(rb_eFatal, "Thread: deadlock");
th_raise_file = ruby_sourcefile;
th_raise_line = ruby_sourceline;
rb_thread_restore_context(main_thread, RESTORE_RAISE);
}
static void
copy_fds(dst, src, max)
fd_set *dst, *src;
int max;
{
int n = 0;
int i;
for (i=0; i<=max; i++) {
if (FD_ISSET(i, src)) {
n = i;
FD_SET(i, dst);
}
}
}
static int
match_fds(dst, src, max)
fd_set *dst, *src;
int max;
{
int i;
for (i=0; i<=max; i++) {
if (FD_ISSET(i, src) && FD_ISSET(i, dst)) {
return Qtrue;
}
}
return Qfalse;
}
static int
intersect_fds(src, dst, max)
fd_set *src, *dst;
int max;
{
int i, n = 0;
for (i=0; i<=max; i++) {
if (FD_ISSET(i, dst)) {
if (FD_ISSET(i, src)) {
/* Wake up only one thread per fd. */
FD_CLR(i, src);
++n;
}
else {
FD_CLR(i, dst);
}
}
}
return n;
}
static int
find_bad_fds(dst, src, max)
fd_set *dst, *src;
int max;
{
int i, test = Qfalse;
for (i=0; i<=max; i++) {
if (FD_ISSET(i, src) && !FD_ISSET(i, dst)) {
FD_CLR(i, src);
test = Qtrue;
}
}
return test;
}
void
rb_thread_schedule()
{
rb_thread_t next; /* OK */
rb_thread_t th;
rb_thread_t curr;
int found = 0;
fd_set readfds;
fd_set writefds;
fd_set exceptfds;
struct timeval delay_tv, *delay_ptr;
double delay, now; /* OK */
int n, max;
int need_select = 0;
int select_timeout = 0;
rb_thread_pending = 0;
if (curr_thread == curr_thread->next
&& curr_thread->status == THREAD_RUNNABLE)
return;
next = 0;
curr = curr_thread; /* starting thread */
while (curr->status == THREAD_KILLED) {
curr = curr->prev;
}
again:
max = -1;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
delay = DELAY_INFTY;
now = -1.0;
FOREACH_THREAD_FROM(curr, th) {
if (!found && th->status <= THREAD_RUNNABLE) {
found = 1;
}
if (th->status != THREAD_STOPPED) continue;
if (th->wait_for & WAIT_JOIN) {
if (rb_thread_dead(th->join)) {
th->status = THREAD_RUNNABLE;
found = 1;
}
}
if (th->wait_for & WAIT_FD) {
FD_SET(th->fd, &readfds);
if (max < th->fd) max = th->fd;
need_select = 1;
}
if (th->wait_for & WAIT_SELECT) {
copy_fds(&readfds, &th->readfds, th->fd);
copy_fds(&writefds, &th->writefds, th->fd);
copy_fds(&exceptfds, &th->exceptfds, th->fd);
if (max < th->fd) max = th->fd;
need_select = 1;
if (th->wait_for & WAIT_TIME) {
select_timeout = 1;
}
th->select_value = 0;
}
if (th->wait_for & WAIT_TIME) {
double th_delay;
if (now < 0.0) now = timeofday();
th_delay = th->delay - now;
if (th_delay <= 0.0) {
th->status = THREAD_RUNNABLE;
found = 1;
}
else if (th_delay < delay) {
delay = th_delay;
need_select = 1;
}
else if (th->delay == DELAY_INFTY) {
need_select = 1;
}
}
}
END_FOREACH_FROM(curr, th);
/* Do the select if needed */
if (need_select) {
/* Convert delay to a timeval */
/* If a thread is runnable, just poll */
if (found) {
delay_tv.tv_sec = 0;
delay_tv.tv_usec = 0;
delay_ptr = &delay_tv;
}
else if (delay == DELAY_INFTY) {
delay_ptr = 0;
}
else {
delay_tv.tv_sec = delay;
delay_tv.tv_usec = (delay - (double)delay_tv.tv_sec)*1e6;
delay_ptr = &delay_tv;
}
n = select(max+1, &readfds, &writefds, &exceptfds, delay_ptr);
if (n < 0) {
if (rb_trap_pending) rb_trap_exec();
if (errno == EINTR) goto again;
FOREACH_THREAD_FROM(curr, th) {
if (th->wait_for & WAIT_SELECT) {
int v = 0;
v |= find_bad_fds(&readfds, &th->readfds, th->fd);
v |= find_bad_fds(&writefds, &th->writefds, th->fd);
v |= find_bad_fds(&exceptfds, &th->exceptfds, th->fd);
if (v) {
th->select_value = n;
n = max;
}
}
}
END_FOREACH_FROM(curr, th);
}
if (select_timeout && n == 0) {
if (now < 0.0) now = timeofday();
FOREACH_THREAD_FROM(curr, th) {
if (((th->wait_for&(WAIT_SELECT|WAIT_TIME)) == (WAIT_SELECT|WAIT_TIME)) &&
th->delay <= now) {
th->status = THREAD_RUNNABLE;
th->wait_for = 0;
th->select_value = 0;
found = 1;
intersect_fds(&readfds, &th->readfds, max);
intersect_fds(&writefds, &th->writefds, max);
intersect_fds(&exceptfds, &th->exceptfds, max);
}
}
END_FOREACH_FROM(curr, th);
}
if (n > 0) {
now = -1.0;
/* Some descriptors are ready.
Make the corresponding threads runnable. */
FOREACH_THREAD_FROM(curr, th) {
if ((th->wait_for&WAIT_FD) && FD_ISSET(th->fd, &readfds)) {
/* Wake up only one thread per fd. */
FD_CLR(th->fd, &readfds);
th->status = THREAD_RUNNABLE;
th->fd = 0;
th->wait_for = 0;
found = 1;
}
if ((th->wait_for&WAIT_SELECT) &&
(match_fds(&readfds, &th->readfds, max) ||
match_fds(&writefds, &th->writefds, max) ||
match_fds(&exceptfds, &th->exceptfds, max))) {
/* Wake up only one thread per fd. */
th->status = THREAD_RUNNABLE;
th->wait_for = 0;
n = intersect_fds(&readfds, &th->readfds, max) +
intersect_fds(&writefds, &th->writefds, max) +
intersect_fds(&exceptfds, &th->exceptfds, max);
th->select_value = n;
found = 1;
}
}
END_FOREACH_FROM(curr, th);
}
/* The delays for some of the threads should have expired.
Go through the loop once more, to check the delays. */
if (!found && delay != DELAY_INFTY)
goto again;
}
FOREACH_THREAD_FROM(curr, th) {
if (th->status == THREAD_TO_KILL) {
next = th;
break;
}
if (th->status == THREAD_RUNNABLE && th->stk_ptr) {
if (!next || next->priority < th->priority)
next = th;
}
}
END_FOREACH_FROM(curr, th);
if (!next) {
/* raise fatal error to main thread */
curr_thread->file = ruby_sourcefile;
curr_thread->line = ruby_sourceline;
FOREACH_THREAD_FROM(curr, th) {
fprintf(stderr, "deadlock 0x%lx: %d:%d %s - %s:%d\n",
th->thread, th->status,
th->wait_for, th==main_thread ? "(main)" : "",
th->file, th->line);
}
END_FOREACH_FROM(curr, th);
next = main_thread;
next->gid = 0;
rb_thread_ready(next);
next->status = THREAD_TO_KILL;
rb_thread_save_context(curr_thread);
rb_thread_deadlock();
}
next->wait_for = 0;
if (next->status == THREAD_RUNNABLE && next == curr_thread) {
return;
}
/* context switch */
if (curr == curr_thread) {
if (THREAD_SAVE_CONTEXT(curr)) {
return;
}
}
curr_thread = next;
if (next->status == THREAD_TO_KILL) {
if (!(next->flags & THREAD_TERMINATING)) {
next->flags |= THREAD_TERMINATING;
/* terminate; execute ensure-clause if any */
rb_thread_restore_context(next, RESTORE_FATAL);
}
}
rb_thread_restore_context(next, RESTORE_NORMAL);
}
void
rb_thread_wait_fd(fd)
int fd;
{
if (rb_thread_critical) return;
if (curr_thread == curr_thread->next) return;
if (curr_thread->status == THREAD_TO_KILL) return;
curr_thread->status = THREAD_STOPPED;
curr_thread->fd = fd;
curr_thread->wait_for = WAIT_FD;
rb_thread_schedule();
}
int
rb_thread_fd_writable(fd)
int fd;
{
if (rb_thread_critical) return Qtrue;
if (curr_thread == curr_thread->next) return Qtrue;
if (curr_thread->status == THREAD_TO_KILL) return Qtrue;
curr_thread->status = THREAD_STOPPED;
FD_ZERO(&curr_thread->readfds);
FD_ZERO(&curr_thread->writefds);
FD_SET(fd, &curr_thread->writefds);
FD_ZERO(&curr_thread->exceptfds);
curr_thread->fd = fd+1;
curr_thread->wait_for = WAIT_SELECT;
rb_thread_schedule();
return Qfalse;
}
void
rb_thread_wait_for(time)
struct timeval time;
{
double date;
if (rb_thread_critical ||
curr_thread == curr_thread->next ||
curr_thread->status == THREAD_TO_KILL) {
int n;
#ifndef linux
double d, limit;
limit = timeofday()+(double)time.tv_sec+(double)time.tv_usec*1e-6;
#endif
for (;;) {
TRAP_BEG;
n = select(0, 0, 0, 0, &time);
TRAP_END;
if (n == 0) return;
if (n < 0) {
switch (errno) {
case EINTR:
#ifdef ERESTART
case ERESTART:
#endif
return;
default:
rb_sys_fail("sleep");
}
}
#ifndef linux
d = limit - timeofday();
time.tv_sec = (int)d;
time.tv_usec = (int)((d - (int)d)*1e6);
if (time.tv_usec < 0) {
time.tv_usec += (long)1e6;
time.tv_sec -= 1;
}
if (time.tv_sec < 0) return;
#endif
}
}
date = timeofday() + (double)time.tv_sec + (double)time.tv_usec*1e-6;
curr_thread->status = THREAD_STOPPED;
curr_thread->delay = date;
curr_thread->wait_for = WAIT_TIME;
rb_thread_schedule();
}
void rb_thread_sleep_forever _((void));
int
rb_thread_alone()
{
return curr_thread == curr_thread->next;
}
int
rb_thread_select(max, read, write, except, timeout)
int max;
fd_set *read, *write, *except;
struct timeval *timeout;
{
double limit;
int n;
if (!read && !write && !except) {
if (!timeout) {
rb_thread_sleep_forever();
return 0;
}
rb_thread_wait_for(*timeout);
return 0;
}
if (timeout) {
limit = timeofday()+
(double)timeout->tv_sec+(double)timeout->tv_usec*1e-6;
}
if (rb_thread_critical ||
curr_thread == curr_thread->next ||
curr_thread->status == THREAD_TO_KILL) {
#ifndef linux
struct timeval tv, *tvp = timeout;
if (timeout) {
tv = *timeout;
tvp = &tv;
}
for (;;) {
TRAP_BEG;
n = select(max, read, write, except, tvp);
TRAP_END;
if (n < 0) {
switch (errno) {
case EINTR:
#ifdef ERESTART
case ERESTART:
#endif
if (timeout) {
double d = limit - timeofday();
tv.tv_sec = (unsigned int)d;
tv.tv_usec = (long)((d-(double)tv.tv_sec)*1e6);
if (tv.tv_sec < 0) tv.tv_sec = 0;
if (tv.tv_usec < 0) tv.tv_usec = 0;
}
continue;
default:
break;
}
}
return n;
}
#else
for (;;) {
TRAP_BEG;
n = select(max, read, write, except, timeout);
TRAP_END;
if (n < 0) {
switch (errno) {
case EINTR:
#ifdef ERESTART
case ERESTART:
#endif
continue;
}
}
return n;
}
#endif
}
curr_thread->status = THREAD_STOPPED;
if (read) curr_thread->readfds = *read;
else FD_ZERO(&curr_thread->readfds);
if (write) curr_thread->writefds = *write;
else FD_ZERO(&curr_thread->writefds);
if (except) curr_thread->exceptfds = *except;
else FD_ZERO(&curr_thread->exceptfds);
curr_thread->fd = max;
curr_thread->wait_for = WAIT_SELECT;
if (timeout) {
curr_thread->delay = timeofday() +
(double)timeout->tv_sec + (double)timeout->tv_usec*1e-6;
curr_thread->wait_for |= WAIT_TIME;
}
rb_thread_schedule();
if (read) *read = curr_thread->readfds;
if (write) *write = curr_thread->writefds;
if (except) *except = curr_thread->exceptfds;
return curr_thread->select_value;
}
static VALUE
rb_thread_join(thread)
VALUE thread;
{
rb_thread_t th = rb_thread_check(thread);
enum thread_status last_status = THREAD_RUNNABLE;
if (rb_thread_critical) rb_thread_deadlock();
if (!rb_thread_dead(th)) {
if (th == curr_thread)
rb_raise(rb_eThreadError, "thread tried to join itself");
if ((th->wait_for & WAIT_JOIN) && th->join == curr_thread)
rb_raise(rb_eThreadError, "Thread#join: deadlock - mutual join");
if (curr_thread->status == THREAD_TO_KILL)
last_status = THREAD_TO_KILL;
curr_thread->status = THREAD_STOPPED;
curr_thread->join = th;
curr_thread->wait_for = WAIT_JOIN;
rb_thread_schedule();
curr_thread->status = last_status;
}
if (!NIL_P(th->errinfo) && (th->flags & THREAD_RAISED)) {
VALUE oldbt = get_backtrace(th->errinfo);
VALUE errat = make_backtrace();
if (TYPE(oldbt) == T_ARRAY && RARRAY(oldbt)->len > 0) {
rb_ary_unshift(errat, rb_ary_entry(oldbt, 0));
}
set_backtrace(th->errinfo, errat);
rb_exc_raise(th->errinfo);
}
return thread;
}
VALUE
rb_thread_current()
{
return curr_thread->thread;
}
VALUE
rb_thread_main()
{
return main_thread->thread;
}
VALUE
rb_thread_list()
{
rb_thread_t th;
VALUE ary = rb_ary_new();
FOREACH_THREAD(th) {
switch (th->status) {
case THREAD_RUNNABLE:
case THREAD_STOPPED:
rb_ary_push(ary, th->thread);
default:
break;
}
}
END_FOREACH(th);
return ary;
}
VALUE
rb_thread_wakeup(thread)
VALUE thread;
{
rb_thread_t th = rb_thread_check(thread);
if (th->status == THREAD_KILLED)
rb_raise(rb_eThreadError, "killed thread");
rb_thread_ready(th);
return thread;
}
VALUE
rb_thread_run(thread)
VALUE thread;
{
rb_thread_wakeup(thread);
if (!rb_thread_critical) rb_thread_schedule();
return thread;
}
static VALUE
rb_thread_kill(thread)
VALUE thread;
{
rb_thread_t th = rb_thread_check(thread);
if (th != curr_thread && th->safe < 4) {
rb_secure(4);
}
if (th->status == THREAD_TO_KILL || th->status == THREAD_KILLED)
return thread;
if (th == th->next || th == main_thread) rb_exit(0);
rb_thread_ready(th);
th->gid = 0;
th->status = THREAD_TO_KILL;
if (!rb_thread_critical) rb_thread_schedule();
return thread;
}
static VALUE
rb_thread_s_kill(obj, th)
VALUE obj, th;
{
return rb_thread_kill(th);
}
static VALUE
rb_thread_exit()
{
return rb_thread_kill(curr_thread->thread);
}
static VALUE
rb_thread_pass()
{
rb_thread_schedule();
return Qnil;
}
VALUE
rb_thread_stop()
{
enum thread_status last_status = THREAD_RUNNABLE;
rb_thread_critical = 0;
if (curr_thread == curr_thread->next) {
rb_raise(rb_eThreadError, "stopping only thread\n\tnote: use sleep to stop forever");
}
if (curr_thread->status == THREAD_TO_KILL)
last_status = THREAD_TO_KILL;
curr_thread->status = THREAD_STOPPED;
rb_thread_schedule();
curr_thread->status = last_status;
return Qnil;
}
struct timeval rb_time_timeval();
void
rb_thread_polling()
{
if (curr_thread != curr_thread->next) {
curr_thread->status = THREAD_STOPPED;
curr_thread->delay = timeofday() + (double)0.06;
curr_thread->wait_for = WAIT_TIME;
rb_thread_schedule();
}
}
void
rb_thread_sleep(sec)
int sec;
{
if (curr_thread == curr_thread->next) {
TRAP_BEG;
sleep(sec);
TRAP_END;
return;
}
rb_thread_wait_for(rb_time_timeval(INT2FIX(sec)));
}
#if !defined HAVE_PAUSE
# if defined _WIN32 && !defined __CYGWIN__
# define pause() Sleep(INFINITE)
# else
# define pause() sleep(0x7fffffff)
# endif
#endif
void
rb_thread_sleep_forever()
{
if (curr_thread == curr_thread->next ||
curr_thread->status == THREAD_TO_KILL) {
TRAP_BEG;
pause();
TRAP_END;
return;
}
curr_thread->delay = DELAY_INFTY;
curr_thread->wait_for = WAIT_TIME;
curr_thread->status = THREAD_STOPPED;
rb_thread_schedule();
}
static VALUE
rb_thread_priority(thread)
VALUE thread;
{
return INT2NUM(rb_thread_check(thread)->priority);
}
static VALUE
rb_thread_priority_set(thread, prio)
VALUE thread, prio;
{
rb_thread_t th;
rb_secure(4);
th = rb_thread_check(thread);
th->priority = NUM2INT(prio);
rb_thread_schedule();
return prio;
}
static VALUE
rb_thread_safe_level(thread)
VALUE thread;
{
rb_thread_t th;
th = rb_thread_check(thread);
if (th == curr_thread) {
return INT2NUM(rb_safe_level());
}
return INT2NUM(th->safe);
}
static int thread_abort;
static VALUE
rb_thread_s_abort_exc()
{
return thread_abort?Qtrue:Qfalse;
}
static VALUE
rb_thread_s_abort_exc_set(self, val)
VALUE self, val;
{
rb_secure(4);
thread_abort = RTEST(val);
return val;
}
static VALUE
rb_thread_abort_exc(thread)
VALUE thread;
{
return rb_thread_check(thread)->abort?Qtrue:Qfalse;
}
static VALUE
rb_thread_abort_exc_set(thread, val)
VALUE thread, val;
{
rb_secure(4);
rb_thread_check(thread)->abort = RTEST(val);
return val;
}
#define THREAD_ALLOC(th) do {\
th = ALLOC(struct thread);\
\
th->next = 0;\
th->prev = 0;\
\
th->status = THREAD_RUNNABLE;\
th->result = 0;\
th->flags = 0;\
\
th->stk_ptr = 0;\
th->stk_len = 0;\
th->stk_max = 0;\
th->wait_for = 0;\
FD_ZERO(&th->readfds);\
FD_ZERO(&th->writefds);\
FD_ZERO(&th->exceptfds);\
th->delay = 0.0;\
th->join = 0;\
\
th->frame = 0;\
th->scope = 0;\
th->klass = 0;\
th->wrapper = 0;\
th->cref = ruby_cref;\
th->dyna_vars = ruby_dyna_vars;\
th->block = 0;\
th->iter = 0;\
th->tag = 0;\
th->tracing = 0;\
th->errinfo = Qnil;\
th->last_status = 0;\
th->last_line = 0;\
th->last_match = Qnil;\
th->abort = 0;\
th->priority = 0;\
th->gid = 1;\
th->locals = 0;\
} while(0)
static rb_thread_t
rb_thread_alloc(klass)
VALUE klass;
{
rb_thread_t th;
struct RVarmap *vars;
THREAD_ALLOC(th);
th->thread = Data_Wrap_Struct(klass, thread_mark, thread_free, th);
for (vars = th->dyna_vars; vars; vars = vars->next) {
if (FL_TEST(vars, DVAR_DONT_RECYCLE)) break;
FL_SET(vars, DVAR_DONT_RECYCLE);
}
return th;
}
#if defined(HAVE_SETITIMER)
static void
catch_timer(sig)
int sig;
{
#if !defined(POSIX_SIGNAL) && !defined(BSD_SIGNAL)
signal(sig, catch_timer);
#endif
if (!rb_thread_critical) {
if (rb_trap_immediate) {
rb_thread_schedule();
}
else rb_thread_pending = 1;
}
}
#else
int rb_thread_tick = THREAD_TICK;
#endif
#if defined(HAVE_SETITIMER)
static int thread_init = 0;
void
rb_thread_start_timer()
{
struct itimerval tval;
if (!thread_init) return;
tval.it_interval.tv_sec = 0;
tval.it_interval.tv_usec = 10000;
tval.it_value = tval.it_interval;
setitimer(ITIMER_VIRTUAL, &tval, NULL);
}
void
rb_thread_stop_timer()
{
struct itimerval tval;
if (!thread_init) return;
tval.it_interval.tv_sec = 0;
tval.it_interval.tv_usec = 0;
tval.it_value = tval.it_interval;
setitimer(ITIMER_VIRTUAL, &tval, NULL);
}
#endif
static VALUE
rb_thread_start_0(fn, arg, th_arg)
VALUE (*fn)();
void *arg;
rb_thread_t th_arg;
{
volatile rb_thread_t th = th_arg;
volatile VALUE thread = th->thread;
struct BLOCK* saved_block = 0;
enum thread_status status;
int state;
#if defined(HAVE_SETITIMER)
if (!thread_init) {
#ifdef POSIX_SIGNAL
posix_signal(SIGVTALRM, catch_timer);
#else
signal(SIGVTALRM, catch_timer);
#endif
thread_init = 1;
rb_thread_start_timer();
}
#endif
if (THREAD_SAVE_CONTEXT(curr_thread)) {
return thread;
}
if (ruby_block) { /* should nail down higher blocks */
struct BLOCK dummy;
dummy.prev = ruby_block;
blk_copy_prev(&dummy);
saved_block = ruby_block = dummy.prev;
}
scope_dup(ruby_scope);
if (!th->next) {
/* merge in thread list */
th->prev = curr_thread;
curr_thread->next->prev = th;
th->next = curr_thread->next;
curr_thread->next = th;
th->priority = curr_thread->priority;
th->gid = curr_thread->gid;
}
PUSH_TAG(PROT_THREAD);
if ((state = EXEC_TAG()) == 0) {
if (THREAD_SAVE_CONTEXT(th) == 0) {
curr_thread = th;
th->result = (*fn)(arg, th);
}
}
POP_TAG();
status = th->status;
if (th == main_thread) ruby_stop(state);
rb_thread_remove(th);
while (saved_block) {
struct BLOCK *tmp = saved_block;
if (tmp->frame.argc > 0)
free(tmp->frame.argv);
saved_block = tmp->prev;
free(tmp);
}
if (state && status != THREAD_TO_KILL && !NIL_P(ruby_errinfo)) {
th->flags |= THREAD_RAISED;
if (state == TAG_FATAL) {
/* fatal error within this thread, need to stop whole script */
main_thread->errinfo = ruby_errinfo;
rb_thread_cleanup();
}
else if (rb_obj_is_kind_of(ruby_errinfo, rb_eSystemExit)) {
if (th->safe >= 4) {
char buf[32];
sprintf(buf, "Insecure exit at level %d", th->safe);
th->errinfo = rb_exc_new2(rb_eSecurityError, buf);
}
else {
/* delegate exception to main_thread */
rb_thread_raise(1, &ruby_errinfo, main_thread);
}
}
else if (th->safe < 4 && (thread_abort || th->abort || RTEST(ruby_debug))) {
VALUE err = rb_exc_new(rb_eSystemExit, 0, 0);
error_print();
/* exit on main_thread */
rb_thread_raise(1, &err, main_thread);
}
else {
th->errinfo = ruby_errinfo;
}
}
rb_thread_schedule();
return 0; /* not reached */
}
VALUE
rb_thread_create(fn, arg)
VALUE (*fn)();
void *arg;
{
return rb_thread_start_0(fn, arg, rb_thread_alloc(rb_cThread));
}
static VALUE
rb_thread_yield(arg, th)
VALUE arg;
rb_thread_t th;
{
const ID *tbl;
scope_dup(ruby_block->scope);
tbl = ruby_scope->local_tbl;
if (tbl) {
int n = *tbl++;
for (tbl += 2, n -= 2; n > 0; --n) { /* skip first 2 ($_ and $~) */
ID id = *tbl++;
if (id != 0 && !rb_is_local_id(id)) /* push flip states */
rb_dvar_push(id, Qfalse);
}
}
rb_dvar_push('_', Qnil);
rb_dvar_push('~', Qnil);
ruby_block->dyna_vars = ruby_dyna_vars;
return rb_yield_0(mvalue_to_svalue(arg), 0, 0, Qtrue);
}
static VALUE
rb_thread_s_new(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
{
rb_thread_t th = rb_thread_alloc(klass);
volatile VALUE *pos;
pos = th->stk_pos;
rb_obj_call_init(th->thread, argc, argv);
if (th->stk_pos == 0) {
rb_raise(rb_eThreadError, "uninitialized thread - check `%s#initialize'",
rb_class2name(klass));
}
return th->thread;
}
static VALUE
rb_thread_initialize(thread, args)
VALUE thread, args;
{
if (!rb_block_given_p()) {
rb_raise(rb_eThreadError, "must be called with a block");
}
return rb_thread_start_0(rb_thread_yield, args, rb_thread_check(thread));
}
static VALUE
rb_thread_start(klass, args)
VALUE klass, args;
{
if (!rb_block_given_p()) {
rb_raise(rb_eThreadError, "must be called with a block");
}
return rb_thread_start_0(rb_thread_yield, args, rb_thread_alloc(klass));
}
static VALUE
rb_thread_value(thread)
VALUE thread;
{
rb_thread_t th = rb_thread_check(thread);
rb_thread_join(thread);
return th->result;
}
static VALUE
rb_thread_status(thread)
VALUE thread;
{
rb_thread_t th = rb_thread_check(thread);
if (rb_thread_dead(th)) {
if (!NIL_P(th->errinfo) && (th->flags & THREAD_RAISED))
return Qnil;
return Qfalse;
}
return rb_str_new2(thread_status_name(th->status));
}
static VALUE
rb_thread_alive_p(thread)
VALUE thread;
{
rb_thread_t th = rb_thread_check(thread);
if (rb_thread_dead(th)) return Qfalse;
return Qtrue;
}
static VALUE
rb_thread_stop_p(thread)
VALUE thread;
{
rb_thread_t th = rb_thread_check(thread);
if (rb_thread_dead(th)) return Qtrue;
if (th->status == THREAD_STOPPED) return Qtrue;
return Qfalse;
}
static void
rb_thread_wait_other_threads()
{
rb_thread_t th;
int found;
/* wait other threads to terminate */
while (curr_thread != curr_thread->next) {
found = 0;
FOREACH_THREAD(th) {
if (th != curr_thread && th->status != THREAD_STOPPED) {
found = 1;
break;
}
}
END_FOREACH(th);
if (!found) return;
rb_thread_schedule();
}
}
static void
rb_thread_cleanup()
{
rb_thread_t th;
while (curr_thread->status == THREAD_KILLED) {
curr_thread = curr_thread->prev;
}
FOREACH_THREAD(th) {
if (th != curr_thread && th->status != THREAD_KILLED) {
rb_thread_ready(th);
th->gid = 0;
th->priority = 0;
th->status = THREAD_TO_KILL;
RDATA(th->thread)->dfree = NULL;
}
}
END_FOREACH(th);
}
int rb_thread_critical;
static VALUE
rb_thread_critical_get()
{
return rb_thread_critical?Qtrue:Qfalse;
}
static VALUE
rb_thread_critical_set(obj, val)
VALUE obj, val;
{
rb_thread_critical = RTEST(val);
return val;
}
void
rb_thread_interrupt()
{
rb_thread_critical = 0;
rb_thread_ready(main_thread);
if (curr_thread == main_thread) {
rb_interrupt();
}
if (THREAD_SAVE_CONTEXT(curr_thread)) {
return;
}
curr_thread = main_thread;
rb_thread_restore_context(curr_thread, RESTORE_INTERRUPT);
}
void
rb_thread_signal_raise(sig)
char *sig;
{
if (sig == 0) return; /* should not happen */
rb_thread_critical = 0;
if (curr_thread == main_thread) {
rb_thread_ready(curr_thread);
rb_raise(rb_eSignal, "SIG%s", sig);
}
rb_thread_ready(main_thread);
if (THREAD_SAVE_CONTEXT(curr_thread)) {
return;
}
th_signm = sig;
curr_thread = main_thread;
rb_thread_restore_context(curr_thread, RESTORE_SIGNAL);
}
void
rb_thread_trap_eval(cmd, sig)
VALUE cmd;
int sig;
{
#if 0
rb_thread_critical = 0;
if (!rb_thread_dead(curr_thread)) {
rb_thread_ready(curr_thread);
rb_trap_eval(cmd, sig);
return;
}
rb_thread_ready(main_thread);
if (THREAD_SAVE_CONTEXT(curr_thread)) {
return;
}
th_cmd = cmd;
th_sig = sig;
curr_thread = main_thread;
rb_thread_restore_context(curr_thread, RESTORE_TRAP);
#else
rb_thread_critical = 0;
if (!rb_thread_dead(curr_thread)) {
if (THREAD_SAVE_CONTEXT(curr_thread)) {
return;
}
}
rb_thread_ready(main_thread);
th_cmd = cmd;
th_sig = sig;
curr_thread = main_thread;
rb_thread_restore_context(curr_thread, RESTORE_TRAP);
#endif
}
static VALUE
rb_thread_raise(argc, argv, th)
int argc;
VALUE *argv;
rb_thread_t th;
{
if (rb_thread_dead(th)) return Qnil;
if (curr_thread == th) {
rb_f_raise(argc, argv);
}
if (THREAD_SAVE_CONTEXT(curr_thread)) {
return th->thread;
}
rb_scan_args(argc, argv, "11", &th_raise_argv[0], &th_raise_argv[1]);
rb_thread_ready(th);
curr_thread = th;
th_raise_argc = argc;
th_raise_file = ruby_sourcefile;
th_raise_line = ruby_sourceline;
rb_thread_restore_context(curr_thread, RESTORE_RAISE);
return Qnil; /* not reached */
}
static VALUE
rb_thread_raise_m(argc, argv, thread)
int argc;
VALUE *argv;
VALUE thread;
{
rb_thread_t th = rb_thread_check(thread);
if (ruby_safe_level > th->safe) {
rb_secure(4);
}
rb_thread_raise(argc, argv, th);
return Qnil; /* not reached */
}
VALUE
rb_thread_local_aref(thread, id)
VALUE thread;
ID id;
{
rb_thread_t th;
VALUE val;
th = rb_thread_check(thread);
if (rb_safe_level() >= 4 && th != curr_thread) {
rb_raise(rb_eSecurityError, "Insecure: thread locals");
}
if (!th->locals) return Qnil;
if (st_lookup(th->locals, id, &val)) {
return val;
}
return Qnil;
}
static VALUE
rb_thread_aref(thread, id)
VALUE thread, id;
{
return rb_thread_local_aref(thread, rb_to_id(id));
}
VALUE
rb_thread_local_aset(thread, id, val)
VALUE thread;
ID id;
VALUE val;
{
rb_thread_t th = rb_thread_check(thread);
if (rb_safe_level() >= 4 && th != curr_thread) {
rb_raise(rb_eSecurityError, "Insecure: can't modify thread locals");
}
if (OBJ_FROZEN(thread)) rb_error_frozen("thread locals");
if (!th->locals) {
th->locals = st_init_numtable();
}
if (NIL_P(val)) {
st_delete(th->locals, &id, 0);
return Qnil;
}
st_insert(th->locals, id, val);
return val;
}
static VALUE
rb_thread_aset(thread, id, val)
VALUE thread, id, val;
{
return rb_thread_local_aset(thread, rb_to_id(id), val);
}
static VALUE
rb_thread_key_p(thread, id)
VALUE thread, id;
{
rb_thread_t th = rb_thread_check(thread);
if (!th->locals) return Qfalse;
if (st_lookup(th->locals, rb_to_id(id), 0))
return Qtrue;
return Qfalse;
}
static int
thread_keys_i(key, value, ary)
ID key;
VALUE value, ary;
{
rb_ary_push(ary, ID2SYM(key));
return ST_CONTINUE;
}
static VALUE
rb_thread_keys(thread)
VALUE thread;
{
rb_thread_t th = rb_thread_check(thread);
VALUE ary = rb_ary_new();
if (th->locals) {
st_foreach(th->locals, thread_keys_i, ary);
}
return ary;
}
static VALUE
rb_thread_inspect(thread)
VALUE thread;
{
char *cname = rb_class2name(CLASS_OF(thread));
rb_thread_t th = rb_thread_check(thread);
const char *status = thread_status_name(th->status);
VALUE str;
str = rb_str_new(0, strlen(cname)+7+16+9+1); /* 7:tags 16:addr 9:status 1:nul */
sprintf(RSTRING(str)->ptr, "#<%s:0x%lx %s>", cname, thread, status);
RSTRING(str)->len = strlen(RSTRING(str)->ptr);
OBJ_INFECT(str, thread);
return str;
}
void
rb_thread_atfork()
{
#if 1 /* enable on 1.7 */
rb_thread_t th;
if (rb_thread_alone()) return;
FOREACH_THREAD(th) {
if (th != curr_thread) {
th->status = THREAD_KILLED;
}
}
END_FOREACH(th);
main_thread = curr_thread;
curr_thread->next = curr_thread;
curr_thread->prev = curr_thread;
#endif
}
static VALUE rb_cCont;
static VALUE
rb_callcc(self)
VALUE self;
{
volatile VALUE cont;
rb_thread_t th;
struct tag *tag;
struct RVarmap *vars;
THREAD_ALLOC(th);
cont = Data_Wrap_Struct(rb_cCont, thread_mark, thread_free, th);
scope_dup(ruby_scope);
for (tag=prot_tag; tag; tag=tag->prev) {
scope_dup(tag->scope);
}
if (ruby_block) {
struct BLOCK *block = ruby_block;
while (block) {
block->tag->flags |= BLOCK_DYNAMIC;
block = block->prev;
}
}
th->thread = curr_thread->thread;
for (vars = th->dyna_vars; vars; vars = vars->next) {
if (FL_TEST(vars, DVAR_DONT_RECYCLE)) break;
FL_SET(vars, DVAR_DONT_RECYCLE);
}
if (THREAD_SAVE_CONTEXT(th)) {
return th->result;
}
else {
return rb_yield(cont);
}
}
static VALUE
rb_cont_call(argc, argv, cont)
int argc;
VALUE *argv;
VALUE cont;
{
rb_thread_t th = rb_thread_check(cont);
if (th->thread != curr_thread->thread) {
rb_raise(rb_eRuntimeError, "continuation called across threads");
}
switch (argc) {
case 0:
th->result = Qnil;
break;
case 1:
th->result = *argv;
break;
default:
th->result = rb_ary_new4(argc, argv);
break;
}
rb_thread_restore_context(th, RESTORE_NORMAL);
return Qnil;
}
struct thgroup {
int gid;
};
static VALUE
thgroup_s_alloc(klass)
VALUE klass;
{
VALUE group;
struct thgroup *data;
static int serial = 1;
group = Data_Make_Struct(klass, struct thgroup, 0, free, data);
data->gid = serial++;
return group;
}
static VALUE
thgroup_list(group)
VALUE group;
{
struct thgroup *data;
rb_thread_t th;
VALUE ary;
Data_Get_Struct(group, struct thgroup, data);
ary = rb_ary_new();
FOREACH_THREAD(th) {
if (th->gid == data->gid) {
rb_ary_push(ary, th->thread);
}
}
END_FOREACH(th);
return ary;
}
static VALUE
thgroup_add(group, thread)
VALUE group, thread;
{
rb_thread_t th;
struct thgroup *data;
rb_secure(4);
th = rb_thread_check(thread);
Data_Get_Struct(group, struct thgroup, data);
th->gid = data->gid;
return group;
}
void
Init_Thread()
{
VALUE cThGroup;
rb_eThreadError = rb_define_class("ThreadError", rb_eStandardError);
rb_cThread = rb_define_class("Thread", rb_cObject);
rb_undef_method(CLASS_OF(rb_cThread), "allocate");
rb_define_singleton_method(rb_cThread, "new", rb_thread_s_new, -1);
rb_define_method(rb_cThread, "initialize", rb_thread_initialize, -2);
rb_define_singleton_method(rb_cThread, "start", rb_thread_start, -2);
rb_define_singleton_method(rb_cThread, "fork", rb_thread_start, -2);
rb_define_singleton_method(rb_cThread, "stop", rb_thread_stop, 0);
rb_define_singleton_method(rb_cThread, "kill", rb_thread_s_kill, 1);
rb_define_singleton_method(rb_cThread, "exit", rb_thread_exit, 0);
rb_define_singleton_method(rb_cThread, "pass", rb_thread_pass, 0);
rb_define_singleton_method(rb_cThread, "current", rb_thread_current, 0);
rb_define_singleton_method(rb_cThread, "main", rb_thread_main, 0);
rb_define_singleton_method(rb_cThread, "list", rb_thread_list, 0);
rb_define_singleton_method(rb_cThread, "critical", rb_thread_critical_get, 0);
rb_define_singleton_method(rb_cThread, "critical=", rb_thread_critical_set, 1);
rb_define_singleton_method(rb_cThread, "abort_on_exception", rb_thread_s_abort_exc, 0);
rb_define_singleton_method(rb_cThread, "abort_on_exception=", rb_thread_s_abort_exc_set, 1);
rb_define_method(rb_cThread, "run", rb_thread_run, 0);
rb_define_method(rb_cThread, "wakeup", rb_thread_wakeup, 0);
rb_define_method(rb_cThread, "kill", rb_thread_kill, 0);
rb_define_method(rb_cThread, "exit", rb_thread_kill, 0);
rb_define_method(rb_cThread, "value", rb_thread_value, 0);
rb_define_method(rb_cThread, "status", rb_thread_status, 0);
rb_define_method(rb_cThread, "join", rb_thread_join, 0);
rb_define_method(rb_cThread, "alive?", rb_thread_alive_p, 0);
rb_define_method(rb_cThread, "stop?", rb_thread_stop_p, 0);
rb_define_method(rb_cThread, "raise", rb_thread_raise_m, -1);
rb_define_method(rb_cThread, "abort_on_exception", rb_thread_abort_exc, 0);
rb_define_method(rb_cThread, "abort_on_exception=", rb_thread_abort_exc_set, 1);
rb_define_method(rb_cThread, "priority", rb_thread_priority, 0);
rb_define_method(rb_cThread, "priority=", rb_thread_priority_set, 1);
rb_define_method(rb_cThread, "safe_level", rb_thread_safe_level, 0);
rb_define_method(rb_cThread, "[]", rb_thread_aref, 1);
rb_define_method(rb_cThread, "[]=", rb_thread_aset, 2);
rb_define_method(rb_cThread, "key?", rb_thread_key_p, 1);
rb_define_method(rb_cThread, "keys", rb_thread_keys, 0);
rb_define_method(rb_cThread, "inspect", rb_thread_inspect, 0);
/* allocate main thread */
main_thread = rb_thread_alloc(rb_cThread);
curr_thread = main_thread->prev = main_thread->next = main_thread;
rb_cCont = rb_define_class("Continuation", rb_cObject);
rb_undef_method(CLASS_OF(rb_cCont), "allocate");
rb_undef_method(CLASS_OF(rb_cCont), "new");
rb_define_method(rb_cCont, "call", rb_cont_call, -1);
rb_define_global_function("callcc", rb_callcc, 0);
cThGroup = rb_define_class("ThreadGroup", rb_cObject);
rb_define_singleton_method(cThGroup, "allocate", thgroup_s_alloc, 0);
rb_define_method(cThGroup, "list", thgroup_list, 0);
rb_define_method(cThGroup, "add", thgroup_add, 1);
rb_define_const(cThGroup, "Default", rb_obj_alloc(cThGroup));
}
static VALUE
rb_f_catch(dmy, tag)
VALUE dmy, tag;
{
int state;
ID t;
VALUE val; /* OK */
t = rb_to_id(tag);
PUSH_TAG(t);
if ((state = EXEC_TAG()) == 0) {
val = rb_yield_0(tag, 0, 0, 0);
}
else if (state == TAG_THROW && t == prot_tag->dst) {
val = prot_tag->retval;
state = 0;
}
POP_TAG();
if (state) JUMP_TAG(state);
return val;
}
static VALUE
catch_i(tag)
ID tag;
{
return rb_funcall(Qnil, rb_intern("catch"), 1, ID2SYM(tag));
}
VALUE
rb_catch(tag, proc, data)
const char *tag;
VALUE (*proc)();
VALUE data;
{
return rb_iterate((VALUE(*)_((VALUE)))catch_i, rb_intern(tag), proc, data);
}
static VALUE
rb_f_throw(argc, argv)
int argc;
VALUE *argv;
{
VALUE tag, value;
ID t;
struct tag *tt = prot_tag;
rb_scan_args(argc, argv, "11", &tag, &value);
t = rb_to_id(tag);
while (tt) {
if (tt->tag == t) {
tt->dst = t;
break;
}
if (tt->tag == PROT_THREAD) {
rb_raise(rb_eThreadError, "uncaught throw `%s' in thread 0x%x",
rb_id2name(t),
curr_thread);
}
tt = tt->prev;
}
if (!tt) {
rb_name_error(t, "uncaught throw `%s'", rb_id2name(t));
}
return_value(value);
rb_trap_restore_mask();
JUMP_TAG(TAG_THROW);
}
void
rb_throw(tag, val)
const char *tag;
VALUE val;
{
VALUE argv[2];
ID t = rb_intern(tag);
argv[0] = ID2SYM(t);
argv[1] = val;
rb_f_throw(2, argv);
}
static void
return_check()
{
struct tag *tt = prot_tag;
while (tt) {
if (tt->tag == PROT_FUNC) {
break;
}
if (tt->tag == PROT_THREAD) {
rb_raise(rb_eThreadError, "return from within thread 0x%x",
curr_thread);
}
tt = tt->prev;
}
}
|