-
Notifications
You must be signed in to change notification settings - Fork 1
/
ml_lua.c
80 lines (65 loc) · 1.64 KB
/
ml_lua.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <string.h>
#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/callback.h>
#include <caml/fail.h>
#include <caml/custom.h>
#include <lua5.1/lua.h>
#include <lua5.1/lualib.h>
#include <lua5.1/lauxlib.h>
value ml_lua_modinfo (value string)
{
CAMLparam1 (string);
CAMLlocal4 (name, version, depends, tuple);
int err, i, n;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
err = luaL_dostring (L, String_val(string));
if (err != 0) {
caml_failwith("Lua.modinfo");
}
name = caml_alloc_string(0);
version = caml_alloc_string(0);
depends = caml_alloc_tuple(0);
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
const char *s = lua_tostring(L, -2);
// Get name string
if (strcasecmp(s, "name") == 0) {
const char *s = lua_tostring(L, -1);
name = caml_copy_string(s);
}
// Get depends array
else if (strcasecmp(s, "depend") == 0) {
lua_pushstring(L, "table");
lua_gettable(L, LUA_GLOBALSINDEX);
lua_pushstring(L, "getn");
lua_gettable(L, -2);
lua_pushvalue(L, -3);
lua_call(L, 1, 1);
n = lua_tonumber(L, -1);
lua_pop(L, 2);
depends = caml_alloc_tuple(n);
i = 0;
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
const char *s = lua_tostring(L, -1);
Store_field(depends, i, caml_copy_string(s));
i++;
lua_pop(L, 1);
}
}
// Get version string
else if (strcasecmp(s, "version") == 0) {
const char *s = lua_tostring(L, -1);
version = caml_copy_string(s);
}
lua_pop(L, 1);
}
tuple = caml_alloc_tuple(3);
Store_field(tuple, 0, name);
Store_field(tuple, 1, version);
Store_field(tuple, 2, depends);
CAMLreturn (tuple);
}