33 lines
855 B
Haskell
33 lines
855 B
Haskell
{-# LANGUAGE CApiFFI #-}
|
|
{-# LANGUAGE ForeignFunctionInterface #-}
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
module WM.Mruby where
|
|
|
|
import Data.FileEmbed (embedFile)
|
|
import Data.Text qualified as T
|
|
import Data.Text.Encoding qualified as TE
|
|
import Data.Text.Encoding.Error qualified as TEE
|
|
import Foreign.C (CString, withCString)
|
|
import Foreign.Ptr
|
|
|
|
runtimeRb :: String
|
|
runtimeRb =
|
|
T.unpack (TE.decodeUtf8With TEE.lenientDecode $(embedFile "./app/ruby/lib.rb"))
|
|
|
|
data MrbState
|
|
|
|
foreign import capi "mruby.h mrb_open"
|
|
mrb_open :: IO (Ptr MrbState)
|
|
|
|
foreign import capi "mruby.h mrb_close"
|
|
mrb_close :: Ptr MrbState -> IO ()
|
|
|
|
foreign import capi "mruby/compile.h mrb_load_string"
|
|
c_mrb_load_string :: Ptr MrbState -> CString -> IO ()
|
|
|
|
runRuby :: Ptr MrbState -> String -> IO ()
|
|
runRuby mrb code =
|
|
withCString code $ \cstr ->
|
|
c_mrb_load_string mrb cstr
|