Dynamic Code Execution in Unity

Introduction

Awhile ago I made a thing that I didn’t talk about. In fact, lately I’ve been doing that a lot, so hopefully I’ll get better about boasting about myself like a good blogger.

I wanted a way to download scripts in a Unity executable and run them (particularly on mobile). A working REPL would also be nice. So I started inventing my own language looking for language runtimes that would be easy to shove into Unity. The easiest option is always to steal someone else’s work and claim it as your own. That led me to Jint, a Javascript interpreter written in C#.

What’s important about Jint is that it’s an interpreter that does not require the DLR and does not generate bytecode. This is important if you want to be able to run on targets that require Unity to AOT compile everything (like iOS). Jint isn’t perfect though. It has actually recently dropped support for .NET 3.5 and does loads of other stuff Unity doesn’t support. But it’s pretty dang close.

Rising Action

So I started a fork with all these issues taken care of.

Here’s a screenshot of an example REPL:

It has some pretty cool features, like a builtin require() function, much like node.js. Require will essentially load a script, execute it in a sandbox, then return the result for code sharing.

inject() is pretty cool too, because it lets you specify how to resolve objects from Unity land.

DENOUEMENT

In conclusion, I’d like to thank the years of work that 40 contributors put into Jint just so I could make a poor-performing JS REPL in Unity.