diff --git a/src/libgap-api.c b/src/libgap-api.c index af1c7b9866..200406d775 100644 --- a/src/libgap-api.c +++ b/src/libgap-api.c @@ -95,6 +95,17 @@ Obj GAP_ValueGlobalVariable(const char * name) } } +int GAP_CanAssignGlobalVariable(const char * name) +{ + UInt gvar = GVarName(name); + return !(IsReadOnlyGVar(gvar) || IsConstantGVar(gvar)); +} + +void GAP_AssignGlobalVariable(const char * name, Obj value) +{ + UInt gvar = GVarName(name); + AssGVar(gvar, value); +} //// //// arithmetic diff --git a/src/libgap-api.h b/src/libgap-api.h index a42c57e207..f5c064fb93 100644 --- a/src/libgap-api.h +++ b/src/libgap-api.h @@ -163,7 +163,7 @@ void GAP_Initialize(int argc, //// program evaluation and execution //// -// Evaluate a string of GAP commands +// Evaluate a string of GAP commands. // // To see an example of how to use this function see tst/testlibgap/basic.c // @@ -175,11 +175,18 @@ Obj GAP_EvalString(const char * cmd); //// variables //// -// Combines GVarName and ValGVar. For a given string, it returns the value -// of the gvar with name , or NULL if the global variable is not -// defined. +// Returns the value of the global GAP variable with name , or NULL if +// no global variable with this this name is defined. Obj GAP_ValueGlobalVariable(const char * name); +// Checks if assigning to the global GAP variable is possible, by +// verifying that is not the name of a read-only or constant variable. +int GAP_CanAssignGlobalVariable(const char * name); + +// Assign to the global GAP variable . If is the name of +// a readonly or constant variable, an error is raised. +void GAP_AssignGlobalVariable(const char * name, Obj value); + //// //// arithmetic @@ -278,7 +285,7 @@ int GAP_IsLargeInt(Obj obj); // `integer.c`). The absolute value of determines the number of limbs. // If is zero, then `INTOBJ_INT(0)` is returned. Otherwise, the sign // of the returned integer object is determined by the sign of . -// // +// // Note that GAP automatically reduces and normalized the integer object, // i.e., it will discard any leading zeros; and if the integer fits into a // small integer, it will be returned as such. diff --git a/tst/testlibgap/api.c b/tst/testlibgap/api.c index 9ddaa0875e..c7935357ed 100644 --- a/tst/testlibgap/api.c +++ b/tst/testlibgap/api.c @@ -138,6 +138,7 @@ void operations(void) void globalvars(void) { Obj a; + int x; a = GAP_ValueGlobalVariable("yaddayaddayadda"); assert(a == 0); @@ -145,6 +146,16 @@ void globalvars(void) // Hopefully this always exists. a = GAP_ValueGlobalVariable("GAPInfo"); assert(GAP_IsRecord(a) != 0); + + x = GAP_CanAssignGlobalVariable("GAPInfo"); + assert(x == 0); + + x = GAP_CanAssignGlobalVariable("GAPInfo_copy"); + assert(x != 0); + + GAP_AssignGlobalVariable("GAPInfo_copy", a); + a = GAP_ValueGlobalVariable("GAPInfo_copy"); + assert(a != 0); } int main(int argc, char ** argv)