[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.2.2.2 Main Program Objects

As mentioned above, Cogsys extensions are dynamically loaded in a "two-way" mechanism: not only can the main program call the extension, but the extension can call the main program. Because the Cogsys runtime linker will fixup references to the main program using the `COGSYS3x.BIM' file, the extension writer can access any Cogsys data structure or function with a simple external declaration.

The most common data structure accessed is the gVars[] array, which stores the values of the Cogsys user variables. For example, if we wanted our shape extension to use the contents of Cogsys variable 90 (set by the user with a call to $AV90=20, for example) for the circle of the radius, as opposed to (or perhaps as an alternative to) the passed argument, we could write something like this before the delcaration of the function extension:

 
extern long gVars[];

And then use gVars as we would any other variable:

 
    case CIRCLE:
      radius = gVars[90];
      ...

We can also use functions that are part of the main program. Normally, functions called are not Cogsys functions, but rather standard C library routines that were statically linked into the Cogsys executable. It is important to remember that the extension is never linked by a standard C linker, but is linked dynamically at runtime.

Fortunately, because an extension rarely needs a function that Cogsys didn't need, this is rarely an issue. However, several functions are often needed in extensions which aren't a part of Cogsys, most notably sin(), cos() and rand().

To work around this, Cogsys 4.1.4 currently has an internal, unreachable dummy() function which deliberately references these functions. When the Cogsys developer builds the `cogsys' executable, dummy() "fools" the linker into including the code for these functions. They are in turn now available to any extension that needs them.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on November, 24 2008 using texi2html