summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Kolesa <quaker66@gmail.com>2014-02-18 15:51:28 +0000
committerDaniel Kolesa <quaker66@gmail.com>2014-02-18 15:51:28 +0000
commita231ce24edf20f6b3f583465fb25f68eb620cfcc (patch)
tree78152f005c9e9b5d604d9ac8876fbf5ada9d0fe6
parent2416539d23e2dc45b5e598d4b6d34ab1ed57267a (diff)
replace io.open
-rw-r--r--luaxpedite.c1
-rw-r--r--require.lua25
2 files changed, 21 insertions, 5 deletions
diff --git a/luaxpedite.c b/luaxpedite.c
index fbdbc59..e0abe8a 100644
--- a/luaxpedite.c
+++ b/luaxpedite.c
@@ -14,7 +14,6 @@ int main(int argc, char **argv) {
14 14
15 LUAOPEN(base) 15 LUAOPEN(base)
16 LUAOPEN(string) 16 LUAOPEN(string)
17 LUAOPEN(io)
18 LUAOPEN(bit) 17 LUAOPEN(bit)
19 LUAOPEN(jit) 18 LUAOPEN(jit)
20 19
diff --git a/require.lua b/require.lua
index c85d454..7524330 100644
--- a/require.lua
+++ b/require.lua
@@ -1,13 +1,30 @@
1local bit, ffi = bit, ffi 1local bit, ffi = bit, ffi
2if require then ffi = require("ffi") end 2if require then ffi = require("ffi") end
3 3
4ffi.cdef [[
5 void *fopen (const char *fname, const char *mode);
6 int fseek (void *f, long offset, int origin);
7 long ftell (void *f);
8 int fclose(void *f);
9 size_t fread(void *ptr, size_t size, size_t count, void *f);
10 char *strerror(int errnum);
11 extern int errno;
12]]
13
14local C = ffi.C
15
4local loaded = { ["ffi"] = ffi, ["bit"] = bit } 16local loaded = { ["ffi"] = ffi, ["bit"] = bit }
5local loader = function(modname) 17local loader = function(modname)
6 local fname = modname:gsub("%.", "/") .. ".lua" 18 local fname = modname:gsub("%.", "/") .. ".lua"
7 local f, err = io.open(fname, "rb") 19 local f = C.fopen(fname, "rb")
8 if not f then return err end 20 if f == nil then return ffi.string(C.strerror(C.errno)) end
9 local cont = f:read("*all") 21 C.fseek(f, 0, 2) -- end
10 f:close() 22 local l = C.ftell(f)
23 C.fseek(f, 0, 0) -- beg
24 local buf = ffi.new("char[?]", l)
25 C.fread(buf, 1, l, f)
26 local cont = ffi.string(buf, l)
27 C.fclose(f)
11 local _, dot = fname:find(".*%.") 28 local _, dot = fname:find(".*%.")
12 if dot then fname = fname:sub(dot + 1) end 29 if dot then fname = fname:sub(dot + 1) end
13 f, err = load(cont, "@" .. fname) 30 f, err = load(cont, "@" .. fname)