cljs-dev

ClojureScript compiler & std lib dev, https://clojurescript.org/community/dev
dpsutton 2020-06-22T02:56:48.285200Z

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)

mfikes 2020-06-22T05:34:10.286900Z

@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))))

mfikes 2020-06-22T05:36:30.287600Z

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

dpsutton 2020-06-22T05:37:03.288100Z

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

mfikes 2020-06-22T05:41:42.288700Z

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.

dpsutton 2020-06-22T05:46:39.289400Z

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

dpsutton 2020-06-22T05:46:42.289600Z

thanks so much