[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: embedding mzscheme into a C++ application



"Brian McAndrews" <mcandrews@larocquetrading.com> writes:

> I want to use scheme as a scripting language extension to our
> application.  I've been using guile, but I am becoming frustrated
> with it (especially on a win32 platform).  I've looked at the Inside
> PLT MzScheme document and it looks promising.  2 questions: 1) is
> this the right tool for the job and 2) where can I find more
> documentation/examples on embedding mzscheme into C based
> applications?

I've been playing around with this same idea, using mzScheme as a
scripting and configuration language for a C++ application.

So far it has worked very nicely.  There is one concern that I have
regarding many embedded script languages, and that is the use of
longjmp.  In a C++ application, if a longjmp jumps over any object
destructor, your program is undefined.

As I understand mzScheme internals (which is extremely minimal) they
use longjmp for exceptions and for continuations.  This must be
handled very carefully in any code that involves C++.  I don't quite
know what to do, but the problem is visible with the following call
chain: 

 1) scheme code ---> 2) c++ code ---> 3) scheme code


Suppose scheme code in section 3 wants to long jmp to a handler in
scheme code (1).  This would bypass the C++ code and render your
program undefined if your C++ code has any temporary or auto (stack
based) objects requiring destruction in scope.

Basically, what needs to be done in every C++ routine that interacts
with scheme (especially if it's reentrant from scheme, as in 1->2
above), it needs to catch any long jumps, clean up all local objects,
and then re-jump to wherever the original long jump was headed.

But I am not sure how to do that cleanly, since mzscheme encapsulates
the setjmp/longjmp routines, and I haven't bothered yet to look at
what they do.

However, I wouldn't deploy any application that doesn't take this into
consideration, because it is a serious problem.  (Many embeddable
languages use longjmp, so this is a somewhat common problem but most
C++ programmers don't consider it, from what I can tell.)

-- 
Chris