@@ -34,6 +34,8 @@ import Foreign.Lua (Lua, NumResults (..), Optional,
3434import Text.DocLayout (Doc )
3535
3636import qualified Foreign.Lua as Lua
37+ import qualified Foreign.Lua.Types.Peekable as Lua
38+ import qualified Foreign.Lua.Userdata as Lua
3739import qualified Text.DocLayout as Doc
3840
3941--
@@ -63,19 +65,32 @@ render doc optLength = return $ Doc.render (Lua.fromOptional optLength) doc
6365-- Marshaling
6466--
6567
68+ -- | Name used for the @Doc@ Lua userdata values.
69+ docTypeName :: String
70+ docTypeName = " HsLua DocLayout.Doc"
71+
6672-- | Retrieve a @Doc Text@ value from the Lua stack. Strings are
6773-- converted to plain @'Doc'@ values.
6874peekDoc :: StackIndex -> Lua (Doc Text )
6975peekDoc idx = Lua. ltype idx >>= \ case
70- Lua. TypeString -> Doc. literal <$> Lua. peek idx
71- _ -> Lua. peekAny idx
76+ Lua. TypeString -> Doc. literal <$> Lua. peek idx
77+ _ -> Lua. reportValueOnFailure docTypeName
78+ (`Lua.toAnyWithName` docTypeName)
79+ idx
7280
7381instance Peekable (Doc Text ) where
7482 peek = peekDoc
7583
7684-- | Push a @Doc Text@ value to the Lua stack.
7785pushDoc :: Doc Text -> Lua ()
78- pushDoc = Lua. pushAny
86+ pushDoc = Lua. pushAnyWithMetatable pushDocMT
87+ where
88+ pushDocMT = Lua. ensureUserdataMetatable docTypeName $
89+ Lua. addfunction " __tostring" __tostring
7990
8091instance Pushable (Doc Text ) where
8192 push = pushDoc
93+
94+ -- | Convert to string by rendering without reflowing.
95+ __tostring :: Doc Text -> Lua Text
96+ __tostring d = return $ Doc. render Nothing d
0 commit comments