Fork me on GitHub
#sci
<
2021-04-25
>
henrik4213:04:03

sci newbie here. Why would I use sci instead of just clojure java interop when using clojure expressions as an embedded dsl in a java app?

borkdude13:04:13

@henrikheine If you can just use Clojure eval and you do not need to control which classes you have access to, by all means, use it

henrik4213:04:21

Ok. The sci readme mentions bindings for eval-string which I like as it lets me define a context for name resolution. Do you think it's worth pulling in sci as a dep'y for this or is there a clojure equivalent you know of?

borkdude13:04:30

You could just evaluate an expression in Clojure in the context of a namespace which has the names you need.

user=> (ns foo)
nil
foo=> (def x 1)
#'foo/x
foo=> (ns bar)
nil
bar=> (binding [*ns* (the-ns 'foo)] (eval 'x))
1

henrik4213:04:14

Good idea. Since this would be a server app I would have to give each user (session? service-call?) a dedicated namespace. Isolation is my concern here.

borkdude13:04:17

Namespaces in Clojure are global, but you could generate the name using some uuid. You don't get any isolation in the level of security though, users could always iterate over all the available namespaces and ruin it for others for example or ruin your running up, call (System/exit 1), etc

👍 3
borkdude13:04:25

If you want something more isolated, then sci is a good option

henrik4213:04:43

I see. Thank's a lot.

henrik4213:04:58

From an older sci cljdoc I had the impression that some parts may have been aot compiled in the past. Is there some kind of java api for sci? Or would I just go through java-interop?

borkdude14:04:46

@henrikheine sci had a Java API but that's been deprecated as it was a lot of work to keep it up to date while nobody was using it (probably). Both from JS and Java you're advised to just make a wrapping function from Clojure that fits your need and then expose that to your target platform

henrik4218:04:21

Thx. That makes sense.

borkdude13:04:44

Note that Clojure eval is not compatible with GraalVM native-image, so if that is a requirement, sci may be a good alternative

borkdude14:04:46

@henrikheine sci had a Java API but that's been deprecated as it was a lot of work to keep it up to date while nobody was using it (probably). Both from JS and Java you're advised to just make a wrapping function from Clojure that fits your need and then expose that to your target platform