Skip to content

Replace eval by generated function #119

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

Merged
merged 2 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "6f1fad26-d15e-5dc8-ae53-837a1d7b8c9f"
license = "MIT"
desc = "Tape based task copying in Turing"
repo = "https://github.com/TuringLang/Libtask.jl.git"
version = "0.6.8"
version = "0.6.9"

[deps]
IRTools = "7869d1d1-7146-5819-86e3-90919afe41df"
Expand Down
17 changes: 15 additions & 2 deletions src/tapedfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,25 @@ function (instr::Instruction{F})() where F
end
end

"""
__new__(T, args)

Return a new instance of `T` with `args` even when there is no inner constructor for these args.
Source: https://discourse.julialang.org/t/create-a-struct-with-uninitialized-fields/6967/5
"""
@generated function __new__(T, args)
return Expr(:splatnew, :T, :args)
end

function _new end
function (instr::Instruction{typeof(_new)})()
# catch run-time exceptions / errors.
try
expr = Expr(:new, map(val, instr.input)...)
output = eval(expr)
input = map(val, instr.input)
T = input[1]
args = input[2:end]
output = __new__(T, args)

instr.output.val = output
instr.tape.counter += 1
catch e
Expand Down