Come together over FFI
Albert Krewinkel
web gateway
(PostgREST, Hasura)
window manager
(XMonad)
web gateway
(Kong)
window manager
(Awesome)
Complex systems
Scriptable systems
data Inline
= Str Text -- ^ Text (string)
| Emph [Inline] -- ^ Emphasized text
| SmallCaps [Inline] -- ^ Small caps text
-- ⋮
Markdown | Pandoc AST |
---|---|
*Hello* |
Emph [Str "Hello"] |
_Hello_ |
Emph [Str "Hello"] |
_*Hello*_ |
Emph [Emph [Str "Hello"]] |
? | SmallCaps [Str "Hello"]] |
function Emph (em)
local nested = em.content[1]
if nested and nested.t == 'Emph' then
return pandoc.SmallCaps(nested.content)
end
end
In action
% echo '_*Hello*_' | pandoc --to=latex
⇒ \emph{\emph{Hello}}
% echo '_*Hello*_' | pandoc --to=latex --lua-filter=sc.lua
⇒ \textsc{Hello}
Haskell | Lua | |
---|---|---|
typing | static | dynamic |
programs | compiled | interpreted |
FP | ✓ | ✓ |
GC | ✓ | ✓ |
C API | ✓ | ✓ |
C header
Import
Simple in C
Expressive in Haskell
Basic bindings to Lua
Lua state as first argument
,----------.
| arg 3 |
+----------+
| arg 2 |
+----------+
| arg 1 |
+----------+ ,----------.
| function | call 3 1 | result 1 |
+----------+ ===========> +----------+
| | | |
| stack | | stack |
| | | |
<----???--- Emph [Str "a"] :: Inline
,----------.
| 5 |
+----------+
| true |
+----------+
| "banana" |
pushInline x = do
newtable
case x of
Str txt -> pushText txt *> setfield (nth 2) "text"
Emph xs -> pushList pushInline xs *>
setfield (nth 2) "content"
-- ...
Str "a"
becomes
Metatables define object behavior.
,---------.
| ??? ---|----> data
`---------'
Foreign.StablePtr
,------------.
| StablePtr -|----> Haskell value
`------------'
Set __gc
metamethod on userdata.
mkRow = defun "Row"
### liftPure2 Row -- lift a pure Haskell function to Lua
<#> udparam typeAttr "attr" "cell attributes"
<#> parameter (peekList peekCell) "{Cell,...}"
"cells" "row cells"
=#> udResult typeRow "new Row object"
registerDocumentedFunction mkRow
In Lua:
main = do
luaTest <- withCurrentDirectory "test" . run $ do
translateResultsFromFile "example-tests.lua"
defaultMain . testGroup "Haskell and Lua tests" $
[ luaTest {- more tasty tests go here -} ]
test/example-tests.lua
constructor
has type `userdata`: OK
accepts list of integers: OK
comparison
equality: OK
unsafe
.https://github.com/dyu/ffi-overhead
setjmp
& longjmp