Skip to content

Commit

Permalink
Add replace_global! helper (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin authored Aug 22, 2023
1 parent 2b1848a commit dfc1daa
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
7 changes: 7 additions & 0 deletions src/globals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ end

propertynames(::GlobalsType, private::Bool=false) = Vector{Symbol}(Globals.NamesGVars())

function replace_global!(name::Symbol, val::Any)
n = GapObj(name)
Wrappers.MakeReadWriteGlobal(n)
setproperty!(Globals, name, val)
Wrappers.MakeReadOnlyGlobal(n)
end

@static if VERSION < v"1.10-DEV"
# HACK to get tab completion to work for GAP globals accessed via GAP.Globals;
# e.g. if the REPL already shows `GAP.Globals.MTX.Is` and the user presses
Expand Down
8 changes: 3 additions & 5 deletions src/packages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module Packages

using Downloads
import ...GAP: Globals, GapObj, RNamObj, sysinfo, Wrappers
import ...GAP: Globals, GapObj, replace_global!, RNamObj, sysinfo, Wrappers

const DEFAULT_PKGDIR = Ref{String}()

Expand All @@ -17,16 +17,14 @@ function init_packagemanager()
global DEFAULT_PKGDIR[] = sysinfo["DEFAULT_PKGDIR"]

# overwrite PKGMAN_DownloadURL
Globals.MakeReadWriteGlobal(GapObj("PKGMAN_DownloadURL"))
Globals.PKGMAN_DownloadURL = function(url)
replace_global!(:PKGMAN_DownloadURL, function(url)
try
buffer = Downloads.download(String(url), IOBuffer())
return GapObj(Dict{Symbol, Any}(:success => true, :result => String(take!(buffer))), recursive=true)
catch
return GapObj(Dict{Symbol, Any}(:success => false), recursive=true)
end
end
Wrappers.MakeReadOnlyGlobal(GapObj("PKGMAN_DownloadURL"))
end)

# Install a method (based on Julia's Downloads package) as the first choice
# for the `Download` function from GAP's utils package,
Expand Down
8 changes: 2 additions & 6 deletions src/prompt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ function prompt()

# restore GAP's error output
disable_error_handler[] = true
Globals.MakeReadWriteGlobal(GapObj("ERROR_OUTPUT"))
Globals.ERROR_OUTPUT = Globals._JULIAINTERFACE_ORIGINAL_ERROR_OUTPUT
Globals.MakeReadOnlyGlobal(GapObj("ERROR_OUTPUT"))
replace_global!(:ERROR_OUTPUT, Globals._JULIAINTERFACE_ORIGINAL_ERROR_OUTPUT)

# enable break loop
Globals.BreakOnError = true
Expand All @@ -40,9 +38,7 @@ function prompt()

# restore GAP.jl error handler
disable_error_handler[] = false
Globals.MakeReadWriteGlobal(GapObj("ERROR_OUTPUT"))
Globals.ERROR_OUTPUT = Globals._JULIAINTERFACE_ERROR_OUTPUT
Globals.MakeReadOnlyGlobal(GapObj("ERROR_OUTPUT"))
replace_global!(:ERROR_OUTPUT, Globals._JULIAINTERFACE_ERROR_OUTPUT)
end

# helper function for `gap.sh` scripts created by create_gap_sh()
Expand Down
1 change: 1 addition & 0 deletions src/wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import GAP: @wrap
@wrap LQUO(x::Any, y::Any)::Any
@wrap LT(x::Any, y::Any)::Bool
@wrap MakeReadOnlyGlobal(x::Any)::Nothing
@wrap MakeReadWriteGlobal(x::Any)::Nothing
@wrap MOD(x::Any, y::Any)::Any
@wrap NextIterator(x::Any)::Any
@wrap NormalizedWhitespace(x::GapObj)::GapObj
Expand Down

0 comments on commit dfc1daa

Please sign in to comment.