2013-11-03 03:49:42 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include <lua.hpp>
|
|
|
|
#include "util/debug.h"
|
|
|
|
#include "util/name.h"
|
2013-11-03 20:16:23 +00:00
|
|
|
#include "bindings/lua/util.h"
|
2013-11-03 03:49:42 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-11-05 03:45:15 +00:00
|
|
|
constexpr char const * name_mt = "name.mt";
|
|
|
|
|
|
|
|
bool is_name(lua_State * L, int idx) {
|
|
|
|
return testudata(L, idx, name_mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
name & to_name(lua_State * L, int idx) {
|
|
|
|
return *static_cast<name*>(luaL_checkudata(L, idx, name_mt));
|
2013-11-05 02:43:41 +00:00
|
|
|
}
|
|
|
|
|
2013-11-08 04:48:49 +00:00
|
|
|
name to_name_ext(lua_State * L, int idx) {
|
|
|
|
if (lua_isstring(L, idx))
|
|
|
|
return luaL_checkstring(L, idx);
|
|
|
|
else
|
|
|
|
return to_name(L, idx);
|
|
|
|
}
|
|
|
|
|
2013-11-05 05:28:17 +00:00
|
|
|
int push_name(lua_State * L, name const & n) {
|
|
|
|
void * mem = lua_newuserdata(L, sizeof(name));
|
|
|
|
new (mem) name(n);
|
|
|
|
luaL_getmetatable(L, name_mt);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-11-03 03:49:42 +00:00
|
|
|
static int mk_name(lua_State * L) {
|
|
|
|
int nargs = lua_gettop(L);
|
2013-11-05 02:43:41 +00:00
|
|
|
name r;
|
|
|
|
for (int i = 1; i <= nargs; i++) {
|
|
|
|
if (lua_isnil(L, i)) {
|
|
|
|
// skip
|
|
|
|
} else if (lua_isuserdata(L, i)) {
|
|
|
|
r = r + to_name(L, i);
|
|
|
|
} else if (lua_isstring(L, i)) {
|
|
|
|
r = name(r, luaL_checkstring(L, i));
|
2013-11-03 03:49:42 +00:00
|
|
|
} else {
|
2013-11-05 02:43:41 +00:00
|
|
|
r = name(r, luaL_checkinteger(L, i));
|
2013-11-03 03:49:42 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-05 05:28:17 +00:00
|
|
|
return push_name(L, r);
|
2013-11-03 03:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int name_gc(lua_State * L) {
|
2013-11-05 02:43:41 +00:00
|
|
|
to_name(L, 1).~name();
|
2013-11-03 03:49:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int name_tostring(lua_State * L) {
|
2013-11-13 20:14:55 +00:00
|
|
|
lua_pushstring(L, to_name(L, 1).to_string().c_str());
|
2013-11-03 03:49:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int name_eq(lua_State * L) {
|
2013-11-05 02:43:41 +00:00
|
|
|
lua_pushboolean(L, to_name(L, 1) == to_name(L, 2));
|
2013-11-03 03:49:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int name_lt(lua_State * L) {
|
2013-11-05 02:43:41 +00:00
|
|
|
lua_pushboolean(L, to_name(L, 1) < to_name(L, 2));
|
2013-11-03 03:49:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-11-08 20:40:28 +00:00
|
|
|
static int name_pred(lua_State * L) {
|
|
|
|
lua_pushboolean(L, is_name(L, 1));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-11-11 21:32:56 +00:00
|
|
|
static int name_hash(lua_State * L) {
|
|
|
|
lua_pushinteger(L, to_name(L, 1).hash());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-11-05 02:48:21 +00:00
|
|
|
static const struct luaL_Reg name_m[] = {
|
|
|
|
{"__gc", name_gc}, // never throws
|
|
|
|
{"__tostring", safe_function<name_tostring>},
|
|
|
|
{"__eq", safe_function<name_eq>},
|
|
|
|
{"__lt", safe_function<name_lt>},
|
2013-11-11 21:32:56 +00:00
|
|
|
{"hash", safe_function<name_hash>},
|
2013-11-05 02:48:21 +00:00
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
2013-11-04 21:54:51 +00:00
|
|
|
void open_name(lua_State * L) {
|
2013-11-05 03:45:15 +00:00
|
|
|
luaL_newmetatable(L, name_mt);
|
2013-11-11 21:32:56 +00:00
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, -2, "__index");
|
2013-11-03 20:16:23 +00:00
|
|
|
setfuncs(L, name_m, 0);
|
2013-11-03 03:49:42 +00:00
|
|
|
|
2013-11-13 19:46:09 +00:00
|
|
|
SET_GLOBAL_FUN(mk_name, "name");
|
|
|
|
SET_GLOBAL_FUN(name_pred, "is_name");
|
2013-11-03 03:49:42 +00:00
|
|
|
}
|
|
|
|
}
|