r/Clojure • • May 16 '20

MAGIC Clojure implementation for CLR can now compile itself and passes most of the Clojure test suite

https://twitter.com/ra/status/1261435359117762560
71 Upvotes

13 comments sorted by

5

u/fancyl May 16 '20 edited Jun 21 '23

This has been deleted in protest of the greedy API changes and the monetization of user-provided content and unpaid user moderation.

9

u/yogthos May 16 '20

This is an alternative implementation of Clojure that targets Microsoft CLR VM instead of the JVM. It was originally built for Arcadia which is a Clojure environment for the Unity 3D game engine.

3

u/didibus May 17 '20

MAGIC is a Clojure to .Net IR (the equivalent of Java ByteCode in the .Net CLR world). It differs from Clojure CLR (which is also a Clojure to .Net IR compiler) in that it tries to generate .Net IR which is compatible with Unity's ahead of time compilation for iOS and Playstation targets.

For those target, Unity does not support the full spectrum of .Net IR code. It only supports a subset. The Clojure CLR compiler generates IR which unfortunately does not always fall under that subset, and thus can't be used if you want your Unity games compiled for iOS or Playstation.

Also, I think MAGIC tries to provide certain additional Clojure macros/functions that the MAGIC compiler knows how to generate a more optimal compilation for. Since games often need special kind of optimization, those allow more control on that.

2

u/attunezero May 17 '20

Does this mean Arcadia is closer to being able to target iOS and consoles?

2

u/yogthos May 17 '20

Yeah, I think that's the plan with MAGIC.

10

u/slifin May 16 '20

Going to try and explain what I think I know on this subject, corrections welcome:

The Clojure CLR makes use of the dynamic language runtime: https://clojure.org/about/clojureclr, which allows us to run Clojure on top of the .NET ecosystem

The biggest Clojure CLR project I know of is https://arcadia-unity.github.io/ which is very very cool, it allows you to do REPL live coding in a popular game creation engine

There are some limitations though: https://github.com/arcadia-unity/Arcadia/wiki/Limitations namely it's not possible to export out to anything but android and windows games

The MAGIC compiler is different in ways I don't understand but it will allow for targeting of iOS PlayStation 4 and likely many others with what I think will be faster performance based on: https://github.com/arcadia-unity/Arcadia/wiki/Optimizations#using-magic

Once the MAGIC compiler is complete the plan is to use it as the default compiler for Arcadia I think it also becomes an alternative to Clojure CLR too

1

u/didibus May 17 '20

Ya, MAGIC is just an alternate compiler which compiles Clojure to .Net IR. You can think of it as just another Clojure target, like how there is babashka, Joker, ClojureScript, ClojureCLR, Clojerl, etc.

I think the difference is Clojure JVM, ClojureCLR and ClojureScript are considered official, where as babashka, Clojerl, Ferret, Magic are unofficial.

3

u/[deleted] May 16 '20 edited May 16 '20

This seems to target classic .net framework(net461 in csproj) right? I'm wondering if it works, or there are any plans to run on dotnet core runtime (aka .net 5)?

edit: no core-clr see https://twitter.com/ra/status/1261657729304838147

1

u/didibus May 17 '20 edited May 17 '20

Ya, I think currently, a lot of the code relies on dynamic runtime, and .Net Core dropped support for dynamic runtime. That's the big challenge for Clojure CLR working on .net Core. It means a lot of the compiler has to be re-written not to use dynamic runtime I believe.

2

u/[deleted] May 16 '20

[removed] — view removed comment

2

u/yogthos May 16 '20

I think writing libraries in cljc is a good default unless there's some platform specific functionality. Even if most of the library is written in cljc, then people would be able to reuse that code and write their own platform specific adapters.

2

u/didibus May 17 '20

Just an FYI, there is already a Clojure to CLR compiler called ClojureCLR that is somewhat considered official and is production ready and support most things Clojure JVM does out of the box. MAGIC is an alternative to this which tries to compile to .net IR which is more restrictive and thus can survive Unity's AOT compilation for Playstation and iOS targets,