Lua: ensure that 're' module is always available.

The module is shipped with LPeg.
This commit is contained in:
Albert Krewinkel 2021-11-08 12:14:44 +01:00
parent cc46667953
commit ab0fe676a8
No known key found for this signature in database
GPG key ID: 388DC0B21F631124
3 changed files with 18 additions and 2 deletions

View file

@ -563,7 +563,7 @@ library
http-types >= 0.8 && < 0.13, http-types >= 0.8 && < 0.13,
ipynb >= 0.1.0.2 && < 0.2, ipynb >= 0.1.0.2 && < 0.2,
jira-wiki-markup >= 1.4 && < 1.5, jira-wiki-markup >= 1.4 && < 1.5,
lpeg >= 1.0 && < 1.1, lpeg >= 1.0.1 && < 1.1,
mtl >= 2.2 && < 2.3, mtl >= 2.2 && < 2.3,
network >= 2.6, network >= 2.6,
network-uri >= 2.6 && < 2.8, network-uri >= 2.6 && < 2.8,

View file

@ -46,6 +46,7 @@ initLuaState :: PandocLua ()
initLuaState = do initLuaState = do
liftPandocLua Lua.openlibs liftPandocLua Lua.openlibs
installPandocPackageSearcher installPandocPackageSearcher
installLpegSearcher
initPandocModule initPandocModule
requireGlobalModules requireGlobalModules
loadInitScript "init.lua" loadInitScript "init.lua"
@ -99,6 +100,13 @@ initLuaState = do
-- Module on top of stack. Register as global -- Module on top of stack. Register as global
Lua.setglobal "lpeg" Lua.setglobal "lpeg"
installLpegSearcher :: PandocLua ()
installLpegSearcher = liftPandocLua $ do
Lua.getglobal' "package.searchers"
Lua.pushHaskellFunction $ Lua.state >>= liftIO . LPeg.lpeg_searcher
Lua.rawseti (Lua.nth 2) . (+1) . fromIntegral =<< Lua.rawlen (Lua.nth 2)
Lua.pop 1 -- remove 'package.searchers' from stack
-- | AST elements are marshaled via normal constructor functions in the -- | AST elements are marshaled via normal constructor functions in the
-- @pandoc@ module. However, accessing Lua globals from Haskell is -- @pandoc@ module. However, accessing Lua globals from Haskell is
-- expensive (due to error handling). Accessing the Lua registry is much -- expensive (due to error handling). Accessing the Lua registry is much

View file

@ -18,7 +18,7 @@ import Control.Monad (when)
import HsLua as Lua hiding (Operation (Div), error) import HsLua as Lua hiding (Operation (Div), error)
import System.FilePath ((</>)) import System.FilePath ((</>))
import Test.Tasty (TestTree, localOption) import Test.Tasty (TestTree, localOption)
import Test.Tasty.HUnit (Assertion, HasCallStack, assertEqual, testCase) import Test.Tasty.HUnit ((@=?), Assertion, HasCallStack, assertEqual, testCase)
import Test.Tasty.QuickCheck (QuickCheckTests (..), ioProperty, testProperty) import Test.Tasty.QuickCheck (QuickCheckTests (..), ioProperty, testProperty)
import Text.Pandoc.Arbitrary () import Text.Pandoc.Arbitrary ()
import Text.Pandoc.Builder (bulletList, definitionList, displayMath, divWith, import Text.Pandoc.Builder (bulletList, definitionList, displayMath, divWith,
@ -211,6 +211,14 @@ tests = map (localOption (QuickCheckTests 20))
ty <- Lua.ltype Lua.top ty <- Lua.ltype Lua.top
Lua.liftIO $ assertEqual "module should be a table" Lua.TypeTable ty Lua.liftIO $ assertEqual "module should be a table" Lua.TypeTable ty
, testCase "module 'lpeg' is loaded into a global" . runLuaTest $ do
s <- Lua.dostring "assert(type(lpeg)=='table');assert(lpeg==require'lpeg')"
Lua.liftIO $ Lua.OK @=? s
, testCase "module 're' is available" . runLuaTest $ do
s <- Lua.dostring "require 're'"
Lua.liftIO $ Lua.OK @=? s
, testCase "informative error messages" . runLuaTest $ do , testCase "informative error messages" . runLuaTest $ do
Lua.pushboolean True Lua.pushboolean True
-- Lua.newtable -- Lua.newtable