ffi

How one deals with typedefs in Squeak FFI

拟墨画扇 提交于 2019-12-13 02:04:38
问题 I want to interface with a library (HDF5) which uses exclusively its own typedefs in both function prototypes and structure definitions. typedef struct { H5L_type_t type; /* Type of link */ hbool_t corder_valid; /* Indicate if creation order is valid */ int64_t corder; /* Creation order */ H5T_cset_t cset; /* Character set of link name */ union { haddr_t address; /* Address hard link points to */ size_t val_size; /* Size of a soft link or UD link value */ } u; } H5L_info_t; In Visualworks

Building a haskell interpreter (hint) as dynamic library, useable from C++: Missing Interpreter.dyn_hi

筅森魡賤 提交于 2019-12-12 16:17:13
问题 I want to create a haskell interpreter that I can use from C++ on linux. I have a file FFIInterpreter.hs which implements the interpreter in haskell and exports the functions via FFI to C++. module FFIInterpreter where import Language.Haskell.Interpreter import Data.IORef import Foreign.StablePtr import Foreign.C.Types import Foreign.C.String import Control.Monad import Foreign.Marshal.Alloc type Session = Interpreter () type Context = StablePtr (IORef Session) foreign export ccall

Convert Option<&mut T> to *mut T

允我心安 提交于 2019-12-12 13:41:44
问题 I'm writing a Rust wrapper around a C library and while doing so I'm trying to take advantage of the "nullable pointer optimization" mentioned in The Book, but I can't find a good way to convert Option<&T> to *const T and Option<&mut T> to *mut T like what they're describing. What I really want is to be able to call Some(&foo) as *const _ . Unfortunately that doesn't work, so the next best thing I can think of is a trait on Option<T> that enables me to call Some(&foo).as_ptr() . The following

Make GHCi load and interpret a module with a “foreign export” declaration (for FFI with C)?

流过昼夜 提交于 2019-12-12 13:09:01
问题 I have a module ( Safe.hs ) with foreign export ccall respond_hs :: CWString -> IO CWString for FFI with C. I'd like to load Safe.hs in GHCi and evaluate some things with it. But ghci fails to load it (I'm specifying two source files because it depends on valencies.lhs ): $ ghci src/valencies.lhs src/Safe.hs GHCi, version 7.6.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ...

Is it possible to use a C++ library from Rust when the library uses templates (generics)?

孤街浪徒 提交于 2019-12-12 11:58:00
问题 Is it possible to use a C++ library from Rust when the library (e.g. Boost) uses templates (generics)? 回答1: Yes , but it may not be practical . The D programming language is one of the very few providing some degree of C++ interoperability; you can read more about it on dlang. Note the limitation for the template section: Note that all instantiations used in D code must be provided by linking to C++ object code or shared libraries containing the instantiations. which essentially means that

Using `foreign import prim` with a C function using STG calling convention

此生再无相见时 提交于 2019-12-12 11:27:44
问题 I have a simple C routine that takes four words and returns four words, and for which gcc can optimize and emit some primops that GHC doesn't support. I'm trying to benchmark various ways of calling this procedure, and am having trouble trying to adapt the technique described here to use foreign import prim . The following is meant to just add 1 to each input word, but segfaults. Main.hs: {-# LANGUAGE GHCForeignImportPrim #-} {-# LANGUAGE ForeignFunctionInterface #-} {-# LANGUAGE MagicHash #-

Are peekCString and peekCStringLen lazy?

三世轮回 提交于 2019-12-12 11:23:28
问题 I have a C function that creates a null terminated string and returns a pointer to it, there is also corresponding deallocation function. foreign import ccall unsafe "get_str" getStr :: IO CString foreign import ccall unsafe "free_str" freeStr :: CString -> IO () I want to create a Haskell String from the returned CString, and free CString as soon as possible. do cStr <- getStr str <- peekCString cStr freeStr cStr -- here str is used Is it safe to free cStr before str is used? In other words,

Why is there no 'foreign import prim unsafe'?

风流意气都作罢 提交于 2019-12-12 10:36:27
问题 This is a followup to my earlier question here. I've been able to get something working per Reid Barton's answer, but I notice in the core I see __pkg_ccall_GC : case {__pkg_ccall_GC hashabler-2.0.0 sipRound_s_x2 Word# -> Word# -> Word# -> Word# -> (# Word#, Word#, Word#, Word# #)} ww1 ww2 ww3 (xor# ww4 b1) Which is I think what you'd expect for a "safe" ffi call. Yet adding "unsafe" to the foreign import string is not allowed (though the error messages doesn't say why): src/Data/Hashabler

How to store a void* reference to a struct in Rust?

余生颓废 提交于 2019-12-12 09:51:19
问题 I'm interacting with some C callbacks that use the standard void* userdata method to allow you to store a reference to some context (e.g. a struct). How can I store a reference to a Rust struct in a void* and still allow it to be moved around? It seems that Rust moves really are moves, i.e. this code fails (as expected): struct Thing { pointer_to_self: *mut Thing, } fn create_thing() -> Thing { let mut a = Thing { pointer_to_self: std::ptr::null_mut(), }; a.pointer_to_self = &mut a as *mut _;

How to Use Haskell's Stack Build Tool to Export a Library to Be Consumed by C/C++?

夙愿已清 提交于 2019-12-12 08:47:37
问题 Suppose one is using the stack build tool to make a Haskell library (importing packages from Hackage, and so forth) to be used with a C/C++ project in which main is located in C/C++ . Supposing your project is named Lib.hs (which uses external libraries from hackage), is there a way to use stack to export your Lib.o , Lib.hi , and Lib_stub.h to be consumed by a C/C++ compiler like gcc or g++ ? EDIT: A related question might be: "how can one use Stack as a build tool to be used with a Haskell