electrodude512 wrote:A mod can include a C library (with require() or something), right? I really don't want to have to do that, but I might have to.
/* gcc -Wall -shared -fPIC -o mttestc.so test.c */
#include <stdio.h> /* Just for basic testing (e.g. printf()) */
#include <lua.h>
#include <lauxlib.h>
#define LUA_NAMESPACE "test"
#define MODNAME "[testc] "
#if LUA_VERSION_NUM > 501
# define luaL_reg luaL_Reg
#endif
struct Params {
int a, b;
};
static int l_testfn(lua_State *L);
/* Private functions */
static int parseargs(lua_State *L, struct Params *args);
const struct luaL_reg lua_funcs[] = {
{ "testfn", l_testfn },
{ NULL, NULL}
};
/* Entry point to the module */
int luaopen_mttestc(lua_State *L)
{
const luaL_reg *f;
luaL_newmetatable(L, LUA_NAMESPACE);
lua_pushstring(L, "__index");
lua_newtable(L);
/* "install" functions into metatable */
for (f = lua_funcs; f->func != NULL && f->name != NULL; f++) {
lua_pushcfunction(L, f->func);
lua_setfield(L, -2, f->name);
}
return 1;
}
/* "Lua Prototype": int testfn(a, b); --> a*b */
static int l_testfn(lua_State *L)
{
struct Params args;
if (!parseargs(L, &args)) {
lua_pushstring(L, MODNAME"Error parsing settings");
lua_error(L);
}
printf("************** FROM C args: %d, %d\n", args.a, args.b);
lua_pushnumber(L, args.a * args.b); // Push a*b onto stack (return value)
return 1;
}
static int parseargs(lua_State *L, struct Params *args)
{
args->a = luaL_checknumber(L, 1);
args->b = luaL_checknumber(L, 2);
return 1;
}
local MYCMOD = require("mttestc")
print("Something:" .. MYCMOD.testfn(5, 6))
local path = minetest.get_modpath("computertest").."/threads.so"
print("Loading threads library at "..path)
local threads = require(path) -- line 9 in error message
Loading threads library at /home/<username>/src/minetest/bin/../mods/computertest/threads.so
12:13:55: ERROR[main]: ========== ERROR FROM LUA ===========
12:13:55: ERROR[main]: Failed to load and run script from
12:13:55: ERROR[main]: /home/<username>/src/minetest/bin/../mods/computertest/init.lua:
12:13:55: ERROR[main]: caught (...)
12:13:55: ERROR[main]: stack traceback:
12:13:55: ERROR[main]: [C]: in function 'require'
12:13:55: ERROR[main]: ...<username>/src/minetest/bin/../mods/computertest/init.lua:9: in main chunk
12:13:55: ERROR[main]: ======= END OF ERROR FROM LUA ========
12:13:55: ERROR[main]: Server: Failed to load and run /home/<username>/src/minetest/bin/../mods/computertest/init.lua
12:13:55: ERROR[main]: ModError: ModError: Failed to load and run /home/<username>/src/minetest/bin/../mods/computertest/init.lua
Neuromancer wrote:Would you consider making your mod compatible with the blockly add on for computer craft?
http://blockly-lua.appspot.com/static/a ... /index.htm
I see the computer craft mod as a gateway to teaching beginners lua. They start with blockly lua using the computer mod. Move on to lua in computer craft, and then writing Mods for Minetest.
Neuromancer wrote:What I like about minetest is that with the API already in lua, a Minetest computer craft mod is potentially much more powerful because it can access the entire API. For example it is difficult to detect water in MC Computercraft, but that would be trivial in the Minetest version.
aldobr wrote:You dont load libraries like that anymore.
Minetest is using LuaJIT. LuaJIT includes a thing called LuaFFI.
LuaFFI allows writting mods in C language and calling external libraries.
See: http://luajit.org/ext_ffi.html
electrodude512 wrote:I can't get it to find your example C library. It compiles fine, but it gives this error when I try to play the game. It gives the same error if I change the name of the library to look for to something that doesn't exist, so it must not be finding it.
Do you have any idea what's wrong?
Thanks,
electrodude
Is this merely the recommended way or actually the only way?
The Minetest that I built manually from git uses LuaJIT, but the version that the Gentoo package manager gave me uses normal Lua.
Is there some way that will work for both Luas? Also, FFI probably won't work, because I need a reference to the lua state so I can lua_xmove stuff between multiple server and computer threads. FFI will only work if Lua is set up to be threadsafe, otherwise I'll have to use lua_xmove and normal libraries. Lua isn't set up to be threadsafe by default but Minetest might set it up right, and I have no idea about LuaJIT's threadsafeness.
aldobr wrote:Is this merely the recommended way or actually the only way?
There is no recommended way. But doing it other way makes no sense.
aldobr wrote:I will give you a clue, its not used to embbed lua inside an application. It has nothing to do with that.
aldobr wrote:Dude, i did a favour to you by enlightening you about LuaFFI.
Instead of learning you are now resorting to nitpicking on dictionary definitions. But, anyway, i lose nothing by ignoring you.
There are some people that cannot be wrong.
ninnghazad wrote:edit:
debug.sethook(co, coroutine.yield, "", 100) won't work with pcall/xpcall in version of lua minetest uses, so
pcall(function() while true do end end) would still mean whole server locks. meh...
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> function foo() while true do print("Hello"); end; end
> t = coroutine.create(foo)
> debug.sethook(t, coroutine.yield, "", 10)
> function r() s, e = coroutine.resume(t); if not s then print("ERROR: "..e); end; end
> r()
Hello
Hello
ERROR: attempt to yield across metamethod/C-call boundary
ninnghazad wrote:btw: of course i wanted to call it computertest... hmpf, well its called dronetest now ;)
Users browsing this forum: No registered users and 2 guests