Re: GForth callbacks

Rene Hartmann <[email protected]>
Newsgroups gmane.comp.lang.forth.gforth
Message-ID <[email protected]>
I now boiled it down to a small example.

File call_binop.h:

#ifndef _CALL_BINOP_H_
#define _CALL_BINOP_H_

typedef int (*binop_callback)(int, int);

int call_binop(int a, int b, binop_callback callback);

#endif


File call_binop.c:

#include "call_binop.h"

int call_binop(int a, int b, binop_callback callback)
{
     return (*callback)(a, b);
}


I compile call_binop.c to a shared library libcall_binop.so and copy 
that to a standard lib directory (there seems to be no way to provide an 
-L option and setting LD_LIBRARY_PATH doesn't seem to work either!?)

Then I execute:

c-library binop-call
s" call_binop" add-lib
\c #include "call_binop.h"
c-function call_binop call_binop n n func -- n
c-callback binop n n -- n
end-c-library

: add + ;

' add binop CONSTANT cb_add
2 3 cb_add call_binop


giving:

*the terminal*:1: error: Invalid memory address
2 3 cb_add *call_binop*

If I add .s to the definition of add I see <-35568003> in the output.

So the callback has been executed, but something went wrong and I don't 
know how to avoid it.

--
René Hartmann


Am 03.09.2017 um 12:00 schrieb Rene Hartmann:
> I'm trying to use the C callbacks available with GForth 0.7.9 but 
> somehow it doesn't seem to work.
> 
> I have a callback that takes two arguments and one return value.
> 
> I define it like this:
> 
> 
> c-callback cs-renderer n a -- a
> 
> ' render>buf cs-renderer CONSTANT c_render_to_buf
> 
> 
> When passing c_render_to_buf to the function which requires the callback 
> the callback gets invoked but when it returns things go wrong.
> 
> If I place a .s in the callback it prints <-34288843> so it seems the 
> stack isn't set up properly. But I don't know what to do different to 
> make it work.
> 
> I've yet to create a complete example I can post.
> 
> -- 
> René Hartmann
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.