next up previous contents index
Next: 3.8 Class InstVar Up: 3. OTcl Linkage Previous: 3.6 Class TclCommand

   
3.7 Class EmbeddedTcl

permits the development of functionality in either compiled code, or through interpreter code, that is evaluated at initialization. For example, the scripts tcl-object.tcl or the scripts in tcl/lib. Such loading and evaluation of scripts is done through objects in the EmbeddedTcl../Tcl/Tcl.h.

The easiest way to extend is to add OTcl code to either tcl-object.tcl or through scripts in the tcl/lib directory. Note that, in the latter case, sources tcl/lib/ns-lib.tcl automatically, and hence the programmer must add a couple of lines to this file so that their script will also get automatically sourced by at startup. As an example, the file tcl/mcast/srm.tcl defines some of the instance procedures to run SRM. In tcl/lib/ns-lib.tcl, we have the lines:

	source tcl/mcast/srm.tcl
to automatically get srm.tcl sourced by at startup.

Three points to note with EmbeddedTcl code are that firstly, if the code has an error that is caught during the eval, then will not run. Secondly, the user can explicitly override any of the code in the scripts. In particular, they can re-source the entire script after making their own changes. Finally, after adding the scripts to tcl/lib/ns-lib.tcl, and every time thereafter that they change their script, the user must recompile for their changes to take effect. Of course, in most cases3.4, the user can source their script to override the embedded code.

The rest of this subsection illustrate how to integrate individual scripts directly into . The first step is convert the script into an EmbeddedTcl object. The lines below expand ns-lib.tcl and create the EmbeddedTcl object instance called et_ns_lib:

        tclsh bin/tcl-expand.tcl tcl/lib/ns-lib.tcl | \bs
                               ../Tcl/tcl2c++ et_ns_lib \> gen/ns_tcl.cc
The script, bin/tcl-expand.tcl../ns-2/tcl-expand.tcl expands ns-lib.tcl by replacing all source lines with the corresponding source files. The program, tcl2cc.c../Tcl/tcl2c++.c.html, converts the OTcl code into an equivalent EmbeddedTcl object, et_ns_lib.

During initialization, invoking the method EmbeddedTcl::load explicitly evaluates the array.

--
tcl-object.tcl../Tcl/tcl-object.tcl is evaluated by the method Tcl::init../Tcl/Tcl.ccTcl::init; []Tcl_AppInit../ns-2/tclAppInit.cc::Tcl_AppInit invokes []Tcl::Init. The exact command syntax for the load is:
	et\_tclobject.load();
--
Similarly, tcl/lib/ns-lib.tcl../ns-2/tcl/lib/ns-lib.tcl is evaluated directly by Tcl_AppInit in ns_tclsh.cc.
	et\_ns\_lib.load();


next up previous contents index
Next: 3.8 Class InstVar Up: 3. OTcl Linkage Previous: 3.6 Class TclCommand

2000-08-24