Skip to content

update lua to 5.4.0, add example exporting javascript function to lua #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/*.o
**/*.a
**/*.a
lua-*/**/*.wasm
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ Currently test on Firefox 52.0 but probably works on other platforms as well.
* Extensible debugger hooks for easy on-target debugging.

Thanks to [rawgit](http://rawgit.com/) for hosting.

# Files
- main.js, main.wasm: auto-generated
- main.c: export lua C function to javascript
- index.html: test page and useage example
- makefile: emcc config (which function to export, init function name, ...etc.)
- lua-<version>: lua source code without any modify
14 changes: 14 additions & 0 deletions demolib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <lauxlib.h>
/* use emscripten to execute javascript */
#include "emscripten.h"

EM_JS(void, js_alert, (const char* message), {
alert(UTF8ToString(message));
});

static int lua_alert(lua_State* L) {
size_t len = 0;
const char * message = lua_tolstring(L, 1, &len);
js_alert(message);
return 0;
}
63 changes: 63 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<html>
<meta charset="utf-8">

<head>
<script src="main.js"></script>
<script>
var Module = undefined;
var timer;
function text_changed() {
clearTimeout(timer);
input = document.getElementById("edit").value;
timer = setTimeout(function () {
document.getElementById("result").innerHTML = "";
Module.ccall("run_lua", 'number', ['string'], [input]);
},
750);
}

// init wasm module
var ModuleConfig = {
print: (function () {
return function (text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
console.log(text);

if (text != "emsc")
document.getElementById("result").innerHTML += "<br>\n" + text;
};
})(),
printErr: function (text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
if (0) { // XXX disabled for safety typeof dump == 'function') {
dump(text + '\n'); // fast, straight to the real console
} else {
console.error(text);
}
}
};

// initWasmModule function name configured in makefile
initWasmModule(ModuleConfig).then((aModule) => {
Module = aModule;
text_changed();
});
</script>
</head>

<body>
<div>
<textarea id="edit" style="width: 800px; height: 480px;" onkeyup="text_changed();">
function hello_lua()
print "Hello Lua!"
print(_VERSION)
alert("this alert is from lua")
end

hello_lua()
return "this is return value"</textarea>
<div id="result" style="width: 800px; float: right"></div>
</div>
</body>

</html>
Binary file modified lua-5.3.4/src/lua
Binary file not shown.
Binary file modified lua-5.3.4/src/luac
Binary file not shown.
106 changes: 106 additions & 0 deletions lua-5.4.0/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Makefile for installing Lua
# See doc/readme.html for installation and customization instructions.

# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================

# Your platform. See PLATS for possible values.
PLAT= guess

# Where to install. The installation starts in the src and doc directories,
# so take care if INSTALL_TOP is not an absolute path. See the local target.
# You may want to make INSTALL_LMOD and INSTALL_CMOD consistent with
# LUA_ROOT, LUA_LDIR, and LUA_CDIR in luaconf.h.
INSTALL_TOP= /usr/local
INSTALL_BIN= $(INSTALL_TOP)/bin
INSTALL_INC= $(INSTALL_TOP)/include
INSTALL_LIB= $(INSTALL_TOP)/lib
INSTALL_MAN= $(INSTALL_TOP)/man/man1
INSTALL_LMOD= $(INSTALL_TOP)/share/lua/$V
INSTALL_CMOD= $(INSTALL_TOP)/lib/lua/$V

# How to install. If your install program does not support "-p", then
# you may have to run ranlib on the installed liblua.a.
INSTALL= install -p
INSTALL_EXEC= $(INSTALL) -m 0755
INSTALL_DATA= $(INSTALL) -m 0644
#
# If you don't have "install" you can use "cp" instead.
# INSTALL= cp -p
# INSTALL_EXEC= $(INSTALL)
# INSTALL_DATA= $(INSTALL)

# Other utilities.
MKDIR= mkdir -p
RM= rm -f

# == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE =======

# Convenience platforms targets.
PLATS= guess aix bsd c89 freebsd generic linux linux-readline macosx mingw posix solaris

# What to install.
TO_BIN= lua luac
TO_INC= lua.h luaconf.h lualib.h lauxlib.h lua.hpp
TO_LIB= liblua.a
TO_MAN= lua.1 luac.1

# Lua version and release.
V= 5.4
R= $V.0

# Targets start here.
all: $(PLAT)

$(PLATS) help test clean:
@cd src && $(MAKE) $@

install: dummy
cd src && $(MKDIR) $(INSTALL_BIN) $(INSTALL_INC) $(INSTALL_LIB) $(INSTALL_MAN) $(INSTALL_LMOD) $(INSTALL_CMOD)
cd src && $(INSTALL_EXEC) $(TO_BIN) $(INSTALL_BIN)
cd src && $(INSTALL_DATA) $(TO_INC) $(INSTALL_INC)
cd src && $(INSTALL_DATA) $(TO_LIB) $(INSTALL_LIB)
cd doc && $(INSTALL_DATA) $(TO_MAN) $(INSTALL_MAN)

uninstall:
cd src && cd $(INSTALL_BIN) && $(RM) $(TO_BIN)
cd src && cd $(INSTALL_INC) && $(RM) $(TO_INC)
cd src && cd $(INSTALL_LIB) && $(RM) $(TO_LIB)
cd doc && cd $(INSTALL_MAN) && $(RM) $(TO_MAN)

local:
$(MAKE) install INSTALL_TOP=../install

# make may get confused with install/ if it does not support .PHONY.
dummy:

# Echo config parameters.
echo:
@cd src && $(MAKE) -s echo
@echo "PLAT= $(PLAT)"
@echo "V= $V"
@echo "R= $R"
@echo "TO_BIN= $(TO_BIN)"
@echo "TO_INC= $(TO_INC)"
@echo "TO_LIB= $(TO_LIB)"
@echo "TO_MAN= $(TO_MAN)"
@echo "INSTALL_TOP= $(INSTALL_TOP)"
@echo "INSTALL_BIN= $(INSTALL_BIN)"
@echo "INSTALL_INC= $(INSTALL_INC)"
@echo "INSTALL_LIB= $(INSTALL_LIB)"
@echo "INSTALL_MAN= $(INSTALL_MAN)"
@echo "INSTALL_LMOD= $(INSTALL_LMOD)"
@echo "INSTALL_CMOD= $(INSTALL_CMOD)"
@echo "INSTALL_EXEC= $(INSTALL_EXEC)"
@echo "INSTALL_DATA= $(INSTALL_DATA)"

# Echo pkg-config data.
pc:
@echo "version=$R"
@echo "prefix=$(INSTALL_TOP)"
@echo "libdir=$(INSTALL_LIB)"
@echo "includedir=$(INSTALL_INC)"

# Targets that do not create files (not all makes understand .PHONY).
.PHONY: all $(PLATS) help test clean install uninstall local dummy echo pc

# (end of Makefile)
6 changes: 6 additions & 0 deletions lua-5.4.0/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

This is Lua 5.4.0, released on 18 Jun 2020.

For installation instructions, license details, and
further information about Lua, see doc/readme.html.

Loading