Simple Example
In the following example, a TJC module
containing a single Tcl script file and a single compiled proc will be
created and loaded into the Jacl shell. This example makes use of the tjc executable and the TJC::package command.
In this example, the Tcl source file used as input to the TJC compiler
is defined as follows:
$ cat simple.tcl
proc simple {} {
return "SIMPLE"
}
The user will now need to create a TJC module file for the TJC
package. The name of the module file will be simple.tjc. The name of the
Java package will also be simple,
this is the same package name that will later be passed to the TJC::package command. The SOURCE and INIT_SOURCE declarations will
list only the simple.tcl
file. Finally, the OPTIONS
declaration will define the -compile option to indicate that the proc
body string will not be compiled.
$ cat simple.tjc
PACKAGE simple
SOURCE simple.tcl
INIT_SOURCE simple.tcl
OPTIONS -compile
Assuming that the tjc executable
is already on the PATH,
the TJC compiler is invoke via:
$ tjc simple.tjc
If no error is generated, then nothing is printed and the files simple.jar and simplesrc.jar are created in
the current directory.
The contents of the generated simple.jar
are as follows:
$ jar -tf simple.jar
META-INF/
META-INF/MANIFEST.MF
simple/
simple/library/
simple/library/simple.tcl
simple/SimpleCmd.class
simple/TJCExtension.class
Note that simple.jar
contains both the compiled version of the proc simple and the script simple.tcl.
This TJC package can be loaded into a Jacl shell by adding simple.jar to the CLASSPATH and invoking Jacl.
$ export
CLASSPATH=${CLASSPATH}:`pwd`/simple.jar
$ java tcl.lang.Shell
At the Jacl shell prompt the following Tcl commands would be entered to
load the TJC package and init the
simple package.
% package require TJC
1.0
% TJC::package simple
% simple
SIMPLE
When the simple package is loaded into Jacl via the TJC::package command, a Tcl
command named simple is created. This new command is the compiled
version of the simple proc defined in simple.tcl.
To explain how package loading is implemented, it is useful to compare
the original simple.tcl script to the version that gets executed when
the simple package is loaded into Jacl.
The contents of the generated simplesrc.jar
are as follows:
$ jar -tf simplesrc.jar
META-INF/
META-INF/MANIFEST.MF
simple/
simple/library/
simple/library/simple.tcl
simple/SimpleCmd.java
simple/TJCExtension.java
Note that the simple/library/simple.tcl
Tcl script in simplesrc.jar
is not the same as the simple/library/simple.tcl
Tcl script in simple.jar.
The Tcl script in simplesrc.jar
is the original while the one in simple.jar
has been modified to support loading into TJC. Comparing the two Tcl
files will make the difference clear.
$ mkdir simple simplesrc
$ cd simple
$ jar
-xf ../simple.jar
$
cd ..
$ cd simplesrc
$ jar -xf
../simplesrc.jar
$ cd ..
$ cat simplesrc/simple/library/simple.tcl
proc simple {} {
return "SIMPLE"
}
$ cat simple/simple/library/simple.tcl
TJC::command simple
simple.SimpleCmd
#
#
The original Tcl script defines a command via the proc command while
the modified version defines a compiled command using TJC::command. This modified
version of simple.tcl is sourced into the Jacl shell when the [TJC::package simple] command
is executed.