Fork me on GitHub
#nbb
<
2023-10-14
>
sher04:10:26

Trying to get hold of the *file* but it is returning nil https://github.com/sher/nbb/blob/extend-fastify/examples/fastify/src/fnbb/plugins/statics.cljs#L1-L10 (you can check out the branch and run npm i && npm run start to confirm the behavior)

Bob B05:10:55

I believe that *file* in nbb probably works the same as babashka and JVM clojure, which is to say it's only usefully bound at compile/evaluation time, so reading it from a function won't work (unless the function call also happens as part of loading a file). <https://stackoverflow.com/a/12693068> is about JVM clojure, but I think the same thing applies (but definitely don't take my word for it, test it out) 🙂

sher08:10:27

Thank you for the help. But this is not to say that all *--* bindings are only available during comptime, right?

borkdude08:10:13

no, this is specific to *file*

sher08:10:26

@borkdude you mentioned https://github.com/babashka/nbb/discussions/218 that there is a workaround to referencing javascript this execution context. I couldn't find in the docs, could you share a sample? (whenever you have time)

borkdude08:10:54

this is always a reference to an object, but you can't use this in nbb for technical reasons. instead of using this you can first create the object,

(let [my-object #js {}]
 ...)
and then create some functions:
(let [my-function (fn [] (do-something-with-object my-object))]
 ...
and then attach that function to the object:
(aset my-object "foo" my-function)

borkdude08:10:21

and then you can call (.foo my-object)

sher08:10:44

Oh I see, the same thing I found here in https://github.com/babashka/sci#this-as. So there is no way to reference this within a function execution that is provided by an external library.

borkdude08:10:29

This is due to the fact that nbb is working through an interpreter. With #C03U8L2NXNC and #C03QZH5PG6M, alternative CLJS compiler projects I'm working on, this isn't a problem, you can just use this-as over there