lumo

:lumo: Standalone ClojureScript environment. Currently at version 1.9.0
rberger 2017-08-02T08:38:16.062258Z

If i do something like the following to be able to use an npm library simple-git

(defonce git (node/require "simple-git"))
and I want to use the function tags from that library, how do I do that? (.tags git cb) gives me:
mygit.core.git.tags is not a function
I’m not quite getting how to use the npm libraries in lumo if they have multiple entry points like tags Thanks!

richiardiandrea 2017-08-02T10:13:23.280859Z

don't know if worth an issue but I have noticed a couple of warning during compilation of master:

### Compiling Macro Namespaces
cljs.user=>        #_=>                 #_=>                 #_=>                 #_=>                 #_=>                 #_=>                 #_=>                 #_=>                 #_=>                 #_=>                 #_=>                 #_=>                 #_=>                 #_=>                 #_=>                 #_=>
WARNING: Use of undeclared Var cljs.spec.test.alpha$macros/eval at line 112 cljs/spec/test/alpha.cljc
WARNING: Use of undeclared Var cljs.spec.test.alpha$macros/eval at line 135 cljs/spec/test/alpha.cljc
WARNING: Use of undeclared Var cljs.spec.test.alpha$macros/eval at line 245 cljs/spec/test/alpha.cljc
nil

metametadata 2017-08-02T11:53:57.219017Z

@rberger according to docs, you need to pass a directory first: (defonce git ((node/require "simple-git") "my-dir"))

anmonteiro 2017-08-02T15:38:47.975704Z

@richiardiandrea those are expected

anmonteiro 2017-08-02T15:39:03.986784Z

We set! eval later

richiardiandrea 2017-08-02T15:39:24.001613Z

ok thanks! it was then a good idea to write here first

2017-08-02T17:14:03.622208Z

I get a difference with requireing from node and lumo

(js/require "web-audio-api")
Octal literals are not allowed in strict mode.
....
> require('web-audio-api')
{ AudioContext: [Function: AudioContext],
  AudioParam: [Function: AudioParam],
  AudioNode: [Function: AudioNode],
  AudioDestinationNode: [Function: AudioDestinationNode],
  AudioBuffer: { [Function: AudioBuffer] filledWithVal: [Function], fromArray: [Function] },
  AudioBufferSourceNode: [Function: AudioBufferSourceNode],
  GainNode: [Function: GainNode],
  constants: { BLOCK_SIZE: 128 } }
> 

anmonteiro 2017-08-02T17:18:41.790402Z

@hlolli (.setFlagsFromString (js/require "v8") "--no-use_strict")

2017-08-02T17:20:06.842787Z

Magic! wow thanks 🙂 @anmonteiro

anmonteiro 2017-08-02T17:20:26.855189Z

@hlolli not magic. we set strict mode in lumo by default because it’s slightly faster

anmonteiro 2017-08-02T17:20:46.867574Z

we’ve had a few people bumping into this, I don’t know if it’s a good default yet

anmonteiro 2017-08-02T17:21:03.877852Z

if more people complain, maybe I can be convinced to change the default to be similar to Node’s

dominicm 2017-08-02T17:21:59.912269Z

@anmonteiro crazy idea, can you override the message? If so, you can link to information

anmonteiro 2017-08-02T17:22:24.927427Z

@dominicm open a ticket? 🙂

2017-08-02T17:22:31.931568Z

yes, or make an FAQ see if that helps.. yes bit old school to use Octal literals, read that its not recommended to be used.

anmonteiro 2017-08-02T17:22:31.931752Z

I don’t know if it’s possible but can explore

dominicm 2017-08-02T17:22:56.946487Z

On mobile right now, no github login. Will do if I can remember sometime tomorrow

anmonteiro 2017-08-02T17:23:13.956760Z

I’ll open

anmonteiro 2017-08-02T17:23:28.966332Z

thanks for the suggestion

dominicm 2017-08-02T17:23:29.966569Z

Thanks

anmonteiro 2017-08-02T17:25:24.037946Z

https://github.com/anmonteiro/lumo/issues/225

2017-08-02T17:30:35.235494Z

I guess I ask the web-audio-api people to clean the octal numbers, now non strict is seemingly conflicting with strict stuff

(new (.-AudioContext (js/require "web-audio-api")))

'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
	 (new)
	 chai.expect (/home/hlolli/csound/panaeolus/node_modules/chai/lib/chai/interface/expect.cljs:9:12)

2017-08-02T17:35:40.427909Z

to be fair, the blame is not web-audi-api but 'abc'

