summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Kolesa <quaker66@gmail.com>2014-02-18 15:37:55 +0000
committerDaniel Kolesa <quaker66@gmail.com>2014-02-18 15:37:55 +0000
commit2416539d23e2dc45b5e598d4b6d34ab1ed57267a (patch)
tree6ae5c1920571fe1428f2c647801bbbe8efab6fc1
parent66e4b81171d9b426d2046ab5407a2891db1dec47 (diff)
replace require and add a minimal wrapper C
-rw-r--r--luaxpedite.c36
-rw-r--r--main.lua2
-rw-r--r--require.lua45
-rw-r--r--util.lua3
4 files changed, 86 insertions, 0 deletions
diff --git a/luaxpedite.c b/luaxpedite.c
new file mode 100644
index 0000000..fbdbc59
--- /dev/null
+++ b/luaxpedite.c
@@ -0,0 +1,36 @@
1#include <lua.h>
2#include <lualib.h>
3#include <lauxlib.h>
4
5int main(int argc, char **argv) {
6 lua_State *L;
7 int i;
8 L = luaL_newstate();
9
10#define LUAOPEN(name) \
11 lua_pushcfunction(L, luaopen_##name); \
12 lua_pushstring(L, #name); \
13 lua_call(L, 1, 0);
14
15 LUAOPEN(base)
16 LUAOPEN(string)
17 LUAOPEN(io)
18 LUAOPEN(bit)
19 LUAOPEN(jit)
20
21 lua_pushcfunction(L, luaopen_ffi);
22 lua_pushstring(L, "ffi");
23 lua_call(L, 1, 1);
24 lua_setglobal(L, "ffi");
25
26 lua_createtable(L, argc, 0);
27 for (i = 0; i < argc; ++i) {
28 lua_pushstring(L, argv[i]);
29 lua_rawseti(L, -2, i);
30 }
31 lua_setglobal(L, "arg");
32 if (!luaL_loadfile(L, "main.lua")) lua_call(L, 0, 0);
33 else lua_pop(L, argc);
34 lua_close(L);
35 return 0;
36} \ No newline at end of file
diff --git a/main.lua b/main.lua
index 1f4058b..915c8d4 100644
--- a/main.lua
+++ b/main.lua
@@ -1,3 +1,5 @@
1dofile("require.lua")
2
1local util = require("util") 3local util = require("util")
2local ffi = require("ffi") 4local ffi = require("ffi")
3 5
diff --git a/require.lua b/require.lua
new file mode 100644
index 0000000..c85d454
--- /dev/null
+++ b/require.lua
@@ -0,0 +1,45 @@
1local bit, ffi = bit, ffi
2if require then ffi = require("ffi") end
3
4local loaded = { ["ffi"] = ffi, ["bit"] = bit }
5local loader = function(modname)
6 local fname = modname:gsub("%.", "/") .. ".lua"
7 local f, err = io.open(fname, "rb")
8 if not f then return err end
9 local cont = f:read("*all")
10 f:close()
11 local _, dot = fname:find(".*%.")
12 if dot then fname = fname:sub(dot + 1) end
13 f, err = load(cont, "@" .. fname)
14 if not f then error("error loading module '" .. modname
15 .. "' from file '" .. fname .. "':\n" .. err, 2)
16 end
17 return f
18end
19
20local find_loader = function(modname)
21 local err = ("module '%s' not found\n"):format(modname)
22 local v = loader(modname)
23 if type(v) == "function" then
24 return v
25 elseif type(v) == "string" then
26 err = err .. v
27 end
28 return nil, err
29end
30
31require = function(modname)
32 local v = loaded[modname]
33 if v ~= nil then return v end
34 local loader, err = find_loader(modname)
35 if not loader then error(err, 2) end
36 local ret = loader(modname)
37 if ret ~= nil then
38 loaded[modname] = ret
39 return ret
40 elseif loaded[modname] == nil then
41 loaded[modname] = true
42 return true
43 end
44 return loaded[modname]
45end \ No newline at end of file
diff --git a/util.lua b/util.lua
index ad6edfb..162a58f 100644
--- a/util.lua
+++ b/util.lua
@@ -1067,6 +1067,7 @@ end
1067ffi.cdef [[ 1067ffi.cdef [[
1068 extern void *stdout, *stderr; 1068 extern void *stdout, *stderr;
1069 int fputs (const char *str, void *stream); 1069 int fputs (const char *str, void *stream);
1070 int printf(const char *fmt, ...);
1070]] 1071]]
1071 1072
1072M.write_out = function(str) 1073M.write_out = function(str)
@@ -1077,6 +1078,8 @@ M.write_err = function(str)
1077 C.fputs(str, C.stderr) 1078 C.fputs(str, C.stderr)
1078end 1079end
1079 1080
1081M.printf = C.printf
1082
1080local resolutions = { 1083local resolutions = {
1081 { "qvga", 320 , 240 }, 1084 { "qvga", 320 , 240 },
1082 { "qvga-p", 240 , 320 }, 1085 { "qvga-p", 240 , 320 },