Fork me on GitHub
#cljs-dev
<
2021-07-15
>
dnolen17:07:03

@plexus sorry been really busy w/ work, I've been thinking a bit about the expand to require change - one thing I don't understand now is why parse-ns needed to be changed

leif19:07:55

Okay @dnolen, copying from #clojurescript:

;; example/core.clj
(ns example.core)
(defmacro add-three [x]
  `(+ ~x 3))
Compiles to:
goog.provide("example.core$macros");
var ret__38016__auto___49 = bob.core$macros.add_three = (function example$core$macros$add_three(_AMPERSAND_form,_AMPERSAND_env,x){
return cljs.core.sequence.call(null,cljs.core.concat.call(null,(new cljs.core.List(null,new cljs.core.Symbol("cljs.core","+","cljs.core/+",(-342754435),null),null,(1),null)),(new cljs.core.List(null,x,null,(1),null)),(new cljs.core.List(null,(3),null,(1),null))));
});
(example.core$macros.add_three.cljs$lang$macro = true);
And
;; example/core.cljs
(ns example.core
  :require-macros [example.core :refer [add-three]])
(println (add-three 4))
fails to compile with error:
#error {:message Could not parse ns form example.core in file /example/core.clj, :data {:tag :cljs/analysis-error}, :cause #error {:message Invalid :refer, macro example.core/add-three does not exist in file /example/core.clj, :data {:tag :cljs/analysis-error}}}

dnolen19:07:29

(ns example.core
  :require-macros [example.core :refer [add-three]])

dnolen19:07:32

it's not right syntatically

dnolen19:07:36

you're missing parens

leif19:07:05

Ah, woops, copy/paste problem. Here it is:

(ns example.core
  (:require-macros [example.core :refer [add-three]]))
(println (add-three 4))

dnolen19:07:22

I can't remember if the macro file has to be .cljc ?

dnolen19:07:33

.clj can't generically work so I don't remember if we excluded them

leif19:07:53

Switching the extension from clj to cljc doesn't seem to do anything. Although that's probably not too surprising since eval-str expects that I provide my own load-fn, which is:

(defn ns->string [fs {:keys [name macros path]} cb]
  ((fn rec [extensions]
     (if (empty? extensions)
       (cb nil)
       (let [file-path (str "/" path "." (first extensions))]
         (ocall
          fs :readFile
          file-path
          (fn [err data]
            (if err
              (rec (rest extensions))
              (cb {:lang (if (= (first extensions)
                                "js")
                           :js
                           :clj)
                   :source (.toString data)
                   :file file-path})))))))
   (if macros
     ["clj" "cljc"]
     ["cljs" "cljc" "js"])))

leif19:07:06

Which I think matches the *load-fn* docs: http://cljs.github.io/api/cljs.js/STARload-fnSTAR

dnolen19:07:19

hrm I can't really dig into this right now - but I mean the above should work - and we run all the normal tests against self-hosted

dnolen19:07:38

i.e. self-hosted runs the entire standard set of runtime tests - macros have to work etc.

leif19:07:36

hmm..okay, thanks. By knowing it 'should' work helps. (By removing that particular class of 'user error', and meaning possibly I'm setting up the environment incorrectly. ) Anyway, thanks. 🙂

dnolen19:07:22

@leif we have tests for exactly what you doing

dnolen19:07:34

you could save yourself some trouble just trying them at a REPL first

leif19:07:58

I would LOVE to. But don't the docs say that macros have to be done in a seperate file?

dnolen19:07:59

@leif are you trying have macros be in the same file? I do not understand how that relates to your problems above

leif19:07:36

Oh, I just thought that meant you couldn't use defmacro in the repl.

dnolen20:07:10

I have no idea what you are doing 🙂

dnolen20:07:35

you cannot mix macros and regular code

dnolen20:07:40

doesn't matter if it's in the REPL or not

dnolen20:07:47

same as JVM ClojureScript

leif20:07:03

So why did you say I would have saved myself a lot of trouble if I used the repl?

dnolen20:07:04

what I mean about the REPL

dnolen20:07:09

was that I don't know what you're doing wrong

dnolen20:07:36

but you can use the CLJS Node REPL to figure it out

leif20:07:45

I misread your statement.

dnolen20:07:46

because you can compile ClojureScript into Node.js

dnolen20:07:58

and then test everything there

leif20:07:15

(Actually, I'm not using node.js, but browserfs)

leif20:07:33

Which gives you a..err...browserify-ed filesystem.

leif20:07:45

(And uses the node.js api.)

leif20:07:02

But ya, okay, I misread your original statement about the repl. That makes a LOT more sense now

leif20:07:08

Will try them. Thanks. 🙂

dnolen20:07:35

I understand but the point is to get it working somewhere

dnolen20:07:39

then find the delta