001: /*
002: * Copyright (c) 2006 Mo DeJong
003: *
004: * See the file "license.amd" for information on usage and
005: * redistribution of this file, and for a DISCLAIMER OF ALL
006: * WARRANTIES.
007: *
008: * RCS: @(#) $Id: TJCReadyVar.java,v 1.1 2006/02/14 04:13:27 mdejong Exp $
009: *
010: */
011:
012: // This class implements the TJCThread.CompiledClassReady
013: // interface and sets a variable to the results of the
014: // compilation.
015: package tcl.lang;
016:
017: import java.util.ArrayList;
018:
019: public class TJCReadyVar implements TJCThread.CompiledClassReady {
020: final Interp interp;
021: final String varname;
022:
023: // Invoked in the calling Thread to indicate
024: // where a compile result should be stored.
025: // varname can be either a scalar or array
026: // variable, it is assumed to be global
027: // unless it has explicit namespace qualifiers.
028: // The variable value is initailly set to {}
029: // whole the compile job is processing.
030:
031: public TJCReadyVar(Interp interp, String varname) {
032: this .interp = interp;
033: this .varname = varname;
034: try {
035: interp.setVar(varname, null, "", TCL.GLOBAL_ONLY);
036: } catch (TclException te) {
037: }
038: }
039:
040: // Invoked by TJCThread when a compile job
041: // is finished. This implementation will
042: // set a variable to a list indicating the
043: // status.
044: //
045: // {STATUS GENINFO FILENAME SRCCODE CNAMES CDATA MSG}
046: //
047: // STATUS: OK or FAIL
048: // GENINFO: Key indicating generated info source
049: // FILENAME: File name for Java source
050: // SRCCODE: Java source code that was compiled
051: // CNAMES: List of names for compiled classes in CDATA
052: // CDATA: List of compiled class data as reflected byte[] Java objects
053: // MSG: String indicating compile error if STATUS != OK
054:
055: public void compiled(final String geninfo, // Name that identifies the source
056: // for generated Java code. This
057: // "" when compiling a Java file.
058: final String jfilename, // File name for Java source,
059: // like "Test.java".
060: final String jsrcode, // Java source that was compiled.
061: final ArrayList cnames, // List of compiled class names.
062: final ArrayList cdata, // List of compiled class data as byte[].
063: final int status, final String msg) {
064: // Add event to Tcl queue that will
065: // calculate and set variable value.
066:
067: TclEvent event = new TclEvent() {
068: public int processEvent(int flags) {
069: try {
070: TclObject tlist = TclList.newInstance();
071:
072: // STATUS
073: if (status == TJCThread.STATUS_OK) {
074: TclList.append(interp, tlist, TclString
075: .newInstance("OK"));
076: } else {
077: // FIXME: Check TJCThread status bits for more info.
078: TclList.append(interp, tlist, TclString
079: .newInstance("FAIL"));
080: }
081:
082: // GENINFO
083: TclList.append(interp, tlist, TclString
084: .newInstance(geninfo));
085:
086: // FILENAME
087: TclList.append(interp, tlist, TclString
088: .newInstance(jfilename));
089:
090: // SRCCODE
091: TclList.append(interp, tlist, TclString
092: .newInstance(jsrcode));
093:
094: // CNAMES
095: TclObject cnames_list = TclList.newInstance();
096: if (cnames != null) {
097: for (int i = 0; i < cnames.size(); i++) {
098: TclList
099: .append(
100: interp,
101: cnames_list,
102: TclString
103: .newInstance((String) cnames
104: .get(i)));
105: }
106: }
107: TclList.append(interp, tlist, cnames_list);
108:
109: // CDATA
110: TclObject cdata_list = TclList.newInstance();
111: if (cdata != null) {
112: for (int i = 0; i < cdata.size(); i++) {
113: byte[] bytes = (byte[]) cdata.get(i);
114: TclObject tobj;
115: if ((bytes.length == 4) && bytes[0] == 'F'
116: && bytes[1] == 'A'
117: && bytes[2] == 'K'
118: && bytes[3] == 'E') {
119: tobj = TclString.newInstance("FAKE");
120: } else {
121: tobj = ReflectObject.newInstance(
122: interp, byte[].class, bytes);
123: }
124: TclList.append(interp, cdata_list, tobj);
125: }
126: }
127: TclList.append(interp, tlist, cdata_list);
128:
129: // MSG
130: if (cdata != null) {
131: TclList.append(interp, tlist, TclString
132: .newInstance(msg));
133: } else {
134: TclList.append(interp, tlist, TclString
135: .newInstance(""));
136: }
137:
138: interp
139: .setVar(varname, null, tlist,
140: TCL.GLOBAL_ONLY);
141:
142: //System.out.println("set TJCReadyVar result for variable " + varname);
143: } catch (TclException ex) {
144: // If an exception was raise, just set the variable to
145: // the empty string in case there is a vwait on the var.
146:
147: try {
148: interp.setVar(varname, null, "",
149: TCL.GLOBAL_ONLY);
150: } catch (TclException te) {
151: }
152: }
153: return 1;
154: }
155: };
156: interp.getNotifier().queueEvent(event, TCL.QUEUE_TAIL);
157:
158: // Don't want for var to be set, just continue processing
159: // next event in TJCThread.
160: //event.sync();
161: }
162: }
|