Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am having a little difficulty seeing how this is done. How is it able to convert it to JS without writing any JS (everything is in Clojure or CS).

Can someone give a simple example of how the compiler would convert something simple to javascript. Maybe (def x "foo") or (defn [x] (+ x x))?

share|improve this question
5  
You don't need Javascript to produce Javascript source code; all you need is string concatenation, which can be done in any programming language. –  Robert Harvey Jul 12 '13 at 22:06
add comment

1 Answer 1

up vote 5 down vote accepted

Emitting JavaScript is handled by the cljs.compiler namespace. (The link is to the source on the master branch.) As you can see, it boils down to printing strings to files.

Which strings exactly depends on the ClojureScript source, of course, but not directly: the original source is first transformed into a different representation more useful during the compilation process. This happens in the cljs.analyzer namespace.

The analyser is rather more involved then the compiler. For your (def x "foo") example, it would produce a fairly simple map which would be handled by the :def method of the emit multimethod of the compiler; search for defmethod emit :def. The things to note are the call to munge (transforming ClojureScript identifiers into valid JavaScript identifiers; e.g. foo-bar -> foo_bar) and the emits call with init as one of the arguments, where the representation of init will be generated recursively. (In this case it'll simply be "foo".)

share|improve this answer
    
So it is string concatenation, essentially. –  Robert Harvey Jul 12 '13 at 22:34
    
Well, yes, of course. (once the analysis is done.) Just wanted to provide pointers to the source, including where exactly to look for what ends up happening to (def x "foo") in particular. –  Michał Marczyk Jul 12 '13 at 22:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.