This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
Function Definitions in Static Modules
- From: David O'Callaghan <david dot ocallaghan at cs dot tcd dot ie>
- To: Kawa List <kawa at sources dot redhat dot com>
- Date: Thu, 02 Mar 2006 16:06:47 +0000
- Subject: Function Definitions in Static Modules
Hi,
I'm having some trouble using static modules from Java. I have a set of
test cases, attached.
Here's the Kawa Scheme code for the module:
(define (f x) (+ 1 x))
(define (make-adder i) (lambda (x) (+ i x)))
(define g (make-adder 1))
(define (h x) (g x))
I can call all the functions if the module is imported into a scheme
program with require [test-def-req.scm in attachment]. However, if I try
to invoke the static methods g or h from Java or from Scheme problems
arise. Firstly, g is not exported as a method; I can guess why. Second,
invoking h gives a null pointer exception when it tries to invoke g; I
find this harder to accept. For example, in [TestDefMainBroken.java]:
System.out.println("(h 41) -> " + TestDefModule.h(41));
will lead to a NullPointerException.
Similarly, with this Kawa code [test-def-stat-broken.scm]:
(display (format "(invoke-static <TestDefModule> 'h 41) -> ~A\n"
(invoke-static <TestDefModule> 'h 41)))
I get a NullPointerException:
java.lang.NullPointerException
at TestDefModule.h(test-def.scm:13)
at TestDefStatBroken.run(test-def-stat-broken.scm:8)
at gnu.expr.ModuleBody.run(ModuleBody.java:44)
at gnu.expr.ModuleBody.run(ModuleBody.java:32)
at gnu.expr.ModuleBody.runAsMain(ModuleBody.java:132)
at TestDefStatBroken.main(test-def-stat-broken.scm)
I found that I could fix the scheme case by adding (require
<TestDefModule>) [see test-def-stat.scm]. I wasn't sure what the
equivalent was in Java, but I eventually found that this fixes the
probem [TestDefMain.java]:
TestDefModule td = new TestDefModule();
td.run();
I then tried to port the fix from my test case back to my real code and
ran into a related problem: I have a static module that is used by a
class created with define-simple-class, which in turn is used by a Java
class. I found it necessary to add a non-default constructor to my
define-simple-class in order to initialise the module. For the test case
[see test-def-class.scm] I need:
((*init*) (invoke (make <TestDefModule>) 'run))
My question is this: is it always necessary to invoke the run method on
a static module for functions like h to work, or am I doing something wrong?
Thanks,
David
Attachment:
test-def.tar.gz
Description: GNU Zip compressed data