Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Haskell & MRuby #5586

Open
synth-me opened this issue Nov 26, 2021 · 4 comments
Open

Haskell & MRuby #5586

synth-me opened this issue Nov 26, 2021 · 4 comments

Comments

@synth-me
Copy link

How May i embed mruby in Haskell ? I can imagine using some FFI but hasn't been working as i tried the traditional mathods for it. Somebody have any ideia about how can I do it ?

@dearblue
Copy link
Contributor

dearblue commented Jan 1, 2022

I was able to run Ruby code from Haskell through mruby.
This is my first time programming Haskell, so I'm probably making a lot of mistakes.
The compiler I used this time was ghc.

(1) Get the mruby source code and build libmruby.a

% git clone https://github.com/mruby/mruby.git
% cd mruby
% rake

Try bin/mruby-config on the same directory if checking the linker flag.

% bin/mruby-config --ldflags
-L/var/tmp/mruby/build/host/lib
% bin/mruby-config --libs
-lmruby -lm

(2) Edit Haskell code (file name is mruby-ffi.hs)

{-# LANGUAGE ForeignFunctionInterface #-}

module Main where

import Foreign.C.String
import Foreign.C.Types
--import Foreign.Marshal.Alloc -- for free
import Foreign.Ptr

data MRubyState
newtype MRubyValue = Storable CUIntPtr

-- from mruby.h
foreign import ccall "mrb_open" mrb_open :: IO (Ptr MRubyState)
foreign import ccall "mrb_close" mrb_close :: Ptr MRubyState -> IO ()
foreign import ccall "mrb_p" mrb_p :: Ptr MRubyState -> MRubyValue -> IO ()

-- from mruby/compile.h
foreign import ccall "mrb_load_string" mrb_load_string :: Ptr MRubyState -> CString -> IO (MRubyValue)

main :: IO ()
main = do
  let rubycode = "puts \"Hello world!!!\"; \"from #{MRUBY_DESCRIPTION}\""
  mrb <- mrb_open
  --cstr <- newCString rubycode
  --val <- mrb_load_string mrb cstr
  --free cstr
  val <- withCString rubycode (\ cstr -> mrb_load_string mrb cstr)
  mrb_p mrb val
  mrb_close mrb

{-
 - References:
 - - (ja) https://yunomu.hatenablog.jp/entry/2012/07/31/005513
 - - (ja) https://xtech.nikkei.com/it/article/COLUMN/20080805/312151/
 - - (ja) https://qiita.com/arowM/items/9ebfb7cafecd99290663
 - - (en) https://wiki.haskell.org/Foreign_Function_Interface
 - - (en) https://hackage.haskell.org/package/base-4.16.0.0/docs/Foreign-C-String.html
 -}

(3) Compiling and running Haskell code

% ghc `bin/mruby-config --ldflags --libs` mruby-ffi.hs
% ./mruby-ffi
Hello world!!!
"from mruby 3.0.0 (2021-03-05)"

@synth-me
Copy link
Author

synth-me commented Jan 2, 2022

I'll have a look at it and then i'll get you a review of that code. Although , look like that this solution is very close to what i've been trying but more detailed. I hope it works !

@synth-me
Copy link
Author

synth-me commented Mar 4, 2022

Just to say , this solution worked but only on linux distributions i tried windows 10 and it didn't work although it's great to me

@rubyFeedback
Copy link

It may be useful to state why it did not work. Perhaps matz can get some ideas what to improve in mruby to make it work better on win10/11. I use linux myself but an elderly relative depends on win10 so ruby and mruby working on windows is useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants