The Labs \ Source Viewer \ SSCLI \ Microsoft.JScript \ Import

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. namespace Microsoft.JScript
  16. {
  17.    
  18.     using Microsoft.JScript.Vsa;
  19.     using System;
  20.     using System.Reflection;
  21.     using System.Reflection.Emit;
  22.    
  23.     public sealed class Import : AST
  24.     {
  25.         private string name;
  26.        
  27.         internal Import(Context context, AST name) : base(context)
  28.         {
  29.             if (name == null)
  30.                 //could happen if constructed while in error recovery mode
  31.                 return;
  32.             WrappedNamespace ns = name.EvaluateAsWrappedNamespace(true);
  33.             this.Engine.SetEnclosingContext(ns);
  34.             this.name = ns.name;
  35.         }
  36.        
  37.         internal override object Evaluate()
  38.         {
  39.             return new Completion();
  40.         }
  41.        
  42.         internal override AST PartiallyEvaluate()
  43.         {
  44.             return this;
  45.         }
  46.        
  47.         public static void JScriptImport(string name, VsaEngine engine)
  48.         {
  49.             int dotPos = name.IndexOf('.');
  50.             string rootName = dotPos > 0 ? name.Substring(0, dotPos) : name;
  51.             GlobalScope scope = ((IActivationObject)engine.ScriptObjectStackTop()).GetGlobalScope();
  52.             FieldInfo field = scope.GetLocalField(rootName);
  53.             if (field == null)
  54.                 field = scope.AddNewField(rootName, Namespace.GetNamespace(rootName, engine), FieldAttributes.Public | FieldAttributes.Literal);
  55.             engine.SetEnclosingContext(new WrappedNamespace(name, engine, false));
  56.         }
  57.        
  58.         internal override void TranslateToIL(ILGenerator il, Type rtype)
  59.         {
  60.         }
  61.        
  62.         internal override void TranslateToILInitializer(ILGenerator il)
  63.         {
  64.             il.Emit(OpCodes.Ldstr, this.name);
  65.             this.EmitILToLoadEngine(il);
  66.             il.Emit(OpCodes.Call, CompilerGlobals.jScriptImportMethod);
  67.         }
  68.        
  69.     }
  70.    
  71. }

Developer Fusion