-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathModule.hs
More file actions
114 lines (104 loc) · 3.43 KB
/
Module.hs
File metadata and controls
114 lines (104 loc) · 3.43 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
{-# LANGUAGE OverloadedStrings #-}
{-|
Module : HsLua.Packaging.Module
Copyright : © 2019-2023 Albert Krewinkel
License : MIT
Maintainer : Albert Krewinkel <albert+hslua@zeitkraut.de>
Stability : alpha
Portability : Requires GHC 8 or later.
Utility functions for HsLua modules.
-}
module HsLua.Packaging.Module
( -- * Documented module
Module (..)
, Field (..)
, registerModule
, preloadModule
, preloadModuleWithName
, pushModule
, Operation (..)
)
where
import Control.Monad (forM_)
import HsLua.Core
import HsLua.Marshalling (Pusher, pushAsTable, pushList, pushName, pushText)
import HsLua.ObjectOrientation.Operation (Operation (..), metamethodName)
import HsLua.Packaging.Documentation
import HsLua.Packaging.Types
import qualified HsLua.Packaging.Function as Fun
-- | Create a new module (i.e., a Lua table).
create :: LuaE e ()
create = newtable
-- | Registers a 'Module'; leaves a copy of the module table on
-- the stack.
registerModule :: LuaError e => Module e -> LuaE e ()
registerModule mdl =
requirehs (moduleName mdl) (const (pushModule mdl))
-- | Add the module under a different name to the table of preloaded
-- packages.
preloadModuleWithName :: LuaError e => Module e -> Name -> LuaE e ()
preloadModuleWithName documentedModule name = preloadModule $
documentedModule { moduleName = name }
-- | Preload self-documenting module using the module's default name.
preloadModule :: LuaError e => Module e -> LuaE e ()
preloadModule mdl =
preloadhs (moduleName mdl) $ do
pushModule mdl
return (NumResults 1)
-- | Pushes a documented module to the Lua stack.
pushModule :: LuaError e => Module e -> LuaE e ()
pushModule mdl = do
checkstack' 10 "pushModule"
pushAsTable
[ ("name", pushName . moduleName)
, ("description", pushText . moduleDescription)
, ("fields", pushList pushFieldDoc . moduleFields)
, ("types", pushTypesFunction . moduleTypeInitializers)
] mdl
create -- module table
pushvalue (nth 2) -- push documentation object
registerDocumentation (nth 2) -- set and pop doc
-- # Functions
--
-- module table now on top
-- documentation table in pos 2
newtable -- function documention
pushName "functions"
pushvalue (nth 2)
rawset (nth 5)
-- function documentation table now on top
-- module table in position 2
-- module documentation table in pos 3
forM_ (zip [1..] (moduleFunctions mdl)) $ \(i, fn) -> do
-- push documented function, thereby registering the function docs
Fun.pushDocumentedFunction fn
-- add function to module
pushName (functionName fn)
pushvalue (nth 2) -- C function
rawset (nth 5) -- module table
-- set documentation
_ <- getdocumentation top
rawseti (nth 3) i
pop 1 -- C Function
pop 1 -- function documentation table
remove (nth 2) -- module documentation table
-- # Fields
--
forM_ (moduleFields mdl) $ \field -> do
pushText (fieldName field)
fieldPushValue field
rawset (nth 3)
case moduleOperations mdl of
[] -> pure ()
ops -> do
-- create a metatable for this module and add operations
newtable
forM_ ops $ \(op, fn) -> do
pushName $ metamethodName op
Fun.pushDocumentedFunction $ Fun.setName "" fn
rawset (nth 3)
setmetatable (nth 2)
pushTypesFunction :: LuaError e => Pusher e [LuaE e Name]
pushTypesFunction initializers = pushHaskellFunction $ do
sequence initializers >>= pushList pushName
pure 1