5 // Cesar Lopez Nataren (cesar@ciencias.unam.mx)
7 // (C) 2003, Cesar Lopez Nataren
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System
.Reflection
;
33 using System
.Security
.Policy
;
34 using System
.Threading
;
35 using Microsoft
.JScript
;
36 using Microsoft
.JScript
.Vsa
;
38 namespace Microsoft
.Vsa
{
40 public abstract class BaseVsaEngine
: IVsaEngine
{
42 private const int ROOT_MONIKER_MAX_SIZE
= 256;
43 private bool monikerAlreadySet
;
44 private string rootMoniker
;
49 private bool siteAlreadySet
;
52 private bool namespaceNotSet
;
53 private bool supportDebug
;
54 private bool generateDebugInfo
;
55 protected bool compiled
;
58 private bool initNewCalled
;
60 private IVsaSite site
;
61 private IVsaItems items
;
63 // its default value has to be "JScript"
64 private string language
;
66 private string version
;
68 private Assembly assembly
;
69 private Evidence evidence
;
71 private string rootNamespace
;
73 // FIXME: must set vars to proper values
74 internal BaseVsaEngine ()
77 public BaseVsaEngine (string language
, string version
, bool supportDebug
)
79 this.language
= language
;
81 // FIXME: I think we must ensure that version it's
82 // compliant with versionformat Major.Minor.Build.Revision.
83 // Not sure about what Exception throw.
84 this.version
= version
;
86 this.supportDebug
= supportDebug
;
88 this.rootMoniker
= "";
91 this.compiled
= false;
94 this.lcid
= Thread
.CurrentThread
.CurrentCulture
.LCID
;
97 this.rootNamespace
= "";
98 this.namespaceNotSet
= true;
100 this.initNewCalled
= false;
101 this.generateDebugInfo
= false;
104 this.siteAlreadySet
= false;
107 public System
._AppDomain AppDomain
{
108 get { throw new NotImplementedException (); }
109 set { throw new NotImplementedException (); }
112 // FIXME: research if we can use "get" Evidence when: running and busy.
113 public Evidence Evidence
{
116 throw new VsaException (VsaError
.EngineClosed
);
117 else if (!initNewCalled
)
118 throw new VsaException (VsaError
.EngineNotInitialized
);
125 throw new VsaException (VsaError
.EngineClosed
);
127 throw new VsaException (VsaError
.EngineRunning
);
129 throw new VsaException (VsaError
.EngineBusy
);
130 else if (!initNewCalled
)
131 throw new VsaException (VsaError
.EngineNotInitialized
);
137 public string ApplicationBase
{
138 get { throw new NotImplementedException (); }
139 set { throw new NotImplementedException (); }
142 public Assembly Assembly
{
145 throw new VsaException (VsaError
.EngineClosed
);
147 throw new VsaException (VsaError
.EngineNotRunning
);
149 throw new VsaException (VsaError
.EngineBusy
);
156 // FIXME: research if we can "get" it when running and busy.
157 // FIXME: when do we throw VsaException with
158 // VsaError set to DebugInfoNotSupported?
160 public bool GenerateDebugInfo
{
163 throw new VsaException (VsaError
.EngineClosed
);
164 else if (!initNewCalled
)
165 throw new VsaException (VsaError
.EngineNotInitialized
);
167 return generateDebugInfo
;
172 throw new VsaException (VsaError
.EngineClosed
);
174 throw new VsaException (VsaError
.EngineRunning
);
176 throw new VsaException (VsaError
.EngineBusy
);
177 else if (!initNewCalled
)
178 throw new VsaException (VsaError
.EngineNotInitialized
);
179 else if (!supportDebug
)
180 throw new VsaException (VsaError
.DebugInfoNotSupported
);
182 generateDebugInfo
= value;
186 public bool IsCompiled
{
191 throw new VsaException (VsaError
.EngineClosed
);
193 throw new VsaException (VsaError
.EngineBusy
);
194 else if (!initNewCalled
)
195 throw new VsaException (VsaError
.EngineNotInitialized
);
202 // FIXME: Research if we can "set" it when running
203 public bool IsDirty
{
206 throw new VsaException (VsaError
.EngineClosed
);
213 throw new VsaException (VsaError
.EngineClosed
);
215 throw new VsaException (VsaError
.EngineBusy
);
216 else if (!initNewCalled
)
217 throw new VsaException (VsaError
.EngineNotInitialized
);
223 public bool IsRunning
{
226 throw new VsaException (VsaError
.EngineClosed
);
228 throw new VsaException (VsaError
.EngineBusy
);
229 else if (!initNewCalled
)
230 throw new VsaException (VsaError
.EngineNotInitialized
);
236 public IVsaItems Items
{
239 throw new VsaException (VsaError
.EngineClosed
);
241 throw new VsaException (VsaError
.EngineBusy
);
242 else if (!initNewCalled
)
243 throw new VsaException (VsaError
.EngineNotInitialized
);
245 items
= new VsaItems ((VsaEngine
) this);
251 public string Language
{
254 throw new VsaException (VsaError
.EngineClosed
);
256 throw new VsaException (VsaError
.EngineBusy
);
257 else if (!initNewCalled
)
258 throw new VsaException (VsaError
.EngineNotInitialized
);
264 // FIXME: research when LCIDNotSupported gets thrown.
268 throw new VsaException (VsaError
.EngineClosed
);
270 throw new VsaException (VsaError
.EngineBusy
);
271 else if (!initNewCalled
)
272 throw new VsaException (VsaError
.EngineNotInitialized
);
274 throw new VsaException (VsaError
.EngineRunning
);
281 throw new VsaException (VsaError
.EngineClosed
);
283 throw new VsaException (VsaError
.EngineBusy
);
284 else if (!initNewCalled
)
285 throw new VsaException (VsaError
.EngineNotInitialized
);
287 throw new VsaException (VsaError
.EngineRunning
);
294 // FIXME: we must throw VsaException, VsaError set to EngineNameInUse
295 // when setting and name is already in use.
299 throw new VsaException (VsaError
.EngineClosed
);
301 throw new VsaException (VsaError
.EngineBusy
);
302 else if (!initNewCalled
)
303 throw new VsaException (VsaError
.EngineNotInitialized
);
305 throw new VsaException (VsaError
.EngineRunning
);
312 throw new VsaException (VsaError
.EngineClosed
);
314 throw new VsaException (VsaError
.EngineBusy
);
315 else if (!initNewCalled
)
316 throw new VsaException (VsaError
.EngineNotInitialized
);
318 throw new VsaException (VsaError
.EngineRunning
);
324 // FIXME: We have to check - when setting - that the moniker
325 // is not already in use.
326 public string RootMoniker
{
329 throw new VsaException (VsaError
.EngineClosed
);
331 throw new VsaException (VsaError
.EngineBusy
);
337 if (monikerAlreadySet
)
338 throw new VsaException (VsaError
.RootMonikerAlreadySet
);
340 throw new VsaException (VsaError
.EngineClosed
);
342 throw new VsaException (VsaError
.EngineBusy
);
344 MonikerState state
= ValidateRootMoniker (value);
347 case MonikerState
.Valid
:
349 monikerAlreadySet
= true;
352 case MonikerState
.Invalid
:
353 throw new VsaException (VsaError
.RootMonikerInvalid
);
355 case MonikerState
.ProtocolInvalid
:
356 throw new VsaException (VsaError
.RootMonikerProtocolInvalid
);
362 internal static MonikerState
ValidateRootMoniker (string n
)
364 if (n
== null || n
== "" || n
.Length
> ROOT_MONIKER_MAX_SIZE
)
365 return MonikerState
.Invalid
;
368 Uri uri
= new Uri (n
);
369 string protocol
= uri
.Scheme
;
371 if (protocol
== "http" || protocol
== "file" ||
372 protocol
== "ftp" || protocol
== "gopher" ||
373 protocol
== "https" || protocol
== "mailto")
374 return MonikerState
.ProtocolInvalid
;
376 return MonikerState
.Valid
;
378 } catch (UriFormatException e
) {
379 return MonikerState
.Invalid
;
384 // FIXME: we must check - when setting - that the value is a valid
385 // namespace (research what does that mean :-)). If not the case
386 // VsaException must be thrown, set to VsaError.NamespaceInvalid
387 public string RootNamespace
{
390 throw new VsaException (VsaError
.EngineClosed
);
392 throw new VsaException (VsaError
.EngineBusy
);
393 else if (!initNewCalled
)
394 throw new VsaException (VsaError
.EngineNotInitialized
);
396 throw new VsaException (VsaError
.EngineRunning
);
398 return rootNamespace
;
403 throw new VsaException (VsaError
.EngineClosed
);
405 throw new VsaException (VsaError
.EngineBusy
);
406 else if (!initNewCalled
)
407 throw new VsaException (VsaError
.EngineNotInitialized
);
409 throw new VsaException (VsaError
.EngineRunning
);
411 rootNamespace
= value;
412 namespaceNotSet
= false;
416 public IVsaSite Site
{
419 throw new VsaException (VsaError
.EngineClosed
);
421 throw new VsaException (VsaError
.EngineBusy
);
422 else if (!monikerAlreadySet
)
423 throw new VsaException (VsaError
.RootMonikerNotSet
);
429 if (!monikerAlreadySet
)
430 throw new VsaException (VsaError
.RootMonikerNotSet
);
431 else if (siteAlreadySet
)
432 throw new VsaException (VsaError
.SiteAlreadySet
);
434 throw new VsaException (VsaError
.EngineClosed
);
436 throw new VsaException (VsaError
.EngineBusy
);
437 else if (value == null)
438 throw new VsaException (VsaError
.SiteInvalid
);
441 siteAlreadySet
= true;
446 // Version Format: Major.Minor.Revision.Build
448 public string Version
{
451 throw new VsaException (VsaError
.EngineClosed
);
453 throw new VsaException (VsaError
.EngineBusy
);
454 else if (!initNewCalled
)
455 throw new VsaException (VsaError
.EngineNotInitialized
);
461 public virtual void Close ()
466 throw new VsaException (VsaError
.EngineClosed
);
468 throw new VsaException (VsaError
.EngineBusy
);
476 // Count that AssemblyExpected exception may be thrown.
478 public virtual bool Compile ()
481 throw new VsaException (VsaError
.EngineClosed
);
483 throw new VsaException (VsaError
.EngineBusy
);
485 throw new VsaException (VsaError
.EngineEmpty
);
487 throw new VsaException (VsaError
.EngineRunning
);
488 else if (!initNewCalled
)
489 throw new VsaException (VsaError
.EngineNotInitialized
);
490 else if (namespaceNotSet
)
491 throw new VsaException (VsaError
.RootNamespaceNotSet
);
496 public virtual object GetOption (string name
)
501 opt
= GetSpecificOption (name
);
502 } catch (VsaException e
) {
508 protected abstract object GetSpecificOption (string name
);
510 protected abstract void SetSpecificOption (string name
, object val
);
512 public virtual void InitNew ()
515 throw new VsaException (VsaError
.EngineClosed
);
517 throw new VsaException (VsaError
.EngineBusy
);
518 else if (initNewCalled
)
519 throw new VsaException (VsaError
.EngineInitialized
);
520 else if (!monikerAlreadySet
)
521 throw new VsaException (VsaError
.RootMonikerNotSet
);
522 else if (!siteAlreadySet
)
523 throw new VsaException (VsaError
.SiteNotSet
);
525 initNewCalled
= true;
528 public virtual void LoadSourceState (IVsaPersistSite site
)
530 throw new NotImplementedException ();
533 public virtual void Reset ()
536 throw new VsaException (VsaError
.EngineClosed
);
538 throw new VsaException (VsaError
.EngineBusy
);
540 throw new VsaException (VsaError
.EngineNotRunning
);
546 public virtual void RevokeCache ()
548 throw new NotImplementedException ();
551 public virtual void Run ()
554 throw new VsaException (VsaError
.EngineClosed
);
556 throw new VsaException (VsaError
.EngineBusy
);
558 throw new VsaException (VsaError
.EngineRunning
);
559 else if (!monikerAlreadySet
)
560 throw new VsaException (VsaError
.RootMonikerNotSet
);
561 else if (!siteAlreadySet
)
562 throw new VsaException (VsaError
.SiteNotSet
);
563 else if (namespaceNotSet
)
564 throw new VsaException (VsaError
.RootNamespaceNotSet
);
569 public virtual void SetOption (string name
, object value)
574 public virtual void SaveCompiledState (out byte [] pe
, out byte [] debugInfo
)
576 throw new NotImplementedException ();
579 public virtual void SaveSourceState (IVsaPersistSite site
)
581 throw new NotImplementedException ();
584 public abstract bool IsValidIdentifier (string ident
);
586 internal bool Closed
{
587 get { return closed; }
590 internal bool Running
{
591 get { return running; }
598 internal bool InitNewCalled
{
599 get { return initNewCalled; }
600 set { initNewCalled = value; }
604 public class BaseVsaSite
: IVsaSite
{
606 public virtual byte [] Assembly
{
607 get { throw new NotImplementedException (); }
610 public virtual byte [] DebugInfo
{
611 get { throw new NotImplementedException (); }
614 public virtual void GetCompiledState (out byte [] pe
, out byte [] debugInfo
)
616 throw new NotImplementedException ();
619 public virtual object GetEventSourceInstance (string itemName
, string eventSourceName
)
621 throw new NotImplementedException ();
624 public virtual object GetGlobalInstance (string name
)
626 throw new NotImplementedException ();
629 public virtual void Notify (string notify
, object optional
)
631 throw new NotImplementedException ();
634 public virtual bool OnCompilerError (IVsaError error
)
636 throw new NotImplementedException ();
641 public abstract class BaseVsaStartup
{
643 public void SetSite (IVsaSite site
)
645 throw new NotImplementedException ();
648 public abstract void Startup ();
650 public abstract void Shutdown ();
653 internal enum MonikerState
{