sci

https://github.com/babashka/SCI - also see #babashka and #nbb
ikitommi 2021-04-27T05:17:11.026900Z

any change of getting an utility to sci (or to a new single-purpose lib) to limit how long an sci eval can take millis? related to (https://github.com/borkdude/sci/pull/349)

borkdude 2021-04-27T07:54:58.028Z

Please read this issue: https://github.com/borkdude/sci/issues/348 On the JVM you can do this by running sci in a thread and killing the thread. But sci itself doesn't have enough control over the host to facilitate this

ikitommi 2021-04-27T05:18:36.027200Z

it’s easy to go wrong in the user space and I think people copy&paste buggy limiters into user code bases to avoid this.

ikitommi 2021-04-27T05:19:29.027400Z

like I just inlined a code snipplet to malli, and just for cljs, and without proper threading….

ikitommi 2021-04-27T05:20:15.027600Z

#?(:clj
   (defn ^:no-doc -run [^Runnable f ms]
     (let [task (FutureTask. f), t (Thread. task)]
       (try
         (.start t) (.get task ms TimeUnit/MILLISECONDS)
         (catch TimeoutException _ (.cancel task true) (.stop t) ::timeout)
         (catch Exception e (.cancel task true) (.stop t) (throw e))))))

ikitommi 2021-04-27T05:21:03.027800Z

the Issue & PR has a lot of good links. smells like a common utility lib 🙂

2021-04-27T09:36:22.029Z

@borkdude Thanks for directing me here - I looked but I must have failed to include a # sign or something like that, so I didn't see it.

2021-04-27T09:37:14.029900Z

I'm just starting to use sci and I'm surprised that I can't eval something like: "^{:foo true}[1 2]" and see that metadata on the result returned by eval-string.

2021-04-27T09:37:39.030200Z

Is that expected?

borkdude 2021-04-27T09:37:42.030300Z

What version are you using?

2021-04-27T09:37:56.030500Z

Version "0.2.4".

borkdude 2021-04-27T09:38:31.030800Z

Are you using tools.deps perhaps?

borkdude 2021-04-27T09:38:38.031Z

or lein?

2021-04-27T09:38:52.031400Z

I'm using deps.edn along with the clj command line tools.

borkdude 2021-04-27T09:39:07.031800Z

Can you try to use the newest commit from master using a git dep? I think this was already fixed.

borkdude 2021-04-27T09:39:14.032100Z

If that works for you can I do a 0.2.5 release tomorrow

2021-04-27T09:39:40.032700Z

Um ok certainly. It'll probably take a few minutes, but thank you!

2021-04-27T09:40:42.033500Z

I've never done this git dep thingy but I'm sure it won't be too hard.

2021-04-27T09:41:06.034100Z

Ok, found an example ...

borkdude 2021-04-27T09:41:32.034400Z

borkdude/sci {:git/url "<https://github.com/borkdude/sci>" :sha "a083698ffcbfd4b81b4b5aaff96cf8d0de6fc4e1"}

2021-04-27T09:41:56.034600Z

Yup, that matches what I just did, thank you!

2021-04-27T09:41:59.034900Z

Restarting now ...

2021-04-27T09:42:49.035200Z

It's very neat that that feature works.

2021-04-27T09:42:57.035500Z

Ok compiling my app and will test with sci in a moment.

2021-04-27T09:44:41.035700Z

Ok, I do see the metadata there.

2021-04-27T09:45:53.036200Z

I also simultaneously found a bug in my own code.

2021-04-27T09:46:14.036700Z

I'm going to revert to the release version of sci and see if it was my bug all along.

2021-04-27T09:47:35.037500Z

Ok, as embarrassing as it is to say, this looks like it was probably my error all along in how I was treating the value returned from sci.

2021-04-27T09:48:26.038400Z

I'm using shadow-cljs to build my project, and I see a couple of whiny warnings about not being able to infer target type.

2021-04-27T09:48:37.038700Z

Do you think that indicates a configuration problem on my end, or is this normal?

