sci

borkdude 2022-05-19T12:55:07.704809Z

SCI lazy loading namespaces: https://github.com/babashka/sci/blob/master/doc/async.md#lazy-loading-a-namespace

mkvlr 2022-05-19T18:13:05.026039Z

nice, we should give this a spin in nextjournal

borkdude 2022-05-19T19:09:14.123659Z

could try next week - I assume you mean http://nextjournal.com, the product right?

👍 1
awb99 2022-05-19T20:00:51.045099Z

(sci/add-namespace! ctx libname sci-ns))) ------------^------------------------------------------------------------------- Use of undeclared Var sci.core/add-namespace!

awb99 2022-05-19T20:01:00.711469Z

I am getting this warning from shadow-cljs.

awb99 2022-05-19T20:02:00.708669Z

weird.

awb99 2022-05-19T20:02:06.957689Z

its a normal function.

awb99 2022-05-19T20:14:53.799549Z

does clojars babashka 0.3.5 contain this function?

awb99 2022-05-19T20:16:41.793349Z

what is weird: clojars says 0.3.5 was published on may 1. but in github releases it says 0.3.5 was published on may 16.

awb99 2022-05-19T20:19:55.111999Z

So I fixed it by using sci via git dependency.

awb99 2022-05-19T20:20:07.727489Z

But the difference of github releases and clojars is still weird.

borkdude 2022-05-19T20:29:45.053079Z

yes, it's not in 0.3.5, just on master

borkdude 2022-05-19T20:30:03.584249Z

I forgot to make a release on github for 0.3.5 so I did that later

awb99 2022-05-19T20:37:40.363049Z

thanks for the explaination!

awb99 2022-05-19T20:37:43.390839Z

@borkdude

awb99 2022-05-19T20:37:47.017699Z

I have one question:

awb99 2022-05-19T20:37:57.980359Z

So I am adding lots and lots of extensions,

awb99 2022-05-19T20:38:01.890989Z

just with edn metadata.

awb99 2022-05-19T20:38:47.667609Z

