-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathUserdata.hs
More file actions
71 lines (65 loc) · 2.46 KB
/
Userdata.hs
File metadata and controls
71 lines (65 loc) · 2.46 KB
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
{-# LANGUAGE RankNTypes #-}
{-|
Module : HsLua.Core.Userdata
Copyright : © 2007–2012 Gracjan Polak;
© 2012–2016 Ömer Sinan Ağacan;
© 2017-2021 Albert Krewinkel
License : MIT
Maintainer : Albert Krewinkel <tarleb+hslua@zeitkraut.de>
Stability : beta
Portability : non-portable (depends on GHC)
Convenience functions to convert Haskell values into Lua userdata.
-}
module HsLua.Core.Userdata
( newhsuserdata
, newudmetatable
, fromuserdata
, putuserdata
) where
import HsLua.Core.Types (LuaE, Name (..), StackIndex, liftLua, fromLuaBool)
import Lua.Userdata
( hslua_fromuserdata
, hslua_newhsuserdata
, hslua_newudmetatable
, hslua_putuserdata
)
import qualified Data.ByteString as B
-- | Creates a new userdata wrapping the given Haskell object. The
-- userdata is pushed to the top of the stack.
newhsuserdata :: forall a e. a -> LuaE e ()
newhsuserdata = liftLua . flip hslua_newhsuserdata
{-# INLINABLE newhsuserdata #-}
-- | Creates and registers a new metatable for a userdata-wrapped
-- Haskell value; checks whether a metatable of that name has been
-- registered yet and uses the registered table if possible.
--
-- Using a metatable created by this functions ensures that the pointer
-- to the Haskell value will be freed when the userdata object is
-- garbage collected in Lua.
--
-- The name may not contain a nul character.
newudmetatable :: Name -> LuaE e Bool
newudmetatable (Name name) = liftLua $ \l ->
B.useAsCString name (fmap fromLuaBool . hslua_newudmetatable l)
{-# INLINABLE newudmetatable #-}
-- | Retrieves a Haskell object from userdata at the given index. The
-- userdata /must/ have the given name.
fromuserdata :: forall a e.
StackIndex -- ^ stack index of userdata
-> Name -- ^ expected name of userdata object
-> LuaE e (Maybe a)
fromuserdata idx (Name name) = liftLua $ \l ->
B.useAsCString name (hslua_fromuserdata l idx)
{-# INLINABLE fromuserdata #-}
-- | Replaces the Haskell value contained in the userdata value at
-- @index@. Checks that the userdata is of type @name@ and returns
-- 'True' on success, or 'False' otherwise.
putuserdata :: forall a e.
StackIndex -- ^ index
-> Name -- ^ name
-> a -- ^ new value
-> LuaE e Bool
putuserdata idx (Name name) x = liftLua $ \l ->
B.useAsCString name $ \namePtr ->
hslua_putuserdata l idx namePtr x
{-# INLINABLE putuserdata #-}