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
|
/****************************************************************************
**
*W gvars.c GAP source Martin Schönert
**
**
*Y Copyright (C) 1996, Lehrstuhl D für Mathematik, RWTH Aachen, Germany
*Y (C) 1998 School Math and Comp. Sci., University of St Andrews, Scotland
*Y Copyright (C) 2002 The GAP Group
**
** This file contains the functions of the global variables package.
**
** The global variables package is the part of the kernel that manages
** global variables, i.e., the global namespace. A global variable binds an
** identifier to a value.
**
** A global variable can be automatic. That means that the global variable
** binds the identifier to a function and an argument. When the value of
** the global variable is needed, the function is called with the argument.
** This function call should, as a side-effect, execute an assignment of a
** value to the global variable, otherwise an error is signalled.
**
** A global variable can have a number of internal copies, i.e., C variables
** that always reference the same value as the global variable.
** It can also have a special type of internal copy (a fopy) only used for
** functions, where the internal copies
** only reference the same value as the global variable if it is a function.
** Otherwise the internal copies reference functions that signal an error.
*/
#include "system.h" /* Ints, UInts */
#include "gasman.h" /* garbage collector */
#include "objects.h" /* objects */
#include "scanner.h" /* scanner */
#include "gap.h" /* error handling, initialisation */
#include "code.h" /* coder */
#include "gvars.h" /* global variables */
#include "calls.h" /* generic call mechanism */
#include "records.h" /* generic records */
#include "precord.h" /* plain records */
#include "lists.h" /* generic lists */
#include "plist.h" /* plain lists */
#include "string.h" /* strings */
#include "bool.h" /* booleans */
#include "tls.h" /* thread-local storage */
#include "thread.h" /* threads */
#include "aobjects.h" /* atomic objects */
/****************************************************************************
**
*V ValGVars . . . . . . . . . . . . . . . . . . values of global variables
*V PtrGVars . . . . . . . . . . . . . pointer to values of global variables
**
** 'ValGVars' is the bag containing the values of the global variables.
**
** 'PtrGVars' is a pointer to the 'ValGVars' bag. This makes it faster to
** access global variables.
**
** Since a garbage collection may move this bag around, the pointer
** 'PtrGVars' must be revalculated afterwards. This should be done by a
** function in this package, but is still done in 'VarsAfterCollectBags'.
*/
Obj ValGVars;
Obj * PtrGVars;
/****************************************************************************
**
*F VAL_GVAR(<gvar>) . . . . . . . . . . . . . . . value of global variable
**
** 'VAL_GVAR' returns the value of the global variable <gvar>. If <gvar>
** has no assigned value, 'VAL_GVAR' returns 0. In this case <gvar> might
** be an automatic global variable, and one should call 'ValAutoGVar', which
** will return the value of <gvar> after evaluating <gvar>-s expression, or
** 0 if <gvar> was not an automatic variable.
**
** 'VAL_GVAR' is defined in the declaration part of this package as follows
**
#define VAL_GVAR(gvar) PtrGVars[ (gvar) ]
*/
/****************************************************************************
**
*V NameGVars . . . . . . . . . . . . . . . . . . . names of global variables
*V WriteGVars . . . . . . . . . . . . . writable flags of global variables
*V ExprGVars . . . . . . . . . . expressions for automatic global variables
*V CopiesGVars . . . . . . . . . . . . . internal copies of global variables
*V FopiesGVars . . . . . . . . internal function copies of global variables
*V CountGVars . . . . . . . . . . . . . . . . . number of global variables
*/
Obj NameGVars;
Obj WriteGVars;
Obj ExprGVars;
Obj CopiesGVars;
Obj FopiesGVars;
UInt CountGVars;
/****************************************************************************
**
*V TableGVars . . . . . . . . . . . . . . hashed table of global variables
*V SizeGVars . . . . . . . current size of hashed table of global variables
*/
Obj TableGVars;
UInt SizeGVars;
/****************************************************************************
**
*V ErrorMustEvalToFuncFunc . . . . . . . . . function that signals an error
*F ErrorMustEvalToFuncHandler(<self>,<args>) . handler that signals an error
**
** 'ErrorMustEvalToFuncFunc' is a (variable number of args) function that
** signals the error ``Function: <func> be a function''.
**
** 'ErrorMustEvalToFuncHandler' is the handler that signals the error
** ``Function: <func> must be a function''.
*/
Obj ErrorMustEvalToFuncFunc;
Obj ErrorMustEvalToFuncHandler (
Obj self,
Obj args )
{
ErrorQuit(
"Function Calls: <func> must be a function",
0L, 0L );
return 0;
}
/****************************************************************************
**
*V ErrorMustHaveAssObjFunc . . . . . . . . . function that signals an error
*F ErrorMustHaveAssObjHandler(<self>,<args>) . handler that signals an error
**
** 'ErrorMustHaveAssObjFunc' is a (variable number of args) function that
** signals the error ``Variable: <<unknown>> must have an assigned value''.
**
** 'ErrorMustHaveAssObjHandler' is the handler that signals the error
** ``Variable: <<unknown>> must have an assigned value''.
*/
Obj ErrorMustHaveAssObjFunc;
Obj ErrorMustHaveAssObjHandler (
Obj self,
Obj args )
{
ErrorQuit(
"Variable: <<unknown>> must have an assigned value",
0L, 0L );
return 0;
}
/****************************************************************************
**
*F AssGVar(<gvar>,<val>) . . . . . . . . . . . . assign to a global variable
**
** 'AssGVar' assigns the value <val> to the global variable <gvar>.
*/
static Obj REREADING; /* Copy of GAP global variable REREADING */
void AssGVar (
UInt gvar,
Obj val )
{
Obj cops; /* list of internal copies */
Obj * copy; /* one copy */
UInt i; /* loop variable */
Obj onam; /* object of <name> */
/* make certain that the variable is not read only */
while ( (REREADING != True) &&
(ELM_PLIST( WriteGVars, gvar ) == INTOBJ_INT(0)) ) {
ErrorReturnVoid(
"Variable: '%s' is read only",
(Int)CSTR_STRING( ELM_PLIST(NameGVars,gvar) ), 0L,
"you can 'return;' after making it writable" );
}
/* assign the value to the global variable */
VAL_GVAR(gvar) = val;
CHANGED_BAG( ValGVars );
/* if the global variable was automatic, convert it to normal */
SET_ELM_PLIST( ExprGVars, gvar, 0 );
/* assign the value to all the internal copies */
cops = ELM_PLIST( CopiesGVars, gvar );
if ( cops != 0 ) {
for ( i = 1; i <= LEN_PLIST(cops); i++ ) {
copy = (Obj*) ELM_PLIST(cops,i);
*copy = val;
}
}
/* if the value is a function, assign it to all the internal fopies */
cops = ELM_PLIST( FopiesGVars, gvar );
if ( cops != 0 && val != 0 && TNUM_OBJ(val) == T_FUNCTION ) {
for ( i = 1; i <= LEN_PLIST(cops); i++ ) {
copy = (Obj*) ELM_PLIST(cops,i);
*copy = val;
}
}
/* if the values is not a function, assign the error function */
else if ( cops != 0 && val != 0 /* && TNUM_OBJ(val) != T_FUNCTION */ ) {
for ( i = 1; i <= LEN_PLIST(cops); i++ ) {
copy = (Obj*) ELM_PLIST(cops,i);
*copy = ErrorMustEvalToFuncFunc;
}
}
/* if this was an unbind, assign the other error function */
else if ( cops != 0 /* && val == 0 */ ) {
for ( i = 1; i <= LEN_PLIST(cops); i++ ) {
copy = (Obj*) ELM_PLIST(cops,i);
*copy = ErrorMustHaveAssObjFunc;
}
}
/* assign name to a function */
if ( val != 0 && TNUM_OBJ(val) == T_FUNCTION && NAME_FUNC(val) == 0 ) {
onam = CopyToStringRep(NameGVarObj(gvar));
RESET_FILT_LIST( onam, FN_IS_MUTABLE );
NAME_FUNC(val) = onam;
CHANGED_BAG(val);
}
}
/****************************************************************************
**
*F ValAutoGVar(<gvar>) . . . . . . . . value of a automatic global variable
**
** 'ValAutoGVar' returns the value of the global variable <gvar>. This will
** be 0 if <gvar> has no assigned value. It will also cause a function
** call, if <gvar> is automatic.
*/
Obj ValAutoGVar (
UInt gvar )
{
Obj func; /* function to call for automatic */
Obj arg; /* argument to pass for automatic */
/* if this is an automatic variable, make the function call */
if ( VAL_GVAR(gvar) == 0 && ELM_PLIST( ExprGVars, gvar ) != 0 ) {
/* make the function call */
func = ELM_PLIST( ELM_PLIST( ExprGVars, gvar ), 1 );
arg = ELM_PLIST( ELM_PLIST( ExprGVars, gvar ), 2 );
CALL_1ARGS( func, arg );
/* if this is still an automatic variable, this is an error */
while ( VAL_GVAR(gvar) == 0 ) {
ErrorReturnVoid(
"Variable: automatic variable '%s' must get a value by function call",
(Int)CSTR_STRING( ELM_PLIST(NameGVars,gvar) ), 0L,
"you can 'return;' after assigning a value" );
}
}
/* return the value */
return VAL_GVAR(gvar);
}
/****************************************************************************
**
*F NameGVar(<gvar>) . . . . . . . . . . . . . . . name of a global variable
**
** 'NameGVar' returns the name of the global variable <gvar> as a C string.
*/
Char * NameGVar (
UInt gvar )
{
return CSTR_STRING( ELM_PLIST( NameGVars, gvar ) );
}
Obj NameGVarObj ( UInt gvar )
{
return ELM_PLIST( NameGVars, gvar );
}
#define NSCHAR '@'
Obj CurrNamespace = 0;
Obj FuncSET_NAMESPACE(Obj self, Obj str)
{
TLS(CurrNamespace) = str;
return 0;
}
Obj FuncGET_NAMESPACE(Obj self)
{
return TLS(CurrNamespace);
}
/****************************************************************************
**
*F GVarName(<name>) . . . . . . . . . . . . . . global variable for a name
**
** 'GVarName' returns the global variable with the name <name>.
*/
UInt GVarName (
const Char * name )
{
Obj gvar; /* global variable (as imm intval) */
Char gvarbuf[1024]; /* temporary copy for namespace */
Char * cns; /* Pointer to current namespace */
UInt pos; /* hash position */
Char namx [1024]; /* temporary copy of <name> */
Obj string; /* temporary string value <name> */
Obj table; /* temporary copy of <TableGVars> */
Obj gvar2; /* one element of <table> */
const Char * p; /* loop variable */
UInt i; /* loop variable */
Int len; /* length of name */
/* First see whether it could be namespace-local: */
cns = CSTR_STRING(TLS(CurrNamespace));
if (*cns) { /* only if a namespace is set */
len = strlen(name);
if (name[len-1] == NSCHAR) {
strlcpy(gvarbuf, name, 512);
strlcat(gvarbuf, cns, sizeof(gvarbuf));
name = gvarbuf;
}
}
/* start looking in the table at the following hash position */
pos = 0;
for ( p = name; *p != '\0'; p++ ) {
pos = 65599 * pos + *p;
}
pos = (pos % SizeGVars) + 1;
/* look through the table until we find a free slot or the global */
while ( (gvar = ELM_PLIST( TableGVars, pos )) != 0
&& strncmp( NameGVar( INT_INTOBJ(gvar) ), name, 1023 ) ) {
pos = (pos % SizeGVars) + 1;
}
/* if we did not find the global variable, make a new one and enter it */
/* (copy the name first, to avoid a stale pointer in case of a GC) */
if ( gvar == 0 ) {
CountGVars++;
gvar = INTOBJ_INT(CountGVars);
SET_ELM_PLIST( TableGVars, pos, gvar );
strlcpy(namx, name, sizeof(namx));
C_NEW_STRING_DYN(string, namx);
RESET_FILT_LIST( string, FN_IS_MUTABLE );
GROW_PLIST( ValGVars, CountGVars );
SET_LEN_PLIST( ValGVars, CountGVars );
SET_ELM_PLIST( ValGVars, CountGVars, 0 );
GROW_PLIST( NameGVars, CountGVars );
SET_LEN_PLIST( NameGVars, CountGVars );
SET_ELM_PLIST( NameGVars, CountGVars, string );
CHANGED_BAG( NameGVars );
GROW_PLIST( WriteGVars, CountGVars );
SET_LEN_PLIST( WriteGVars, CountGVars );
SET_ELM_PLIST( WriteGVars, CountGVars, INTOBJ_INT(1) );
GROW_PLIST( ExprGVars, CountGVars );
SET_LEN_PLIST( ExprGVars, CountGVars );
SET_ELM_PLIST( ExprGVars, CountGVars, 0 );
GROW_PLIST( CopiesGVars, CountGVars );
SET_LEN_PLIST( CopiesGVars, CountGVars );
SET_ELM_PLIST( CopiesGVars, CountGVars, 0 );
GROW_PLIST( FopiesGVars, CountGVars );
SET_LEN_PLIST( FopiesGVars, CountGVars );
SET_ELM_PLIST( FopiesGVars, CountGVars, 0 );
PtrGVars = ADDR_OBJ( ValGVars );
}
/* if the table is too crowed, make a larger one, rehash the names */
if ( SizeGVars < 3 * CountGVars / 2 ) {
table = TableGVars;
SizeGVars = 2 * SizeGVars + 1;
TableGVars = NEW_PLIST( T_PLIST, SizeGVars );
SET_LEN_PLIST( TableGVars, SizeGVars );
for ( i = 1; i <= (SizeGVars-1)/2; i++ ) {
gvar2 = ELM_PLIST( table, i );
if ( gvar2 == 0 ) continue;
pos = 0;
for ( p = NameGVar( INT_INTOBJ(gvar2) ); *p != '\0'; p++ ) {
pos = 65599 * pos + *p;
}
pos = (pos % SizeGVars) + 1;
while ( ELM_PLIST( TableGVars, pos ) != 0 ) {
pos = (pos % SizeGVars) + 1;
}
SET_ELM_PLIST( TableGVars, pos, gvar2 );
}
}
/* return the global variable */
return INT_INTOBJ(gvar);
}
/****************************************************************************
**
*V Tilde . . . . . . . . . . . . . . . . . . . . . . . . global variable '~'
**
** 'Tilde' is the global variable '~', the one used in expressions such as
** '[ [ 1, 2 ], ~[1] ]'.
**
** Actually when such expressions appear in functions, one should probably
** use a local variable. But for now this is good enough.
*/
UInt Tilde;
/****************************************************************************
**
*F MakeReadOnlyGVar( <gvar> ) . . . . . . make a global variable read only
*/
void MakeReadOnlyGVar (
UInt gvar )
{
SET_ELM_PLIST( WriteGVars, gvar, INTOBJ_INT(0) );
CHANGED_BAG(WriteGVars)
}
/****************************************************************************
**
*F MakeReadOnlyGVarHandler(<self>,<name>) make a global variable read only
**
** 'MakeReadOnlyGVarHandler' implements the function 'MakeReadOnlyGVar'.
**
** 'MakeReadOnlyGVar( <name> )'
**
** 'MakeReadOnlyGVar' make the global variable with the name <name> (which
** must be a GAP string) read only.
*/
Obj MakeReadOnlyGVarHandler (
Obj self,
Obj name )
{
/* check the argument */
while ( ! IsStringConv( name ) ) {
name = ErrorReturnObj(
"MakeReadOnlyGVar: <name> must be a string (not a %s)",
(Int)TNAM_OBJ(name), 0L,
"you can return a string for <name>" );
}
/* get the variable and make it read only */
MakeReadOnlyGVar(GVarName(CSTR_STRING(name)));
/* return void */
return 0;
}
/****************************************************************************
**
*F MakeReadWriteGVar( <gvar> ) . . . . . . make a global variable read write
*/
void MakeReadWriteGVar (
UInt gvar )
{
SET_ELM_PLIST( WriteGVars, gvar, INTOBJ_INT(1) );
CHANGED_BAG(WriteGVars)
}
/****************************************************************************
**
*F MakeReadWriteGVarHandler(<self>,<name>) make a global variable read write
**
** 'MakeReadWriteGVarHandler' implements the function 'MakeReadWriteGVar'.
**
** 'MakeReadWriteGVar( <name> )'
**
** 'MakeReadWriteGVar' make the global variable with the name <name> (which
** must be a GAP string) read and writable.
*/
Obj MakeReadWriteGVarHandler (
Obj self,
Obj name )
{
/* check the argument */
while ( ! IsStringConv( name ) ) {
name = ErrorReturnObj(
"MakeReadWriteGVar: <name> must be a string (not a %s)",
(Int)TNAM_OBJ(name), 0L,
"you can return a string for <name>" );
}
/* get the variable and make it read write */
MakeReadWriteGVar(GVarName(CSTR_STRING(name)));
/* return void */
return 0;
}
/****************************************************************************
**
*F IsReadOnlyGVar( <gvar> ) . . . . . . return status of a global variable
*/
Int IsReadOnlyGVar (
UInt gvar )
{
return !INT_INTOBJ(ELM_PLIST(WriteGVars, gvar));
}
/****************************************************************************
**
*F FuncIsReadOnlyGVar( <name> ) . . .handler for GAP function
**
*/
static Obj FuncIsReadOnlyGVar (
Obj self,
Obj name )
{
/* check the argument */
while ( ! IsStringConv( name ) ) {
name = ErrorReturnObj(
"IsReadOnlyGVar: <name> must be a string (not a %s)",
(Int)TNAM_OBJ(name), 0L,
"you can return a string for <name>" );
}
/* get the answer */
return IsReadOnlyGVar(GVarName(CSTR_STRING(name))) ? True : False;
}
/****************************************************************************
**
*F AUTOHandler() . . . . . . . . . . . . . make automatic global variables
**
** 'AUTOHandler' implements the internal function 'AUTO'.
**
** 'AUTO( <func>, <arg>, <name1>, ... )'
**
** 'AUTO' makes the global variables, whose names are given the strings
** <name1>, <name2>, ..., automatic. That means that when the value of one
** of those global variables is requested, then the function <func> is
** called and the argument <arg> is passed. This function call should,
** cause the execution of an assignment to that global variable, otherwise
** an error is signalled.
*/
Obj AUTOFunc;
Obj AUTOHandler (
Obj self,
Obj args )
{
Obj func; /* the function to call */
Obj arg; /* the argument to pass */
Obj list; /* function and argument list */
Obj name; /* one name (as a GAP string) */
UInt gvar; /* one global variable */
UInt i; /* loop variable */
/* check that there are enough arguments */
if ( LEN_LIST(args) < 2 ) {
ErrorQuit(
"usage: AUTO( <func>, <arg>, <name1>... )",
0L, 0L );
return 0;
}
/* get and check the function */
func = ELM_LIST( args, 1 );
while ( TNUM_OBJ(func) != T_FUNCTION ) {
func = ErrorReturnObj(
"AUTO: <func> must be a function (not a %s)",
(Int)TNAM_OBJ(func), 0L,
"you can return a function for <func>" );
}
/* get the argument */
arg = ELM_LIST( args, 2 );
/* make the list of function and argument */
list = NEW_PLIST( T_PLIST, 2 );
SET_LEN_PLIST( list, 2 );
SET_ELM_PLIST( list, 1, func );
SET_ELM_PLIST( list, 2, arg );
/* make the global variables automatic */
for ( i = 3; i <= LEN_LIST(args); i++ ) {
name = ELM_LIST( args, i );
while ( ! IsStringConv(name) ) {
name = ErrorReturnObj(
"AUTO: <name> must be a string (not a %s)",
(Int)TNAM_OBJ(name), 0L,
"you can return a string for <name>" );
}
gvar = GVarName( CSTR_STRING(name) );
SET_ELM_PLIST( ValGVars, gvar, 0 );
SET_ELM_PLIST( ExprGVars, gvar, list );
CHANGED_BAG( ExprGVars );
}
/* return void */
return 0;
}
/****************************************************************************
**
*F iscomplete( <name>, <len> ) . . . . . . . . find the completions of name
*F completion( <name>, <len> ) . . . . . . . . find the completions of name
*/
UInt iscomplete_gvar (
Char * name,
UInt len )
{
Char * curr;
UInt i, k;
for ( i = 1; i <= CountGVars; i++ ) {
curr = NameGVar( i );
for ( k = 0; name[k] != 0 && curr[k] == name[k]; k++ ) ;
if ( k == len && curr[k] == '\0' ) return 1;
}
return 0;
}
UInt completion_gvar (
Char * name,
UInt len )
{
Char * curr;
Char * next;
UInt i, k;
next = 0;
for ( i = 1; i <= CountGVars; i++ ) {
/* consider only variables which are currently bound for completion */
if ( VAL_GVAR( i ) || ELM_PLIST( ExprGVars, i )) {
curr = NameGVar( i );
for ( k = 0; name[k] != 0 && curr[k] == name[k]; k++ ) ;
if ( k < len || curr[k] <= name[k] ) continue;
if ( next != 0 ) {
for ( k = 0; curr[k] != '\0' && curr[k] == next[k]; k++ ) ;
if ( k < len || next[k] < curr[k] ) continue;
}
next = curr;
}
}
if ( next != 0 ) {
for ( k = 0; next[k] != '\0'; k++ )
name[k] = next[k];
name[k] = '\0';
}
return next != 0;
}
/****************************************************************************
**
*F FuncIDENTS_GVAR( <self> ) . . . . . . . . . . idents of global variables
*/
Obj FuncIDENTS_GVAR (
Obj self )
{
/*QQ extern Obj NameGVars; */
Obj copy;
UInt i;
copy = NEW_PLIST( T_PLIST+IMMUTABLE, LEN_PLIST(NameGVars) );
for ( i = 1; i <= LEN_PLIST(NameGVars); i++ ) {
SET_ELM_PLIST( copy, i, ELM_PLIST( NameGVars, i ) );
}
SET_LEN_PLIST( copy, LEN_PLIST(NameGVars) );
return copy;
}
Obj FuncIDENTS_BOUND_GVARS (
Obj self )
{
/*QQ extern Obj NameGVars; */
Obj copy;
UInt i, j;
copy = NEW_PLIST( T_PLIST+IMMUTABLE, LEN_PLIST(NameGVars) );
for ( i = 1, j = 1; i <= LEN_PLIST(NameGVars); i++ ) {
if ( VAL_GVAR( i ) || ELM_PLIST( ExprGVars, i )) {
SET_ELM_PLIST( copy, j, ELM_PLIST( NameGVars, i ) );
j++;
}
}
SET_LEN_PLIST( copy, j - 1 );
return copy;
}
/****************************************************************************
**
*F FuncASS_GVAR( <self>, <gvar>, <val> ) . . . . assign to a global variable
*/
Obj FuncASS_GVAR (
Obj self,
Obj gvar,
Obj val )
{
/* check the argument */
while ( ! IsStringConv( gvar ) ) {
gvar = ErrorReturnObj(
"READ: <gvar> must be a string (not a %s)",
(Int)TNAM_OBJ(gvar), 0L,
"you can return a string for <gvar>" );
}
AssGVar( GVarName( CSTR_STRING(gvar) ), val );
return 0L;
}
/****************************************************************************
**
*F FuncISB_GVAR( <self>, <gvar> ) . . check assignment of a global variable
*/
Obj FuncISB_GVAR (
Obj self,
Obj gvar )
{
UInt gv;
/* check the argument */
while ( ! IsStringConv( gvar ) ) {
gvar = ErrorReturnObj(
"ISB_GVAR: <gvar> must be a string (not a %s)",
(Int)TNAM_OBJ(gvar), 0L,
"you can return a string for <gvar>" );
}
gv = GVarName( CSTR_STRING(gvar) );
return ( VAL_GVAR( gv ) ||
ELM_PLIST( ExprGVars, gv )) ? True : False;
}
/****************************************************************************
**
*F FuncVAL_GVAR( <self>, <gvar> ) . . contents of a global variable
*/
Obj FuncVAL_GVAR (
Obj self,
Obj gvar )
{
Obj val;
/* check the argument */
while ( ! IsStringConv( gvar ) ) {
gvar = ErrorReturnObj(
"VAL_GVAR: <gvar> must be a string (not a %s)",
(Int)TNAM_OBJ(gvar), 0L,
"you can return a string for <gvar>" );
}
/* get the value */
val = ValAutoGVar( GVarName( CSTR_STRING(gvar) ) );
while (val == (Obj) 0)
val = ErrorReturnObj("VAL_GVAR: No value bound to %s",
(Int)CSTR_STRING(gvar), (Int) 0,
"you can return a value" );
return val;
}
/****************************************************************************
**
*F FuncUNB_GVAR( <self>, <gvar> ) . . unbind a global variable
*/
Obj FuncUNB_GVAR (
Obj self,
Obj gvar )
{
/* check the argument */
while ( ! IsStringConv( gvar ) ) {
gvar = ErrorReturnObj(
"UNB_GVAR: <gvar> must be a string (not a %s)",
(Int)TNAM_OBJ(gvar), 0L,
"you can return a string for <gvar>" );
}
/* */
AssGVar( GVarName( CSTR_STRING(gvar) ), (Obj)0 );
return (Obj) 0;
}
/****************************************************************************
**
*F * * * * * * * * * * * * * copies and fopies * * * * * * * * * * * * * * *
*/
/****************************************************************************
**
*V CopyAndFopyGVars . . . . . . kernel table of kernel copies and "fopies"
**
** This needs to be kept inside the kernel so that the copies can be updated
** after loading a workspace.
*/
typedef struct {
Obj * copy;
UInt isFopy;
const Char * name;
} StructCopyGVar;
#ifndef MAX_COPY_AND_FOPY_GVARS
#define MAX_COPY_AND_FOPY_GVARS 30000
#endif
static StructCopyGVar CopyAndFopyGVars[MAX_COPY_AND_FOPY_GVARS];
static Int NCopyAndFopyGVars;
/****************************************************************************
**
*F InitCopyGVar( <name>, <copy> ) . . declare C variable as copy of global
**
** 'InitCopyGVar' makes the C variable <cvar> at address <copy> a copy of
** the global variable named <name> (which must be a kernel string).
**
** The function only registers the information in <CopyAndFopyGVars>. At a
** latter stage one has to call 'UpdateCopyFopyInfo' to actually enter the
** information stored in <CopyAndFopyGVars> into a plain list.
**
** This is OK for garbage collection, but a real problem for saving in any
** event, this information does not really want to be saved because it is
** kernel centred rather than workspace centred.
**
** Accordingly we provide two functions `RemoveCopyFopyInfo' and
** `RestoreCopyFopyInfo' to remove or restore the information from the
** workspace. The Restore function is also intended to be used after
** loading a saved workspace
*/
void InitCopyGVar (
const Char * name ,
Obj * copy )
{
/* make a record in the kernel for saving and loading */
if ( NCopyAndFopyGVars >= MAX_COPY_AND_FOPY_GVARS ) {
Pr( "Panic, no room to record CopyGVar\n", 0L, 0L );
SyExit(1);
}
CopyAndFopyGVars[NCopyAndFopyGVars].copy = copy;
CopyAndFopyGVars[NCopyAndFopyGVars].isFopy = 0;
CopyAndFopyGVars[NCopyAndFopyGVars].name = name;
NCopyAndFopyGVars++;
}
/****************************************************************************
**
*F InitFopyGVar( <name>, <copy> ) . . declare C variable as copy of global
**
** 'InitFopyGVar' makes the C variable <cvar> at address <copy> a (function)
** copy of the global variable <gvar>, whose name is <name>. That means
** that whenever the value of <gvar> is a function, then <cvar> will
** reference the same value (i.e., will hold the same bag identifier). When
** the value of <gvar> is not a function, then <cvar> will reference a
** function that signals the error ``<func> must be a function''. When
** <gvar> has no assigned value, then <cvar> will reference a function that
** signals the error ``<gvar> must have an assigned value''.
*/
void InitFopyGVar (
const Char * name,
Obj * copy )
{
/* make a record in the kernel for saving and loading */
if ( NCopyAndFopyGVars >= MAX_COPY_AND_FOPY_GVARS ) {
Pr( "Panic, no room to record FopyGVar\n", 0L, 0L );
SyExit(1);
}
CopyAndFopyGVars[NCopyAndFopyGVars].copy = copy;
CopyAndFopyGVars[NCopyAndFopyGVars].isFopy = 1;
CopyAndFopyGVars[NCopyAndFopyGVars].name = name;
NCopyAndFopyGVars++;
}
/****************************************************************************
**
*F UpdateCopyFopyInfo() . . . . . . . . . . convert kernel info into plist
*/
static Int NCopyAndFopyDone;
void UpdateCopyFopyInfo ( void )
{
Obj cops; /* copies list */
UInt ncop; /* number of copies */
UInt gvar;
const Char * name; /* name of the variable */
Obj * copy; /* address of the copy */
/* loop over new copies and fopies */
for ( ; NCopyAndFopyDone < NCopyAndFopyGVars; NCopyAndFopyDone++ ) {
name = CopyAndFopyGVars[NCopyAndFopyDone].name;
copy = CopyAndFopyGVars[NCopyAndFopyDone].copy;
gvar = GVarName(name);
/* get the copies list and its length */
if ( CopyAndFopyGVars[NCopyAndFopyDone].isFopy ) {
if ( ELM_PLIST( FopiesGVars, gvar ) != 0 ) {
cops = ELM_PLIST( FopiesGVars, gvar );
}
else {
cops = NEW_PLIST( T_PLIST, 0 );
SET_ELM_PLIST( FopiesGVars, gvar, cops );
CHANGED_BAG(FopiesGVars);
}
}
else {
if ( ELM_PLIST( CopiesGVars, gvar ) != 0 ) {
cops = ELM_PLIST( CopiesGVars, gvar );
}
else {
cops = NEW_PLIST( T_PLIST, 0 );
SET_ELM_PLIST( CopiesGVars, gvar, cops );
CHANGED_BAG(CopiesGVars);
}
}
ncop = LEN_PLIST(cops);
/* append the copy to the copies list */
GROW_PLIST( cops, ncop+1 );
SET_LEN_PLIST( cops, ncop+1 );
SET_ELM_PLIST( cops, ncop+1, (Obj)copy );
CHANGED_BAG(cops);
/* now copy the value of <gvar> to <cvar> */
if ( CopyAndFopyGVars[NCopyAndFopyDone].isFopy ) {
if ( VAL_GVAR(gvar) != 0 && IS_FUNC(VAL_GVAR(gvar)) ) {
*copy = VAL_GVAR(gvar);
}
else if ( VAL_GVAR(gvar) != 0 ) {
*copy = ErrorMustEvalToFuncFunc;
}
else {
*copy = ErrorMustHaveAssObjFunc;
}
}
else {
*copy = VAL_GVAR(gvar);
}
}
}
/****************************************************************************
**
*F RemoveCopyFopyInfo() . . . remove the info about copies of gvars from ws
*/
void RemoveCopyFopyInfo( void )
{
UInt i, l;
l = LEN_PLIST(CopiesGVars);
for ( i = 1; i <= l; i++ )
SET_ELM_PLIST( CopiesGVars, i, 0 );
l = LEN_PLIST(FopiesGVars);
for ( i = 1; i <= l; i++ )
SET_ELM_PLIST( FopiesGVars, i, 0 );
NCopyAndFopyDone = 0;
return;
}
/****************************************************************************
**
*F RestoreCopyFopyInfo() . . . restore the info from the copy in the kernel
*/
void RestoreCopyFopyInfo( void )
{
NCopyAndFopyDone = 0;
UpdateCopyFopyInfo();
}
/****************************************************************************
**
*F * * * * * * * * * * * * * initialize package * * * * * * * * * * * * * * *
*/
/****************************************************************************
**
*V GVarFuncs . . . . . . . . . . . . . . . . . . list of functions to export
*/
static StructGVarFunc GVarFuncs [] = {
{ "MakeReadOnlyGVar", 1, "name",
MakeReadOnlyGVarHandler, "src/gap.c:MakeReadOnlyGVar" },
{ "MakeReadWriteGVar", 1, "name",
MakeReadWriteGVarHandler, "src/gap.c:MakeReadWriteGVar" },
{ "IsReadOnlyGVar", 1, "name",
FuncIsReadOnlyGVar, "src/gap.c:IsReadOnlyGVar" },
{ "AUTO", -1, "args",
AUTOHandler, "src/gap.c:AUTO" },
{ "IDENTS_GVAR", 0L, "",
FuncIDENTS_GVAR, "src/gap.c:IDENTS_GVAR" },
{ "IDENTS_BOUND_GVARS", 0L, "",
FuncIDENTS_BOUND_GVARS, "src/gap.c:IDENTS_BOUND_GVARS" },
{ "ISB_GVAR", 1L, "gvar",
FuncISB_GVAR, "src/gap.c:ISB_GVAR" },
{ "ASS_GVAR", 2L, "gvar, value",
FuncASS_GVAR, "src/gap.c:ASS_GVAR" },
{ "VAL_GVAR", 1L, "gvar",
FuncVAL_GVAR, "src/gap.c:VAL_GVAR" },
{ "UNB_GVAR", 1L, "gvar",
FuncUNB_GVAR, "src/gap.c:UNB_GVAR" },
{ "SET_NAMESPACE", 1L, "str",
FuncSET_NAMESPACE, "src/gvars.c:SET_NAMESPACE" },
{ "GET_NAMESPACE", 0L, "",
FuncGET_NAMESPACE, "src/gvars.c:GET_NAMESPACE" },
{ 0 }
};
/****************************************************************************
**
*F InitKernel( <module> ) . . . . . . . . initialise kernel data structures
*/
static Int InitKernel (
StructInitInfo * module )
{
ValGVars = (Obj) 0;
NCopyAndFopyGVars = 0;
NCopyAndFopyDone = 0;
InitHandlerRegistration();
/* init global bags and handler */
InitGlobalBag( &ErrorMustEvalToFuncFunc,
"src/gvars.c:ErrorMustEvalToFuncFunc" );
InitGlobalBag( &ErrorMustHaveAssObjFunc,
"src/gvars.c:ErrorMustHaveAssObjFunc" );
InitGlobalBag( &ValGVars,
"src/gvars.c:ValGVars" );
InitGlobalBag( &NameGVars,
"src/gvars.c:NameGVars" );
InitGlobalBag( &WriteGVars,
"src/gvars.c:WriteGVars" );
InitGlobalBag( &ExprGVars,
"src/gvars.c:ExprGVars" );
InitGlobalBag( &CopiesGVars,
"src/gvars.c:CopiesGVars" );
InitGlobalBag( &FopiesGVars,
"src/gvars.c:FopiesGVars" );
InitGlobalBag( &TableGVars,
"src/gvars.c:TableGVars" );
InitGlobalBag( &CurrNamespace,
"src/gvars.c:CurrNamespace" );
InitHandlerFunc( ErrorMustEvalToFuncHandler,
"src/gvars.c:ErrorMustEvalToFuncHandler" );
InitHandlerFunc( ErrorMustHaveAssObjHandler,
"src/gvars.c:ErrorMustHaveAssObjHandler" );
/* init filters and functions */
InitHdlrFuncsFromTable( GVarFuncs );
/* Get a copy of REREADING */
ImportGVarFromLibrary("REREADING", &REREADING);
/* return success */
return 0;
}
/****************************************************************************
**
*F PostRestore( <module> ) . . . . . . . . . . . . . after restore workspace
*/
static Int PostRestore (
StructInitInfo * module )
{
/* make the lists for global variables */
CountGVars = LEN_PLIST( ValGVars );
PtrGVars = ADDR_OBJ( ValGVars );
SizeGVars = LEN_PLIST( TableGVars );
/* create the global variable '~' */
Tilde = GVarName( "~" );
/* update fopies and copies */
UpdateCopyFopyInfo();
/* return success */
return 0;
}
/****************************************************************************
**
*F PreSave( <module> ) . . . . . . . . . . . . . before save workspace
*/
static Int PreSave (
StructInitInfo * module )
{
RemoveCopyFopyInfo();
return 0;
}
/****************************************************************************
**
*F PostSave( <module> ) . . . . . . . . . . . . . aftersave workspace
*/
static Int PostSave (
StructInitInfo * module )
{
UpdateCopyFopyInfo();
return 0;
}
/****************************************************************************
**
*F InitLibrary( <module> ) . . . . . . . initialise library data structures
*/
static Int InitLibrary (
StructInitInfo * module )
{
/* make the error functions for 'AssGVar' */
ErrorMustEvalToFuncFunc = NewFunctionC(
"ErrorMustEvalToFunc", -1,"args", ErrorMustEvalToFuncHandler );
ErrorMustHaveAssObjFunc = NewFunctionC(
"ErrorMustHaveAssObj", -1L,"args", ErrorMustHaveAssObjHandler );
/* make the lists for global variables */
ValGVars = NEW_PLIST( T_PLIST, 0 );
SET_LEN_PLIST( ValGVars, 0 );
NameGVars = NEW_PLIST( T_PLIST, 0 );
SET_LEN_PLIST( NameGVars, 0 );
WriteGVars = NEW_PLIST( T_PLIST, 0 );
SET_LEN_PLIST( WriteGVars, 0 );
ExprGVars = NEW_PLIST( T_PLIST, 0 );
SET_LEN_PLIST( ExprGVars, 0 );
CopiesGVars = NEW_PLIST( T_PLIST, 0 );
SET_LEN_PLIST( CopiesGVars, 0 );
FopiesGVars = NEW_PLIST( T_PLIST, 0 );
SET_LEN_PLIST( FopiesGVars, 0 );
/* make the list of global variables */
SizeGVars = 997;
TableGVars = NEW_PLIST( T_PLIST, SizeGVars );
SET_LEN_PLIST( TableGVars, SizeGVars );
/* Create the current namespace: */
TLS(CurrNamespace) = NEW_STRING(0);
SET_LEN_STRING(TLS(CurrNamespace),0);
/* fix C vars */
PostRestore( module );
/* init filters and functions */
InitGVarFuncsFromTable( GVarFuncs );
/* return success */
return 0;
}
/****************************************************************************
**
*F CheckInit( <module> ) . . . . . . . . . . . . . . . check initialisation
*/
static Int CheckInit (
StructInitInfo * module )
{
Int success = 1;
if ( NCopyAndFopyGVars != NCopyAndFopyDone ) {
success = 0;
Pr( "#W failed to updated copies and fopies\n", 0L, 0L );
}
/* return success */
return ! success;
}
/****************************************************************************
**
*F InitInfoGVars() . . . . . . . . . . . . . . . . . table of init functions
*/
static StructInitInfo module = {
MODULE_BUILTIN, /* type */
"gvars", /* name */
0, /* revision entry of c file */
0, /* revision entry of h file */
0, /* version */
0, /* crc */
InitKernel, /* initKernel */
InitLibrary, /* initLibrary */
CheckInit, /* checkInit */
PreSave, /* preSave */
PostSave, /* postSave */
PostRestore /* postRestore */
};
StructInitInfo * InitInfoGVars ( void )
{
return &module;
}
/****************************************************************************
**
*E gvars.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . ends here
*/
|