Fork me on GitHub
#nbb
<
2022-12-04
>
sparkofreason01:12:23

Is there a way to configure nbb with sci options from JavaScript? I'm experimenting using nbb (or maybe just sci) for embedded scripting in our app, and would like to limit things that could create infinite loops.

borkdude10:12:45

we could just expose the SCI API to nbb

sparkofreason14:12:21

After looking at the nbb code and seeing how it uses sci, I suppose another option is to use sci directly. I'd only need to evaluate a single short file (maybe with some common functions injected), no async. Maybe I could do a simplified version of eval-next https://github.com/babashka/nbb/blob/main/src/nbb/core.cljs#L383

borkdude14:12:25

@U066LQXPZ In this case, you might just want to use SCI directly in a bespoke CLJS project and compile it to JS with the interface you like

sparkofreason14:12:41

Ah, right, all of that API isn't available to JS.

borkdude14:12:18

There used to be a JS api, but it was a pain to maintain

sparkofreason14:12:58

Ah, I guess that's why those docs are commented out in the README. They still show up on https://www.npmjs.com/package/@borkdude/sci

borkdude14:12:18

it's not a good habit to remove npm packages, is it? ;)

sparkofreason14:12:51

No, probably not, though I see now that it's pretty old compared to the latest sci release.

sparkofreason14:12:24

Maybe add some notice?

borkdude15:12:56

Does npm support archiving / deprecating a project?

sparkofreason15:12:53

I don't know, haven't worked with it from that end.

sparkofreason16:12:06

Looks like it meets the criteria for being unpublished: https://docs.npmjs.com/policies/unpublish

borkdude16:12:26

I wonder who will start complaining...

borkdude16:12:40

maybe I should have checked the usages first :/

sparkofreason16:12:27

It said there were zero dependents.

borkdude16:12:04

anyway, one should probably upgrade to the newest SCI instead of using one from 3 years ago

1
sparkofreason16:12:15

Back to the original question...would you anticipate any problems pushing sci options through something like nbb.loadFile?

borkdude16:12:04

That's not what I plan to support, but what I would support is this:

(nbb.loadString `(require '[sci.core :as sci]) (sci/eval-string "{yourProgram}")`)
or so. Not sure if that would help you and what the requirements of yourProgram are - e.g. should it have access to the environment? Another thing you could use is a Node vm which you can kill after x time. I've used this in the nbb REPL. E.g. when you evaluate (range) you can do ctrl-c without losing your previous state

sparkofreason16:12:27

I'll have to think about it. It's just a simple script returning one function and some data, and then the potential execution of the function. I'm trying to pitch this as giving a really easy user experience (despite being Clojure 😜). But I'm just in the initial stages of thinking about this, so there's opportunity to adapt. For selling it to the team, it would be a big win if it was "just a library". If we're going to go full VM or similar, we'd probably just go JS for this.

borkdude16:12:27

I didn't mean VM as in a dedicated virtual machine

borkdude16:12:37

I meant a Node.js vm, it's an built-in sandboxing-like thing

borkdude16:12:20

Just try npx nbb, then (range) and then hit ctrl-c. This is implemented with the Node.js vm library

sparkofreason17:12:44

Ah, cool, will check that out. Thanks!

sparkofreason17:12:03

That's good to know about, though I think it comes back to the same thing, where if that's the only safety mechanism we might as well just use JS. I'll see where I can get calling sci from inside nbb.

borkdude17:12:54

yeah, I think we could maybe support load-string or so with extra SCI opts but maybe not exposed via the normal namespace. Feel free to experiment locally and see how far you get

1
sparkofreason18:12:17

From nbb running jacked-in from calva:

(require '[sci.core :as sci])
(sci/eval-string "(inc 1)")  ; => Could not resolve symbol: sci/eval-string
require worked, so this is puzzling.

sparkofreason18:12:43

Actually, maybe require didn't work. I tried it again and the first time it gave Could not resolve symbol: -invoke, subsequently nil.

borkdude18:12:13

$ nbb
Welcome to nbb v1.1.150!
user=> (require '[sci.core :as sci])
"Error: Could not find namespace: sci.core"

borkdude18:12:21

sci itself needs to be exposed through the sci options

Benjamin15:12:48

is j/call not a macro?

[applied-science.js-interop :as j]
also
(j/lit {-b 10})
ah now I see it nbb readme it says it is not supported

borkdude15:12:44

Those macros are only there for externs I think, in nbb you can just directly call those things