anmonteiro 2017-08-02T17:38:47.544517Z

@hlolli it seems like you’re still in strict-mode

2017-08-02T17:40:36.612217Z

yes, you were right. Now it works. If I try requireing in strict, then turn if off, try again, then some dirt(state) is stored in the web-audio-api instance, so this works if it's done in right sequence!

anmonteiro 2017-08-02T17:45:13.782383Z

@hlolli just set strict mode to false at the beginning of your script

anmonteiro 2017-08-02T17:45:27.791504Z

you may run into issues if you enable it again

2017-08-02T17:46:41.836799Z

yup yup, I will try to do it conditionally, I'm attempting to use webassembly to run csound in my livecoding app, so if a user wants native installation, then I could see a potential efficiency boost from strict.

2017-08-02T17:47:43.875552Z

that means with electron, if I play the cards right, panaeolus the livecoding app Im making, could be 1 executeable file that could potentially run out of the box without deps.

2017-08-02T17:48:56.921315Z

if that succeeds, I'll do a release party in Berlin, free wine, all lumoers welcome 🙂

anmonteiro 2017-08-02T18:22:25.191522Z

just built Lumo against Node 8.3.0-rc which has V8 6.0

anmonteiro 2017-08-02T18:23:06.217371Z

should have a different perf profile now that TF + I are enabled by default

dominicm 2017-08-02T18:42:30.949810Z

What are they?

anmonteiro 2017-08-02T18:46:06.084262Z

TurboFan + Ignition

anmonteiro 2017-08-02T18:46:33.100828Z

the new V8 execution pipeline

dominicm 2017-08-02T18:57:50.519826Z

Sounds exciting

rberger 2017-08-02T19:01:24.654614Z

Let me restate my problem. Once I require a npm library (any npm library that has more than a single function) such as:

(defonce git (node/require "simple-git" "."))
How do I access the functions associated with the library? For instance that library has a .tag function. How would I execute that function in the library that would be represented by the object git? I’ve tried several ways and they don’t work, I’m sure I’m missing something easy, but I can’t figure it out.

rberger 2017-08-02T19:04:18.763024Z

If I use the javascript accessor method

(.tags git (fn [err, tgs] (println "Yay; " tgs))))
I get :
mygit.core.git.tags is not a function

rberger 2017-08-02T19:05:14.796842Z

This is a general question on how to call functions from npm libraries

dominicm 2017-08-02T19:06:02.825203Z

The function is .tag, you're calling tags

rberger 2017-08-02T19:07:16.868479Z

(.tag git (fn [err, tgs] (println "Yay; " tgs))))
...
mygit.core.git.tag is not a function

rberger 2017-08-02T19:08:18.904411Z

(It is .tags for that library)

rberger 2017-08-02T19:09:34.947345Z

Its failing the same way for both. I’m pretty sure there is something wrong with how I’m calling the library from clojurscript in lumo

rberger 2017-08-02T19:10:58.996620Z

I found that for a npm library that has a single entry I can do the following:

(defonce gitlatest (node/require "git-latest-semver-tag"))
(gitlatest (fn [err,tag] (println "err: " error " tags: " tag))))
And that works

dominicm 2017-08-02T19:58:54.627886Z

I'm not familiar with how you're using require in the first example, what does the . Do?

rberger 2017-08-02T20:03:12.783094Z

That was in response to @metametadata statement that I needed to have a path passed as an argument to the library. But it doesn’t work with or without the "."

dominicm 2017-08-02T20:04:14.819827Z

Yeah, you don't pass it there

rberger 2017-08-02T20:04:32.830338Z

Maybe I’m confusing things with this example using simple-git My question is really how do I call a function from an npm library that has mulitple functions in clojurescript in lumo

anmonteiro 2017-08-02T20:04:38.833787Z

require takes 1 argument

rberger 2017-08-02T20:04:50.840746Z

It fails the same way with no argument

anmonteiro 2017-08-02T20:04:57.844798Z

(def my-lib (js/require "my-lib"))

(.someFunction my-lib)

anmonteiro 2017-08-02T20:05:05.849586Z

if the exports of my-lib is an object

anmonteiro 2017-08-02T20:05:14.855091Z

but the library can also export a single function

anmonteiro 2017-08-02T20:05:23.860579Z

in which case you call (my-lib) directly

rberger 2017-08-02T20:09:37.002112Z

Here’s my script:

(ns mygit.core
  (:require [cljs.nodejs :as node]))

(node/enable-util-print!)

(def git (js/require "simple-git"))
(defn do-tags []
   (.tags git  (fn [err, tgs] (println "Yay; " tgs))))

