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
|
#include <HsFFI.h>
#include "hslua.h"
#include "hsludata.h"
/* ***************************************************************
* Userdata for Haskell values
* ***************************************************************/
/*
** Free stable Haskell pointer in userdata.
**
** The userdata whose contents is garbage collected must be on
** stack index 1 (i.e., the first argument).
*/
static int hslua_userdata_gc(lua_State *L)
{
HsStablePtr *userdata = lua_touserdata(L, 1);
if (userdata) {
hs_free_stable_ptr(*userdata);
}
return 0;
}
/*
** Creates a new userdata metatable for Haskell objects, or gets
** is from the registry if possible.
**
** Returns `true` if the metatable was created, and `false` if it
** already existed and was fetched from the registry.
*/
int hslua_newudmetatable(lua_State *L, const char *tname)
{
int created = luaL_newmetatable(L, tname);
if (created) {
/* Prevent accessing or changing the metatable with
* getmetatable/setmetatable. */
lua_pushboolean(L, 1);
lua_setfield(L, -2, "__metatable");
/* Mark objects for finalization when collecting garbage. */
lua_pushcfunction(L, &hslua_userdata_gc);
lua_setfield(L, -2, "__gc");
}
return created;
}
|