{:name "js-interop" ; build :lazy false :cljs-namespace [; clojure clojure.string clojure.walk applied-science.js-interop] :cljs-bindings {} :cljs-ns-bindings {'string {'split clojure.string/split 'join clojure.string/join 'escape clojure.string/escape 'blank? clojure.string/blank? 'replace clojure.string/replace 'lower-case clojure.string/lower-case} 'j {'get applied-science.js-interop/get 'call applied-science.js-interop/call 'get-in applied-science.js-interop/get-in 'call-in applied-science.js-interop/call-in 'obj applied-science.js-interop/obj} 'walk {'postwalk clojure.walk/postwalk 'prewalk clojure.walk/prewalk 'keywordize-keys clojure.walk/keywordize-keys 'walk clojure.walk/walk 'postwalk-replace clojure.walk/postwalk-replace 'prewalk-replace clojure.walk/prewalk-replace 'stringify-keys clojure.walk/stringify-keys}} }

awb99 2022-05-19T20:38:50.919919Z

this is one example.

awb99 2022-05-19T20:39:07.680809Z

lazy:false means this module put into the main js file of the bundle

awb99 2022-05-19T20:39:20.428349Z

lazy:true means put this into a separate js module that gets lazy loaded.

awb99 2022-05-19T20:39:41.187539Z

the reason why I go with this edn file is,

awb99 2022-05-19T20:40:02.452019Z

so that I do not need any sci dependency in libraries that might be used without sci.

awb99 2022-05-19T20:40:15.230509Z

I autogenerate the rest based on many of this definitions.

awb99 2022-05-19T20:40:31.796609Z

Now I would like to build the sci environment in the best way possible

awb99 2022-05-19T20:40:52.849639Z

I understand that macros from clojurescript will not work, and they need to be rewritten.

awb99 2022-05-19T20:40:58.206669Z

but most of the other stuff should work.

awb99 2022-05-19T20:41:11.587349Z

So this wrappers you use in your sci-namespace definitions,

awb99 2022-05-19T20:41:25.120929Z

I guess they are all macros that supply meta data to the sci environment?

awb99 2022-05-19T20:42:13.083879Z

Or is there any other reason why an export would better not be defined in an edn module?

borkdude 2022-05-19T20:43:14.180179Z

clojure string and walk are already included in SCI so it doesn't make sense to make those lazy. you can see the config for js-interop here: https://github.com/babashka/sci.configs/blob/main/src/sci/configs/applied_science/js_interop.cljs in fact, you can use sci.configs as a library to get the config

borkdude 2022-05-19T20:44:40.928579Z

you can check out nbb's shadow config: https://github.com/babashka/nbb/blob/main/shadow-cljs.edn everything except the core module is loaded lazily

borkdude 2022-05-19T20:46:21.236149Z

the idea is to just compile separate modules and some are optional. the optional ones add something to the sci context when loaded

borkdude 2022-05-19T20:48:17.564809Z

you can even make an optional module which first defines the sci context, so you can exclude SCI for a very thin build

borkdude 2022-05-19T20:48:29.043079Z

and then the other ones add something to the context on load

awb99 2022-05-19T21:04:46.560519Z

its an amazing idea! I will start using sci.configs !

awb99 2022-05-19T21:04:53.024469Z

Do you plan to add more configs in the future?

awb99 2022-05-19T21:05:36.438919Z

Or in other works: do you plan to make sci.configs to somethinig similar to the babashka pods registry?

borkdude 2022-05-19T21:06:38.320339Z

yes, more configs will be there probably

awb99 2022-05-19T21:07:01.509219Z

PERFECT!

awb99 2022-05-19T21:07:03.814649Z

Great news!

awb99 2022-05-19T21:08:53.752589Z

thanks a lot!

awb99 2022-05-19T22:05:59.423229Z

It all works now @borkdude. But there is one edge case:

awb99 2022-05-19T22:06:01.373249Z

(ns example3 (:require [funny])) (funny/joke)

awb99 2022-05-19T22:06:25.136609Z

(ns example4 (:require [funny :refer [joke]])) (joke)

awb99 2022-05-19T22:06:38.757359Z

(ns example5 (:require [funny :as f])) (f/joke)

awb99 2022-05-19T22:07:22.437729Z

so all 3 examples work!

awb99 2022-05-19T22:07:29.317019Z

when I start a new sci context,

awb99 2022-05-19T22:07:39.123609Z

lazy loading works, sci eval works - perfect.

awb99 2022-05-19T22:07:40.102409Z

BUT

awb99 2022-05-19T22:07:49.177049Z

the example5, whith :as f

awb99 2022-05-19T22:07:52.767169Z

that does not work.

borkdude 2022-05-19T22:08:46.718279Z

can you make a github issue with repro? then I'll take a look tomorrow

awb99 2022-05-19T22:10:48.649169Z

sure

awb99 2022-05-19T22:58:07.548909Z

ticket done. I kept it really short. But I verified all a couple of times.

borkdude 2022-05-20T14:24:59.750869Z

ticket solved

awb99 2022-05-20T20:08:00.937759Z

Great! Thankd a lot!

awb99 2022-05-20T20:11:22.288549Z

Just some report on the async loader: in goldly I have 20 source code files that get evaled on startup. And I have a browser based sci clojurescript repl. I yesterday (before you fixed the issue) changed all requires so that I would avoid :refer and :as. All working perfect! This async loader really is the key issue to loading modules. I still have an async ui renderer loader that works with sync eval only. But I intend to rewrite it as soon as I did some more tests on the async eval.

awb99 2022-05-20T20:13:14.442419Z

What I noticed is that even though it is just a very minor interface differene (promise instead of result) I had to change aa lot of code to make it work. So I will migrate 100% to the async loader. This is so that I can use modules. Because module loading does not work in sync eval.

👍 1
awb99 2022-05-19T05:13:15.045759Z

async evals with the async-loader work great. but there are some situations where it does not work: The code below is a snippet which I eval in its entirity in sci in the browser. When I require [funny :as f] then it works, so (f/joke) in the code works. But when I just have [funny] and then I call (funny/joke), then it does NOT work. If my require is [funny :as funny] then (funny/joke) works also. A require with :refer does not work in an circumstance. (I didnt put an example for that into the snippet) this is a link to the async-eval in clojurescript: https://github.com/pink-gorilla/goldly/blob/master/goldly/src/goldly/sci/kernel_cljs.cljs

(ns demo.notebook.lazy
  (:require
   [r]
   [string]
   [user :refer [compile-sci-async println]]))

; this is a sci-cljs notebook!

(defn wrap-code [form]
  (string/join
   "\n"
   (map pr-str form)))

(defn show-atom [a]
  [:p (pr-str @a)])

(defn async-eval [form]
  (let [d (r/atom "async compiling..")
        p (compile-sci-async (wrap-code form))]
    (.then (:result p)
           (fn [result]
             (.log js/console "async result: " result)
             (reset! d result)))
    ^:R
    [show-atom d]))

^:R
[:div
 (async-eval
  '[(ns example1 (:require ["some_js_lib" :as my-lib]))
    (my-lib/libfn)])
 (async-eval
  '[(ns example2 (:require [adder :as x]))
    (x/add 7 13)])
 (async-eval
  '[(ns example3 (:require [funny :as f]))
    (f/joke)])
 #_(async-eval
    '[(ns example4 (:require [funny :as funny]))
      (funny/joke)])
 (async-eval
  '[(ns example5 (:require [funny]))
    (funny/joke)])]

borkdude 2022-05-19T10:48:17.485289Z

@hoertlehner I see. If you return {:handed true} then SCI does not process any :refer [x y z] and it assumes you have handled that yourself.

borkdude 2022-05-19T10:52:16.255089Z

Let me work out a scenario where you would like to define a normal Clojure namespace in SCI using async load fn

awb99 2022-05-19T15:14:11.932669Z

Perfect!!! Thank you so much!