sci

Bob B 2023-04-28T19:04:09.810579Z

I had a silly idea yesterday, and I'm wondering if there's a sci interface to something like eval-string from a vanilla-ish JS environment (details in 🧡 )

Bob B 2023-04-29T17:18:18.138209Z

still a bunch of kinks to work out, but preliminary looks are good with a lightly modified scittle:

πŸŽ‰ 1
Bob B 2023-04-28T19:08:15.834199Z

so, Gnome has an 'extension' mechanism in what seems to be a tweaked JS environment - you can write javascript that e.g. allows for adding panel items, tweaking how workspace switching works, etc... but from my initial research, it seems to lack an interactive experience (e.g. I want to get the list of panel items and maybe shuffle their positions around or something)... I immediately thought "JS, interactive, sci?"

borkdude 2023-04-28T19:10:11.836909Z

perhaps scittle already works, if you're lucky

borkdude 2023-04-28T19:10:34.513289Z

you might need to mock something like js/window or so

borkdude 2023-04-28T19:10:58.017759Z

and else it's not difficult to compile your own SCI thing

borkdude 2023-04-28T19:12:32.817129Z

scittle exposes scittle.core.eval_string

Bob B 2023-04-28T19:12:34.155669Z

scittle is what I was looking at as well - is there an 'exported function' at all (as opposed to the script blocks from the examples)? I might just need more hammock time to imagine the picture more clearly

Bob B 2023-04-28T19:13:13.713599Z

oh... lol... borkdude is the borkdude of running clojure on everything πŸ™‚

πŸ˜… 1
Bob B 2023-04-28T19:14:19.810319Z

I'm worried I might start using that expression at work and people will be confused, but it'll be an opportunity to shill

borkdude 2023-04-28T19:14:56.635519Z

hehe

borkdude 2023-04-28T19:16:08.811899Z

This might also be fun to play around with: https://github.com/squint-cljs/cherry/blob/main/doc/embed.md

schadocalex 2023-04-28T00:25:11.762279Z

Hi there, I want to eval a string with sci "(js/Promise.resolve 3)", but I can’t figure out how to setup sci to allow only js/Promise and not the whole js namespace

borkdude 2023-05-01T09:45:23.495099Z

I added docs now: https://github.com/babashka/sci#classes

schadocalex 2023-05-01T21:26:50.470049Z

Sorry I forgot to create the issue (should have done it immediately). Very nice tho, very explicit, thanks

borkdude 2023-05-01T21:32:12.786839Z

np

schadocalex 2023-04-28T07:39:58.615659Z

(sci/eval-string "(js/Promise.)" {:classes {'js/Promise js/Promise}})
(sci/eval-string "(js/Promise)" {:classes {'js/Promise js/Promise}})
(sci/eval-string "(js/Promise.resolve 3)" {:classes {'js/Promise js/Promise}})
(sci/eval-string "(js/Promise.resolve 3)" {:classes {'js {'Promise js/Promise}}})
(sci/eval-string "(Promise.resolve 3)" {:classes {'Promise js/Promise}})
(sci/eval-string "(my-promise 3)" {:namespaces {'user {'my-promise js/Promise.resolve}}})
(sci/eval-string "(js/Promise.resolve 3)" {:namespaces {'js {'Promise js/Promise}}})
(sci/eval-string "(.resolve js/Promise 3)" {:namespaces {'js {'Promise js/Promise}}})
(sci/eval-string "(js/Promise.resolve 3)" {:namespaces {'js {'Promise js/Promise}}})
(sci/eval-string "(.resolve js/Promise 3)" {:namespaces {'js {'Promise js/Promise}}})
(sci/eval-string "(.resolve js/Promise 3)" {:namespaces {'js {'Promise goog.global.Promise}}})

schadocalex 2023-04-28T07:40:22.026199Z

none of them work, but I found one working:

(sci/eval-string "(.resolve js/Promise 3)" {:namespaces {'js {'Promise goog/global.Promise}} :classes {:allow :all}})

borkdude 2023-04-28T07:40:50.724919Z

What I meant was:

{:classes {'js #js {:Promise js/Promise}}}

borkdude 2023-04-28T07:41:15.088489Z

classes needs to be a map of symbol to host specific object, so not a clojure data structure

schadocalex 2023-04-28T07:43:22.815839Z

(sci/eval-string "(js/Promise.resolve 3)" {:classes #js {:js #js {:Promise js/Promise}}}) :repl/exception! ; ; Execution error (Error) at (<cljs repl>:1). ; [object Object] is not ISeqable

schadocalex 2023-04-28T07:43:43.514119Z

Ok but in the readme, {:classes {'js goog/global :allow :all}} it is a clojure map?

schadocalex 2023-04-28T07:44:06.080649Z

oh maybe mix the two

borkdude 2023-04-28T07:44:06.346139Z

I'm sorry, like this:

cljs.user=> (sci/eval-string "(js/Promise.resolve 1)" {:classes {'js #js {:Promise js/Promise}}})
#object[Promise [object Promise]]

πŸŽ‰ 1
borkdude 2023-04-28T07:44:16.287199Z

so a clojure map of symbol to a host object

borkdude 2023-04-28T07:44:37.308209Z

(corrected the other message now too)

schadocalex 2023-04-28T07:45:02.701449Z

Thanks! I've spent so much time on this

borkdude 2023-04-28T07:45:43.528589Z

Is there a place in the documentation where you think this might be added so others would save time in the future?

schadocalex 2023-04-28T07:46:15.527859Z

My feelings is there is no doc about this x)

borkdude 2023-04-28T07:46:39.412099Z

OK, if you think it's worth it, can you make an issue about the above?

schadocalex 2023-04-28T07:46:52.945509Z

but I would say here https://github.com/babashka/sci#classes

schadocalex 2023-04-28T07:46:58.179219Z

Sure!

schadocalex 2023-04-28T07:47:30.393979Z

Like I could not transpose the JAVA example for js/xxxx

borkdude 2023-04-28T07:48:36.494129Z

yes, I can see how that is confusing. But globalThis is basically also a JS object with keys of which Promise is one of the keys, so {'js js/globalThis} is the global map

borkdude 2023-04-28T07:49:03.303749Z

and {'js #js {:Promise js/Promise}} is basically the same but restricted to one key

schadocalex 2023-04-28T07:53:25.903349Z

yes now I see, but I didn't know that js/ was a valid syntax to access a JS Object (the / part)

schadocalex 2023-04-28T07:54:45.308799Z

thanks!

schadocalex 2023-04-28T07:55:53.636589Z

(I'll make the issue but in 2 hours I have to rush some code)

schadocalex 2023-04-28T07:57:55.593149Z

hmm, got an error using it

schadocalex 2023-04-28T07:58:08.528939Z

minimal example (sci/eval-string "(.then (js/Promise.resolve 1) (fn [x]))" {:classes {'js #js {:Promise js/Promise}}}) :repl/exception! ; ; Execution error (ExceptionInfo) at (<cljs repl>:1). ; Method then on function Promise() { [native code] } not allowed!

schadocalex 2023-04-28T08:00:43.795449Z

it's the same error I got from other tries

borkdude 2023-04-28T08:07:16.880929Z

Try allow all

borkdude 2023-04-28T08:07:49.585709Z

This will still only give access to your exposed promise

schadocalex 2023-04-28T08:10:09.649249Z

works, thanks!

borkdude 2023-04-28T06:42:34.682009Z

You can create an object with a js key and Promise inside of that and then provide that under classes

borkdude 2023-04-28T06:50:26.222139Z

Or you can expose Promise as a function or non-global