Ejemplo n.º 1
0
        public Context(LensCompilerOptions options = null)
        {
            Options = options ?? new LensCompilerOptions();

            _DefinedTypes = new Dictionary<string, TypeEntity>();
            _DefinedProperties = new Dictionary<string, GlobalPropertyInfo>();

            Unique = new UniqueNameGenerator();

            if (Options.UseDefaultNamespaces)
            {
                Namespaces.Add("System", true);
                Namespaces.Add("System.Linq", true);
                Namespaces.Add("System.Text.RegularExpressions", true);
            }

            _AssemblyCache = new ReferencedAssemblyCache(Options.UseDefaultAssemblies);
            _ExtensionResolver = new ExtensionMethodResolver(Namespaces, _AssemblyCache);
            _TypeResolver = new TypeResolver(Namespaces, _AssemblyCache)
            {
                ExternalLookup = name =>
                {
                    TypeEntity ent;
                    _DefinedTypes.TryGetValue(name, out ent);
                    return ent == null ? null : ent.TypeBuilder;
                }
            };

            AssemblyName an;
            lock(typeof(Context))
                an = new AssemblyName(Unique.AssemblyName());

            if (Options.AllowSave)
            {
                if(string.IsNullOrEmpty(Options.FileName))
                    Options.FileName = an.Name + (Options.SaveAsExe ? ".exe" : ".dll");

                MainAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave);
                MainModule = MainAssembly.DefineDynamicModule(an.Name, Options.FileName);
            }
            else
            {
                MainAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
                MainModule = MainAssembly.DefineDynamicModule(an.Name);
            }

            ContextId = GlobalPropertyHelper.RegisterContext();

            MainType = CreateType(EntityNames.MainTypeName, prepare: false);
            MainType.Kind = TypeEntityKind.Main;
            MainType.Interfaces = new[] {typeof (IScript)};
            MainMethod = MainType.CreateMethod(EntityNames.RunMethodName, typeof(object), Type.EmptyTypes, false, true, false);

            if(Options.LoadStandardLibrary)
                initStdlib();

            InitSafeMode();
        }
Ejemplo n.º 2
0
        public Context(LensCompilerOptions options = null)
        {
            Options = options ?? new LensCompilerOptions();

            _definedTypes      = new Dictionary <string, TypeEntity>();
            _definedProperties = new Dictionary <string, GlobalPropertyInfo>();

            Unique = new UniqueNameGenerator();

            if (Options.UseDefaultNamespaces)
            {
                Namespaces.Add("System", true);
                Namespaces.Add("System.Linq", true);
                Namespaces.Add("System.Text.RegularExpressions", true);
            }

            AssemblyCache      = new ReferencedAssemblyCache(Options.UseDefaultAssemblies);
            _extensionResolver = new ExtensionMethodResolver(Namespaces, AssemblyCache);
            _typeResolver      = new TypeResolver(Namespaces, AssemblyCache)
            {
                ExternalLookup = name =>
                {
                    _definedTypes.TryGetValue(name, out var ent);
                    return(ent?.TypeBuilder);
                }
            };

            AssemblyName an;

            lock (typeof(Context))
                an = new AssemblyName(Unique.AssemblyName());

#if NET_CLASSIC
            if (Options.AllowSave)
            {
                if (string.IsNullOrEmpty(Options.FileName))
                {
                    Options.FileName = an.Name + (Options.SaveAsExe ? ".exe" : ".dll");
                }

                MainAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave);
                MainModule   = MainAssembly.DefineDynamicModule(an.Name, Options.FileName);
            }
            else
            {
                MainAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
                MainModule   = MainAssembly.DefineDynamicModule(an.Name);
            }
#else
            MainAssembly = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
            MainModule   = MainAssembly.DefineDynamicModule(an.Name);
#endif

            ContextId = GlobalPropertyHelper.RegisterContext();

            MainType            = CreateType(EntityNames.MainTypeName, prepare: false);
            MainType.Kind       = TypeEntityKind.Main;
            MainType.Interfaces = new[] { typeof(IScript) };
            MainMethod          = MainType.CreateMethod(EntityNames.RunMethodName, typeof(object), Type.EmptyTypes, false, true, false);

            if (Options.LoadStandardLibrary)
            {
                InitStdlib();
            }

            InitSafeMode();
        }