Fork me on GitHub
#sci
<
2022-08-28
>
john00:08:16

Hey all. I'm playing around with a sci evaluator in sci/scljs in sci/cljs. So like, a scittle page with a textarea which can be evaluated in browser, with an evaluate button like:

...
       :on-click #(let [res (eval (read-string code-str))]
                    (swap! results-atom assoc :result res))
       ...
Which seems to work, but when I put code in there that for instance has a r/atom, I'm getting the error: Could not resolve symbol: r/atom And doing something like: (ns self (:require [reagent.core :as r])) causes an error like: Can't change/establish root binding of #'*ns* with set Can I somehow pass through bindings from the parent sci context to the inner one?

borkdude09:08:34

@john You might want to use load-string or scittle.core.eval_string

👀 1
john15:08:25

I tried both like so:

...
       :on-click #(let [res (js/scittle.core.eval_string
                             #_load-string
                             code-str
                             {:namespaces {'clojure.core {'println (partial println :new-stuff)}}})]
                    (swap! results-atom assoc :result res))
       ...
And when I evaluate (do (println :original) 2) it returns 2 but it prints :original, with no :new-stuff.

borkdude15:08:07

what makes you think eval-string takes an additional SCI configuration?

john15:08:45

From the README (sci/eval-string "(println \"hello\")" {:namespaces {'clojure.core {'println println}}})

borkdude15:08:55

That is SCI, not scittle

john15:08:15

Didn't realize they were different

borkdude15:08:45

scittle is made with SCI to evaluate code from browser tags. if you want to have full control, you can just use SCI directly

john15:08:58

From within scittle?

borkdude15:08:37

No I meant, write a CLJS project using SCI?

borkdude15:08:46

@john if you want to alter println you can evaluate (alter-var-root #'println ...) within a SCI context

borkdude15:08:37

It could be that this currently doesn't work, due to a safety setting that should be enabled, but this is an easy change

borkdude15:08:27

a wait, this probably also doesn't work, because scittle has no reified vars (due to an optimization setting, I thought I could save some bundle size by leaving out docstrings, etc)

john15:08:49

hmm okay

john15:08:32

My idea for my weekend project was to build a little blog thing where you can edit your scittle coded blog in scittle. So like a component that can hide and show and edit your other components in

john15:08:52

Kinda almost like the bookmarklet code editor

john15:08:12

But I'm not able to do requires and whatnot in that bookmarklet context

borkdude15:08:00

There was a similar thing recently, I forgot the name...

john15:08:37

I did it once a few years ago with a self hosted cljs

john15:08:20

"Repl on a git stick"

john15:08:45

Might be cleaner with a sci-like thing though

borkdude15:08:14

@john The benefit of going directly with SCI (or a fork of scittle) is that you can add in your own libraries too

borkdude15:08:28

and then advance-compile them

john15:08:26

Yeah, that would be necessary eventually. I'm also curious about the idea of bringing in deps at runtime, written in sci. Maybe sci snippets from gh gists or something, imported in via some declaration or something, so a blog author can extend from the browser editor and then just commit the change from the browser. Basically a pure browser build and deploy scenario.

borkdude15:08:53

do you mean CLJS code snippets?

john15:08:14

yeah, SCI compatible CLJS

borkdude15:08:19

yeah, why not

john15:08:47

I'm also curious about an impl of sci that is more locked down, which can pull in approved deps/plugins, but can't call js/eval or js/alert or other stuff, maybe sandboxed in a iframed web worker, for the purposes of semi-trusted distributed computations

borkdude16:08:05

you could do that. you can disallow eval and all access to js/ (in fact, that's not even enabled by default)

borkdude16:08:21

SCI aims to be safe out of the box for sandboxing

borkdude16:08:36

the only thing it cannot do is pause/cancel running code

borkdude16:08:54

this has to be controlled by the host (by killing worker/thread)

john16:08:27

yeah, that's what I'd do. I'm thinking 3 seconds of execution time every 8 seconds

john16:08:38

3 seconds split across a per-host worker pool. Time spent in workers waiting on results would not go against the time quota, so they could spread across all 8 seconds, but 8 seconds would be the cutoff

john16:08:00

I'd think that's long enough to get some task done

john16:08:51

Pull data from IPFS, do something, put the result up. And then an author can construct larger computations by putting together circuits of these 3 second computations

john16:08:07

Hook it up to some utility coin and let content consumers and producers collect coin for resources provided, etc

john16:08:59

Donating 1 webworker/process thread for 3 seconds of execution time every 8 seconds doesn't seem too egregious. Maybe it could be larger than 8 seconds. 16, 32?

john16:08:16

It would be opt-in anyway, so the consumer could decide how much execution time to contribute

john16:08:51

Anyway, the idea is like a distributed computation platform for building backend services for distributed apps

borkdude16:08:23

sounds very futuristic, if SCI can help with that, why not :)

john16:08:43

Yeah, imagine an email client you could use for 10 bucks a month, or just enable a light-weight compute sharing thing and get it for free! Would be sweet.

john16:08:55

Just need a way to lock down and fully instrument and meter the runtime

john16:08:21

Seems like a sci-like thing is a lot of the way there

john16:08:39

It's pretty much the missing piece for a lot of these blockchains. You can't do most of the compute on these networks, because every node has to compute everything. So what you need is a separate set of partitions where small groups of "lite clients" can split off and do the work and then put the result back on-chain.

john19:08:01

Dude, sci is future-sauce. This is amazing https://github.com/babashka/sci/blob/master/doc/async.md

🎉 1
🔥 2
teodorlu11:08:38

sci is amazing, I can't believe we have an embedded, isolated, safe clojure in clojure

teodorlu11:08:04

it's like python's eval only good rather than terribly scary

john14:08:51

Yeah, pretty amazing. The async thing is sweet too. Especially how require can go async and park the interpreter until the async job finishes - That's amazing. You could impl simulated blocking takes and all kinds of stuff with that.

john14:08:45

And being serializable... The distributed computing applications are fascinating

💯 1
âž• 1
teodorlu09:08:43

and you can even make that serializable function perform well. Simply provide well-performing functions from the "host" and provide them to the sci environment