common-lisp cffi evaluation order when used with progn

83 views Asked by At

I have created a simple shared library in C with just one function :

void sayHello () {
    printf ("Hello World\n");
}

Then, after compiling, I loaded that library into a lisp program using cffi :

(cffi:define-foreign-library libtest1
    (t "./lib/libtest1.so"))

(cffi:use-foreign-library libtest1)

Then I defined 'sayHello' using cffi:defcfun :

(cffi:defcfun "sayHello" :void)

Everything is fine, if I call sayHello from lisp, it works as intended :

? (sayHello)
Hello World
NIL
? 

(Note that '?' is the REPL prompt of Clozure CL)

Now my actual question, watch this :

? (progn (print 'hello) (print 'world))

HELLO 
WORLD 
WORLD
? (progn (sayHello) (print 'world))
Hello World

WORLD 
WORLD
? (progn (print 'hello) (sayHello))
Hello World

HELLO 
NIL
? 

I ran 3 statements at REPL. Look at the output from the last one, "Hello World" is printed BEFORE "HELLO" was printed, which is not how (progn ...) is supposed to work. Its working correctly for first 2 statements.

I cannot understand this strange behaviour. I tested it with both SBCL and Clozure (on Ubuntu 14.04 x64), both give same results.

1

There are 1 answers

1
jlahd On BEST ANSWER

Your Lisp implementation is buffering output and only dumping it on the terminal before displaying the next REPL prompt. You need to explicitly flush the output buffer if you need to.

? (progn (print 'hello) (finish-output) (sayHello))

HELLO Hello World

NIL