Skip to content

Struct by value support for FFI #3835

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

Open
ankane opened this issue Apr 21, 2025 · 2 comments
Open

Struct by value support for FFI #3835

ankane opened this issue Apr 21, 2025 · 2 comments

Comments

@ankane
Copy link

ankane commented Apr 21, 2025

Hi, while it isn't super common, it'd be nice to support passing and retrieving structs by value with FFI.

require "ffi"

module Hello
  extend FFI::Library
  ffi_lib "./libhello.dylib"

  class World < ::FFI::Struct
    layout :a, :int, :b, :int
  end

  attach_function :get_struct, [], World.by_value
  attach_function :print_struct, [World.by_value], :void
end

Hello.print_struct(Hello.get_struct)

Code for shared library

// gcc -shared -o libhello.dylib hello.c

#include <stdio.h>

struct World {
    int a;
    int b;
};

struct World get_struct(void) {
    struct World w = { .a = 1, .b = 2 };
    return w;
}

void print_struct(struct World w) {
    printf("a = %d, b = %d\n", w.a, w.b);
}

CRuby outputs:

a = 1, b = 2

TruffleRuby 24.2.0 currently errors with:

unknown simple type 'STRUCT_BY_VALUE' (Polyglot::ForeignException)
@andrykonchin
Copy link
Member

Thank you for the report, we'll look into it.

@eregon
Copy link
Member

eregon commented May 5, 2025

This is blocked by struct-by-value support in Truffle NFI (internal issue: GR-13304).
We'll have to wait until that is fixed.

Same as #2069, I'll close the older one as this issue is much clearer.

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

No branches or pull requests

3 participants