On 5-Oct-09, at 7:20 AM, Michele Zaffalon wrote:
Hi,
I have found the following remark by Marc in one of the posts,
On some platforms (processor and operating system dependent) that can execute dynamically generated machine code (32 bit intel, power-pc, sparc), you can pass any procedure including closures. This feature is enabled if USE_dynamic_code_gen is defined in lib/os_dyn.h and this is the default in recent versions of Gambit. Give it a try... it may work to pass a closure on your platform... but don't count on it to be portable!
Could somebody explain how it works?
It used to work! I noticed a stupid bug in lib/os_dyn.c . You have to replace all the ___WORD_SIZE by ___WORD_WIDTH and then recompile the system with "make".
Then, on supported platforms such as 32 bit intel, you'll be able to run code like this which passes a closure to a foreign function:
(c-declare "
int f( int g( int x ) ) { return g(1) + g(2); }
")
(define f (c-lambda ((function (int) int)) int "f"))
(c-define (square n) (int) int "square" "" (* n n))
(pp (square 10)) ;; prints 100
(pp (f square)) ;; prints 5 (i.e. 1^2 + 2^2)
(pp (f (lambda (x) (expt 2 x)))) ;; prints 6 (i.e. 2^1 + 2^2)
It is the last expression, i.e. the call (f (lambda (x) (expt 2 x))), which is enabled by the dynamic code generation. The call (f square) is always supported because it does not require dynamic code generation (because square is defined with a c-define form).
Marc