borkdude 2021-04-27T09:48:54.039200Z

The shadow-cljs warnings should be fixed on master as well I think. Are you still using the git dep?

2021-04-27T09:49:13.039700Z

I'm not using the git dep anymore, but I can test with that and see if the warnings go away. One sec.

2021-04-27T09:49:32.040Z

Ok, recompiling now with the git dep again.

2021-04-27T09:50:14.040500Z

I still see the 2 warnings even with the git dep.

2021-04-27T09:50:28.040800Z

------ WARNING #1 - :infer-warning ---------------------------------------------
 Resource: sci/impl/vars$macros.cljc:107:14
--------------------------------------------------------------------------------
 104 | 
 105 | (defn push-thread-bindings [bindings]
 106 |   (let [frame (get-thread-binding-frame)
 107 |         bmap (.-bindings frame)
--------------------^-----------------------------------------------------------
 Cannot infer target type in expression (. frame -bindings)
--------------------------------------------------------------------------------
 108 |         bmap (reduce (fn [acc [var* val*]]
 109 |                        (when-not (dynamic-var? var*)
 110 |                          (throw (new #?(:clj IllegalStateException
 111 |                                         :cljs js/Error)
--------------------------------------------------------------------------------

------ WARNING #2 - :infer-warning ---------------------------------------------
 Resource: sci/impl/vars$macros.cljc:131:21
--------------------------------------------------------------------------------
 128 | (defn get-thread-bindings []
 129 |   (let [f (get-thread-binding-frame)]
 130 |     (loop [ret {}
 131 |            kvs (seq (.-bindings f))]
---------------------------^----------------------------------------------------
 Cannot infer target type in expression (. f -bindings)
--------------------------------------------------------------------------------
 132 |       (if kvs
 133 |         (let [[var* ^TBox tbox] (first kvs)
 134 |               tbox-val (t/getVal tbox)]
 135 |           (recur (assoc ret var* tbox-val)
--------------------------------------------------------------------------------

2021-04-27T09:50:42.041200Z

However, I should say that I do have a bootstrap build going on as well.

2021-04-27T09:50:49.041400Z

I don't know if that could have any impact here or not.

borkdude 2021-04-27T09:51:09.041800Z

in general shadow warnings don't really hurt but it would be nice to get rid of them. I'll take a look

2021-04-27T09:51:20.042Z

Ok, thank you for your support. I appreciate it.

2021-04-27T09:56:49.042400Z

In sci is it possible to do things like create a new javascript date in clojurescript?

2021-04-27T09:57:09.042700Z

Ie, an equivalent of (js/Date.)

2021-04-27T09:57:50.043200Z

I can obviously create a binding which has a function which does that.

2021-04-27T09:57:59.043600Z

I'm just trying to understand what js interop exists, if any.

borkdude 2021-04-27T09:59:50.044100Z

@pmooser You can. The config for this you can borrow from: https://github.com/babashka/xterm-sci which is hosted here: https://babashka.org/xterm-sci/

borkdude 2021-04-27T10:00:13.044500Z

Welcome to xterm-sci.
user=&gt; (js/Date.)
#inst "2021-04-27T10:00:05.412-00:00"
user=&gt; 

2021-04-27T10:00:22.044700Z

Ah, great! Thank you.

borkdude 2021-04-27T10:07:10.044900Z

Releasing 0.2.5 now

borkdude 2021-04-27T10:07:27.045200Z

oh crap, I forgot about the shadow warnings, cancelling...

2021-04-27T10:18:45.045600Z

Ok, the example you gave me (xterm-sci) was just what I needed.

2021-04-27T10:18:54.046Z

It still boggles my mind that we can do this stuff with cljs in the browser.

borkdude 2021-04-27T10:20:24.046400Z

I'm not getting any replies from #shadow-cljs, I think I'll just go ahead with the release

2021-04-27T10:20:33.046600Z

Ok, thank you for checking.

borkdude 2021-04-27T10:23:57.046900Z

@pmooser Can you do one more check with commit 45a3363cc0dc09f9d6e50c7ec88fe29612597c79 to see if this fixed the warning?

2021-04-27T10:24:11.047100Z

Yes, one moment.

2021-04-27T10:25:03.047300Z

mmm edamame ...

2021-04-27T10:25:29.047600Z

Ok - mixed result. 1 warning is gone, 1 remains.

2021-04-27T10:25:39.047900Z

------ WARNING #1 - :infer-warning ---------------------------------------------
 Resource: sci/impl/vars$macros.cljc:131:21
--------------------------------------------------------------------------------
 128 | (defn get-thread-bindings []
 129 |   (let [f (get-thread-binding-frame)]
 130 |     (loop [ret {}
 131 |            kvs (seq (.-bindings f))]
---------------------------^----------------------------------------------------
 Cannot infer target type in expression (. f -bindings)
--------------------------------------------------------------------------------
 132 |       (if kvs
 133 |         (let [[var* ^TBox tbox] (first kvs)
 134 |               tbox-val (t/getVal tbox)]
 135 |           (recur (assoc ret var* tbox-val)
--------------------------------------------------------------------------------

borkdude 2021-04-27T10:26:22.048300Z

ok, it seems shadow-cljs doesn't understand return tags from get-thread-binding-frame

borkdude 2021-04-27T10:28:53.048500Z

releasing 0.2.5 now

2021-04-27T10:30:05.048700Z

Thank you!

borkdude 2021-04-27T10:32:43.048900Z

should be there now

2021-04-27T10:35:49.049100Z

Hmm. I don't know how to explain this, but I saw no warnings that time.

borkdude 2021-04-27T10:38:25.049500Z

in 0.2.5 the warnings are probably gone now

2021-04-27T10:38:57.049800Z

Yup. That seems to be the case. Thank you.

2021-04-27T10:52:58.050900Z

Is it correct that in cljs right now, with sci, the story with things like println is ... we should bind them ourselves to the normal cljs println ? (or some other function that does whatever we want it to do).

2021-04-27T10:53:25.051800Z

By default calling println seems to result in an opaque exception, but I see an issue in github regarding cljs printing.

borkdude 2021-04-27T10:53:28.051900Z

yeah, sci is "safe" by default and doesn't let you mutate the outside world by default.

borkdude 2021-04-27T10:53:55.052900Z

so you have to either bind sci/out to *out* or override println with something else

borkdude 2021-04-27T10:54:19.053600Z

just see the xterm-sci project for an example

2021-04-27T10:54:21.053700Z

Oh wait, does that work?

2021-04-27T10:54:22.053900Z

Hmm.

2021-04-27T10:54:31.054400Z

I tried that (binding) ... I must have done it incorrectly.

2021-04-27T10:54:36.054700Z

I'll look in xterm-sci.

2021-04-27T10:56:06.055800Z

Ah yes. I just meant I hadn't had any luck binding sci/out to *out* in cljs or whatever.

2021-04-27T10:56:21.056500Z

What you did there definitely works.

borkdude 2021-04-27T10:56:29.056700Z

that might be because some functions use *print-fn* rather than *out* ?

borkdude 2021-04-27T10:56:40.056900Z

not sure

2021-04-27T10:57:01.057400Z

It's ok - the example from xterm-sci is really nice, including the goog string buffer which is obvious in cljs.

2021-04-27T10:57:22.058Z

It's a great starting point, and when I dig into it, if I really get stuck, I'll ask you, but the working example is definitely great.

2021-04-27T10:57:26.058200Z

🤌

borkdude 2021-04-27T10:58:50.059Z

if you think anything should be improved to the docs, (e.g. js interop, link to xterm-sci, etc) please do contribute

2021-04-27T10:59:56.060100Z

Sure thing. One thing I was surprised by regarding the main page is that there was very little mention of errors or exceptions or how they are returned. It becomes pretty clear once you start using it, but that seemed like an omission to me.