wasCSharpSQLite – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * Extension.java -- |
||
3 | * |
||
4 | * Copyright (c) 1997 Cornell University. |
||
5 | * Copyright (c) 1997 Sun Microsystems, Inc. |
||
6 | * |
||
7 | * See the file "license.terms" for information on usage and |
||
8 | * redistribution of this file, and for a DISCLAIMER OF ALL |
||
9 | * WARRANTIES. |
||
10 | * |
||
11 | * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart |
||
12 | * |
||
13 | * RCS @(#) $Id: Extension.java,v 1.2 1999/05/09 21:18:54 dejong Exp $ |
||
14 | * |
||
15 | */ |
||
16 | using System; |
||
17 | namespace tcl.lang |
||
18 | { |
||
19 | |||
20 | /// <summary> Base class for all Tcl Extensions. A Tcl Extension defines a set of |
||
21 | /// commands that can be loaded into an Interp as a single unit. |
||
22 | /// |
||
23 | /// When a Tcl Extension is loaded into an Interp, either statically |
||
24 | /// (using the "new" operator inside Java code) or dynamically (using |
||
25 | /// the java::load command in Tcl scripts), it usually creates a set of |
||
26 | /// commands inside the interpreter. Occasionally, loading an Extension |
||
27 | /// may lead to additional side effects. For example, a communications |
||
28 | /// Extension may open network connections when it's loaded. Please |
||
29 | /// refer to the documentation of the specific Extension for details. |
||
30 | /// </summary> |
||
31 | |||
32 | abstract public class Extension |
||
33 | { |
||
34 | |||
35 | /// <summary> Default constructor. Does nothing. The purpose of this |
||
36 | /// constructor is to make sure instances of this Extension can be |
||
37 | /// loaded dynamically using the "java::load" command, which calls |
||
38 | /// Class.newInstance(). |
||
39 | /// </summary> |
||
40 | |||
41 | public Extension() |
||
42 | { |
||
43 | } |
||
44 | |||
45 | /// <summary> Initialize the Extension to run in a normal (unsafe) |
||
46 | /// interpreter. This usually means creating all the commands |
||
47 | /// provided by this class. A particular implementation can arrange |
||
48 | /// the commands to be loaded on-demand using the loadOnDemand() |
||
49 | /// function. |
||
50 | /// |
||
51 | /// </summary> |
||
52 | /// <param name="interp">current interpreter. |
||
53 | /// </param> |
||
54 | |||
55 | abstract public void init( Interp interp ); |
||
56 | |||
57 | /// <summary> Initialize the Extension to run in a safe interpreter. This |
||
58 | /// method should be written carefully, so that it initializes the |
||
59 | /// safe interpreter only with partial functionality provided by |
||
60 | /// the Extension that is safe for use by untrusted code. |
||
61 | /// |
||
62 | /// The default implementation always throws a TclException, so that |
||
63 | /// a subclass of Extension cannot be loaded into a safe interpreter |
||
64 | /// unless it has overridden the safeInit() method. |
||
65 | /// |
||
66 | /// </summary> |
||
67 | /// <param name="safeInterp">the safe interpreter in which the Extension should |
||
68 | /// be initialized. |
||
69 | /// </param> |
||
70 | |||
71 | public void safeInit( Interp safeInterp ) |
||
72 | { |
||
73 | throw new TclException( safeInterp, "Extension \"" + GetType().ToString() + "\" cannot be loaded into a safe interpreter" ); |
||
74 | } |
||
75 | |||
76 | /// <summary> Create a stub command which autoloads the real command the first time |
||
77 | /// the stub command is invoked. Register the stub command in the |
||
78 | /// interpreter. |
||
79 | /// |
||
80 | /// </summary> |
||
81 | /// <param name="interp">current interp. |
||
82 | /// </param> |
||
83 | /// <param name="cmdName">name of the command, e.g., "after". |
||
84 | /// </param> |
||
85 | /// <param name="clsName">name of the Java class that implements this command, |
||
86 | /// e.g. "tcl.lang.AfterCmd" |
||
87 | /// </param> |
||
88 | |||
89 | public static void loadOnDemand( Interp interp, string cmdName, string clsName ) |
||
90 | { |
||
91 | interp.createCommand( cmdName, new AutoloadStub( clsName ) ); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /// <summary> The purpose of AutoloadStub is to load-on-demand the classes that |
||
96 | /// implement Tcl commands. This reduces Jacl start up time and, when |
||
97 | /// running Jacl off a web page, reduces download time significantly. |
||
98 | /// </summary> |
||
99 | |||
100 | class AutoloadStub : Command |
||
101 | { |
||
102 | internal string className; |
||
103 | |||
104 | /// <summary> Create a stub command which autoloads the real command the first time |
||
105 | /// the stub command is invoked. |
||
106 | /// |
||
107 | /// </summary> |
||
108 | /// <param name="clsName">name of the Java class that implements this command, |
||
109 | /// e.g. "tcl.lang.AfterCmd" |
||
110 | /// </param> |
||
111 | internal AutoloadStub( string clsName ) |
||
112 | { |
||
113 | className = clsName; |
||
114 | } |
||
115 | |||
116 | /// <summary> Load the class that implements the given command and execute it. |
||
117 | /// |
||
118 | /// </summary> |
||
119 | /// <param name="interp">the current interpreter. |
||
120 | /// </param> |
||
121 | /// <param name="argv">command arguments. |
||
122 | /// </param> |
||
123 | /// <exception cref=""> TclException if error happens inside the real command proc. |
||
124 | /// </exception> |
||
125 | public TCL.CompletionCode cmdProc( Interp interp, TclObject[] argv ) |
||
126 | { |
||
127 | Type cmdClass = null; |
||
128 | Command cmd; |
||
129 | try |
||
130 | { |
||
131 | cmdClass = System.Type.GetType( className, true ); |
||
132 | } |
||
133 | catch ( System.Exception e ) |
||
134 | { |
||
135 | throw new TclException( interp, "ClassNotFoundException for class \"" + className + "\"" ); |
||
136 | } |
||
137 | |||
138 | try |
||
139 | { |
||
140 | cmd = (Command)SupportClass.CreateNewInstance( cmdClass ); |
||
141 | } |
||
142 | catch ( System.UnauthorizedAccessException e1 ) |
||
143 | { |
||
144 | throw new TclException( interp, "IllegalAccessException for class \"" + cmdClass.FullName + "\"" ); |
||
145 | } |
||
146 | catch ( System.InvalidCastException e3 ) |
||
147 | { |
||
148 | throw new TclException( interp, "ClassCastException for class \"" + cmdClass.FullName + "\"" ); |
||
149 | } |
||
150 | catch ( System.Exception e2 ) |
||
151 | { |
||
152 | throw new TclException( interp, "InstantiationException for class \"" + cmdClass.FullName + "\"" ); |
||
153 | } |
||
154 | |||
155 | interp.createCommand( argv[0].ToString(), cmd ); |
||
156 | TCL.CompletionCode rc = cmd.cmdProc( interp, argv ); |
||
157 | return rc == TCL.CompletionCode.EXIT ? TCL.CompletionCode.EXIT : TCL.CompletionCode.RETURN; |
||
158 | } |
||
159 | } |
||
160 | } |