(defn -main []
  (do-tags))
And when I run it with: lumo -c src -m mygit.core I get:
mygit.core.git.tags is not a function
	 mygit$core$do_tags (evalmachine.<anonymous>:5:23)
	 mygit$core$_main (evalmachine.<anonymous>:10:27)
	 cljs.core.Var.cljs$core$IFn$_invoke$arity$0 (NO_SOURCE_FILE <embedded>:389:475)
	 Function.cljs.core.apply_to_simple.cljs$core$IFn$_invoke$arity$2 (NO_SOURCE_FILE <embedded>:777:120)
	 Function.cljs.core.apply.cljs$core$IFn$_invoke$arity$2 (NO_SOURCE_FILE <embedded>:790:244)
	 (NO_SOURCE_FILE <embedded>:6352:455)
	 (NO_SOURCE_FILE <embedded>:5947:221)
	 z (NO_SOURCE_FILE <embedded>:5948:13)
	 Object.cljs.js.eval_str_STAR_ (NO_SOURCE_FILE <embedded>:5949:61)
	 Function.cljs.js.eval_str.cljs$core$IFn$_invoke$arity$5 (NO_SOURCE_FILE <embedded>:5952:508)

anmonteiro 2017-08-02T20:10:09.020356Z

@rberger I’ll look into it in like 20 min and let you know

anmonteiro 2017-08-02T20:10:21.026782Z

in a meeting right now

rberger 2017-08-02T20:10:28.031024Z

Really appreciate it!!! In the mean time I’ll try with another library

2017-08-02T20:13:13.122945Z

@rberger what happens if you do

(def git ((js/require "simple-git") "./"))

rberger 2017-08-02T20:13:28.131484Z

trying

rberger 2017-08-02T20:14:42.173416Z

Well that seemed to kind of work. The output is:

Yay;  #object[TagList [object Object]]

rberger 2017-08-02T20:15:26.198286Z

So the issue was how I initialized even though the simple-git says the path is optional… Do I just do a js->clj on that object now?

2017-08-02T20:15:59.216978Z

You probably want to get properties of that object, with aget or .-property

rberger 2017-08-02T20:16:32.236023Z

Oh, Ok, Looks like you solved my problem… Thanks!!

anmonteiro 2017-08-02T20:16:39.239372Z

@rberger you can do js/console.log FWIW

anmonteiro 2017-08-02T20:16:50.245842Z

I think it prints JS objects more nicely

anmonteiro 2017-08-02T20:17:48.278296Z

glad your problem is solved. In your case, simple-git is of type 2 in the examples I enumerated above

rberger 2017-08-02T20:17:49.278725Z

Ok, though the eventual thing will be to actually use the results. Is there any easy way to turn these async calls into sync calls? I’m pretty new to the JS interopt

anmonteiro 2017-08-02T20:17:51.280063Z

the module exports a single function

anmonteiro 2017-08-02T20:18:09.290110Z

turning async -> sync is not possible

rberger 2017-08-02T20:23:11.464355Z

Any pointers on good patterns of using such async calls in clojurescript scripting applications? My hope is to use Lumo in place of bash or other scripting for ops and for use with AWS Lambda. Having the vast npm libraries is the key right now, but this async style is non-intuitive for me. Thanks again for the help getting me past what turned out to be a simple error.

2017-08-02T20:26:23.574242Z

I was going to write something long, but in your case, you already have a callback on receive, so the difficult part is done for you there, just call a function, a callback within the callback, where you process the response.

rberger 2017-08-02T20:28:23.641106Z

So do I need to use something like core.async to mix the async stuff with my more linear flow of the script?

2017-08-02T20:31:55.766544Z

if you want the script to have some return value instead of just side effects, then maybe core.async could help you.

2017-08-02T20:34:27.854084Z

you will find it under the name Andare for lumo (patched for self-hostet cljs repls)

rberger 2017-08-02T20:50:13.389471Z

Will give it a try. Was hoping to avoid that, but looks unavoidable. There were some interesting attempts to hide this, but I couldn’t quite get it to work and its a bit old now: http://bryangilbert.com/post/code/clojure/escape-callback-hell-core-async/

2017-08-02T20:54:23.532150Z

in your case you could do something like

(def res-chan (chan 1))
(defn do-tags []
   (.tags git  (fn [err, tgs] (>! res-chan tgs))))

(defn -main []
(go
  (do-tags)
  (<! res-chan))

rberger 2017-08-02T20:55:16.562314Z

Thanks! That will give me something to go on. Appreciate the extra effort to help educate me!

🍻 1