From c09bcf271262972cc291561c7f42098ae4686129 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 12 Dec 2024 15:44:52 +0100 Subject: [PATCH] GAP: add and use ReplaceBinding helper This is a variant of Oscar's `ReplaceGapFunc` but I figure it is be useful for other code. --- gap/exec.g | 5 +---- gap/systemfile.g | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/gap/exec.g b/gap/exec.g index 74080892..77453b1d 100644 --- a/gap/exec.g +++ b/gap/exec.g @@ -1,4 +1 @@ -BindGlobal("_ORIG_ExecuteProcess", ExecuteProcess); -MakeReadWriteGlobal("ExecuteProcess"); -ExecuteProcess := Julia.GAP.GAP_ExecuteProcess; -MakeReadOnlyGlobal("ExecuteProcess"); +ReplaceBinding("ExecuteProcess", Julia.GAP.GAP_ExecuteProcess); diff --git a/gap/systemfile.g b/gap/systemfile.g index d66fe66e..c99221dc 100644 --- a/gap/systemfile.g +++ b/gap/systemfile.g @@ -1,3 +1,20 @@ +# Helper for replacing a GAP global variable or function by a new value. +# The original value is retained under the name `_ORIG_***` +BIND_GLOBAL("ReplaceBinding", function(name, val) + local orig_name; + # Store a copy of the original value, but only if that copy does not yet + # exist. This ensures we don't overwrite it during a call to `Reread`. + orig_name := "_ORIG_"; + APPEND_LIST_INTR(orig_name, name); + if not ISB_GVAR(orig_name) then + ASS_GVAR(orig_name, VAL_GVAR(name)); + MakeReadOnlyGVar(orig_name); + fi; + MakeReadWriteGVar(name); + ASS_GVAR(name, val); + MakeReadOnlyGVar(name); +end); + (function() local deps;