Thanks to visit codestin.com
Credit goes to github.com

Skip to content

planck.core/load-string evaluates in current namespace #867

@pyrmont

Description

@pyrmont

planck.core/load-string can be provided a string with Clojure code that it will evaluate. Contrary to expectations, if the code defines a namespace the code is not evaluated relative to this namespace. Consider the following example:

cljs.user=> (require '[planck.core :as pl])
;=> nil
cljs.user=> (pl/load-string "(ns test.core) (defn eg-f [] 1)")
;=> #'cljs.user/eg-f
cljs.user=> (in-ns 'test-core)
;=> nil
test.core=> (def x 1)
;=> #'test.core/x
test.core=> (in-ns 'cljs.user)
;=> nil
cljs.user=> (pl/load-string "(ns test.core) (defn eg-g [] x)")
;=> WARNING: Use of undeclared Var cljs.user/x
;=> #'cljs.user/eg-g

We would expect that eg-f and eg-g would be defined in the test.core namespace but they are instead defined in the namespace in which load-string is called (in this case cljs.user but, if we were in a different namespace, the evaluation would be performed relative to that namespace).

The load-string function is not available as part of the ClojureScript API and, while it is available as part of the Clojure API, it works in the context of the compiler (written in Java) and so I am not sure how helpful that code is as a reference.

While it isn't part of the ClojureScript API, some of the code in the cljs.repl namespace may be of more assistance. The function load-stream appears to me to bear some similarity to how load-string works. It relies on evaluate-form within cljs.repl and planck.core/load-string may need to implement something similar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions