I have now written a [blog post about
this](http://www.bootstrap.me.uk/bootstrapped-blog/nim-for-the-discerning-rubyist)
Feel free to ignore the first, introductory, part
Great!
If you have some time to spare, it would be nice if you document or write your
process to blog, it would be helpful for everyone
ok, I finally resolved this using tuples in the following way:
type
MyData = tuple[x, y, z: int, s: cstring]
proc new_list(data: ptr MyData): ptr MyData {.cdecl, exportc} =
var data = cast[ptr MyData](alloc0(sizeof(MyData)))
data.x = 111
data.y = 222
RedFred, note that
proc test_arr(): seq[int] {.cdecl, exportc} =
...
return list
may work better, as a seq is already a pointer. You don't want a pointer to
that pointer. Of course the seq is more than a plain array internally, there is
at least the size member
var p_arr = alloc(size_of(array[int, 3]))
Above is incorrect syntax
Do
var p_arr = alloc sizeof(array[3, int])
Also [pointer arithmetic post](https://forum.nim-lang.org/t/1188#7366) should
be helpful
When you `alloc` something, it'll be put in heap, and
Hi @mashingan, @Stefan_Salewski
I find it impossible to allocate memory for array, as when I try to do
something like:
var p_arr = alloc(size_of(array[int, 3]))
I get a 'type expected' error.
Besides, if array is created on the stack then passing a pointer to it is
> I was not aware that Nim can create cstrings for FFI access out of the box.
I think you missed reading `cstring` definition in manual. It's stated there,
especially using C backend.
> So now, using cstring instead of string means the test_string function works
> smoothly!
Can you show the example? I was not aware that Nim can create cstrings for FFI
access out of the box.
> but still keep getting random numbers.
Well, your example can not work. Returning the addr should
@Stefan_Salewski, @mashingan: thank you both, your suggestions were great. I
was under the misapprehension that the Nim primitives were C-like. So now,
using _cstring_ instead of _string_ means the test_string function works
smoothly! For the array function I am now returning a ptr, which I can
Try `cstring` instead of `string`. For array, I think you should return `ptr`.
> I'm trying to create a Ruby extension in Nim,
Interesting, maybe you can collect your insights in a blog or forum post.
I have used Ruby too in the past, and I wrote some C extensions for using CGAL
and Boost library. While I was told that extending Ruby with C was easier than
extending
Hi all,
I'm trying to create a Ruby extension in Nim, so I've built a Nim dynamic
library (.so) that exports a number of functions, which I'm then accessing
using Ruby's [FFI](https://github.com/ffi/ffi) module. What I've found, is that
I can easily use functions that return ints or floats,
12 matches
Mail list logo