Skip to content

Commit 77effa3

Browse files
authored
use isdefinedglobal instead of isdefined(::Module, ::Symbol) (#681)
1 parent 0c37ff5 commit 77effa3

File tree

11 files changed

+73
-65
lines changed

11 files changed

+73
-65
lines changed

bin/generate_builtins.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function scopedname(f)
6161
tn = typeof(f).name
6262
Base.isexported(tn.module, Symbol(fstr)) && return fstr
6363
fsym = Symbol(fstr)
64-
isdefined(tn.module, fsym) && return string(tn.module) * '.' * fstr
64+
isdefinedglobal(tn.module, fsym) && return string(tn.module) * '.' * fstr
6565
return "Base." * fstr
6666
end
6767

@@ -75,7 +75,7 @@ function nargs(f, table, id)
7575
end
7676
# Specialize ~arrayref and arrayset~ memoryrefnew for small numbers of arguments
7777
# TODO: how about other memory intrinsics?
78-
if (@static isdefined(Core, :memoryrefnew) ? f == Core.memoryrefnew : f == Core.memoryref)
78+
if (@static isdefinedglobal(Core, :memoryrefnew) ? f == Core.memoryrefnew : f == Core.memoryref)
7979
maxarg = 5
8080
end
8181
return minarg, maxarg
@@ -249,7 +249,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
249249
elseif f === Core.current_scope
250250
print(io,
251251
"""
252-
elseif @static isdefined(Core, :current_scope) && f === Core.current_scope
252+
elseif @static isdefinedglobal(Core, :current_scope) && f === Core.current_scope
253253
if nargs == 0
254254
currscope = Core.current_scope()
255255
for scope in frame.framedata.current_scopes
@@ -271,7 +271,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
271271
end
272272
print(io,
273273
"""
274-
$head @static isdefined($(ft.name.module), $(repr(nameof(f)))) && f === $fname
274+
$head @static isdefinedglobal($(ft.name.module), $(repr(nameof(f)))) && f === $fname
275275
$fcall
276276
""")
277277
else
@@ -326,7 +326,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
326326
rname = repr(name)
327327
print(io,
328328
"""
329-
elseif @static (isdefined($mod, $rname) && $_scopedname isa Core.Builtin) && f === $_scopedname
329+
elseif @static (isdefinedglobal($mod, $rname) && $_scopedname isa Core.Builtin) && f === $_scopedname
330330
$fcall
331331
""")
332332
end
@@ -335,7 +335,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
335335
minmin, maxmax = typemax(Int), 0
336336
for fsym in names(Core.Intrinsics)
337337
fsym === :Intrinsics && continue
338-
isdefined(Base, fsym) || continue
338+
isdefinedglobal(Base, fsym) || continue
339339
f = getfield(Base, fsym)
340340
id = reinterpret(Int32, f) + 1
341341
minarg, maxarg = nargs(f, Core.Compiler.T_IFUNC, id)

src/builtins.jl

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
8686
return Some{Any}(Core._call_in_world_total(getargs(args, frame)...))
8787
elseif f === Core._compute_sparams
8888
return Some{Any}(Core._compute_sparams(getargs(args, frame)...))
89-
elseif @static isdefined(Core, :_defaultctors) && f === Core._defaultctors
89+
elseif @static isdefinedglobal(Core, :_defaultctors) && f === Core._defaultctors
9090
return Some{Any}(Core._defaultctors(getargs(args, frame)...))
9191
elseif f === Core._equiv_typedef
9292
return Some{Any}(Core._equiv_typedef(getargs(args, frame)...))
@@ -120,7 +120,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
120120
else
121121
return Some{Any}(Core.compilerbarrier(getargs(args, frame)...))
122122
end
123-
elseif @static isdefined(Core, :current_scope) && f === Core.current_scope
123+
elseif @static isdefinedglobal(Core, :current_scope) && f === Core.current_scope
124124
if nargs == 0
125125
currscope = Core.current_scope()
126126
for scope in frame.framedata.current_scopes
@@ -154,33 +154,33 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
154154
else
155155
return Some{Any}(Core.ifelse(getargs(args, frame)...))
156156
end
157-
elseif @static isdefined(Core, :invoke_in_world) && f === Core.invoke_in_world
157+
elseif @static isdefinedglobal(Core, :invoke_in_world) && f === Core.invoke_in_world
158158
return Some{Any}(Core.invoke_in_world(getargs(args, frame)...))
159-
elseif @static isdefined(Core, :memorynew) && f === Core.memorynew
159+
elseif @static isdefinedglobal(Core, :memorynew) && f === Core.memorynew
160160
if nargs == 2
161161
return Some{Any}(Core.memorynew(@lookup(frame, args[2]), @lookup(frame, args[3])))
162162
else
163163
return Some{Any}(Core.memorynew(getargs(args, frame)...))
164164
end
165-
elseif @static isdefined(Core, :memoryref_isassigned) && f === Core.memoryref_isassigned
165+
elseif @static isdefinedglobal(Core, :memoryref_isassigned) && f === Core.memoryref_isassigned
166166
if nargs == 3
167167
return Some{Any}(Core.memoryref_isassigned(@lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4])))
168168
else
169169
return Some{Any}(Core.memoryref_isassigned(getargs(args, frame)...))
170170
end
171-
elseif @static isdefined(Core, :memoryrefget) && f === Core.memoryrefget
171+
elseif @static isdefinedglobal(Core, :memoryrefget) && f === Core.memoryrefget
172172
if nargs == 3
173173
return Some{Any}(Core.memoryrefget(@lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4])))
174174
else
175175
return Some{Any}(Core.memoryrefget(getargs(args, frame)...))
176176
end
177-
elseif @static isdefined(Core, :memoryrefmodify!) && f === Core.memoryrefmodify!
177+
elseif @static isdefinedglobal(Core, :memoryrefmodify!) && f === Core.memoryrefmodify!
178178
if nargs == 5
179179
return Some{Any}(Core.memoryrefmodify!(@lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4]), @lookup(frame, args[5]), @lookup(frame, args[6])))
180180
else
181181
return Some{Any}(Core.memoryrefmodify!(getargs(args, frame)...))
182182
end
183-
elseif @static isdefined(Core, :memoryrefnew) && f === Core.memoryrefnew
183+
elseif @static isdefinedglobal(Core, :memoryrefnew) && f === Core.memoryrefnew
184184
if nargs == 1
185185
return Some{Any}(Core.memoryrefnew(@lookup(frame, args[2])))
186186
elseif nargs == 2
@@ -194,31 +194,31 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
194194
else
195195
return Some{Any}(Core.memoryrefnew(getargs(args, frame)...))
196196
end
197-
elseif @static isdefined(Core, :memoryrefoffset) && f === Core.memoryrefoffset
197+
elseif @static isdefinedglobal(Core, :memoryrefoffset) && f === Core.memoryrefoffset
198198
if nargs == 1
199199
return Some{Any}(Core.memoryrefoffset(@lookup(frame, args[2])))
200200
else
201201
return Some{Any}(Core.memoryrefoffset(getargs(args, frame)...))
202202
end
203-
elseif @static isdefined(Core, :memoryrefreplace!) && f === Core.memoryrefreplace!
203+
elseif @static isdefinedglobal(Core, :memoryrefreplace!) && f === Core.memoryrefreplace!
204204
if nargs == 6
205205
return Some{Any}(Core.memoryrefreplace!(@lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4]), @lookup(frame, args[5]), @lookup(frame, args[6]), @lookup(frame, args[7])))
206206
else
207207
return Some{Any}(Core.memoryrefreplace!(getargs(args, frame)...))
208208
end
209-
elseif @static isdefined(Core, :memoryrefset!) && f === Core.memoryrefset!
209+
elseif @static isdefinedglobal(Core, :memoryrefset!) && f === Core.memoryrefset!
210210
if nargs == 4
211211
return Some{Any}(Core.memoryrefset!(@lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4]), @lookup(frame, args[5])))
212212
else
213213
return Some{Any}(Core.memoryrefset!(getargs(args, frame)...))
214214
end
215-
elseif @static isdefined(Core, :memoryrefsetonce!) && f === Core.memoryrefsetonce!
215+
elseif @static isdefinedglobal(Core, :memoryrefsetonce!) && f === Core.memoryrefsetonce!
216216
if nargs == 5
217217
return Some{Any}(Core.memoryrefsetonce!(@lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4]), @lookup(frame, args[5]), @lookup(frame, args[6])))
218218
else
219219
return Some{Any}(Core.memoryrefsetonce!(getargs(args, frame)...))
220220
end
221-
elseif @static isdefined(Core, :memoryrefswap!) && f === Core.memoryrefswap!
221+
elseif @static isdefinedglobal(Core, :memoryrefswap!) && f === Core.memoryrefswap!
222222
if nargs == 4
223223
return Some{Any}(Core.memoryrefswap!(@lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4]), @lookup(frame, args[5])))
224224
else
@@ -232,7 +232,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
232232
end
233233
elseif f === Core.svec
234234
return Some{Any}(Core.svec(getargs(args, frame)...))
235-
elseif @static isdefined(Core, :throw_methoderror) && f === Core.throw_methoderror
235+
elseif @static isdefinedglobal(Core, :throw_methoderror) && f === Core.throw_methoderror
236236
return Some{Any}(Core.throw_methoderror(getargs(args, frame)...))
237237
elseif f === applicable
238238
return Some{Any}(applicable(getargs(args, frame)...))
@@ -270,7 +270,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
270270
# This uses the original arguments to avoid looking them up twice
271271
# See #442
272272
return Expr(:call, invoke, args[2:end]...)
273-
elseif @static isdefined(Core, :invokelatest) && f === Core.invokelatest
273+
elseif @static isdefinedglobal(Core, :invokelatest) && f === Core.invokelatest
274274
args = getargs(args, frame)
275275
if !expand
276276
return Some{Any}(Core.invokelatest(args...))
@@ -296,7 +296,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
296296
else
297297
return Some{Any}(isdefined(getargs(args, frame)...))
298298
end
299-
elseif @static isdefined(Core, :isdefinedglobal) && f === isdefinedglobal
299+
elseif @static isdefinedglobal(Core, :isdefinedglobal) && f === isdefinedglobal
300300
return Some{Any}(isdefinedglobal(getargs(args, frame)...))
301301
elseif f === modifyfield!
302302
if nargs == 4
@@ -306,7 +306,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
306306
else
307307
return Some{Any}(modifyfield!(getargs(args, frame)...))
308308
end
309-
elseif @static isdefined(Core, :modifyglobal!) && f === modifyglobal!
309+
elseif @static isdefinedglobal(Core, :modifyglobal!) && f === modifyglobal!
310310
if nargs == 4
311311
return Some{Any}(Base.invoke_in_world(frame.world, modifyglobal!, @lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4]), @lookup(frame, args[5])))
312312
elseif nargs == 5
@@ -330,7 +330,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
330330
else
331331
return Some{Any}(replacefield!(getargs(args, frame)...))
332332
end
333-
elseif @static isdefined(Core, :replaceglobal!) && f === replaceglobal!
333+
elseif @static isdefinedglobal(Core, :replaceglobal!) && f === replaceglobal!
334334
if nargs == 4
335335
return Some{Any}(Base.invoke_in_world(frame.world, replaceglobal!, @lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4]), @lookup(frame, args[5])))
336336
elseif nargs == 5
@@ -348,7 +348,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
348348
else
349349
return Some{Any}(setfield!(getargs(args, frame)...))
350350
end
351-
elseif @static isdefined(Core, :setfieldonce!) && f === setfieldonce!
351+
elseif @static isdefinedglobal(Core, :setfieldonce!) && f === setfieldonce!
352352
if nargs == 3
353353
return Some{Any}(setfieldonce!(@lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4])))
354354
elseif nargs == 4
@@ -366,7 +366,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
366366
else
367367
return Some{Any}(Base.invoke_in_world(frame.world, setglobal!, getargs(args, frame)...))
368368
end
369-
elseif @static isdefined(Core, :setglobalonce!) && f === setglobalonce!
369+
elseif @static isdefinedglobal(Core, :setglobalonce!) && f === setglobalonce!
370370
if nargs == 3
371371
return Some{Any}(Base.invoke_in_world(frame.world, setglobalonce!, @lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4])))
372372
elseif nargs == 4
@@ -384,7 +384,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
384384
else
385385
return Some{Any}(swapfield!(getargs(args, frame)...))
386386
end
387-
elseif @static isdefined(Core, :swapglobal!) && f === swapglobal!
387+
elseif @static isdefinedglobal(Core, :swapglobal!) && f === swapglobal!
388388
if nargs == 3
389389
return Some{Any}(Base.invoke_in_world(frame.world, swapglobal!, @lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4])))
390390
elseif nargs == 4
@@ -426,7 +426,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
426426
call_expr.args[3] = @lookup(frame, args[3])
427427
return Some{Any}(Core.eval(moduleof(frame), call_expr))
428428
end
429-
elseif @static (isdefined(Core, :arrayref) && Core.arrayref isa Core.Builtin) && f === Core.arrayref
429+
elseif @static (isdefinedglobal(Core, :arrayref) && Core.arrayref isa Core.Builtin) && f === Core.arrayref
430430
if nargs == 1
431431
return Some{Any}(Core.arrayref(@lookup(frame, args[2])))
432432
elseif nargs == 2
@@ -440,7 +440,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
440440
else
441441
return Some{Any}(Core.arrayref(getargs(args, frame)...))
442442
end
443-
elseif @static (isdefined(Core, :arrayset) && Core.arrayset isa Core.Builtin) && f === Core.arrayset
443+
elseif @static (isdefinedglobal(Core, :arrayset) && Core.arrayset isa Core.Builtin) && f === Core.arrayset
444444
if nargs == 1
445445
return Some{Any}(Core.arrayset(@lookup(frame, args[2])))
446446
elseif nargs == 2
@@ -456,7 +456,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
456456
else
457457
return Some{Any}(Core.arrayset(getargs(args, frame)...))
458458
end
459-
elseif @static (isdefined(Core, :arrayset) && Core.arrayset isa Core.Builtin) && f === Core.arrayset
459+
elseif @static (isdefinedglobal(Core, :arrayset) && Core.arrayset isa Core.Builtin) && f === Core.arrayset
460460
if nargs == 1
461461
return Some{Any}(Core.arrayset(@lookup(frame, args[2])))
462462
elseif nargs == 2
@@ -472,7 +472,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
472472
else
473473
return Some{Any}(Core.arrayset(getargs(args, frame)...))
474474
end
475-
elseif @static (isdefined(Core, :const_arrayref) && Core.const_arrayref isa Core.Builtin) && f === Core.const_arrayref
475+
elseif @static (isdefinedglobal(Core, :const_arrayref) && Core.const_arrayref isa Core.Builtin) && f === Core.const_arrayref
476476
if nargs == 1
477477
return Some{Any}(Core.const_arrayref(@lookup(frame, args[2])))
478478
elseif nargs == 2
@@ -486,7 +486,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
486486
else
487487
return Some{Any}(Core.const_arrayref(getargs(args, frame)...))
488488
end
489-
elseif @static (isdefined(Core, :memoryref) && Core.memoryref isa Core.Builtin) && f === Core.memoryref
489+
elseif @static (isdefinedglobal(Core, :memoryref) && Core.memoryref isa Core.Builtin) && f === Core.memoryref
490490
if nargs == 1
491491
return Some{Any}(Core.memoryref(@lookup(frame, args[2])))
492492
elseif nargs == 2
@@ -500,19 +500,19 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
500500
else
501501
return Some{Any}(Core.memoryref(getargs(args, frame)...))
502502
end
503-
elseif @static (isdefined(Core, :set_binding_type!) && Core.set_binding_type! isa Core.Builtin) && f === Core.set_binding_type!
503+
elseif @static (isdefinedglobal(Core, :set_binding_type!) && Core.set_binding_type! isa Core.Builtin) && f === Core.set_binding_type!
504504
if nargs == 2
505505
return Some{Any}(Core.set_binding_type!(@lookup(frame, args[2]), @lookup(frame, args[3])))
506506
elseif nargs == 3
507507
return Some{Any}(Core.set_binding_type!(@lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4])))
508508
else
509509
return Some{Any}(Core.set_binding_type!(getargs(args, frame)...))
510510
end
511-
elseif @static (isdefined(Core, :_apply_pure) && Core._apply_pure isa Core.Builtin) && f === Core._apply_pure
511+
elseif @static (isdefinedglobal(Core, :_apply_pure) && Core._apply_pure isa Core.Builtin) && f === Core._apply_pure
512512
return Some{Any}(Core._apply_pure(getargs(args, frame)...))
513-
elseif @static (isdefined(Core, :_call_in_world) && Core._call_in_world isa Core.Builtin) && f === Core._call_in_world
513+
elseif @static (isdefinedglobal(Core, :_call_in_world) && Core._call_in_world isa Core.Builtin) && f === Core._call_in_world
514514
return Some{Any}(Core._call_in_world(getargs(args, frame)...))
515-
elseif @static (isdefined(Core, :_call_latest) && Core._call_latest isa Core.Builtin) && f === Core._call_latest
515+
elseif @static (isdefinedglobal(Core, :_call_latest) && Core._call_latest isa Core.Builtin) && f === Core._call_latest
516516
args = getargs(args, frame)
517517
if !expand
518518
return Some{Any}(Core._call_latest(args...))

src/interpret.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ isassign(frame::Frame) = isassign(frame, frame.pc)
22
isassign(frame::Frame, pc::Int) = (pc in frame.framecode.used)
33

44
lookup_var(frame::Frame, val::SSAValue) = frame.framedata.ssavalues[val.id]
5-
lookup_var(frame::Frame, ref::GlobalRef) = invokelatest(getfield, ref.mod, ref.name)
5+
lookup_var(frame::Frame, ref::GlobalRef) = @invokelatest getglobal(ref.mod, ref.name)
66
function lookup_var(frame::Frame, slot::SlotNumber)
77
val = frame.framedata.locals[slot.id]
88
val !== nothing && return val.value
@@ -225,7 +225,7 @@ end
225225

226226
function native_call(fargs::Vector{Any}, frame::Frame)
227227
f = popfirst!(fargs) # now it's really just `args`
228-
if (@static isdefined(Core.IR, :EnterNode) && true)
228+
if (@static isdefinedglobal(Core.IR, :EnterNode) && true)
229229
newscope = Core.current_scope()
230230
if newscope !== nothing || !isempty(frame.framedata.current_scopes)
231231
for scope in frame.framedata.current_scopes
@@ -322,7 +322,7 @@ function evaluate_methoddef(frame::Frame, node::Expr)
322322
else
323323
# TODO: This logic isn't fully correct, but it's been used for a long
324324
# time, so let's leave it for now.
325-
if Base.isbindingresolved(mod, name) && @invokelatest isdefined(mod, name) # `isdefined` accesses the binding, making it impossible to create a new one
325+
if Base.isbindingresolved(mod, name) && @invokelatest isdefinedglobal(mod, name) # `isdefinedglobal` accesses the binding, making it impossible to create a new one
326326
f = @invokelatest getfield(mod, name)
327327
else
328328
f = Core.eval(mod, Expr(:function, name)) # create a new function
@@ -414,9 +414,9 @@ function check_isdefined(frame::Frame, @nospecialize(node))
414414
elseif isexpr(node, :static_parameter)
415415
return isassigned(data.sparams, node.args[1]::Int)
416416
elseif isa(node, GlobalRef)
417-
return isdefined(node.mod, node.name)
417+
return isdefinedglobal(node.mod, node.name)
418418
elseif isa(node, Symbol)
419-
return isdefined(moduleof(frame), node)
419+
return isdefinedglobal(moduleof(frame), node)
420420
else # QuoteNode or other implicitly quoted object
421421
return true
422422
end
@@ -578,7 +578,7 @@ function step_expr!(@nospecialize(recurse), frame::Frame, @nospecialize(node), i
578578
elseif istoplevel && isa(node, LineNumberNode)
579579
elseif istoplevel && isa(node, Symbol)
580580
rhs = invokelatest(getfield, moduleof(frame), node)
581-
elseif @static (isdefined(Core.IR, :EnterNode) && true) && isa(node, Core.IR.EnterNode)
581+
elseif @static (isdefinedglobal(Core.IR, :EnterNode) && true) && isa(node, Core.IR.EnterNode)
582582
rhs = node.catch_dest
583583
push!(data.exception_frames, rhs)
584584
if isdefined(node, :scope)

0 commit comments

Comments
 (0)