augmented Lua
$ git clone https://github.com/edubart/minilua.git $ git clone --recursive https://github.com/edubart/nelua-decl.git $ ( cd nelua-decl/gcc-lua; make ) $ cp minilua/minilua.h nelua-decl/libs/minilua $ ( cd nelua-decl/libs/minilua; make ) $ nelua lua.nelua -Lnelua-decl/libs/minilua 3 HELLO
with lua.nelua this test from discord:
## function make_all_functions_available_in_lua_please(module)
## local nfuncs = 0
## for funcname,funcsym in pairs(module.value.metafields) do
local function #|funcname|#(L: *lua_State): cint
## local argtypes = funcsym.type.argtypes
## local callargs = {}
## for i,argtype in ipairs(argtypes) do
## if argtype.is_integral then
local #|'a'..i|# = lua_tointeger(L, #[i]#)
## elseif argtype.is_string then
local #|'a'..i|# = lua_tostring(L, #[i]#)
## end
## callargs[i] = aster.Id{'a'..i}
## end
local ret = #[funcsym]#(#[aster.unpack(callargs)]#)
## local rettype = funcsym.type:get_return_type(1)
## if rettype.is_void then
return 0
## elseif rettype.is_integral then
lua_pushinteger(L, ret)
return 1
## elseif rettype.is_string then
lua_pushlstring(L, ret, #ret)
return 1
## end
end
## nfuncs = nfuncs + 1
## end
local libfuncs: [#[nfuncs+1]#]luaL_Reg
## local i = 0
## for funcname in pairs(module.value.metafields) do
libfuncs[#[i]#] = {#[funcname]#, #|funcname|#}
## i = i + 1
## end
lua_createtable(L, 0, #libfuncs)
luaL_setfuncs(L, &libfuncs[0], 0)
lua_setglobal(L, #[module.name]#)
## end
require 'minilua'
require 'string'
-- module exposed to Lua
local mymodule = @record{}
function mymodule.sum(a: integer, b: integer): integer
return a+b
end
function mymodule.upper(a: string): string
return a:upper()
end
-- create a Lua state and bind all libraries
local L = luaL_newstate()
luaL_openlibs(L)
## make_all_functions_available_in_lua_please(mymodule, L)
-- execute a script
luaL_loadstring(L, [[
print(mymodule.sum(1,2))
print(mymodule.upper("hello"))
]])
lua_call(L, 0, 0)
-- destroy Lua
lua_close(L)