GithubHelp home page GithubHelp logo

Call a function pointer about jnr-ffi HOT 9 OPEN

pron avatar pron commented on July 19, 2024
Call a function pointer

from jnr-ffi.

Comments (9)

pron avatar pron commented on July 19, 2024 1

Do you think this is a feature we should lift up to jnr-ffi?

Oh, absolutely! This is the scenario I'm facing: I'm trying to implement an SPI for a filesystem in Java. The Java method receives a C callback -- a visitor -- which it should then invoke for each file in a directory.

AFAIK, JNA has this feature, too.

from jnr-ffi.

wks avatar wks commented on July 19, 2024 1

+1. I am developing a virtual machine which exposes a JNI-like interface, where the C native program receives a struct (like JNIEnv) and each API function is a function pointer in that struct. If a Java programmer wants to control a native component that exposes such an interface (imagine controlling another JVM from one JVM), it will be very common to call many functions via their function pointers.

The desired JNR interface should be similar to Closure, albeit it is Java calling C rather than C calling back to Java. The callee is a stateless C function rather than an object-bound Java method, so it should be easier to implement.

from jnr-ffi.

headius avatar headius commented on July 19, 2024

Directly? Not really. But I think you have at least two options:

  • Bind syscall directly. I have no idea the correct way to call it from there because I've never used it.
  • Use jffi to invoke the function. jffi contains the low-level native bits that jnr-ffi uses for function calls. See Foreign, which has "invoke" endpoints, and look at code that uses them.

Do you think this is a feature we should lift up to jnr-ffi?

from jnr-ffi.

SerCeMan avatar SerCeMan commented on July 19, 2024

+1, very important feature. Also, function pointer and structures as closure parameters would be very useful.

from jnr-ffi.

headius avatar headius commented on July 19, 2024

I don't think we've made any progress on this, but it's probably still useful. Anyone want to bring this issue back to life and try to come up with a good API for this?

from jnr-ffi.

gembin avatar gembin commented on July 19, 2024

I think it's useful, How to implement this kind of callback?
What' the equivalent of com.sun.jna.Callback ?

https://curl.haxx.se/libcurl/c/curl_easy_setopt.html

CURLOPT_HEADERFUNCTION

Callback for writing received headers. See CURLOPT_HEADERFUNCTION
 setopt(CurlOption.CURLOPT_HEADERFUNCTION, headerHandler);
 setopt(CurlOption.CURLOPT_WRITEFUNCTION, contentHandler);

from jnr-ffi.

headius avatar headius commented on July 19, 2024

It seems unlikely that you can't already do this in jnr-ffi via some combination of APIs. Are we simply missing a nice endpoint that says "give me a callable wrapper around this C function pointer?

from jnr-ffi.

pron avatar pron commented on July 19, 2024

I think someone has made such an API here.

from jnr-ffi.

atoulme avatar atoulme commented on July 19, 2024

I'm interested by this functionality as well. Is there a better way to accomplish this now? If not, I'll look at Foreign and jffi as @headius mentioned.

from jnr-ffi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.