Thread: [tcljava-user] Advice Porting Tcl C API Tcl_CreateObjCommand to TclJava tcl.lang.Interp#createComma
Brought to you by:
mdejong
From: Brian B. (US) <Bri...@da...> - 2016-04-29 16:13:03
|
We're porting a TCL-based C app to Java using TclJava (technically, jtcl). We have a lot of TCL commands written in C which utilize the ClientData passed to Tcl_CreateObjCommand https://www.tcl.tk/man/tcl/TclLib/CrtObjCmd.htm. TclJava lacks command ClientData support. So I'm trying to come up with an alternative that works with TclJava tcl.lang.Interp#createCommand http://tcljava.sourceforge.net/docs/TclJavaLib/createCmd.htm. A couple of Intra-Process-Communication approaches I've considering: 1. Use AssocData http://tcljava.sourceforge.net/docs/TclJavaLib/AssocData.htm. 2. Use a Java class static member (guarded for thread-safety) e.g. Package org.myorg.myprod; public class GlobalData { public static Map<String,Object> clientData = new HashMap<>(); } We have hundreds of TCL scripts which already use these commands, so altering the scripts to pass the "ClientData" as command arguments isn't really an option. Has anyone already successfully solved this problem? Are there examples of how to do this already? Any advice? Sincerely, Brian Brooks Sr Software Engineer Duluth, GA 30096 This email and any files transmitted with it are intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This message contains information that is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited. |
From: Tom P. <tpo...@ny...> - 2016-05-03 03:17:11
|
Hi Brian, There's not ClientData in JTcl/Jacl, because Java already gives you that functionality for free, just for having Java classes. Writing a JTcl extension in Java requires that you implement the tcl.lang.Command interface; your actual classes can have any amount of private data per command class. Of course you'll need a constructor that provides or creates your private data, but that can happen once when you load your package. Many of the builtin command implementations use some private data, but mostly these are constants ("private static final ...."). There's no reason these can't be ordinary Java class fields. Also check out tcl.lang.cmd.AfterCmd.java. It uses the interp AssocData, you can see how that's done. Here's a sample using the 'hyde' extension, and the Java code that it produces: ----------------------------------------------- package require hyde hyde::configure -debug 1 hyde::jcommand lastInvoked -auxmethods { // some private data private int lastInvokedSeconds; // constructor for this command (hyde adds the "Cmd" suffix) public lastInvokedCmd () { getSeconds(); } // a helper method private void getSeconds() { lastInvokedSeconds = (int) (System.currentTimeMillis() / 1000); } } -body { // body of our command, just return the last invoked time interp.setResult(lastInvokedSeconds); // set the time for the next invocation getSeconds(); } ----------------------------------------------- Here's the Java code that hyde produces, compiles, and creates into a Tcl command: ----------------------------------------------- package hyde; import tcl.lang.*; public class lastInvokedCmd implements Command { // some private data private int lastInvokedSeconds; // constructor for this command (hyde adds the "Cmd" suffix) public lastInvokedCmd () { getSeconds(); } // a helper method private void getSeconds() { lastInvokedSeconds = (int) (System.currentTimeMillis() / 1000); } public void cmdProc (Interp interp, TclObject argv[]) throws TclException { // body of our command, just return the last invoked time interp.setResult(lastInvokedSeconds); // set the time for the next invocation getSeconds(); } } ----------------------------------------------- Now, try out our new 'lastInvoked' command: proc testLastInvoked {} { puts "time now is [clock format [clock seconds]]" lastInvoked after 5000 puts " it's now [clock format [clock seconds]], \ but the last time invoked was [clock format [lastInvoked]]" } testLastInvoked ==> time now is Mon May 02 20:47:41 MDT 2016 ==> it's now Mon May 02 20:47:46 MDT 2016, but the last time invoked was Mon May 02 20:47:41 MDT 2016 Good luck, Tom On Fri, Apr 29, 2016 at 03:40:31PM +0000, Brian Brooks (US) wrote: > We're porting a TCL-based C app to Java using TclJava (technically, jtcl). > > We have a lot of TCL commands written in C which utilize the ClientData passed to Tcl_CreateObjCommand https://www.tcl.tk/man/tcl/TclLib/CrtObjCmd.htm. > > TclJava lacks command ClientData support. So I'm trying to come up with an alternative that works with TclJava tcl.lang.Interp#createCommand http://tcljava.sourceforge.net/docs/TclJavaLib/createCmd.htm. > > A couple of Intra-Process-Communication approaches I've considering: > 1. Use AssocData http://tcljava.sourceforge.net/docs/TclJavaLib/AssocData.htm. > 2. Use a Java class static member (guarded for thread-safety) e.g. > > Package org.myorg.myprod; > > public class GlobalData { > public static Map<String,Object> clientData = new HashMap<>(); > } > > We have hundreds of TCL scripts which already use these commands, so altering the scripts to pass the "ClientData" as command arguments isn't really an option. > > Has anyone already successfully solved this problem? Are there examples of how to do this already? Any advice? > > Sincerely, > Brian Brooks > Sr Software Engineer > Duluth, GA 30096 > > This email and any files transmitted with it are intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This message contains information that is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited. > > > > ------------------------------------------------------------------------------ > Find and fix application performance issues faster with Applications Manager > Applications Manager provides deep performance insights into multiple tiers of > your business applications. It resolves application problems quickly and > reduces your MTTR. Get your free trial! > https://ad.doubleclick.net/ddm/clk/302982198;130105516;z > _______________________________________________ > tcljava-user mailing list > tcl...@li... > https://lists.sourceforge.net/lists/listinfo/tcljava-user -- Tom Poindexter tpo...@ny... |