Back: Object representation
Up: C and Smalltalk
Forward: Incubator
 
Top: GNU Smalltalk User's Guide
Contents: Table of Contents
Index: Class index
About: About this document

4.8 Using the Smalltalk environment as an extension library

If you are reading this chapter because you are going to write extensions to GNU Smalltalk, this section won't probably interest you. But if you intend to use GNU Smalltalk as a scripting language or an extension language for your future marvellous software projects, you might be interest.

How to initialize GNU Smalltalk is most briefly and easily explained by looking at GNU Smalltalk's own source code. For this reason, here are two snippets from `main.c' and `libgst/cint.c'.

 
/* From main.c */
int main(argc, argv)
int     argc;
char    **argv;
{
  smalltalkArgs(argc, argv);
  initSmalltalk();
  topLevelLoop();

  exit(0);
}

/* From cint.c */
void initCFuncs()
{
  /* Access to command line args */
  defineCFunc("getArgc", getArgc);
  defineCFunc("getArgv", getArgv);

  /* Test functions */
  defineCFunc("testCallin", testCallin);
  defineCFunc("testCString", testCString);
  defineCFunc("testCStringArray", testCStringArray);

  /* ... */

  /* Initialize any user C function definitions. initUserCFuncs,
     defined in cfuncs.c, is overridden by explicit definition
     before linking with the Smalltalk library. */
  initUserCFuncs();
}

Your initialization code will be almost the same as that in GNU Smalltalk's main(), with the exception of the call to topLevelLoop. All you'll have to do is to pass some arguments to the GNU Smalltalk library via smalltalkArgs, and then call initSmalltalk.

Note that initSmalltalk will likely take some time (from a second to 30-40 seconds), because it has to check if the image file must be be rebuilt and, if so, it reloads and recompiles the 34000 lines of Smalltalk code in a basic image. To avoid this check, pass a `-I' flag:

 
char myArgv[][] = { "-I", "myprog.im", nil };
int myArgc;
/* ... */
myArgc = sizeof(myArgv) / sizeof (char *) - 1;
smalltalkArgs(myArgc, myArgv);

If you're using GNU Smalltalk as an extension library, you might also want to disable the two ObjectMemory class methods, quit and quit: method. I advice you not to change the Smalltalk kernel code. Instead, in the script that loads your extension classes add these two lines:

 
ObjectMemory class compile: 'quit        self shouldNotImplement'!
ObjectMemory class compile: 'quit: n     self shouldNotImplement'!

which will effectively disable the two offending methods. Other possibilities include using atexit (from the C library) to exit your program in a less traumatic way, or redefining these two methods to exit through a call out to a C routine in your program.

Also, note that it is not a problem if you develop the class libraries for your programs within GNU Smalltalk's environment without defineCFunc-ing your own C call-outs, since GNU Smalltalk recalculates the addresses of the C call-outs every time it is started.




This document was generated on May, 12 2002 using texi2html