Fork me on GitHub
#clojurescript
<
2022-02-28
>
agold02:02:02

Apparently I can use (js* "dirname") to read the dirname variable in node. Are there other uses for the js* special form? Is this documented anywhere?

p-himik02:02:37

What prevents you from using just js/__dirname? IIRC js* is an internal thing.

pez09:02:19

How come that in the REPL:

js/sessionStorage
=> #object [Storage [object Storage]]
While also in the REPL:
(let [sessionStorage js/sessionStorage]
  sessionStorage)
=> nil
What gives? (I'm now using this, but I'm still curious about what it is I am not getting.)
(let [sessionStorage  (aget js/window "sessionStorage")]
  sessionStorage)
=> #object [Storage [object Storage]]

p-himik12:02:06

Never use aget for anything but JS arrays. In the case above, you can replace it with (.-sessionStorage js/window) or even js/window.sessionStorage. Regarding the issue itself - looks like a bug in CLJS compiler.

p-himik12:02:05

Also, seems like using any other name than sessionStorage on the CLJS side also fixes the issue. Personally, I prefer using cebab-case for CLJS variables, so I'd go with session-storage. Or just use js/sessionStorage directly, without binding it to a different name - after all, there's no reason to do it except for maybe some aesthetic one since js/... does not incur any computations.

pez13:02:05

Thanks. What's the reason to not use aget? I'm thinking of it as equivalent to o["some-attribute"].

pez13:02:21

I'll switch to kebab-case especially since that fixes the issue. The reason it was camelCase was that I wanted it to show that it is a JS thing in there. But I agree it is probably better to keep it kebab.

p-himik13:02:22

> What's the reason to not use aget? Because it is explicitly only for arrays. Also, it makes it impossible to the compiler to analyze what is used in which context. Might lead to incorrect externs inference IIRC.

🙏 1
pez13:02:25

I'm not trying to avoid computations, actually. 😃 I'm mocking local and session storage to be able to test it and also for the code to just run in nodejs, where the test runner lives.

👍 1
lilactown13:02:26

I just ran into this problem the other day, but hadn't made a bug report yet!

lilactown13:02:31

I was trying to create a local binding

(let [window (js/window.open ,,,)]
  ,,,)
and was confused why I was getting nil on the left hand side. renaming the binding to win fixed it

pez13:02:35

Haha, I guess you realized it was a bug. I was more like ”TIL!" and went to the rvalue to see if I could make it return what I wanted. 😃

lilactown13:02:06

it took some fiddling at the REPL before I figured out it was a bug

dnolen14:02:56

@pez you are at the top-level in the REPL

dnolen14:02:20

that is somewhat tricky to handle since we cannot possibly know about all things at the top-level

dnolen14:02:13

this would be more concerning if this was true within functions - which I believe we fixed many cases long ago

pez17:02:45

@dnolen I ran into it in a function. The above was merely to try minimize the question.

dnolen17:02:21

right the REPL minimization is not actually a minimization at all

dnolen17:02:24

because of top-level problems

dnolen17:02:28

so it doesn't tell you much

dnolen17:02:18

need something inside a defn that demonstrates the issue - again we have lots of shadowing code so need to find where gap is

pez20:02:51

Roger that. I think I can provide a repro.

pez07:03:30

Here's a session using ClojureScript Getting Started instructions:

$ clj -M --main cljs.main --compile hello-world.core --repl
ClojureScript 1.11.4
cljs.user=> (defn f1 []
  (let [window js/window]
    window))

(defn f2 []
  (let [vindow js/window]
    vindow))
#'cljs.user/f1
cljs.user=> cljs.user=> #'cljs.user/f2
cljs.user=> (f1)
nil
cljs.user=> (f2)
#object[Window [object Window]]
cljs.user=> 
I've updated https://ask.clojure.org/index.php/11607/binding-js-variable-to-variable-binds-nil. I think the JIRA ticket also needs a new description.

dnolen17:02:56

for example what you might be seeing this because of JS var hoisting

dnolen17:02:06

and completely unrelated issue to trying let at the REPL

Sam Ritchie19:02:32

Hey all - is there a built in function I can use to replace the js* form in (js* "~{} ** ~{}" (js/BigInt 10) (js/BigInt 2))

Sam Ritchie19:02:02

Math/pow won’t work on bigints

p-himik19:02:38

Doesn't seem like it - the whole CLJS codebase does not include ** anywhere except in comments.

Sam Ritchie19:02:03

that’s what I had found too

pez07:03:30

Here's a session using ClojureScript Getting Started instructions:

$ clj -M --main cljs.main --compile hello-world.core --repl
ClojureScript 1.11.4
cljs.user=> (defn f1 []
  (let [window js/window]
    window))

(defn f2 []
  (let [vindow js/window]
    vindow))
#'cljs.user/f1
cljs.user=> cljs.user=> #'cljs.user/f2
cljs.user=> (f1)
nil
cljs.user=> (f2)
#object[Window [object Window]]
cljs.user=> 
I've updated https://ask.clojure.org/index.php/11607/binding-js-variable-to-variable-binds-nil. I think the JIRA ticket also needs a new description.