ocaml-ctypes icon indicating copy to clipboard operation
ocaml-ctypes copied to clipboard

Needs a way to do lookup in cstubs using dlsym/eglGetProcAddress

Open whitequark opened this issue 10 years ago • 4 comments

See https://github.com/dbuenzli/tgls/pull/14 for details. I was going to implement this but the C representation used by cstubs is horrendously opaque and I am not motivated enough to figure it out.

whitequark avatar Mar 06 '15 01:03 whitequark

@whitequark if you have a clear idea of what should be done API-wise maybe you should expand a bit on that/explain the problem and try to make a more precise proposal (personally I'm completly ignorant of stub generation and don't have much time to work on that at the moment). For reference this is what Jeremy wrote about the problem at a certain point in time.

dbuenzli avatar Mar 07 '15 01:03 dbuenzli

The idea is very simple. Currently the stubs look like this:

value stub(value foo) {
  long c_foo = Long_val(foo);
  c_call(c_foo);
  return Val_unit;
}

With this proposal, they would look like:

value stub(value foo) {
  static void(*c_call)(long);
  if(!c_call) {
    c_call = [GetProcAddress];
    if(!c_call) caml_raise(*caml_named_value("Cstubs.Dynamic_stub_not_found"), caml_alloc_string("c_call"));
  }
  c_call(c_foo);
  return Val_unit;
}

The [GetProcAddress] part could be:

  • GetProcAddress(GetModuleHandle("opengl32.dll"), "c_call"),
  • wglGetProcAddress("c_call"),
  • wglGetProcAddress("c_call") || GetProcAddress(GetModuleHandle("opengl32.dll"), "c_call") (I wish I was kidding, but this horrifying construct can be necessary: https://www.opengl.org/wiki/Load_OpenGL_Functions),
  • dlsym(NULL, "c_call") (thankfully, POSIX doesn't require specifying module handles explicitly. might be worth reimplementing this for Windows like this: https://stackoverflow.com/questions/23437007/getprocaddress-with-all-loaded-libraries),
  • eglGetProcAddress("c_call"),
  • possibly some other terrible combination I have not yet encountered.

This would add just one argument to Cstubs.FOREIGN.foreign, like ?reference:[ direct |dlsym | `custom of string ].

This would be trivial if not for the many layers of abstraction between cstubs' interface and the generated C code.

whitequark avatar Mar 07 '15 01:03 whitequark

@yallop Any opinion on this?

whitequark avatar Mar 25 '16 13:03 whitequark

I'd like to support this kind of thing, but ideally via a general mechanism of customising the generated code (#211). In the short/medium term one possibility is to generate code that makes calls via a macro CALL, like this

value stub(value foo) {
  long c_foo = Long_val(foo);
  CALL(c_call, c_foo);
  return Val_unit;
}

and allow you to redefine CALL to insert the GetProcAddress code.

yallop avatar Mar 29 '16 10:03 yallop