File: hsludata.c

package info (click to toggle)
haskell-lua 2.3.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 276 kB
  • sloc: haskell: 1,582; ansic: 403; makefile: 7
file content (44 lines) | stat: -rw-r--r-- 1,235 bytes parent folder | download | duplicates (2)
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;
}