Copyright ©1997 by AAA+ Software Forschungs- und Entwicklungs Ges.m.b.H. All Rights Reserved. Revision 1.02 - October 31, 1997.
Extending Joy by C functions
This mini-example shows how to extend the functionality of the Joy interpreter with compiled C functions.
Start the Joy interpreter (e.g. inside InterfaceBuilder if you have the Joy palette installed - see the Joy tutorial for details) and change to this directory by typing at the prompt:
joy> cd $env(NEXT_ROOT)/LocalLibrary/Joy/Examples/C-Extensions
The file c2f.c contains the C source of a simple function converting temperatures from Celsius to Fahrenheit. Compile this file to a shared library using the command:
joy> exec gcc -bundle -o c2f.dll c2f.c
To load the package into the Joy interpreter type:
joy> load c2f.dll CelsiusToFahrenheitConversion
CelsiusToFahrenheitConversion is the name of the package and must be the same as the name of the init-function in it without the trailing _Init (capitalization can be different). The name of the init-function of a package has to begin with an uppercase letter and be lowercase elsewise. It can be given (as a third argument to load) a Joy interpreter as argument for which the initialization should take place. Default is the calling interpreter. The init-function should return nonzero if initialization failed.
To make the C function c2f of the package visible to the interpreter type
joy> objc:declare float {c2f c2f:c2f} {float}
The c2f:c2f part is necessary because under Windows, Joy has to know the name of the dll in addition to the function name to get the function's address. So, the objc:funcPtr, objc:varPtr, and objc:declare commands support a special notation for linker symbols, where the dll name (without extension) is specified before a colon, and the symbol name after the colon. For symbols defined by Joy or the NeXT frameworks you don't have to use the colon-notation, because Joy looks in those dll's by default.
You can now call the C function like any internal command, e.g.:
joy> c2f 12.5
54.5