=head1 NAME Tk2portableTk - how to make your B source portable to other interpreted languages. =for category C Programming =head1 Author Ilya Zakharevich has contributed most of this document. Many thanks. =head1 DESCRIPTION B is an attempt to make B useful from other languages. Currently tk4.0 runs under Perl using this approach. Below, I is the notation for an external language to which B glues B code. The main problem with using the code developed for B with different languages is the absence of data types: almost anything is C. It makes automatic translation hopeless. However, if you C several new symbols to be C, you can still use your code in B, I it will make the automatic translation possible. Another problem with the approach that "everything is a string" is impossibility to have a result that says "NotApplicable" without setting an error. Thus different B command return different string values that mean "error happened", like C<"">, C<" "> or C<"??">. Other languages can be more flexible, so in B you should inform the compiler that what you want to return means "error" (see L). Currently B uses several different approachs to simplify translation: several B functions that are especially dangerous to use are undefined, so you can easily find places that need to be updated to use Language-independent functions based on compiler warnings. Eventually a way to use these Language-independent functions under proper B will be also provided. The end of this document provides a starting point for such a project. =head1 Structure of B, porting your code B, that is a port of B, is very special with respect to porting of other code to B. The problem is that currently there is very little hope to merge the modifications back into B, so a special strategy is needed to maintain this port. Do not use this strategy to port your own code. B is produced from B via a two-step process: first, some manual editing (the result is in the subdirectory C), and second, automatic conversion by the C script (written in Perl). Thus the subdirectory C contains code with minimal possible difference from the virgin B code, so it is easier to merge(1) the differences between B versions into modified code. It looks like the strategy for a portable code should be exactly opposite: starting from B-based code, apply C, and then hand-edit the resulting code. Probably it is also possible to target your code to B from scratch, since this will make it possible to run it under a lot of Iuages. The only reason anyone would like to look into contents of C directory is to find out which constructs are not supported by C. On the other hand, C directory contains code that is conformant to B, so you can look there to find example code. C is the script that converts most common B constructs to their C equivalent. For your code to qualify, you should follow B conventions on indentation and names of variables, in particular, the array of arguments for the C<...CmdProc> should be called C. For details on what C can do, see L. =head1 B API =head2 Checking what you are running under B provides a symbol C. If this symbol is defined, your source is compiled with it. =head2 New types of configuration options B defines several new types of configuration options: TK_CONFIG_CALLBACK TK_CONFIG_LANGARG TK_CONFIG_SCALARVAR TK_CONFIG_HASHVAR TK_CONFIG_ARRAYVAR TK_CONFIG_IMAGE You should use them instead of TK_CONFIG_STRING whenever appropriate. This allows your application to receive a direct representation of the corresponding resource instead of the string representation, if this is possible under given language. ???? It looks like C and C set variables of type C. =head2 Language data The following data types are defined: =over 4 =item C is the main datatype of the language. This is a type that your C function gets pointers to for arguments when the corresponding I function is called. The corresponding config type is C. This is also a type that keeps information about contents of I variable. =item C Is a substitute for a C that contains name of variable. In I it is an object that contains reference to another I variable. =item C ???? =item C C a substitute for a C that contains command to call. The corresponding config type is C. =item C It is the type that the C sets. Before you call it, declare Args *args; LangFreeProc *freeProc = NULL; ... code = Lang_SplitList(interp, value, &argc, &args, &freeProc); After you use the split values, call if (args != NULL && freeProc) (*freeProc)(argc,args); It is not guaranteed that the C can survive deletion of C. =back =head2 Conversion The following macros and functions are used for conversion between strings and the additional types: LangCallback * LangMakeCallback(Tcl_Obj *) Tcl_Obj * LangCallbackArg(LangCallback *) char * LangString(Tcl_Obj *) After you use the result of LangCallbackArg(), you should free it with C C (it is not guaranteed that any change of C will not be reflected in , so you cannot do LangSet...() in between, and you should reset it to C if you want to do any further assignments to this C). The following function returns the C that is a reference to C: Tcl_Obj * LangVarArg(Var) ???? It is very anti-intuitive, I hope the name is changed. int LangCmpCallback(LangCallback *a,Tcl_Obj * b) (currently only a stub), and, at last, LangCallback * LangCopyCallback(LangCallback *) =head2 Callbacks Above we have seen the new datatype C and the corresponding I C. The following functions are provided for manipulation of Cs: void LangFreeCallback(LangCallback *) int LangDoCallback(Tcl_Interp *,LangCallback *, int result,int argc, char *format,...) The argument C of C should contain a string that is suitable for C with optional arguments of C. C should be false if result of callback is not needed. int LangMethodCall(Tcl_Interp *,Tcl_Obj *,char *method, int result,int argc,...) ???? Conceptually, C is a substitute for ubiquitous C in B. So you should use C instead of C or C if appropriate. =head2 Setting variables void LangFreeArg (Tcl_Obj *, Tcl_FreeProc *freeProc) Tcl_Obj * LangCopyArg (Tcl_Obj *); void Tcl_AppendArg (Tcl_Interp *interp, Tcl_Obj *) void LangSetString(Tcl_Obj * *, char *s) void LangSetDefault(Tcl_Obj * *, char *s) These two are equivalent unless s is an empty string. In this case C behaves like C with C, i.e., it sets the current value of the I variable to be false. void LangSetInt(Tcl_Obj * *,int) void LangSetDouble(Tcl_Obj * *,double) The I functions separate uninitialized and initialized data comparing data with C. So the declaration for an C should look like Tcl_Obj * arg = NULL; if you want to use this C with the above functions. After you are done, you should use C with C as C. =head2 Language functions Use =over 4 =item C to check that an object is false; =item C ???? =item C to make a proper shutdown; =item C to call I C; =item C =item C =item C =item C currently stubs only; =item C to save the structure C into I variable C<*varPtr>; =item C to free the result; =item C ???? =item C =item C =item C unsupported????; =item C =back Another useful construction is Tcl_Obj * variable = LangFindVar(interp, Tk_Window tkwin, char *name); After using the above function, you should call LangFreeVar(Var variable); ???? Note discrepancy in types! If you want to find the value of a variable (of type C) given the variable name, use C. If you are interested in the string value of this variable, use C. To get a B array of C of length C, use Tcl_Obj * *args = LangAllocVec(n); ... LangFreeVec(n,args); You can set the values of the Cs using C functions, and get string value using C. If you want to merge an array of Cs into one C (that will be an array variable), use result = Tcl_Merge(listLength, list); =head2 Translation of some TCL functions We mark items that can be dealt with by C by I. =over 4 =item C does not take C<(char*)NULL>, but C as delimiter. I. =item C, C C, C, the second argument is the window itself, not the pathname. I. =item Cresult, "%d %d %d %d",...)> C. I. =item Cresult = "1";> C. I. =item Reading Cresult> C. I. =item Cresult = Tk_PathName(textPtr-Etkwin);> Ctkwin)>. I. =item Sequence C Use a single command void Tcl_DoubleResults(Tcl_Interp *interp, int append, int argc,...); C governs whether it is required to clear the result first. A similar command for C arguments is C. =item C Use C (see the description above). =back =head1 Translation back to TCL To use your B program with B, put #include "ptcl.h" I inclusion of C, and link the resulting code with C. These files currently implement the following: =over 4 =item Additional config types: TK_CONFIG_CALLBACK TK_CONFIG_LANGARG TK_CONFIG_SCALARVAR TK_CONFIG_HASHVAR TK_CONFIG_ARRAYVAR TK_CONFIG_IMAGE =item Types: Var, Tcl_Obj *, LangCallback, LangFreeProc. =item Functions and macros: Lang_SplitList, LangString, LangSetString, LangSetDefault, LangSetInt, LangSetDouble Tcl_ArgResult, LangCallbackArg, LangSaveVar, LangFreeVar, LangFreeSplitProc, LangFreeArg, Tcl_DoubleResults, Tcl_IntResults, LangDoCallback, Tk_WidgetResult, Tcl_CreateCommand, Tcl_DeleteCommand, Tcl_GetResult. =back Current implementation contains enough to make it possible to compile C with the virgin B. =head2 New types of events ???? PortableTk defines following new types of events: TK_EVENTTYPE_NONE TK_EVENTTYPE_STRING TK_EVENTTYPE_NUMBER TK_EVENTTYPE_WINDOW TK_EVENTTYPE_ATOM TK_EVENTTYPE_DISPLAY TK_EVENTTYPE_DATA and a function char * Tk_EventInfo(int letter, Tk_Window tkwin, XEvent *eventPtr, KeySym keySym, int *numPtr, int *isNum, int *type, int num_size, char *numStorage) =head1 Checking for trouble If you start with working TCL code, you can start convertion using the above hints. Good indication that you are doing is OK is absence of C and C in your code (at least in the part that is working with interpreter). =head1 Additional API What is described here is not included into base B distribution. Currently it is coded in B and as Perl macros (core is coded as functions, so theoretically you can use the same object files with different interpreted languages). =head2 C Dynamic arrays in B are used for two different purposes: to construct strings, and to construct lists. These two usages will have separate interfaces in other languages (since list is a different type from a string), so you should use a different interface in your code. The type for construction of dynamic lists is C. The API below is a counterpart of the API for construction of dynamic lists in B: void ListFactoryInit(ListFactory *) void ListFactoryFinish(ListFactory *) void ListFactoryFree(ListFactory *) Tcl_Obj * * ListFactoryArg(ListFactory *) void ListFactoryAppend(ListFactory *, Tcl_Obj * *arg) void ListFactoryAppendCopy(ListFactory *, Tcl_Obj * *arg) ListFactory * ListFactoryNewLevel(ListFactory *) ListFactory * ListFactoryEndLevel(ListFactory *) void ListFactoryResult(Tcl_Interp *, ListFactory *) The difference is that a call to C should precede the actual usage of the value of C, and there are two different ways to append an C to a C: ListFactoryAppendCopy() guarantees that the value of C is copied to the list, but ListFactoryAppend() may append to the list a reference to the current value of C. If you are not going to change the value of C after appending, the call to ListFactoryAppend may be quicker. As in B, the call to ListFactoryFree() does not free the C, only the objects it references. The functions ListFactoryNewLevel() and ListFactoryEndLevel() return a pointer to a C to fill. The argument of ListFactoryEndLevel() cannot be used after a call to this function. =head2 DStrings Production of strings are still supported in B. =head2 Accessing Cs The following functions for getting a value of an C I be provided: double LangDouble(Tcl_Obj *) int LangInt(Tcl_Obj *) long LangLong(Tcl_Obj *) int LangIsList(Tcl_Obj * arg) The function LangIsList() is supported only partially under B, since there is no data types. It checks whether there is a space inside the string C. =head2 Assigning numbers to Cs While LangSetDouble() and LangSetInt() are supported ways to assign numbers to assign an integer value to a variable, for the sake of efficiency under B it is supposed that the destination of these commands was massaged before the call so it contains a long enough string to sprintf() the numbers inside it. If you are going to immediately use the resulting C, the best way to do this is to declare a buffer in the beginning of a block by dArgBuffer; and assign this buffer to the C by void LangSetDefaultBuffer(Tcl_Obj * *) You can also create the buffer(s) manually and assign them using void LangSetBuffer(Tcl_Obj * *, char *) This is the only choice if you need to assign numeric values to several Cs simultaneously. The advantage of the first approach is that the above declarations can be made Cs in different languages. Note that if you apply C to an C that contains some value, you can create a leak if you do not free that C first. This is a non-problem in real languages, but can be a trouble in C, unless you use only the above API. =head2 Creating new Cs The API for creating a new C is void LangNewArg(Tcl_Obj * *, LangFreeProc *) The API for creating a new C is absent. Just initialize C to be C, and apply one of C methods. After you use this C, it should be freed thusly: C. =head2 Evaluating a list Use int LangArgEval(Tcl_Interp *, Tcl_Obj * arg) Here C should be a list to evaluate, in particular, the first element should be a C massaged to be an C. The arguments can be send to the subroutine by reference or by value in different languages. =head2 Getting result as C Use C. It is not guaranteed that result survives this operation, so the C you get should be the only mean to access the data from this moment on. After you use this C, you should free it with C C (you can do LangSet...() in between). =cut