-
Notifications
You must be signed in to change notification settings - Fork 32
fix: preserve symbolic external units across precompile #215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4cf054a
37b3bd9
45aa0f7
7645686
524cb15
dde2975
982937d
b06c809
39abd36
97a2983
a8912ae
c5c1868
6efcab1
9f886d9
7ec2b6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,39 @@ | ||
| const EXTERNAL_UNIT_DECLARATION_LOCK = Threads.SpinLock() | ||
| const EXTERNAL_UNIT_DECLARATIONS = IdDict{Module,Set{Symbol}}() | ||
|
|
||
| function declare_external_unit(mod::Module, name::Symbol) | ||
| lock(EXTERNAL_UNIT_DECLARATION_LOCK) do | ||
| push!(get!(() -> Set{Symbol}(), EXTERNAL_UNIT_DECLARATIONS, mod), name) | ||
| end | ||
| return nothing | ||
| end | ||
|
|
||
| function external_unit_declaration(mod::Module, name::Symbol) | ||
| lock(EXTERNAL_UNIT_DECLARATION_LOCK) do | ||
| declarations = get(EXTERNAL_UNIT_DECLARATIONS, mod, nothing) | ||
| return declarations !== nothing && name in declarations | ||
| end | ||
| end | ||
|
|
||
| function external_quantity_binding(mod::Module, sym::Symbol) | ||
| return isdefined(mod, sym) && getfield(mod, sym) isa UnionAbstractQuantity | ||
| end | ||
|
|
||
| function ensure_registered_external_unit(sym::Symbol, unit::UnionAbstractQuantity) | ||
| lock(UNIT_UPDATE_LOCK) do | ||
| if iszero(get(UNIT_MAPPING, sym, 0)) | ||
| update_all_values_unlocked(sym, unit) | ||
| end | ||
| end | ||
| return nothing | ||
| end | ||
|
|
||
| function lookup_registered_unit(sym::Symbol) | ||
| i = get(UNIT_MAPPING, sym, 0) | ||
| iszero(i) && throw(ArgumentError("Symbol $sym not found in `Units`.")) | ||
| return ALL_VALUES[i] | ||
| end | ||
|
|
||
| module UnitsParse | ||
|
|
||
| using DispatchDoctor: @unstable | ||
|
|
@@ -6,7 +42,11 @@ import ..constructorof | |
| import ..DEFAULT_QUANTITY_TYPE | ||
| import ..DEFAULT_DIM_TYPE | ||
| import ..DEFAULT_VALUE_TYPE | ||
| import ..Units: UNIT_SYMBOLS, UNIT_VALUES | ||
| import ..external_quantity_binding | ||
| import ..external_unit_declaration | ||
| import ..ensure_registered_external_unit | ||
| import ..lookup_registered_unit | ||
| import ..Units: UNIT_SYMBOLS | ||
| import ..Constants: CONSTANT_SYMBOLS, CONSTANT_VALUES | ||
| import ..Constants | ||
|
|
||
|
|
@@ -59,38 +99,50 @@ the quantity corresponding to the speed of light multiplied by Hertz, | |
| squared. | ||
| """ | ||
| macro u_str(s) | ||
| ex = map_to_scope(Meta.parse(s)) | ||
| ex = map_to_scope(__module__, Meta.parse(s)) | ||
| ex = :($as_quantity($ex)) | ||
| return esc(ex) | ||
| end | ||
|
|
||
| @unstable function map_to_scope(ex::Expr) | ||
| @unstable map_to_scope(ex::Expr) = map_to_scope(@__MODULE__, ex) | ||
| @unstable function map_to_scope(mod::Module, ex::Expr) | ||
| if !(ex.head == :call) && !(ex.head == :. && ex.args[1] == :Constants) | ||
| throw(ArgumentError("Unexpected expression: $ex. Only `:call` and `:.` (for `Constants`) are expected.")) | ||
| end | ||
| if ex.head == :call | ||
| ex.args[2:end] = map(map_to_scope, ex.args[2:end]) | ||
| ex.args[2:end] = map(arg -> map_to_scope(mod, arg), ex.args[2:end]) | ||
| return ex | ||
| else # if ex.head == :. && ex.args[1] == :Constants | ||
| @assert ex.args[2] isa QuoteNode | ||
| return lookup_constant(ex.args[2].value) | ||
| return Expr(:call, GlobalRef(@__MODULE__, :lookup_constant), QuoteNode(ex.args[2].value)) | ||
| end | ||
| end | ||
| function map_to_scope(sym::Symbol) | ||
| if sym in UNIT_SYMBOLS | ||
| return lookup_unit(sym) | ||
| elseif sym in CONSTANT_SYMBOLS | ||
| map_to_scope(sym::Symbol) = map_to_scope(@__MODULE__, sym) | ||
| function map_to_scope(mod::Module, sym::Symbol) | ||
| has_registered_binding = sym in UNIT_SYMBOLS | ||
| has_external_binding = !(mod === @__MODULE__) && ( | ||
| external_quantity_binding(mod, sym) || external_unit_declaration(mod, sym) | ||
| ) | ||
|
|
||
| if !has_registered_binding && sym in CONSTANT_SYMBOLS | ||
| throw(ArgumentError("Symbol $sym found in `Constants` but not `Units`. Please use `u\"Constants.$sym\"` instead.")) | ||
| else | ||
| elseif !has_registered_binding && !has_external_binding | ||
| throw(ArgumentError("Symbol $sym not found in `Units` or `Constants`.")) | ||
| elseif has_external_binding | ||
| return Expr(:call, GlobalRef(@__MODULE__, :lookup_external_unit), QuoteNode(mod), QuoteNode(sym)) | ||
|
Comment on lines
+131
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a caller module does Useful? React with 👍 / 👎. |
||
| end | ||
|
|
||
| return Expr(:call, GlobalRef(@__MODULE__, :lookup_unit), QuoteNode(sym)) | ||
| end | ||
| function map_to_scope(ex) | ||
| return ex | ||
| end | ||
| function lookup_unit(ex::Symbol) | ||
| i = findfirst(==(ex), UNIT_SYMBOLS)::Int | ||
| return UNIT_VALUES[i] | ||
| map_to_scope(::Module, ex) = ex | ||
|
|
||
| @unstable lookup_unit(ex::Symbol) = lookup_registered_unit(ex) | ||
| @unstable function lookup_external_unit(mod::Module, sym::Symbol) | ||
| ensure_registered_external_unit(sym, getfield(mod, sym)) | ||
| return lookup_registered_unit(sym) | ||
| end | ||
| function lookup_constant(ex::Symbol) | ||
| i = findfirst(==(ex), CONSTANT_SYMBOLS)::Int | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| module ExternalUnitRegistration | ||
|
|
||
| using DynamicQuantities: @register_unit, @u_str, @us_str | ||
| using DynamicQuantities: DEFAULT_QUANTITY_TYPE, DEFAULT_SYMBOLIC_QUANTITY_OUTPUT_TYPE | ||
|
|
||
| @register_unit MyWb u"m^2*kg*s^-2*A^-1" | ||
|
|
||
| function __init__() | ||
| @register_unit MyInitWb u"m^2*kg*s^-2*A^-1" | ||
| end | ||
|
|
||
| const MYWB_EXPANDED = u"MyWb" | ||
|
|
||
| expanded_mywb() = 1u"MyWb" | ||
| symbolic_mywb() = 1us"MyWb" | ||
| expanded_mywb_from_helper() = one(expanded_mywb()) * u"MyWb" | ||
| symbolic_mywb_from_helper() = one(symbolic_mywb()) * us"MyWb" | ||
| expanded_constant_mywb() = MYWB_EXPANDED | ||
| init_expanded_mywb() = 1u"MyInitWb" | ||
| init_symbolic_mywb() = 1us"MyInitWb" | ||
|
|
||
| export MyWb | ||
| export MYWB_EXPANDED | ||
| export expanded_constant_mywb, expanded_mywb, expanded_mywb_from_helper | ||
| export symbolic_mywb, symbolic_mywb_from_helper | ||
| export init_expanded_mywb, init_symbolic_mywb | ||
|
|
||
| end |
Uh oh!
There was an error while loading. Please reload this page.