/usr/share/tcltk/nsf-nx/nx.tcl is in nsf 2.0.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # -*- tcl -*-
############################################################
# nx.tcl --
#
# Implementation of the Next Scripting Language (NX) object
# system, based on the Next Scripting Framework (NSF).
#
# Copyright (C) 2010-2014 Gustaf Neumann
# Copyright (C) 2010-2014 Stefan Sobernig
#
# Vienna University of Economics and Business
# Institute of Information Systems and New Media
# A-1020, Welthandelsplatz 1
# Vienna, Austria
#
# This work is licensed under the MIT License http://www.opensource.org/licenses/MIT
#
# Copyright:
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
package require nsf
package provide nx 2.0.0
namespace eval ::nx {
namespace eval ::nsf {} ;# make pkg-indexer happy
namespace eval ::nsf::object {} ;# make pkg-indexer happy
namespace eval ::nsf::parameter {} ;# make pkg-indexer happy
namespace eval ::nx::internal {} ;# make pkg-indexer happy
namespace eval ::nx::traits {} ;# make pkg-indexer happy
#
# By setting the variable bootstrap, we can check later, whether we
# are in bootstrapping mode
#
set ::nsf::bootstrap ::nx
#
# First create the ::nx object system. The internally called methods,
# which are not defined by in this script, must have method handles
# included. The methods "create", "configure", "destroy", "move" and
# "__objectcoonfigureparameter" are defined in this script (either scripted, or
# via alias).
#
::nsf::objectsystem::create ::nx::Object ::nx::Class {
-class.alloc {__alloc ::nsf::methods::class::alloc 1}
-class.create create
-class.dealloc {__dealloc ::nsf::methods::class::dealloc 1}
-class.configureparameter __class_configureparameter
-class.recreate {__recreate ::nsf::methods::class::recreate 1}
-object.configure __configure
-object.configureparameter __object_configureparameter
-object.defaultmethod {defaultmethod ::nsf::methods::object::defaultmethod}
-object.destroy destroy
-object.init {init ::nsf::methods::object::init}
-object.move move
-object.unknown unknown
}
#
# get frequently used primitiva from the next scripting framework
#
namespace export next current self configure
namespace import ::nsf::next ::nsf::current ::nsf::self ::nsf::dispatch
#
# provide the standard command set for ::nx::Object
#
::nsf::method::alias Object upvar ::nsf::methods::object::upvar
::nsf::method::alias Object destroy ::nsf::methods::object::destroy
::nsf::method::alias Object uplevel ::nsf::methods::object::uplevel
#
# provide ::eval as method for ::nx::Object
#
::nsf::method::alias Object eval -frame method ::eval
######################################################################
# Default Methods (referenced via createobjectsystem)
######################################################################
namespace eval ::nsf::methods {} ;# make pkg-indexer happy
namespace eval ::nsf::methods::object {} ;# make pkg-indexer happy
# Actually, we do not need an unknown handler, but if someone
# defines his own unknown handler we define it automatically
proc ::nsf::methods::object::unknown {m args} {
return -code error "[::nsf::self]: unable to dispatch method '$m'"
}
# The default constructor
proc ::nsf::methods::object::init args {}
# This method can be called on invocations of the object without a
# specified method.
proc ::nsf::methods::object::defaultmethod {} {::nsf::self}
######################################################################
# Class methods
######################################################################
# provide the standard command set for Class
::nsf::method::alias Class create ::nsf::methods::class::create
::nsf::method::alias Class new ::nsf::methods::class::new
# set a few aliases as protected
# "__next", if defined, should be added as well
foreach cmd {uplevel upvar} {
::nsf::method::property Object $cmd call-protected 1
}
unset cmd
# protect some methods against redefinition
::nsf::method::property Object destroy redefine-protected true
::nsf::method::property Class create redefine-protected true
#
# Use method::provide for base methods in case they are overloaded
# with scripted counterparts
::nsf::method::provide __alloc {::nsf::method::alias __alloc ::nsf::methods::class::alloc}
::nsf::method::provide __dealloc {::nsf::method::alias __dealloc ::nsf::methods::class::dealloc}
::nsf::method::provide __recreate {::nsf::method::alias __recreate ::nsf::methods::class::recreate}
::nsf::method::provide __configure {::nsf::method::alias __configure ::nsf::methods::object::configure}
::nsf::method::provide unknown {::nsf::method::alias unknown ::nsf::methods::object::unknown}
#
# The method __resolve_method_path resolves a space separated path
# of a method name and creates from the path the necessary ensemble
# objects when needed.
#
::nsf::method::create Object __resolve_method_path {
-per-object:switch
-verbose:switch
path
} {
set object [::nsf::self]
set methodName $path
set regObject ""
if {[string first " " $path] > -1} {
set methodName [lindex $path end]
set regObject $object
foreach w [lrange $path 0 end-1] {
set scope [expr {[::nsf::is class $object] && !${per-object} ? "class" : "object"}]
if {[::nsf::is class $object] && !${per-object}} {
set scope class
set ensembleName [::nx::slotObj ${object} __$w]
if {[: ::nsf::methods::class::info::method exists $w]
&& [: ::nsf::methods::class::info::method type $w] ne "alias"} {
return -code error "refuse to overwrite method $w; delete/rename method first."
}
} else {
set scope object
if {[: ::nsf::methods::object::info::method exists $w]
&& [: ::nsf::methods::object::info::method type $w] ne "object"} {
return -code error "refuse to overwrite object method $w; delete/rename object method first."
}
set ensembleName ${object}::$w
}
#puts stderr "NX check $scope $object info methods $path @ <$w> cmd=[info command $w] obj?[nsf::object::exists $ensembleName] "
if {![nsf::object::exists $ensembleName]} {
#
# Create dispatch/ensemble object and accessor method (if wanted)
#
set o [nx::EnsembleObject create $ensembleName]
if {$scope eq "class"} {
if {$verbose} {puts stderr "... create object $o"}
# We are on a class, and have to create an alias to be
# accessible for objects
::nsf::method::alias $object $w $o
if {$verbose} {puts stderr "... create alias $object $w $o"}
} else {
if {$verbose} {puts stderr "... create object $o"}
}
set object $o
} else {
#
# The accessor method exists already, check, if it is
# appropriate for extending.
#
set type [::nsf::directdispatch $object ::nsf::methods::${scope}::info::method type $w]
set definition [::nsf::directdispatch $object ::nsf::methods::${scope}::info::method definition $w]
if {$scope eq "class"} {
if {$type eq ""} {
# In case of a copy operation, the ensemble object might
# exist, but the alias might be missing.
::nsf::method::alias $object $w $ensembleName
set object $ensembleName
} else {
if {$type ne "alias"} {error "can't append to $type"}
if {$definition eq ""} {error "definition must not be empty"}
set object [lindex $definition end]
}
} else {
if {$type ne "object"} {error "can't append to $type"}
if {[llength $definition] != 3} {error "unexpected definition '$definition'"}
append object ::$w
}
}
}
#puts stderr "... final object $object method $methodName"
}
return [list object $object methodName $methodName regObject $regObject]
}
::nsf::method::property Object __resolve_method_path call-protected true
######################################################################
# Define default method and property protection
######################################################################
::nsf::method::create Object __default_method_call_protection args {return false}
::nsf::method::create Object __default_accessor args {return public}
::nsf::method::property Object __default_method_call_protection call-protected true
::nsf::method::property Object __default_accessor call-protected true
######################################################################
# Define method "method" for Class
######################################################################
::nsf::method::create Class method {
name arguments:parameter,0..* -checkalways:switch -returns body
} {
set p [:__resolve_method_path $name]
set p [dict filter $p script {k v} {expr {$k in {object regObject methodName}}}]
dict with p {
#puts "class method $object.$methodName [list $arguments] {...}"
set r [::nsf::method::create $object \
-checkalways=$checkalways \
{*}[expr {$regObject ne "" ? "-reg-object [list $regObject]" : ""}] \
$methodName $arguments $body]
if {$r ne ""} {
# the method was not deleted
::nsf::method::property $object $r call-protected \
[::nsf::dispatch $object __default_method_call_protection]
if {[info exists returns]} {::nsf::method::property $object $r returns $returns}
}
return $r
}
}
######################################################################
# Define method "unknown"
######################################################################
Class eval {
# define unknown handler for class
:method unknown {methodName args} {
return -code error "method '$methodName' unknown for [::nsf::self];\
consider '[::nsf::self] create $methodName $args' instead of '[::nsf::self] $methodName $args'"
}
# protected is not yet defined
::nsf::method::property [::nsf::self] unknown call-protected true
}
######################################################################
# Remember names of method defining methods
######################################################################
# Well, class is not a method defining method either, but a modifier
array set ::nsf::methodDefiningMethod {
method 1 alias 1 forward 1 object 1
::nsf::classes::nx::Class::method 1 ::nsf::classes::nx::Object::method 1
::nsf::classes::nx::Class::alias 1 ::nsf::classes::nx::Object::alias 1
::nsf::classes::nx::Class::forward 1 ::nsf::classes::nx::Object::forward 1
}
######################################################################
# Provide method modifiers for ::nx::Object
######################################################################
Object eval {
# method modifier "public"
:method public {args} {
if {![info exists ::nsf::methodDefiningMethod([lindex $args 0])]} {
return -code error "'[lindex $args 0]' is not a method defining method"
} elseif {[lindex $args 0] eq "object" && ![info exists ::nsf::methodDefiningMethod([lindex $args 1])]} {
return -code error "'[lindex $args 1]' is not a method defining method"
}
set r [: -system {*}$args]
if {$r ne ""} {::nsf::method::property [self] $r call-protected false}
return $r
}
# method modifier "protected"
:method protected {args} {
if {![info exists ::nsf::methodDefiningMethod([lindex $args 0])]} {
return -code error "'[lindex $args 0]' is not a method defining method"
} elseif {[lindex $args 0] eq "object" && ![info exists ::nsf::methodDefiningMethod([lindex $args 1])]} {
return -code error "'[lindex $args 1]' is not a method defining method"
}
set r [: -system {*}$args]
if {$r ne ""} {::nsf::method::property [self] $r call-protected true}
return $r
}
# method modifier "private"
:method private {args} {
if {![info exists ::nsf::methodDefiningMethod([lindex $args 0])]} {
return -code error "'[lindex $args 0]' is not a method defining method"
} elseif {[lindex $args 0] eq "object" && ![info exists ::nsf::methodDefiningMethod([lindex $args 1])]} {
return -code error "'[lindex $args 1]' is not a method defining method"
}
set r [: -system {*}$args]
if {$r ne ""} {::nsf::method::property [self] $r call-private true}
return $r
}
}
# Provide a placeholder for objectparameter during the bootup
# process. The real definition is based on slots, which are not
# available at this point.
Object protected method __object_configureparameter {} {;}
######################################################################
# Define forward methods
######################################################################
#
# We could do this simply as
#
# ::nsf::method::forward Object forward ::nsf::method::forward %self -per-object
# ::nsf::method::forward Class forward ::nsf::method::forward %self
#
# but then, we would loose the option to use compound names
#
Class public method forward {
methodName
-default -prefix -frame -onerror -returns -verbose:switch
target:optional args
} {
set pathData [:__resolve_method_path $methodName]
set arguments [lrange [::nsf::current args] 1 end]
set object [dict get $pathData object]
if {[info exists target] && [string range $target 0 0] eq "-"} {
error "target '$target' must not start with a dash"
}
if {[info exists frame] && $frame ni {object default}} {
error "value of parameter -frame must be 'object' or 'default'"
}
if {[info exists returns]} {
set nrPreArgs [expr {[llength $arguments]-[llength $args]}]
# search for "-returns" in the arguments before $args and remove it
set p [lsearch -exact [lrange $arguments 0 $nrPreArgs] -returns]
if {$p > -1} {set arguments [lreplace $arguments $p $p+1]}
}
set r [::nsf::method::forward $object [dict get $pathData methodName] {*}$arguments]
::nsf::method::property $object $r call-protected \
[::nsf::dispatch $object __default_method_call_protection]
if {[info exists returns]} {::nsf::method::property $object $r returns $returns}
return $r
}
######################################################################
# Provide method "alias"
#
# -frame object|method make only sense for c-defined cmds,
######################################################################
Class public method alias {methodName -returns {-frame default} cmd} {
set pathData [:__resolve_method_path $methodName]
set object [dict get $pathData object]
#puts "class alias $object.[dict get $pathData methodName] $cmd"
set r [::nsf::method::alias $object [dict get $pathData methodName] -frame $frame $cmd]
::nsf::method::property $object $r call-protected \
[::nsf::dispatch $object __default_method_call_protection]
if {[info exists returns]} {::nsf::method::property $object $r returns $returns}
return $r
}
######################################################################
# Basic definitions for slots
######################################################################
#
# The function isSlotContainer tests, whether the provided object is
# a slot container based on the method property slotcontainer, used
# internally by the serializer.
#
proc ::nx::isSlotContainer {object} {
return [::nsf::object::property $object slotcontainer]
}
#
# The function nx::internal::setSlotContainerProperties set the method
# properties for slot containers
#
proc ::nx::internal::setSlotContainerProperties {baseObject containerName} {
set slotContainer ${baseObject}::$containerName
$slotContainer ::nsf::methods::object::requirenamespace
::nsf::method::property $baseObject -per-object $containerName call-protected true
::nsf::method::property $baseObject -per-object $containerName redefine-protected true
#puts stderr "::nsf::method::property $baseObject -per-object $containerName slotcontainer true"
#::nsf::method::property $baseObject -per-object $containerName slotcontainer true
::nsf::object::property $slotContainer slotcontainer true
}
#
# The function nx::slotObj ensures that the slot container for the
# provided baseObject exists. It returns either the name of the
# slotContainer (when no slot name was provided) or the fully
# qualified name of the slot object.
#
::nsf::proc ::nx::slotObj {{-container slot} baseObject name:optional} {
# Create slot container object if needed
set slotContainer ${baseObject}::$container
if {![::nsf::object::exists $slotContainer]} {
::nx::Object ::nsf::methods::class::alloc $slotContainer
::nx::internal::setSlotContainerProperties $baseObject $container
if {$container eq "per-object-slot"} {
::nsf::object::property $baseObject hasperobjectslots true
}
}
if {[info exists name]} {
return ${slotContainer}::$name
}
return ${slotContainer}
}
######################################################################
# Allocate system slot containers
######################################################################
::nx::slotObj ::nx::Class
::nx::slotObj ::nx::Object
######################################################################
# Define the EnsembleObject with its base methods
######################################################################
Class create ::nx::EnsembleObject
::nx::EnsembleObject eval {
#
# The EnsembleObjects are called typically with a "self" bound to
# the object, on which they are registered as methods. This way,
# only method registered on the object are resolved (ensemble
# methods). Only for the methods "unknown" and "defaultmethod",
# self is actually the ensemble object. These methods are
# maintenance methods. We have to be careful here ...
#
# a) not to interfere between "maintenance methods" and "ensemble
# methods" within the maintenance methods. This is achieved
# via explicit dispatch commands in the maintenance methods.
#
# b) not to overload "maintenance methods" with "ensemble
# methods". This is achieved via the object-method-only policy
# (we cannot call "subcmd <subcmdName>" when "subcmdName" is a
# method on EnsembleObject) and via a skip object-methods flag
# in nsf when calling e.g. "unknown" (such that a subcmd
# "unknown" does not interfere with the method "unknown").
#
:protected method init {} {
::nsf::object::property [self] keepcallerself true
::nsf::object::property [self] perobjectdispatch true
}
:protected method unknown {callInfo args} {
set path [lrange $callInfo 1 end-1]
set m [lindex $callInfo end]
set obj [lindex $callInfo 0]
#::nsf::__db_show_stack
#puts stderr "CI=<$callInfo> args <$args>"
#puts stderr "### [list $obj ::nsf::methods::object::info::lookupmethods -path \"$path *\"]"
if {[catch {set valid [$obj ::nsf::methods::object::info::lookupmethods -path "$path *"]} errorMsg]} {
set valid ""
puts stderr "+++ UNKNOWN raises error $errorMsg"
}
set ref "\"$m\" of $obj $path"
return -code error "unable to dispatch sub-method $ref; valid are: [join [lsort $valid] {, }]"
}
:protected method defaultmethod {} {
if {[catch {set obj [uplevel ::nsf::current]}]} {
error "ensemble dispatch called outside of method context"
}
set path [uplevel {::nsf::current methodpath}]
set l [string length $path]
set submethods [$obj ::nsf::methods::object::info::lookupmethods -path "$path *"]
foreach sm $submethods {set results([lindex [string range $sm $l+1 end] 0]) 1}
return -code error "valid submethods of $obj $path: [lsort [array names results]]"
}
# end of EnsembleObject
}
######################################################################
# Now we are able to use ensemble methods in the definition of NX
######################################################################
Object eval {
#
# Define method defining methods for Object.
#
# These are:
# - "method"
# - "alias"
# - "forward"
:public method "object method" {
methodName arguments:parameter,0..* -checkalways:switch -returns body
} {
set pathData [:__resolve_method_path -per-object $methodName]
set object [dict get $pathData object]
set regObject [dict get $pathData regObject]
# puts "object method $object.[dict get $pathData methodName] [list $arguments] {...}"
set r [::nsf::method::create $object \
-checkalways=$checkalways \
{*}[expr {$regObject ne "" ? "-reg-object [list $regObject]" : ""}] \
-per-object \
[dict get $pathData methodName] $arguments $body]
if {$r ne ""} {
# the method was not deleted
::nsf::method::property $object $r call-protected \
[::nsf::dispatch $object __default_method_call_protection]
if {[info exists returns]} {::nsf::method::property $object $r returns $returns}
}
return $r
}
:public method "object alias" {methodName -returns {-frame default} cmd} {
set pathData [:__resolve_method_path -per-object $methodName]
set object [dict get $pathData object]
#puts "object alias $object.[dict get $pathData methodName] $cmd"
set r [::nsf::method::alias $object -per-object [dict get $pathData methodName] \
-frame $frame $cmd]
::nsf::method::property $object -per-object $r call-protected \
[::nsf::dispatch $object __default_method_call_protection]
if {[info exists returns]} {::nsf::method::property $object $r returns $returns}
return $r
}
:public method "object forward" {
methodName
-default -prefix -frame -onerror -returns -verbose:switch
target:optional args
} {
set arguments [lrange [::nsf::current args] 1 end]
set pathData [:__resolve_method_path -per-object $methodName]
set object [dict get $pathData object]
if {[info exists target] && [string range $target 0 0] eq "-"} {
error "target '$target' must not start with a dash"
}
if {[info exists frame] && $frame ni {object default}} {
error "value of parameter '-frame' must be 'object' or 'default'"
}
if {[info exists returns]} {
set nrPreArgs [expr {[llength $arguments]-[llength $args]}]
# search for "-returns" in the arguments before $args ...
set p [lsearch -exact [lrange $arguments 0 $nrPreArgs] -returns]
# ... and remove it if found
if {$p > -1} {set arguments [lreplace $arguments $p $p+1]}
}
set r [::nsf::method::forward $object -per-object \
[dict get $pathData methodName] {*}$arguments]
::nsf::method::property $object -per-object $r call-protected \
[::nsf::dispatch $object __default_method_call_protection]
if {[info exists returns]} {::nsf::method::property $object $r returns $returns}
return $r
}
}
#
# Method for deletion of properties, variables and plain methods
#
Object eval {
:public method "delete object method" {name} {
::nsf::method::delete [self] -per-object $name
}
:public method "delete object property" {name} {
# call explicitly the per-object variant of "info::slotobjects"
set slot [: ::nsf::methods::object::info::slotobjects -type ::nx::Slot $name]
if {$slot eq ""} {
return -code error \
"[self]: cannot delete object-specific property '$name'"
}
$slot destroy
nsf::var::unset -nocomplain [self] $name
}
:public method "delete object variable" {name} {
# First remove the instance variable and complain, if it does
# not exist.
if {[nsf::var::exists [self] $name]} {
nsf::var::unset [self] $name
} else {
return -code error \
"[self]: object does not have an instance variable '$name'"
}
# call explicitly the per-object variant of "info::slotobejcts"
set slot [: ::nsf::methods::object::info::slotobjects -type ::nx::Slot $name]
if {$slot ne ""} {
# it is not a slot-less variable
$slot destroy
}
}
}
Class eval {
:public method "delete method" {name} {
::nsf::method::delete [self] $name
}
:public method "delete property" {name} {
set slot [:info slots $name]
if {$slot eq ""} {
return -code error "[self]: cannot delete property '$name'"
}
$slot destroy
}
:public method "delete variable" {name} {
set slot [:info slots $name]
if {$slot eq ""} {
return -code error "[self]: cannot delete variable '$name'"
}
$slot destroy
}
}
######################################################################
# Provide method "require"
######################################################################
Object eval {
:method "require namespace" {} {
::nsf::directdispatch [::nsf::self] ::nsf::methods::object::requirenamespace
}
#
# method require, base cases
#
:method "require object method" {methodName} {
::nsf::method::require [::nsf::self] $methodName 1
return [:info lookup method $methodName]
}
#
# method require, public explicitly
#
:method "require public object method" {methodName} {
set result [:require object method $methodName]
::nsf::method::property [self] $result call-protected false
return $result
}
#
# method require, protected explicitly
#
:method "require protected object method" {methodName} {
set result [:require object method $methodName]
::nsf::method::property [self] $result call-protected true
return $result
}
#
# method require, private explicitly
#
:method "require private object method" {methodName} {
set result [:require object method $methodName]
::nsf::method::property [self] $result call-private true
return $result
}
}
nx::Class eval {
:method "require method" {methodName} {
return [::nsf::method::require [::nsf::self] $methodName 0]
}
:method "require public method" {methodName} {
set result [:require method $methodName]
::nsf::method::property [self] $result call-protected false
return $result
}
:method "require protected method" {methodName} {
set result [:require method $methodName]
::nsf::method::property [self] $result call-protected true
return $result
}
:method "require private method" {methodName} {
set result [:require method $methodName]
::nsf::method::property [self] $result call-private true
return $result
}
}
######################################################################
# Info definition
######################################################################
# we have to use "eval", since objectParameters are not defined yet
Object eval {
:alias "info lookup filter" ::nsf::methods::object::info::lookupfilter
:alias "info lookup filters" ::nsf::methods::object::info::lookupfilters
:alias "info lookup method" ::nsf::methods::object::info::lookupmethod
:alias "info lookup methods" ::nsf::methods::object::info::lookupmethods
:alias "info lookup mixins" ::nsf::methods::object::info::lookupmixins
:method "info lookup slots" {{-type:class ::nx::Slot} -source pattern:optional} {
set cmd [list ::nsf::methods::object::info::lookupslots -type $type]
if {[info exists source]} {lappend cmd -source $source}
if {[info exists pattern]} {lappend cmd $pattern}
return [: {*}$cmd]
}
:method "info lookup parameters" {methodName pattern:optional} {
return [::nsf::cmd::info \
parameter \
-context [self] \
[:info lookup method $methodName] \
{*}[expr {[info exists pattern] ? $pattern : ""}] ]
}
:method "info lookup syntax" {methodName pattern:optional} {
return [::nsf::cmd::info \
syntax \
-context [self] \
[:info lookup method $methodName] \
{*}[expr {[info exists pattern] ? $pattern : ""}] ]
}
:method "info lookup variables" {pattern:optional} {
return [: info lookup slots -type ::nx::VariableSlot {*}[current args]]
}
:alias "info children" ::nsf::methods::object::info::children
:alias "info class" ::nsf::methods::object::info::class
:alias "info has mixin" ::nsf::methods::object::info::hasmixin
:alias "info has namespace" ::nsf::methods::object::info::hasnamespace
:alias "info has type" ::nsf::methods::object::info::hastype
:alias "info name" ::nsf::methods::object::info::name
:alias "info object filters" ::nsf::methods::object::info::filters
:alias "info object methods" ::nsf::methods::object::info::methods
:alias "info object mixins" ::nsf::methods::object::info::mixins
:method "info object slots" {{-type:class ::nx::Slot} pattern:optional} {
set method [list ::nsf::methods::object::info::slotobjects -type $type]
if {[info exists pattern]} {lappend method $pattern}
return [: {*}$method]
}
:method "info object variables" {pattern:optional} {
return [: info object slots -type ::nx::VariableSlot {*}[current args]]
}
#
# Parameter extractors
#
# :method "info parameter default" {p:parameter varName:optional} {
# if {[info exists varName]} {
# uplevel [list ::nsf::parameter::info default $p $varName]
# } else {
# ::nsf::parameter::info default $p
# }
# }
# :method "info parameter name" {p:parameter} {::nsf::parameter::info name $p}
# :method "info parameter syntax" {p:parameter} {::nsf::parameter::info syntax $p}
# :method "info parameter type" {p:parameter} {::nsf::parameter::info type $p}
:alias "info parent" ::nsf::methods::object::info::parent
:alias "info precedence" ::nsf::methods::object::info::precedence
:alias "info vars" ::nsf::methods::object::info::vars
:method "info variable definition" {handle:object,type=::nx::VariableSlot} {
return [$handle definition]
}
:method "info variable name" {handle:object,type=::nx::VariableSlot} {
return [$handle cget -name]
}
:method "info variable parameter" {handle:object,type=::nx::VariableSlot} {
return [$handle parameter]
}
}
######################################################################
# Create the ensemble object for "info" here manually to prevent the
# replicated definitions from Object.info in Class.info.
# Potentially, some names are overwritten later by Class.info. Note,
# that the automatically created name of the ensemble object has to
# be the same as defined above.
######################################################################
#
# The following test is just for the redefinition case, after a
# "package forget". We clear "info method" for ::nx::Object to avoid
# confusions in the copy loop below, which uses method "method".
#
if {[::nsf::directdispatch ::nx::Object::slot::__info ::nsf::methods::object::info::methods "method"] ne ""} {
Object method "info method" {} {}
}
#
# There is not need to copy the "info object" ensemble to
# ::nx::Class since this is reached via ensemble "next" in
# nx::Object.
#
Class eval {
:alias "info lookup" ::nx::Object::slot::__info::lookup
:alias "info filters" ::nsf::methods::class::info::filters
:alias "info has" ::nx::Object::slot::__info::has
:alias "info heritage" ::nsf::methods::class::info::heritage
:alias "info instances" ::nsf::methods::class::info::instances
:alias "info methods" ::nsf::methods::class::info::methods
:alias "info mixins" ::nsf::methods::class::info::mixins
:alias "info mixinof" ::nsf::methods::class::info::mixinof
:method "info slots" {{-type ::nx::Slot} -closure:switch -source:optional pattern:optional} {
set cmd [list ::nsf::methods::class::info::slotobjects -type $type]
if {[info exists source]} {lappend cmd -source $source}
if {$closure} {lappend cmd -closure}
if {[info exists pattern]} {lappend cmd $pattern}
return [: {*}$cmd]
}
:alias "info subclasses" ::nsf::methods::class::info::subclass
:alias "info superclasses" ::nsf::methods::class::info::superclass
:method "info variables" {pattern:optional} {
set cmd {info slots -type ::nx::VariableSlot}
if {[info exists pattern]} {lappend cmd $pattern}
return [: {*}$cmd]
}
}
######################################################################
# Define "info info" and "info unknown"
######################################################################
::nsf::proc ::nx::internal::infoOptions {-asList:switch obj {methods ""}} {
# puts stderr "INFO INFO $obj -> '[::nsf::directdispatch $obj ::nsf::methods::object::info::methods -type all]'"
foreach name [::nsf::directdispatch $obj ::nsf::methods::object::info::methods] {
if {$name in $methods || $name eq "unknown"} continue
lappend methods $name
}
if {$asList} {
return $methods
} else {
return "valid options are: [join [lsort $methods] {, }]"
}
}
Object method "info info" {-asList:switch} {
::nx::internal::infoOptions -asList=$asList ::nx::Object::slot::__info
}
Class method "info info" {-asList:switch} {
::nx::internal::infoOptions -asList=$asList ::nx::Class::slot::__info [next {info -asList}]
}
# finally register method for "info method" (otherwise, we cannot use "method" above)
Class eval {
#:alias "info method" ::nsf::methods::class::info::method
:method "info method args" {name} {: ::nsf::methods::class::info::method args $name}
:method "info method body" {name} {: ::nsf::methods::class::info::method body $name}
:method "info method definition" {name} {: ::nsf::methods::class::info::method definition $name}
:method "info method exists" {name} {: ::nsf::methods::class::info::method exists $name}
:method "info method handle" {name} {: ::nsf::methods::class::info::method definitionhandle $name}
:method "info method registrationhandle" {name} {: ::nsf::methods::class::info::method registrationhandle $name}
:method "info method definitionhandle" {name} {: ::nsf::methods::class::info::method definitionhandle $name}
:method "info method origin" {name} {: ::nsf::methods::class::info::method origin $name}
:method "info method parameters" {name pattern:optional} {
set defs [: ::nsf::methods::class::info::method parameter $name]
if {[info exists pattern]} {return [::nsf::parameter::filter $defs $pattern]}
return $defs
}
:method "info method syntax" {name} {
return [string trimright "/cls/ [namespace tail $name] [: ::nsf::methods::class::info::method syntax $name]" { }]
}
:method "info method type" {name} {: ::nsf::methods::class::info::method type $name}
:method "info method submethods" {name} {: ::nsf::methods::class::info::method submethods $name}
:method "info method returns" {name} {: ::nsf::methods::class::info::method returns $name}
}
Object eval {
#:alias "info object method" ::nsf::methods::object::info::method
:method "info object method args" {name} {: ::nsf::methods::object::info::method args $name}
:method "info object method body" {name} {: ::nsf::methods::object::info::method body $name}
:method "info object method definition" {name} {: ::nsf::methods::object::info::method definition $name}
:method "info object method exists" {name} {: ::nsf::methods::object::info::method exists $name}
:method "info object method handle" {name} {: ::nsf::methods::object::info::method definitionhandle $name}
:method "info object method registrationhandle" {name} {: ::nsf::methods::object::info::method registrationhandle $name}
:method "info object method definitionhandle" {name} {: ::nsf::methods::object::info::method definitionhandle $name}
:method "info object method origin" {name} {: ::nsf::methods::object::info::method origin $name}
:method "info object method parameters" {name pattern:optional} {
set defs [: ::nsf::methods::object::info::method parameter $name]
if {[info exists pattern]} {return [::nsf::parameter::filter $defs $pattern]}
return $defs
}
:method "info object method syntax" {name} {
return [string trimright "/obj/ [namespace tail $name] [: ::nsf::methods::object::info::method syntax $name]" { }]
}
:method "info object method type" {name} {: ::nsf::methods::object::info::method type $name}
:method "info object method submethods" {name} {: ::nsf::methods::object::info::method submethods $name}
:method "info object method returns" {name} {: ::nsf::methods::object::info::method returns $name}
}
######################################################################
# Provide Tk-style methods for configure and cget
######################################################################
Object eval {
:public alias cget ::nsf::methods::object::cget
:public alias configure ::nsf::methods::object::configure
#:public method "info configure" {} {: ::nsf::methods::object::info::objectparameter syntax}
}
#nsf::method::create ::nx::Class::slot::__info::configure defaultmethod {} {
# uplevel {: ::nsf::methods::object::info::objectparameter syntax}
#}
######################################################################
# Definition of "abstract method foo ...."
#
# Deactivated for now. If we like to revive this method, it should
# be integrated with the method modifiers and the method "class"
#
# Object method abstract {methtype -per-object:switch methName arglist} {
# if {$methtype ne "method"} {
# error "invalid method type '$methtype', must be 'method'"
# }
# set body "
# if {!\[::nsf::current isnextcall\]} {
# error \"abstract method $methName $arglist called\"
# } else {::nsf::next}
# "
# if {${per-object}} {
# :method -per-object $methName $arglist $body
# } else {
# :method $methName $arglist $body
# }
# }
######################################################################
# MetaSlot definitions
#
# The MetaSlots are used later to create SlotClasses
######################################################################
#
# We are in bootstrap code; we cannot use slots/parameter to define
# slots, so the code is a little low level. After the definition of
# the slots, we can use slot-based code such as "-parameter" or
# "objectparameter".
#
Class create ::nx::MetaSlot
::nsf::relation::set MetaSlot superclass Class
MetaSlot object method requireClass {required:class old:class,0..1} {
#
# Combine two classes and return the more specialized one
#
if {$old eq "" || $old eq $required} {return $required}
if {[$required info superclasses -closure $old] ne ""} {
#puts stderr "required $required has $old as superclass => specializing"
return $required
} elseif {[$required info subclasses -closure $old] ne ""} {
#puts stderr "required $required is more general than $old => keep $old"
return $old
} else {
return -code error "required class $required not compatible with $old"
}
}
MetaSlot public object method parseParameterSpec {
{-class ""}
{-defaultopts ""}
spec
default:optional
} {
array set opt $defaultopts
set opts ""
set colonPos [string first : $spec]
if {$colonPos == -1} {
set name $spec
set parameterOptions ""
} else {
set parameterOptions [string range $spec [expr {$colonPos+1}] end]
set name [string range $spec 0 [expr {$colonPos -1}]]
foreach property [split $parameterOptions ,] {
if {$property in [list "required" "convert" "substdefault" "noarg" "nodashalnum"]} {
if {$property eq "convert" } {set class [:requireClass ::nx::VariableSlot $class]}
lappend opts -$property 1
} elseif {$property eq "noconfig"} {
set opt(-configurable) 0 ;# TODO
} elseif {$property eq "incremental"} {
return -code error "parameter option incremental must not be used; use non-positional argument -incremental instead"
} elseif {[string match type=* $property]} {
set class [:requireClass ::nx::VariableSlot $class]
set type [string range $property 5 end]
if {![string match ::* $type]} {set type ::$type}
} elseif {[string match arg=* $property]} {
set argument [string range $property 4 end]
lappend opts -arg $argument
} elseif {[string match method=* $property]} {
lappend opts -methodname [string range $property 7 end]
} elseif {$property eq "optional"} {
lappend opts -required 0
} elseif {$property in [list "alias" "forward" "cmd" "initcmd"]} {
lappend opts -disposition $property
set class [:requireClass ::nx::ObjectParameterSlot $class]
} elseif {[regexp {([01])[.][.]([1n*])} $property _ minOccurance maxOccurance]} {
lappend opts -multiplicity $property
} else {
set type $property
}
}
}
if {[info exists type]} {
#if {$type eq "switch"} {error "switch is not allowed as type for object parameter $name"}
lappend opts -type $type
}
lappend opts {*}[array get opt]
#puts stderr "[self] *** parseParameterSpec [list $name $parameterOptions $class $opts]"
return [list $name $parameterOptions $class $opts]
}
MetaSlot public object method createFromParameterSpec {
target
-per-object:switch
{-class ""}
{-initblock ""}
{-private:switch}
{-incremental:switch}
{-defaultopts ""}
spec
default:optional
} {
lassign [:parseParameterSpec -class $class -defaultopts $defaultopts $spec] \
name parameterOptions class opts
lappend opts -incremental $incremental
if {[info exists default]} {
lappend opts -default $default
}
if {${per-object}} {
lappend opts -per-object true
set scope object
set container per-object-slot
} else {
set scope class
set container slot
}
if {$private} {
regsub -all : __$target _ prefix
lappend opts -settername $name -name __private($target,$name)
set slotname ${prefix}.$name
} else {
set slotname $name
}
if {$class eq ""} {
set class ::nx::VariableSlot
} else {
#puts stderr "*** Class for '$target $name' is $class // [$class info heritage]"
}
set slotObj [::nx::slotObj -container $container $target $slotname]
#puts stderr "*** [list $class create $slotObj] {*}$opts $initblock]"
set r [$class create $slotObj {*}$opts $initblock]
#puts stderr "*** returned $r"
return $r
}
}
namespace eval ::nx {
######################################################################
# Slot definitions
######################################################################
MetaSlot create ::nx::Slot
MetaSlot create ::nx::ObjectParameterSlot
::nsf::relation::set ObjectParameterSlot superclass Slot
MetaSlot create ::nx::MethodParameterSlot
::nsf::relation::set MethodParameterSlot superclass Slot
# Create a slot instance for dispatching method parameter specific
# value checkers
MethodParameterSlot create ::nx::methodParameterSlot
# Define a temporary, low level interface for defining slot
# values. Normally, this is done via slot objects, which are defined
# later. The proc is removed later in this script.
proc createBootstrapVariableSlots {class definitions} {
foreach att $definitions {
if {[llength $att]>1} {lassign $att att default}
set slotObj [::nx::slotObj $class $att]
#puts stderr "::nx::BootStrapVariableSlot create $slotObj"
::nx::BootStrapVariableSlot create $slotObj
if {[info exists default]} {
#puts stderr "::nsf::var::set $slotObj default $default"
::nsf::var::set $slotObj default $default
unset default
}
#
# register the standard setter
#
#::nsf::method::setter $class $att
#
# make setter protected
#
#regexp {^([^:]+):} $att . att
#::nsf::method::property $class $att call-protected true
#
# set for every bootstrap property slot the position 0
#
::nsf::var::set $slotObj position 0
::nsf::var::set $slotObj configurable 1
}
#puts stderr "Bootstrap-slot for $class calls parameter::cache::classinvalidate"
::nsf::parameter::cache::classinvalidate $class
}
ObjectParameterSlot protected method namedParameterSpec {-map-private:switch prefix name options} {
#
# Build a pos/nonpos parameter specification from name and option list
#
if {${map-private} && [info exists :accessor] && ${:accessor} eq "private"} {
set pName ${:settername}
} else {
set pName $name
}
if {[llength $options]>0} {
return $prefix${pName}:[join $options ,]
} else {
return $prefix${pName}
}
}
######################################################################
# Define slots for slots
######################################################################
#
# We would like to have property slots during bootstrap to
# configure the slots itself (e.g. a relation slot object). This is
# however a chicken/egg problem, so we use a very simple class for
# defining slots for slots, called BootStrapVariableSlot.
#
MetaSlot create ::nx::BootStrapVariableSlot
::nsf::relation::set BootStrapVariableSlot superclass ObjectParameterSlot
BootStrapVariableSlot public method getParameterSpec {} {
#
# Bootstrap version of getParameter spec. Just bare essentials.
#
if {[info exists :parameterSpec]} {
return ${:parameterSpec}
}
set name [namespace tail [self]]
set prefix [expr {[info exists :positional] && ${:positional} ? "" : "-"}]
set options [list]
if {[info exists :default]} {
if {[string match {*\[*\]*} ${:default}]} {
append options substdefault
}
set :parameterSpec [list [list [:namedParameterSpec $prefix $name $options]] ${:default}]
} else {
set :parameterSpec [list [:namedParameterSpec $prefix $name $options]]
}
return ${:parameterSpec}
}
BootStrapVariableSlot protected method init {args} {
#
# Empty constructor; do nothing, intentionally without "next"
#
}
######################################################################
# configure nx::Slot
######################################################################
createBootstrapVariableSlots ::nx::Slot {
}
######################################################################
# configure nx::ObjectParameterSlot
######################################################################
createBootstrapVariableSlots ::nx::ObjectParameterSlot {
{name "[namespace tail [::nsf::self]]"}
{domain "[lindex [regexp -inline {^(.*)::(per-object-slot|slot)::[^:]+$} [::nsf::self]] 1]"}
{manager "[::nsf::self]"}
{per-object false}
{methodname}
{forwardername}
{defaultmethods {}}
{accessor public}
{incremental:boolean false}
{configurable true}
{noarg}
{nodashalnum}
{disposition alias}
{required false}
{default}
{initblock}
{substdefault false}
{position 0}
{positional}
{elementtype}
{multiplicity 1..1}
}
# TODO: check, if substdefault/default could work with e.g. alias; otherwise, move substdefault down
#
# Default unknown handler for all slots
#
ObjectParameterSlot protected method unknown {method args} {
#
# Report just application specific methods not starting with "__"
#
set methods [list]
foreach m [::nsf::directdispatch [::nsf::self] \
::nsf::methods::object::info::lookupmethods -source application] {
if {[string match __* $m]} continue
lappend methods $m
}
return -code error "method '$method' unknown for slot [::nsf::self]; valid are: {[lsort $methods]}"
}
ObjectParameterSlot protected method init {args} {
#
# Provide a default depending on :name for :methodname. When slot
# objects are created, invalidate the object parameters to reflect
# the changes
#
if {${:incremental} && [:info class] eq [current class]} {
return -code error "flag incremental must not be used for this slot type"
}
if {![info exists :methodname]} {
set :methodname ${:name}
}
if {${:per-object}} {
::nsf::parameter::cache::objectinvalidate ${:domain}
} else {
::nsf::parameter::cache::classinvalidate ${:domain}
}
nsf::object::property [self] initialized 1
#
# plain object parameter have currently no setter/forwarder
#
}
ObjectParameterSlot public method destroy {} {
if {[info exists :domain] && ${:domain} ne ""} {
#
# When slot objects are destroyed, flush the parameter cache and
# delete the accessors
#
#puts stderr "*** slot destroy of [self], domain ${:domain} per-object ${:per-object}"
if {${:per-object}} {
::nsf::parameter::cache::objectinvalidate ${:domain}
if {[${:domain} ::nsf::methods::object::info::method exists ${:name}]} {
::nsf::method::delete ${:domain} -per-object ${:name}
}
} else {
::nsf::parameter::cache::classinvalidate ${:domain}
if {[${:domain} ::nsf::methods::class::info::method exists ${:name}]} {
::nsf::method::delete ${:domain} ${:name}
}
}
}
::nsf::next
}
#
# Define a forwarder directing accessor calls to the slot
#
ObjectParameterSlot protected method createForwarder {name domain} {
set dm [${:manager} cget -defaultmethods]
::nsf::method::forward $domain \
-per-object=${:per-object} \
$name \
-prefix "value=" \
-onerror [list ${:manager} onError] \
${:manager} \
[expr {$dm ne "" ? [list %1 $dm] : "%1"}] %self \
${:forwardername}
}
ObjectParameterSlot public method onError {cmd msg} {
if {[string match "%1 requires argument*" $msg]} {
set template {wrong # args: use \"$cmd [join $methods |]\"}
} elseif {[string match "*unknown for slot*" $msg]} {
lassign $cmd slot calledMethod obj .
regexp {=(.*)$} $calledMethod . calledMethod
regexp {::([^:]+)$} $slot . slot
set template {submethod $calledMethod undefined for $slot: use \"$obj $slot [join $methods |]\"}
}
if {[info exists template]} {
set methods ""
foreach m [lsort [:info lookup methods -callprotection public value=*]] {
lappend methods [lindex [split $m =] end]
}
return -code error [subst $template]
}
return -code error $msg
}
ObjectParameterSlot protected method makeForwarder {} {
#
# Build forwarder from the source object class ($domain) to the slot
# to delegate read and update operations
#
# intended to be called on RelationSlot or VariableSlot
#
if {![info exists :forwardername]} {
set :forwardername ${:methodname}
}
#puts stderr "makeforwarder --> '${:forwardername}'"
if {[info exists :settername]} {
set d [nsf::directdispatch ${:domain} \
::nsf::classes::nx::Object::__resolve_method_path \
{*}[expr {${:per-object} ? "-per-object" : ""}] ${:settername}]
:createForwarder [dict get $d methodName] [dict get $d object]
} else {
:createForwarder ${:name} ${:domain}
}
}
ObjectParameterSlot protected method getParameterOptions {
{-withMultiplicity 0}
{-forObjectParameter 0}
} {
#
# Obtain a list of parameter options from slot object
#
set options [list]
if {[info exists :elementtype] && ${:elementtype} ne {}} {
lappend options ${:elementtype}
#puts stderr "+++ [self] added elementtype ${:elementtype}"
}
if {${:disposition} eq "slotset"} {
lappend options slot=[::nsf::self] ${:disposition}
if {${:forwardername} ne ${:name}} {
lappend options method=${:forwardername}
}
} else {
lappend options ${:disposition}
}
if {${:name} ne ${:methodname}} {
lappend options method=${:methodname}
}
if {${:required}} {
lappend options required
} elseif {[info exists :positional] && ${:positional}} {
lappend options optional
}
if {$forObjectParameter} {
#
# Check, if get or set methods were overloaded
#
if {[:info lookup method value=set] ni {"" "::nsf::classes::nx::RelationSlot::value=set"}} {
# In case the "set" method was provided on the slot, ask nsf to call it directly
lappend options slot=[::nsf::self] slotset
} elseif {[:info lookup method value=get] ni {"" "::nsf::classes::nx::RelationSlot::value=get"}} {
# In case the "get" method was provided on the slot, ask nsf to call it directly
lappend options slot=[::nsf::self]
}
if {[info exists :substdefault] && ${:substdefault}} {
lappend options substdefault
}
}
if {[info exists :noarg] && ${:noarg}} {lappend options noarg}
if {[info exists :nodashalnum] && ${:nodashalnum}} {lappend options nodashalnum}
if {$withMultiplicity && [info exists :multiplicity] && ${:multiplicity} ne "1..1"} {
#puts stderr "### [self] added multiplicity ${:multiplicity}"
lappend options ${:multiplicity}
}
return $options
}
ObjectParameterSlot public method getParameterSpec {} {
#
# Get a full object parmeter specification from slot object
#
if {[info exists :parameterSpec]} {
} else {
set prefix [expr {[info exists :positional] && ${:positional} ? "" : "-"}]
set options [:getParameterOptions -withMultiplicity true -forObjectParameter true]
if {[info exists :initblock]} {
if {[info exists :default]} {
if {[llength $options] > 0} {
::nsf::is -complain [join $options ,] ${:default}
#puts stderr "::nsf::is -complain [join $options ,] ${:default} ==> OK"
}
append initblock "\n::nsf::var::set \[::nsf::self\] ${:name} [list ${:default}]\n"
#puts stderr ================append-default-to-initblock-old=<${:initblock}>
}
lappend options initcmd
append initblock ${:initblock}
set :parameterSpec [list [:namedParameterSpec $prefix ${:name} $options] $initblock]
} elseif {[info exists :default]} {
# deactivated for now: || [string first {$} ${:default}] > -1
if {[string match {*\[*\]*} ${:default}]} {
lappend options substdefault
}
set :parameterSpec [list [:namedParameterSpec $prefix ${:name} $options] ${:default}]
} else {
set :parameterSpec [list [:namedParameterSpec $prefix ${:name} $options]]
}
}
#puts stderr ================${:parameterSpec}
return ${:parameterSpec}
}
ObjectParameterSlot public method getPropertyDefinitionOptions {parameterSpec} {
#puts "accessor <${:accessor}> configurable ${:configurable} per-object ${:per-object}"
set mod [expr {${:per-object} ? "object" : ""}]
set opts ""
if {${:configurable}} {
lappend opts -accessor ${:accessor}
if {${:incremental}} {lappend opts -incremental}
if {[info exists :default]} {
return [list ${:domain} {*}$mod property {*}$opts [list $parameterSpec ${:default}]]
}
set methodName property
} else {
lappend opts -accessor ${:accessor}
if {${:configurable}} {lappend opts -configurable true}
if {[info exists :default]} {
return [list ${:domain} {*}$mod variable {*}$opts $parameterSpec ${:default}]
}
set methodName variable
}
return [list ${:domain} {*}$mod $methodName {*}$opts $parameterSpec]
}
ObjectParameterSlot public method definition {} {
set options [:getParameterOptions -withMultiplicity true]
if {[info exists :positional]} {lappend options positional}
#if {!${:configurable}} {lappend options noconfig}
return [:getPropertyDefinitionOptions [:namedParameterSpec -map-private "" ${:name} $options]]
}
######################################################################
# We have no working objectparameter yet, since it requires a
# minimal slot infrastructure to build object parameters from
# slots. The above definitions should be sufficient as a basis for
# object parameters. We provide the definition here before we refine
# the slot definitions.
#
# Invalidate previously defined object parameter (built with the
# empty objectparameter definition.
#
::nsf::parameter::cache::classinvalidate MetaSlot
######################################################################
# Define objectparameter method
######################################################################
Object protected method __object_configureparameter {} {
set slotObjects [nsf::directdispatch [self] ::nsf::methods::object::info::lookupslots -type ::nx::Slot]
return [::nsf::parameter::specs $slotObjects]
}
Class protected method __class_configureparameter {} {
set slotObjects [nsf::directdispatch [self] ::nsf::methods::class::info::slotobjects -closure -type ::nx::Slot]
return [::nsf::parameter::specs $slotObjects]
}
}
namespace eval ::nx {
######################################################################
# class nx::RelationSlot
######################################################################
MetaSlot create ::nx::RelationSlot
::nsf::relation::set RelationSlot superclass ObjectParameterSlot
createBootstrapVariableSlots ::nx::RelationSlot {
{accessor public}
{multiplicity 0..n}
{settername}
}
RelationSlot protected method init {} {
::nsf::next
if {${:accessor} ne ""} {
:makeForwarder
}
}
#
# create methods for slot operations set/get/add/clear/delete
#
::nsf::method::alias RelationSlot value=set ::nsf::relation::set
::nsf::method::alias RelationSlot value=get ::nsf::relation::get
RelationSlot public method value=clear {obj prop} {
set result [::nsf::relation::set $obj $prop]
::nsf::relation::set $obj $prop {}
return $result
}
RelationSlot protected method delete_value {obj prop old value} {
#
# Helper method for the delete operation, deleting a value from a
# relation slot list.
#
if {[string first * $value] > -1 || [string first \[ $value] > -1} {
#
# Value contains globbing meta characters.
#
if {[info exists :elementtype] && ${:elementtype} eq "mixinreg"
&& ![string match ::* $value]} {
#
# Prefix glob pattern with ::, since all object names have
# leading "::"
#
set value ::$value
}
return [lsearch -all -not -glob -inline $old $value]
} elseif {[info exists :elementtype] && ${:elementtype} eq "mixinreg"} {
#
# Value contains no globbing meta characters, and elementtype could be
# fully qualified
#
if {[string first :: $value] == -1} {
#
# Obtain a fully qualified name.
#
if {![::nsf::object::exists $value]} {
return -code error "$value does not appear to be an object"
}
set value [::nsf::directdispatch $value -frame method ::nsf::self]
}
}
set p [lsearch -exact $old $value]
if {$p > -1} {
return [lreplace $old $p $p]
} else {
#
# In the resulting list might be guards. If so, do another round
# of checking to test the first list element.
#
set new [list]
set found 0
foreach v $old {
if {[llength $v]>1 && $value eq [lindex $v 0]} {
set found 1
continue
}
lappend new $v
}
if {!$found} {
return -code error "$value is not a $prop of $obj (valid are: $old)"
}
return $new
}
}
RelationSlot public method value=add {obj prop value {pos 0}} {
set oldSetting [::nsf::relation::get $obj $prop]
#puts stderr [list ::nsf::relation::set $obj $prop [linsert $oldSetting $pos $value]]
#
# Use uplevel to avoid namespace surprises
#
uplevel [list ::nsf::relation::set $obj $prop [linsert $oldSetting $pos $value]]
}
RelationSlot public method value=delete {-nocomplain:switch obj prop value} {
uplevel [list ::nsf::relation::set $obj $prop \
[:delete_value $obj $prop [::nsf::relation::get $obj $prop] $value]]
}
######################################################################
# Register system slots
######################################################################
#
# Create relation slots
#
# on nx::Object for
#
# object-mixin
# object-filter
#
# and on nx::Class for
#
# mixin
# filter
::nx::RelationSlot create ::nx::Object::slot::object-mixins \
-multiplicity 0..n \
-defaultmethods {} \
-disposition slotset \
-forwardername object-mixin \
-settername "object mixins" \
-elementtype mixinreg
::nx::RelationSlot create ::nx::Object::slot::object-filters \
-multiplicity 0..n \
-defaultmethods {} \
-disposition slotset \
-forwardername object-filter \
-settername "object filters" \
-elementtype filterreg
::nx::RelationSlot create ::nx::Class::slot::mixins \
-multiplicity 0..n \
-defaultmethods {} \
-disposition slotset \
-forwardername "class-mixin" \
-elementtype mixinreg
::nx::RelationSlot create ::nx::Class::slot::filters \
-multiplicity 0..n \
-defaultmethods {} \
-disposition slotset \
-forwardername class-filter \
-elementtype filterreg
#
# Define "class" as a ObjectParameterSlot defined as alias
#
::nx::ObjectParameterSlot create ::nx::Object::slot::class \
-methodname "::nsf::methods::object::class" -elementtype class
#
# Define "superclass" as a ObjectParameterSlot defined as alias
#
::nx::ObjectParameterSlot create ::nx::Class::slot::superclasses \
-methodname "::nsf::methods::class::superclass" \
-elementtype class \
-multiplicity 1..n \
-default ::nx::Object
#
# Define the initblock as a positional ObjectParameterSlot
#
::nx::ObjectParameterSlot create ::nx::Object::slot::__initblock \
-disposition cmd \
-nodashalnum true \
-positional true \
-position 2
#
# Make sure the invalidate all ObjectParameterSlots
#
::nsf::parameter::cache::classinvalidate ::nx::ObjectParameterSlot
#
# Define method "guard" and "methods" for object filters
#
::nx::Object::slot::object-filters object method value=guard {obj prop filter guard:optional} {
if {[info exists guard]} {
::nsf::directdispatch $obj ::nsf::methods::object::filterguard $filter $guard
} else {
lindex [$obj info object filters -guards $filter] 0 2
}
}
::nx::Object::slot::object-filters object method value=methods {obj prop pattern:optional} {
if {[info exists pattern]} {
$obj info object filters $pattern
} else {
$obj info object filters
}
}
#
# Define method "guard" and "methods" for filters
#
::nx::Class::slot::filters object method value=guard {obj prop filter guard:optional} {
if {[info exists guard]} {
::nsf::directdispatch $obj ::nsf::methods::class::filterguard $filter $guard
} else {
lindex [$obj info filters -guards $filter] 0 2
}
}
::nx::Class::slot::filters object method value=methods {obj prop pattern:optional} {
if {[info exists pattern]} {
$obj info filters $pattern
} else {
$obj info filters
}
}
#
# object mixins
#
::nx::Object::slot::object-mixins object method value=classes {obj prop pattern:optional} {
if {[info exists pattern]} {
$obj info object mixins $pattern
} else {
$obj info object mixins
}
}
::nx::Object::slot::object-mixins object method value=guard {obj prop mixin guard:optional} {
if {[info exists guard]} {
::nsf::directdispatch $obj ::nsf::methods::object::mixinguard $mixin $guard
} else {
lindex [$obj info object mixins -guards $mixin] 0 2
}
}
#
# mixins
#
::nx::Class::slot::mixins object method value=classes {obj prop pattern:optional} {
if {[info exists pattern]} {
$obj info mixins $pattern
} else {
$obj info mixins
}
}
::nx::Class::slot::mixins object method value=guard {obj prop filter guard:optional} {
if {[info exists guard]} {
::nsf::directdispatch $obj ::nsf::methods::class::mixinguard $filter $guard
} else {
lindex [$obj info mixins -guards $mixin] 0 2
}
}
#::nsf::method::alias ::nx::Class::slot::object-filters guard ::nx::Object::slot::object-filters::guard
#
# With a special purpose eval, we could avoid the need for
# reconfigure for slot changes via eval (two cases in the regression
# test). However, most of the eval uses are from various reading
# purposes, so maybe this is an overkill.
#
#::nx::ObjectParameterSlot public method eval {cmd} {
# set r [next]
# #puts stderr "eval on slot [self] $cmd -> $r"
# :reconfigure
# return $r
#}
######################################################################
# Variable slots
######################################################################
::nsf::parameter::cache::classinvalidate MetaSlot
MetaSlot create ::nx::VariableSlot -superclass ::nx::ObjectParameterSlot
createBootstrapVariableSlots ::nx::VariableSlot {
{arg}
{convert false}
{incremental:boolean false}
{multiplicity 1..1}
{accessor public}
{type}
{settername}
valuecmd
defaultcmd
valuechangedcmd
}
::nx::VariableSlot public method setCheckedInstVar {-nocomplain:switch object value} {
if {[::nsf::var::exists $object ${:name}] && !$nocomplain} {
return -code error \
"object $object has already an instance variable named '${:name}'"
}
set options [:getParameterOptions -withMultiplicity true]
if {[llength $options]} {
::nsf::is -configure -complain -name ${:name}: [join $options ,] $value
}
set restore [:removeTraces $object *]
::nsf::var::set $object ${:name} ${:default}
if {[info exists restore]} { {*}$restore }
}
::nx::VariableSlot protected method setterRedefinedOptions {} {
if {[:info lookup method value=set] ne "::nsf::classes::nx::VariableSlot::value=set"} {
# In case the "set" method was provided on the slot, ask nsf to call it directly
return [list slot=[::nsf::self] slotset]
}
if {[:info lookup method value=get] ne "::nsf::classes::nx::VariableSlot::value=get"} {
# In case the "get" method was provided on the slot, ask nsf to call it directly
return [list slot=[::nsf::self]]
}
}
::nx::VariableSlot protected method getParameterOptions {
{-withMultiplicity 0}
{-forObjectParameter 0}
} {
set options ""
set slotObject ""
if {[info exists :type]} {
set type ${:type}
if {$type eq "switch" && !$forObjectParameter} {set type boolean}
if {$type in {cmd initcmd}} {
lappend options $type
} elseif {[string match ::* $type]} {
lappend options [expr {[::nsf::is metaclass $type] ? "class" : "object"}] type=$type
} else {
lappend options $type
if {$type ni [list "" \
"boolean" "integer" "object" "class" \
"metaclass" "baseclass" "parameter" \
"alnum" "alpha" "ascii" "control" "digit" "double" \
"false" "graph" "lower" "print" "punct" "space" "true" \
"wideinteger" "wordchar" "xdigit" ]} {
lappend options slot=[::nsf::self]
}
}
}
if {$forObjectParameter} {
foreach o [:setterRedefinedOptions] {
if {$o ni $options} {lappend options $o}
}
}
if {[:info lookup method initialize] ne "" && $forObjectParameter} {
if {"slot=[::nsf::self]" ni $options} {lappend options slot=[::nsf::self]}
lappend options slotinitialize
}
if {[info exists :arg]} {lappend options arg=${:arg}}
if {${:required}} {
lappend options required
} elseif {[info exists :positional] && ${:positional}} {
lappend options optional
}
if {${:convert}} {lappend options convert}
if {$withMultiplicity && [info exists :multiplicity] && ${:multiplicity} ne "1..1"} {
lappend options ${:multiplicity}
}
if {$forObjectParameter} {
if {[info exists :substdefault] && ${:substdefault}} {
lappend options substdefault
}
if {[info exists :configurable] && !${:configurable}} {
lappend options noconfig
}
}
#puts stderr "*** getParameterOptions $withMultiplicity $forObjectParameter [self] returns '$options'"
return $options
}
::nx::VariableSlot protected method isMultivalued {} {
return [string match {*..[n*]} ${:multiplicity}]
}
#
# When there are accessors defined, we use always the forwarders in
# NX. XOTcl2 has a detailed optimization.
#
::nx::VariableSlot protected method needsForwarder {} {
return 1
}
::nx::VariableSlot protected method makeAccessor {} {
if {${:accessor} eq "none"} {
#puts stderr "*** Do not register forwarder ${:domain} ${:name}"
return 0
}
if {[:needsForwarder]} {
set handle [:makeForwarder]
:makeIncrementalOperations
} else {
set handle [:makeSetter]
}
if {${:accessor} eq "protected"} {
::nsf::method::property ${:domain} {*}[expr {${:per-object} ? "-per-object" : ""}] \
$handle call-protected true
set :configurable 0
} elseif {${:accessor} eq "private"} {
::nsf::method::property ${:domain} {*}[expr {${:per-object} ? "-per-object" : ""}] \
$handle call-private true
set :configurable 0
} elseif {${:accessor} ne "public"} {
set msg "accessor value '${:accessor}' invalid; might be one of public|protected|private or none"
:destroy
return -code error $msg
}
return 1
}
::nx::VariableSlot public method reconfigure {} {
#puts stderr "*** Should we reconfigure [self]???"
unset -nocomplain :parameterSpec
if {${:incremental}} {
if {${:accessor} eq "none"} { set :accessor "public" }
if {![:isMultivalued]} { set :multiplicity [string range ${:multiplicity} 0 0]..n }
}
:makeAccessor
if {${:per-object} && [info exists :default]} {
:setCheckedInstVar -nocomplain=[info exists :nocomplain] ${:domain} ${:default}
}
if {[::nsf::is class ${:domain}]} {
::nsf::parameter::cache::classinvalidate ${:domain}
}
}
::nx::VariableSlot public method parameter {} {
# This is a shortend "lightweight" version of "getParameterSpec"
# returning less (implicit) details. used e.g. by "info variable parameter"
set options [:getParameterOptions -withMultiplicity true]
set spec [:namedParameterSpec -map-private "" ${:name} $options]
if {[info exists :default]} {lappend spec ${:default}}
return $spec
}
::nx::VariableSlot protected method checkDefault {} {
if {![info exists :default] || [string match {*\[*\]*} ${:default}]} {return}
set options [:getParameterOptions -withMultiplicity true]
if {[llength $options] > 0} {
if {[catch {::nsf::is -complain -configure -name ${:name}: [join $options ,] ${:default}} errorMsg]} {
#puts stderr "**** destroy [self] - $errorMsg"
:destroy
return -code error $errorMsg
}
}
}
::nx::VariableSlot protected method init {} {
#puts "VariableSlot [self] ${:incremental} && ${:accessor} && ${:multiplicity} incremental ${:incremental}"
if {${:incremental}} {
if {${:accessor} eq "none"} { set :accessor "public" }
if {![:isMultivalued]} {
set :multiplicity [string range ${:multiplicity} 0 0]..n
}
}
next
:makeAccessor
:checkDefault
:handleTraces
}
::nx::VariableSlot protected method makeSetter {} {
set options [:getParameterOptions -withMultiplicity true]
set setterParam ${:name}
if {[llength $options]>0} {append setterParam :[join $options ,]}
::nsf::method::setter ${:domain} {*}[expr {${:per-object} ? "-per-object" : ""}] $setterParam
}
::nx::VariableSlot protected method defineIncrementalOperations {options_single options} {
#
# Just define these setter methods, when these are not defined
# jet. We need the methods as well for e.g. private properties,
# where the setting of the property is handled via slot.
#
if {[:info lookup method value=set] eq "::nsf::classes::nx::VariableSlot::value=set"} {
set args [list obj var [:namedParameterSpec {} value $options]]
:public object method value=set $args {::nsf::var::set $obj $var $value}
}
if {[:isMultivalued] && [:info lookup method value=add] eq "::nsf::classes::nx::VariableSlot::value=add"} {
lappend options_single slot=[::nsf::self]
set args [list obj prop [:namedParameterSpec {} value $options_single] {pos 0}]
:public object method value=add $args {::nsf::next}
} else {
# TODO should we deactivate add/delete?
}
}
::nx::VariableSlot protected method makeIncrementalOperations {} {
set options_single [:getParameterOptions]
#if {[llength $options_single] == 0} {}
if {![info exists :type]} {
# No need to make per-slot methods; the general rules on
# nx::VariableSlot are sufficient
return
}
#puts "makeIncrementalOperations -- single $options_single type ${:type}"
#if {[info exists :type]} {puts ".... type ${:type}"}
set options [:getParameterOptions -withMultiplicity true]
lappend options slot=[::nsf::self]
:defineIncrementalOperations $options_single $options
}
######################################################################
# Handle variable traces
######################################################################
::nx::VariableSlot protected method removeTraces {object matchOps} {
#puts stderr "====removeTraces ${:name} $matchOps"
set restore ""
set traces [::nsf::directdispatch $object -frame object ::trace info variable ${:name}]
foreach trace $traces {
lassign $trace ops cmdPrefix
if {![string match $matchOps $ops]} continue
#puts stderr "====remove trace variable ${:name} $ops $cmdPrefix"
::nsf::directdispatch $object -frame object ::trace remove variable ${:name} $ops $cmdPrefix
append restore "[list ::nsf::directdispatch $object -frame object ::trace add variable ${:name} $ops $cmdPrefix]\n"
}
return $restore
}
::nx::VariableSlot protected method handleTraces {} {
# essentially like before
set __initblock ""
set trace {::nsf::directdispatch [::nsf::self] -frame object ::trace}
# There be already default values registered on the
# class. If so, defaultcmd is ignored.
if {[info exists :default]} {
if {[info exists :defaultcmd]} {
return -code error \
"defaultcmd can't be used together with default value"
}
if {[info exists :valuecmd]} {
return -code error \
"valuecmd can't be used together with default value"}
} elseif [info exists :defaultcmd] {
if {[info exists :valuecmd]} {
return -code error \
"valuecmd can't be used together with defaultcmd"
}
append __initblock "::nsf::directdispatch [::nsf::self] -frame object :removeTraces \[::nsf::self\] read\n"
append __initblock "$trace add variable [list ${:name}] read \
\[list [::nsf::self] __default_from_cmd \[::nsf::self\] [list [set :defaultcmd]]\]\n"
} elseif [info exists :valuecmd] {
append __initblock "::nsf::directdispatch [::nsf::self] -frame object :removeTraces \[::nsf::self\] read\n"
append __initblock "$trace add variable [list ${:name}] read \
\[list [::nsf::self] __value_from_cmd \[::nsf::self\] [list [set :valuecmd]]\]"
}
if {[info exists :valuechangedcmd]} {
append __initblock "::nsf::directdispatch [::nsf::self] -frame object :removeTraces \[::nsf::self\] write\n"
append __initblock "$trace add variable [list ${:name}] write \
\[list [::nsf::self] __value_changed_cmd \[::nsf::self\] [list [set :valuechangedcmd]]\]"
}
if {$__initblock ne ""} {
if {${:per-object}} {
${:domain} eval $__initblock
}
#puts stderr initblock=$__initblock
set :initblock $__initblock
}
}
#
# Implementation of methods called by the traces
#
::nx::VariableSlot method __default_from_cmd {obj cmd var sub op} {
#puts "GETVAR [::nsf::current method] obj=$obj cmd=$cmd, var=$var, op=$op"
::nsf::directdispatch $obj -frame object \
::trace remove variable $var $op [list [::nsf::self] [::nsf::current method] $obj $cmd]
::nsf::var::set $obj $var [$obj eval $cmd]
}
::nx::VariableSlot method __value_from_cmd {obj cmd var sub op} {
#puts "GETVAR [::nsf::current method] obj=$obj cmd=$cmd, var=$var, op=$op"
::nsf::var::set $obj $var [$obj eval $cmd]
}
::nx::VariableSlot method __value_changed_cmd {obj cmd var sub op} {
# puts "valuechanged obj=$obj cmd=$cmd, var=$var, op=$op, ...\n$obj exists $var -> [::nsf::var::set $obj $var]"
eval $cmd
}
######################################################################
# Implementation of (incremental) forwarder operations for
# VariableSlots:
# - set
# - get
# - add
# - delete
######################################################################
::nsf::method::alias ::nx::VariableSlot value=get ::nsf::var::get
::nsf::method::alias ::nx::VariableSlot value=set ::nsf::var::set
::nx::VariableSlot public method value=unset {obj prop -nocomplain:switch} {
::nsf::var::unset -nocomplain=$nocomplain $obj $prop
}
::nx::VariableSlot public method value=add {obj prop value {pos 0}} {
if {![:isMultivalued]} {
#puts stderr "... vars [[self] info vars] // [[self] eval {set :multiplicity}]"
return -code error "property $prop of [set :domain] ist not multivalued"
}
if {[::nsf::var::exists $obj $prop]} {
::nsf::var::set $obj $prop [linsert [::nsf::var::set $obj $prop] $pos $value]
} else {
::nsf::var::set $obj $prop [list $value]
}
}
::nx::VariableSlot public method value=delete {-nocomplain:switch obj prop value} {
set old [::nsf::var::set $obj $prop]
set p [lsearch -glob $old $value]
if {$p>-1} {::nsf::var::set $obj $prop [lreplace $old $p $p]} else {
return -code error "$value is not a $prop of $obj (valid are: $old)"
}
}
######################################################################
# Define methods "property" and "variable"
######################################################################
nx::Object method "object variable" {
{-accessor "none"}
{-incremental:switch}
{-class ""}
{-configurable:boolean false}
{-initblock ""}
{-nocomplain:switch}
spec:parameter
defaultValue:optional
} {
#
# This method creates sometimes a slot, sometimes not
# (optimization). We need a slot currently in the following
# situations:
# - when accessors are needed
# (serializer uses slot object to create accessors)
# - when initblock is non empty
#
#puts stderr "Object variable $spec accessor $accessor nocomplain $nocomplain incremental $incremental"
# get name and list of parameter options
lassign [::nx::MetaSlot parseParameterSpec -class $class $spec] \
name parameterOptions class options
array set opts $options
if {[info exists opts(-configurable)]} {
set configurable $opts(-configurable)
}
#if {$initblock eq "" && $accessor eq "none" && !$incremental}
if {$initblock eq "" && !$configurable && $accessor eq "none" && !$incremental} {
#
# we can build a slot-less variable
#
#puts "... slotless variable $spec"
set isSwitch [regsub {\mswitch\M} $parameterOptions boolean parameterOptions]
if {[info exists defaultValue]} {
if {[info exists :$name] && !$nocomplain} {
return -code error \
"object [self] has already an instance variable named '$name'"
}
if {$parameterOptions ne ""} {
#puts stderr "*** ::nsf::is $parameterOptions $defaultValue // opts=$options"
# we rely here that the nsf::is error message expresses the implementation limits
set noptions {}
foreach o [split $parameterOptions ,] {
if {$o ne "noconfig"} {lappend noptions $o}
}
set parameterOptions [join $noptions ,]
::nsf::is -complain $parameterOptions $defaultValue
} else {
set name $spec
}
set :$name $defaultValue
} elseif {$isSwitch} {
set :$name 0
} else {
return -code error \
"variable definition for '$name' (without value and accessor) is useless"
}
return
}
#puts "... slot variable $spec"
#
# create variable via a slot object
#
set slot [::nx::MetaSlot createFromParameterSpec [self] \
-per-object \
-class $class \
-initblock $initblock \
-incremental=$incremental \
-private=[expr {$accessor eq "private"}] \
-defaultopts [list -accessor $accessor] \
$spec \
{*}[expr {[info exists defaultValue] ? [list $defaultValue] : ""}]]
if {$nocomplain} {$slot eval {set :nocomplain 1}}
if {!$configurable} {$slot eval {set :configurable false}}
if {[info exists defaultValue]} {
# We could consider calling "configure" instead, but that would
# not work for true "variable" handlers
$slot setCheckedInstVar -nocomplain=$nocomplain [self] $defaultValue
#set :__initblock($name) 1
}
if {[$slot eval {info exists :settername}]} {
set name [$slot cget -settername]
} else {
set name [$slot cget -name]
}
return [::nsf::directdispatch [self] ::nsf::methods::object::info::method registrationhandle $name]
}
Object method "object property" {
{-accessor ""}
{-configurable:boolean true}
{-incremental:switch}
{-class ""}
{-nocomplain:switch}
spec:parameter
{initblock ""}
} {
if {${accessor} eq ""} {
set accessor [::nsf::dispatch [self] __default_accessor]
#puts stderr "OBJECT [self] got default accessor ${accessor}"
}
set r [[self] object variable \
-accessor $accessor \
-incremental=$incremental \
-class $class \
-initblock $initblock \
-configurable $configurable \
-nocomplain=$nocomplain \
{*}$spec]
return $r
}
nx::Class method variable {
{-accessor "none"}
{-incremental:switch}
{-class ""}
{-configurable:boolean false}
{-initblock ""}
spec:parameter
defaultValue:optional
} {
set slot [::nx::MetaSlot createFromParameterSpec [::nsf::self] \
-class $class \
-initblock $initblock \
-incremental=$incremental \
-private=[expr {$accessor eq "private"}] \
-defaultopts [list -accessor $accessor -configurable $configurable] \
$spec \
{*}[expr {[info exists defaultValue] ? [list $defaultValue] : ""}]]
if {[$slot eval {info exists :settername}]} {
set name [$slot cget -settername]
} else {
set name [$slot cget -name]
}
#puts stderr handle=[::nsf::directdispatch [self] ::nsf::methods::class::info::method registrationhandle $name]
return [::nsf::directdispatch [self] ::nsf::methods::class::info::method registrationhandle $name]
}
nx::Class method property {
{-accessor ""}
{-configurable:boolean true}
{-incremental:switch}
{-class ""}
spec:parameter
{initblock ""}
} {
if {${accessor} eq ""} {
set accessor [::nsf::dispatch [self] __default_accessor]
#puts stderr "CLASS [self] got default accessor ${accessor}"
}
set r [[self] ::nsf::classes::nx::Class::variable \
-accessor $accessor \
-incremental=$incremental \
-class $class \
-configurable $configurable \
-initblock $initblock \
{*}$spec]
return $r
}
######################################################################
# Define method "properties" for convenience to define multiple
# properties based on a list of parameter specifications.
######################################################################
#
#proc ::nx::internal::addProperties {arglist} {
# foreach arg $arglist {:property $arg}
#}
#::nx::ObjectParameterSlot create ::nx::Object::slot::properties \
# -methodname "::nx::internal::addProperties"
######################################################################
# Minimal definition of a value checker that permits every value
# without warnings. The primary purpose of this value checker is to
# provide a means to specify that the value can have every possible
# content and not to produce a warning when it might look like a
# non-positional parameter.
######################################################################
::nx::Slot method type=any {name value} { }
::nsf::method::property ::nx::Slot type=any call-protected true
######################################################################
# Now the slots are defined; now we can defines the Objects or
# classes with parameters more easily than above.
######################################################################
# remove helper proc
rename ::nx::createBootstrapVariableSlots ""
######################################################################
# Define a scoped "new" method, which is similar to plain new, but
# uses the current namespace by default as root of the object name.
######################################################################
Class create ::nx::NsScopedNew {
:public method new {-childof args} {
if {![info exists childof]} {
#
# Obtain the namespace from plain uplevel to honor the
# namespace provided by apply
#
set childof [uplevel {namespace current}]
}
#
# Use the uplevel method to assure that e.g. "... new -volatile ..."
# has the right scope
#
:uplevel [list [self] ::nsf::methods::class::new -childof $childof {*}$args]
}
}
######################################################################
# The method 'contains' changes the namespace in which objects with
# relative names are created. Therefore, 'contains' provides a
# friendly notation for creating nested object
# structures. Optionally, creating new objects in the specified
# scope can be turned off.
######################################################################
Object public method contains {
{-withnew:boolean true}
-object
{-class:class ::nx::Object}
cmds
} {
if {![info exists object]} {set object [::nsf::self]}
if {![::nsf::object::exists $object]} {$class create $object}
# This method is reused in XOTcl which has e.g. no "require";
# therefore use nsf primitiva.
::nsf::directdispatch $object ::nsf::methods::object::requirenamespace
if {$withnew} {
#
# When $withnew is requested we replace the default new method
# with a version using the current namespace as root. Earlier
# implementations used a mixin on nx::Class and xotcl::Class,
# but frequent mixin operations on the most general meta-class
# are expensive when there are many classes defined
# (e.g. several ten thousands), since the mixin operation
# invalidates the mixins for all instances of the meta-class
# (i.e. for all classes)
#
set infoMethod "::nsf::methods::class::info::method"
set plainNew "::nsf::methods::class::new"
set mappedNew [::nx::NsScopedNew $infoMethod definitionhandle new]
set nxMapNew [expr {[::nx::Class $infoMethod origin new] eq $plainNew}]
if {$nxMapNew} {::nsf::method::alias ::nx::Class new $mappedNew}
if {[::nsf::is class ::xotcl::Class]} {
set xotclMapNew [expr {[::xotcl::Class $infoMethod origin new] eq $plainNew}]
if {$xotclMapNew} {::nsf::method::alias ::xotcl::Class new $mappedNew }
}
#
# Evaluate the command under catch to ensure reverse mapping
# of "new"
#
set errorOccured [catch [list ::apply [list {} $cmds $object]] errorMsg]
#
# Remove the mapped "new" method, if it was added above
#
if {$nxMapNew} {::nsf::method::alias ::nx::Class new $plainNew}
if {[::nsf::is class ::xotcl::Class]} {
if {$xotclMapNew} {::nsf::method::alias ::xotcl::Class new $plainNew}
}
if {$errorOccured} {return -code error $errorMsg}
} else {
::apply [list {} $cmds $object]
}
}
######################################################################
# copy/move implementation
######################################################################
Class create ::nx::CopyHandler {
:property {targetList ""}
:property {dest ""}
:property objLength
:method makeTargetList {t} {
if {[::nsf::is object,type=::nx::EnsembleObject $t]} {
#
# we do not copy ensemble objects, since method
# introspection/recreation will care about these
#
return
}
lappend :targetList $t
#puts stderr "COPY makeTargetList $t targetList '${:targetList}'"
# if it is an object without namespace, it is a leaf
if {[::nsf::object::exists $t]} {
if {[::nsf::directdispatch $t ::nsf::methods::object::info::hasnamespace]} {
# make target list from all children
set children [$t info children]
} else {
# ok, no namespace -> no more children
return
}
}
# now append all namespaces that are in the obj, but that
# are not objects
foreach c [namespace children $t] {
if {![::nsf::object::exists $c]} {
lappend children [namespace children $t]
}
}
# a namespace or an obj with namespace may have children
# itself
foreach c $children {
:makeTargetList $c
}
}
# construct destination obj name from old qualified ns name
:method getDest {origin} {
if {${:dest} eq ""} {
return ""
} else {
set tail [string range $origin [set :objLength] end]
return ::[string trimleft [set :dest]$tail :]
}
}
:method copyTargets {} {
#puts stderr "COPY will copy targetList = [set :targetList]"
set objs {}
array set cmdMap {alias alias forward forward method create setter setter}
foreach origin [set :targetList] {
set dest [:getDest $origin]
if {[::nsf::object::exists $origin]} {
if {$dest eq ""} {
#set obj [[$origin info class] new -noinit]
set obj [::nsf::object::alloc [$origin info class] ""]
#nsf::object::property $obj initialized 1
set dest [set :dest $obj]
} else {
#
# Slot container are handled separately, since
# ::nx::slotObj does already the right thing. We have just
# to copy the variables (XOTcl keeps the parameter
# definitions there).
#
if {[::nsf::object::property $origin slotcontainer]} {
::nx::slotObj -container [namespace tail $origin] \
[namespace qualifiers $dest]
::nsf::nscopyvars $origin $dest
continue
} else {
#
# create an object without calling init
#
#set obj [[$origin info class] create $dest -noinit]
set obj [::nsf::object::alloc [$origin info class] $dest]
#nsf::object::property $obj initialized 1
#puts stderr "COPY obj=<$obj>"
}
}
# copy class information
if {[::nsf::is class $origin]} {
# obj is a class, copy class specific information
::nsf::relation::set $obj superclass [$origin ::nsf::methods::class::info::superclass]
::nsf::method::assertion $obj class-invar [::nsf::method::assertion $origin class-invar]
::nsf::relation::set $obj class-filter [::nsf::relation::get $origin class-filter]
::nsf::relation::set $obj class-mixin [::nsf::relation::get $origin class-mixin]
::nsf::nscopyvars ::nsf::classes$origin ::nsf::classes$dest
foreach m [$origin ::nsf::methods::class::info::methods -path -callprotection all] {
set rest [lassign [$origin ::nsf::methods::class::info::method definition $m] . protection what .]
# remove -returns from reported definitions
set p [lsearch -exact $rest -returns]; if {$p > -1} {set rest [lreplace $rest $p $p+1]}
set pathData [$obj eval [list :__resolve_method_path $m]]
set object [dict get $pathData object]
set r [::nsf::method::$cmdMap($what) $object [dict get $pathData methodName] {*}$rest]
::nsf::method::property $object $r returns [$origin ::nsf::methods::class::info::method returns $m]
::nsf::method::property $object $r call-protected [::nsf::method::property $origin $m call-protected]
::nsf::method::property $object $r call-private [::nsf::method::property $origin $m call-private]
}
}
# copy object -> might be a class obj
::nsf::object::property $obj keepcallerself [::nsf::object::property $origin keepcallerself]
::nsf::object::property $obj perobjectdispatch [::nsf::object::property $origin perobjectdispatch]
::nsf::object::property $obj hasperobjectslots [::nsf::object::property $origin hasperobjectslots]
::nsf::method::assertion $obj check [::nsf::method::assertion $origin check]
::nsf::method::assertion $obj object-invar [::nsf::method::assertion $origin object-invar]
::nsf::relation::set $obj object-filter [::nsf::relation::get $origin object-filter]
::nsf::relation::set $obj object-mixin [::nsf::relation::get $origin object-mixin]
# reused in XOTcl, no "require namespace" there, so use nsf primitiva
if {[::nsf::directdispatch $origin ::nsf::methods::object::info::hasnamespace]} {
::nsf::directdispatch $obj ::nsf::methods::object::requirenamespace
}
} else {
namespace eval $dest {}
}
lappend objs $obj
::nsf::nscopyvars $origin $dest
foreach m [$origin ::nsf::methods::object::info::methods -path -callprotection all] {
set rest [lassign [$origin ::nsf::methods::object::info::method definition $m] . protection . what .]
# remove -returns from reported definitions
set p [lsearch -exact $rest -returns]; if {$p > -1} {set rest [lreplace $rest $p $p+1]}
set pathData [$obj eval [list :__resolve_method_path -per-object $m]]
set object [dict get $pathData object]
set r [::nsf::method::$cmdMap($what) $object -per-object \
[dict get $pathData methodName] {*}$rest]
::nsf::method::property $object -per-object $r \
returns \
[$origin ::nsf::methods::object::info::method returns $m]
::nsf::method::property $object -per-object $r \
call-protected \
[::nsf::method::property $origin -per-object $m call-protected]
::nsf::method::property $object -per-object $r \
call-private \
[::nsf::method::property $origin -per-object $m call-private]
}
#
# transfer the traces
#
foreach var [$origin info vars] {
set cmds [::nsf::directdispatch $origin -frame object ::trace info variable $var]
#puts stderr "COPY $var <$cmds>"
if {$cmds ne ""} {
foreach cmd $cmds {
lassign $cmd op def
#$origin trace remove variable $var $op $def
set domain [lindex $def 0]
if {$domain eq $origin} {
set def [concat $dest [lrange $def 1 end]]
}
#puts stderr "COPY $var domain $domain [::nsf::object::exists $domain] && [$domain info has type ::nx::Slot]"
#if {[::nsf::object::exists $domain] && [$domain info has type ::nx::Slot]} {
# slot traces are handled already by the slot mechanism
#continue
#}
#
# handle the most common cases to replace $origin by $dest in trace command
#
if {[lindex $def 2] eq $origin} {
set def [lreplace $def 2 2 $dest]
} elseif {[lindex $def 0] eq $origin} {
set def [lreplace $def 0 0 $dest]
}
::nsf::directdispatch $dest -frame object ::trace add variable $var $op $def
}
}
}
}
#
# alter 'domain' and 'manager' in slot objects
#
foreach origin [set :targetList] {
set dest [:getDest $origin]
set slots [list]
#
# get class specific slots
#
if {[::nsf::is class $origin]} {
set slots [$origin ::nsf::methods::class::info::slotobjects -type ::nx::Slot]
}
#
# append object specific slots
#
foreach slot [$origin ::nsf::methods::object::info::slotobjects -type ::nx::Slot] {
lappend slots $slot
}
#puts stderr "replacing domain and manager from <$origin> to <$dest> in slots <$slots>"
foreach oldslot $slots {
set container [expr {[$oldslot cget -per-object] ? "per-object-slot" : "slot"}]
set newslot [::nx::slotObj -container $container $dest [namespace tail $oldslot]]
if {[$oldslot cget -domain] eq $origin} {$newslot configure -domain $dest}
if {[$oldslot cget -manager] eq $oldslot} {$newslot configure -manager $newslot}
$newslot eval :init
}
}
return [lindex $objs 0]
}
:public method copy {obj {dest ""}} {
#puts stderr "[::nsf::self] copy <$obj> <$dest>"
set :objLength [string length $obj]
set :dest $dest
:makeTargetList $obj
:copyTargets
}
}
Object public method copy {{newName ""}} {
if {[string trimleft $newName :] ne [string trimleft [::nsf::self] :]} {
set h [CopyHandler new]
set r [$h copy [::nsf::self] $newName]
$h destroy
return $r
}
}
Object public method move {newName} {
if {[string trimleft $newName :] ne [string trimleft [::nsf::self] :]} {
if {$newName ne ""} {
:copy $newName
}
### let all subclasses get the copied class as superclass
if {[::nsf::is class [::nsf::self]] && $newName ne ""} {
foreach subclass [: ::nsf::methods::class::info::subclass] {
set scl [$subclass ::nsf::methods::class::info::superclass]
if {[set index [lsearch -exact $scl [::nsf::self]]] != -1} {
set scl [lreplace $scl $index $index $newName]
::nsf::relation::set $subclass superclass $scl
}
}
}
:destroy
}
}
######################################################################
# Methods of meta-classes are methods intended for classes. Make
# sure, these methods are only applied on classes.
######################################################################
foreach m [Class info methods] {
::nsf::method::property Class $m class-only true
}
if {[info exists m]} {unset m}
######################################################################
# some utilities
######################################################################
#
# Provide mechanisms to configure nx
#
::nx::Object create ::nx::configure {
#
# Set the default method protection for nx methods. This
# protection level is used per default for all method definitions
# of scripted methods, aliases and forwarders without explicit
# protection specified.
#
:object method defaultMethodCallProtection {value:boolean,optional} {
if {[info exists value]} {
::nsf::method::create Object __default_method_call_protection args [list return $value]
::nsf::method::property Object __default_method_call_protection call-protected true
}
return [::nsf::dispatch [::nx::self] __default_method_call_protection]
}
#
# Set the default method accessor handling nx properties. The configured
# value is used for creating accessors for properties in nx.
#
:object method defaultAccessor {value:optional} {
if {[info exists value]} {
if {$value ni {"public" "protected" "private" "none"}} {
return -code error {defaultAccessor must be "public", "protected", "private" or "none"}
}
::nsf::method::create Object __default_accessor args [list return $value]
::nsf::method::property Object __default_accessor call-protected true
}
return [::nsf::dispatch [::nx::self] __default_accessor]
}
}
#
# Make the default protected methods
#
::nx::configure defaultMethodCallProtection true
::nx::configure defaultAccessor none
#
# Provide an ensemble-like interface to the ::nsf primitiva to
# access variables. Note that aliasing in the next scripting
# framework is faster than namespace-ensembles.
#
Object create ::nx::var {
:public object alias exists ::nsf::var::exists
:public object alias get ::nsf::var::get
:public object alias import ::nsf::var::import
:public object alias set ::nsf::var::set
}
#interp alias {} ::nx::self {} ::nsf::self
set value "add /class/|classes ?/pattern/?|clear|delete /class/|get|guard /class/ /?expr?/|set /class .../"
set "::nsf::parameter::syntax(::nx::Object::slot::__object::object mixins)" $value
set "::nsf::parameter::syntax(::nsf::classes::nx::Class::mixins)" $value
# set "::nsf::parameter::syntax(::nsf::classes::nx::Class::superclasses)" $value
set "::nsf::parameter::syntax(::nsf::classes::nx::Object::class)" "?/className/?"
set value "add /filter/|clear|delete /filter/|get|guard /filter/ ?/expr/?|methods ?/pattern/?|set /filter .../"
set "::nsf::parameter::syntax(::nx::Object::slot::__object::object filters)" $value
set "::nsf::parameter::syntax(::nsf::classes::nx::Class::filters)" $value
set "::nsf::parameter::syntax(::nsf::classes::nx::Object::eval)" "/arg/ ?/arg/ ...?"
unset value
::nsf::configure debug 1
}
namespace eval ::nx {
######################################################################
# Define exported Tcl commands
######################################################################
# export the main commands of ::nx
namespace export Object Class next self current
set ::nx::confdir ~/.nx
set ::nx::logdir $::nx::confdir/log
unset ::nsf::bootstrap
}
if {[info command ::lmap] eq ""} {
# provide a simple forward compatible version of Tcl 8.6's lmap
proc lmap {_var list body} {
upvar 1 $_var var
set res {}
foreach var $list {lappend res [uplevel 1 $body]}
return $res
}
}
#
# When debug is not deactivated, tell the developer, what happened
#
if {[::nsf::configure debug] > 1} {
foreach ns {::nsf ::nx} {
puts "vars of $ns: [info vars ${ns}::*]"
puts stderr "$ns exports: [namespace eval $ns {lsort [namespace export]}]"
}
puts stderr "======= nx loaded"
}
#
# Local variables:
# mode: tcl
# tcl-indent-level: 2
# indent-tabs-mode: nil
# End:
|