
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
This package aims to provide a way to:
To initialize, create a new Lua state, register the standard library, set a global variable, execute a code and get a global variable:
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()
}
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.
$: wasmoon [options] [file] [args]
Available options are:
-l
: Include a file or directory-i
: Enter interactive mode after running the files$: wasmoon -i sum.lua 10 30
And if you are in Unix, you can also use it as a script interpreter with Shebang:
#!/usr/bin/env wasmoon
return arg[1] + arg[2]
$: ./sum.lua 10 30
Wasmoon compiles the official Lua code to webassembly and creates an abstraction layer to interop between Lua and JS, instead of fengari, that is an entire Lua VM rewritten in JS.
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 in a list of 20k numbers 10x:
wasmoon | fengari |
---|---|
0.177ms | 2.107ms |
Fengari is lighter than wasmoon, which can improve the user experience if in web environments:
wasmoon | fengari | |
---|---|---|
plain | 393kB | 214kB |
gzipped | 130kB | 69kB |
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:
Add the resolve.fallback
snippet to your config:
module.exports = {
entry: './src/index.js', // Here is your entry file
resolve: {
fallback: {
path: false,
fs: false,
child_process: false,
crypto: false,
url: false,
},
},
}
With the package rollup-plugin-ignore, add this snippet to your config:
export default {
input: 'src/index.js', // Here is your entry file,
plugins: [ignore(['path', 'fs', 'child_process', 'crypto', 'url'])],
}
Add the section browser on package.json
:
{
"main": "src/index.js",
"browser": {
"child_process": false,
"fs": false,
"path": false,
"crypto": false,
"url": false
}
}
FAQs
A real lua VM with JS bindings made with webassembly
The npm package wasmoon receives a total of 469 weekly downloads. As such, wasmoon popularity was classified as not popular.
We found that wasmoon demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.