Fix cimpiled ruby library system

This commit is contained in:
2026-07-15 14:44:05 +01:00
parent 1d5acfdc94
commit 12d5815fb6
10 changed files with 40 additions and 31 deletions
+18 -13
View File
@@ -1,17 +1,25 @@
module WM.Core where
import Control.Concurrent (threadDelay)
import Control.Exception (bracket)
import Foreign (Ptr)
import System.Environment (getArgs)
import WM.Mruby
import WM.XKutu
data State = State
{ mruby_pointer :: Ptr MrbState,
running :: Bool
{ mruby_pointer :: !(Ptr MrbState),
running :: !Bool
}
deriving (Show)
newState :: Ptr MrbState -> State
newState ptr =
State
{ mruby_pointer = ptr,
running = True
}
run :: IO ()
run = do
is_started <- deploy
@@ -24,11 +32,10 @@ run = do
print screen_info
pointer_info <- get_pointer
print pointer_info
mrb <- mrb_open
runRuby mrb runtimeRb
handle_args mrb
loop $ State mrb True
mrb_close mrb
bracket mrb_open mrb_close $ \mrb -> do
loadRubyLibs mrb
handle_args mrb
loop (newState mrb)
handle_args :: Ptr MrbState -> IO ()
handle_args mrb = do
@@ -41,12 +48,10 @@ handle_args mrb = do
_ -> putStrLn "unknown arguments"
handle_event :: Maybe Event -> State -> IO State
handle_event Nothing state = pure state
handle_event (Just ev) state
| event_btn ev == 24 = pure state {running = False}
| otherwise = do
print ev
pure state
handle_event Nothing s = pure s
handle_event (Just ev) s
| event_btn ev == 24 = pure s {running = False}
| otherwise = print ev >> pure s
loop :: State -> IO ()
loop state
+13 -10
View File
@@ -1,20 +1,11 @@
{-# 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.C (CChar, CInt (CInt), 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"
@@ -30,3 +21,15 @@ runRuby :: Ptr MrbState -> String -> IO ()
runRuby mrb code =
withCString code $ \cstr ->
c_mrb_load_string mrb cstr
foreign import capi "ruby_compiled.h value mgems"
c_mgems :: Ptr CChar
foreign import capi "ruby_compiled.h value mgems_len"
c_mgems_len :: CInt
foreign import capi "mruby/irep.h mrb_load_irep_buf"
c_mrb_load_irep_buf :: Ptr MrbState -> Ptr CChar -> CInt -> IO ()
loadRubyLibs :: Ptr MrbState -> IO ()
loadRubyLibs mrb = c_mrb_load_irep_buf mrb c_mgems c_mgems_len
-1
View File
@@ -1 +0,0 @@
puts "hello from library"
-1
View File
@@ -1 +0,0 @@
puts "hello from ruby"
+1
View File
@@ -0,0 +1 @@
puts "hello from config"
+1
View File
@@ -35,6 +35,7 @@
gnumake
ruby
xxd
haskellPackages.ghc
haskellPackages.cabal-install
+1 -1
View File
@@ -24,7 +24,7 @@ executable kutu
WM.XKutu
WM.Mruby
build-depends: base, extra, bytestring, file-embed, text
build-depends: base, bytestring
hs-source-dirs: app
+3 -3
View File
@@ -4,7 +4,7 @@ set -e
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
OUTPUT_TMP=$(mktemp)
OUTPUT="$SCRIPT_DIR/../../include/binding/ruby_compiled.h"
OUTPUT="$SCRIPT_DIR/../../app/c/ruby_compiled.h"
if test "$1" = "clean"; then
rm -f "$OUTPUT"
@@ -32,8 +32,8 @@ fi
echo "#ifndef MGEMS_H"
echo "#define MGEMS_H"
xxd -i -n mgems $OUTPUT_TMP \
| sed -e 's/^unsigned char /constexpr unsigned char /' \
-e 's/^unsigned int /constexpr unsigned int /'
| sed -e 's/^unsigned char /unsigned char /' \
-e 's/^unsigned int /unsigned int /'
echo "#endif"
} > "$OUTPUT"
+1
View File
@@ -0,0 +1 @@
puts "hello from lib"