# Eval a CodeInfo object?

**URL:** <https://discourse.julialang.org/t/eval-a-codeinfo-object/18076>\
**Category:** General Usage\
**Created:** [November 27, 2018, 8:23pm UTC](https://discourse.julialang.org/t/eval-a-codeinfo-object/18076 "2018-11-27T20:23:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [November 27, 2018, 8:23pm UTC](https://discourse.julialang.org/t/eval-a-codeinfo-object/18076/1 "2018-11-27T20:23:22Z")

</div>

Digging through the Julia [AST docs](https://github.com/JuliaLang/julia/blob/master/doc/src/devdocs/ast.md) I’ve got curious how far we can go with the lowered code representation. In particular:

1. Can we evaluate `CodeInfo` object as a whole?
2. Can we evaluate each its expression (in `.code` field) step by step?
3. Can we run type inference and/or compile `CodeInfo` object into a callable object (e.g. a function)?

I did all of these extensively in Espresso.jl for surface AST, but it has its drawbacks, so I’d like to know what I can do with the lowered version.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [July 16, 2019, 2:25pm UTC](https://discourse.julialang.org/t/eval-a-codeinfo-object/18076/2 "2019-07-16T14:25:24Z")

</div>

I have the same question, and Google gave me this thread and no other answer.

[An example from the documentation](https://pkg.julialang.org/docs/julia/THl1k/1.1.1/devdocs/reflection.html#Expansion-and-lowering-1) lead me to try this:

```julia
julia> g() = 42
g (generic function with 1 method)

julia> ci = code_typed(g, ())[1][1]
CodeInfo(
1 ─ return 42
)

julia> eval(Expr(:thunk, ci))
42

```

So it seems I’m able to evaluate a CodeInfo object, but I can’t figure out how to turn one into a function.

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [July 18, 2019, 8:13am UTC](https://discourse.julialang.org/t/eval-a-codeinfo-object/18076/3 "2019-07-18T08:13:45Z")

</div>

It seems an example of how to turn a CodeInfo object into a function can be found here:  
[https://github.com/NHDaly/DeepcopyModules.jl/blob/master/src/deepcopy\_functions.jl](https://github.com/NHDaly/DeepcopyModules.jl/blob/master/src/deepcopy_functions.jl)

🙂

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [July 19, 2019, 8:07am UTC](https://discourse.julialang.org/t/eval-a-codeinfo-object/18076/4 "2019-07-19T08:07:44Z")

</div>

Based on the above-mentioned code by @NHDaly, I wrote a small package that can be used to turn `CodeInfo` objects back into functions. (Still unregistered, with very little documentation and testing.)

[https://github.com/perrutquist/CodeTransformation.jl](https://github.com/perrutquist/CodeTransformation.jl)

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [July 19, 2019, 8:14am UTC](https://discourse.julialang.org/t/eval-a-codeinfo-object/18076/5 "2019-07-19T08:14:36Z")

</div>

Wow, looks great! Thanks for your effort!

---

<div class="post-metadata">

**Author:** ![NHDaly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhdaly/32/1413_2.png) [@NHDaly](https://discourse.julialang.org/u/NHDaly)\
**Post date:** [September 16, 2019, 3:18am UTC](https://discourse.julialang.org/t/eval-a-codeinfo-object/18076/6 "2019-09-16T03:18:59Z")

</div>

Cool, that looks really handy! 🙂 I’m glad it was helpful for you.
