-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathREADME.md
183 lines (134 loc) · 5.27 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
[![Build Status](https://github.com/ceifa/wasmoon/actions/workflows/publish.yml/badge.svg)](https://github.com/ceifa/wasmoon/actions/workflows/publish.yml)
[![npm](https://img.shields.io/npm/v/wasmoon.svg)](https://npmjs.com/package/wasmoon)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
# Wasmoon
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
const { LuaFactory } = require('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 2k numbers 10x(less is better):
| wasmoon | fengari |
| -------- | --------- |
| 15.267ms | 389.923ms |
### Size
Fengari is smaller than wasmoon, which can improve the user experience if in web environments:
| | wasmoon | fengari |
| ----------- | ------- | ------- |
| **plain** | 393kB | 214kB |
| **gzipped** | 130kB | 69kB |
## Fixing common errors on web environment
Bundle/require errors can happen because wasmoon tries to safely import some node modules even in a browser environment, the bundler is not prepared to that since it tries to statically resolve everything on build time.
Polyfilling these modules is not the right solution because they are not actually being used, you just have to ignore them:
### Webpack
Add the `resolve.fallback` snippet to your config:
```js
module.exports = {
entry: './src/index.js', // Here is your entry file
resolve: {
fallback: {
path: false,
fs: false,
child_process: false,
crypto: false,
url: false,
module: false
},
},
}
```
### Rollup
With the package [rollup-plugin-ignore](https://www.npmjs.com/package/rollup-plugin-ignore), add this snippet to your config:
```js
export default {
input: 'src/index.js', // Here is your entry file,
plugins: [ignore(['path', 'fs', 'child_process', 'crypto', 'url', 'module'])],
}
```
### Angular
Add the section browser on `package.json`:
```json
{
"main": "src/index.js",
"browser": {
"child_process": false,
"fs": false,
"path": false,
"crypto": false,
"url": false,
"module": false
}
}
```
## How to build
Firstly download the lua submodule and install the other Node.JS dependencies:
```sh
git submodule update --init # download lua submodule
npm i # install dependencies
```
### Windows / Linux (Docker way)
You need to install [docker](https://www.docker.com/) and ensure it is on your `PATH`.
After cloned the repo, to build you just have to run these:
```sh
npm run build:wasm:docker:dev # build lua
npm run build # build the js code/bridge
npm test # ensure everything it's working fine
```
### Ubuntu / Debian
You need to install [emscripten](https://emscripten.org/) and ensure it is on your `PATH`.
After cloned the repo, to build you just have to run these:
```sh
npm run build:wasm:dev # build lua
npm run build # build the js code/bridge
npm test # ensure everything it's working fine
```