Next: , Previous: , Up: C and Smalltalk  


5.9 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 is a simplified snippet from gst-tool.c.

int main(argc, argv)
int     argc;
char    **argv;
{
  gst_set_var (GST_VERBOSITY, 1);
  gst_smalltalk_args (argc - 1, argv + 1);
  gst_set_executable_path (argv[0]);
  result = gst_initialize ("kernel-dir", "image-file", GST_NO_TTY);
  if (result != 0)
    exit (result < 0 ? 1 : result);

  if (!gst_process_file ("source-file", GST_DIR_KERNEL_SYSTEM))
    perror ("gst: couldn't load `source-file'");

  gst_invoke_hook (GST_ABOUT_TO_QUIT);
  exit (0);
}

Your initialization code will be almost the same as that in GNU Smalltalk’s main(), with the exception of the call to gst_process_file. All you’ll have to do is to pass some arguments to the GNU Smalltalk library via gst_smalltalk_args, possibly modify some defaults using gst_get_var and gst_set_var, and then call gst_initialize.

Variable indices that can be passed to gst_get_var and gst_set_var include:

GST_DECLARE_TRACING
GST_EXECUTION_TRACING
GST_EXECUTION_TRACING_VERBOSE
GST_GC_MESSAGE
GST_VERBOSITY
GST_MAKE_CORE_FILE
GST_REGRESSION_TESTING

While the flags that can be passed as the last parameter to gst_initialize are any combination of these:

GST_REBUILD_IMAGE
GST_MAYBE_REBUILD_IMAGE
GST_IGNORE_USER_FILES
GST_IGNORE_BAD_IMAGE_NAME
GST_IGNORE_BAD_IMAGE_PATH
GST_IGNORE_BAD_KERNEL_PATH
GST_NO_TTY

Note that gst_initialize will likely take some time (from a tenth of a second to 3-4 seconds), because it has to check if the image file must be be rebuilt and, if so, it reloads and recompiles the over 50,000 lines of Smalltalk code that form a basic image. To avoid this check, pass a valid image file as the second argument to gst_initialize.

The result of gst_init_smalltalk is 0 for success, while anything else is an error code.

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 (which will not call defineCFunc for your own C call-outs), since the addresses of the C call-outs are looked up again when an image is restored.


Next: , Previous: , Up: C and Smalltalk