Skip to content

Commit

Permalink
Add 'IsAutoGlobal'
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisJefferson authored and markuspf committed Dec 5, 2018
1 parent 78832e1 commit 4ee0c13
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/global.gd
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ DeclareGlobalFunction("ValueGlobal");
##
DeclareGlobalFunction("IsBoundGlobal");

#############################################################################
##
#F IsAutoGlobal( <name> ) . . . . . check if a global is automatic
##
## <ManSection>
## <Func Name="IsAutoGlobal" Arg='name'/>
##
## <Description>
## IsAutoGlobal ( <A>name</A> ) returns true if there is a global variable
## named <A>name</A>, declared using
## <Ref Func="DeclareAutoreadableVariables"/>, which has not yet been
## accessed. This means reading this variable's value will cause code
## to be executed.
## </Description>
## </ManSection>
##
DeclareGlobalFunction("IsAutoGlobal");

#############################################################################
##
Expand Down
15 changes: 15 additions & 0 deletions lib/global.gi
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ InstallGlobalFunction( IsBoundGlobal,
end);


#############################################################################
##
#M IsAutoGlobal ( <name> ) . . . . check if a global is automatic
##


InstallGlobalFunction( IsAutoGlobal,
function (name)
local isauto;
CheckGlobalName( name );
isauto := IS_AUTO_GVAR(name);
Info( InfoGlobal, 3,
"IsAutoGlobal: called for ", name, " returned ", isauto);
return isauto;
end);

#############################################################################
##
Expand Down
16 changes: 16 additions & 0 deletions src/gvars.c
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,21 @@ Obj FuncISB_GVAR (
#endif
}

/****************************************************************************
**
*F FuncIS_AUTO_GVAR( <self>, <gvar> ) . . check if a global variable is auto
*/

Obj FuncIS_AUTO_GVAR (
Obj self,
Obj gvar )
{
// check the argument
RequireStringRep("IS_AUTO_GVAR", gvar);
Obj expr = ExprGVar(GVarName( CONST_CSTR_STRING(gvar) ) );
return (expr && !IS_INTOBJ(expr)) ? True : False;
}


/****************************************************************************
**
Expand Down Expand Up @@ -1493,6 +1508,7 @@ static StructGVarFunc GVarFuncs[] = {
GVAR_FUNC(IDENTS_GVAR, 0, ""),
GVAR_FUNC(IDENTS_BOUND_GVARS, 0, ""),
GVAR_FUNC(ISB_GVAR, 1, "gvar"),
GVAR_FUNC(IS_AUTO_GVAR, 1, "gvar"),
GVAR_FUNC(ASS_GVAR, 2, "gvar, value"),
GVAR_FUNC(VAL_GVAR, 1, "gvar"),
GVAR_FUNC(UNB_GVAR, 1, "gvar"),
Expand Down
33 changes: 33 additions & 0 deletions tst/testinstall/auto.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
gap> IsAutoGlobal("testglobalvar");
false
gap> g := function(x) Print("Checkg\n"); end;;
gap> f := function(x) Print("Checkf\n"); testglobalvar := 3; end;;
Syntax warning: Unbound global variable in stream:1
f := function(x) Print("Checkf\n"); testglobalvar := 3; end;;
^^
gap> AUTO(g, 1, "testglobalvar");
gap> IsAutoGlobal("testglobalvar");
true
gap> testglobalvar;
Checkg
Error, Variable: automatic variable 'testglobalvar' must get a value by functi\
on call
gap> AUTO(f, 1, "testglobalvar");
gap> IsAutoGlobal("testglobalvar");
true
gap> testglobalvar;
Checkf
3
gap> IsAutoGlobal("testglobalvar");
false
gap> testglobalvar;
3
gap> Unbind(testglobalvar);
gap> IsAutoGlobal("testglobalvar");
false
#@if IsHPCGAP
gap> threadlocalvar := "test";;
gap> MakeThreadLocal("threadlocalvar");;
gap> IsAutoGlobal("threadlocalvar");
false
#@fi

0 comments on commit 4ee0c13

Please sign in to comment.