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)@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))))
(If the :statement
/ :expr
stuff doesn't make sense, consider the code generated for (do (and 1 2) nil)
)
i had no idea non-side-effecting code was elided. awesome. thanks very much
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.
ok. was wondering what kind of heuristic or analysis let the compiler be certain of side-effect free
thanks so much