Skip to content

Commit ce5dbbb

Browse files
Merge pull request #2379 from ven-k/vkb/fix-precompilation-of-mtkmodel
fix: remove `eval`s from model parsing
2 parents dee5752 + e60c214 commit ce5dbbb

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

format/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[deps]
2+
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"

src/systems/model_parsing.jl

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,10 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs;
178178
(var, def)
179179
end
180180
Expr(:ref, a, b...) => begin
181+
indices = map(i -> UnitRange(i.args[2], i.args[end]), b)
181182
parse_variable_def!(dict, mod, a, varclass, kwargs;
182-
def, indices = [eval.(b)...])
183+
def, indices)
183184
end
184-
#= Expr(:if, condition, a) => begin
185-
var, def = [], []
186-
for var_def in a.args
187-
parse_variable_def!(dict, mod, var_def, varclass, kwargs)
188-
end
189-
end =#
190185
_ => error("$arg cannot be parsed")
191186
end
192187
end
@@ -301,7 +296,7 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps,
301296
parse_equations!(exprs, eqs, dict, body)
302297
elseif mname == Symbol("@icon")
303298
isassigned(icon) && error("This model has more than one icon.")
304-
parse_icon!(icon, dict, body)
299+
parse_icon!(body, dict, icon, mod)
305300
else
306301
error("$mname is not handled.")
307302
end
@@ -614,7 +609,7 @@ function parse_equations!(exprs, eqs, dict, body)
614609
end
615610
end
616611

617-
function parse_icon!(icon, dict, body::String)
612+
function parse_icon!(body::String, dict, icon, mod)
618613
icon_dir = get(ENV, "MTK_ICONS_DIR", joinpath(DEPOT_PATH[1], "mtk_icons"))
619614
dict[:icon] = icon[] = if isfile(body)
620615
URI("file:///" * abspath(body))
@@ -633,8 +628,8 @@ function parse_icon!(icon, dict, body::String)
633628
end
634629
end
635630

636-
function parse_icon!(icon, dict, body::Expr)
637-
parse_icon!(icon, dict, eval(body))
631+
function parse_icon!(body::Symbol, dict, icon, mod)
632+
parse_icon!(getfield(mod, body), dict, icon, mod)
638633
end
639634

640635
### Parsing Components:

test/model_parsing.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ export Pin
1818
@icon "pin.png"
1919
end
2020

21+
ground_logo = read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
2122
@mtkmodel Ground begin
2223
@components begin
2324
g = Pin()
2425
end
25-
@icon read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
26+
@icon ground_logo
2627
@equations begin
2728
g.v ~ 0
2829
end
@@ -321,6 +322,21 @@ end
321322
@test A.structure[:components] == [[:cc, :C]]
322323
end
323324

325+
# Ensure that modules consisting MTKModels with component arrays and icons of
326+
# `Expr` type and `unit` metadata can be precompiled.
327+
@testset "Precompile packages with MTKModels" begin
328+
push!(LOAD_PATH, joinpath(@__DIR__, "precompile_test"))
329+
330+
using ModelParsingPrecompile: ModelWithComponentArray
331+
332+
@named model_with_component_array = ModelWithComponentArray()
333+
334+
@test ModelWithComponentArray.structure[:parameters][:R][:unit] == u""
335+
@test lastindex(parameters(model_with_component_array)) == 3
336+
337+
pop!(LOAD_PATH)
338+
end
339+
324340
@testset "Conditional statements inside the blocks" begin
325341
@mtkmodel C begin end
326342

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module ModelParsingPrecompile
2+
3+
using ModelingToolkit
4+
using Unitful
5+
6+
@mtkmodel ModelWithComponentArray begin
7+
@parameters begin
8+
R(t)[1:3] = 1, [description = "Parameter array", unit = u""]
9+
end
10+
end
11+
12+
end

0 commit comments

Comments
 (0)