feat(lua): add set_global_function template, and to_name_ext function

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2013-11-07 20:48:49 -08:00
parent db8b16641c
commit fb09fc9fe6
7 changed files with 21 additions and 17 deletions

View file

@ -128,13 +128,9 @@ void open_format(lua_State * L) {
lua_setfield(L, -2, "__index");
setfuncs(L, format_m, 0);
lua_pushcfunction(L, safe_function<mk_format>);
lua_setglobal(L, "format");
lua_pushcfunction(L, safe_function<format_line>);
lua_setglobal(L, "line");
lua_pushcfunction(L, safe_function<format_space>);
lua_setglobal(L, "space");
set_global_function<mk_format>(L, "format");
set_global_function<format_line>(L, "line");
set_global_function<format_space>(L, "space");
}
}
#endif

View file

@ -21,6 +21,13 @@ name & to_name(lua_State * L, int idx) {
return *static_cast<name*>(luaL_checkudata(L, idx, name_mt));
}
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);
}
int push_name(lua_State * L, name const & n) {
void * mem = lua_newuserdata(L, sizeof(name));
new (mem) name(n);
@ -78,8 +85,7 @@ void open_name(lua_State * L) {
luaL_newmetatable(L, name_mt);
setfuncs(L, name_m, 0);
lua_pushcfunction(L, safe_function<mk_name>);
lua_setglobal(L, "name");
set_global_function<mk_name>(L, "name");
}
}
#endif

View file

@ -11,6 +11,7 @@ class name;
void open_name(lua_State * L);
bool is_name(lua_State * L, int idx);
name & to_name(lua_State * L, int idx);
name to_name_ext(lua_State * L, int idx);
int push_name(lua_State * L, name const & n);
}
#endif

View file

@ -121,8 +121,7 @@ void open_mpz(lua_State * L) {
luaL_newmetatable(L, mpz_mt);
setfuncs(L, mpz_m, 0);
lua_pushcfunction(L, safe_function<mk_mpz>);
lua_setglobal(L, "mpz");
set_global_function<mk_mpz>(L, "mpz");
}
constexpr char const * mpq_mt = "mpq.mt";
@ -233,8 +232,7 @@ void open_mpq(lua_State * L) {
luaL_newmetatable(L, mpq_mt);
setfuncs(L, mpq_m, 0);
lua_pushcfunction(L, safe_function<mk_mpq>);
lua_setglobal(L, "mpq");
set_global_function<mk_mpq>(L, "mpq");
}
}
#endif

View file

@ -195,8 +195,7 @@ void open_options(lua_State * L) {
lua_setfield(L, -2, "__index");
setfuncs(L, options_m, 0);
lua_pushcfunction(L, safe_function<mk_option>);
lua_setglobal(L, "options");
set_global_function<mk_option>(L, "options");
}
}
#endif

View file

@ -204,8 +204,8 @@ void open_sexpr(lua_State * L) {
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
setfuncs(L, sexpr_m, 0);
lua_pushcfunction(L, safe_function<mk_sexpr>);
lua_setglobal(L, "sexpr");
set_global_function<mk_sexpr>(L, "sexpr");
}
}
#endif

View file

@ -16,5 +16,9 @@ int safe_function_wrapper(lua_State * L, lua_CFunction f);
template<lua_CFunction F> int safe_function(lua_State * L) {
return safe_function_wrapper(L, F);
}
template<lua_CFunction F> void set_global_function(lua_State * L, char const * name) {
lua_pushcfunction(L, safe_function<F>);
lua_setglobal(L, name);
}
}
#endif