pharo-users@lists.pharo.org

Any question about pharo is welcome

View all threads

UFFI callback, getting function pointer

TT
tomazz.turk@gmail.com
Sun, Dec 5, 2021 5:31 PM

Dear all,

I'm constructing a rudimentary webview library for Pharo with UFFI (available here https://github.com/eftomi/Pharo-Webview). For now, the basic things work under Windows. I’m using “a tiny cross-platform webview library“ available at https://github.com/webview/webview.

One of the functions in the dll allows to register a C function to act as a callback and that can be used as a function in javascript, which is convenient for webview presenter - main program communication. This function has the signature:

extern void webview_bind(webview_t w, const char *name,
                              void (*fn)(const char *seq, const char *req,
                                         void *arg),
                              void *arg);

fn is the callback function.

Is there any way to make a ffiCall description of this kind of signature, where fn would be an address of the callback in Pharo?

Thanks and best wishes,
Tomaz

Dear all, I'm constructing a rudimentary webview library for Pharo with UFFI (available here [https://github.com/eftomi/Pharo-Webview](https://github.com/eftomi/Pharo-Webview "https://github.com/eftomi/Pharo-Webview")). For now, the basic things work under Windows. I’m using “a tiny cross-platform webview library“ available at [https://github.com/webview/webview](https://github.com/webview/webview "https://github.com/webview/webview"). One of the functions in the dll allows to register a C function to act as a callback and that can be used as a function in javascript, which is convenient for webview presenter - main program communication. This function has the signature: ``` extern void webview_bind(webview_t w, const char *name, void (*fn)(const char *seq, const char *req, void *arg), void *arg); ``` fn is the callback function. Is there any way to make a ffiCall description of this kind of signature, where fn would be an address of the callback in Pharo? Thanks and best wishes,\ Tomaz
TT
tomazz.turk@gmail.com
Mon, Dec 6, 2021 3:53 PM

Found the solution! Here it is, if somebody needs the description.

The webview_bind() function in DLL that expects to get a pointer to the function in Pharo can be called like this:

WebViewLibrary >> bind: webview name: aString callback: aFFICallback arguments: aByteString
	^ self ffiCall: #(void webview_bind(void * webview, const char * aString, FFICallback aFFICallback, void * aByteString))

Before doing the actual call, the callback should be made according to fn signature:

doIt := FFICallback 
		signature: #( void (const char *seq, const char *req, void * arg) )
		block: [ :seq :req :arg | 
                        “do the stuff”
		].

And the call is then made as:

WebViewLibrary new bind: webview name: 'newJsFunctionName' callback: doIt  arguments: args.

The next challenge is, of course, threading ...

Best wishes,
Tomaz

Found the solution! Here it is, if somebody needs the description. The webview_bind() function in DLL that expects to get a pointer to the function in Pharo can be called like this: ``` WebViewLibrary >> bind: webview name: aString callback: aFFICallback arguments: aByteString ``` ``` ^ self ffiCall: #(void webview_bind(void * webview, const char * aString, FFICallback aFFICallback, void * aByteString)) ``` Before doing the actual call, the callback should be made according to fn signature: ``` doIt := FFICallback ``` ``` signature: #( void (const char *seq, const char *req, void * arg) ) ``` ``` block: [ :seq :req :arg | ``` ``` “do the stuff” ``` ``` ]. ``` And the call is then made as: ``` WebViewLibrary new bind: webview name: 'newJsFunctionName' callback: doIt arguments: args. ``` The next challenge is, of course, threading ... Best wishes,\ Tomaz
HV
Harald VICAIRE
Tue, Dec 7, 2021 12:33 AM

Thanks for sharing the solution mate :)
I don't know if you are aware, but you can also ask questions on Pharo's
discord. Though it's easier to go through the history of conversations by
email.
Cheers,

Harald

Le mar. 7 déc. 2021 à 04:53, tomazz.turk@gmail.com a écrit :

Found the solution! Here it is, if somebody needs the description.

The webview_bind() function in DLL that expects to get a pointer to the
function in Pharo can be called like this:

WebViewLibrary >> bind: webview name: aString callback: aFFICallback arguments: aByteString

^ self ffiCall: #(void webview_bind(void * webview, const char * aString, FFICallback aFFICallback, void * aByteString))

Before doing the actual call, the callback should be made according to fn
signature:

doIt := FFICallback

	signature: #( void (const char *seq, const char *req, void * arg) )

	block: [ :seq :req :arg |

                     “do the stuff”

	].

And the call is then made as:

WebViewLibrary new bind: webview name: 'newJsFunctionName' callback: doIt  arguments: args.

The next challenge is, of course, threading ...

Best wishes,
Tomaz

Thanks for sharing the solution mate :) I don't know if you are aware, but you can also ask questions on Pharo's discord. Though it's easier to go through the history of conversations by email. Cheers, Harald Le mar. 7 déc. 2021 à 04:53, <tomazz.turk@gmail.com> a écrit : > Found the solution! Here it is, if somebody needs the description. > > The webview_bind() function in DLL that expects to get a pointer to the > function in Pharo can be called like this: > > WebViewLibrary >> bind: webview name: aString callback: aFFICallback arguments: aByteString > > ^ self ffiCall: #(void webview_bind(void * webview, const char * aString, FFICallback aFFICallback, void * aByteString)) > > Before doing the actual call, the callback should be made according to fn > signature: > > doIt := FFICallback > > signature: #( void (const char *seq, const char *req, void * arg) ) > > block: [ :seq :req :arg | > > “do the stuff” > > ]. > > And the call is then made as: > > WebViewLibrary new bind: webview name: 'newJsFunctionName' callback: doIt arguments: args. > > The next challenge is, of course, threading ... > > Best wishes, > Tomaz >