Fork me on GitHub
#sci
<
2021-04-27
>
ikitommi05:04:11

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)

ikitommi05:04:36

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.

ikitommi05:04:29

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

ikitommi05:04:15

#?(: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))))))

ikitommi05:04:03

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

borkdude07:04:58

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

pmooser09:04:22

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

pmooser09:04:14

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.

pmooser09:04:39

Is that expected?

borkdude09:04:42

What version are you using?

pmooser09:04:56

Version "0.2.4".

borkdude09:04:31

Are you using tools.deps perhaps?

pmooser09:04:52

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

borkdude09:04:07

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

borkdude09:04:14

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

pmooser09:04:40

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

pmooser09:04:42

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

pmooser09:04:06

Ok, found an example ...

borkdude09:04:32

borkdude/sci {:git/url "" :sha "a083698ffcbfd4b81b4b5aaff96cf8d0de6fc4e1"}

pmooser09:04:56

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

pmooser09:04:59

Restarting now ...

pmooser09:04:49

It's very neat that that feature works.

pmooser09:04:57

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

pmooser09:04:41

Ok, I do see the metadata there.

pmooser09:04:53

I also simultaneously found a bug in my own code.

pmooser09:04:14

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

pmooser09:04:35

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.

pmooser09:04:26

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.

pmooser09:04:37

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

borkdude09:04:54

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

pmooser09:04:13

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

pmooser09:04:32

Ok, recompiling now with the git dep again.

pmooser09:04:14

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

pmooser09:04:28

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

pmooser09:04:42

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

pmooser09:04:49

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

borkdude09:04:09

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

pmooser09:04:20

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

pmooser09:04:49

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

pmooser09:04:09

Ie, an equivalent of (js/Date.)

pmooser09:04:50

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

pmooser09:04:59

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

borkdude09:04:50

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

borkdude10:04:13

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

pmooser10:04:22

Ah, great! Thank you.

borkdude10:04:10

Releasing 0.2.5 now

borkdude10:04:27

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

pmooser10:04:45

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

pmooser10:04:54

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

borkdude10:04:24

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

pmooser10:04:33

Ok, thank you for checking.

borkdude10:04:57

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

pmooser10:04:11

Yes, one moment.

pmooser10:04:03

mmm edamame ...

pmooser10:04:29

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

pmooser10:04:39

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

borkdude10:04:22

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

borkdude10:04:53

releasing 0.2.5 now

pmooser10:04:05

Thank you!

borkdude10:04:43

should be there now

pmooser10:04:49

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

borkdude10:04:25

in 0.2.5 the warnings are probably gone now

pmooser10:04:57

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

pmooser10:04:58

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

pmooser10:04:25

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

borkdude10:04:28

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

borkdude10:04:55

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

borkdude10:04:19

just see the xterm-sci project for an example

pmooser10:04:21

Oh wait, does that work?

pmooser10:04:31

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

pmooser10:04:36

I'll look in xterm-sci.

pmooser10:04:06

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

pmooser10:04:21

What you did there definitely works.

borkdude10:04:29

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

pmooser10:04:01

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

pmooser10:04:22

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.

borkdude10:04:50

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

pmooser10:04:56

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.