Matthew Molloy
Matthew Molloy

Reputation: 1186

Clojurescript Self Compile of def

Clojurescript 1.7 now supports self compilation (see here). I can compile code as follows

(ns self-compile.core
  (:require cljs.js))

(enable-console-print!)
(set! cljs.js/*eval-fn* cljs.js/js-eval)
(def state (cljs.js/empty-state))

(cljs.js/eval-str state "(+ 1 2)"
                (fn [response] ...))

This works fine for most code, except (def a 3) which gives the error #error {:message "ERROR", :data {:tag :cljs/analysis-error}, :cause #object[TypeError TypeError: Cannot set property 'a' of undefined]}.

How can I fix the setup?

Upvotes: 1

Views: 96

Answers (1)

Joaquin
Joaquin

Reputation: 2744

I had the same issues, here's a few things I had to do go get it working:

  1. Pass options to eval-str, you may have to pass options like:

    :context :expr
    :def-emits-var true
    :ns 'cljs.user
    
  2. Try evaling (ns cljs.user) first before evaling the def.

That should fix it.

See https://github.com/cljsinfo/cljs-api-docs/blob/catalog/refs/cljs.js_eval-str.md

Upvotes: 3

Related Questions