Skip to content

Commit ebd41f2

Browse files
committed
replace Lua fengari with manual Lua using wasm, also updates to Lua 5.4.8 and uses 64 bit integers
1 parent 707dca2 commit ebd41f2

7 files changed

Lines changed: 80 additions & 25 deletions

File tree

Lua.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!DOCTYPE html>
12
<html>
23
<head>
34
<link rel="shortcut icon" type="image/png" href="favicon.png"/>
@@ -31,14 +32,14 @@
3132
<meta name="viewport" content="width=device-width,initial-scale=1">
3233
<title>Masterjun | Lua</title>
3334
</head>
34-
<body onload="run();">
35+
<body onload="handleChange();" style="overflow-y: scroll;">
3536
<a href=".">&lt Back</a><br>
3637
<div class="title">
3738
<h2>Lua</h2>
3839
</div>
3940
<hr>
4041
<div class="code">
41-
<textarea id="code" oninput="run();">
42+
<textarea id="code" oninput="handleChange();">
4243
print(_VERSION)
4344

4445
for i=1,10 do
@@ -49,10 +50,11 @@ <h2>Lua</h2>
4950
<hr>
5051
<div>
5152
<pre id="out">
52-
hi
53+
Initializing...
5354
</pre>
5455
</div>
5556
</body>
57+
<script src="https://cdnjs.cloudflare.com/ajax/libs/he/1.2.0/he.min.js" integrity="sha512-PEsccDx9jqX6Dh4wZDCnWMaIO3gAaU0j46W//sSqQhUQxky6/eHZyeB3NrXD2xsyugAKd4KPiDANkcuoEa2JuA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
5658
<script src="Lua.js"></script>
57-
<script src="fengari-web.js"></script>
59+
<script src="Luawasm.js"></script>
5860
</html>

Lua.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1-
oldlog = console.log
2-
console.log = function(s){
3-
document.getElementById("out").innerHTML += s+"<br>";
1+
const outEl = document.getElementById("out");
2+
const code = document.getElementById("code");
3+
4+
var runCode = function() {}
5+
6+
function handleChange() {
7+
code.style.height = "auto";
8+
code.style.height = (25 + document.getElementById("code").scrollHeight) + "px";
9+
10+
runCode();
11+
}
12+
13+
console.log = (msg) => {
14+
outEl.innerHTML += he.encode(msg) + "\n"
415
}
516

6-
function run(){
7-
document.getElementById("code").style.height = "auto";
8-
document.getElementById("code").style.height = (25+document.getElementById("code").scrollHeight)+"px";
9-
10-
document.getElementById("out").innerHTML = "";
11-
try{
12-
fengari.load(document.getElementById("code").value)();
13-
}catch(error){
14-
document.getElementById("out").innerHTML = error;
17+
var Module = {};
18+
19+
Module.onRuntimeInitialized = () => {
20+
const runLua = Module.cwrap(
21+
"run_lua",
22+
"number",
23+
["string"]
24+
);
25+
26+
runCode = function () {
27+
outEl.innerHTML = "";
28+
const errPtr = runLua(code.value);
29+
30+
if (errPtr) {
31+
const err = UTF8ToString(errPtr);
32+
outEl.innerHTML = he.encode(err);
33+
}
1534
}
16-
}
35+
runCode()
36+
}

Luawasm.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Luawasm.wasm

200 KB
Binary file not shown.

Luawasm/build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
emcc $(ls lua-5.4.8/src/*.c | grep -vE 'lua\.c|luac\.c') lua_wasm.c -Os -s EXPORTED_FUNCTIONS='["_run_lua"]' -s EXPORTED_RUNTIME_METHODS='["cwrap","
3+
UTF8ToString"]' -o Luawasm.js

Luawasm/lua_wasm.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include "lua-5.4.8/src/lua.h"
4+
#include "lua-5.4.8/src/lauxlib.h"
5+
#include "lua-5.4.8/src/lualib.h"
6+
7+
static lua_State *L;
8+
9+
static int l_print(lua_State *L) {
10+
int n = lua_gettop(L);
11+
for (int i = 1; i <= n; i++) {
12+
const char *s = lua_tostring(L, i);
13+
if (s) printf("%s", s);
14+
if (i < n) printf("\t");
15+
}
16+
printf("\n");
17+
return 0;
18+
}
19+
20+
const char *run_lua(const char *code) {
21+
static char error[512];
22+
23+
if (!L) {
24+
L = luaL_newstate();
25+
luaL_openlibs(L);
26+
lua_pushcfunction(L, l_print);
27+
lua_setglobal(L, "print");
28+
}
29+
30+
if (luaL_dostring(L, code) != LUA_OK) {
31+
snprintf(error, sizeof(error), "%s", lua_tostring(L, -1));
32+
lua_pop(L, 1);
33+
return error;
34+
}
35+
36+
return NULL;
37+
}

fengari-web.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)