Fork me on GitHub
#sci
<
2023-04-28
>
schadocalex00:04:11

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

borkdude06:04:34

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

borkdude06:04:26

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

schadocalex07:04:58

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

schadocalex07:04:22

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

borkdude07:04:50

What I meant was:

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

borkdude07:04:15

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

schadocalex07:04:22

(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

schadocalex07:04:43

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

schadocalex07:04:06

oh maybe mix the two

borkdude07:04:06

I'm sorry, like this:

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

πŸŽ‰ 2
borkdude07:04:16

so a clojure map of symbol to a host object

borkdude07:04:37

(corrected the other message now too)

schadocalex07:04:02

Thanks! I've spent so much time on this

borkdude07:04:43

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

schadocalex07:04:15

My feelings is there is no doc about this x)

borkdude07:04:39

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

schadocalex07:04:30

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

borkdude07:04:36

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

borkdude07:04:03

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

schadocalex07:04:25

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

schadocalex07:04:53

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

schadocalex07:04:55

hmm, got an error using it

schadocalex07:04:08

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!

schadocalex08:04:43

it's the same error I got from other tries

borkdude08:04:16

Try allow all

borkdude08:04:49

This will still only give access to your exposed promise

schadocalex08:04:09

works, thanks!

schadocalex21:05:50

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

Bob B19:04:09

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 B19:04:15

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?"

borkdude19:04:11

perhaps scittle already works, if you're lucky

borkdude19:04:34

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

borkdude19:04:58

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

borkdude19:04:32

scittle exposes scittle.core.eval_string

Bob B19:04:34

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 B19:04:13

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

πŸ˜… 2
Bob B19:04:19

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

Bob B17:04:18

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

πŸŽ‰ 2