ffi

Haskell FFI - can you obtain a C Pointer from a Haskell data structure?

99封情书 提交于 2019-12-03 10:04:00
I have quite a few C structs structured like typedef struct { unsigned int a; unsigned int b; } StructA; And a lot of functions like void doSomethingWith(StructA*,StructB*,StructC*); Is there an easy way to call these functions with Haskell FFI? Like, is there something that behaves like the & operator in C? (I imagine there isn't, but if there was I'd like to know). Do I have to make the Haskell side data 's instance of storeable (I don't have any constructor functions for these structs). Also: If I have to pass a struct instead of a struct pointer (not a hypothetical question, I have a few

How to link custom object file with Haskell library?

孤街浪徒 提交于 2019-12-03 09:33:00
I've created a Haskell package that makes FFI calls to functions defined in CUDA code. I'd like to compile .cu file to an object (.o) file during package build and force linker to link it in. So far, I tried to use a technique found this question . I've customized buildHook to: run nvcc run default buildHook create ar library file with nvcc compiled code. Setup.hs is available here . This solution has a major disadvantage in restricting this package to static linking. Although cabal produces a shared library, it won't work because it has no way of resolving symbols located in the object file.

Using higher-order Haskell types in C#

旧巷老猫 提交于 2019-12-03 09:30:45
How can I use and call Haskell functions with higher-order type signatures from C# (DLLImport), like... double :: (Int -> Int) -> Int -> Int -- higher order function typeClassFunc :: ... -> Maybe Int -- type classes data MyData = Foo | Bar -- user data type dataFunc :: ... -> MyData What are the corresponding type signature in C#? [DllImport ("libHSDLLTest")] private static extern ??? foo( ??? ); Additionally (because it may be easier): How can I use "unknown" Haskell types within C#, so I can at least pass them around, without C# knowing any specific type? The most important functionality I

How to expose a Rust `Vec<T>` to FFI?

爱⌒轻易说出口 提交于 2019-12-03 06:13:39
I'm trying to construct a pair of elements: array: *mut T array_len: usize array is intended to own the data However, Box::into_raw will return *mut [T] . I cannot find any info on converting raw pointers to slices. What is its layout in memory? How do I use it from C? Should I convert to *mut T ? If so, how? sellibitze If you just want some C function to mutably borrow the Vec , you can do it like this: extern "C" { fn some_c_function(ptr: *mut i32, len: ffi::size_t); } fn safe_wrapper(a: &mut [i32]) { unsafe { some_c_function(a.as_mut_ptr(), a.len() as ffi::size_t); } } Of course, the C

Is it possible to invoke bash or shell scripts from a haskell program?

牧云@^-^@ 提交于 2019-12-03 04:54:06
I'm writing some shell scripts with haskell, which I'm running in gitbash, but there are a few other existing scripts I'd like to be able to use from those scripts. For example, I'd like to run maven goals or do a git pull, but without having to integrate specifically with those tools. Is there a way to do this? ДМИТРИЙ МАЛИКОВ You can use System.Process . For example, executing seq 1 10 shell command: > import System.Process > readProcess "seq" ["1", "10"] "" "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" it :: String > readProcessWithExitCode "seq" ["1", "10"] "" (ExitSuccess,"1\n2\n3\n4\n5\n6\n7\n8\n9

Is it possible to use Fiddle to pass or return a struct to native code?

帅比萌擦擦* 提交于 2019-12-03 04:45:47
I would like to use Fiddle to access a native library compiled from Rust code. The C representation of the struct is very simple, it is just a pointer and a length: typedef struct { char *data; size_t len; } my_thing_t; // Example function that somehow accepts a struct void accepts_a_struct(my_thing_t thing); // Example function that somehow returns a struct my_thing_t returns_a_struct(void); However, all examples I can find accept or return pointers to structs, and not the structs themselves. I'd like to avoid the double indirection if at all possible. I've borrowed an example from the Fiddle

Interchange structured data between Haskell and C

两盒软妹~` 提交于 2019-12-03 04:18:04
问题 First, I'm a Haskell beginner. I'm planning integrating Haskell into C for realtime game. Haskell does logic, C does rendering. To do this, I have to pass huge complexly structured data (game state) from/to each other for each tick (at least 30 times per second). So the passing data should be lightweight. This state data may laid on sequential space on memory. Both of Haskell and C parts should access every area of the states freely. In best case, the cost of passing data can be copying a

Understand foreign function interface (FFI) and language binding

◇◆丶佛笑我妖孽 提交于 2019-12-03 01:22:04
问题 Mixing different programming languages has long been something I don't quite understand. According to this Wikipedia article, a foreign function interface (or FFI) can be done in several ways: Requiring that guest-language functions which are to be host-language callable be specified or implemented in a particular way; often using a compatibility library of some sort. Use of a tool to automatically "wrap" guest-language functions with appropriate glue code, which performs any necessary

How do I call C from Go using the “foreign function interface”

南笙酒味 提交于 2019-12-02 20:20:59
How do I use Go's "foreign function interface" to call out to a C function? This interface is mentioned on the FAQ , but I cannot see it mentioned elsewhere in the docs. Paul Wicks Check out this file from the Go repository. It shows how to wrap a C library in Go and has quite a few comments that explain the process. Here is an article that demonstrates the following with Cgo : To lead with an example, here's a Go package that provides two functions - Random and Seed - that wrap C's rand and srand functions. 来源: https://stackoverflow.com/questions/2740315/how-do-i-call-c-from-go-using-the

Understand foreign function interface (FFI) and language binding

倖福魔咒の 提交于 2019-12-02 16:38:48
Mixing different programming languages has long been something I don't quite understand. According to this Wikipedia article , a foreign function interface (or FFI) can be done in several ways: Requiring that guest-language functions which are to be host-language callable be specified or implemented in a particular way; often using a compatibility library of some sort. Use of a tool to automatically "wrap" guest-language functions with appropriate glue code, which performs any necessary translation. Use of wrapper libraries Restricting the set of host language capabilities which can be used