![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Fengari is a JavaScript implementation of the Lua programming language. It allows you to run Lua code within a JavaScript environment, making it possible to leverage Lua's simplicity and efficiency in web applications or Node.js projects.
Running Lua code
This feature allows you to run Lua code directly from JavaScript. The code sample demonstrates how to initialize a Lua state, open standard libraries, and execute a Lua script that prints a message.
const fengari = require('fengari');
const lua = fengari.lua;
const lauxlib = fengari.lauxlib;
const lualib = fengari.lualib;
const L = lauxlib.luaL_newstate();
lualib.luaL_openlibs(L);
lauxlib.luaL_dostring(L, 'print("Hello from Lua!")');
Interoperability between Lua and JavaScript
This feature demonstrates how to call JavaScript functions from Lua. The code sample shows how to push a JavaScript function onto the Lua stack and call it from Lua code.
const fengari = require('fengari');
const lua = fengari.lua;
const lauxlib = fengari.lauxlib;
const lualib = fengari.lualib;
const L = lauxlib.luaL_newstate();
lualib.luaL_openlibs(L);
lua.lua_pushjsfunction(L, function(L) {
console.log('Hello from JavaScript!');
return 0;
});
lua.lua_setglobal(L, 'jsFunction');
lauxlib.luaL_dostring(L, 'jsFunction()');
Embedding Lua scripts in JavaScript
This feature allows you to embed Lua scripts within JavaScript code. The code sample demonstrates how to define a Lua function in a script and call it from JavaScript.
const fengari = require('fengari');
const lua = fengari.lua;
const lauxlib = fengari.lauxlib;
const lualib = fengari.lualib;
const L = lauxlib.luaL_newstate();
lualib.luaL_openlibs(L);
const luaScript = `
function greet(name)
return 'Hello, ' .. name .. '!'
end
`;
lauxlib.luaL_dostring(L, luaScript);
lauxlib.luaL_dostring(L, 'print(greet("World"))');
lua.vm.js is another JavaScript implementation of the Lua programming language. It provides similar functionality to Fengari, allowing you to run Lua code in a JavaScript environment. However, Fengari is known for its closer adherence to the Lua 5.3 standard and better performance.
Moonshine is a Lua VM written in JavaScript. It aims to be a lightweight and fast implementation of Lua for the web. Compared to Fengari, Moonshine is more focused on being lightweight and may not support all Lua features as comprehensively as Fengari.
Luna is a Lua VM for the browser and Node.js. It allows you to run Lua code in JavaScript environments. While similar to Fengari, Luna is designed to be more modular and extensible, providing more flexibility for advanced use cases.
🐺 φεγγάρι - The Lua VM written in JS ES6 for Node and the browser
This repository contains the core fengari code (which is a port of the Lua C library) which includes parser, virtual machine and base libraries. However it is rare to use this repository directly.
lua
command line tool, but running under node.js, see fengari-node-cliOnce you've loaded fengari, you can use the JS API:
const luaconf = fengari.luaconf;
const lua = fengari.lua;
const lauxlib = fengari.lauxlib;
const lualib = fengari.lualib;
const L = lauxlib.luaL_newstate();
lualib.luaL_openlibs(L);
lua.lua_pushliteral(L, "hello world!");
The JS API is exactly the same as the C API so fengari.lua
exposes the same constants and functions as lua.h
, fengari.lauxlib
the same as lauxlib.h
and fengari.lualib
the same as lualib.h
. If you're unfamiliar with the C API, you can take a look at the manual.
Fengari implements Lua 5.3 semantics and will hopefully follow future Lua releases. If you find any noticeable difference between Fengari and Lua's behaviours, please report it.
Lua strings are 8-bits clean and can embed \0
. Which means that invalid UTF-8/16 strings are valid Lua strings. Lua functions like string.dump
even use strings as a way of storing binary data.
To address that issue, Fengari uses Uint8Array
objects containing the raw bytes to implement lua strings. To push a JS string on the stack you can use lua_pushliteral
which will convert it to an array of bytes before pushing it. To get a Lua string on the stack as a JS string you can use lua_tojsstring
which will attempt to convert it to a UTF-16 JS string. The latter won't give you what you expect if the Lua string is not a valid UTF-16 sequence. You can also convert strings with luastring_of
, to_luastring
, to_jsstring
and to_uristring
.
The JS number type is always a double, and hence cannot accurately represent integers with more than 53 bits. As such, we've taken the route of a rarely used define (LUA_INT_TYPE=LUA_INT_LONG
) in the PUC-Rio sources, where floats are doubles, but integers are 32 bits.
require
and package.loadlib
In the browser require
and package.loadlib
try to find a file by making synchronous XHR requests.
require
has been extended to allow searchers to yield.
lua_gc
/collectgarbage
: Fengari relies on the JS garbage collector and does not implement its own.io
libos.remove
os.rename
os.tmpname
os.execute
debug.debug()
doesn't work from web workers due to lack of a method to get synchronous user input__gc
metamethodspackage.jspath
instead of package.cpath
LUA_JSPATH_DEFAULT
instead of LUA_CPATH_DEFAULT
(and contains .js extensions rather than .so or .dll extensions)lua_tointegerx
and lua_tonumberx
do not have out-parameters indicating conversion success. Instead, false
is returned when conversion fails.luaL_execresult
takes an extra argument: an error object. The error object should have a fields status
, signal
and errno
.luaL_fileresult
takes an extra argument: an error object. The error object should have a field errno
.Some luaconf options can be chosen at library load time. Fengari looks for process.env.FENGARICONF
and if it exists, parses it as a JSON string.
dv = lua_todataview(L, idx)
Equivalent to lua_tolstring
but returns a DataView
instead of a string.
lua_pushjsfunction(L, func)
Alias for lua_pushcfunction
.
lua_pushjsclosure(L, func, n)
Alias for lua_pushcclosure
.
lua_atnativeerror(L, func)
Sets a function to be called if a native JavaScript error is thrown across a lua pcall. The function will be run as if it were a message handler (see https://www.lua.org/manual/5.3/manual.html#2.3). The current message handler will be run after the native error handler returns.
b = lua_isproxy(p, L)
Returns a boolean b
indicating whether p
is a proxy (See lua_toproxy
).
If L
is non-null, only returns true
if p
belongs to the same global state.
p = lua_toproxy(L, idx)
Returns a JavaScript object p
that holds a reference to the lua value at the stack index idx
.
This object can be called with a lua_State to push the value onto that state's stack.
This example would be an inefficient way to write lua_pushvalue(L, 1)
:
var p = lua_toproxy(L, 1);
p(L);
fengari
libraryA library containing metadata about the fengari release.
AUTHORS
COPYRIGHT
RELEASE
VERSION
VERSION_MAJOR
VERSION_MINOR
VERSION_NUM
VERSION_RELEASE
This library is automatically loaded by luaL_openlibs
into the global "fengari"
.
io.input()
: partially implementedio.lines()
io.open()
io.output()
: partially implementedio.popen()
io.read()
io.tmpfile()
file:lines()
file:read()
file:setvbuf()
file:__gc()
FAQs
A Lua VM written in JS ES6 targeting the browser
The npm package fengari receives a total of 0 weekly downloads. As such, fengari popularity was classified as not popular.
We found that fengari demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.