Fork me on GitHub
#cljs-dev
<
2020-06-22
>
dpsutton02:06:48

just trying to get an environment up where i can test things. using cursive in cljs.compiler.api and wanting to see emitted code. I expect that i could do something like (println (emit (ana-api/analyze (ana-api/empty-env) '(and 1 2)))) to see the emitted js code. it's close but not quite there. i'm seeing the following output which only has the first test and not the second:

var and__9016__auto___16490 = (1);
if(cljs.core.truth_(and__9016__auto___16490)){
} else {
}
anyone know off hand how to get a fast feedback loop up in cursive? (open to bare clj in a terminal if that's easier)

mfikes05:06:10

@dpsutton In terms of the output you are seeing above, it is a consequence of the default :context being :statement and the fact that numbers have no side effects so are elided as an optimization. If instead you set :context to be :expr you'll get what you are expecting. In other words try

(println (emit (ana-api/analyze (assoc (ana-api/empty-env) :context :expr) '(and 1 2))))

mfikes05:06:30

(If the :statement / :expr stuff doesn't make sense, consider the code generated for (do (and 1 2) nil))

dpsutton05:06:03

i had no idea non-side-effecting code was elided. awesome. thanks very much

mfikes05:06:42

You can see that optimization here https://github.com/clojure/clojurescript/blob/master/src/main/clojure/cljs/compiler.cljc#L608-L611 It is really only applied to constants IIRC.

dpsutton05:06:39

ok. was wondering what kind of heuristic or analysis let the compiler be certain of side-effect free

dpsutton05:06:42

thanks so much