lumo

:lumo: Standalone ClojureScript environment. Currently at version 1.9.0
johanatan 2018-05-23T18:40:23.000075Z

does anyone see a problem with the following code?:

(require 'goog.object)
(def spawn (goog.object/get (js/require "child_process") "spawn"))
(def proc (spawn "ls" (clj->js ["-lh", "/usr"])))
((.. proc -stdout -on) "data" (fn [data] (js/console.log data)))
(js/console.log "we are here")

johanatan 2018-05-23T18:40:31.000170Z

we are here doesn't print

johanatan 2018-05-23T18:40:47.000042Z

and i get the following exception:

Cannot read property '_events' of null
	 _addListener (events.cljs:201:19)
	 addListener (events.cljs:258:10)
	 Readable.on (_stream_readable.cljs:784:35)
	 (evalmachine.<anonymous>:11:26)
	 Script.runInThisContext (vm.cljs:65:33)
	 Object.runInThisContext (vm.cljs:197:38)
	 (<http://Object.yt|Object.yt>)
	 (Object.lumo.repl.caching_node_eval)
	 (NO_SOURCE_FILE &lt;embedded&gt;:5824:287)
	 z (NO_SOURCE_FILE &lt;embedded&gt;:5825:306)

anmonteiro 2018-05-23T18:46:15.000595Z

@johanatan looking, FWIW you can use (require '[child_process :refer [spawn]])

1😲
anmonteiro 2018-05-23T18:46:39.000541Z

feels more idiomatic to me

johanatan 2018-05-23T18:46:47.000673Z

ah yea. that's better

anmonteiro 2018-05-23T18:48:23.000471Z

@johanatan I think it’s a problem with JS this binding

anmonteiro 2018-05-23T18:48:27.000301Z

unfortunate

anmonteiro 2018-05-23T18:48:33.000330Z

here’s how you can write it:

anmonteiro 2018-05-23T18:48:43.000143Z

(.on (.-stdout ls) "data" (fn [data] (prn data)))

johanatan 2018-05-23T18:49:04.000801Z

ah, so .on instead of .-on ?

anmonteiro 2018-05-23T18:49:08.000214Z

(.. proc -stdout -on) -> (.on (.-stdout proc))

anmonteiro 2018-05-23T18:50:15.000390Z

if you start lumo with -v / --verbose here’s the generated code:

cljs.user=&gt; ((.. ls -stdout -on) "data" (fn [data] (prn data)))
Evaluating ((.. ls -stdout -on) "data" (fn [data] (prn data)))
cljs.user.ls.stdout.on.call(null,"data",(function (data){
return cljs.core.prn.call(null,data);
}))
Cannot read property '_events' of null
         _addListener (events.cljs:201:19)
         addListener (events.cljs:258:10)
         Readable.on (_stream_readable.cljs:784:35)
         (evalmachine.&lt;anonymous&gt;:1:24)
         Script.runInThisContext (vm.cljs:65:33)
         Object.runInThisContext (vm.cljs:197:38)
         (Object.wt)
         (Object.lumo.repl.caching_node_eval)
         (NO_SOURCE_FILE &lt;embedded&gt;:5824:287)
         z (NO_SOURCE_FILE &lt;embedded&gt;:5825:306)

johanatan 2018-05-23T18:50:15.000447Z

hmm, i got The "listener" argument must be of type Function

anmonteiro 2018-05-23T18:50:28.000681Z

vs:

cljs.user=&gt; (.on (.-stdout ls) "data" (fn [data] (prn data)))
Evaluating (.on (.-stdout ls) "data" (fn [data] (prn data)))
cljs.user.ls.stdout.on("data",(function (data){
return cljs.core.prn.call(null,data);
}))
#object[Socket [object Object]]

johanatan 2018-05-23T18:50:45.000636Z

oh, oops, extra set of parens

anmonteiro 2018-05-23T18:50:46.000673Z

you’re probably not passing "data" as the arg

anmonteiro 2018-05-23T18:50:50.000601Z

or something

anmonteiro 2018-05-23T18:51:05.000322Z

but do you see the difference above?

johanatan 2018-05-23T18:51:09.000115Z

yea, got it.

johanatan 2018-05-23T18:51:10.000186Z

thx!

anmonteiro 2018-05-23T18:51:10.000476Z

one is using .call(null)

anmonteiro 2018-05-23T18:51:21.000157Z

^ this binds the function this to null

anmonteiro 2018-05-23T18:51:26.000196Z

which is wrong

johanatan 2018-05-23T18:51:29.000351Z

btw, how do you get lumo to output the js it generates?

anmonteiro 2018-05-23T18:51:33.000447Z

lumo --verbose

johanatan 2018-05-23T18:51:37.000588Z

ah, cool

anmonteiro 2018-05-23T18:51:43.000016Z

or -v