Moonshine

Getting started

  1. Prerequisites
  2. Write some Lua
  3. Distil your script
  4. Download the virtual machine
  5. Add it to your page
  6. Load your script
  7. Pass an API
  8. What next?

Prerequisites

In order to use Moonshine, you'll need to have the Lua compiler, Node and NPM installed on your local machine.

Installing the Lua compiler

The Lua compiler (or luac) comes packaged with Lua. The easiest way to install this on a Mac is to use Homebrew.

$ brew install lua

Alternative ways of installing Lua are available on the Lua downloads page.

Installing Node and NPM

NPM comes packaged along with Node. Again, the easiest way to install this on a Mac is to use Homebrew.

$ brew install node

Alternative ways of installing Node are available on the Node downloads page.

Write some Lua

For example, save the following as "hello.lua":

print 'Hello, World.'

Distil your script

The distillation process prepares your code to be run by the Moonshine virtual machine. In effect, it compiles the Lua code and translates the byte code to JSON.

In order to distil your code, you first need to install the Moonshine CLI using NPM. You'll only need to do this once:

$ npm install -g moonshine

Once that's installed you'll be able to distil:

$ moonshine distil hello.lua
File created: hello.lua.json

Download the virtual machine

Go to the download page and select the version you wish to add to your site.

Add it to your page

You'll need to insert the Moonshine script into your page and create a new VM object, like so:

<html>
	<head>
		<script src="./js/moonshine.js"></script>
		<script>
			var vm = new shine.VM();
		</script>
	</head>
	<body>
	⋮
	</body>
</html>

Load your script

Tell the VM to load your file and, by default, it will be executed once its loaded.

<html>
	<head>
		<script src="./js/moonshine.js"></script>
		<script>
			var vm = new shine.VM();
			vm.load('./lua/hello.lua.json');
		</script>
	</head>
	<body>
	⋮
	</body>
</html>

Pass an API

Pass in an object when you create the VM and you'll find the properties in the global namespace in Lua. Call any function in that object like it was a Lua function. For example:

⋮
<script src="./js/moonshine.js"></script>
<script>
	var vm, env;

	env = {
		speedOfLight: 299792458,
		distanceToMoon: 384400000,
		log: function log (message) {
			console.log('Message from Lua: ' + message);
		}
	};

	vm = new shine.VM(env);
	vm.load('./lua/light-seconds.lua.json');
</script>
⋮
local lightSeconds = distanceToMoon / speedOfLight
log('It takes '..lightSeconds..' seconds for light to travel from the Moon.')
			
Message from Lua: It takes 1.2822203819416964 seconds for light to travel from the Moon.

What next?

Go code. Try it out. This is all you need to know in order to be able to create wonderful things. If, however, you're up for more of a challenge, try out some of the How-tos.