Can clojure/clojurescript compile down to webassembly? - clojure

Thinking about writing a game using web assembly. Is there a good way of doing that using Clojure or clojurescript?

Clojure compiles all functions to JVM bytecode.
Clojurescript to javascript.
You can't change that part.
But you can use .wasm files with clojurescript, just load it via regular js/WebAssembly.

Related

What is the benefit of having a Clojure project before starting the REPL? Why Cider keeps asking and nudging the user about it?

I am using Emacs and every time I start a Clojure REPL with cider-jack-in
, the minibuffer echoes the following:
Are you sure you want to run `cider-jack-in' without a Clojure project? (y or n)
I have a prior experience with Common Lisp and Slime. In this other lisp ecosystem, there is not such a thing. You just start Common Lisp without questions asked.
I do not get the purpose of this message from Cider. Probably, because I am missing something.
It is important to highlight that I do know how to create a Clojure project using lein command. I just do not get its relevance.
What am I missing for not using a Clojure project before starting the REPL?
Why is it so relevant?
What is the downside of developing a Clojure program with the .clj file, the REPL, doing some interactive programming, and re-compiling things as the program evolves with editions?
Just preventing problems with namespace?
Thanks
The main reason for using a "project", whether it is via a deps.edn file (for Clojure CLI tooling) or a project.clj file (for lein), is to provide the dependencies and hence the CLASSPATH. Since Clojure runs on the JVM, the CLASSPATH has to be set before the Java process is started. As an aside, this is not strictly true, but I'll ignore that complexity for now. The CLASSPATH is used by the JVM to find and then load classes during runtime.
If you're not using any dependencies then you can probably get away with not using a project.
I use emacs with cider and the Clojure CLI tooling myself. You might also want to look into the inf-clojure emacs package.
I am also coming from Common Lisp to Clojure and was confused a lot at the beginning.
And we use lein new because BraveClojure etc. shows this as the only way. Despite of its truths, it is too old. The Clojure community should write more up-to-date tutorials and books.
There is e.g. boot - the other build tooling tool than lein:
https://github.com/boot-clj/boot
Short intro:
https://jjmojojjmojo.github.io/boot-getting-started-with-clojure-in-10-minutes.html
Which allows you to start a repl with $ boot repl a repl with boot.user as the namespace
similar to CLUSER.
It is as powerful as lein.
And for command line scripting, there is babashka https://github.com/babashka/babashka
which is interpreted Clojure for scripting - callable even from the command line.

Is there a way to look into jvm/Clojure source code from repl?

As some parts of Clojure are written in Java is there a way to look into these parts of source code from repl? I'm looking for something like this :
(source clojure.lang.Numbers/add)
As this part is implemented with Java
source prints Source not found and returns nil.
It depends on the environment you're using.
With plain REPL, you are quite limited.
Two most popular IDEs are Cursive and Emacs Cider.
As #Carcigenicate already pointed out, Cursive has an excellent Java support and allows you to jump to clojure java sources (or any other 3rd party lib sources - providing the sources have been published) easily.
It also allows you to debug Clojure compiler itself quite easily.
Emacs Cider has some support for Java.
Out of the box you can jump to JDK sources (with some caveats, see https://github.com/clojure-emacs/cider/issues/2687).
For other artifacts (like clojure.lang itself or other 3rd party java libs) you should be able to jump to the source code as long as you add the corresponding source jar to :resource-paths (assuming leiningen-based project here).
See here for more info about Cider's support.

how does jvm bytecode get converted to js in cljs build

As I understand most clojure and java libraries can be included in cljs code. How does the cljs compiler accomplish this ?
I understand that if I have a source file, the cljs compiler takes the source and outputs javascript. However how about if I am using java libraries such as joda-time for which I have included its clojure wrapper clj-time. So in this case it only has the java byte code for joda-time. So how will the cljs build tool, generate js code from the byte code of the java jar (joda-time)?
As I understand most clojure and java libraries can be included in cljs code. How does the cljs compiler accomplish this ?
You are mistaken. Many clojure libraries can be compiled for CLJS, with only minimal changes (sometimes even none), but there is no way to use classfiles: it must be .clj source files, compiled to javascript instead of to classfiles.
You cannot use Java libraries in cljs code.

simple tool for compiling Clojure .clj into .class / .jar

I've found two ways of compiling Clojure *.clj files into *.class files, and while they both work, I have some gripes with both of them.
The first one uses a REPL and so cannot be automated (or can it?)
The second one uses lein. To be frank I don't see why I should use a dependency management tool for something that should be part of the core tool-chain of the language. But in any case, using lein soon forces you to use a local Maven repository if your Clojure code needs to access local jars (which is highly likely).
Is there a better way to generate *.class or *.jar files from Clojure code that involves only core Clojure tools and that can be employed in a scripted, non-interactive way?
Clojure provides clojure.core.Compile, which can be used to compile Clojure code from the command-line.
java -Dclojure.compile.path=<targetdir> -cp <targetdir>;clojure.jar <list of namespaces>
For an example of how to use this from ant, look at the compile-clojure task in Clojure's own build.xml file.
clojure.core/compile can be automated via Clojure

How to analyze Java source files with Clojure

I'm trying to analyze Java source files with Clojure but I couldn't find a way to do that.
First, I thought using Eclipse AST plugin(by copying necessary JAR's to my Clojure project) but I gave up after seeing Eclipse AST's API(visitor based walker).
Then I've tried creating a Java parser with ANTLR. I can only find one Java 1.6 grammar for ANTLR( http://openjdk.java.net/projects/compiler-grammar/antlrworks/Java.g ) and it doesn't compile with latest ANTLR(here's the errors I'm getting ).
Now I have no idea how can I do that. At worst I'll try to go with Eclipse AST.
Does anyone know a better way to parse Java files with Clojure?
Thanks.
Edit: To clarify my point:
I need to find some specific method calls in Java projects and inspect it's parameters(we have multiple definitions of the method, with different type of parameters). Right now I have a simple solution written in Java(Eclipse AST) but I want to use Clojure in this project as much as possible.
... and it doesn't compile with latest ANTLR ...
I could not reproduce that.
Using ANTLR v3.2, I got some warnings, but no errors. Using both ANTLR v3.3 and v3.4 (latest version), I have no problems generating a parser.
You didn't mention how you're (trying) to generate a lexer/parser, but here's how it works for me:
java -cp antlr-3.4.jar org.antlr.Tool Java.g
EDIT 1
Here's my output when running the commands:
ls
wget http://www.antlr.org/download/antlr-3.4-complete.jar
wget http://openjdk.java.net/projects/compiler-grammar/antlrworks/Java.g
java -cp antlr-3.4-complete.jar org.antlr.Tool Java.g
ls
As you can see, the .java files of the lexer and parser are properly created.
EDIT 2
Instead of generating a parser yourself (from a grammar), you could use an existing parser like this one (Java 1.5 only AFAIK) and call it from your Clojure code.
It depends a bit on what you want to do - what are you hoping to get from the analysis?
If you want to actually compile Java or at least build an AST, then you probably need to go the ANTLR or Eclipse AST route. Java isn't that bad of a language to parse, but you still probably don't want to be reinventing too many wheels..... so you might as well build on the Eclipse and OpenJDK work.
If however you are just interesting in parsing the basic syntax and analysing certain features, it might be easier to use a simpler general purpose parser combinator library. Options to explore:
fnparse (Clojure, not sure how well maintained)
jparsec (Java, but can probably be used quite easily from Clojure)