# @theoparis/wasmoon

> A real lua VM with JS bindings made with webassembly

Latest version **0.0.3** (published 2022-04-04) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @theoparis/wasmoon
pnpm add @theoparis/wasmoon
yarn add @theoparis/wasmoon
bun add @theoparis/wasmoon
```

Provides the command `wasmoon`.

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2022-04-04 |
| First published | 2022-04-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 6 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Theo Paris |
| Maintainers | creepinson |
| Keywords | lua, moon, wasm, webassembly |

## Links

- npm: https://www.npmjs.com/package/@theoparis/wasmoon
- Repository: https://sr.ht/~theoparis/wasmoon/
- Homepage: https://sr.ht/~theoparis/wasmoon/tree/main/item/readme.md
- Issues: https://todo.sr.ht/~theoparis/wasmoon
- npm.io page: https://npm.io/package/@theoparis/wasmoon

## Dependencies (1)

- [@types/emscripten](https://npm.io/package/@types/emscripten.md) 1.39.6

## Recent versions

- 0.0.3 (latest) — 2022-04-04
- 0.0.2 — 2022-04-04

## README

# Wasmoon

[![npm](https://img.shields.io/npm/v/@theoparis/wasmoon.svg)](https://npmjs.com/package/@theoparis/wasmoon)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

This package aims to provide a way to:

- Embed Lua to any Node.js, Deno or Web Application.
- Run lua code in any operational system
- Interop Lua and JS without memory leaks (including the DOM)

## API Usage

To initialize, create a new Lua state, register the standard library, set a global variable, execute a code and get a global variable:

```js
import { LuaFactory } from "@theoparis/wasmoon"

// Initialize a new lua environment factory
// You can pass the wasm location as the first argument, useful if you are using wasmoon on a web environment and want to host the file by yourself
const factory = new LuaFactory()
// Create a standalone lua environment from the factory
const lua = await factory.createEngine()

try {
  // Set a JS function to be a global lua function
  lua.global.set('sum', (x, y) => x + y)
  // Run a lua string
  await lua.doString(`
    print(sum(10, 10))
    function multiply(x, y)
        return x * y
    end
  `)
  // Get a global lua function as a JS function
  const multiply = lua.global.get('multiply')
  console.log(multiply(10, 10))
} finally {
  // Close the lua environment, so it can be freed
  lua.global.close()
}
```

## CLI Usage

Although Wasmoon has been designed to be embedded, you can run it on command line as well, but, if you want something more robust on this, we recommend to take a look at [demoon](https://github.com/ceifa/demoon).

```sh
$: wasmoon [options] [file] [args]
```

Available options are:

- `-l`: Include a file or directory
- `-i`: Enter interactive mode after running the files

### Example

```sh
$: wasmoon -i sum.lua 10 30
```

And if you are in Unix, you can also use it as a script interpreter with [Shebang](<https://en.wikipedia.org/wiki/Shebang_(Unix)>):

```lua
#!/usr/bin/env wasmoon
return arg[1] + arg[2]
```

```sh
$: ./sum.lua 10 30
```

## When to use wasmoon and fengari

Wasmoon compiles the [official Lua code](https://github.com/lua/lua) to webassembly and creates an abstraction layer to interop between Lua and JS, instead of [fengari](https://github.com/fengari-lua/fengari), that is an entire Lua VM rewritten in JS.

### Performance

Because of wasm, wasmoon will run Lua code much faster than fengari, but if you are going to interop a lot between JS and Lua, this may be not be true anymore, you probably should test on you specific use case to take the prove.

This is the results running a [heap sort code](https://github.com/ceifa/wasmoon/blob/main/bench/heapsort.lua) in a list of 20k numbers 10x:

| wasmoon | fengari |
| ------- | ------- |
| 0.177ms | 2.107ms |

### Size

Fengari is lighter than wasmoon, which can improve the user experience if in web environments:

|             | wasmoon | fengari |
| ----------- | ------- | ------- |
| **plain**   | 393kB   | 214kB   |
| **gzipped** | 130kB   | 69kB    |

## Contributing

You can [submit bugs here](https://todo.sr.ht/~theoparis/wasmoon), and you can create patches by following the [git send-email guide](https://git-send-email.io).

---
_Source: https://npm.io/package/@theoparis/wasmoon · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
